diff --git a/providers/dns/lightsail/lightsail.go b/providers/dns/lightsail/lightsail.go index 2f6feddd..6a3089a1 100644 --- a/providers/dns/lightsail/lightsail.go +++ b/providers/dns/lightsail/lightsail.go @@ -4,6 +4,7 @@ package lightsail import ( "math/rand" + "os" "time" "github.com/aws/aws-sdk-go/aws" @@ -20,7 +21,8 @@ const ( // DNSProvider implements the acme.ChallengeProvider interface type DNSProvider struct { - client *lightsail.Lightsail + client *lightsail.Lightsail + dnsZone string } // customRetryer implements the client.Retryer interface by composing the @@ -61,11 +63,16 @@ func (c customRetryer) RetryRules(r *request.Request) time.Duration { func NewDNSProvider() (*DNSProvider, error) { r := customRetryer{} r.NumMaxRetries = maxRetries - config := request.WithRetryer(aws.NewConfig(), r) - client := lightsail.New(session.New(config)) + + config := aws.NewConfig().WithRegion("us-east-1") + sess, err := session.NewSession(request.WithRetryer(config, r)) + if err != nil { + return nil, err + } return &DNSProvider{ - client: client, + dnsZone: os.Getenv("DNS_ZONE"), + client: lightsail.New(sess), }, nil } @@ -73,6 +80,7 @@ func NewDNSProvider() (*DNSProvider, error) { func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) value = `"` + value + `"` + err := d.newTxtRecord(domain, fqdn, value) return err } @@ -82,7 +90,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { fqdn, value, _ := acme.DNS01Record(domain, keyAuth) value = `"` + value + `"` params := &lightsail.DeleteDomainEntryInput{ - DomainName: aws.String(domain), + DomainName: aws.String(d.dnsZone), DomainEntry: &lightsail.DomainEntry{ Name: aws.String(fqdn), Type: aws.String("TXT"), @@ -95,7 +103,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) newTxtRecord(domain string, fqdn string, value string) error { params := &lightsail.CreateDomainEntryInput{ - DomainName: aws.String(domain), + DomainName: aws.String(d.dnsZone), DomainEntry: &lightsail.DomainEntry{ Name: aws.String(fqdn), Target: aws.String(value), diff --git a/providers/dns/lightsail/lightsail_integration_test.go b/providers/dns/lightsail/lightsail_integration_test.go index 579780d4..a26d7868 100644 --- a/providers/dns/lightsail/lightsail_integration_test.go +++ b/providers/dns/lightsail/lightsail_integration_test.go @@ -26,7 +26,10 @@ func TestLightsailTTL(t *testing.T) { // we need a separate Lightshail client here as the one in the DNS provider is // unexported. fqdn := "_acme-challenge." + m["lightsailDomain"] - svc := lightsail.New(session.New()) + sess, err := session.NewSession() + require.NoError(t, err) + + svc := lightsail.New(sess) if err != nil { provider.CleanUp(m["lightsailDomain"], "foo", "bar") t.Fatal(err) diff --git a/providers/dns/lightsail/lightsail_test.go b/providers/dns/lightsail/lightsail_test.go index ee41073a..a846ff3d 100644 --- a/providers/dns/lightsail/lightsail_test.go +++ b/providers/dns/lightsail/lightsail_test.go @@ -9,7 +9,7 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/lightsail" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var ( @@ -30,7 +30,7 @@ func restoreEnv() { os.Setenv("AWS_HOSTED_ZONE_ID", lightsailZone) } -func makeLightsailProvider(ts *httptest.Server) *DNSProvider { +func makeLightsailProvider(ts *httptest.Server) (*DNSProvider, error) { config := &aws.Config{ Credentials: credentials.NewStaticCredentials("abc", "123", " "), Endpoint: aws.String(ts.URL), @@ -38,8 +38,13 @@ func makeLightsailProvider(ts *httptest.Server) *DNSProvider { MaxRetries: aws.Int(1), } - client := lightsail.New(session.New(config)) - return &DNSProvider{client: client} + sess, err := session.NewSession(config) + if err != nil { + return nil, err + } + + client := lightsail.New(sess) + return &DNSProvider{client: client}, nil } func TestCredentialsFromEnv(t *testing.T) { @@ -52,24 +57,27 @@ func TestCredentialsFromEnv(t *testing.T) { CredentialsChainVerboseErrors: aws.Bool(true), } - sess := session.New(config) - _, err := sess.Config.Credentials.Get() - assert.NoError(t, err, "Expected credentials to be set from environment") + sess, err := session.NewSession(config) + require.NoError(t, err) + + _, err = sess.Config.Credentials.Get() + require.NoError(t, err, "Expected credentials to be set from environment") } func TestLightsailPresent(t *testing.T) { - mockResponses := MockResponseMap{ - "/": MockResponse{StatusCode: 200, Body: ""}, + mockResponses := map[string]MockResponse{ + "/": {StatusCode: 200, Body: ""}, } ts := newMockServer(t, mockResponses) defer ts.Close() - provider := makeLightsailProvider(ts) + provider, err := makeLightsailProvider(ts) + require.NoError(t, err) domain := "example.com" keyAuth := "123456d==" - err := provider.Present(domain, "", keyAuth) - assert.NoError(t, err, "Expected Present to return no error") + err = provider.Present(domain, "", keyAuth) + require.NoError(t, err, "Expected Present to return no error") } diff --git a/providers/dns/lightsail/testutil_test.go b/providers/dns/lightsail/mock_server_test.go similarity index 79% rename from providers/dns/lightsail/testutil_test.go rename to providers/dns/lightsail/mock_server_test.go index 11141216..bba3b37a 100644 --- a/providers/dns/lightsail/testutil_test.go +++ b/providers/dns/lightsail/mock_server_test.go @@ -16,10 +16,7 @@ type MockResponse struct { Body string } -// MockResponseMap maps request paths to responses -type MockResponseMap map[string]MockResponse - -func newMockServer(t *testing.T, responses MockResponseMap) *httptest.Server { +func newMockServer(t *testing.T, responses map[string]MockResponse) *httptest.Server { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path resp, ok := responses[path]