From abef952139b065e061d65620fd623af44cc020f9 Mon Sep 17 00:00:00 2001 From: Pascal Frenz <5677521+PascalFrenz@users.noreply.github.com> Date: Fri, 24 May 2024 11:04:01 +0200 Subject: [PATCH 1/3] add option to alias the profile name and added --no-export and --debug options --- aws_login.sh | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/aws_login.sh b/aws_login.sh index 774f958..692d402 100644 --- a/aws_login.sh +++ b/aws_login.sh @@ -7,7 +7,7 @@ # to avoid the login screen in the future. # # usage: source aws_login.sh -# aws_login [aws_account_id] +# aws_login [--no-export|--debug] [aws_account_id|aws_account_id=aws_profile_name_alias] # # @@ -25,7 +25,33 @@ function is_shell() { } function aws_login() { - aws_account_no="$1"; + no_export=false + aws_profile_name_alias="" + aws_account_no="" + debug=false + # Read all parameters into local variables. If no parameters are given, set the default values. + while [ "$#" -gt 0 ]; do + case "$1" in + --no-export) + no_export=true; + ;; + --debug) + debug=true; + set -x; + ;; + *) + # if $1 contains =, then split it and assign the first part to aws_accunt and the second part to aws_profile_name_input + if [[ "$1" == *"="* ]]; then + aws_account_no=$(echo "$1" | cut -d'=' -f1) + aws_profile_name_alias=$(echo "$1" | cut -d'=' -f2) + else + aws_account_no="$1" + fi + ;; + esac + shift + done + local config_dir="$HOME/.config/rackspace-aws-login" if [ ! -d "$config_dir" ]; then mkdir -p "$config_dir" @@ -219,13 +245,27 @@ function aws_login() { aws configure --profile "$aws_profile_name" set aws_access_key_id "$aws_access_key_id" aws configure --profile "$aws_profile_name" set aws_secret_access_key "$aws_secret_access_key" aws configure --profile "$aws_profile_name" set aws_session_token "$aws_session_token" + + aws configure --profile "$aws_profile_name_alias" set aws_access_key_id "$aws_access_key_id" + aws configure --profile "$aws_profile_name_alias" set aws_secret_access_key "$aws_secret_access_key" + aws configure --profile "$aws_profile_name_alias" set aws_session_token "$aws_session_token" else echo "The AWS credentials are still valid." fi - echo "Setting AWS_PROFILE to $aws_profile_name" + # Export the AWS_PROFILE variable if the --no-export flag is not set + if [ "$no_export" = false ]; then + if [ -z "$aws_profile_name_alias" ]; then + aws_profile_name_alias="$aws_profile_name" + fi + echo "Setting AWS_PROFILE to $aws_profile_name_alias" + export AWS_PROFILE="$aws_profile_name_alias" + fi - export AWS_PROFILE="$aws_profile_name" + # disable debug mode + if [ "$debug" = true ]; then + set +x + fi return 0 } From b458833cd1860d88e8ad82e78cb8917cebd2045d Mon Sep 17 00:00:00 2001 From: Pascal Frenz <5677521+PascalFrenz@users.noreply.github.com> Date: Fri, 24 May 2024 11:12:19 +0200 Subject: [PATCH 2/3] check input for validity if alias is present --- aws_login.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/aws_login.sh b/aws_login.sh index 692d402..6058f34 100644 --- a/aws_login.sh +++ b/aws_login.sh @@ -44,6 +44,12 @@ function aws_login() { if [[ "$1" == *"="* ]]; then aws_account_no=$(echo "$1" | cut -d'=' -f1) aws_profile_name_alias=$(echo "$1" | cut -d'=' -f2) + + # validate, that aws_account_no and aws_profile_name_alias are not empty + if [ -z "$aws_account_no" ] || [ -z "$aws_profile_name_alias" ]; then + echo "Invalid input. Please provide the AWS account number and the AWS profile name alias separated by '='." + return 1 + fi else aws_account_no="$1" fi From 5e8e62dbf821603cc4a351d3cf033b73cabfff2d Mon Sep 17 00:00:00 2001 From: Pascal Frenz <5677521+PascalFrenz@users.noreply.github.com> Date: Fri, 24 May 2024 11:13:56 +0200 Subject: [PATCH 3/3] fix spelling mistake --- aws_login.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws_login.sh b/aws_login.sh index 6058f34..f4feed2 100644 --- a/aws_login.sh +++ b/aws_login.sh @@ -40,7 +40,7 @@ function aws_login() { set -x; ;; *) - # if $1 contains =, then split it and assign the first part to aws_accunt and the second part to aws_profile_name_input + # if $1 contains =, then split it and assign the first part to aws_account and the second part to aws_profile_name_input if [[ "$1" == *"="* ]]; then aws_account_no=$(echo "$1" | cut -d'=' -f1) aws_profile_name_alias=$(echo "$1" | cut -d'=' -f2)