Created
June 5, 2018 06:15
-
-
Save masami256/2361e3df9474e443a8493fbd237b6f1f to your computer and use it in GitHub Desktop.
AWSのコマンドラインインターフェースで2段階認証を使うための便利ツール
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
#!/bin/bash | |
# Before run this script, get authengication token via google authenticator app or other tool | |
if [ $# != 1 ]; then | |
echo "usage: $0 [token]" | |
exit 1 | |
fi | |
tokencode=$1 | |
which jq >/dev/null 2>&1 | |
if [ $? != 0 ]; then | |
echo "jq command is required" | |
exit 1 | |
fi | |
if [ "${MFA_AWS_DEVICE_ARN}" = "" ]; then | |
echo "please set MFA device's arn to MFA_AWS_DEVICE_ARN" | |
exit 1 | |
fi | |
tmpfile=$(mktemp) | |
aws sts get-session-token --serial-number "${MFA_AWS_DEVICE_ARN}" --token-code "${tokencode}" > ${tmpfile} | |
if [ $? = 0 ]; then | |
echo "Please run following command." | |
echo "export AWS_ACCESS_KEY_ID=$(jq '.Credentials.AccessKeyId' ${tmpfile})" | |
echo "export AWS_SECRET_ACCESS_KEY=$(jq '.Credentials.SecretAccessKey' ${tmpfile})" | |
echo "export AWS_SESSION_TOKEN=$(jq '.Credentials.SessionToken' ${tmpfile})" | |
else | |
echo "failed to get session" | |
fi | |
rm -f ${tmpfile} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment