-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathawsConfigExtracter.awk
More file actions
45 lines (40 loc) · 1.52 KB
/
awsConfigExtracter.awk
File metadata and controls
45 lines (40 loc) · 1.52 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
# script I use for simplifying AWS SSO login stuff...I want to just remember
# the [sso-session x] and use that in scripts/functions (like my aws_sso_login
# function in ~/development/configurations/bash/functions.bash). This script
# takes a nice name like "litstream_staging_admin_sso" and looks through
# ~/.aws/config for a profile that has that as it's sso_session. We could
# conceivably extend this to do other things, hence the generic name and the
# fact you pass in a target_config_field (even though only one works at the moment!)
#
# Usage is like:
#
# cat ~/.aws/config | awk -v target_config_field="profile_name" -v target_aws_session="litstream_staging_admin_sso" -f ~/development/shellScripts/awsConfigExtracter.awk
#
# this will return 0 and print out the value if it works, or return 1 and print out an
# error if it didn't.
BEGIN {
# we are just using awk to process whole lines; we don't want to split
# so we set FS to some garbage string we won't actually ever encounter
FS = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
# IGNORECASE=1
currentProfile = ""
finalOutput = ""
}
match($1, /^\[profile (.*)\]/, x) {
currentProfile = x[1]
}
match($1, /^sso_session.*= *(.*)/, x) {
if (target_config_field == "profile_name" && x[1] == target_aws_session) {
# print "found sso_session: " x[1] " (" currentProfile ")"
finalOutput = currentProfile
exit 0
}
}
END {
if (finalOutput == "") {
print "could not find " target_config_field " for session " target_aws_session
exit 1
} else {
print finalOutput
}
}