Created
February 27, 2020 15:31
-
-
Save alertedsnake/5d3c7c85da75ce5dee9536027c34e034 to your computer and use it in GitHub Desktop.
Bash function to do AWS 2-factor auth
This file contains 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
# | |
# Bash function to peform AWS 2-factor auth. | |
# | |
# This assumes you have $AWS_CREDENTIAL_FILE set to an actual credentials file, and | |
# AWS_PROFILE set to the appropriate profile in said file. | |
# | |
awsmfa() { | |
unset AWS_ACCESS_KEY_ID | |
unset AWS_AWS_SECRET_ACCESS_KEY | |
unset AWS_SESSION_TOKEN | |
aws_code=$1 | |
if [ -z "$1" ]; then | |
read -sp 'MFA Code: ' aws_code | |
echo | |
fi | |
AWS_MFA_ID=$(aws iam list-mfa-devices | jq -r '.MFADevices[0].SerialNumber') | |
AWS_STS_AUTH=$(aws sts get-session-token --serial-number "${AWS_MFA_ID}" --token-code "$aws_code") | |
export AWS_ACCESS_KEY_ID="$(echo "$AWS_STS_AUTH" | jq -r .Credentials.AccessKeyId)" | |
export AWS_SECRET_ACCESS_KEY="$(echo "$AWS_STS_AUTH" | jq -r .Credentials.SecretAccessKey)" | |
export AWS_SESSION_TOKEN="$(echo "$AWS_STS_AUTH" | jq -r .Credentials.SessionToken)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment