How to use MFA with AWS CLI?

前端 未结 13 1185
太阳男子
太阳男子 2020-12-05 00:05

How do I type in the MFA code when using the AWS CLI? I have checked the documentation page of IAM http://docs.aws.amazon.com/cli/latest/reference/iam/index.html.

I

相关标签:
13条回答
  • 2020-12-05 00:28

    The CLI can manage a lot of this for you if you're using roles. Described here: http://docs.aws.amazon.com/cli/latest/userguide/cli-roles.html

    In my credentials file I have:

    [my_iam_user]
    aws_access_key_id = AKIABLAHBLAHBLAHBLAH
    aws_secret_access_key = <blah>
    region = us-east-1
    
    [my_admin_role]
    role_arn = arn:aws:iam::123456789123:role/my_admin_role
    source_profile = my_iam_user
    mfa_serial = arn:aws:iam::123456789123:mfa/my_iam_user
    region = us-east-1
    

    Note the mfa_serial entry. You can get this value from your user details in the AWS IAM console. This entry tells the CLI that MFA is required for that role.

    When I call aws s3 ls --profile my_admin_role it says Enter MFA code:, after I paste in the code it returns the listing.

    Note: I haven't found a way to get the CLI to ask for MFA when calling a user profile (--profile my_iam_user) only calling a role profile triggers the MFA request.

    The MFA token is then carried forward and the user profile can be used as well:

    aws sts get-caller-identity --profile my_iam_user
     # {
     # "Account": "123456789123",
     # "UserId": "AIDABLAHBLAHBLAHBLAH",
     # "Arn": "arn:aws:iam::123456789123:user/my_iam_user"
     # }
    
    aws sts get-caller-identity --profile my_admin_role
     # {
     # "Account": "123456789123",
     # "UserId": "AROABLAHBLAHBLAHBLAH:AWS-CLI-session-1234567890",
     # "Arn": "arn:aws:sts::123456789123:assumed-role/my_admin_role/AWS-CLI-session-1234567890"
     # }
    
    0 讨论(0)
  • 2020-12-05 00:31

    I have published a PR for aws-cli, which will allow to use mfa_serial in the credentials, that will force you to enter the token before making request to AWS (and it will be cached while token is valid)

    • Issue: https://github.com/aws/aws-cli/issues/3172
    • botocore PR: https://github.com/boto/botocore/pull/1399
    • aws-cli PR: https://github.com/aws/aws-cli/pull/3174

    Feel free to vote, if you want to get it in.

    0 讨论(0)
  • 2020-12-05 00:36

    Wrote a tool to add MFA support for standard IAM user profiles until @outcoldman PR gets merged: https://github.com/tongueroo/aws-mfa-secure

    Setup for those in a hurry

    1. Install gem
    gem install aws-mfa-secure
    
    1. Setup your ~/.aws/credentials with mfa_serial

    ~/.aws/credentials:

    [mfa]
    aws_access_key_id = BKCAXZ6ODJLQ1EXAMPLE
    aws_secret_access_key = ABCDl4hXikfOHTvNqFAnb2Ea62bUuu/eUEXAMPLE
    mfa_serial = arn:aws:iam::112233445566:mfa/MFAUser
    
    1. Add the alias to your ~/.bash_profile
    alias aws="aws-mfa-secure session"
    

    Restart your terminal.

    Example with Output

    $ export AWS_PROFILE=mfa
    $ aws s3 ls
    Please provide your MFA code: 751888
    2019-09-21 15:53:34 my-example-test-bucket
    $ aws s3 ls
    2019-09-21 15:53:34 my-example-test-bucket
    $
    

    Assume Role Profiles

    Assume role profiles work already for the AWS CLI, here's an example:

    ~/.aws/credentials:

    [mfa]
    aws_access_key_id = BKCAXZ6ODJLQ1EXAMPLE
    aws_secret_access_key = ABCDl4hXikfOHTvNqFAnb2Ea62bUuu/eUEXAMPLE
    mfa_serial = arn:aws:iam::112233445566:mfa/MFAUser
    
    [assumed-role]
    role_arn = arn:aws:iam::112233445566:role/Admin
    source_profile = mfa
    role_session_name = MFAUser
    mfa_serial = arn:aws:iam::112233445566:mfa/MFAUser
    
    0 讨论(0)
  • 2020-12-05 00:38

    My use-case is I have a root account where all IAM users are created and assigned to IAM groups which in turn have the capability to assume roles on a different account with varying degree of access depending on the group they are on. I have a few house rules in place;

    1. No one is allowed to do anything on the root account except to manage their own IAM Users account.
    2. Required password reset.
    3. Required MFA.
    4. You cannot switch accounts without logging in with MFA.

    This has been set up using AWS Shared Organizations.

    Previously, I've been using a python script I wrote to let my users to login via cli with MFA and switch accounts. This is done by manipulating the ~/.aws/credentials.

    I've since migrated to using this project https://gitlab.com/severity1/aws-auth, which is written in Go and allows me to do the same without much setup and it works on windows, macosx and linux.

    This effectively gives all my users the ability to do local testing while developing Apps for AWS without having to hardcode AWS Credentials into their code.

    0 讨论(0)
  • 2020-12-05 00:38

    Run the sts get-session-token AWS CLI command, replacing the variables with information from your account, resources, and MFA device:

    $ aws sts get-session-token --serial-number arn-of-the-mfa-device --token-code code-from-token

    https://aws.amazon.com/premiumsupport/knowledge-center/authenticate-mfa-cli/

    0 讨论(0)
  • 2020-12-05 00:38

    We documented a few considerations for AWS API multifactor in general (where to add the conditions, what are the implications etc.) in the documentation for some custom tooling (https://github.com/kreuzwerker/awsu) we developed for using Yubikeys as source for the TOTP tokens. This makes working with roles and long-term credentials + session tokens pretty easy.

    0 讨论(0)
提交回复
热议问题