@@ -9,10 +9,12 @@ import (
99 "fmt"
1010 "log"
1111 "os"
12+ "time"
1213
1314 "github.com/DefangLabs/cloudacme/aws/acm"
1415 "github.com/DefangLabs/cloudacme/aws/alb"
1516 awsalb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
17+ "github.com/aws/smithy-go"
1618 "github.com/mholt/acmez"
1719 "go.uber.org/zap"
1820)
@@ -64,9 +66,23 @@ func UpdateAcmeCertificate(ctx context.Context, albArn, domain string, solver ac
6466
6567func GetExistingCertificate (ctx context.Context , albArn , domain string ) (string , * x509.Certificate , error ) {
6668 // Find the certificate to update from all the certificates attached to the ALB
67- certArns , err := alb .GetAlbCerts (ctx , albArn )
68- if err != nil {
69- return "" , nil , fmt .Errorf ("failed to get ALB certificates: %w" , err )
69+ var certArns []string
70+ var err error
71+ for i := 0 ; ; i ++ {
72+ certArns , err = alb .GetAlbCerts (ctx , albArn )
73+ if err != nil {
74+ var apiErr smithy.APIError
75+ if errors .As (err , & apiErr ) && apiErr .ErrorCode () == "AccessDenied" {
76+ log .Printf ("Access denied to ALB %v, retrying (%d/10)..." , albArn , i + 1 )
77+ if i >= 10 {
78+ return "" , nil , fmt .Errorf ("access denied to ALB %v: %w" , albArn , err )
79+ }
80+ SleepWithContext (ctx , 10 * time .Second )
81+ continue
82+ }
83+ return "" , nil , fmt .Errorf ("failed to get ALB certificates: %w" , err )
84+ }
85+ break
7086 }
7187
7288 var getCertErrs []error
@@ -139,3 +155,14 @@ func getAccountKey() (*ecdsa.PrivateKey, error) {
139155 }
140156 return key , nil
141157}
158+
159+ func SleepWithContext (ctx context.Context , d time.Duration ) error {
160+ timer := time .NewTimer (d )
161+ defer timer .Stop ()
162+ select {
163+ case <- timer .C :
164+ return nil
165+ case <- ctx .Done ():
166+ return ctx .Err ()
167+ }
168+ }
0 commit comments