From 76b3a35926b6f36de52c99a6d38b9c752d0ef392 Mon Sep 17 00:00:00 2001 From: Richard G <guo_yiwei@yahoo.com> Date: Tue, 15 Feb 2022 13:05:03 -0800 Subject: [PATCH 1/2] added fallback date method --- gen-aws-creds.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gen-aws-creds.sh b/gen-aws-creds.sh index 95d4d87..2f356f6 100755 --- a/gen-aws-creds.sh +++ b/gen-aws-creds.sh @@ -1,5 +1,5 @@ #!/bin/bash - +#set -x # Generates time-bound AWS credentials from vault secrets engine # stashes credentials in the vault token cubbyhole to avoid generating # new credentials when previously generated creds have not expired @@ -78,7 +78,8 @@ if [[ $vault_loggedon = 'TRUE' ]]; then # use date or gdate for linux/mac interop # gdate on mac: brew install coreutils now=$(date +%s 2>/dev/null) && expiration_u=$(date -d "$expiration" +%s 2>/dev/null) || { - now=$(gdate +%s) && expiration_u=$(gdate -d "$expiration" +%s) + now=$(gdate +%s) && expiration_u=$(gdate -d "$expiration" +%s) || + now=$(date +%s 2>/dev/null) && expiration_u=$(date -j -f "%m/%d/%Y %H:%M:%S" "$expiration" +%s 2>/dev/null) } # check if stashed creds have not expired @@ -139,9 +140,9 @@ if [[ ( $creds_generated == 'TRUE' ) && ( $creds_get_new == 'TRUE' ) ]]; then # calc lease expiration time # use date or gdate for linux/mac interop - expiration=$(date --date="$request_time+ $lease_duration seconds" '+%m/%d/%Y %T' 2>/dev/null) || { - expiration=$(gdate --date="$request_time+ $lease_duration seconds" '+%m/%d/%Y %T') - } + expiration=$(date --date="$request_time+ $lease_duration seconds" '+%m/%d/%Y %T' 2>/dev/null) || + expiration=$(gdate --date="$request_time+ $lease_duration seconds" '+%m/%d/%Y %T' 2>/dev/null) || + expiration=$(date -j -f "%a %b %d %T %Z %Y" -v+${lease_duration}S "$request_time" '+%m/%d/%Y %T') if [[ ( $access_key ) && ( $secret_key ) ]]; then export AWS_SECRET_ACCESS_KEY=$secret_key -- GitLab From 7f1e7de83a88648897074228dc2557ff2bd5c2a2 Mon Sep 17 00:00:00 2001 From: Richard G <guo_yiwei@yahoo.com> Date: Tue, 15 Feb 2022 14:41:14 -0800 Subject: [PATCH 2/2] Added pipe to null to supress unnecessary error messages --- gen-aws-creds.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gen-aws-creds.sh b/gen-aws-creds.sh index 2f356f6..c15beb2 100755 --- a/gen-aws-creds.sh +++ b/gen-aws-creds.sh @@ -78,7 +78,7 @@ if [[ $vault_loggedon = 'TRUE' ]]; then # use date or gdate for linux/mac interop # gdate on mac: brew install coreutils now=$(date +%s 2>/dev/null) && expiration_u=$(date -d "$expiration" +%s 2>/dev/null) || { - now=$(gdate +%s) && expiration_u=$(gdate -d "$expiration" +%s) || + now=$(gdate +%s 2>/dev/null) && expiration_u=$(gdate -d "$expiration" +%s 2>/dev/null) || now=$(date +%s 2>/dev/null) && expiration_u=$(date -j -f "%m/%d/%Y %H:%M:%S" "$expiration" +%s 2>/dev/null) } @@ -142,7 +142,7 @@ if [[ ( $creds_generated == 'TRUE' ) && ( $creds_get_new == 'TRUE' ) ]]; then # use date or gdate for linux/mac interop expiration=$(date --date="$request_time+ $lease_duration seconds" '+%m/%d/%Y %T' 2>/dev/null) || expiration=$(gdate --date="$request_time+ $lease_duration seconds" '+%m/%d/%Y %T' 2>/dev/null) || - expiration=$(date -j -f "%a %b %d %T %Z %Y" -v+${lease_duration}S "$request_time" '+%m/%d/%Y %T') + expiration=$(date -j -f "%a %b %d %T %Z %Y" -v+${lease_duration}S "$request_time" '+%m/%d/%Y %T' 2>/dev/null) if [[ ( $access_key ) && ( $secret_key ) ]]; then export AWS_SECRET_ACCESS_KEY=$secret_key -- GitLab