aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

feature-request: support mfa_serial (consistency with aws cli)

Open ChintanRaval opened this issue 8 years ago • 14 comments

if a profile in ~/.aws/config includes mfa_serial option, then aws cli reads it and asks for MFA code:

Enter MFA code for arn:aws:iam::665544332211:mfa/myusername:

where as cdk simply errors out:

The security token included in the request is expired

for the time being, I use a hacky solution where i first use AWS CLI with a sample command like aws ec2 describe-availability-zones --profile my-profile which asks me for MFA token and then upon authentication, creates a file ~/.aws/cli/cache/xxxx.json from where I can extract the temp access key, secret and session token and set them as environment variables for terminal. then, AWS CDK utilises those values and works as expected.

Can mfa support please be added to cdk also please?

ChintanRaval avatar Nov 27 '18 00:11 ChintanRaval

I've never used this feature. Does the CLI request an MFA token on every request, or only once in a while?

rix0rrr avatar Nov 27 '18 10:11 rix0rrr

Only once in a while. The CLI caches the token in a file at ~/.aws/cli/cache/<some-guid>.json. This file also contains an expiration timestamp.

ChintanRaval avatar Nov 27 '18 17:11 ChintanRaval

+1 for this feature. This could be very useful to practice CDK in my organization.

solcyr avatar Dec 05 '18 08:12 solcyr

I think I'm also running into this while trying to use cdk deploy.

@ChintanRaval Any chance you could be more explicit about which variables you need to set? It sounds like you're using the returned credentials (sub)object, but I'm not sure which ENV keys to set with those values. Thanks!

Edit:

Found the variables in AWS documents: AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN

https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html

Setting these from the cached credentials object causes the CDK to properly function, when it previously wouldn't work with my profile. So I suspect something related to MFA.

Cached credentials are generated with aws --profile <me> s3 ls, which prompts me for an MFA code then caches the response.

ghost avatar Dec 17 '18 19:12 ghost

+1 cdk support mfa for IAM best practice https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#enable-mfa-for-privileged-users

angusfz avatar Apr 12 '19 03:04 angusfz

Our org only allows for accounts with MFA enabled using IAM. The workaround is ugly and not very accessible unless you're an advanced user.. The CDK (IMO) makes cloudformation more accessible, which is kind of the opposite of this workaround 😄 Hopefully this gets prioritised soon!

abbottships avatar Jun 07 '19 16:06 abbottships

Our organization also enforces MFA access, so until that is added we won't be able to use the CDK.

timoteialbu avatar Aug 23 '19 17:08 timoteialbu

I can't use CDK until MFA support is included.

ghost avatar Sep 04 '19 00:09 ghost

@dacoty You may can use my little helper script, to store temporary role session credentials in your credential file. It shares the same credential cache as aws cli. It supports MFA as well. Should work out of the box if you've installed aws cli already https://gist.github.com/qoomon/e08136f7d50df0cfac1e618c0de94fec

qoomon avatar Sep 04 '19 05:09 qoomon

JS SDK seems to support this now:

https://github.com/aws/aws-sdk-js/pull/2126

rix0rrr avatar Sep 25 '19 09:09 rix0rrr

I hate to resurrect this, but this issue/request is NOT resolved. At least not for people using Python anyway. I'm posting the following in case anyone else runs into the issue and hits this when using teh googles.

You have to use a 3rd party python library(aws-mfa) to get MFA to work with this. I've checked it out beforehand , no funkiness or typo squatting so the library seems safe. It would be nice if this can functionality can get included in the CDK project so we don't have to resort to using libs like this. Such as open source life i guess... Anyways.....

how to fix the problem:

  • download and install aws-mfa python library(https://github.com/broamski/aws-mfa)
  • in your credentials file, copy everything from [default] and make new entry called [default-long-term]
  • copy your MFA arn from your AWS environment( in IAM/users/username/security credentials/assigned mfa device)
  • run this command: aws-mfa --duration 1800 --device yourdevicearn
  • enter your MFA code when prompted
  • Rejoice and finally use cdk deploy for the next 1800 seconds.

ghost avatar Jan 06 '20 23:01 ghost

@dacoty You may can use my little helper script, to store temporary role session credentials in your credential file. It shares the same credential cache as aws cli. It supports MFA as well. Should work out of the box if you've installed aws cli already https://gist.github.com/qoomon/e08136f7d50df0cfac1e618c0de94fec

The link to this is broken, but you could script out the steps i have above if you wanted.

ghost avatar Jan 07 '20 12:01 ghost

This is a feature stopping us from adopting CDK as an official Deployer, so +1

mateja82 avatar Jun 09 '20 11:06 mateja82

I use https://github.com/99designs/aws-vault as a workaround. It supports MFA natively and is a tiny wrapper around the "cdk deploy: command basically.

My tipical workflow would like:

aws-vault exec --duration 8h mycompanyprofile // this one prompts for MFA and stores short term credentials for 8 hours == working day
cdk deploy // Notice there's no need for AWS_PROFILE or any other trickeries anymore because it uses the credentials, set in environment from the previous command

Dzhuneyt avatar Jun 30 '20 22:06 Dzhuneyt