-
Notifications
You must be signed in to change notification settings - Fork 428
Upgrade token verification to AWS SDK Go V2 #875
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
base: master
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: gargipanatula The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Hi @gargipanatula. Thanks for your PR. I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
/ok-to-test |
/retest |
1 similar comment
/retest |
b5c4f5d
to
d0c8b7b
Compare
@@ -521,73 +572,6 @@ func response(account, userID, arn string) getCallerIdentityWrapper { | |||
return wrapper | |||
} | |||
|
|||
func Test_getDefaultHostNameForRegion(t *testing.T) { |
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.
context: this function doesn't exist anymore, so no need to test
@@ -171,8 +194,32 @@ func TestSTSEndpoints(t *testing.T) { | |||
{"aws-not-a-partition", "sts.amazonaws.com", false, ""}, | |||
} | |||
|
|||
regions := []ec2types.Region{ |
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.
context: tests that simply verified valid sts endpoints, not necessarily region/partition matching, need a stub list of regions that have each region used in the test.
DescribeDelay = 100 | ||
) | ||
|
||
func newMockedEC2ProviderImpl() *ec2ProviderImpl { |
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.
context: slightly changed to mocking to support extended mocking. essentially moved the functions around and directly implemented functions for MockEc2Client rather than relaying through the EC2API.
cmd/aws-iam-authenticator/root.go
Outdated
partitionKeys = append(partitionKeys, p.ID()) | ||
} | ||
if _, ok := partitionMap[cfg.PartitionID]; !ok { | ||
if slices.Contains(PartitionKeys, cfg.PartitionID) { |
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.
doesn't contain right?
@@ -215,9 +216,10 @@ func (c *Server) getHandler(ctx context.Context, backendMapper BackendMapper, ec | |||
} else { | |||
instanceRegion = instanceRegionOutput.Region | |||
} | |||
ec2Client := ec2.NewFromConfig(cfg) |
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.
are we creating new client instead of using ec2provider because we need to make call against the account where the authenticator is running instead of ServerEC2DescribeInstancesRoleARN ?
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.
Yeah, I figured configuring a client directly would be more straightforward than creating ec2provider
with the host account's ARN.
cmd/aws-iam-authenticator/verify.go
Outdated
func getInstanceRegion(ctx context.Context) string { | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
fmt.Printf("[Warn] Unable to create metadata client, err: %v", err) |
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.
nit: the error message is not correct right as we just generating config here and not creating client?
@@ -54,14 +56,17 @@ var verifyCmd = &cobra.Command{ | |||
os.Exit(1) | |||
} | |||
|
|||
sess := session.Must(session.NewSession()) |
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.
Here we are expecting to have the session object right? But in getInstanceRegion function its best effort , any specific reason?
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.
My mistake, this requirement shouldn't have been relaxed. I've added a panic to getInstanceRegion
that occurs when config.LoadDefaultConfig
returns an error, to replicate the panic that session.Must()
would've sent if session.New()
had an error. (session code).
bd9b879
to
e802f44
Compare
19aa9a2
to
f80e105
Compare
/retest |
@gargipanatula: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
switch partitionID { | ||
case "aws": | ||
tlds = []string{"amazonaws.com", "api.aws"} | ||
validSTShostnames["sts.amazonaws.com"] = true |
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.
todo: support global for all partitions
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.
update: it's only supported for the commercial partition, so this is actually not necessary
PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
What this PR does / why we need it:
Updates the way that the server discovers valid partitions and regions during token verification. In essence, the host of a token is valid if it 1) comes from the same partition as the server and 2) contains a valid region.
Functionality changes
Implementation changes
github.com/aws/aws-sdk-go/aws/endpoints
package → using a hardcoded list of partitionsgithub.com/aws/aws-sdk-go/aws/endpoints
package → discovering regions through theec2.DescribeRegions
API.Testing
Created a token using credentials from an account in the
aws
partition (US_TOKEN
) and an account in theaws-cn
partition (CHINA_TOKEN
)Ran
verify
on each token using accounts from within their partition, one with the same and one with a different region than the account that created the token. I was able to see that the token was correctly successfully verified when an account is in the same partition as the one that generated the token, regardless of region.Output of this test for
CHINA_TOKEN
(similar forUS_TOKEN
):Then, I ran
verify
on each token with both the correct and incorrect partition (aws
forCHINA_TOKEN
andaws-cn
forUS_TOKEN
) to replicate conditions where the client is in a different partition from the server. I saw that the command correctly rejected the tokens when coming from a different partition.Output for
US_TOKEN
(similar forCHINA_TOKEN
):Note that the warning about ec2imds comes from the fact that the
AWS_EC2_METADATA_DISABLED
env var is set to true, if set to false the output is the same but without the warning.Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #