Last active
October 10, 2024 16:01
Revisions
-
spencerdcarlson revised this gist
Oct 10, 2024 . 1 changed file with 4 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,14 +27,8 @@ function get_account () { local account=-1 account=$(aws sts get-caller-identity --query "Account" --profile "${profile}" --output text 2>/dev/null) [ $? -eq 0 ] && [ "${account}" -ne -1 ] && echo "${account}" return 0 } @@ -76,6 +70,8 @@ if [ "${CURRENT_ACCOUNT:--1}" -ne "${ACCOUNT_ID}" ]; then AUTH_CODE=$(login) if [ $? -eq 0 ] && [ -n "${AUTH_CODE}" ]; then log "Authorization Code: ${AUTH_CODE}" if command -v say >/dev/null 2>&1; then echo "${AUTH_CODE}" | awk '{ for(i=1; i<=length($0); i++) print substr($0, i, 1) }' | say fi fi fi -
spencerdcarlson revised this gist
Oct 10, 2024 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -24,14 +24,14 @@ function with_error () { function get_account () { local profile=${1:-$PROFILE} local account=-1 account=$(aws sts get-caller-identity --query "Account" --profile "${profile}" --output text 2>/dev/null) if [ $? -ne 0 ]; then log "Failed to get account ID with profile ${profile}" true return 1 fi if [ "${account}" -ne -1 ]; then echo "${account}" fi @@ -49,7 +49,8 @@ function login () { while [ ! -s "${TEMP_FILE}" ]; do sleep 0.1 done local code="" code=$(cat "${TEMP_FILE}" | tail -1) if [[ "${code}" =~ ^[A-Z]{4}-[A-Z]{4}$ ]]; then echo "${code}" return 0 -
spencerdcarlson revised this gist
Oct 9, 2024 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -51,7 +51,6 @@ function login () { done local code=$(cat "${TEMP_FILE}" | tail -1) if [[ "${code}" =~ ^[A-Z]{4}-[A-Z]{4}$ ]]; then echo "${code}" return 0 else -
spencerdcarlson revised this gist
Oct 9, 2024 . 1 changed file with 33 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ #!/usr/bin/env bash set -euo pipefail PROFILE=${1} ACCOUNT_ID=${2} @@ -14,50 +16,66 @@ function log () { fi } function with_error () { local message="${1}" log "${message}" true exit 1 } function get_account () { local profile=${1:-$PROFILE} local account=$(aws sts get-caller-identity --query "Account" --profile "${profile}" --output text 2>/dev/null) if [ $? -ne 0 ]; then log "Failed to get account ID with profile ${profile}" true return 1 fi account=${account:--1} if [ "${account}" -ne -1 ]; then echo "${account}" fi return 0 } TEMP_FILE="" function login () { local profile=${1:-$PROFILE} TEMP_FILE=$(mktemp) trap 'rm -f "${TEMP_FILE}"' EXIT # run in background, because output is blocked by web browser aws sso login --profile "${profile}" > "${TEMP_FILE}" 2>&1 & while [ ! -s "${TEMP_FILE}" ]; do sleep 0.1 done local code=$(cat "${TEMP_FILE}" | tail -1) if [[ "${code}" =~ ^[A-Z]{4}-[A-Z]{4}$ ]]; then #rm "${TEMP_FILE}" echo "${code}" return 0 else log "Invalid authorization code. code=${code}, file=${TEMP_FILE}" true return 1 fi } if ! command -v aws >/dev/null 2>&1; then with_error "'aws' is required."; fi CURRENT_ACCOUNT=$(get_account) # Login if there is no session if [ $? -eq 0 ] && [ -n "${CURRENT_ACCOUNT}" ]; then log "Currently logged into ${CURRENT_ACCOUNT}" else log "No active session. Starting SSO flow..." fi # Login if currently not logged in or logged into a diffeent account if [ "${CURRENT_ACCOUNT:--1}" -ne "${ACCOUNT_ID}" ]; then AUTH_CODE=$(login) if [ $? -eq 0 ] && [ -n "${AUTH_CODE}" ]; then log "Authorization Code: ${AUTH_CODE}" echo "${AUTH_CODE}" | awk '{ for(i=1; i<=length($0); i++) print substr($0, i, 1) }' | say fi fi -
spencerdcarlson revised this gist
Oct 9, 2024 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ function log () { } function get_account () { local profile=${1:-$PROFILE} local account=$(aws sts get-caller-identity --query "Account" --profile "${profile}" --output text 2>/dev/null) account=${account:--1} if [ "${account}" -ne -1 ]; then @@ -26,7 +26,7 @@ function get_account () { } function login () { local profile=${1:-$PROFILE} local sout=$(mktemp) # run in background, because output is blocked by web browser aws sso login --profile "${profile}" > "${sout}" 2>&1 & -
spencerdcarlson created this gist
Oct 9, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,63 @@ #!/usr/bin/env bash PROFILE=${1} ACCOUNT_ID=${2} function log () { local message="${1}" local is_error="${2:-false}" if [[ "${is_error}" == true ]]; then echo "$(date +"%Y-%m-%d %H:%M:%S") - ERROR: ${message}" >&2 else echo "$(date +"%Y-%m-%d %H:%M:%S") - INFO: ${message}" fi } function get_account () { local profile=${PROFILE} local account=$(aws sts get-caller-identity --query "Account" --profile "${profile}" --output text 2>/dev/null) account=${account:--1} if [ "${account}" -ne -1 ]; then CURRENT_ACCOUNT="${account}" echo "${CURRENT_ACCOUNT}" fi return 0 } function login () { local profile=${PROFILE} local sout=$(mktemp) # run in background, because output is blocked by web browser aws sso login --profile "${profile}" > "${sout}" 2>&1 & while [ ! -s "${sout}" ]; do sleep 0.1 done local code=$(cat "${sout}" | tail -1) echo "Authorization Code: ${code}" rm "${sout}" if [[ "${code}" =~ ^[A-Z]{4}-[A-Z]{4}$ ]]; then AUTH_CODE="${code}" echo "${AUTH_CODE}" return 0 fi return 1 } get_account > /dev/null 2>&1 # Login if there is no session if [ -n "${CURRENT_ACCOUNT}" ]; then log "Currently logged into ${CURRENT_ACCOUNT}" else log "No active session. Starting SSO flow..." fi # Login if currently not logged in or logged into a diffeent account if [ "${CURRENT_ACCOUNT:--1}" -ne "${ACCOUNT_ID}" ]; then login > /dev/null 2>&1 if [ $? -eq 0 ] && [ -n "${AUTH_CODE}" ]; then log "Authorization Code: ${AUTH_CODE}" echo "${AUTH_CODE}" | awk '{ for(i=1; i<=length($0); i++) print substr($0, i, 1) }' | say fi fi