This repository has been archived by the owner on Mar 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile
executable file
·318 lines (269 loc) · 10.5 KB
/
profile
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
NO_COLOR="\033[0m"
OK_COLOR="\033[32;01m"
ERROR_COLOR="\033[31;01m"
PROMPT_COLOR="\033[01m"
export AWS_SESSION_DURATION=3600
export NTP_SERVER=${NTP_SERVER:-pool.ntp.org}
export NTPD=$(which ntpd)
which jq >/dev/null || (echo "Missing required 'jq' dependency"; exit 1)
function prompt() {
if [ "${BASH_VERSINFO}" -lt 4 ]; then
echo "Bash Version >= 4 required (${BASH_VERSINFO} installed) for this feature"
exit 1
fi
local env=$1
local prompt=$2
local default_value=$3
local value
if [ -n "$prompt" ]; then
echo ">>> $prompt"
fi
# Use default value if empty
if [ -n "${!env}" ]; then
value=${!env};
else
value=${default_value}
fi
while true; do
echo -ne "${OK_COLOR}$env${NO_COLOR}"
read -e -i "$value" -p ": " $env
if [ -n "${!env}" ]; then
export $env
break
else
echo "<<< Value cannot be empty"
fi
done
}
## Display an exit greeting
function _exit() {
echo 'Goodbye'
exit 0
}
trap _exit EXIT
## Initialize the shell
function init() {
local aws_default_profile=$1
if [ -n "$aws_default_profile" ]; then
assume-role "$aws_default_profile"
else
shell
fi
}
# Sync Docker VM's hardware clock which can drift when host machine sleeps
# e.g. An error occurred (SignatureDoesNotMatch) when calling the AssumeRole operation:
# Signature expired: 20170103T233357Z is now earlier than 20170104T042623Z (20170104T044123Z - 15 min.)
function sync_clock() {
if [ -f "/.dockerenv" ]; then
if [ -n "${NTP_SERVER}" ] && [ -n "${NTPD}" ]; then
echo "Synchronizing clock..."
${NTPD} -d -q -n -p "${NTP_SERVER}" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "WARNING: unable to sync time; requests might fail due to time drift in docker"
fi
fi
fi
}
## Calculate the current shell prompt
function console-prompt() {
NOW=$(date +%s)
OS=$(uname)
if [ -n "${AWS_SESSION_EXPIRATION}" ]; then
if [ "${OS}" == "Darwin" ]; then
export AWS_SESSION_EXPIRATION_TIMESTAMP=$(TZ=GMT date -j -f "%Y-%m-%dT%H:%M:%SZ" "${AWS_SESSION_EXPIRATION}" +%s)
else
if [[ "`date --help 2>&1|head -1`" =~ BusyBox ]]; then
export AWS_SESSION_EXPIRATION_TIMESTAMP=$(TZ=GMT date -D "%Y-%m-%dT%H:%M:%SZ" --date="${AWS_SESSION_EXPIRATION}" +%s)
else
export AWS_SESSION_EXPIRATION_TIMESTAMP=$(TZ=GMT date --date="${AWS_SESSION_EXPIRATION}" +%s)
fi
fi
else
export AWS_SESSION_EXPIRATION_TIMESTAMP=0
fi
if [ $AWS_SESSION_EXPIRATION_TIMESTAMP -ge 0 ]; then
export AWS_SESSION_TTL=$(($AWS_SESSION_EXPIRATION_TIMESTAMP - ${NOW}))
if [ $AWS_SESSION_TTL -le 0 ]; then
AWS_SESSION_TTL_FMT="\[\033[5mexpired\033[0m\]"
else
AWS_SESSION_TTL_FMT="$(($AWS_SESSION_TTL/60))m"
fi
export AWS_SESSION_TTL_FMT
fi
if [ -z "${AWS_PROFILE}" ]; then
export ROLE_PROMPT="(\[${ERROR_COLOR}\]no profile selected\[${NO_COLOR}\])"
elif [ -n "${AWS_IAM_ROLE_ARN}" ]; then
if [[ ${AWS_ASSUME_ROLE_POLICY} =~ ops ]]; then
ROLE_COLOR="$ERROR_COLOR"
else
ROLE_COLOR="$OK_COLOR"
fi
export ROLE_PROMPT="(assume-role \[${ROLE_COLOR}\]${AWS_DEFAULT_PROFILE}\[${NO_COLOR}\]:${AWS_SESSION_TTL_FMT})"
else
export ROLE_PROMPT="(profile \[${OK_COLOR}\]${AWS_DEFAULT_PROFILE}\[${NO_COLOR}\])"
fi
export PS1="$ROLE_PROMPT \W> "
}
## Start the shell
function shell() {
help
export PROMPT_COMMAND=console-prompt
}
## Present the user a menu of available commands
function help() {
echo 'Available commands:'
printf ' %-15s %s\n' 'leave-role' "Leave the current role; run this to release your session"
printf ' %-15s %s\n' 'assume-role' "Assume a new role; run this to renew your session"
printf ' %-15s %s\n' 'setup-role' "Setup a new role; run this to configure your AWS profile"
printf ' %-15s %s\n' 'use-profile' "Use a preconfigured profile; run this to use an AWS profile without assumed roles"
echo
}
function update_profile() {
if [ -n "$AWS_PROFILE" ]; then
aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" --profile $AWS_PROFILE
aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" --profile $AWS_PROFILE
aws configure set aws_session_token "$AWS_SECURITY_TOKEN" --profile $AWS_PROFILE
aws configure set region "$AWS_REGION" --profile $AWS_PROFILE
aws configure set source_profile "$AWS_PROFILE" --profile $AWS_PROFILE
fi
}
function setup-role() {
prompt AWS_PROFILE "What should we call this profile [no spaces]? (e.g. ops) " "ops"
prompt AWS_MAIN_ACCOUNT_ID "What is your main AWS Account ID? (e.g. 324149397721)"
prompt AWS_ORGANIZATION_ACCOUNT_ID "What is your AWS Organization Account ID? (e.g. 324149397721)"
prompt AWS_IAM_USERNAME "What is your AWS IAM Username? (e.g. erik)" `whoami`
prompt AWS_IAM_ROLE "What is the IAM Role you wish to assume? (e.g. ops)" "ops"
prompt AWS_ACCESS_KEY_ID "What is your AWS Access Key ID? (e.g. ZSIKIY1ZX44WRKCLS3GB)"
prompt AWS_SECRET_ACCESS_KEY "What is your AWS Secret Access Key? (e.g. FW8qWWafMaUi+siNcRiawxr4GadKf6We1fl90G5x)"
prompt AWS_REGION "What default region do you want? (e.g. us-east-1)" "us-east-1"
AWS_IAM_ROLE_ARN="arn:aws:iam::${AWS_ORGANIZATION_ACCOUNT_ID}:role/${AWS_IAM_ROLE}"
AWS_IAM_MFA_SERIAL="arn:aws:iam::${AWS_MAIN_ACCOUNT_ID}:mfa/${AWS_IAM_USERNAME}"
# When creating a new/non-existent profile, the `aws configure` command gets confused if `AWS_PROFILE` or `AWS_DEFAULT_PROFILE`
# are set to something which does not yet exist. Running it in `env` lets us sanify the environment.
env -u AWS_PROFILE -u AWS_DEFAULT_PROFILE aws configure set "profile.${AWS_PROFILE}.region" "$AWS_REGION"
env -u AWS_PROFILE -u AWS_DEFAULT_PROFILE aws configure set "profile.${AWS_PROFILE}.role_arn" "$AWS_IAM_ROLE_ARN"
env -u AWS_PROFILE -u AWS_DEFAULT_PROFILE aws configure set "profile.${AWS_PROFILE}.mfa_serial" "$AWS_IAM_MFA_SERIAL"
env -u AWS_PROFILE -u AWS_DEFAULT_PROFILE aws configure set "profile.${AWS_PROFILE}.source_profile" "$AWS_PROFILE"
env -u AWS_PROFILE -u AWS_DEFAULT_PROFILE aws configure set aws_access_key_id "$AWS_ACCESS_KEY_ID" --profile $AWS_PROFILE
env -u AWS_PROFILE -u AWS_DEFAULT_PROFILE aws configure set aws_secret_access_key "$AWS_SECRET_ACCESS_KEY" --profile $AWS_PROFILE
echo "Profile $AWS_PROFILE created"
# Cleanup
unset AWS_PROFILE
unset AWS_DEFAULT_PROFILE
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_IAM_MFA_SERIAL
unset AWS_IAM_ROLE_ARN
unset AWS_REGION
}
## Leave the currently assumed role
function leave-role() {
if [ -n "$AWS_DEFAULT_PROFILE" ]; then
find $HOME/.aws/cli/cache -name "${AWS_DEFAULT_PROFILE}*.json" -delete
find $HOME/.aws/cli/cache -name "${AWS_DEFAULT_PROFILE}" -delete
fi
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
unset AWS_SECURITY_TOKEN
unset AWS_IAM_MFA_SERIAL
unset AWS_IAM_ROLE_ARN
unset AWS_REGION
unset AWS_DEFAULT_PROFILE
if [ -n "${AWS_PROFILE}" ]; then
# wipe out temporary session
update_profile
unset AWS_PROFILE
else
echo "No role currently assumed"
fi
}
function use-profile() {
if [ -n "$1" ]; then
export AWS_DEFAULT_PROFILE=$1
fi
if [ -z "$AWS_DEFAULT_PROFILE" ]; then
echo "AWS_DEFAULT_PROFILE not set"
return 1
fi
if [ ! -f "${AWS_CONFIG_FILE}" ]; then
echo "AWS Configuration does not exist. Run \`setup-role\` or \`aws configure\`."
exit 1
fi
grep -q "\[profile ${AWS_DEFAULT_PROFILE}\]" "${AWS_CONFIG_FILE}"
if [ $? -ne 0 ]; then
echo "Profile for '${AWS_DEFAULT_PROFILE}' does not exist"
return 1
fi
# Unset vars from previously assumed role
unset AWS_SESSION_TOKEN
unset AWS_SECURITY_TOKEN
unset AWS_IAM_MFA_SERIAL
unset AWS_IAM_ROLE_ARN
sync_clock
echo "Preparing to assume role associated with $AWS_DEFAULT_PROFILE"
export AWS_REGION=$(aws configure get region --profile $AWS_DEFAULT_PROFILE 2>/dev/null)
if [ -z "$AWS_REGION" ]; then
echo "region not set for $AWS_DEFAULT_PROFILE profile"
return 1
fi
export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile $AWS_DEFAULT_PROFILE 2>/dev/null)
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
echo "aws_access_key_id not set for $AWS_DEFAULT_PROFILE profile"
return 1
fi
export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile $AWS_DEFAULT_PROFILE 2>/dev/null)
if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
echo "aws_secret_access_key not set for $AWS_DEFAULT_PROFILE profile"
return 1
fi
export AWS_PROFILE=${AWS_DEFAULT_PROFILE};
sync_clock
}
function assume-role() {
use-profile "$1"
echo "Preparing to assume role associated with $AWS_DEFAULT_PROFILE"
# Reset the environment, or the awscli call will fail
unset AWS_PROFILE
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
export AWS_IAM_ROLE_ARN=$(aws configure get role_arn --profile $AWS_DEFAULT_PROFILE 2>/dev/null)
export AWS_IAM_MFA_SERIAL=$(aws configure get mfa_serial --profile $AWS_DEFAULT_PROFILE 2>/dev/null)
if [ -z "$AWS_IAM_ROLE_ARN" ]; then
echo "role_arn not set $AWS_DEFAULT_PROFILE profile"
return 1
fi
if [ -z "$AWS_IAM_MFA_SERIAL" ]; then
echo "mfa_serial not set $AWS_DEFAULT_PROFILE profile"
return 1
fi
echo "region=$AWS_REGION"
echo "role_arn=$AWS_IAM_ROLE_ARN"
echo "mfa_serial=$AWS_IAM_MFA_SERIAL"
until aws ec2 describe-regions > /dev/null; do
echo "Retrying..."
sleep 1
done
TMP_FILE=$(find $HOME/.aws/cli/cache -name "${AWS_DEFAULT_PROFILE}*.json" | head -1)
if [ -f "$TMP_FILE" ]; then
export AWS_PROFILE="$AWS_DEFAULT_PROFILE-session"
export AWS_ACCESS_KEY_ID=$(cat ${TMP_FILE} | jq -r ".Credentials.AccessKeyId")
export AWS_SECRET_ACCESS_KEY=$(cat ${TMP_FILE} | jq -r ".Credentials.SecretAccessKey")
export AWS_SESSION_TOKEN=$(cat ${TMP_FILE} | jq -r ".Credentials.SessionToken")
export AWS_SESSION_EXPIRATION=$(cat ${TMP_FILE} | jq -r ".Credentials.Expiration")
export AWS_SECURITY_TOKEN=${AWS_SESSION_TOKEN}
update_profile
write_credentials
echo "You have assumed the role associated with $AWS_DEFAULT_PROFILE. It expires $AWS_SESSION_EXPIRATION."
else
echo "Failed to obtain temporary session for $AWS_DEFAULT_PROFILE"
fi
}
function write_credentials() {
# Write credentials in a format compatible with http://169.254.169.254/latest/meta-data/iam/security-credentials/$role
# This can be used with `s3fs` to use an assumed role outside of AWS (with patch)
jq '.Credentials' < ${AWS_DATA_PATH}/cli/cache/${AWS_DEFAULT_PROFILE}--*.json \
> ${AWS_DATA_PATH}/cli/cache/${AWS_DEFAULT_PROFILE}
}
init $*