Skip to content

Commit

Permalink
Merge pull request #125 from vkareh/SDA-2919/validate-credentials-early
Browse files Browse the repository at this point in the history
aws: Split configuration to ensure early failure
  • Loading branch information
vkareh authored Sep 30, 2020
2 parents 4b9b0c7 + 8cafa54 commit d17865b
Showing 1 changed file with 33 additions and 26 deletions.
59 changes: 33 additions & 26 deletions pkg/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,49 +143,56 @@ func (b *ClientBuilder) Build() (Client, error) {
SharedConfigState: session.SharedConfigEnable,
Profile: profile.Profile(),
Config: aws.Config{
Region: b.region,
// MaxRetries to limit the number of attempts on failed API calls
MaxRetries: aws.Int(25),
// Set MinThrottleDelay to 1 second
Retryer: client.DefaultRetryer{
NumMaxRetries: 5,
MinThrottleDelay: 1 * time.Second,
},
Logger: logger,
HTTPClient: &http.Client{
Transport: http.DefaultTransport,
},
CredentialsChainVerboseErrors: aws.Bool(true),
Region: b.region,
},
})
if err != nil {
return nil, err
}

if b.logger.IsLevelEnabled(logrus.DebugLevel) {
var dumper http.RoundTripper
dumper, err = logging.NewRoundTripper().
Logger(b.logger).
Next(sess.Config.HTTPClient.Transport).
Build()
if err != nil {
return nil, err
}
sess.Config.HTTPClient.Transport = dumper
if profile.Profile() != "" {
b.logger.Debugf("Using AWS profile: %s", profile.Profile())
}

// Check that the region is set:
region := aws.StringValue(sess.Config.Region)
if region == "" {
return nil, fmt.Errorf("Region is not set")
}
if profile.Profile() != "" {
b.logger.Debugf("Using AWS profile: %s", profile.Profile())
}

// Check that the AWS credentials are available:
_, err = sess.Config.Credentials.Get()
if err != nil {
return nil, fmt.Errorf("Failed to find credentials: %v", err)
b.logger.Debugf("Failed to find credentials: %v", err)
return nil, fmt.Errorf("Failed to find credentials. Check your AWS configuration and try again")
}

// Update session config
sess = sess.Copy(&aws.Config{
// MaxRetries to limit the number of attempts on failed API calls
MaxRetries: aws.Int(25),
// Set MinThrottleDelay to 1 second
Retryer: client.DefaultRetryer{
NumMaxRetries: 5,
MinThrottleDelay: 1 * time.Second,
},
Logger: logger,
HTTPClient: &http.Client{
Transport: http.DefaultTransport,
},
})

if b.logger.IsLevelEnabled(logrus.DebugLevel) {
var dumper http.RoundTripper
dumper, err = logging.NewRoundTripper().
Logger(b.logger).
Next(sess.Config.HTTPClient.Transport).
Build()
if err != nil {
return nil, err
}
sess.Config.HTTPClient.Transport = dumper
}

// Create and populate the object:
Expand Down

0 comments on commit d17865b

Please sign in to comment.