33package route53
44
55import (
6+ "errors"
67 "fmt"
78 "math/rand"
89 "os"
@@ -17,15 +18,30 @@ import (
1718 "github.com/xenolf/lego/acme"
1819)
1920
20- const (
21- maxRetries = 5
22- route53TTL = 10
23- )
21+ // Config is used to configure the creation of the DNSProvider
22+ type Config struct {
23+ MaxRetries int
24+ TTL int
25+ PropagationTimeout time.Duration
26+ PollingInterval time.Duration
27+ HostedZoneID string
28+ }
29+
30+ // NewDefaultConfig returns a default configuration for the DNSProvider
31+ func NewDefaultConfig () * Config {
32+ return & Config {
33+ MaxRetries : 5 ,
34+ TTL : 10 ,
35+ PropagationTimeout : time .Minute * 2 ,
36+ PollingInterval : time .Second * 4 ,
37+ HostedZoneID : os .Getenv ("AWS_HOSTED_ZONE_ID" ),
38+ }
39+ }
2440
2541// DNSProvider implements the acme.ChallengeProvider interface
2642type DNSProvider struct {
27- client * route53.Route53
28- hostedZoneID string
43+ client * route53.Route53
44+ config * Config
2945}
3046
3147// customRetryer implements the client.Retryer interface by composing the
@@ -65,35 +81,49 @@ func (d customRetryer) RetryRules(r *request.Request) time.Duration {
6581//
6682// See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk
6783func NewDNSProvider () (* DNSProvider , error ) {
68- hostedZoneID := os .Getenv ("AWS_HOSTED_ZONE_ID" )
84+ return NewDNSProviderConfig (NewDefaultConfig ())
85+ }
86+
87+ // NewDNSProviderConfig takes a given config ans returns a custom configured
88+ // DNSProvider instance
89+ func NewDNSProviderConfig (config * Config ) (* DNSProvider , error ) {
90+ if config == nil {
91+ return nil , errors .New ("the configuration of the Route53 DNS provider is nil" )
92+ }
6993
7094 r := customRetryer {}
71- r .NumMaxRetries = maxRetries
72- config := request .WithRetryer (aws .NewConfig (), r )
73- session , err := session .NewSessionWithOptions (session.Options {Config : * config })
95+ r .NumMaxRetries = config . MaxRetries
96+ sessionCfg := request .WithRetryer (aws .NewConfig (), r )
97+ session , err := session .NewSessionWithOptions (session.Options {Config : * sessionCfg })
7498 if err != nil {
7599 return nil , err
76100 }
77101 client := route53 .New (session )
78102
79103 return & DNSProvider {
80- client : client ,
81- hostedZoneID : hostedZoneID ,
104+ client : client ,
105+ config : config ,
82106 }, nil
83107}
84108
109+ // Timeout returns the timeout and interval to use when checking for DNS
110+ // propagation.
111+ func (r * DNSProvider ) Timeout () (timeout , interval time.Duration ) {
112+ return r .config .PropagationTimeout , r .config .PollingInterval
113+ }
114+
85115// Present creates a TXT record using the specified parameters
86116func (r * DNSProvider ) Present (domain , token , keyAuth string ) error {
87117 fqdn , value , _ := acme .DNS01Record (domain , keyAuth )
88118 value = `"` + value + `"`
89- return r .changeRecord ("UPSERT" , fqdn , value , route53TTL )
119+ return r .changeRecord ("UPSERT" , fqdn , value , r . config . TTL )
90120}
91121
92122// CleanUp removes the TXT record matching the specified parameters
93123func (r * DNSProvider ) CleanUp (domain , token , keyAuth string ) error {
94124 fqdn , value , _ := acme .DNS01Record (domain , keyAuth )
95125 value = `"` + value + `"`
96- return r .changeRecord ("DELETE" , fqdn , value , route53TTL )
126+ return r .changeRecord ("DELETE" , fqdn , value , r . config . TTL )
97127}
98128
99129func (r * DNSProvider ) changeRecord (action , fqdn , value string , ttl int ) error {
@@ -123,7 +153,7 @@ func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
123153
124154 statusID := resp .ChangeInfo .Id
125155
126- return acme .WaitFor (120 * time . Second , 4 * time . Second , func () (bool , error ) {
156+ return acme .WaitFor (r . config . PropagationTimeout , r . config . PollingInterval , func () (bool , error ) {
127157 reqParams := & route53.GetChangeInput {
128158 Id : statusID ,
129159 }
@@ -139,8 +169,8 @@ func (r *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
139169}
140170
141171func (r * DNSProvider ) getHostedZoneID (fqdn string ) (string , error ) {
142- if r .hostedZoneID != "" {
143- return r .hostedZoneID , nil
172+ if r .config . HostedZoneID != "" {
173+ return r .config . HostedZoneID , nil
144174 }
145175
146176 authZone , err := acme .FindZoneByFqdn (fqdn , acme .RecursiveNameservers )
0 commit comments