22package lightsail
33
44import (
5+ "context"
56 "errors"
67 "fmt"
78 "math/rand"
89 "strconv"
910 "time"
1011
11- "github.com/aws/aws-sdk-go/aws"
12- "github.com/aws/aws-sdk-go/aws/client "
13- "github.com/aws/aws-sdk-go/aws/request "
14- "github.com/aws/aws-sdk-go/aws/session "
15- "github.com/aws/aws-sdk-go/service/lightsail"
12+ "github.com/aws/aws-sdk-go-v2 /aws"
13+ "github.com/aws/aws-sdk-go-v2 /aws/retry "
14+ awsconfig "github.com/aws/aws-sdk-go-v2/config "
15+ "github.com/aws/aws-sdk-go-v2/service/lightsail "
16+ awstypes "github.com/aws/aws-sdk-go-v2 /service/lightsail/types "
1617 "github.com/go-acme/lego/v4/challenge/dns01"
1718 "github.com/go-acme/lego/v4/platform/config/env"
1819)
@@ -32,27 +33,6 @@ const (
3233 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
3334)
3435
35- // customRetryer implements the client.Retryer interface by composing the DefaultRetryer.
36- // It controls the logic for retrying recoverable request errors (e.g. when rate limits are exceeded).
37- type customRetryer struct {
38- client.DefaultRetryer
39- }
40-
41- // RetryRules overwrites the DefaultRetryer's method.
42- // It uses a basic exponential backoff algorithm that returns an initial
43- // delay of ~400ms with an upper limit of ~30 seconds which should prevent
44- // causing a high number of consecutive throttling errors.
45- // For reference: Route 53 enforces an account-wide(!) 5req/s query limit.
46- func (c customRetryer ) RetryRules (r * request.Request ) time.Duration {
47- retryCount := r .RetryCount
48- if retryCount > 7 {
49- retryCount = 7
50- }
51-
52- delay := (1 << uint (retryCount )) * (rand .Intn (50 ) + 200 )
53- return time .Duration (delay ) * time .Millisecond
54- }
55-
5636// Config is used to configure the creation of the DNSProvider.
5737type Config struct {
5838 DNSZone string
@@ -71,7 +51,7 @@ func NewDefaultConfig() *Config {
7151
7252// DNSProvider implements the challenge.Provider interface.
7353type DNSProvider struct {
74- client * lightsail.Lightsail
54+ client * lightsail.Client
7555 config * Config
7656}
7757
@@ -102,35 +82,55 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
10282 return nil , errors .New ("lightsail: the configuration of the DNS provider is nil" )
10383 }
10484
105- retryer := customRetryer {}
106- retryer .NumMaxRetries = maxRetries
107-
108- conf := aws .NewConfig ().WithRegion (config .Region )
109- sess , err := session .NewSession (request .WithRetryer (conf , retryer ))
85+ ctx := context .Background ()
86+
87+ cfg , err := awsconfig .LoadDefaultConfig (ctx ,
88+ awsconfig .WithRegion (config .Region ),
89+ awsconfig .WithRetryer (func () aws.Retryer {
90+ return retry .NewStandard (func (options * retry.StandardOptions ) {
91+ options .MaxAttempts = maxRetries
92+
93+ // It uses a basic exponential backoff algorithm that returns an initial
94+ // delay of ~400ms with an upper limit of ~30 seconds which should prevent
95+ // causing a high number of consecutive throttling errors.
96+ // For reference: Route 53 enforces an account-wide(!) 5req/s query limit.
97+ options .Backoff = retry .BackoffDelayerFunc (func (attempt int , err error ) (time.Duration , error ) {
98+ retryCount := attempt
99+ if retryCount > 7 {
100+ retryCount = 7
101+ }
102+
103+ delay := (1 << uint (retryCount )) * (rand .Intn (50 ) + 200 )
104+ return time .Duration (delay ) * time .Millisecond , nil
105+ })
106+ })
107+ }),
108+ )
110109 if err != nil {
111110 return nil , err
112111 }
113112
114113 return & DNSProvider {
115114 config : config ,
116- client : lightsail .New ( sess ),
115+ client : lightsail .NewFromConfig ( cfg ),
117116 }, nil
118117}
119118
120119// Present creates a TXT record using the specified parameters.
121- func (d * DNSProvider ) Present (domain , token , keyAuth string ) error {
120+ func (d * DNSProvider ) Present (domain , _ , keyAuth string ) error {
121+ ctx := context .Background ()
122122 info := dns01 .GetChallengeInfo (domain , keyAuth )
123123
124124 params := & lightsail.CreateDomainEntryInput {
125125 DomainName : aws .String (d .config .DNSZone ),
126- DomainEntry : & lightsail .DomainEntry {
126+ DomainEntry : & awstypes .DomainEntry {
127127 Name : aws .String (info .EffectiveFQDN ),
128128 Target : aws .String (strconv .Quote (info .Value )),
129129 Type : aws .String ("TXT" ),
130130 },
131131 }
132132
133- _ , err := d .client .CreateDomainEntry (params )
133+ _ , err := d .client .CreateDomainEntry (ctx , params )
134134 if err != nil {
135135 return fmt .Errorf ("lightsail: %w" , err )
136136 }
@@ -139,19 +139,20 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
139139}
140140
141141// CleanUp removes the TXT record matching the specified parameters.
142- func (d * DNSProvider ) CleanUp (domain , token , keyAuth string ) error {
142+ func (d * DNSProvider ) CleanUp (domain , _ , keyAuth string ) error {
143+ ctx := context .Background ()
143144 info := dns01 .GetChallengeInfo (domain , keyAuth )
144145
145146 params := & lightsail.DeleteDomainEntryInput {
146147 DomainName : aws .String (d .config .DNSZone ),
147- DomainEntry : & lightsail .DomainEntry {
148+ DomainEntry : & awstypes .DomainEntry {
148149 Name : aws .String (info .EffectiveFQDN ),
149150 Type : aws .String ("TXT" ),
150151 Target : aws .String (strconv .Quote (info .Value )),
151152 },
152153 }
153154
154- _ , err := d .client .DeleteDomainEntry (params )
155+ _ , err := d .client .DeleteDomainEntry (ctx , params )
155156 if err != nil {
156157 return fmt .Errorf ("lightsail: %w" , err )
157158 }
0 commit comments