From a37a54615dd8c913d3de3e4783d55fab9703836f Mon Sep 17 00:00:00 2001 From: Michael Goodness Date: Mon, 27 Mar 2017 20:10:54 -0500 Subject: [PATCH] Created agp; ~/.aws/config is now optional --- functions/agp.fish | 8 ++++ functions/asp.fish | 98 ++++++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 functions/agp.fish diff --git a/functions/agp.fish b/functions/agp.fish new file mode 100644 index 0000000..e2d61b8 --- /dev/null +++ b/functions/agp.fish @@ -0,0 +1,8 @@ +function agp --description 'Get current AWS profile' + if test -n "$AWS_DEFAULT_PROFILE" + echo "$AWS_DEFAULT_PROFILE" + else + echo "No profile set" + end +end + diff --git a/functions/asp.fish b/functions/asp.fish index b156de1..9c659fc 100644 --- a/functions/asp.fish +++ b/functions/asp.fish @@ -1,63 +1,57 @@ function asp --description 'Switches AWS profile' --argument-names 'aws_profile' - if test -n "$aws_profile" - if fgrep -q "[profile $aws_profile]" ~/.aws/config - set -l region \ - (awk "/\[profile\ $aws_profile\]/,/^\$/ { if (\$1 == \"region\") { print \$3 }}" \ - ~/.aws/config) - - if test -z "$region" - echo "No region listed for $aws_profile profile" - return 1 - end - - set -l access_key \ - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_access_key_id\") { print \$3 }}" \ - ~/.aws/credentials) - set -l secret_key \ - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_secret_access_key\") { print \$3 }}" \ - ~/.aws/credentials) - set -l session_token "" + if test -z "$aws_profile" + echo "usage: asp " + return 1 + end - if test -z "$access_key" -o -z "$secret_key" - set -l role_arn \ - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"role_arn\") { print \$3 }}" \ - ~/.aws/credentials) - set -l source_profile \ - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"source_profile\") { print \$3 }}" \ - ~/.aws/credentials) + set -l access_key \ + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_access_key_id\") { print \$3 }}" \ + $HOME/.aws/credentials) + set -l secret_key \ + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_secret_access_key\") { print \$3 }}" \ + $HOME/.aws/credentials) - if test -n "$role_arn" -a -n "$source_profile" - set -l json \ - (aws sts assume-role --profile "$source_profile" --role-arn "$role_arn" \ - --role-session-name "$aws_profile" --output json) + set -l session_token "" + if test -z "$access_key" -o -z "$secret_key" + set -l role_arn \ + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"role_arn\") { print \$3 }}" \ + $HOME/.aws/credentials) + set -l source_profile \ + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"source_profile\") { print \$3 }}" \ + $HOME/.aws/credentials) - set access_key (echo $json | jq -r '.Credentials.AccessKeyId') - set secret_key (echo $json | jq -r '.Credentials.SecretAccessKey') - set session_token (echo $json | jq -r '.Credentials.SessionToken') - else - echo "Invalid $aws_profile profile in ~/.aws/credentials" - return 1 - end - end + if test -n "$role_arn" -a -n "$source_profile" + set -l json \ + (aws sts assume-role --profile "$source_profile" --role-arn "$role_arn" \ + --role-session-name "$aws_profile" --output json) - set -gx AWS_ACCESS_KEY_ID "$access_key" - set -gx AWS_SECRET_ACCESS_KEY "$secret_key" - set -gx AWS_SESSION_TOKEN "$session_token" - set -gx AWS_SECURITY_TOKEN "$AWS_SESSION_TOKEN" - set -gx AWS_DEFAULT_REGION "$region" - set -gx AWS_DEFAULT_PROFILE "$aws_profile" - set -gx aws_profile "$aws_profile" + set access_key (echo $json | jq -r '.Credentials.AccessKeyId') + set secret_key (echo $json | jq -r '.Credentials.SecretAccessKey') + set session_token (echo $json | jq -r '.Credentials.SessionToken') else - echo "No $aws_profile profile found in ~/.aws/config" + echo "Invalid $aws_profile profile in $HOME/.aws/credentials" return 1 end + end - else - if test -n "$AWS_DEFAULT_PROFILE" - asp "$AWS_DEFAULT_PROFILE" - echo "$AWS_DEFAULT_PROFILE" - else - echo "No profile set" - end + set -gx AWS_ACCESS_KEY_ID "$access_key" + set -gx AWS_SECRET_ACCESS_KEY "$secret_key" + set -gx AWS_SESSION_TOKEN "$session_token" + set -gx AWS_SECURITY_TOKEN "$AWS_SESSION_TOKEN" + set -gx AWS_DEFAULT_PROFILE "$aws_profile" + set -g aws_profile "$aws_profile" + + set -l region "" + if fgrep -qs "$aws_profile" $HOME/.aws/config + set region \ + (awk "/$aws_profile/,/^\$/ { if (\$1 == \"region\") { print \$3 }}" \ + $HOME/.aws/config) end + if fgrep -qs "[$aws_profile]" $HOME/.aws/credentials + set region \ + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"region\") { print \$3 }}" \ + $HOME/.aws/credentials) + end + + set -gx AWS_DEFAULT_REGION "$region" end