diff --git a/binaries/awssudo b/binaries/awssudo new file mode 100755 index 0000000..485b03e --- /dev/null +++ b/binaries/awssudo @@ -0,0 +1,123 @@ +#!/usr/bin/env bash + +set -e # Exit on any child process error + +function usage () { + echo "awsudo [-h|--help] [-v|--verbose] [-d|--duration-seconds] [-u|--use-credential-store] " + exit 1 +} + +function check_dependency () { + if [[ "$(which $1 2> /dev/null)" == "" ]]; then + echo "$1 not found, please install and make sure it's on the PATH" + exit 1 + fi +} + +VERBOSE=false +DURATION_SECONDS=900 +USE_CREDENTIALS_STORE="false" +POSITIONAL=() + +while [[ $# -gt 0 ]] +do +key="$1" +case $key in + # IMPORTANT: We MUST skip options that are not at the very beginning of the command. + # Otherwise we could be stripping options being sent to the subcommand + -h|--help) + if [[ "${#POSITIONAL[@]}" = 0 ]]; then + usage + else + POSITIONAL+=("$1") + shift + fi + ;; + -v|--verbose) + if [[ "${#POSITIONAL[@]}" = 0 ]]; then + VERBOSE=true + shift + else + POSITIONAL+=("$1") + shift + fi + ;; + -d|--duration-seconds) + if [[ "${#POSITIONAL[@]}" = 0 ]]; then + DURATION_SECONDS=$2 + shift 2 + else + POSITIONAL+=("$1") + shift + fi + ;; + -u|--use-credential-store) + if [[ "${#POSITIONAL[@]}" = 0 ]]; then + USE_CREDENTIALS_STORE="true" + shift + else + POSITIONAL+=("$1") + shift + fi + ;; + *) # unknown option + POSITIONAL+=("$1") + shift + ;; +esac +done +set -- "${POSITIONAL[@]}" # restore positional parameters + +ROLE_ARN=$1 +[[ -z ${ROLE_ARN} ]] && usage +shift + +if [[ ${ROLE_ARN} =~ ^arn:aws:iam ]]; then + if [[ "$VERBOSE" == "true" ]]; then + echo "Using RoleArn: ${ROLE_ARN}" + echo "Using Duration: ${DURATION_SECONDS}" + fi +else + echo "Invalid role arn provided. Provided value: ${ROLE_ARN}" + exit 1 +fi + +check_dependency "jq" +check_dependency "aws" + +# Assume role for running cloud formation +if [[ "$USE_CREDENTIALS_STORE" == "true" ]]; then + tmpfile=$(mktemp /tmp/awssudo.XXXXXX) + + cat >${tmpfile} <