-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f393e3
44833 Truncate AssumeRole session name to API limits
joaoubaldo 11d2169
Link reference
joaoubaldo d379ef7
Hash the username and add audit log
greedy52 0864e26
review comments
greedy52 5acbb2b
remove audit
greedy52 0d939ef
Merge branch 'master' into STeve/pr-buddy-44836
greedy52 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -18,6 +18,7 @@ package auth | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -83,3 +84,35 @@ func TestServer_CreateWebSessionFromReq_deviceWebToken(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func Test_guessAWSRoleSessionName(t *testing.T) { | ||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
localClusterName := "teleport.abigcompany.com" | ||
|
||
for _, tt := range []struct { | ||
request NewAppSessionRequest | ||
expected string | ||
}{ | ||
{ | ||
request: NewAppSessionRequest{ | ||
NewWebSessionRequest: NewWebSessionRequest{ | ||
User: "[email protected]", | ||
}, | ||
ClusterName: "teleport.abigcompany.com", | ||
}, | ||
expected: "[email protected]", | ||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
{ | ||
request: NewAppSessionRequest{ | ||
NewWebSessionRequest: NewWebSessionRequest{ | ||
User: "[email protected]", | ||
}, | ||
ClusterName: "leaf.teleport.abigcompany.com", | ||
}, | ||
expected: "remote-raimundo.oliveir-8fe1f87e599b043e6a0108338feee3f111e9c1e4", | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("%s@%s", tt.request.User, tt.request.ClusterName), func(t *testing.T) { | ||
require.Equal(t, tt.expected, guessAWSRoleSessionName(localClusterName, tt.request)) | ||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
} | ||
} |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -21,7 +21,10 @@ package aws | |||||
import ( | ||||||
"bytes" | ||||||
"context" | ||||||
"crypto/sha1" | ||||||
"encoding/hex" | ||||||
"fmt" | ||||||
"log/slog" | ||||||
"net/http" | ||||||
"net/textproto" | ||||||
"sort" | ||||||
|
@@ -33,7 +36,6 @@ import ( | |||||
v4 "github.com/aws/aws-sdk-go/aws/signer/v4" | ||||||
"github.com/aws/aws-sdk-go/service/iam" | ||||||
"github.com/gravitational/trace" | ||||||
"github.com/sirupsen/logrus" | ||||||
|
||||||
apievents "github.com/gravitational/teleport/api/types/events" | ||||||
apiawsutils "github.com/gravitational/teleport/api/utils/aws" | ||||||
|
@@ -68,6 +70,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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
) | ||||||
|
||||||
// SigV4 contains parsed content of the AWS Authorization header. | ||||||
|
@@ -249,7 +255,7 @@ func FilterAWSRoles(arns []string, accountID string) (result Roles) { | |||||
for _, roleARN := range arns { | ||||||
parsed, err := ParseRoleARN(roleARN) | ||||||
if err != nil { | ||||||
logrus.Warnf("skipping invalid AWS role ARN: %v", err) | ||||||
slog.WarnContext(context.Background(), "Skipping invalid AWS role ARN.", "error", err) | ||||||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
continue | ||||||
} | ||||||
if accountID != "" && parsed.AccountID != accountID { | ||||||
|
@@ -484,3 +490,30 @@ func iamResourceARN(partition, accountID, resourceType, resourceName string) str | |||||
Resource: fmt.Sprintf("%s/%s", resourceType, resourceName), | ||||||
}.String() | ||||||
} | ||||||
|
||||||
// MaybeHashRoleSessionName truncates the role session name and adds a hash | ||||||
// when the original role session name is greater than AWS character limit | ||||||
// (64). | ||||||
func MaybeHashRoleSessionName(roleSessionName string) (ret string) { | ||||||
if len(roleSessionName) <= MaxRoleSessionName { | ||||||
return roleSessionName | ||||||
} | ||||||
|
||||||
hash := sha1.New() | ||||||
hash.Write([]byte(roleSessionName)) | ||||||
hex := hex.EncodeToString(hash.Sum(nil)) | ||||||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
// "1" for the delimiter. | ||||||
keepPrefixIndex := MaxRoleSessionName - len(hex) - 1 | ||||||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
if keepPrefixIndex < 0 { | ||||||
// This should not happen since sha1 length and MaxRoleSessionName are | ||||||
// both constant. | ||||||
ret = hex[:MaxRoleSessionName] | ||||||
greedy52 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} else { | ||||||
ret = fmt.Sprintf("%s-%s", roleSessionName[:keepPrefixIndex], hex) | ||||||
} | ||||||
|
||||||
slog.DebugContext(context.Background(), "AWS role session name is too long. Using a hash instead.", "hashed", ret, "original", roleSessionName) | ||||||
return ret | ||||||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make it more generic like
I wonder what other fields can be added to AWSSessionMetadata, where AWSMetadata would be more flexible and in the case of other AWS fields not related to AWSSession context I think that the AWSMetadata it will be a better fit.