Skip to content

Instantly share code, notes, and snippets.

@joariasl
Last active November 3, 2021 15:06
Show Gist options
  • Save joariasl/d2a4a05ec05b68218ea3ed9d9eeb27bb to your computer and use it in GitHub Desktop.
Save joariasl/d2a4a05ec05b68218ea3ed9d9eeb27bb to your computer and use it in GitHub Desktop.

Revisions

  1. joariasl revised this gist Jun 18, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -99,7 +99,7 @@ if [ "${token_code}" ]; then
    command="${command} --token-code ${token_code}"
    fi

    result="$(eval ${command})" || exit 1;
    result=$(eval ${command}) || exit 1;
    access_key_id=$(printf '%s' "${result}" | awk '{print $1;}')
    secret_access_key=$(printf '%s' "${result}" | awk '{print $2;}')
    session_token=$(printf '%s' "${result}" | sed 's/[[:blank:]]$//g' | awk '{print $3;}')
  2. joariasl revised this gist Jun 18, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -99,10 +99,10 @@ if [ "${token_code}" ]; then
    command="${command} --token-code ${token_code}"
    fi

    result=$(eval ${command}) || exit 1;
    access_key_id=$(echo "${result}" | awk '{print $1;}')
    secret_access_key=$(echo "${result}" | awk '{print $2;}')
    session_token=$(echo "${result}" | awk '{print $3;}')
    result="$(eval ${command})" || exit 1;
    access_key_id=$(printf '%s' "${result}" | awk '{print $1;}')
    secret_access_key=$(printf '%s' "${result}" | awk '{print $2;}')
    session_token=$(printf '%s' "${result}" | sed 's/[[:blank:]]$//g' | awk '{print $3;}')

    aws configure set profile.${profile_set}.aws_access_key_id $access_key_id
    aws configure set profile.${profile_set}.aws_secret_access_key $secret_access_key
  3. joariasl revised this gist Jun 12, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -25,6 +25,7 @@ ${text_bold}SYNOPSIS${text_normal}
    ${text_bold}EXAMPLES${text_normal}
    Issue a STS token using example.mfa profile to set the example profile
    $ ${script_name} --profile-mfa example.mfa --profile-set example --duration-seconds 129600 --serial-number arn:aws:iam::000000000000:mfa/iam_user
    $ ${script_name} --profile-mfa example.mfa --profile-set example --duration-seconds 129600 --serial-number arn:aws:iam::000000000000:mfa/iam_user --token-code 000000
    ${script_name}" | less
  4. joariasl revised this gist Jun 12, 2020. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,15 @@ text_bold=$(tput bold)
    text_normal=$(tput sgr0)

    showHelp() {
    echo "${script_name}
    echo -e "${script_name}
    ${text_bold}DESCRIPTION${text_normal}
    The aws configure set command can be used to set a single configuration
    Script to issue a STS token using an AWS profile credential that set
    another AWS profile credential with the result configuration values
    from the config file.
    See '${script_name} help' for descriptions of global parameters.
    ${text_bold}SYNOPSIS${text_normal}
    ${script_name}
    @@ -19,7 +27,7 @@ ${text_bold}EXAMPLES${text_normal}
    Issue a STS token using example.mfa profile to set the example profile
    $ ${script_name} --profile-mfa example.mfa --profile-set example --duration-seconds 129600 --serial-number arn:aws:iam::000000000000:mfa/iam_user --token-code 000000
    ${script_name}"
    ${script_name}" | less
    }

    if (( ${#@} == 0 )); then
    @@ -51,7 +59,7 @@ while [ "$1" != "" ]; do
    shift
    token_code=$1
    ;;
    -h | --help )
    help | --help | -h )
    showHelp
    exit 0
    ;;
  5. joariasl revised this gist Jun 12, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ ${text_bold}SYNOPSIS${text_normal}
    [--profile-set <value>]
    [--duration-seconds <value>]
    [--serial-number <value>]
    --token-code <mfa-code>
    [--token-code <mfa-code>]
    ${text_bold}EXAMPLES${text_normal}
    Issue a STS token using example.mfa profile to set the example profile
  6. joariasl revised this gist Jun 12, 2020. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -63,9 +63,17 @@ while [ "$1" != "" ]; do
    shift
    done

    if [ -z "${profile_set}" ]; then
    profile_set="default"
    fi

    if [ -z "${token_code}" ]; then
    echo "--token-code is required"
    exit 1
    echo -n "Enter token code: "
    read -r token_code
    if [ -z "${token_code}" ]; then
    echo "--token-code is required"
    exit 1
    fi
    fi

    command="aws sts get-session-token --output text --query '*.[AccessKeyId,SecretAccessKey,SessionToken]'"
    @@ -82,7 +90,7 @@ if [ "${token_code}" ]; then
    command="${command} --token-code ${token_code}"
    fi

    result=$(eval ${command});
    result=$(eval ${command}) || exit 1;
    access_key_id=$(echo "${result}" | awk '{print $1;}')
    secret_access_key=$(echo "${result}" | awk '{print $2;}')
    session_token=$(echo "${result}" | awk '{print $3;}')
  7. joariasl revised this gist Jun 9, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,7 @@ ${text_bold}SYNOPSIS${text_normal}
    ${text_bold}EXAMPLES${text_normal}
    Issue a STS token using example.mfa profile to set the example profile
    $ ${script_name} --profile-mfa example.mfa --profile-set example --duration-seconds 129600 --serial-number arn:aws:iam::000000000000:mfa/iam_user --token-code 000000
    ${script_name}"
    }

  8. joariasl created this gist Jun 9, 2020.
    93 changes: 93 additions & 0 deletions aws-sts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    #!/bin/bash
    script_name=`basename "$0"`

    text_bold=$(tput bold)
    text_normal=$(tput sgr0)

    showHelp() {
    echo "${script_name}
    ${text_bold}SYNOPSIS${text_normal}
    ${script_name}
    [--profile-mfa <value>]
    [--profile-set <value>]
    [--duration-seconds <value>]
    [--serial-number <value>]
    --token-code <mfa-code>
    ${text_bold}EXAMPLES${text_normal}
    Issue a STS token using example.mfa profile to set the example profile
    $ ${script_name} --profile-mfa example.mfa --profile-set example --duration-seconds 129600 --serial-number arn:aws:iam::000000000000:mfa/iam_user --token-code 000000
    ${script_name}"
    }

    if (( ${#@} == 0 )); then
    showHelp
    exit 1
    fi

    while [ "$1" != "" ]; do
    case $1 in
    --profile-mfa )
    shift
    profile_mfa=$1
    ;;
    --profile-set )
    shift
    profile_set=$1
    ;;
    --duration-seconds )
    shift
    duration_seconds=$1
    ;;

    --serial-number )
    shift
    serial_number=$1
    ;;

    --token-code )
    shift
    token_code=$1
    ;;
    -h | --help )
    showHelp
    exit 0
    ;;
    * )
    showHelp
    exit 1
    ;;
    esac
    shift
    done

    if [ -z "${token_code}" ]; then
    echo "--token-code is required"
    exit 1
    fi

    command="aws sts get-session-token --output text --query '*.[AccessKeyId,SecretAccessKey,SessionToken]'"
    if [ "${profile_mfa}" ]; then
    command="${command} --profile ${profile_mfa}"
    fi
    if [ "${duration_seconds}" ]; then
    command="${command} --duration-seconds ${duration_seconds}"
    fi
    if [ "${serial_number}" ]; then
    command="${command} --serial-number ${serial_number}"
    fi
    if [ "${token_code}" ]; then
    command="${command} --token-code ${token_code}"
    fi

    result=$(eval ${command});
    access_key_id=$(echo "${result}" | awk '{print $1;}')
    secret_access_key=$(echo "${result}" | awk '{print $2;}')
    session_token=$(echo "${result}" | awk '{print $3;}')

    aws configure set profile.${profile_set}.aws_access_key_id $access_key_id
    aws configure set profile.${profile_set}.aws_secret_access_key $secret_access_key
    aws configure set profile.${profile_set}.aws_session_token $session_token

    exit 0