Last active
November 26, 2017 01:07
-
-
Save mandusm/be5479997f19a73ed40cd1e482c8c1fc to your computer and use it in GitHub Desktop.
Bash Native AWS Federation Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to URLEncode JSON and Other Strings | |
urlencode() { | |
local LANG=C; local c; while IFS= read -r c; do | |
case $c in [a-zA-Z0-9.~_-]) printf "$c"; continue ;; esac | |
printf "$c" | od -An -tx1 | tr ' ' % | tr -d '\n' | |
done <<EOF | |
$(fold -w1) | |
EOF | |
echo | |
} | |
# I Very frequently use AWS Temporary Credentials to Federate into an AWS Account. | |
# This will detect the Exported AWS Environment Variables and generate a Federation Signin URL | |
function aws_federate { | |
`which curl &> /dev/null` | |
if [ $? -ne 0 ]; then | |
echo "Please install curl" | |
exit 1 | |
fi | |
`which jq &> /dev/null` | |
if [ $? -ne 0 ]; then | |
echo "Please install jq" | |
exit 1 | |
fi | |
AWS_SESSION_ENC=`echo "{\"sessionId\": \"${AWS_ACCESS_KEY_ID}\", \"sessionKey\": \"${AWS_SECRET_ACCESS_KEY}\", \"sessionToken\": \"${AWS_SESSION_TOKEN}\"}" | urlencode` | |
SIGNIN_TOKEN=`curl -ksL "https://signin.aws.amazon.com/federation?Action=getSigninToken&SessionDuration=43200&SessionType=json&Session=${AWS_SESSION_ENC}" | jq -r '.SigninToken'` | |
ISSUER=`echo "http://localhost" | urlencode` | |
DESTINATION=`echo "https://us-west-2.console.aws.amazon.com/console/home" | urlencode` | |
URL="https://signin.aws.amazon.com/federation?Action=login&Issuer=${ISSUER}&Destination=${DESTINATION}&SigninToken=${SIGNIN_TOKEN}" | |
echo $URL | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment