Using aws-cli and oathtool

Seamlessly two-factor authentication for the AWS CLI

Published: Oct 27, 2019 by martoc

Using two factor authentication with the AWS CLI is sometimes a pain, you need to get a new token every N minutes then parse the result of this operation and create the corresponding environment variables, I’ve installed the oath-toolkit and configured the AWS CLI to get these OTP dynamically.

Steps

  • Install oath-toolkit
brew install oath-toolkit

For more information about Homebrew please visit https://brew.sh/

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "*"
      ],
      "Resource": [
        "*"
      ],
      "Condition": {
        "Bool": {
          "aws:MultiFactorAuthPresent": "true"
        }
      }
    }
  ]
}
aws configure
  • Now to access your API you need to create a new set of credentials calling the AWS STS service, in the following command replace the ACCOUNT_ID and USERNAME.
aws sts get-session-token --region us-east-1 --serial-number arn:aws:iam::<ACCOUNT_ID>:mfa/<USERNAME> --token-code $(oathtool --base32 --totp $(cat ~/.aws-mfa))
  • The output of the previous command returns a credential for accessing the AWS API.

  • Install jq.

brew install jq
  • Add the following function in your ``~/.zshrc`.
aws-login() {
  OUTPUT=$(aws sts get-session-token --region us-east-1 --serial-number arn:aws:iam::<ACCOUNT_ID>:mfa/<USERNAME> --token-code $(oathtool --base32 --totp $(cat ~/.aws-mfa)))
  export AWS_ACCESS_KEY_ID=$(echo $OUTPUT | jq .Credentials.AccessKeyId --raw-output)
  export AWS_SECRET_ACCESS_KEY=$(echo $OUTPUT | jq .Credentials.SecretAccessKey --raw-output)
  export AWS_SESSION_TOKEN=$(echo $OUTPUT | jq .Credentials.SessionToken --raw-output)
  export AWS_SECURITY_TOKEN=$AWS_SESSION_TOKEN
}
  • You can open a new terminal and execute the following command to authenticate to your account.
aws-login

Share

Latest Posts

AWS KMS Key Replication
AWS KMS Key Replication

When architecting cloud-based solutions, one key principle I follow is to isolate resources within their respective regions and avoid sharing or replicating them across regions. This approach consistently provides a more secure and compliant framework for business continuity. Recently, AWS has introduced replication capabilities for various resources. In this post, I will delve into AWS Key Management Service (KMS) and assess whether adopting replication for KMS keys offers tangible benefits.

AWS VPN Client
AWS VPN Client

Amazon Web Services (AWS) offers a VPN Client that is particularly advantageous for organizations seeking scalable and secure connectivity solutions compared to traditional VPN services like NordVPN. This distinction is largely due to the inherent flexibility and elasticity of cloud-based services provided by AWS, tailored to meet the dynamic requirements of modern businesses.

Analysis of Github Actions
Analysis of Github Actions

The examination of GitHub Actions involves a comprehensive evaluation of its functionalities, features, and overall effectiveness. GitHub Actions is a powerful tool for automating workflows within the GitHub platform, enabling seamless integration and continuous delivery processes. It facilitates the automation of tasks such as code compilation, testing, and deployment, contributing to an efficient and streamlined development pipeline.