Skip to content

Commit

Permalink
Route53: make provider timeouts configurable (go-acme#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mongey authored and ldez committed Jul 18, 2018
1 parent baad3de commit 8b67015
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
64 changes: 47 additions & 17 deletions providers/dns/route53/route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package route53

import (
"errors"
"fmt"
"math/rand"
"os"
Expand All @@ -17,15 +18,30 @@ import (
"github.com/xenolf/lego/acme"
)

const (
maxRetries = 5
route53TTL = 10
)
// Config is used to configure the creation of the DNSProvider
type Config struct {
MaxRetries int
TTL int
PropagationTimeout time.Duration
PollingInterval time.Duration
HostedZoneID string
}

// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
MaxRetries: 5,
TTL: 10,
PropagationTimeout: time.Minute * 2,
PollingInterval: time.Second * 4,
HostedZoneID: os.Getenv("AWS_HOSTED_ZONE_ID"),
}
}

// DNSProvider implements the acme.ChallengeProvider interface
type DNSProvider struct {
client *route53.Route53
hostedZoneID string
client *route53.Route53
config *Config
}

// customRetryer implements the client.Retryer interface by composing the
Expand Down Expand Up @@ -65,35 +81,49 @@ func (d customRetryer) RetryRules(r *request.Request) time.Duration {
//
// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
func NewDNSProvider() (*DNSProvider, error) {
hostedZoneID := os.Getenv("AWS_HOSTED_ZONE_ID")
return NewDNSProviderConfig(NewDefaultConfig())
}

// NewDNSProviderConfig takes a given config ans returns a custom configured
// DNSProvider instance
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("the configuration of the Route53 DNS provider is nil")
}

r := customRetryer{}
r.NumMaxRetries = maxRetries
config := request.WithRetryer(aws.NewConfig(), r)
session, err := session.NewSessionWithOptions(session.Options{Config: *config})
r.NumMaxRetries = config.MaxRetries
sessionCfg := request.WithRetryer(aws.NewConfig(), r)
session, err := session.NewSessionWithOptions(session.Options{Config: *sessionCfg})
if err != nil {
return nil, err
}
client := route53.New(session)

return &DNSProvider{
client: client,
hostedZoneID: hostedZoneID,
client: client,
config: config,
}, nil
}

// Timeout returns the timeout and interval to use when checking for DNS
// propagation.
func (r *DNSProvider) Timeout() (timeout, interval time.Duration) {
return r.config.PropagationTimeout, r.config.PollingInterval
}

// Present creates a TXT record using the specified parameters
func (r *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
value = `"` + value + `"`
return r.changeRecord("UPSERT", fqdn, value, route53TTL)
return r.changeRecord("UPSERT", fqdn, value, r.config.TTL)
}

// CleanUp removes the TXT record matching the specified parameters
func (r *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
value = `"` + value + `"`
return r.changeRecord("DELETE", fqdn, value, route53TTL)
return r.changeRecord("DELETE", fqdn, value, r.config.TTL)
}

func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
Expand Down Expand Up @@ -123,7 +153,7 @@ func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {

statusID := resp.ChangeInfo.Id

return acme.WaitFor(120*time.Second, 4*time.Second, func() (bool, error) {
return acme.WaitFor(r.config.PropagationTimeout, r.config.PollingInterval, func() (bool, error) {
reqParams := &route53.GetChangeInput{
Id: statusID,
}
Expand All @@ -139,8 +169,8 @@ func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
}

func (r *DNSProvider) getHostedZoneID(fqdn string) (string, error) {
if r.hostedZoneID != "" {
return r.hostedZoneID, nil
if r.config.HostedZoneID != "" {
return r.config.HostedZoneID, nil
}

authZone, err := acme.FindZoneByFqdn(fqdn, acme.RecursiveNameservers)
Expand Down
3 changes: 2 additions & 1 deletion providers/dns/route53/route53_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ func makeRoute53Provider(ts *httptest.Server) *DNSProvider {
}

client := route53.New(session.New(config))
return &DNSProvider{client: client}
cfg := NewDefaultConfig()
return &DNSProvider{client: client, config: cfg}
}

func TestCredentialsFromEnv(t *testing.T) {
Expand Down

0 comments on commit 8b67015

Please sign in to comment.