aws-web-login 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. get-credentials() {
  23. if command -v aws-keyring > /dev/null; then
  24. aws-keyring --profile "$profile" --duration=12h
  25. else
  26. get-aws-login --profile "$profile" --duration 43200 --output credential_process
  27. fi
  28. }
  29. container=$(container_name)
  30. credentials=$(get-credentials | jq -c \
  31. '{
  32. sessionId: .AccessKeyId,
  33. sessionKey: .SecretAccessKey,
  34. sessionToken: .SessionToken
  35. }'
  36. )
  37. response=$(curl -sfS "https://signin.aws.amazon.com/federation?Action=getSigninToken&SessionDuration=28800&Session=$(urlencode "$credentials")")
  38. 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")"
  39. if [ "$option" = "ff" ]; then
  40. firefox "ext+container:name=$(urlencode "$container")&url=$(urlencode "$url")"
  41. else
  42. echo "$url"
  43. fi