From a593994d08e341b5379bcbdcc565e46bb00b5604 Mon Sep 17 00:00:00 2001 From: Hari Sekhon Date: Thu, 16 Dec 2021 16:19:44 +0000 Subject: [PATCH] updated aws_csv_creds.sh --- aws_csv_creds.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/aws_csv_creds.sh b/aws_csv_creds.sh index 632386b0..7765f82e 100755 --- a/aws_csv_creds.sh +++ b/aws_csv_creds.sh @@ -22,7 +22,7 @@ srcdir="$(dirname "${BASH_SOURCE[0]}")" # shellcheck disable=SC2034,SC2154 usage_description=" -Prints AWS credentials from a standard AWS CSV export file as shell export statements +Prints AWS credentials from a standard AWS CSV export file or access key export CSV as shell export statements Useful to quickly switch your shell to some exported credentials from a service account for testing permissions or pipe to upload to a CI/CD system via an API eg. circleci_project_set_env_vars.sh @@ -49,8 +49,21 @@ if ! [ -f "$csv" ]; then die "File not found: $csv" fi +if ! grep -Fq 'AKIA'; then + die "Access Key not found in file '$csv'" +fi + # access keys are prefixed with AKIA, skips header row -awk -F, '/AKIA/{ - print "export AWS_ACCESS_KEY_ID="$3 - print "export AWS_SECRET_ACCESS_KEY="$4 -}' "$csv" +if grep -Fxq 'Access key ID,Secret access key' "$csv"; then + awk -F, '/AKIA/{ + print "export AWS_ACCESS_KEY_ID="$1 + print "export AWS_SECRET_ACCESS_KEY="$2 + }' "$csv" +elif grep -Fxq 'User name,Password,Access key ID,Secret access key,Console login link' "$csv"; then + awk -F, '/AKIA/{ + print "export AWS_ACCESS_KEY_ID="$3 + print "export AWS_SECRET_ACCESS_KEY="$4 + }' "$csv" +else + die "unrecognized CSV header line, may have changed so code may need an update" +fi