-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch-role
More file actions
executable file
·44 lines (34 loc) · 1.31 KB
/
switch-role
File metadata and controls
executable file
·44 lines (34 loc) · 1.31 KB
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
43
44
#!/usr/bin/env bash
set -e
script_dir=$(cd "$(dirname "$0")" ; pwd -P)
base_profile="${1}"
role_name="${2}"
target_profile=data-derp
usage() { echo "Usage: $0 [-b <base profile: string>] [-t <target role: string>]" 1>&2; exit 1; }
while getopts ":b:t:" o; do
case "${o}" in
b)
base=${OPTARG}
;;
t)
target=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${base}" ] || [ -z "${target}" ] ; then
usage
fi
identity=$(AWS_PROFILE=${base} aws sts get-caller-identity)
account=$(echo $identity | jq -r '.Account')
arn=$(echo $identity | jq -r '.Arn')
response=$(AWS_PROFILE=${base} aws sts assume-role \
--role-arn "arn:aws:iam::${account}:role/${target}" \
--role-session-name "bootstrap-$(echo $arn | cut -d '/' -f 3)")
aws configure set aws_access_key_id $(echo $response | jq -r '.Credentials.AccessKeyId') --profile "${target_profile}"
aws configure set aws_secret_access_key $(echo $response | jq -r '.Credentials.SecretAccessKey') --profile "${target_profile}"
aws configure set aws_session_token $(echo $response | jq -r '.Credentials.SessionToken') --profile "${target_profile}"
echo "Successfully switched role to arn:aws:iam::${account}:role/${target}. Credentials saved in profile: ${target_profile}."