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

aws provider allowed_account_ids does not work with iam instance profile #3431

Closed
mnylen opened this issue Oct 7, 2015 · 18 comments
Closed

Comments

@mnylen
Copy link

mnylen commented Oct 7, 2015

provider "aws" {
  region = "${var.region}"
  allowed_account_ids = ["${var.allowed_account_id}"]
}

With this provider config, terraform apply fails when running on EC2 instance with IAM instance profile. The error message I'm getting this:

* Failed getting account ID from IAM: ValidationError: Must specify userName when calling with non-User credentials
      status code: 400, request id: [...]

If I remove the allowed_account_ids attribute, it starts to work.

@mnylen
Copy link
Author

mnylen commented Oct 7, 2015

Terraform version: 0.6.3

@stack72
Copy link
Contributor

stack72 commented Oct 7, 2015

@mnylen are you providing IAM keys as well? or just the region and allowed_account_ids?

@radeksimko
Copy link
Member

@stack72 I don't think he is.

when running on EC2 instance with IAM instance profile

which means that credentials are coming from the internal metadata service.

@apparentlymart
Copy link
Contributor

This is handled in config.go.

It looks like the implementation is to call iam:GetUser with no UserName argument. According to the docs that returns "the user making the request". Makes sense that this wouldn't work for an instance profile since that's not a user, and so there is no "current user" to return.

The particular error you are getting seems to have been fixed in b95e7a9, where it became silently ignored. However, I think a bug may still remain here: if the GetUser call fails then we presumably won't have a user ARN, and so line 248 will either produce a crash or an empty string.

So all of this is to say: as currently implemented it doesn't look like allowed_account_ids can work with instance profile credentials, since we depend on being able to retrieve the user object making the request. In the next version of Terraform this will behave differently, but may stil have some issues in this case.

@stack72
Copy link
Contributor

stack72 commented Oct 7, 2015

@radeksimko d'oh! Missed that :(

@apparentlymart
Copy link
Contributor

It looks like in the case where we're running with instance credentials we can get an instance profile ARN from the metadata endpoint http://169.254.169.254/latest/meta-data/iam/info.

Perhaps it would be sensible to detect whether we got the credentials from the environment or from the instance metadata, and do the account checks differently in the latter case so we'll get the right result from the metadata API.

@radeksimko
Copy link
Member

we can get an instance profile ARN from the metadata endpoint http://169.254.169.254/latest/meta-data/iam/info.

That looks like a good plan. 👍

@bwalding
Copy link

There doesn't appear to be a great solution for identifying the current account (they're all hacks) when dealing with IAM/STS assume-role generated credentials.

Since an account always has security groups - you could pull the OwnerId off the first entry. The downside for a regular program would be that it may not have access to security groups, but I would assume Terraform would always have at least read access to them.

aws ec2 describe-security-groups

@radeksimko
Copy link
Member

Since an account always has security groups

I'm afraid this isn't true. It is possible to remove the default ones in the default VPC and I wouldn't rely on any security groups outside of default VPC either. You don't really need any SGs in cases where you're running background jobs for example.

I think that gracefully falling back to the metadata endpoint if IAM call fails is ok.
Here's an example of response from that endpoint:

{
  "Code" : "Success",
  "LastUpdated" : "2015-10-28T16:49:39Z",
  "InstanceProfileArn" : "arn:aws:iam::101636750127:instance-profile/aws-elasticbeanstalk-ec2-role",
  "InstanceProfileId" : "AIPAREU6ES7PV7RYRYPIC"
}

@apparentlymart
Copy link
Contributor

Right... I think it doesn't even need to be implemented as a fallback, because Terraform "knows" whether it got the credentials from the instance metadata, and so it knows it's using an instance profile without needing to try the initial GetUser request to see if it will fail.

@catsby
Copy link
Contributor

catsby commented Nov 30, 2015

Hey all – doing a review of old issues , is this still relevant?

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

This is still relevant. Essentially #3313 caused that allowed_account_ids/forbidden_account_ids are being ignored on instances w/ IAM Instance Profiles.

@radeksimko radeksimko removed the waiting-response An issue/pull request is waiting for a response from the community label Dec 1, 2015
@roderickrandolph
Copy link

As a fallback when a user name is not provided (i.e. STS roles/credentials) why not attempt to grab the first IAM user's ARN within the account and parse out that user's account ID. It would require the iam:ListUsers permission which is one more than the iam:GetUser permission that's currently required for allowed_account_ids / forbidden_account_ids to work anyway.

It's certainly a hack but it'll work since all users within an account will have the same ID and terraform requires at least one user to exist.

@roderickrandolph
Copy link

I hit this problem while using terraform and STS outside of an instance (i.e. where instance metadata is not available)

@roderickrandolph
Copy link

d'oh looks like #4290 may solve the problem

@radeksimko
Copy link
Member

See #5030
I just tested that ^ with both EC2 & instance profile and federated account and it worked aok.
Any feedback is welcomed, especially before we merge that PR.

@radeksimko
Copy link
Member

Fixed via #5030 and #6536 which is also re-enabling credentials validation on EC2 instance (will be part of 0.7.0 release).

@ghost
Copy link

ghost commented Apr 24, 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 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants