Skip to content

Commit

Permalink
adding session error if values aren't properly configured for web ide…
Browse files Browse the repository at this point in the history
…ntity
  • Loading branch information
xibz committed Oct 9, 2018
1 parent 9ca78b7 commit 774cd1e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 39 deletions.
15 changes: 4 additions & 11 deletions aws/credentials/stscreds/web_identity_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const (
// ErrCodeWebIdentityRetrievalErr will be used as an error code when constructing
// a new error to be returned during Retrieve.
ErrCodeWebIdentityRetrievalErr = "WebIdentityRetrievalErr"

// WebIdentityProviderName is the web identity provider name
WebIdentityProviderName = "WebIdentityCredentials"
)

// now is used to return a time.Time object representing
Expand Down Expand Up @@ -59,21 +62,10 @@ func NewWebIdentityRoleProvider(svc stsiface.STSAPI, roleARN, roleSessionName, p
}
}

var emptyTokenFilePathErr = awserr.New(ErrCodeWebIdentityRetrievalErr, "token file path is not set", nil)
var emptyRoleARNErr = awserr.New(ErrCodeWebIdentityRetrievalErr, "role ARN is not set", nil)

// Retrieve will attempt to assume a role from a token which is located at
// 'WebIdentityTokenFilePath' specified destination and if that is empty an
// error will be returned.
func (p *WebIdentityRoleProvider) Retrieve() (credentials.Value, error) {
if len(p.tokenFilePath) == 0 {
return credentials.Value{}, emptyTokenFilePathErr
}

if len(p.roleARN) == 0 {
return credentials.Value{}, emptyRoleARNErr
}

b, err := ioutil.ReadFile(p.tokenFilePath)
if err != nil {
errMsg := fmt.Sprintf("unabled to read file at %s", p.tokenFilePath)
Expand Down Expand Up @@ -101,6 +93,7 @@ func (p *WebIdentityRoleProvider) Retrieve() (credentials.Value, error) {
AccessKeyID: aws.StringValue(resp.Credentials.AccessKeyId),
SecretAccessKey: aws.StringValue(resp.Credentials.SecretAccessKey),
SessionToken: aws.StringValue(resp.Credentials.SessionToken),
ProviderName: WebIdentityProviderName,
}
return value, nil
}
30 changes: 2 additions & 28 deletions aws/credentials/stscreds/web_identity_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build go1.7

package stscreds

import (
Expand Down Expand Up @@ -38,34 +40,6 @@ func TestWebIdentityProviderRetrieve(t *testing.T) {
expectedError error
expectedCredValue credentials.Value
}{
{
name: "no role arn",
tokenFilepath: "foo/bar",
mockSTS: &mockSTS{
AssumeRoleWithWebIdentityFn: func(input *sts.AssumeRoleWithWebIdentityInput) (*sts.AssumeRoleWithWebIdentityOutput, error) {
if e, a := fmt.Sprintf("%d", now().UnixNano()), *input.RoleSessionName; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, but received %v", e, a)
}

return &sts.AssumeRoleWithWebIdentityOutput{}, nil
},
},
expectedError: emptyRoleARNErr,
},
{
name: "no token file path",
roleARN: "arn",
mockSTS: &mockSTS{
AssumeRoleWithWebIdentityFn: func(input *sts.AssumeRoleWithWebIdentityInput) (*sts.AssumeRoleWithWebIdentityOutput, error) {
if e, a := fmt.Sprintf("%d", now().UnixNano()), *input.RoleSessionName; !reflect.DeepEqual(e, a) {
t.Errorf("expected %v, but received %v", e, a)
}

return &sts.AssumeRoleWithWebIdentityOutput{}, nil
},
},
expectedError: emptyTokenFilePathErr,
},
{
name: "session name case",
roleARN: "arn",
Expand Down
8 changes: 8 additions & 0 deletions aws/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ func Must(sess *Session, err error) *Session {
return sess
}

// WebIdentityEmptyRoleARNErr will occur if 'AWS_WEB_IDENTITY_TOKEN_FILE' was set but
// 'AWS_IAM_ROLE_ARN' was not set.
var WebIdentityEmptyRoleARNErr = awserr.New(ErrCodeWebIdentityRetrievalErr, "role ARN is not set", nil)

func deprecatedNewSession(cfgs ...*aws.Config) *Session {
cfg := defaults.Config()
handlers := defaults.Handlers()
Expand Down Expand Up @@ -444,6 +448,10 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg share
} else if len(envCfg.WebIdentityTokenFilePath) > 0 {
// handles assume role via OIDC token. This should happen before any other
// assume role call.
if len(envCfg.WebIdentityRoleARN) == 0 {
return WebIdentityEmptyRoleARNErr
}

sessionName := envCfg.IAMRoleSessionName
if len(sessionName) == 0 {
sessionName = sharedCfg.AssumeRole.RoleSessionName
Expand Down

0 comments on commit 774cd1e

Please sign in to comment.