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

[buddy] Truncate AssumeRole session name to API limits #45202

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion lib/srv/app/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (c *cloud) getAWSSigninToken(ctx context.Context, req *AWSSigninRequest, en
options = append(options, func(creds *stscreds.AssumeRoleProvider) {
// Setting role session name to Teleport username will allow to
// associate CloudTrail events with the Teleport user.
creds.RoleSessionName = req.Identity.Username
creds.RoleSessionName = awsutils.TruncateRoleSessionName(req.Identity.Username)
greedy52 marked this conversation as resolved.
Show resolved Hide resolved

// Setting web console session duration through AssumeRole call for AWS
// sessions with temporary credentials.
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const (
AmzJSON1_0 = "application/x-amz-json-1.0"
// AmzJSON1_1 is an AWS Content-Type header that indicates the media type is JSON.
AmzJSON1_1 = "application/x-amz-json-1.1"

// MaxRoleSessionName is the maximum length of the role session name used by the AssumeRole call.
// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html
MaxRoleSessionName = 64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just to make it explicit that this is not count but name length.

Suggested change
MaxRoleSessionName = 64
MaxRoleSessionNameLength = 64

)

// SigV4 contains parsed content of the AWS Authorization header.
Expand Down Expand Up @@ -484,3 +488,11 @@ func iamResourceARN(partition, accountID, resourceType, resourceName string) str
Resource: fmt.Sprintf("%s/%s", resourceType, resourceName),
}.String()
}

// TruncateRoleSessionName truncates the role session name to AWS character limit (64).
func TruncateRoleSessionName(roleSessionName string) string {
if len(roleSessionName) > MaxRoleSessionName {
return roleSessionName[:MaxRoleSessionName]
}
return roleSessionName
}
23 changes: 23 additions & 0 deletions lib/utils/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,26 @@ func TestResourceARN(t *testing.T) {
})
}
}

func TestTruncateRoleSessionName(t *testing.T) {
for _, tt := range []struct {
name string
role string
expected string
}{
{
name: "role session name not truncated, less than 64 characters",
role: "MyRole",
expected: "MyRole",
},
{
name: "role session name truncated, longer than 64 characters",
role: "remote-raimundo.oliveira@abigcompany.com-teleport.abigcompany.com",
expected: "[email protected]",
},
} {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, TruncateRoleSessionName(tt.role))
})
}
}
2 changes: 1 addition & 1 deletion lib/utils/aws/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (g *credentialsGetter) Get(_ context.Context, request GetCredentialsRequest
logrus.Debugf("Creating STS session %q for %q.", request.SessionName, request.RoleARN)
return stscreds.NewCredentials(request.Provider, request.RoleARN,
func(cred *stscreds.AssumeRoleProvider) {
cred.RoleSessionName = request.SessionName
cred.RoleSessionName = TruncateRoleSessionName(request.SessionName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark about clashes here.

cred.Expiry.SetExpiration(request.Expiry, 0)

if request.ExternalID != "" {
Expand Down
Loading