Skip to content

Instantly share code, notes, and snippets.

@spencerdcarlson
Last active October 10, 2024 16:01

Revisions

  1. spencerdcarlson revised this gist Oct 10, 2024. 1 changed file with 4 additions and 8 deletions.
    12 changes: 4 additions & 8 deletions awslogin.sh
    Original 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)

    if [ $? -ne 0 ]; then
    log "Failed to get account ID with profile ${profile}" true
    return 1
    fi
    [ $? -eq 0 ] && [ "${account}" -ne -1 ] && echo "${account}"

    if [ "${account}" -ne -1 ]; then
    echo "${account}"
    fi
    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}"
    echo "${AUTH_CODE}" | awk '{ for(i=1; i<=length($0); i++) print substr($0, i, 1) }' | say
    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
  2. spencerdcarlson revised this gist Oct 10, 2024. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions awslogin.sh
    Original 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=$(aws sts get-caller-identity --query "Account" --profile "${profile}" --output text 2>/dev/null)
    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

    account=${account:--1}
    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=$(cat "${TEMP_FILE}" | tail -1)
    local code=""
    code=$(cat "${TEMP_FILE}" | tail -1)
    if [[ "${code}" =~ ^[A-Z]{4}-[A-Z]{4}$ ]]; then
    echo "${code}"
    return 0
  3. spencerdcarlson revised this gist Oct 9, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion awslogin.sh
    Original 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
    #rm "${TEMP_FILE}"
    echo "${code}"
    return 0
    else
  4. spencerdcarlson revised this gist Oct 9, 2024. 1 changed file with 33 additions and 15 deletions.
    48 changes: 33 additions & 15 deletions awslogin.sh
    Original 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
    CURRENT_ACCOUNT="${account}"
    echo "${CURRENT_ACCOUNT}"
    echo "${account}"
    fi
    return 0
    }

    TEMP_FILE=""

    function login () {
    local profile=${1:-$PROFILE}
    local sout=$(mktemp)
    TEMP_FILE=$(mktemp)
    trap 'rm -f "${TEMP_FILE}"' EXIT
    # run in background, because output is blocked by web browser
    aws sso login --profile "${profile}" > "${sout}" 2>&1 &
    while [ ! -s "${sout}" ]; do
    aws sso login --profile "${profile}" > "${TEMP_FILE}" 2>&1 &
    while [ ! -s "${TEMP_FILE}" ]; do
    sleep 0.1
    done
    local code=$(cat "${sout}" | tail -1)
    echo "Authorization Code: ${code}"
    rm "${sout}"
    local code=$(cat "${TEMP_FILE}" | tail -1)
    if [[ "${code}" =~ ^[A-Z]{4}-[A-Z]{4}$ ]]; then
    AUTH_CODE="${code}"
    echo "${AUTH_CODE}"
    #rm "${TEMP_FILE}"
    echo "${code}"
    return 0
    else
    log "Invalid authorization code. code=${code}, file=${TEMP_FILE}" true
    return 1
    fi
    return 1
    }

    get_account > /dev/null 2>&1
    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 [ -n "${CURRENT_ACCOUNT}" ]; then
    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
    login > /dev/null 2>&1
    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
    fi
  5. spencerdcarlson revised this gist Oct 9, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions awslogin.sh
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ function log () {
    }

    function get_account () {
    local profile=${PROFILE}
    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=${PROFILE}
    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 &
  6. spencerdcarlson created this gist Oct 9, 2024.
    63 changes: 63 additions & 0 deletions awslogin.sh
    Original 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