Skip to main content

A 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 version 2 of the AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html. It uses the AWS CLI to manage the configuration and credential files: 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 called "RequireMFAEverywhere", 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.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iam_mfa-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file iam_mfa-0.1.0.tar.gz.

File metadata

  • Download URL: iam_mfa-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for iam_mfa-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5da983b896007a8ab8e175a4def8108b3e89f7bc27d2bb445b3ee0f35e7acf99
MD5 cc3a35c196334fa4a62acb02ec77067d
BLAKE2b-256 cddd09d1d8654df816efe05ff6b442923909448a76615c1562700b9b30489a38

See more details on using hashes here.

File details

Details for the file iam_mfa-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: iam_mfa-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for iam_mfa-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 245b70cf4ba27603aec6069d4523e170188fe60c52e499a91a9282d0e1744687
MD5 745c669c596d0cd7e07471b005aa9e93
BLAKE2b-256 186cbdc111165a3248c049d4121a580fd9159bbce5ed9d2332a6c83eee023177

See more details on using hashes here.

Supported by

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