Skip to content

Commit a75ddb5

Browse files
committed
adds retry function for STS call
1 parent ec5c471 commit a75ddb5

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

main.go

+47-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io/ioutil"
99
"log"
10+
"math"
1011
"os"
1112
"strings"
1213
"time"
@@ -169,13 +170,34 @@ func main() {
169170
"mode", qbconfOperationMode,
170171
)
171172

172-
OidcToken, OidcTokenErr := getOidcGithubActionsToken()
173-
if OidcTokenErr != nil {
174-
logSugar.Error(OidcTokenErr)
175-
return OidcTokenErr
173+
// Add your own configuration here
174+
maxRetries := 3
175+
backoffBaseSeconds := 2
176+
177+
assumeRoleWithWebIdentityErr := retryWithExponentialBackoff(maxRetries, backoffBaseSeconds, func() error {
178+
var err error
179+
180+
oidcToken, err := getOidcGithubActionsToken()
181+
if err != nil {
182+
return err
183+
}
184+
185+
awsConfig.Credentials, err = assumeRoleWithWebIdentity(c.String("role-arn"), c.String("role-session-name"), *oidcToken, awsConfig)
186+
return err
187+
})
188+
189+
if assumeRoleWithWebIdentityErr != nil {
190+
logSugar.Error(assumeRoleWithWebIdentityErr)
191+
return assumeRoleWithWebIdentityErr
176192
}
177193

178-
awsConfig.Credentials = assumeRoleWithWebIdentity(c.String("role-arn"), c.String("role-session-name"), *OidcToken, awsConfig)
194+
// OidcToken, OidcTokenErr := getOidcGithubActionsToken()
195+
// if OidcTokenErr != nil {
196+
// logSugar.Error(OidcTokenErr)
197+
// return OidcTokenErr
198+
// }
199+
200+
// awsConfig.Credentials = assumeRoleWithWebIdentity(c.String("role-arn"), c.String("role-session-name"), *OidcToken, awsConfig)
179201
}
180202

181203
_, getAWSIdentityErr := getAWSIdentity(*awsConfig)
@@ -269,7 +291,7 @@ func assumeRoleByArn(roleArn, roleSessionName string, awsConfig *aws.Config) *st
269291
}
270292

271293
// Function to assume role with OIDC ( token )
272-
func assumeRoleWithWebIdentity(roleArn, roleSessionName, token string, awsConfig *aws.Config) *aws.CredentialsCache {
294+
func assumeRoleWithWebIdentity(roleArn, roleSessionName, token string, awsConfig *aws.Config) (*aws.CredentialsCache, error) {
273295

274296
// Create an STS client using the default config
275297
stsClient := sts.NewFromConfig(*awsConfig)
@@ -284,8 +306,7 @@ func assumeRoleWithWebIdentity(roleArn, roleSessionName, token string, awsConfig
284306
// Call the AssumeRoleWithWebIdentity API to assume the IAM role
285307
resp, err := stsClient.AssumeRoleWithWebIdentity(context.Background(), input)
286308
if err != nil {
287-
logSugar.Error(err)
288-
panic(err)
309+
return nil, err
289310
}
290311

291312
// value := aws.Credentials{
@@ -305,7 +326,7 @@ func assumeRoleWithWebIdentity(roleArn, roleSessionName, token string, awsConfig
305326
),
306327
)
307328

308-
return credsProvider
329+
return credsProvider, nil
309330
}
310331

311332
// Function to generate a kubeconfig for a given EKS cluster
@@ -446,9 +467,7 @@ func getOidcGithubActionsToken() (*string, error) {
446467
//ACTIONS_ID_TOKEN_REQUEST_TOKEN
447468
logSugar.Debug("retrieval of ACTIONS_ID_TOKEN_REQUEST_TOKEN env variable")
448469
tokenRequestToken := os.Getenv("ACTIONS_ID_TOKEN_REQUEST_TOKEN")
449-
logSugar.Infow("retrieved ACTIONS_ID_TOKEN_REQUEST_TOKEN",
450-
"oidc_token_request_token", maskString(tokenRequestToken),
451-
)
470+
logSugar.Info("retrieved ACTIONS_ID_TOKEN_REQUEST_TOKEN")
452471

453472
logSugar.Debugw("prepared URL for requesting token value towards OIDC endpoint",
454473
"oidc_token_request_url", "%s&audience=sts.amazonaws.com",
@@ -466,3 +485,19 @@ func getOidcGithubActionsToken() (*string, error) {
466485

467486
return &tokenValue, nil
468487
}
488+
489+
func retryWithExponentialBackoff(maxRetries int, backoffBaseSeconds int, operation func() error) error {
490+
var err error
491+
for i := 0; i < maxRetries; i++ {
492+
err = operation()
493+
if err == nil {
494+
return nil
495+
}
496+
497+
waitTime := time.Duration(math.Pow(float64(backoffBaseSeconds), float64(i))) * time.Second
498+
logSugar.Infow("retry wait added", "wait_time", waitTime, "attempt", i, "max_retries", maxRetries)
499+
time.Sleep(waitTime)
500+
}
501+
502+
return err
503+
}

0 commit comments

Comments
 (0)