aws-web-login 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. profile=$1
  4. option=${2:-ff}
  5. container_name() {
  6. if ! aws configure get "profile.$profile.ff_container"; then
  7. echo "Unknown profile"
  8. exit 1
  9. fi
  10. }
  11. urlencode() {
  12. # urlencode <string>
  13. local length="${#1}"
  14. for (( i = 0; i < length; i++ )); do
  15. local c="${1:i:1}"
  16. case $c in
  17. [a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
  18. *) printf '%%%02X' "'$c" ;;
  19. esac
  20. done
  21. }
  22. container=$(container_name)
  23. credentials=$(get-aws-login --profile "$profile" --duration 43200 --output credential_process | jq -c \
  24. '{
  25. sessionId: .AccessKeyId,
  26. sessionKey: .SecretAccessKey,
  27. sessionToken: .SessionToken
  28. }'
  29. )
  30. response=$(curl -sfS "https://signin.aws.amazon.com/federation?Action=getSigninToken&SessionDuration=28800&Session=$(urlencode "$credentials")")
  31. url="https://signin.aws.amazon.com/federation?Action=login&Issuer=aws-web-login&Destination=$(urlencode "https://console.aws.amazon.com")&SigninToken=$(jq -r .SigninToken <<<"$response")"
  32. if [ "$option" = "ff" ]; then
  33. firefox "ext+container:name=$(urlencode "$container")&url=$(urlencode "$url")"
  34. else
  35. echo "$url"
  36. fi