2019-05-14 18:17:23 +02:00
|
|
|
#!/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.
|
|
|
|
#
|
2020-06-19 14:14:57 +02:00
|
|
|
# Mariusz B., mgeeky '19-20
|
2019-05-14 18:17:23 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
#
|
2019-05-16 11:00:46 +02:00
|
|
|
# --------------------------
|
2019-05-14 18:17:23 +02:00
|
|
|
# Configure below variables.
|
|
|
|
#
|
2019-05-16 11:00:46 +02:00
|
|
|
|
2020-06-19 14:31:53 +02:00
|
|
|
# Below two values are REQUIRED
|
2020-06-19 14:14:57 +02:00
|
|
|
PROFILE_NAME=
|
|
|
|
ROLE_NAME=
|
|
|
|
|
|
|
|
# If left empty, will be deduced from `aws sts get-caller-identity` output.
|
|
|
|
ACCOUNT_NUMBER=
|
2019-05-16 10:37:11 +02:00
|
|
|
|
2020-06-19 14:31:53 +02:00
|
|
|
# If left empty, will use ROLE_NAME
|
|
|
|
SESSION_NAME=
|
|
|
|
|
2019-05-16 11:00:46 +02:00
|
|
|
# If you leave this field empty - it will be deduced from `aws sts get-caller-identity` output
|
2019-05-16 10:37:11 +02:00
|
|
|
#SERIAL_MFA=arn:aws:iam::<NUMBER>:mfa/<USER-NAME>
|
|
|
|
SERIAL_MFA=
|
|
|
|
|
2019-05-16 11:00:46 +02:00
|
|
|
# Duration in seconds. Values possible range: 900-43200
|
|
|
|
# 1 hour - 3600, 2 hours - 7200, 3 hours - 10800, 6 hours - 21600, 12 hours - 43200
|
2020-06-19 14:23:11 +02:00
|
|
|
DURATION=3600
|
2019-05-14 18:17:23 +02:00
|
|
|
|
|
|
|
#
|
2019-05-16 11:00:46 +02:00
|
|
|
# --------------------------
|
2019-05-14 18:17:23 +02:00
|
|
|
#
|
|
|
|
|
2019-05-16 10:37:11 +02:00
|
|
|
# Some times assume-role may return with an Access-Denied if there were no account authenticated
|
|
|
|
# regular commands sent first.
|
2020-06-19 14:14:57 +02:00
|
|
|
out=$(aws --profile $PROFILE_NAME sts get-caller-identity)
|
2019-05-16 10:37:11 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
2020-06-19 14:14:57 +02:00
|
|
|
echo "[!] Could not get caller's identity: "
|
|
|
|
echo "$out"
|
|
|
|
exit 1
|
2019-05-16 10:37:11 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$SERIAL_MFA" = "" ]]; then
|
2020-06-19 14:14:57 +02:00
|
|
|
SERIAL_MFA=$(echo "$out" | python3 -c "import sys,json; foo=json.loads(sys.stdin.read()); print('arn:aws:iam::{}:mfa/{}'.format(foo['Account'], foo['Arn'].split('/')[1]))" )
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$ACCOUNT_NUMBER" = "" ]]; then
|
|
|
|
ACCOUNT_NUMBER=$(echo "$out" | python3 -c "import sys,json; foo=json.loads(sys.stdin.read()); print(foo['Account'])" )
|
2019-05-16 10:37:11 +02:00
|
|
|
fi
|
|
|
|
|
2020-06-19 14:31:53 +02:00
|
|
|
if [[ "$SESSION_NAME" = "" ]]; then
|
|
|
|
SESSION_NAME=$ROLE_NAME
|
|
|
|
fi
|
2020-06-19 14:14:57 +02:00
|
|
|
|
|
|
|
ROLE_ARN=arn:aws:iam::$ACCOUNT_NUMBER:role/$ROLE_NAME
|
|
|
|
|
|
|
|
echo "[.] Using Role ARN: $ROLE_ARN"
|
|
|
|
|
|
|
|
read -p "Type your AWS MFA Code (leave empty if not needed): " code
|
2019-05-14 18:17:23 +02:00
|
|
|
echo
|
|
|
|
|
2020-06-19 14:14:57 +02:00
|
|
|
if [[ "$code" = "" ]] || [[ "$SERIAL_MFA" == "" ]]; then
|
|
|
|
echo "[.] MFA not provided, will attempt to assume role without it."
|
2020-06-19 14:31:53 +02:00
|
|
|
out=$(aws --profile $PROFILE_NAME sts assume-role --role-arn $ROLE_ARN --role-session-name $SESSION_NAME --duration-seconds $DURATION 2>&1)
|
2020-06-19 14:14:57 +02:00
|
|
|
else
|
|
|
|
echo "[.] Will attempt to assume role with MFA provided."
|
|
|
|
out=$(aws --profile $PROFILE_NAME sts assume-role --serial-number $SERIAL_MFA --role-arn $ROLE_ARN --role-session-name $ROLE_NAME --duration-seconds $DURATION --token-code $code 2>&1)
|
|
|
|
fi
|
2019-05-14 18:17:23 +02:00
|
|
|
|
|
|
|
if [ $? -eq 0 ]; then
|
2020-06-19 14:14:57 +02:00
|
|
|
valid=$(printf '%dh:%dm:%ds\n' $(($DURATION/3600)) $(($DURATION%3600/60)) $(($DURATION%60)))
|
|
|
|
echo "[+] Collected session credentials. They will be valid for: $valid. "
|
|
|
|
echo -e "\tPaste below lines to your '~/.aws/credentials' file:"
|
|
|
|
echo
|
2020-06-19 14:31:53 +02:00
|
|
|
echo "[$PROFILE_NAME-$SESSION_NAME]"
|
2020-06-19 14:23:11 +02:00
|
|
|
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"]))'
|
2020-06-19 14:14:57 +02:00
|
|
|
echo
|
2019-05-14 18:17:23 +02:00
|
|
|
else
|
2020-06-19 14:14:57 +02:00
|
|
|
echo "[!] Could not obtain assume-role session credentials:"
|
|
|
|
echo "$out"
|
|
|
|
echo
|
|
|
|
out2=$(env | grep -E 'AWS_[^=]+')
|
|
|
|
if [[ "$out2" != "" ]]; then
|
|
|
|
echo "[!] Your command could fail because of pre-set AWS-related environment variables."
|
|
|
|
echo -e "\tPlease review them, correct any problems and re-launch that script."
|
|
|
|
echo
|
|
|
|
echo "$out2"
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
exit 1
|
2020-06-19 14:31:53 +02:00
|
|
|
fi
|