DNSimpleProvider: fetch credentials from env

I also had to rename the `envAuth()` in the Cloudflare implementation
to avoid the "redeclared" error

    acme/dns_challenge_dnsimple.go:41: envAuth redeclared in this block
        previous declaration at acme/dns_challenge_cloudflare.go:154
This commit is contained in:
Simone Carletti 2016-01-26 12:42:44 +01:00
parent bcfce0809a
commit 6a3297e36f
3 changed files with 47 additions and 4 deletions

View file

@ -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 {

View file

@ -2,6 +2,7 @@ package acme
import (
"fmt"
"os"
"github.com/weppos/go-dnsimple/dnsimple"
)
@ -12,11 +13,15 @@ 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 == "" {
dnsimpleEmail, dnsimpleApiKey = dnsimpleEnvAuth()
if dnsimpleEmail == "" || dnsimpleApiKey == "" {
return nil, fmt.Errorf("DNSimple credentials missing")
}
}
c := &DNSProviderDNSimple{
client: dnsimple.NewClient(dnsimpleApiKey, dnsimpleEmail),
@ -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
}

View file

@ -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()
}