-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaws-assume
More file actions
executable file
·165 lines (147 loc) · 4.78 KB
/
aws-assume
File metadata and controls
executable file
·165 lines (147 loc) · 4.78 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
# Uses AWS STS to assume role, setting environment variables
# To run: . aws-assume
pkg=${0##*/}
## Functions
log() {
echo "$(date +"[%F %X,000]") $(hostname) $1"
}
die() {
log "[FAIL] $1" >&2 && return 1
}
role_unset() {
# Unsets environment variables #
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
unset AWS_SESSION_EXPIRY
unset AWS_ROLE_NAME
}
role_status() {
# Checks the role status and displays the expiration time remaining #
TIMELEFT=0
if [[ ! -z "${AWS_SESSION_EXPIRY}" ]]; then
EXPEPOCH=$(date --date "${AWS_SESSION_EXPIRY}" +%s)
NOWEPOCH=$(date +%s)
TIMELEFT=$((${EXPEPOCH} - ${NOWEPOCH}))
if [[ "$TIMELEFT" -gt 0 ]]; then
log "Assumed IAM Role: ${AWS_ROLE_NAME} (expires in "${TIMELEFT}"s)"
else
role_unset
log "Role of ${ROLE_ARN} expired."
fi
fi
}
usage() {
# Show usage #
echo "
NAME
${pkg} - AWS STS role assumption
DESCRIPTION
${pkg} will, based on parameters passed, assume a role and provide AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
and an AWS_SESSION_TOKEN which are needed for SDK, CLI, and other such access requirements for tools. You
can also pass your MFA token if enabled on your profile.
SYNOPSIS
${pkg} [--profile=<name>] [--account=<id>] [--role=<name>] [--user=<username>] [--mfa=<token>]
OPTIONS
--profile=<name>
Name of AWS profile from your ~/.aws/config file to use.
--account=<id>
AWS Account ID in which operation will be performed.
--role=<name>
Name of AWS role to assume.
--user=<username>
Name of AWS IAM User which has the MFA device.
--mfa=<token>
MFA token.
"
}
assume() {
# Assume the specified role and get the STS credentials to be set within the environment #
if [ -z "${AWS_PROFILE_NAME}" ] || [ -z "${AWS_ROLE_NAME}" ]; then
die "Missing parameters. Set both '--profile=<name>' and '--role=<name>', or environment variables 'AWS_PROFILE_NAME' and 'AWS_ROLE_NAME'." ; return 1
fi
ROLE_ARN=$(aws --profile=${AWS_PROFILE_NAME} --output=json iam get-role --role-name ${AWS_ROLE_NAME} 2>/dev/null | jq -r '.Role | .Arn')
if [ $? -ne 0 ] || [ -z "${ROLE_ARN}" ]; then
die "Error getting role ARN." ; return 1
fi
if [ -z "${AWS_ACCOUNT_ID}" ] || [ -z "${USER}" ]; then
die "Missing parameters. Set both '--account=<id>' and '--user=<username>', or environment variables 'AWS_ACCOUNT_ID' and 'USER'." ; return 1
fi
MFA_SERIAL_NUMBER="arn:aws:iam::${AWS_ACCOUNT_ID}:mfa/${USER}"
## AWS_SESSION=$(aws --profile=${AWS_PROFILE_NAME} sts assume-role --output=json --role-arn "${ROLE_ARN}" --role-session-name ${AWS_PROFILE_NAME}-${AWS_ROLE_NAME} --serial-number "${MFA_SERIAL_NUMBER}" --token-code "${MFA_TOKEN}")
AWS_SESSION=$(aws --profile=${AWS_PROFILE_NAME} sts assume-role --output=json --role-arn "${ROLE_ARN}" --role-session-name ${AWS_PROFILE_NAME}-${AWS_ROLE_NAME} 2>/dev/null)
if [ $? -eq 0 ] && [ ! -z "${AWS_SESSION}" ]; then
export AWS_SECRET_ACCESS_KEY=$(echo "${AWS_SESSION}" | jq -r '.Credentials|.SecretAccessKey')
export AWS_ACCESS_KEY_ID=$(echo "${AWS_SESSION}" | jq -r '.Credentials|.AccessKeyId')
export AWS_SESSION_TOKEN=$(echo "${AWS_SESSION}" | jq -r '.Credentials|.SessionToken')
export AWS_SESSION_EXPIRY=$(echo "${AWS_SESSION}" | jq -r '.Credentials|.Expiration')
role_status
else
die "Error assuming role to get credentials." ; return 1
fi
}
main () {
# Main function to handle processing #
# Process command line
for arg in "${@}"; do
case "${arg}" in
--profile)
if [ -n "${2}" ]; then
AWS_PROFILE_NAME=${2}
shift
else
die "Option ${arg} requires a value." ; return 1
fi
;;
--profile=*)
AWS_PROFILE_NAME=${arg#*=} # Delete everything up to "=" and assign the remainder.
;;
--account)
if [ -n "${2}" ]; then
AWS_ACCOUNT_ID=${2}
shift
else
die "Option ${arg} requires a value." ; return 1
fi
;;
--account=*)
AWS_ACCOUNT_ID=${arg#*=} # Delete everything up to "=" and assign the remainder.
;;
--role)
if [ -n "${2}" ]; then
AWS_ROLE_NAME=${2}
shift
else
die "Option ${arg} requires a value." ; return 1
fi
;;
--role=*)
AWS_ROLE_NAME=${arg#*=} # Delete everything up to "=" and assign the remainder.
;;
--mfa)
if [ -n "${2}" ]; then
MFA_TOKEN=${2}
shift
else
die "Option ${arg} requires a value." ; return 1
fi
;;
--mfa=*)
MFA_TOKEN=${arg#*=}
;;
-h | --help)
usage
return 0
;;
*)
log "Invalid option '${arg}'"
usage
return 1
;;
esac
done
assume
}
## Main
main $@