Pārlūkot izejas kodu

bin: add aws-web-login

Thomas Dy 4 gadi atpakaļ
vecāks
revīzija
78a3eff31d
1 mainītis faili ar 44 papildinājumiem un 0 dzēšanām
  1. 44 0
      .local/bin/aws-web-login

+ 44 - 0
.local/bin/aws-web-login

@@ -0,0 +1,44 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+profile=$1
+option=${2:-ff}
+
+container_name() {
+  if ! aws configure get "profile.$profile.ff_container"; then
+    echo "Unknown profile"
+    exit 1
+  fi
+}
+
+urlencode() {
+  # urlencode <string>
+  local length="${#1}"
+  for (( i = 0; i < length; i++ )); do
+    local c="${1:i:1}"
+    case $c in
+      [a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
+      *) printf '%%%02X' "'$c" ;;
+    esac
+  done
+}
+
+container=$(container_name)
+credentials=$(get-aws-login --profile "$profile" --duration 43200 --output credential_process | jq -c \
+  '{
+    sessionId: .AccessKeyId,
+    sessionKey: .SecretAccessKey,
+    sessionToken: .SessionToken
+  }'
+)
+
+response=$(curl -sfS "https://signin.aws.amazon.com/federation?Action=getSigninToken&SessionDuration=28800&Session=$(urlencode "$credentials")")
+
+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")"
+
+if [ "$option" = "ff" ]; then
+  firefox "ext+container:name=$(urlencode "$container")&url=$(urlencode "$url")"
+else
+  echo "$url"
+fi