Skip to content

Instantly share code, notes, and snippets.

@ch1bo
Last active March 25, 2020 15:56
Show Gist options
  • Save ch1bo/39f288820a8adb5483d74e0dc510c06c to your computer and use it in GitHub Desktop.
Save ch1bo/39f288820a8adb5483d74e0dc510c06c to your computer and use it in GitHub Desktop.
AWS environment setup with MFA devices
#!/usr/bin/env bash
set -e
SERIAL_NUMBER=$1
TOKEN_CODE=$2
if [ -z "${SERIAL_NUMBER}" ] || [ -z "${TOKEN_CODE}" ]; then
# Clear when called with eval
>&2 echo "# Clearing credentials"
echo "unset AWS_ACCESS_KEY_ID"
echo "unset AWS_SECRET_ACCESS_KEY"
echo "unset AWS_SESSION_TOKEN"
echo "unset AWS_PROFILE"
>&2 echo "# To request credentials: eval \$($0 MFA-SERIAL-NUMBER TOKEN-CODE)"
exit 2
fi
# Clear within session to retrieve new tokens
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
RESULT=$(aws sts get-session-token --serial-number ${SERIAL_NUMBER} --token-code ${TOKEN_CODE})
EXPIRATION=$(echo ${RESULT} | jq '.Credentials.Expiration')
ACCESS_KEY_ID=$(echo ${RESULT} | jq '.Credentials.AccessKeyId')
SECRET_ACCESS_KEY=$(echo ${RESULT} | jq '.Credentials.SecretAccessKey')
SESSION_TOKEN=$(echo ${RESULT} | jq '.Credentials.SessionToken')
>&2 echo "# Got credentials, valid until ${EXPIRATION}"
echo "export AWS_PROFILE=${AWS_PROFILE}"
echo "export AWS_ACCESS_KEY_ID=${ACCESS_KEY_ID}"
echo "export AWS_SECRET_ACCESS_KEY=${SECRET_ACCESS_KEY}"
echo "export AWS_SESSION_TOKEN=${SESSION_TOKEN}"
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment