From 8e9c5ac3e6bf1392a581eece36752e7360a3414b Mon Sep 17 00:00:00 2001 From: Daniel Alan Miller Date: Thu, 26 Apr 2018 01:12:41 +1000 Subject: [PATCH] Adding output of which envvars are missing in Cloudflare and Azure (#537) * Adding output of which envvars are missing in Cloudflare dns provider * go fmt, duh * Fixing & adding test(s) * Adding azure missing env vars checking * Fixing test * Doh, fixing up expected output --- providers/dns/azure/azure.go | 12 +++++++++--- providers/dns/azure/azure_test.go | 2 +- providers/dns/cloudflare/cloudflare.go | 10 +++++++++- providers/dns/cloudflare/cloudflare_test.go | 9 ++++++++- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/providers/dns/azure/azure.go b/providers/dns/azure/azure.go index cc15ca7e4..9022af477 100644 --- a/providers/dns/azure/azure.go +++ b/providers/dns/azure/azure.go @@ -28,7 +28,7 @@ type DNSProvider struct { tenantId string resourceGroup string - context context.Context + context context.Context } // NewDNSProvider returns a DNSProvider instance configured for azure. @@ -47,7 +47,13 @@ func NewDNSProvider() (*DNSProvider, error) { // DNSProvider instance configured for azure. func NewDNSProviderCredentials(clientId, clientSecret, subscriptionId, tenantId, resourceGroup string) (*DNSProvider, error) { if clientId == "" || clientSecret == "" || subscriptionId == "" || tenantId == "" || resourceGroup == "" { - return nil, fmt.Errorf("Azure configuration missing") + missingEnvVars := []string{} + for _, envVar := range []string{"AZURE_CLIENT_ID", "AZURE_CLIENT_SECRET", "AZURE_SUBSCRIPTION_ID", "AZURE_TENANT_ID", "AZURE_RESOURCE_GROUP"} { + if os.Getenv(envVar) == "" { + missingEnvVars = append(missingEnvVars, envVar) + } + } + return nil, fmt.Errorf("Azure configuration missing: %s", strings.Join(missingEnvVars, ",")) } return &DNSProvider{ @@ -57,7 +63,7 @@ func NewDNSProviderCredentials(clientId, clientSecret, subscriptionId, tenantId, tenantId: tenantId, resourceGroup: resourceGroup, // TODO: A timeout can be added here for cancellation purposes. - context: context.Background(), + context: context.Background(), }, nil } diff --git a/providers/dns/azure/azure_test.go b/providers/dns/azure/azure_test.go index db55f578a..3eeb10fc9 100644 --- a/providers/dns/azure/azure_test.go +++ b/providers/dns/azure/azure_test.go @@ -58,7 +58,7 @@ func TestNewDNSProviderValidEnv(t *testing.T) { func TestNewDNSProviderMissingCredErr(t *testing.T) { os.Setenv("AZURE_SUBSCRIPTION_ID", "") _, err := NewDNSProvider() - assert.EqualError(t, err, "Azure configuration missing") + assert.EqualError(t, err, "Azure configuration missing: AZURE_CLIENT_ID,AZURE_CLIENT_SECRET,AZURE_SUBSCRIPTION_ID,AZURE_TENANT_ID,AZURE_RESOURCE_GROUP") restoreAzureEnv() } diff --git a/providers/dns/cloudflare/cloudflare.go b/providers/dns/cloudflare/cloudflare.go index 84952238d..d62b26f01 100644 --- a/providers/dns/cloudflare/cloudflare.go +++ b/providers/dns/cloudflare/cloudflare.go @@ -9,6 +9,7 @@ import ( "io" "net/http" "os" + "strings" "time" "github.com/xenolf/lego/acme" @@ -37,7 +38,14 @@ func NewDNSProvider() (*DNSProvider, error) { // DNSProvider instance configured for cloudflare. func NewDNSProviderCredentials(email, key string) (*DNSProvider, error) { if email == "" || key == "" { - return nil, fmt.Errorf("CloudFlare credentials missing") + missingEnvVars := []string{} + if email == "" { + missingEnvVars = append(missingEnvVars, "CLOUDFLARE_EMAIL") + } + if key == "" { + missingEnvVars = append(missingEnvVars, "CLOUDFLARE_API_KEY") + } + return nil, fmt.Errorf("CloudFlare credentials missing: %s", strings.Join(missingEnvVars, ",")) } return &DNSProvider{ diff --git a/providers/dns/cloudflare/cloudflare_test.go b/providers/dns/cloudflare/cloudflare_test.go index 19b5a40b9..9fab1622d 100644 --- a/providers/dns/cloudflare/cloudflare_test.go +++ b/providers/dns/cloudflare/cloudflare_test.go @@ -49,10 +49,17 @@ func TestNewDNSProviderMissingCredErr(t *testing.T) { os.Setenv("CLOUDFLARE_EMAIL", "") os.Setenv("CLOUDFLARE_API_KEY", "") _, err := NewDNSProvider() - assert.EqualError(t, err, "CloudFlare credentials missing") + assert.EqualError(t, err, "CloudFlare credentials missing: CLOUDFLARE_EMAIL,CLOUDFLARE_API_KEY") restoreCloudFlareEnv() } +func TestNewDNSProviderMissingCredErrSingle(t *testing.T){ + os.Setenv("CLOUDFLARE_EMAIL", "awesome@possum.com") + _, err:= NewDNSProvider() + assert.EqualError(t, err, "CloudFlare credentials missing: CLOUDFLARE_API_KEY") + restoreCloudFlareEnv() +} + func TestCloudFlarePresent(t *testing.T) { if !cflareLiveTest { t.Skip("skipping live test")