-
Notifications
You must be signed in to change notification settings - Fork 1
/
set_temporary_credentials.sh
executable file
·42 lines (34 loc) · 1.52 KB
/
set_temporary_credentials.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
# USAGE:
# source set_temporary_credentials.sh Role MFAcode [Duration]
# source set_temporary_credentials.sh poweruser 123456 3600
# NOTE:
# Duration in seconds (900 - 43200) (15min to 12 hours), default=3600
ROLE=$1
CODE=$2
DURATION="${3:-3600}"
NAME=`aws sts get-caller-identity | jq -r ".Arn" | cut -d/ -f2`
#echo "$NAME $ROLE $CODE $DURATION"
# Poweruser role requires MFA
case $ROLE in
admin|poweruser|readonly|tacowrite)
aws sts get-session-token \
--serial-number "arn:aws:iam::118211588532:mfa/$NAME" \
--token-code $2 > /tmp/creds.txt
export AWS_ACCESS_KEY_ID="$(cat /tmp/creds.txt | jq -r ".Credentials.AccessKeyId")"
export AWS_SECRET_ACCESS_KEY="$(cat /tmp/creds.txt | jq -r ".Credentials.SecretAccessKey")"
export AWS_SESSION_TOKEN="$(cat /tmp/creds.txt | jq -r ".Credentials.SessionToken")" ;;
esac
aws sts assume-role \
--role-arn "arn:aws:iam::118211588532:role/$1" \
--duration-seconds "$DURATION" \
--role-session-name "$NAME" > /tmp/creds.txt
export AWS_ACCESS_KEY_ID="$(cat /tmp/creds.txt | jq -r ".Credentials.AccessKeyId")"
export AWS_SECRET_ACCESS_KEY="$(cat /tmp/creds.txt | jq -r ".Credentials.SecretAccessKey")"
export AWS_SESSION_TOKEN="$(cat /tmp/creds.txt | jq -r ".Credentials.SessionToken")"
aws sts get-caller-identity
EXPIRATION="$(cat /tmp/creds.txt | jq -r ".Credentials.Expiration")"
echo "Temporary credentials set. Expiration = $EXPIRATION"
echo "To return to previous identity:"
echo "unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN"
rm /tmp/creds.txt