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 (
"math/rand"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
@ -21,6 +22,7 @@ const (
// DNSProvider implements the acme.ChallengeProvider interface
type DNSProvider struct {
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),

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
// 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)

View file

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

View file

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