Added get-session-creds-in-config-format.sh

This commit is contained in:
mgeeky 2019-05-14 18:17:23 +02:00
parent ea7701a590
commit da866502ac
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
#!/bin/bash
#
# This script simply calls `aws sts assume-role` using hardcoded parameters, in order
# to retrieve set of session credentials and reformat it into ~/.aws/credentials file format.
#
# Mariusz B., mgeeky '19
#
#
# Configure below variables.
#
PROFILE_NAME=your-profile-name
SERIAL_MFA=arn:aws:iam::<NUMBER>:mfa/<USER-NAME>
ROLE_NAME=Your_Role_Name
ROLE_ARN=arn:aws:iam::<NUMBER>:role/$ROLE_NAME
DURATION=42000
#
# ------------------------
#
read -p "Type your AWS MFA Code: " code
echo
out=$(aws sts assume-role --serial-number $SERIAL_MFA --role-arn $ROLE_ARN --role-session-name $ROLE_NAME --duration-seconds $DURATION --token-code $code)
if [ $? -eq 0 ]; then
echo "[$PROFILE_NAME]"
echo "$out" | python3 -c 'import sys,json; foo=json.loads(sys.stdin.read()); print("aws_access_key_id={}\naws_secret_access_key={}\naws_session_token={}".format(foo["Credentials"]["AccessKeyId"],foo["Credentials"]["SecretAccessKey"],foo["Credentials"]["SessionToken"]))'
echo
else
echo $out
fi