Skip to content

Commit 085fee1

Browse files
authored
Merge pull request #286 from dbarbashov/feature/aws-sts-web-identity-provider
Implement STS Web Identity Role credentials provider
2 parents 7b03b5a + e1cbd45 commit 085fee1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

s3.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
8+
"github.com/aws/aws-sdk-go/service/sts"
79
"io"
10+
"os"
811
"path"
912
"strings"
1013
"sync"
@@ -70,7 +73,7 @@ func NewS3Datastore(conf Config) (*S3Bucket, error) {
7073
}
7174

7275
d := defaults.Get()
73-
creds := credentials.NewChainCredentials([]credentials.Provider{
76+
providers := []credentials.Provider{
7477
&credentials.StaticProvider{Value: credentials.Value{
7578
AccessKeyID: conf.AccessKey,
7679
SecretAccessKey: conf.SecretKey,
@@ -82,7 +85,16 @@ func NewS3Datastore(conf Config) (*S3Bucket, error) {
8285
endpointcreds.NewProviderClient(*d.Config, d.Handlers, conf.CredentialsEndpoint,
8386
func(p *endpointcreds.Provider) { p.ExpiryWindow = credsRefreshWindow },
8487
),
85-
})
88+
}
89+
90+
if len(os.Getenv("AWS_ROLE_ARN")) > 0 && len(os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE")) > 0 {
91+
stsClient := sts.New(sess)
92+
stsProvider := stscreds.NewWebIdentityRoleProviderWithOptions(stsClient, os.Getenv("AWS_ROLE_ARN"), "", stscreds.FetchTokenPath(os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE")))
93+
// prepend sts provider to list of providers
94+
providers = append([]credentials.Provider{stsProvider}, providers...)
95+
}
96+
97+
creds := credentials.NewChainCredentials(providers)
8698

8799
if conf.RegionEndpoint != "" {
88100
awsConfig.WithS3ForcePathStyle(true)

0 commit comments

Comments
 (0)