Created
November 10, 2020 17:24
-
-
Save john-e/f1fa75ac558850ac50dec89e4b3b8e9a to your computer and use it in GitHub Desktop.
Create AWS Token when using MFA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| if [[ -z $AWS_MFA_DEVICE ]]; then | |
| MFA_DEVICE=${1} | |
| MFA_TOKEN=$2 | |
| else | |
| MFA_DEVICE=${AWS_MFA_DEVICE} | |
| MFA_TOKEN=${1} | |
| fi | |
| if [[ -z $MFA_DEVICE ]]; then | |
| read -p "Enter MFA Device serial number:" MFA_DEVICE | |
| fi | |
| if [[ -z $MFA_TOKEN ]]; then | |
| read -p "Enter MFA token:" MFA_TOKEN | |
| fi | |
| getJSONValue() { | |
| HAYSTACK=$1 | |
| KEY=$2 | |
| VALUE=`echo "$HAYSTACK" | grep $KEY | awk '{print $2}' | sed -e 's/^"//' -e 's/",$//' -e 's/"$//'` | |
| echo $VALUE | |
| } | |
| # fetch temp token from AWS | |
| TOKENS=`aws sts get-session-token --serial-number ${MFA_DEVICE} --token-code ${MFA_TOKEN}` | |
| ACCESS_KEY_ID=`getJSONValue "$TOKENS" "AccessKeyId"` | |
| SECRET_ACCESS_KEY=`getJSONValue "$TOKENS" "SecretAccessKey"` | |
| SESSION_TOKEN=`getJSONValue "$TOKENS" "SessionToken"` | |
| echo -e """ | |
| export AWS_ACCESS_KEY_ID=\"$ACCESS_KEY_ID\" | |
| export AWS_SECRET_ACCESS_KEY=\"$SECRET_ACCESS_KEY\" | |
| export AWS_SESSION_TOKEN=\"$SESSION_TOKEN\" | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment