MyNixOS website logo
Description

Keep your AWS credentials file up to date with MFA-carrying credentials.

As described in http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_configure-api-require.html, some IAM policies may require API calls to be authenticated with MFA for additional security. This service tracks the lifetime of the temporary credentials used to authenticate with MFA, prompting the user for a new MFA code as the old credentials are about to expire and updating the AWS credentials file with the new credentials.

aws-mfa-credentials

Keep your AWS credentials file up to date with MFA-carrying temporary credentials

Usage

aws-mfa-credentials --mfa-serial-number SERIAL_NUMBER

See Locking protcol for important information about avoiding data loss!

From the API reference for GetSessionToken:

SerialNumber: The identification number of the MFA device that is associated with the IAM user who is making the GetSessionToken call. Specify this value if the IAM user has a policy that requires MFA authentication. The value is either the serial number for a hardware device (such as GAHT12345678) or an Amazon Resource Name (ARN) for a virtual device (such as arn:aws:iam::123456789012:mfa/user). You can find the device for an IAM user by going to the AWS Management Console and viewing the user's security credentials.

Optional flags:

  • --duration DURATION_SECONDS The lifetime of the credentials. Defaults to AWS default (43200 at the time of this writing).
  • --profile PROFILE The name of the profile whose credentials we're managing. Defaults to 'default'.
  • --refresh-lead-time LEAD_SECONDS The amount of time before credential expiry to refresh the credentials. Defaults to 300.

Environment variables:

  • PATH: A program named ssh-askpass, accepting a prompt as an argument and outputting a password to stdout, must be available on the path.
  • HOME: The home directory containing the .aws/credentials file

Operation

aws-mfa-credentials will request an MFA code from the user using ssh-askpass, request temporary credentials from Amazon, and update the credentials file with the temporary credentials.

The new credentials are placed into a profile named PROFILE-mfa, e.g. when the --profile flag is not given they are placed in default-mfa. Use the AWS_PROFILE environment variable or other profile-specifying mechanisms to use the credentials.

aws-mfa-credentials exits on any error, you may want to run it in a retry loop (with backoff).

Locking protocol

To allow safe usage of multiple instances of aws-mfa-credentials in parallel, a locking protocol is used for the credentials file: An exclusive fcntl lock is held on the file $HOME/.aws/.credentials.lock when writing (and thus data read before the lock is obtained must be rechecked within the lock).

Unfortunately, the AWS cli and other tools do not respect this protocol. Therefore, if you are modifying the credentials file in any way besides aws-mfa-credentials, you should either stop any existing instances of aws-mfa-credentials or obtain the lock yourself. Credential loss may occur if this is not followed!

Metadata

Version

0.1.0.0

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows