Snippet: show-all-aws-sso-sessions.bash

From Tayledras
Jump to: navigation, search
AWS Single Sign On (SSO)

Purpose

History

show-all-sessions.bash was a bash script developed for Teaching Strategies, LLC, to quickly iterate through cached AWS SSO sessions (~/.aws/sso/cache/*.json) to show which ones were still open, when they'd expire, and time remaining before expiry.

Bash Source

EXAMPLE:

 1 ./show-all-sessions.bash
 2 
 3 SESSION FILE /Users/kenforeman/.aws/sso/cache/99f8391cfacbf85eaa9ae55bdc5d40171d7e3799.json:
 4   Currently:    2023-12-14 11:36:09
 5   Expires at:   2022-09-08 13:23:05
 6   Expiry (UTC): 2022-09-08T17:23:05Z
 7   Time remaining: -11087 hours, -13 minutes, -4 seconds
 8 
 9 SESSION FILE /Users/kenforeman/.aws/sso/cache/a6119e9072c284ee185ac024bb6e7cfd9335ab63.json:
10   Currently:    2023-12-14 11:36:09
11   Expires at:   2023-12-14 15:15:42
12   Expiry (UTC): 2023-12-14T20:15:42Z
13   Time remaining: 3 hours, 39 minutes, 33 seconds
14 
15 SESSION FILE /Users/kenforeman/.aws/sso/cache/botocore-client-id-us-east-1.json:
16   Currently:    2023-12-14 11:36:09
17   Expires at:   2023-12-14 10:10:03
18   Expiry (UTC): 2023-12-14T15:10:03Z
19   Time remaining: -1 hours, -26 minutes, -6 seconds
20 
21 SUMMARY:
22   Total sessions found: 3

show-all-sessions.bash

 1 #!/bin/bash
 2 
 3 # NOTE: uses GNU Date (gdate) from coreutils rather than date that ships with MacOS
 4 
 5 # Script iterates through all cached AWS SSO sessions, displays expiry and time remaining
 6 
 7 # Set the path to the AWS SSO cache directory
 8 AWS_SSO_CACHE_DIR="$HOME/.aws/sso/cache"
 9 
10 # Check if the cache directory exists
11 if [ ! -d "$AWS_SSO_CACHE_DIR" ]; then
12     echo "AWS SSO cache directory not found."
13     exit 1
14 fi
15 
16 # Initialize counters
17 session_count=0
18 
19 # Iterate through each file in the cache directory
20 for file in "$AWS_SSO_CACHE_DIR"/*.json; do
21     if [ -f "$file" ]; then
22         # Extract session information from the file
23         expires_at=$(jq -r '.expiresAt' "$file" 2>/dev/null)
24         epoch_time=$(gdate -u -d "$expires_at" +"%s")
25 
26         if [ $? -eq 0 ]; then
27             # Calculate time remaining for the session
28             current_time=$(gdate -u +"%s")
29             time_remaining=$((epoch_time - current_time))
30 
31             # Display session information
32             echo "SESSION FILE $file:"
33             echo "  Currently:    $(gdate -d @"$((current_time))" +"%Y-%m-%d %H:%M:%S")"
34             echo "  Expires at:   $(gdate -d @"$((epoch_time))" +"%Y-%m-%d %H:%M:%S")"
35             echo "  Expiry (UTC): "$expires_at
36 
37             # Calculate hours, minutes, and seconds remaining
38             hours=$((time_remaining / 3600))
39             minutes=$(( (time_remaining % 3600) / 60 ))
40             seconds=$((time_remaining % 60))
41 
42             echo "  Time remaining: $hours hours, $minutes minutes, $seconds seconds"
43             echo
44             session_count=$((session_count + 1))
45         else
46             echo "Error parsing session information from file: $file"
47         fi
48     fi
49 done
50 
51 # Display summary
52 echo "SUMMARY:"
53 echo "  Total sessions found: $session_count"