-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use aws:username for IAM initiated federated console sessions. (#…
…626) * feat: Use aws:username for IAM initiated federated console sessions. The changes refactor the way federation token ID is used for AWS IAM credentials. Instead of relying on the userID which was previously parsed, the code now uses the userName which is more easily attributable to the IAM user name in the Cloudtrail events list view. In the Cloudtrail console's event history view, the IAM user name will now display in the `user name` column. Previously, the `user id` would display (e.g. AIDA.....). Signed-off-by: Matthew Hembree <47449406+matthewhembree@users.noreply.github.com> * add unit test around behaviour --------- Signed-off-by: Matthew Hembree <47449406+matthewhembree@users.noreply.github.com> Co-authored-by: chrnorm <17420369+chrnorm@users.noreply.github.com>
- Loading branch information
1 parent
1c0922f
commit 3bfb958
Showing
2 changed files
with
112 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cfaws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sts" | ||
"github.com/aws/aws-sdk-go-v2/service/sts/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_getSessionTags(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
|
||
caller *sts.GetCallerIdentityOutput | ||
wantTags []types.Tag | ||
wantUserName string | ||
}{ | ||
{ | ||
name: "ok", | ||
caller: &sts.GetCallerIdentityOutput{ | ||
Account: aws.String("123456789012"), | ||
Arn: aws.String("arn:aws:iam::123456789012:user/example-user"), | ||
UserId: aws.String("XXXYYYZZZ"), | ||
}, | ||
wantUserName: "example-user", | ||
wantTags: []types.Tag{ | ||
{Key: aws.String("userID"), Value: aws.String("XXXYYYZZZ")}, | ||
{Key: aws.String("account"), Value: aws.String("123456789012")}, | ||
{Key: aws.String("principalArn"), Value: aws.String("arn:aws:iam::123456789012:user/example-user")}, | ||
{Key: aws.String("userName"), Value: aws.String("example-user")}, | ||
}, | ||
}, | ||
{ | ||
name: "falls_back_to_user_id_if_arn_invalid", | ||
caller: &sts.GetCallerIdentityOutput{ | ||
Account: aws.String("123456789012"), | ||
Arn: aws.String("invalid arn"), | ||
UserId: aws.String("XXXYYYZZZ"), | ||
}, | ||
wantUserName: "XXXYYYZZZ", | ||
wantTags: []types.Tag{ | ||
{Key: aws.String("userID"), Value: aws.String("XXXYYYZZZ")}, | ||
{Key: aws.String("account"), Value: aws.String("123456789012")}, | ||
{Key: aws.String("principalArn"), Value: aws.String("invalid arn")}, | ||
}, | ||
}, | ||
{ | ||
name: "wont_panic", | ||
caller: nil, | ||
wantUserName: "", | ||
wantTags: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotTags, gotUserName := getSessionTags(tt.caller) | ||
assert.Equal(t, tt.wantTags, gotTags) | ||
if gotUserName != tt.wantUserName { | ||
t.Errorf("getSessionTags() gotUserName = %v, want %v", gotUserName, tt.wantUserName) | ||
} | ||
}) | ||
} | ||
} |