#!/usr/bin/env bash
set -Eeuo pipefail
shopt -s extglob
shopt -s nocasematch
shopt -s extdebug
IFS=$' \t\n'
unset -f unalias
\unalias -a
unset -f command
if ! PATH="$(command -p getconf PATH 2>/dev/null)" && [[ -z "$PATH" ]]; then
PATH="/usr/bin:/bin"
fi
PATH+=":/usr/local/bin"
trap 'declare rc=$?;
>&2 echo "Unexpected error (exit-code $rc) executing $BASH_COMMAND at ${BASH_SOURCE[0]} line $LINENO";
exit $rc' ERR
main () {
declare username=""
username="$(aws sts get-caller-identity \
--query Arn \
--output text \
| cut -f 2 -d /)"
declare old_access_keys=""
old_access_keys="$(aws iam list-access-keys \
--user-name "$username" \
--query 'AccessKeyMetadata[].AccessKeyId' \
--output text)"
aws iam create-access-key \
--query '[AccessKey.AccessKeyId,AccessKey.SecretAccessKey]' \
--output text \
| awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"" }'
declare old_key=""
for old_key in $old_access_keys
do
aws iam delete-access-key --access-key-id "$old_key" || true
done
}
main "$@"