From 358b2dc37a04bc817fc006059cd028f71c2f79d5 Mon Sep 17 00:00:00 2001 From: Robert Navarro Date: Fri, 5 May 2023 09:52:44 -0700 Subject: [PATCH] Implement insecure requests functionality (#2) --- .env.example | 3 ++- client.go | 19 +++++++++++++++++-- provider.go | 8 ++++---- provider_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index a76b1dd..45c324b 100644 --- a/.env.example +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/client.go b/client.go index 1e723f4..c2da6a1 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package directadmin import ( "context" + "crypto/tls" "encoding/json" "fmt" "github.com/libdns/libdns" @@ -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 @@ -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 diff --git a/provider.go b/provider.go index c5a7a92..e962243 100644 --- a/provider.go +++ b/provider.go @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/provider_test.go b/provider_test.go index d02c644..aeb697f 100644 --- a/provider_test.go +++ b/provider_test.go @@ -6,6 +6,7 @@ import ( "github.com/joho/godotenv" "github.com/libdns/libdns" "os" + "strconv" "testing" "time" ) @@ -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 { @@ -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() @@ -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