Skip to content

Commit 87dec44

Browse files
committed
commit wip updates
1 parent 9d25ee4 commit 87dec44

File tree

5 files changed

+111
-11
lines changed

5 files changed

+111
-11
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
# AWS
66

77
cli/cache
8+
9+
# Logs
10+
11+
*.log
12+
13+
# Temporary files
14+
15+
.temp

get-1fa-credentials.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
set -e -o pipefail
44

5+
# Log the attempt
6+
TS=$( date +'%Y-%m-%dT%H:%M:%S%z' )
7+
PID=$$
8+
ARG=$( ps -ww -o args= -p $PID )
9+
PARG=$( ps -ww -o args= -p $PPID )
10+
PPPID=$( ps -o ppid= -p $PPID )
11+
PPARG=$( ps -ww -o args= -p $PPPID )
12+
PPPPID=$( ps -o ppid= -p $PPPID )
13+
PPPARG=$( ps -ww -o args= -p $PPPPID )
14+
printf '{ "ts": "%s", "grandgrandparent": { "pid": %s, "command": "%s" }, "grandparent": { "pid": %s, "command": "%s" }, "parent": { "pid": %s, "command": "%s" }, "self": { "pid": %s, "command": "%s" } }\n' \
15+
"${TS}" \
16+
"${PPPPID}" "${PPPARG}" \
17+
"${PPPID}" "${PPARG}" \
18+
"${PPID}" "${PARG}" \
19+
"${PID}" "${ARG}" \
20+
>> ${HOME}/.aws/get-1fa-credentials.log
21+
522
pwd=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
623

724
. $pwd/lib.sh

get-2fa-credentials.sh

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,40 @@
33
set -e -o pipefail
44

55
pwd=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6-
76
. $pwd/lib.sh
87

98
AWS_SESSION_TOKEN=$( get_2fa_token )
109
EXPIRATION=$( printf "${AWS_SESSION_TOKEN}" | jq -r ".Expiration" )
1110
NOW=$( date -u +"%Y-%m-%dT%H:%M:%S%z" )
1211

12+
if [[ -n "${DEBUG}" ]]; then
13+
echo "AWS_SESSION_TOKEN: ${AWS_SESSION_TOKEN}" >> ${HOME}/.aws/debug.log
14+
echo "EXPIRATION: ${EXPIRATION}" >> ${HOME}/.aws/debug.log
15+
echo "NOW: ${NOW}" >> ${HOME}/.aws/debug.log
16+
fi
17+
18+
# Log the attempt
19+
PID=$$
20+
ARG=$( ps -ww -o args= -p $PID )
21+
PARG=$( ps -ww -o args= -p $PPID | tr '"' '\"' )
22+
PPPID=$( ps -o ppid= -p $PPID )
23+
PPARG=$( ps -ww -o args= -p $PPPID | tr '"' '\"' )
24+
PPPPID=$( ps -o ppid= -p $PPPID )
25+
PPPARG=$( ps -ww -o args= -p $PPPPID | tr '"' '\"' )
26+
printf '{ "ts": "%s", "expiration": "%s", "grandgrandparent": { "pid": %s, "command": "%s" }, "grandparent": { "pid": %s, "command": "%s" }, "parent": { "pid": %s, "command": "%s" }, "self": { "pid": %s, "command": "%s" } }\n' \
27+
"${NOW}" \
28+
"${EXPIRATION}" \
29+
"${PPPPID}" "${PPPARG}" \
30+
"${PPPID}" "${PPARG}" \
31+
"${PPID}" "${PARG}" \
32+
"${PID}" "${ARG}" \
33+
>> ${HOME}/.aws/get-2fa-credentials.log
34+
1335
if [ "$NOW" \> "$EXPIRATION" ]; then
36+
if [[ -n "${DEBUG}" ]]; then
37+
echo "Logging in..." >> ${HOME}/.aws/debug.log
38+
fi
39+
1440
$pwd/login.sh
1541
AWS_SESSION_TOKEN=$( get_2fa_token )
1642
fi

lib.sh

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,37 @@ function get_2fa_token() {
2020
function save_2fa_token() {
2121
local AWS_SESSION_TOKEN=$1
2222

23-
security delete-generic-password \
24-
-l "${AWS_MFA_DEVICE}" \
25-
-a "${AWS_MFA_DEVICE}" \
26-
-s "${AWS_MFA_DEVICE}" \
27-
> /dev/null 2>&1 || true
23+
if [[ -n "${DEBUG}" ]]; then
24+
echo "New AWS_SESSION_TOKEN: ${AWS_SESSION_TOKEN}" >> ${HOME}/.aws/debug.log
25+
fi
2826

29-
security add-generic-password \
30-
-l "${AWS_MFA_DEVICE}" \
31-
-a "${AWS_MFA_DEVICE}" \
32-
-s "${AWS_MFA_DEVICE}" \
33-
-w "${AWS_SESSION_TOKEN}"
27+
if [[ -z "${DEBUG}" ]]; then
28+
security delete-generic-password \
29+
-l "${AWS_MFA_DEVICE}" \
30+
-a "${AWS_MFA_DEVICE}" \
31+
-s "${AWS_MFA_DEVICE}" \
32+
> /dev/null 2>&1 || true
33+
34+
security add-generic-password \
35+
-l "${AWS_MFA_DEVICE}" \
36+
-a "${AWS_MFA_DEVICE}" \
37+
-s "${AWS_MFA_DEVICE}" \
38+
-w "${AWS_SESSION_TOKEN}" \
39+
> /dev/null 2>&1 || true
40+
else
41+
security delete-generic-password \
42+
-l "${AWS_MFA_DEVICE}" \
43+
-a "${AWS_MFA_DEVICE}" \
44+
-s "${AWS_MFA_DEVICE}" \
45+
>> ${HOME}/.aws/debug.log 2>&1 || true
46+
47+
security add-generic-password \
48+
-l "${AWS_MFA_DEVICE}" \
49+
-a "${AWS_MFA_DEVICE}" \
50+
-s "${AWS_MFA_DEVICE}" \
51+
-w "${AWS_SESSION_TOKEN}" \
52+
>> ${HOME}/.aws/debug.log
53+
fi
3454
}
3555

3656
function request_2fa_token() {

login.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,37 @@
22

33
set -e -o pipefail
44

5+
# Log the login attempt
6+
TS=$( date +'%Y-%m-%dT%H:%M:%S%z' )
7+
PID=$$
8+
ARG=$( ps -ww -o args= -p $PID )
9+
PARG=$( ps -ww -o args= -p $PPID )
10+
PPPID=$( ps -o ppid= -p $PPID )
11+
PPARG=$( ps -ww -o args= -p $PPPID )
12+
PPPPID=$( ps -o ppid= -p $PPPID )
13+
PPPARG=$( ps -ww -o args= -p $PPPPID )
14+
printf '{ "ts": "%s", "grandgrandparent": { "pid": %s, "command": "%s" }, "grandparent": { "pid": %s, "command": "%s" }, "parent": { "pid": %s, "command": "%s" }, "self": { "pid": %s, "command": "%s" } }\n' \
15+
"${TS}" \
16+
"${PPPPID}" "${PPPARG}" \
17+
"${PPPID}" "${PPARG}" \
18+
"${PPID}" "${PARG}" \
19+
"${PID}" "${ARG}" \
20+
>> ${HOME}/.aws/login.log
21+
22+
if [[ -f ${HOME}/.aws/login.lock ]]; then
23+
if [[ -t 0 ]]; then
24+
>&2 echo "Warning: another login attempt might be ongoing in parallel!"
25+
else
26+
exit 1
27+
fi
28+
fi
29+
30+
touch ${HOME}/.aws/login.lock
31+
532
pwd=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
633

734
. $pwd/lib.sh
835

936
save_2fa_token $( request_2fa_token )
37+
38+
rm -f ${HOME}/.aws/login.lock

0 commit comments

Comments
 (0)