Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix cred chain #20

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions awsauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,11 @@ func GetCredentialsFromSession(c *Config) (*awsCredentials.Credentials, error) {
return creds, nil
}

// GetCredentials gets credentials from the environment, shared credentials,
// or the session (which may include a credential process). GetCredentials also
// validates the credentials and the ability to assume a role or will return an
// error if unsuccessful.
func GetCredentials(c *Config) (*awsCredentials.Credentials, error) {
// build a chain provider, lazy-evaluated by aws-sdk
providers := []awsCredentials.Provider{
&awsCredentials.StaticProvider{Value: awsCredentials.Value{
AccessKeyID: c.AccessKey,
SecretAccessKey: c.SecretKey,
SessionToken: c.Token,
}},
&awsCredentials.EnvProvider{},
&awsCredentials.SharedCredentialsProvider{
Filename: c.CredsFilename,
Profile: c.Profile,
},
}
// GetCredentialsFromMetadata returns credentials derived from and ECS or ECS
// metadata endpoint.
func GetCredentialsFromMetadata(c *Config) (*awsCredentials.Credentials, error) {
log.Printf("[INFO] Attempting to use metadata-derived credentials")
providers := []awsCredentials.Provider{}

// Build isolated HTTP client to avoid issues with globally-shared settings
client := cleanhttp.DefaultClient()
Expand Down Expand Up @@ -293,14 +280,51 @@ func GetCredentials(c *Config) (*awsCredentials.Credentials, error) {
}
}

// Validate the credentials before returning them
creds := awsCredentials.NewChainCredentials(providers)
cp, err := creds.Get()
if err != nil {
if IsAWSErr(err, "NoCredentialProviders", "") {
return nil, ErrNoValidCredentialSources
}
return nil, fmt.Errorf("Error deriving credentials from metadata: %s", err)
}

log.Printf("[INFO] Successfully derived credentials from metadata")
log.Printf("[INFO] AWS Auth provider used: %q", cp.ProviderName)
return creds, nil
}

// GetCredentials gets credentials from the environment, shared credentials,
// the session (which may include a credential process), or ECS/EC2 metadata endpoints.
// GetCredentials also validates the credentials and the ability to assume a role
// or will return an error if unsuccessful.
func GetCredentials(c *Config) (*awsCredentials.Credentials, error) {
// build a chain provider, lazy-evaluated by aws-sdk
providers := []awsCredentials.Provider{
&awsCredentials.StaticProvider{Value: awsCredentials.Value{
AccessKeyID: c.AccessKey,
SecretAccessKey: c.SecretKey,
SessionToken: c.Token,
}},
&awsCredentials.EnvProvider{},
&awsCredentials.SharedCredentialsProvider{
Filename: c.CredsFilename,
Profile: c.Profile,
},
}

// Validate the credentials before returning them
creds := awsCredentials.NewChainCredentials(providers)
cp, err := creds.Get()
if err != nil {
if IsAWSErr(err, "NoCredentialProviders", "") {
creds, err = GetCredentialsFromSession(c)
if err != nil {
return nil, err
creds, err = GetCredentialsFromMetadata(c)
if err != nil {
return nil, err
}
}
} else {
return nil, fmt.Errorf("Error loading credentials for AWS Provider: %s", err)
Expand Down
1 change: 1 addition & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func GetSessionOptions(c *Config) (*session.Options, error) {
MaxRetries: aws.Int(0),
Region: aws.String(c.Region),
},
Profile: c.Profile,
}

// get and validate credentials
Expand Down