Skip to content

Commit

Permalink
Support KOPS_AWS_ROLE_ARN in aws-sdk-go-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
rifelpet committed Mar 31, 2024
1 parent 856b819 commit 19f53b4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions upup/pkg/fi/cloudup/awsup/aws_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/aws/retry"
stscredsv2 "github.com/aws/aws-sdk-go-v2/credentials/stscreds"
elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing/types"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
Expand Down Expand Up @@ -295,16 +296,13 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
},
}

cfgV2, err := awsconfig.LoadDefaultConfig(ctx,
loadOptions := []func(*awsconfig.LoadOptions) error{
awsconfig.WithRegion(region),
awsconfig.WithClientLogMode(awsv2.LogRetries),
awsconfig.WithLogger(awsLogger{}),
awsconfig.WithRetryer(func() awsv2.Retryer {
return retry.NewStandard()
}),
)
if err != nil {
return c, fmt.Errorf("failed to load default aws config: %w", err)
}

config := aws.NewConfig().WithRegion(region)
Expand All @@ -323,6 +321,15 @@ func NewAWSCloud(region string, tags map[string]string) (AWSCloud, error) {
// assumes the role before executing commands
roleARN := os.Getenv("KOPS_AWS_ROLE_ARN")
if roleARN != "" {
cfgV2, err := awsconfig.LoadDefaultConfig(ctx, loadOptions...)
if err != nil {
return c, fmt.Errorf("failed to load default aws config: %w", err)
}
stsClient := sts.NewFromConfig(cfgV2)
assumeRoleProvider := stscredsv2.NewAssumeRoleProvider(stsClient, roleARN)

loadOptions = append(loadOptions, awsconfig.WithCredentialsProvider(assumeRoleProvider))

creds := stscreds.NewCredentials(sess, roleARN)
config = &aws.Config{Credentials: creds}
config = setConfig(config).WithRegion(region)
Expand Down

0 comments on commit 19f53b4

Please sign in to comment.