Skip to content

Instantly share code, notes, and snippets.

@yareeh
Created January 6, 2021 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yareeh/b39a48d342c529801fdddfbf0d95a0d8 to your computer and use it in GitHub Desktop.
Save yareeh/b39a48d342c529801fdddfbf0d95a0d8 to your computer and use it in GitHub Desktop.
YubiKeyAwsCliZsh
# Add this to your zshrc
# To set up the YubiKey to work with AWS CLI check
# https://scalesec.com/blog/why-your-yubikey-wont-work-with-aws-cli/
alias oath='ykman oath code -s $@ | pbcopy'
# Usage:
# awsCliSession <your-aws-profile-name>
# Your AWS profile name should also match the yubikey OATH key name for this to work
awsCliSession () {
mfa=$(aws iam list-mfa-devices --profile "$1" --query 'MFADevices[0].SerialNumber' --output text)
credentials=$(aws sts get-session-token --profile "$1" --serial-number "${mfa}" --token-code $(ykman oath code -s "$1"))
export AWS_PROFILE=$1
export AWS_ACCESS_KEY_ID=$(echo $credentials | jq -r '.Credentials.AccessKeyId')
export AWS_SECRET_ACCESS_KEY=$(echo $credentials | jq -r '.Credentials.SecretAccessKey')
export AWS_SESSION_TOKEN=$(echo $credentials | jq -r '.Credentials.SessionToken')
}
alias awsLogout='unset AWS_PROFILE AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment