feat(aws): add creds expiration timer

This commit is contained in:
Nick Lupien 2023-07-16 16:13:28 -04:00
parent 8bdb5c959c
commit 33f1019af5
No known key found for this signature in database
GPG key ID: FF6B5049617E2B3F

View file

@ -195,6 +195,50 @@ function aws_change_access_key() {
aws --no-cli-pager iam list-access-keys
}
calculate_time_difference() {
# Check if both arguments are provided
if [[ $# -lt 2 ]]; then
echo "Error: Two arguments are required."
return 1
fi
# Assign the arguments to variables
local time1=$1
local time2=$2
# Convert times to Unix timestamps
local timestamp1=$(date -u -j -f "%Y-%m-%dT%H:%M:%SZ" "$time1" +"%s")
local timestamp2=$(date -u -j -f "%Y-%m-%dT%H:%M:%SZ" "$time2" +"%s")
# Calculate the difference in seconds
diff_seconds=$((timestamp2 - timestamp1))
# Convert the difference to HH:MM:SS format
local hours=$((diff_seconds / 3600))
local minutes=$(( (diff_seconds / 60) % 60 ))
local seconds=$((diff_seconds % 60))
# Place diff in time_diff
time_diff="${hours}:${minutes}:${seconds}"
}
function tte() {
# calculates and displays time until current AWS creds expire
cachedir=$HOME/.aws/sso/cache
current_cache_file="$cachedir/$(ls -tU $cachedir | head -n1)"
expiration_time=$(jq -r .expiresAt $current_cache_file)
current_time=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
calculate_time_difference $current_time $expiration_time
if [[ $diff_seconds -le 0 ]];then
echo "Creds have expired. Run `aws sso login` to renew."
return 1
else
echo "Time remaining: $time_diff"
fi
}
function aws_regions() {
if [[ $AWS_DEFAULT_PROFILE || $AWS_PROFILE ]];then
aws ec2 describe-regions |grep RegionName | awk -F ':' '{gsub(/"/, "", $2);gsub(/,/, "", $2);gsub(/ /, "", $2); print $2}'