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

Adds support for SourceIdentity to stscreds.AssumeRoleProvider #1588

Merged
merged 2 commits into from Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions credentials/stscreds/assume_role_provider.go
Expand Up @@ -208,6 +208,18 @@ type AssumeRoleOptions struct {
// or an Amazon Resource Name (ARN) for a virtual device (such as arn:aws:iam::123456789012:mfa/user).
SerialNumber *string

// The source identity specified by the principal that is calling the AssumeRole
// operation. You can require users to specify a source identity when they assume a
// role. You do this by using the sts:SourceIdentity condition key in a role trust
// policy. You can use source identity information in CloudTrail logs to determine
// who took actions with a role. You can use the aws:SourceIdentity condition key
// to further control access to Amazon Web Services resources based on the value of
// source identity. For more information about using source identity, see Monitor
// and control actions taken with assumed roles
// (https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_control-access_monitor.html)
// in the IAM User Guide.
SourceIdentity *string

// Async method of providing MFA token code for assuming an IAM role with MFA.
// The value returned by the function will be used as the TokenCode in the Retrieve
// call. See StdinTokenProvider for a provider that prompts and reads from stdin.
Expand Down Expand Up @@ -266,6 +278,7 @@ func (p *AssumeRoleProvider) Retrieve(ctx context.Context) (aws.Credentials, err
RoleArn: aws.String(p.options.RoleARN),
RoleSessionName: aws.String(p.options.RoleSessionName),
ExternalId: p.options.ExternalID,
SourceIdentity: p.options.SourceIdentity,
Tags: p.options.Tags,
TransitiveTagKeys: p.options.TransitiveTagKeys,
}
Expand Down
30 changes: 30 additions & 0 deletions credentials/stscreds/assume_role_provider_test.go
Expand Up @@ -145,6 +145,36 @@ func TestAssumeRoleProvider_MFAWithNoToken(t *testing.T) {
}
}

func TestAssumeRoleProvider_WithSourceIdentity(t *testing.T) {
const sourceIdentity = "Source-Identity"

stub := &mockAssumeRole{
TestInput: func(in *sts.AssumeRoleInput) {
if e, a := sourceIdentity, *in.SourceIdentity; e != a {
t.Fatalf("expect %v, got %v", e, a)
}
},
}
p := stscreds.NewAssumeRoleProvider(stub, roleARN, func(options *stscreds.AssumeRoleOptions) {
options.SourceIdentity = aws.String(sourceIdentity)
})

creds, err := p.Retrieve(context.Background())
if err != nil {
t.Fatalf("Expect no error, %v", err)
}

if e, a := roleARN, creds.AccessKeyID; e != a {
t.Errorf("Expect access key ID to be reflected role ARN")
}
if e, a := "assumedSecretAccessKey", creds.SecretAccessKey; e != a {
t.Errorf("Expect secret access key to match")
}
if e, a := "assumedSessionToken", creds.SessionToken; e != a {
t.Errorf("Expect session token to match")
}
}

func TestAssumeRoleProvider_WithTags(t *testing.T) {
stub := &mockAssumeRole{
TestInput: func(in *sts.AssumeRoleInput) {
Expand Down