-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 Assume Role Doesn't work with MFA Enforced Roles #5078
Comments
It seems the assume-role with a backend provider is not working. Once I modified my backend bucket policy I was able to use the assumed-role in my regular provider for everything else. |
I am using the s3 backend and I don't use the |
I have using Terraform via the script to use AssumeRole with MFA. #!/bin/bash
set -e
# It generates json referring to the processing of `AssumeRoleCredentialFetcher` and `_create_cache_key`.
#
# memo:
# * https://github.com/boto/botocore/blob/1.12.162/botocore/credentials.py#L611
# * https://github.com/boto/botocore/blob/1.12.162/botocore/credentials.py#L690-L692
ROLE_ARGS=$(cat - << EOS
{
"RoleArn": "$(aws configure get role_arn)",
"SerialNumber": "$(aws configure get mfa_serial)"
}
EOS
)
CREATE_CACHE_KEY=$(cat - << EOS
import sys, os, json;
from hashlib import sha1;
args = json.load(sys.stdin);
hash = sha1(json.dumps(args, sort_keys=True)).hexdigest();
print hash.replace(':', '_').replace(os.path.sep, '_').replace('/', '_');
EOS
)
CACHE_KEY=$(echo -n "$ROLE_ARGS" | python -c "$CREATE_CACHE_KEY")
CACHE_PATH="$HOME/.aws/cli/cache/$CACHE_KEY.json"
if [ -e "$CACHE_PATH" ]; then
EXPIRATION=$(cat "$CACHE_PATH" | jq --raw-output .Credentials.Expiration)
EXPIRATION_UNIX=$(date -u -jf %FT%TZ $EXPIRATION +%s)
NOW_UNIX=$(date +%s)
if [ $EXPIRATION_UNIX -lt $NOW_UNIX ]; then
aws sts get-caller-identity > /dev/null
fi
else
aws sts get-caller-identity > /dev/null
fi
export AWS_ACCESS_KEY_ID=$(cat $CACHE_PATH | jq -r .Credentials.AccessKeyId)
export AWS_SECRET_ACCESS_KEY=$(cat $CACHE_PATH | jq -r .Credentials.SecretAccessKey)
export AWS_SESSION_TOKEN=$(cat $CACHE_PATH | jq -r .Credentials.SessionToken)
$* This script uses
You can use AssumeRole with MFA as follows: $ AWS_PROFILE=role-with-mfa mfa terraform plan
Enter MFA code for arn:aws:iam::128716708097:mfa/cli-user: |
Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label. If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you! |
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. |
I am reopening #472, that issue is marked as "Closed" but still has active discussion as the issue isn't fixed.
I have a user with no access to anything other than the ability to assume a role.
The role has the policies and trust rules as shown in this Gist [1].
I use one profile for holding my standard Access ID & Secret Token, I use a tool to create an profile with MFA access stored in the indicated configuration profile
session_profile
With the MFA requirement on the Role's Trust policy the following happens:
As soon as I remove the MFA requirement:
I know that the Role logic and permissions work, as I can access the bucket via the Console.
Community Note
Terraform Version
Terraform v0.11.7
Affected Resource(s)
s3 backend and AWS Profile
Expected Behavior
Terraform should initialize correctly and plan/apply should work using the specified Role ARN.
Actual Behavior
Terraform init fails and apply fails due to no MFA.
Steps to Reproduce
(See the associated Gist)
terraform init
using the profileReferences
[1] - https://gist.github.com/bstopp/c62ad43629865e9529d3c7350fa39791
The text was updated successfully, but these errors were encountered: