Skip to content

Instantly share code, notes, and snippets.

@enly1
Created January 3, 2020 18:24
Show Gist options
  • Select an option

  • Save enly1/b812c34a2659fc59d4a6d84c7d02d971 to your computer and use it in GitHub Desktop.

Select an option

Save enly1/b812c34a2659fc59d4a6d84c7d02d971 to your computer and use it in GitHub Desktop.

Revisions

  1. enly1 created this gist Jan 3, 2020.
    65 changes: 65 additions & 0 deletions awslogin
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/bin/bash

    function awslogin {
    # set to yes to create one-time use profiles in /tmp
    # anything else will create them in $HOME/.aws/awschrome
    TEMP_PROFILE="no"

    # set to yes to always start in a new window
    NEW_WINDOW="no"

    profile="$1"
    if [[ -z "$profile" ]]; then
    echo "Profile is a required argument" >&2
    return 1
    fi

    themecolor="$2, $3, $4"
    themepath="${HOME}/.aws/themes"
    if [[ "$4" ]]; then

    mkdir "$themepath/$2-$3-$4" 2&>1 >/dev/null
    echo "{ \"manifest_version\": 2, \"version\": \"0.1\", \"name\": \"AWSLogin\", \"theme\": { \"colors\": { \"frame\": [$themecolor] } } }" > $themepath/$2-$3-$4/manifest.json

    themeswitch="--load-extension=$themepath/$2-$3-$4"
    fi

    # replace non word and not - with __
    profile_dir_name=${profile//[^a-zA-Z0-9_-]/__}
    user_data_dir="${HOME}/.aws/awschrome/${profile_dir_name}"
    new_window_arg=''

    if [[ "$TEMP_PROFILE" = "yes" ]]; then
    user_data_dir=$(mktemp -d /tmp/awschrome_userdata.XXXXXXXX)
    fi

    if [[ "$NEW_WINDOW" = "yes" ]]; then
    new_window_arg='--new-window'
    fi

    # run aws-vault
    # --prompt osascript only works on OSX
    url=$(aws-vault login $profile --stdout)
    status=$?

    if [[ ${status} -ne 0 ]]; then
    # bash will also capture stderr, so echo $url
    echo ${url}
    return ${status}
    fi

    mkdir -p ${user_data_dir}
    disk_cache_dir=$(mktemp -d /tmp/awschrome_cache.XXXXXXXX)
    /opt/google/chrome/chrome \
    --no-first-run \
    --user-data-dir=${user_data_dir} \
    --disk-cache-dir=${disk_cache_dir} \
    --disk-cache-size=10240 \
    --start-maximized \
    $themeswitch \
    ${new_window_arg} \
    ${url} \
    >/dev/null 2>&1 &
    }

    awslogin $@