Skip to content

Commit 49d8690

Browse files
committed
Use default credential chain if credentialsfile not specified
At the moment, credentials must come from the `credentialsfile` setting in the config file because the AWS SDK credentials chain has been replaced. This means it isn't possible to use environment variables to specify the credentials at all, or from the instance/container IAM role if we ever decide to go down that road. This change makes go-minipypi only replace the default credential chain if credentialfile is specified in the config. If not, then the default credential chain will be used, i.e. the order defined here - https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials
1 parent 3e226f5 commit 49d8690

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

main.go

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ func isValidConfig(config Configs) bool {
7272
valid = valid && config.WebConfigs.Port > 0
7373
valid = valid && config.WebConfigs.Port < 65535
7474
valid = valid && len(config.S3configs.BucketName) > 0
75-
valid = valid && len(config.S3configs.CredentialsFile) > 0
7675
valid = valid && len(config.S3configs.Region) > 0
7776
return valid
7877
}

s3fetcher.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"log"
5+
46
"github.com/aws/aws-sdk-go/aws"
57
"github.com/aws/aws-sdk-go/aws/credentials"
68
"github.com/aws/aws-sdk-go/aws/session"
@@ -20,9 +22,15 @@ type S3configs struct {
2022
// NewS3Fetcher is a S3 backed implementation of the FileFetcher interface.
2123
// it does the setup of the S3 service session state required to implement FileFetcher interface
2224
func NewS3Fetcher(cfg S3configs) FileFetcher {
23-
svc := s3.New(session.New(
24-
aws.NewConfig().WithRegion(cfg.Region).WithCredentials(
25-
credentials.NewSharedCredentials(cfg.CredentialsFile, "default"))))
25+
awsCfg := aws.NewConfig().WithRegion(cfg.Region)
26+
if cfg.CredentialsFile != "" {
27+
log.Printf("using AWS credentials from %s", cfg.CredentialsFile)
28+
awsCfg = awsCfg.WithCredentials(credentials.NewSharedCredentials(cfg.CredentialsFile, "default"))
29+
} else {
30+
log.Print("no AWS credentials file specified, using the default credentials chain")
31+
}
32+
33+
svc := s3.New(session.New(awsCfg))
2634
cfg.s3svc = svc
2735

2836
return cfg

0 commit comments

Comments
 (0)