Skip to content

Instantly share code, notes, and snippets.

@k4200
Created November 24, 2020 02:22
Show Gist options
  • Save k4200/5f5d1d55de411af4ef8b18fa26b60215 to your computer and use it in GitHub Desktop.
Save k4200/5f5d1d55de411af4ef8b18fa26b60215 to your computer and use it in GitHub Desktop.
Executes aws sts get-session-token, and sets the result to .aws/credentials
#!/bin/bash
# Based on the following shell script:
# https://github.com/DevNambi/utility-scripts/blob/master/sessioner.sh
#
echo $@
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--serial)
SERIAL="$2"
shift # past argument
shift # past value
;;
-p|--profile)
PROFILE="$2"
shift # past argument
shift # past value
;;
-t|--token)
TOKEN="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
echo "Configuring $PROFILE with token $TOKEN"
CREDJSON="$(aws sts get-session-token --duration 129600 --serial-number $SERIAL --profile $PROFILE --token-code $TOKEN)"
ACCESSKEY="$(echo $CREDJSON | jq '.Credentials.AccessKeyId' | sed 's/"//g')"
SECRETKEY="$(echo $CREDJSON | jq '.Credentials.SecretAccessKey' | sed 's/"//g')"
SESSIONTOKEN="$(echo $CREDJSON | jq '.Credentials.SessionToken' | sed 's/"//g')"
PROFILENAME=${PROFILE}-session
echo "Profile $PROFILENAME AccessKey $ACCESSKEY SecretKey $SECRETKEY"
echo "SessionToken $SESSIONTOKEN"
aws configure set aws_access_key_id $ACCESSKEY --profile $PROFILENAME
aws configure set aws_secret_access_key $SECRETKEY --profile $PROFILENAME
aws configure set aws_session_token $SESSIONTOKEN --profile $PROFILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment