Skip to content

Commit e782848

Browse files
committed
feat: assume role support
1 parent b087379 commit e782848

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

config.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/aws"
7+
"github.com/aws/aws-sdk-go-v2/config"
8+
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
9+
"github.com/aws/aws-sdk-go-v2/service/sts"
10+
)
11+
12+
func loadConfig(ctx context.Context) (*aws.Config, error) {
13+
cfg, err := config.LoadDefaultConfig(ctx)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
if e, ok := getEnvConfig(&cfg); ok && e.RoleARN != "" {
19+
cfg.Credentials = stscreds.NewAssumeRoleProvider(sts.NewFromConfig(cfg), e.RoleARN, func(o *stscreds.AssumeRoleOptions) {
20+
o.RoleSessionName = e.RoleSessionName
21+
})
22+
}
23+
24+
return &cfg, nil
25+
}
26+
27+
func getEnvConfig(cfg *aws.Config) (*config.EnvConfig, bool) {
28+
for _, s := range cfg.ConfigSources {
29+
if c, ok := s.(config.EnvConfig); ok {
30+
return &c, true
31+
}
32+
}
33+
return nil, false
34+
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ go 1.21
55
require (
66
github.com/aws/aws-sdk-go-v2 v1.22.0
77
github.com/aws/aws-sdk-go-v2/config v1.20.0
8+
github.com/aws/aws-sdk-go-v2/credentials v1.14.0
9+
github.com/aws/aws-sdk-go-v2/service/sts v1.24.0
810
github.com/hashicorp/go-retryablehttp v0.7.4
911
github.com/spf13/cobra v1.7.0
1012
)
1113

1214
require (
13-
github.com/aws/aws-sdk-go-v2/credentials v1.14.0 // indirect
1415
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.0 // indirect
1516
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.0 // indirect
1617
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.0 // indirect
1718
github.com/aws/aws-sdk-go-v2/internal/ini v1.4.0 // indirect
1819
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.0 // indirect
1920
github.com/aws/aws-sdk-go-v2/service/sso v1.16.0 // indirect
2021
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.18.0 // indirect
21-
github.com/aws/aws-sdk-go-v2/service/sts v1.24.0 // indirect
2222
github.com/aws/smithy-go v1.16.0 // indirect
2323
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
2424
github.com/inconshreveable/mousetrap v1.1.0 // indirect

main.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"strings"
1313
"time"
1414

15-
"github.com/aws/aws-sdk-go-v2/config"
1615
"github.com/hashicorp/go-retryablehttp"
1716
"github.com/spf13/cobra"
1817

@@ -52,7 +51,7 @@ func run(cmd *cobra.Command, args []string) {
5251
ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt)
5352
defer stop()
5453

55-
cfg, err := config.LoadDefaultConfig(ctx)
54+
cfg, err := loadConfig(ctx)
5655
if err != nil {
5756
log.Fatalf("failed to load configuration: %v", err)
5857
}
@@ -62,7 +61,7 @@ func run(cmd *cobra.Command, args []string) {
6261
log.Fatalf("failed to create HTTP request: %v", err)
6362
}
6463

65-
resp, err := sigv4.NewHTTPClient(&cfg, opt.Service, newRetryableHTTPClient()).Do(req)
64+
resp, err := sigv4.NewHTTPClient(cfg, opt.Service, newRetryableHTTPClient()).Do(req)
6665
if err != nil {
6766
log.Fatalf("failed to HTTP request: %v", err)
6867
}

0 commit comments

Comments
 (0)