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

provider/aws: Allow account ID checks on EC2 instances & w/ federated accounts #5030

Merged
merged 2 commits into from
Apr 27, 2016

Conversation

radeksimko
Copy link
Member

I'm building on top of @ColinHebert 's PR and also recently merged PR in AWS SDK which made this fairly clean approach possible.

This is closing #3431

Here's an overview of the implemented approaches per provider:

provider/approaches 1st 2nd 3rd
EC2RoleProvider Metadata API ERR
EnvProvider iam:GetUser iam:ListRoles ERR
SharedCredentialsProvider iam:GetUser iam:ListRoles ERR
StaticCredentialsProvider iam:GetUser iam:ListRoles ERR
CustomProvider iam:GetUser iam:ListRoles ERR

I would love to add some tests for getAccountId() but mocking the IAM service seems quite tricky at the moment.

I have tested all of the providers (launched EC2 instance w/ instance profile + test all other providers each locally).

if url != "" {
log.Printf("[WARN] Using deprecated ENV variable AWS_METADATA_URL, " +
"use AWS_METADATA_HOST instead to set the hostname only")
return url
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this work? Don't we need to add /meta-data/iam/info? AWS_METADATA_URL used to be ts.URL+"/latest"
If it's deprecated, could failing completely here be an option?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, my assumption is that this has only been used in tests and never documented, so I was even thinking of just removing this ENV variable. Erroring out is another option. Unfortunately we don't have any nice way to return warnings from this scope.

Copy link
Member Author

Choose a reason for hiding this comment

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

AWS_METADATA_URL used to be ts.URL+"/latest"

Correct, that is why am I treating it differently and just take the whole URL, instead of building it.
That function is used in two places, have a look into the full diff - we call this API first when deciding whether to use EC2Role provider at all and then I used it here (with different path) to get the role ARN and Account ID from it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd be all for removing that Env variable, if it isn't documented (plus given the nature of the variable) it's safe to assume it hasn't been used and is safe to remove.

@radeksimko
Copy link
Member Author

@ColinHebert I think I addressed all of your comments. I have also separated the PR into more commits to make each refactoring phase isolated and more readable. Let me know if you see anything else.

Pinging @catsby as he was behind the recent December refactoring of this. Can you specifically look at eeea864 , please? As @ColinHebert suggested, this seems to be much cleaner approach to address that problem with metadata API timeout (which is 5 secs by default).

btw. after this refactoring which makes the code a bit cleaner, there's one concern left, see aws/aws-sdk-go#543

@ColinHebert
Copy link
Contributor

This is so awesome! Nice work @radeksimko!

@radeksimko
Copy link
Member Author

This is currently on hold.

I'm waiting for response from AWS in regards to suggestions on best way to verify EC2 metadata API endpoint: aws/aws-sdk-go#543 , https://forums.aws.amazon.com/thread.jspa?threadID=226140

@catsby catsby added the waiting-response An issue/pull request is waiting for a response from the community label Mar 30, 2016
@radeksimko
Copy link
Member Author

@catsby I will refactor this and then retest on real EC2 instance and w/ real federated credentials.

@radeksimko radeksimko changed the title provider/aws: Allow account ID checks on EC2 instances & w/ federated accounts [WIP] provider/aws: Allow account ID checks on EC2 instances & w/ federated accounts Mar 30, 2016
@radeksimko radeksimko force-pushed the account_check branch 6 times, most recently from f592303 to ff9ae5b Compare April 3, 2016 17:57
@radeksimko radeksimko removed the waiting-response An issue/pull request is waiting for a response from the community label Apr 3, 2016
@radeksimko
Copy link
Member Author

This is now refactored. I also spent some time creating mock IAM server which allowed me writing couple of tests.

Besides those tests I also tested this manually with real federated credentials and in a real EC2 instance w/ instance profile. Both worked ok.

I also exposed both methods (GetCredentials and GetAccountId) as public to make it useful for #5270

@radeksimko radeksimko changed the title [WIP] provider/aws: Allow account ID checks on EC2 instances & w/ federated accounts provider/aws: Allow account ID checks on EC2 instances & w/ federated accounts Apr 3, 2016
@stack72 stack72 mentioned this pull request Apr 11, 2016
5 tasks
@radeksimko
Copy link
Member Author

radeksimko commented Apr 22, 2016

This is now squashed into 2 commits (vendor + actual changes), as discussed w/ @catsby

The plan is to review & merge this after the next release (0.6.15). This is to give more time for testing of such a sensitive part of the provider "in the wild" to allow capturing any potential edge-cases/bugs that may not have been covered by tests.

@catsby
Copy link
Contributor

catsby commented Apr 27, 2016

LGTM, unit tests and I ran some acceptance tests locally which passed, I think we're good to merge here thanks!

@radeksimko radeksimko merged commit 0a8ea04 into hashicorp:master Apr 27, 2016
@radeksimko radeksimko deleted the account_check branch April 27, 2016 19:07
@radeksimko
Copy link
Member Author

Just FYI: Although account ID checking by itself does work with EC2 instances and federated credentials now, Terraform still calls iam:GetUser as part of credentials validation. This still fails on EC2 & w/ federated credentials.

Feel free to subscribe to #6523 where we track this issue.

@radeksimko
Copy link
Member Author

radeksimko commented May 8, 2016

Actually, apologies, what I said is not entirely true, it does work for some cases which can be characterised by the following error check for iam:GetUser:

if awsErr.Code() == "AccessDenied" || awsErr.Code() == "ValidationError" {

The linked issue & upcoming PR will hopefully actually validate the credentials at all cases instead of ignoring the check though, so the note is still partially valid.

@ghost
Copy link

ghost commented Apr 26, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants