Skip to content

Instantly share code, notes, and snippets.

@dessibelle
Last active November 14, 2024 19:45
Show Gist options
  • Select an option

  • Save dessibelle/7944a58ee17c7c4945f8aea2acefca85 to your computer and use it in GitHub Desktop.

Select an option

Save dessibelle/7944a58ee17c7c4945f8aea2acefca85 to your computer and use it in GitHub Desktop.
Open an AWS console using separate Chrome profiles
#!/bin/bash
SCRIPT_NAME=$(basename $0)
OVERRIDE_BROWSER=$(cat ~/.awsconsolerc | grep -i BROWSER_BUNDLE_IDENTIFIER | cut -d '=' -f 2 | xargs)
DEFAULT_BROWSER=$(defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | grep -B 1 "LSHandlerURLScheme = https;" | sed -n -e 's/^.*RoleAll = "//' -e 's/";//p')
BROWSER_BUNDLE_IDENTIFIER="${OVERRIDE_BROWSER:-$DEFAULT_BROWSER}"
function print_usage() {
echo "Usage:"
echo -e "\t${SCRIPT_NAME} [-p | --prompt <aws-vault prompt>] [-b | --browser <bundle identifier>] <aws_profile>"
echo -e "\nHint: set AWS_VAULT_PROMPT to e.g. 'ykman' to have aws-console default to ykman."
}
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
print_usage
exit
;;
-p | --prompt)
shift
VALUE=$1
PROMPT="--prompt ${VALUE}"
;;
-b | --browser)
shift
VALUE=$1
BROWSER_BUNDLE_IDENTIFIER="${VALUE}"
;;
*)
PROFILE_NAME=$PARAM
;;
esac
shift
done
LOGIN_URL=$(aws-vault $PROMPT login $PROFILE_NAME --stdout)
case $BROWSER_BUNDLE_IDENTIFIER in
# Google Chrome & Microsoft Edge
com.google.Chrome | com.microsoft.edgemac)
echo "${LOGIN_URL}" | nohup xargs -t open -b "${BROWSER_BUNDLE_IDENTIFIER}" -n --args --profile-directory="${PROFILE_NAME}" > /dev/null 2>&1 &
;;
# Firefox
# TODO: This should be able to create/switch profiles and open tabs in the correct profile.
org.mozilla.firefox)
# open -b org.mozilla.firefox --args -no-remote -CreateProfile "${PROFILE_NAME}" &
# open -b org.mozilla.firefox --args -no-remote -ProfileManager &
echo "${LOGIN_URL}" | nohup xargs -t open -b org.mozilla.firefox --args -no-remote -P "${PROFILE_NAME}" -foreground -new-window > /dev/null 2>&1 &
;;
# Arc
# TODO: This should be able to create/switch Spaces and open tabs in the correct Space.
company.thebrowser.Browser)
open -b company.thebrowser.Browser
echo "${LOGIN_URL}" | nohup xargs -S 100000 -I% osascript -e "tell application \"Arc.app\"
tell front window
create new space
tell space \"${PROFILE_NAME}\"
make new tab with properties {URL:\"%\"}
end tell
end tell
activate
end tell" > /dev/null 2>&1 &
;;
*)
echo "Browser with bundle identifier '${BROWSER_BUNDLE_IDENTIFIER}' not supported."
echo "Use this URL to sign in: ${LOGIN_URL}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment