diff --git a/acme/dns_challenge_cloudflare.go b/acme/dns_challenge_cloudflare.go index 9418dfc2..8fbe947a 100644 --- a/acme/dns_challenge_cloudflare.go +++ b/acme/dns_challenge_cloudflare.go @@ -20,7 +20,7 @@ type DNSProviderCloudFlare struct { // variables CLOUDFLARE_EMAIL and CLOUDFLARE_API_KEY. func NewDNSProviderCloudFlare(cloudflareEmail, cloudflareKey string) (*DNSProviderCloudFlare, error) { if cloudflareEmail == "" || cloudflareKey == "" { - cloudflareEmail, cloudflareKey = envAuth() + cloudflareEmail, cloudflareKey = cloudflareEnvAuth() if cloudflareEmail == "" || cloudflareKey == "" { return nil, fmt.Errorf("CloudFlare credentials missing") } @@ -151,7 +151,7 @@ func sanitizeTTL(ttl int) int { } } -func envAuth() (email, apiKey string) { +func cloudflareEnvAuth() (email, apiKey string) { email = os.Getenv("CLOUDFLARE_EMAIL") apiKey = os.Getenv("CLOUDFLARE_API_KEY") if len(email) == 0 || len(apiKey) == 0 { diff --git a/acme/dns_challenge_dnsimple.go b/acme/dns_challenge_dnsimple.go index 754cce63..fce5bee1 100644 --- a/acme/dns_challenge_dnsimple.go +++ b/acme/dns_challenge_dnsimple.go @@ -2,6 +2,7 @@ package acme import ( "fmt" + "os" "github.com/weppos/go-dnsimple/dnsimple" ) @@ -12,10 +13,14 @@ type DNSProviderDNSimple struct { } // NewDNSProviderDNSimple returns a DNSProviderDNSimple instance with a configured dnsimple client. -// Authentication is either done using the passed credentials. +// Authentication is either done using the passed credentials or - when empty - using the environment +// variables DNSIMPLE_EMAIL and DNSIMPLE_API_KEY. func NewDNSProviderDNSimple(dnsimpleEmail, dnsimpleApiKey string) (*DNSProviderDNSimple, error) { if dnsimpleEmail == "" || dnsimpleApiKey == "" { - return nil, fmt.Errorf("DNSimple credentials missing") + dnsimpleEmail, dnsimpleApiKey = dnsimpleEnvAuth() + if dnsimpleEmail == "" || dnsimpleApiKey == "" { + return nil, fmt.Errorf("DNSimple credentials missing") + } } c := &DNSProviderDNSimple{ @@ -34,3 +39,12 @@ func (c *DNSProviderDNSimple) Present(domain, token, keyAuth string) error { func (c *DNSProviderDNSimple) CleanUp(domain, token, keyAuth string) error { return nil } + +func dnsimpleEnvAuth() (email, apiKey string) { + email = os.Getenv("DNSIMPLE_EMAIL") + apiKey = os.Getenv("DNSIMPLE_API_KEY") + if len(email) == 0 || len(apiKey) == 0 { + return "", "" + } + return +} diff --git a/acme/dns_challenge_dnsimple_test.go b/acme/dns_challenge_dnsimple_test.go index 6fa1b76f..b8234d6f 100644 --- a/acme/dns_challenge_dnsimple_test.go +++ b/acme/dns_challenge_dnsimple_test.go @@ -1,17 +1,46 @@ package acme import ( + "os" "testing" "github.com/stretchr/testify/assert" ) +var ( + dnsimpleEmail string + dnsimpleAPIKey string +) + +func init() { + dnsimpleEmail = os.Getenv("DNSIMPLE_EMAIL") + dnsimpleAPIKey = os.Getenv("DNSIMPLE_API_KEY") +} + +func restoreDNSimpleEnv() { + os.Setenv("DNSIMPLE_EMAIL", dnsimpleEmail) + os.Setenv("DNSIMPLE_API_KEY", dnsimpleAPIKey) +} + func TestNewDNSProviderDNSimpleValid(t *testing.T) { + os.Setenv("DNSIMPLE_EMAIL", "") + os.Setenv("DNSIMPLE_API_KEY", "") _, err := NewDNSProviderDNSimple("example@example.com", "123") assert.NoError(t, err) + restoreDNSimpleEnv() +} +func TestNewDNSProviderDNSimpleValidEnv(t *testing.T) { + os.Setenv("DNSIMPLE_EMAIL", "example@example.com") + os.Setenv("DNSIMPLE_API_KEY", "123") + _, err := NewDNSProviderDNSimple("", "") + assert.NoError(t, err) + restoreDNSimpleEnv() } func TestNewDNSProviderDNSimpleMissingCredErr(t *testing.T) { + os.Setenv("DNSIMPLE_EMAIL", "") + os.Setenv("DNSIMPLE_API_KEY", "") _, err := NewDNSProviderDNSimple("", "") assert.EqualError(t, err, "DNSimple credentials missing") + restoreDNSimpleEnv() }