Skip to content

Commit

Permalink
Implement insecure requests functionality (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnavarro authored May 5, 2023
1 parent 505020a commit 358b2dc
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
LIBDNS_DA_TEST_ZONE=domain.com
LIBDNS_DA_TEST_ZONE=domain.com.
LIBDNS_DA_TEST_SERVER_URL=https://da.domain.com:2222
LIBDNS_DA_TEST_INSECURE_SERVER_URL=https://1.1.1.1:2222
LIBDNS_DA_TEST_USER=admin
LIBDNS_DA_TEST_LOGIN_KEY=MySecretKey
19 changes: 17 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package directadmin

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"github.com/libdns/libdns"
Expand Down Expand Up @@ -42,7 +43,14 @@ func (p *Provider) getZoneRecords(ctx context.Context, zone string) ([]libdns.Re

req.SetBasicAuth(p.User, p.LoginKey)

resp, err := http.DefaultClient.Do(req)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: p.InsecureRequests,
},
}}

resp, err := client.Do(req)
if err != nil {
fmt.Printf("[%s] failed to execute request: %v\n", p.caller(callerSkipDepth), err)
return nil, err
Expand Down Expand Up @@ -217,7 +225,14 @@ func (p *Provider) executeRequest(ctx context.Context, method, url string) error

req.SetBasicAuth(p.User, p.LoginKey)

resp, err := http.DefaultClient.Do(req)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: p.InsecureRequests,
},
}}

resp, err := client.Do(req)
if err != nil {
fmt.Printf("[%s] failed to execute request: %v\n", p.caller(callerSkipDepth), err)
return err
Expand Down
8 changes: 4 additions & 4 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Provider struct {

// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
zone = strings.TrimRight(zone, ".")
zone = strings.TrimSuffix(zone, ".")

records, err := p.getZoneRecords(ctx, zone)
if err != nil {
Expand All @@ -58,7 +58,7 @@ func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record

// AppendRecords adds records to the zone. It returns the records that were added.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = strings.TrimRight(zone, ".")
zone = strings.TrimSuffix(zone, ".")

var created []libdns.Record
for _, rec := range records {
Expand All @@ -75,7 +75,7 @@ func (p *Provider) AppendRecords(ctx context.Context, zone string, records []lib
// SetRecords sets the records in the zone, either by updating existing records or creating new ones.
// It returns the updated records.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = strings.TrimRight(zone, ".")
zone = strings.TrimSuffix(zone, ".")

var updated []libdns.Record
for _, rec := range records {
Expand All @@ -91,7 +91,7 @@ func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns

// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = strings.TrimRight(zone, ".")
zone = strings.TrimSuffix(zone, ".")

var deleted []libdns.Record
for _, rec := range records {
Expand Down
46 changes: 44 additions & 2 deletions provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/joho/godotenv"
"github.com/libdns/libdns"
"os"
"strconv"
"testing"
"time"
)
Expand All @@ -19,15 +20,29 @@ func initProvider() (*Provider, string) {

zone := envOrFail("LIBDNS_DA_TEST_ZONE")

insecureRequest, err := strconv.ParseBool(defaultEnv("LIBDNS_DA_TEST_INSECURE_REQUESTS", "false"))
if err != nil {
insecureRequest = false
}

provider := &Provider{
ServerURL: envOrFail("LIBDNS_DA_TEST_SERVER_URL"),
User: envOrFail("LIBDNS_DA_TEST_USER"),
LoginKey: envOrFail("LIBDNS_DA_TEST_LOGIN_KEY"),
InsecureRequests: true,
InsecureRequests: insecureRequest,
}
return provider, zone
}

func defaultEnv(key, fallback string) string {
val := os.Getenv(key)
if len(val) == 0 {
return fallback
}

return val
}

func envOrFail(key string) string {
val := os.Getenv(key)
if len(val) == 0 {
Expand Down Expand Up @@ -64,6 +79,31 @@ func TestProvider_GetRecords(t *testing.T) {
fmt.Println()
}

func TestProvider_InsecureGetRecords(t *testing.T) {
ctx := context.TODO()

// Configure the DNS provider
provider, zone := initProvider()
provider.ServerURL = envOrFail("LIBDNS_DA_TEST_INSECURE_SERVER_URL")
provider.InsecureRequests = true

// list records
records, err := provider.GetRecords(ctx, zone)

if len(records) == 0 {
t.Errorf("expected >0 records")
}

if err != nil {
t.Error(err)
}

// Hack to work around "unsupported record conversion of type SRV: _xmpp._tcp"
// output not generating a new line. This breaks GoLands test results output
// https://stackoverflow.com/a/68607772/95790
fmt.Println()
}

func TestProvider_AppendRecords(t *testing.T) {
ctx := context.TODO()

Expand Down Expand Up @@ -169,7 +209,9 @@ func TestProvider_DotZoneAppendRecords(t *testing.T) {

// Configure the DNS provider
provider, zone := initProvider()
zone = zone + "."
if zone[len(zone)-1:] != "." {
zone = zone + "."
}

var tests = []struct {
records []libdns.Record
Expand Down

0 comments on commit 358b2dc

Please sign in to comment.