-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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-cli fails to acquire session token before issuing sts:AssumeRole call #2279
Comments
Actually... I was unable to acquire the role manually either. It seems probable that this is a botocore issue rather than a aws-cli issue. To add to this, Terraform acquires the role fine using just the profile name. |
Alright, so I can confirm that getting assume role can be tricky. Here's what you need:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::ACCOUNT-ID:role/RoleToAssume"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::ACCOUNT-ID:user/Assumer"
},
"Action": "sts:AssumeRole",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
}
]
}
[assume-source]
aws_access_key_id = EXAMPLE
aws_secret_access_key = EXAMPLE
[profile assume-source]
output = json
region = us-west-2
[profile assume]
output = json
region = us-west-2
role_arn = arn:aws:iam::ACCOUNT-ID:role/RoleToAssume
source_profile = assume-source
mfa_serial = arn:aws:iam::ACCOUNT-ID:mfa/Assumer Once all that is set up, you run some command. Let's say my role gives me S3 access, so I would do this: > aws s3 ls --profile assume
Enter MFA code:
2016-11-14 17:39:43 somerandombucketname Can you confirm you have a similar setup? |
Hi Jordan, Thanks for investigating. I can confirm that this is exactly the config I used. In my previous tests, I had placed the MFA configuration in my credentials file. I tried putting the source IAM user's credentials in the credentials file and I created a profile for the source user and role in the config file. The end result was the same:
I can confirm that the IAM policies are exactly as you documented. I can also confirm that my IAM user can assume the role via the GUI using MFA, and that I can assume the role on the command line using Terraform (GO AWS SDK.) The issue is specific to boto. As an added comment, I can acquire a MFA authenticated session token by hand. Terraform / GO AWS SDK will happily use those credentials (set using environment variables,) however the AWS CLI continues to throw a Access Denied error. I did my best to try different config permutations, to try older versions of Boto/AWS CLI, and to review the source code for both Boto and AWS cli looking for any obvious issues. As best I can tell, Boto simply isn't acquiring an MFA authenticated session token prior to issuing the sts:AssumeRole call. I'm not ruling out PEBCAK, but I did cover a lot of this before filing a bug. |
I can see in your posted debug log that the token is coming from somewhere: Could you do Also note that go does not support mfa (source). Does it work if you drop the mfa token? |
Hi Jordan, The AWS CLI client correctly prompts for a MFA token; that's where the value is coming from. However, the prompted token isn't passed to AWS. This is based on the output from cloudtrail. Note that while the MFA token is prompted for, both current/valid and invalid codes produce the same result. The GO SDK doesn't support the MFA config option, but it will work normally if I generate a MFA authenticated session by hand. Unfortunately, the same approach doesn't work with the CLI; even if I correctly configure my environment to use a MFA authenticated session, the call to sts:AssumeRole fails and CloudTrail reports no MFA key. E.g. if I export I am testing this on an account that has minimal native access; there is zero chance Terraform works without acquiring a role, and I can confirm that it fails to acquire the role throwing an error if the environment variables are not set, or not correct. As best I can tell, boto-core simply ignores the existing session when requesting a new role session. |
Im having same problem. Is this really a bug in boto-core? How is this not impacting lots of people? |
@cbarbour I noticed in your initial post, it appears you have an extra '/' in the role_arn.
Original: Should read: I made that change on my end and authentication is failing following the same recipe as described in this thread. Without the slash, my MFA auth is fine (again, with the same recipe described in this thread). I don't think that's proper arn syntax. |
Thanks @dhollema I reviewed my config to see if I had that same problem. My busted config: Now working: |
Kicking this issue to note that this same issue can come from a range of |
Here's a twist! The AWS Cli caches the supposedly failed results in .aws/cli/cache/ and the contents are perfectly valid and usable. I extract the seesion_token and secrets from the JSON and works like a champ. The error below despite PROMPTING me for the Token and accepting it. This smells strongly of a problem in botocore.
|
I probably need to open a separate issue but Botocore is doing 2 POST operations. The first one detects that the cached creds have expired and re-prompts me for a TOKEN and via a POST gets a new set of keys and session_token.
Please note the POST request it purports to generate looks like it's missing several of the data fields seen above. I don't know if that's correct.
STS returns a valid HTTP Response object, so far so good?
It is this second and seemingly gratuitous request that results in the error in the prior post. |
Even after i changed my aws configuration to assume the role am getting the same problem
my configuration
|
Same issue here, even without MFA set up. Running into AccessDenied errors when using If I try to use |
Same problem here, my config : /.aws/credentials
/.aws/config
other_profile IAM policy
ROLE_TO_ASSUME IAM trust policy
Then the command
produces |
can this design be worse? |
I have a similar problem to above..it seems that the command |
So if '--serial-number' is omitted (aka inferred from ~/.aws/config) and the token-code is prompted for interactively, or alternatively '--profile' is not specified (inferred from env:AWS_PROFILE), the command succeeds reliably, like this?
Ok, I can confirm. Evidently there is considerable neglect to check and validate inputs within the Boto functions. For instance I also had a case where some AWS_SESSION_* variables were empty or malformed while I was testing. The Boto code didn't bother to check any of them for validity (many cases of the source code checks against 'None' instead of 'not blank or None') and submitted them to the API only for it to complain about not seeing the expected payload format. BTW I finally pin-pointed what this 2nd web call was for - validating and effectively re-fetching the XML describing the session object in order to then print the contents. The logic is such that despite JUST having gotten a session object from the API (XML) and saved it to the local cache in JSON, or having loaded and validated a cached session (json), Boto makes this 2nd API call using 'X-Amz-Security-Token' to once again "describe the session" to itself. Clearly someone didn't think thru the program flow properly. It's during this second call that if you had specified '--serial-number' on the command-line (or I guess '--profile' too) that the previously resident values and MFA Tokens are not carried over. Why in the world a "validate/describe" of 'X-Amz-Security-Token' requires MFA at all is beyond me. The Token is either valid, invalid, or expired.
|
If I omit serial number and token-code the command succeeds, however I do not get prompted for an MFA token (which shouldn't happen as MFA is enabled on the account). When using multiple profiles, if the profile flag is omitted then it succeeds as expected.
|
the AWS CLI doesn't query your account to see if it needs to prompt you for an MFA. It is simply keying off of whether 'mfa_serial' is present in the AWS_CONFIG_FILE and AWS_PROFILE stanza you're using. |
In my instance, a subtle casing issue was the culprit, after following a template issued by a system administrator and. Note that the MFA device ARN is case sensitive. Copy and paste into your config file, to be on the safe side. You can confirm your MFA device is working on cli with:
Might give you some faith (as it did me). |
I am also experiencing this error while running |
looks like need first to get the temporary credentials for the source profile, then use those credentials to assume to target profile/account.
|
Looks like this has been addressed with a number of suggestions. @cbarbour @gadelkareem are you still having trouble? |
Greetings! It looks like this issue hasn’t been active in longer than a week. We encourage you to check if this is still an issue in the latest release. Because it has been longer than a week since the last update on this, and in the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or add an upvote to prevent automatic closure, or if the issue is already closed, please feel free to open a new one. |
I have this issue, intermittent "MultiFactorAuthentication failed with invalid MFA one time pass code. " error. |
AWS CLI fails while attempting to issue API calls with MFA authentication. It appears to be issuing a
sts:AssumeRole
API call without generating or passing an appropriate session token as part of the call.As a result, the use of MFA authentication with a role fails.
Here's the basic config:
I invoke the role like this:
Here's the debug output as a GIST:
https://gist.github.com/cbarbour/f3785cb5111009e27d6d1517b1bc5171
I've confirmed that the MFA token works fine with the web interface.
I've also confirmed that I can generate a MFA authenticated session on the command line manually, allowing me to acquire the role.I've enabled cloudtrace, and confirmed that the failed authentication attempts do not contain the appropriate credentials.
Normally, I'd expect to see a user identity like this:
However, when I attempt to authenticate using a role, the resulting cloudtrail entry looks like this:
I suspect this is a Boto bug rather than an aws-cli bug, however I felt it best to file here since I haven't attempted to reproduce the problem using botocore.
The text was updated successfully, but these errors were encountered: