Lightsail: Fix Domain does not exist error (#512)

This commit is contained in:
Derek Chen 2018-07-01 08:24:20 +08:00 committed by Ludovic Fernandez
parent f9c0fbd298
commit 3a1c6202f6
4 changed files with 39 additions and 23 deletions

View file

@ -4,6 +4,7 @@ package lightsail
import ( import (
"math/rand" "math/rand"
"os"
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
@ -20,7 +21,8 @@ const (
// DNSProvider implements the acme.ChallengeProvider interface // DNSProvider implements the acme.ChallengeProvider interface
type DNSProvider struct { type DNSProvider struct {
client *lightsail.Lightsail client *lightsail.Lightsail
dnsZone string
} }
// customRetryer implements the client.Retryer interface by composing the // 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) { func NewDNSProvider() (*DNSProvider, error) {
r := customRetryer{} r := customRetryer{}
r.NumMaxRetries = maxRetries 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{ return &DNSProvider{
client: client, dnsZone: os.Getenv("DNS_ZONE"),
client: lightsail.New(sess),
}, nil }, nil
} }
@ -73,6 +80,7 @@ func NewDNSProvider() (*DNSProvider, error) {
func (d *DNSProvider) Present(domain, token, keyAuth string) error { func (d *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value, _ := acme.DNS01Record(domain, keyAuth) fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
value = `"` + value + `"` value = `"` + value + `"`
err := d.newTxtRecord(domain, fqdn, value) err := d.newTxtRecord(domain, fqdn, value)
return err return err
} }
@ -82,7 +90,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, value, _ := acme.DNS01Record(domain, keyAuth) fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
value = `"` + value + `"` value = `"` + value + `"`
params := &lightsail.DeleteDomainEntryInput{ params := &lightsail.DeleteDomainEntryInput{
DomainName: aws.String(domain), DomainName: aws.String(d.dnsZone),
DomainEntry: &lightsail.DomainEntry{ DomainEntry: &lightsail.DomainEntry{
Name: aws.String(fqdn), Name: aws.String(fqdn),
Type: aws.String("TXT"), 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 { func (d *DNSProvider) newTxtRecord(domain string, fqdn string, value string) error {
params := &lightsail.CreateDomainEntryInput{ params := &lightsail.CreateDomainEntryInput{
DomainName: aws.String(domain), DomainName: aws.String(d.dnsZone),
DomainEntry: &lightsail.DomainEntry{ DomainEntry: &lightsail.DomainEntry{
Name: aws.String(fqdn), Name: aws.String(fqdn),
Target: aws.String(value), Target: aws.String(value),

View file

@ -26,7 +26,10 @@ func TestLightsailTTL(t *testing.T) {
// we need a separate Lightshail client here as the one in the DNS provider is // we need a separate Lightshail client here as the one in the DNS provider is
// unexported. // unexported.
fqdn := "_acme-challenge." + m["lightsailDomain"] 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 { if err != nil {
provider.CleanUp(m["lightsailDomain"], "foo", "bar") provider.CleanUp(m["lightsailDomain"], "foo", "bar")
t.Fatal(err) t.Fatal(err)

View file

@ -9,7 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lightsail" "github.com/aws/aws-sdk-go/service/lightsail"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
var ( var (
@ -30,7 +30,7 @@ func restoreEnv() {
os.Setenv("AWS_HOSTED_ZONE_ID", lightsailZone) os.Setenv("AWS_HOSTED_ZONE_ID", lightsailZone)
} }
func makeLightsailProvider(ts *httptest.Server) *DNSProvider { func makeLightsailProvider(ts *httptest.Server) (*DNSProvider, error) {
config := &aws.Config{ config := &aws.Config{
Credentials: credentials.NewStaticCredentials("abc", "123", " "), Credentials: credentials.NewStaticCredentials("abc", "123", " "),
Endpoint: aws.String(ts.URL), Endpoint: aws.String(ts.URL),
@ -38,8 +38,13 @@ func makeLightsailProvider(ts *httptest.Server) *DNSProvider {
MaxRetries: aws.Int(1), MaxRetries: aws.Int(1),
} }
client := lightsail.New(session.New(config)) sess, err := session.NewSession(config)
return &DNSProvider{client: client} if err != nil {
return nil, err
}
client := lightsail.New(sess)
return &DNSProvider{client: client}, nil
} }
func TestCredentialsFromEnv(t *testing.T) { func TestCredentialsFromEnv(t *testing.T) {
@ -52,24 +57,27 @@ func TestCredentialsFromEnv(t *testing.T) {
CredentialsChainVerboseErrors: aws.Bool(true), CredentialsChainVerboseErrors: aws.Bool(true),
} }
sess := session.New(config) sess, err := session.NewSession(config)
_, err := sess.Config.Credentials.Get() require.NoError(t, err)
assert.NoError(t, err, "Expected credentials to be set from environment")
_, err = sess.Config.Credentials.Get()
require.NoError(t, err, "Expected credentials to be set from environment")
} }
func TestLightsailPresent(t *testing.T) { func TestLightsailPresent(t *testing.T) {
mockResponses := MockResponseMap{ mockResponses := map[string]MockResponse{
"/": MockResponse{StatusCode: 200, Body: ""}, "/": {StatusCode: 200, Body: ""},
} }
ts := newMockServer(t, mockResponses) ts := newMockServer(t, mockResponses)
defer ts.Close() defer ts.Close()
provider := makeLightsailProvider(ts) provider, err := makeLightsailProvider(ts)
require.NoError(t, err)
domain := "example.com" domain := "example.com"
keyAuth := "123456d==" keyAuth := "123456d=="
err := provider.Present(domain, "", keyAuth) err = provider.Present(domain, "", keyAuth)
assert.NoError(t, err, "Expected Present to return no error") require.NoError(t, err, "Expected Present to return no error")
} }

View file

@ -16,10 +16,7 @@ type MockResponse struct {
Body string Body string
} }
// MockResponseMap maps request paths to responses func newMockServer(t *testing.T, responses map[string]MockResponse) *httptest.Server {
type MockResponseMap map[string]MockResponse
func newMockServer(t *testing.T, responses MockResponseMap) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
resp, ok := responses[path] resp, ok := responses[path]