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

Fix IAM Role ARN if trying to run against GovCloud #48

Merged
merged 3 commits into from
May 14, 2020
Merged

Conversation

dynamike
Copy link
Contributor

Up til now we've been hardcoding the AWS partition, which breaks when trying to run against GovCloud. This PR looks up the AWS partition based on the region you're passing in.

Tested by running

go run cmd/main.go --role admin --iam-user dynatest --profile=dynatest --account-id=<gov-account-id> --region=us-gov-west-1
go run cmd/main.go --role admin --iam-user dynatest --profile=dynatest --account-id=<commercial-account-id>

cmd/main.go Outdated
Comment on lines 457 to 461
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region); ok {
return partition.ID()
}
return "aws"
}

Choose a reason for hiding this comment

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

Why not always return partition.ID? Won't this always be either "aws" or "aws-gov"? If it errors out, isn't that a bad sign? I suspect this is a "Chas doesn't understand Go" problem, but I'm curious nevertheless.

cmd/main.go Outdated
Comment on lines 456 to 461
func getPartition(region string) string {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region); ok {
return partition.ID()
}
return "aws"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func getPartition(region string) string {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region); ok {
return partition.ID()
}
return "aws"
}
func getPartition(region string) (string, error) {
partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), region)
if !ok {
return "", fmt.Errorf("Error finding partition for region: %s", region)
}
return partition.ID(), nil
}

cmd/main.go Outdated
RoleARN: fmt.Sprintf("arn:aws:iam::%v:role/%v",
options.AwsAccountID, options.Role),
RoleARN: fmt.Sprintf("arn:%s:iam::%d:role/%s",
getPartition(options.AwsRegion),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
getPartition(options.AwsRegion),
partition,

Then add above something to call getPartition like:

partition, err := getPartition(options.AwsRegion)
	if err != nil {
		fmt.Println(err)
		fmt.Println("Using aws as partition")
		partition = "aws"
	}

If you want to avoid noping out of the process

@dynamike
Copy link
Contributor Author

dynamike commented May 13, 2020

@mdawn Thanks for keeping my Go honest. I made the suggested changes.

@cblkwell I think defaulting to the "aws" partition was a premature, and if it can't find the partition then bail out.

@dynamike dynamike merged commit 983b2da into master May 14, 2020
@dynamike dynamike deleted the mk-govcloud branch May 14, 2020 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants