forked from 0x416e746f6e/.aws
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.sh
347 lines (291 loc) Β· 9.59 KB
/
lib.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
function get_1fa_token() {
case "${AWS_HELPER_VAULT_ENGINE}" in
osxkeychain)
get_1fa_token_osxkeychain
;;
file)
get_1fa_token_file
;;
*)
echo "π« Invalid vault engine: ${AWS_HELPER_VAULT_ENGINE}" >&2
exit 1
;;
esac
}
function get_1fa_token_osxkeychain() {
security find-generic-password \
-l "${AWS_HELPER_USER_ARN}" \
-a "${AWS_HELPER_USER_ARN}" \
-s "${AWS_HELPER_USER_ARN}" \
-w
}
function get_1fa_token_file() {
cat "${HOME}/.aws/1fa_token" | jq -r ".\"${AWS_HELPER_USER_ARN}\""
}
function get_2fa_token() {
case "${AWS_HELPER_VAULT_ENGINE}" in
osxkeychain)
get_2fa_token_osxkeychain
;;
file)
get_2fa_token_file
;;
*)
echo "π« Invalid vault engine: ${AWS_HELPER_VAULT_ENGINE}" >&2
exit 1
;;
esac
}
function get_2fa_token_osxkeychain() {
security find-generic-password \
-l "${AWS_HELPER_MFA_DEVICE_ARN}" \
-a "${AWS_HELPER_MFA_DEVICE_ARN}" \
-s "${AWS_HELPER_MFA_DEVICE_ARN}" \
-w
}
function get_2fa_token_file() {
cat "${HOME}/.aws/2fa_token" | jq -r ".\"${AWS_HELPER_MFA_DEVICE_ARN}\""
}
function save_1fa_token() {
if [[ -n "${DEBUG}" ]]; then
echo "New AWS_HELPER_CREDENTIALS" >> "${HOME}/.aws/debug.log"
fi
case "${AWS_HELPER_VAULT_ENGINE}" in
osxkeychain)
save_1fa_token_osxkeychain "$1" "$2"
;;
file)
save_1fa_token_file "$1" "$2"
;;
*)
echo "π« Invalid vault engine: ${AWS_HELPER_VAULT_ENGINE}" >&2
exit 1
;;
esac
}
function save_1fa_token_osxkeychain() {
local AWS_HELPER_USER_ARN="$1"
local AWS_HELPER_CREDENTIALS="$2"
security delete-generic-password \
-l "${AWS_HELPER_USER_ARN}" \
-a "${AWS_HELPER_USER_ARN}" \
-s "${AWS_HELPER_USER_ARN}" \
> /dev/null 2>&1 || true
security add-generic-password \
-l "${AWS_HELPER_USER_ARN}" \
-a "${AWS_HELPER_USER_ARN}" \
-s "${AWS_HELPER_USER_ARN}" \
-w "${AWS_HELPER_CREDENTIALS}" \
> /dev/null 2>&1 || true
}
function save_1fa_token_file() {
local AWS_HELPER_USER_ARN="$1"
local AWS_HELPER_CREDENTIALS="$2"
echo "{\"${AWS_HELPER_USER_ARN}\": ${AWS_HELPER_CREDENTIALS}}" > "${HOME}/.aws/1fa_token"
}
function save_2fa_token() {
if [[ -n "${DEBUG}" ]]; then
echo "New AWS_HELPER_SESSION_TOKEN: $1" >> "${HOME}/.aws/debug.log"
fi
case "${AWS_HELPER_VAULT_ENGINE}" in
osxkeychain)
save_2fa_token_osxkeychain "$1"
;;
file)
save_2fa_token_file "$1"
;;
*)
echo "π« Invalid vault engine: ${AWS_HELPER_VAULT_ENGINE}" >&2
exit 1
;;
esac
}
function save_2fa_token_osxkeychain() {
local AWS_HELPER_SESSION_TOKEN="$1"
if [[ -z "${DEBUG}" ]]; then
security delete-generic-password \
-l "${AWS_HELPER_MFA_DEVICE_ARN}" \
-a "${AWS_HELPER_MFA_DEVICE_ARN}" \
-s "${AWS_HELPER_MFA_DEVICE_ARN}" \
> /dev/null 2>&1 || true
security add-generic-password \
-l "${AWS_HELPER_MFA_DEVICE_ARN}" \
-a "${AWS_HELPER_MFA_DEVICE_ARN}" \
-s "${AWS_HELPER_MFA_DEVICE_ARN}" \
-w "${AWS_HELPER_SESSION_TOKEN}" \
> /dev/null 2>&1 || true
else
security delete-generic-password \
-l "${AWS_HELPER_MFA_DEVICE_ARN}" \
-a "${AWS_HELPER_MFA_DEVICE_ARN}" \
-s "${AWS_HELPER_MFA_DEVICE_ARN}" \
>> "${HOME}/.aws/debug.log" 2>&1 || true
security add-generic-password \
-l "${AWS_HELPER_MFA_DEVICE_ARN}" \
-a "${AWS_HELPER_MFA_DEVICE_ARN}" \
-s "${AWS_HELPER_MFA_DEVICE_ARN}" \
-w "${AWS_HELPER_SESSION_TOKEN}" \
>> "${HOME}/.aws/debug.log"
fi
}
function save_2fa_token_file() {
local AWS_HELPER_SESSION_TOKEN="$1"
echo "{\"${AWS_HELPER_MFA_DEVICE_ARN}\": ${AWS_HELPER_SESSION_TOKEN}}" > "${HOME}/.aws/2fa_token"
}
function get_2fa_otp_manual() {
local PIN
while true; do
PIN="$( get_prompt_private_string "Enter one-time password for \`${AWS_HELPER_MFA_DEVICE_ARN}\`:" )"
if [[ "${PIN}" =~ ^[0-9]{6}$ ]]; then
break
else
printf "%s " "π« Invalid input. Please enter a 6-digit number." >/dev/tty
printf "\n" >/dev/tty
fi
done
printf "%s" "${PIN}"
}
function request_2fa_token() {
local PIN
echo "π Logging into AWS..." >&2
# Check if Yubikey or 1password are available
if ykman=$( which ykman ); then
if [ -z "$( $ykman list )" ]; then
echo "π Please insert your yubikey and hit <ENTER>..." >&2
read
fi
PIN=$( $ykman oath accounts code --single ${AWS_HELPER_MFA_DEVICE_ARN} )
elif op=$( which op ) && [ -n "${AWS_HELPER_OP_ITEM}" ]; then
PIN=$( $op item get ${AWS_HELPER_OP_ITEM} --otp )
else
PIN=$( get_2fa_otp_manual )
fi
echo "π Requesting session token from AWS API..." >&2
if ! AWS_HELPER_SESSION_TOKEN=$( aws \
--profile 1fa \
sts get-session-token \
--serial-number "${AWS_HELPER_MFA_DEVICE_ARN}" \
--duration-seconds 3600 \
--token-code "${PIN}" \
| jq -r -c ".Credentials + { \"Version\": 1 }"
); then
echo "π« Failed to retrieve AWS session token" >&2
exit 1
fi
echo "β
Done." >&2
printf "${AWS_HELPER_SESSION_TOKEN}"
}
function save_custom_config() {
local AWS_CONFIG_SOURCE="$1"
local AWS_CONFIG_DEST="${HOME}/.aws/config"
if [[ -f "${AWS_CONFIG_SOURCE}" ]]; then
cp "${AWS_CONFIG_SOURCE}" "${AWS_CONFIG_DEST}"
elif [[ "${AWS_CONFIG_SOURCE}" =~ ^http ]]; then
if command -v curl >/dev/null 2>&1; then
curl -sL -o "${AWS_CONFIG_DEST}" "${AWS_CONFIG_SOURCE}"
elif command -v wget >/dev/null 2>&1; then
wget -q -O "${AWS_CONFIG_DEST}" "${AWS_CONFIG_SOURCE}"
else
echo "π« Neither curl nor wget are available. Please install one of these and try again."
exit 1
fi
else
echo "π« Invalid config source: ${AWS_CONFIG_SOURCE}" >&2
exit 1
fi
}
function get_prompt_string() {
printf "%s " "π $1" >/dev/tty
read -r CHOICE </dev/tty
printf "%s" "${CHOICE}"
}
function get_prompt_private_string() {
local prompt="$1"
printf "%s" "π $prompt" >/dev/tty
read -rs CHOICE </dev/tty
printf "\n" >/dev/tty
printf "%s" "${CHOICE}"
}
function get_prompt_bool() {
printf "%s " "π $1 [y/N]" >/dev/tty
read -r CHOICE </dev/tty
case "${CHOICE}" in
y|Y ) return 0;;
* ) return 1;;
esac
}
function set_profile_env() {
local KEY="$1"
local VALUE="$2"
local AWS_HELPER_PROFILE="${HOME}/.awsup"
if grep -q "^export ${KEY}=" "${AWS_HELPER_PROFILE}"; then
if [[ "$OSTYPE" == "darwin"* ]]; then
sed -i '' "s|^export ${KEY}=.*|export ${KEY}=${VALUE}|" "${AWS_HELPER_PROFILE}"
else
sed -i "s|^export ${KEY}=.*|export ${KEY}=${VALUE}|" "${AWS_HELPER_PROFILE}"
fi
else
echo "export ${KEY}=${VALUE}" >> "$AWS_HELPER_PROFILE"
fi
}
function save_setup() {
local SETUP_AWS_HELPER_ACCOUNT_ID="$( get_prompt_string "Enter your AWS account ID:" )"
local SETUP_AWS_HELPER_IAM_USERNAME="$( get_prompt_string "Enter your IAM username:" )"
local SETUP_AWS_HELPER_USER_ARN="arn:aws:iam::${SETUP_AWS_HELPER_ACCOUNT_ID}:user/${SETUP_AWS_HELPER_IAM_USERNAME}"
set_profile_env "AWS_HELPER_USER_ARN" "${SETUP_AWS_HELPER_USER_ARN}"
local SETUP_AWS_HELPER_MFA_DEVICE_NAME="$( get_prompt_string "Enter the name of your AWS MFA device:" )"
local SETUP_AWS_HELPER_MFA_DEVICE_ARN="arn:aws:iam::${SETUP_AWS_HELPER_ACCOUNT_ID}:mfa/${SETUP_AWS_HELPER_MFA_DEVICE_NAME}"
set_profile_env "AWS_HELPER_MFA_DEVICE_ARN" "${SETUP_AWS_HELPER_MFA_DEVICE_ARN}"
if [[ "$OSTYPE" == "darwin"* ]]; then
set_profile_env "AWS_HELPER_VAULT_ENGINE" "osxkeychain"
else
set_profile_env "AWS_HELPER_VAULT_ENGINE" "file"
fi
if get_prompt_bool "Do you use 1password client for 2fa?"; then
local SETUP_AWS_HELPER_OP_ITEM="$( get_prompt_string "Enter the name or ID of your 1password item:" )"
set_profile_env "AWS_HELPER_OP_ITEM" "${SETUP_AWS_HELPER_OP_ITEM}"
fi
if get_prompt_bool "Do you want to add a custom aws config file?"; then
local SETUP_CUSTOM_CONFIG="$( get_prompt_string "Enter the path or URL to the custom config file:" )"
save_custom_config "${SETUP_CUSTOM_CONFIG}"
fi
local SETUP_AWS_ACCESS_KEY_ID="$( get_prompt_private_string "Enter your AWS access key ID:" )"
local SETUP_AWS_SECRET_ACCESS_KEY="$( get_prompt_private_string "Enter your AWS secret access key:" )"
local SETUP_AWS_HELPER_CREDENTIALS="{\"Version\":1,\"AccessKeyId\":\"${SETUP_AWS_ACCESS_KEY_ID}\",\"SecretAccessKey\":\"${SETUP_AWS_SECRET_ACCESS_KEY}\"}"
source_config
save_1fa_token "${SETUP_AWS_HELPER_USER_ARN}" "${SETUP_AWS_HELPER_CREDENTIALS}"
save_2fa_token $( request_2fa_token )
echo "β
Setup complete! To validate, run:"
echo "β¨ aws sts get-caller-identity"
}
function set_lock() {
if [[ -f "${HOME}/.aws/login.lock" ]]; then
if [[ -t 0 ]]; then
>&2 echo "π« Warning: another login attempt might be ongoing in parallel!"
else
exit 1
fi
fi
touch "${HOME}/.aws/login.lock"
}
function delete_lock() {
rm -f "${HOME}/.aws/login.lock"
}
function get_help_message() {
local HELP_MESSAGE=<<EOF
CLI for awsup helper.
Usage:
~/.aws/helper.sh [options]
Options:
--custom-config <url_or_path> Save custom config file from URL or path
--help Show this help message
--login Request and save 2fa token
--setup Run setup to save account and 2fa details
EOF
echo "${HELP_MESSAGE}"
}
function source_config() {
local AWS_HELPER_PROFILE="${HOME}/.awsup"
touch "${AWS_HELPER_PROFILE}" && source "${AWS_HELPER_PROFILE}"
}
source_config