Skip to main content

Command line tool to manage AWS CLI credentials with MFA (multi-factor authentication)

Project description

IAM MFA

This command line tool helps you manage AWS CLI credentials with MFA (multi-factor authentication).

This is intended as a companion to an IAM policy that requiring MFA everywhere, including on API access. One such policy is inculded in this readme.

The tool takes a source IAM profile (which should store your permanent credentials) and an MFA code and outputs API AWS credentials to a destination IAM profile. The output credentials are valid for 24 hours and are blessed with MFA (and thus can be used on APIs that require MFA).

Instalation

It's a pip library, installed by pip install iam-mfa.

It requires the AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html

Usage

iam-mfa <source_profile> <dest_profile> <username>

Arguments:

  • source_profile: The profile to use for calling sts get-session-token; this should contain your permanent AWS API credentials
  • dest_profile: The profile in which the temporary credentials will be saved; note that this profile will be overridden
  • username: The IAM username that owns the source_profile credentials

The MFA code is provided as an input to the script running so that it isn't stored in bash history.

AWS IAM Policy

It is recommended that you have an IAM policy on your AWS account that enforces the use of multi-factor authentication for all access.

Below is a policy that does the following:

  • Allow users to change their own password
  • Allow users to view and edit their own MFA devices (a necessary component to requiring them to have MFA)
  • Prevent users from doing most things if they logged in to the console without MFA
  • Still allow users to set up MFA if they logged in without it
  • Enforce these rules for both console and API aceess

It is best to create this as an IAM policy, and then attach it to any user groups that have human users. You can also attach the policy to a user directly, but that's harder to manage.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:ChangePassword",
                "iam:CreateVirtualMFADevice",
                "iam:EnableMFADevice",
                "iam:ResyncMFADevice",
                "iam:DeleteVirtualMFADevice",
                "iam:DeactivateMFADevice",
                "iam:ListMFADevices",
                "iam:ListVirtualMFADevices",
                "iam:ListAccessKeys",
                "iam:GetAccessKeyLastUsed",
                "iam:GetUser"
            ],
            "Resource": [
                "arn:aws:iam::*:mfa/${aws:username}",
                "arn:aws:iam::*:user/${aws:username}"
            ]
        },
        {
            "Sid": "BlockMostAccessUnlessSignedInWithMFA",
            "Effect": "Deny",
            "NotAction": [
                "iam:CreateVirtualMFADevice",
                "iam:ListVirtualMFADevices",
                "iam:EnableMFADevice",
                "iam:ResyncMFADevice",
                "iam:DeleteVirtualMFADevice",
                "iam:DeactivateMFADevice",
                "iam:ListAccountAliases",
                "iam:ListUsers",
                "iam:ListSSHPublicKeys",
                "iam:ListAccessKeys",
                "iam:GetAccessKeyLastUsed",
                "iam:ListServiceSpecificCredentials",
                "iam:ListMFADevices",
                "iam:GetAccountSummary",
                "iam:ChangePassword",
                "iam:GetUser",
                "sts:GetSessionToken"
            ],
            "Resource": "*",
            "Condition": {
                "BoolIfExists": {
                    "aws:MultiFactorAuthPresent": "false"
                }
            }
        }
    ]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iam-mfa-0.0.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

iam_mfa-0.0.1-py3.8.egg (5.4 kB view details)

Uploaded Source

File details

Details for the file iam-mfa-0.0.1.tar.gz.

File metadata

  • Download URL: iam-mfa-0.0.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.26.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for iam-mfa-0.0.1.tar.gz
Algorithm Hash digest
SHA256 461ad16fab2ba9fe8fa2af5bb3b94bfc8f6654fe1932fb058d463b493dfe686b
MD5 052912968b0359eb55d28ede18e88187
BLAKE2b-256 6ba14828a3d9095ca1c1459e9e74ca8d81446f2f13862292979c4d1752be7af1

See more details on using hashes here.

File details

Details for the file iam_mfa-0.0.1-py3.8.egg.

File metadata

  • Download URL: iam_mfa-0.0.1-py3.8.egg
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.26.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.10

File hashes

Hashes for iam_mfa-0.0.1-py3.8.egg
Algorithm Hash digest
SHA256 e2ab9338914eac8ceede5f30d407fc0b6ceb248a330107ef4eb6a6c3b9392e16
MD5 780c7c48fb34e1d1b518b27f1312b618
BLAKE2b-256 1ed59e6f44d1fda58f971971b025d0ddf8fe86b3b120fca1c94ac69e1bcc94dc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page