Skip to content

Commit

Permalink
Add error handling if neither TokenCode nor TokenProvider are set
Browse files Browse the repository at this point in the history
  • Loading branch information
jasdel committed Feb 18, 2017
1 parent 706f206 commit 2000a7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions aws/credentials/stscreds/assume_role_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,20 @@ func (p *AssumeRoleProvider) Retrieve() (credentials.Value, error) {
if p.TokenCode != nil {
input.SerialNumber = p.SerialNumber
input.TokenCode = p.TokenCode
} else {
} else if p.TokenProvider != nil {
input.SerialNumber = p.SerialNumber
provider := p.TokenProvider
code, err := provider()
code, err := p.TokenProvider()
if err != nil {
return credentials.Value{ProviderName: ProviderName}, err
}
input.TokenCode = aws.String(code)
} else {
return credentials.Value{ProviderName: ProviderName},
fmt.Errorf("assume role with MFA enabled, but neither TokenCode nor TokenProvider are set.")
}
}
roleOutput, err := p.Client.AssumeRole(input)

roleOutput, err := p.Client.AssumeRole(input)
if err != nil {
return credentials.Value{ProviderName: ProviderName}, err
}
Expand Down
20 changes: 20 additions & 0 deletions aws/credentials/stscreds/assume_role_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ func TestAssumeRoleProvider_WithTokenProviderError(t *testing.T) {
assert.Empty(t, creds.SessionToken)
}

func TestAssumeRoleProvider_MFAWithNoToken(t *testing.T) {
stub := &stubSTS{
TestInput: func(in *sts.AssumeRoleInput) {
assert.Fail(t, "API request should not of been called")
},
}
p := &AssumeRoleProvider{
Client: stub,
RoleARN: "roleARN",
SerialNumber: aws.String("0123456789"),
}

creds, err := p.Retrieve()
assert.Error(t, err)

assert.Empty(t, creds.AccessKeyID)
assert.Empty(t, creds.SecretAccessKey)
assert.Empty(t, creds.SessionToken)
}

func BenchmarkAssumeRoleProvider(b *testing.B) {
stub := &stubSTS{}
p := &AssumeRoleProvider{
Expand Down

0 comments on commit 2000a7d

Please sign in to comment.