Skip to content

Commit

Permalink
Set global $aws_profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Goodness committed Oct 11, 2016
1 parent 3906eef commit 8399887
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<br/>

Enables quick switching between AWS profiles. Supports both hard-coded keys and role assumption.
Enables quick switching between AWS profiles. Supports both hard-coded keys and role assumption. Works with the [aws](https://github.com/oh-my-fish/plugin-aws) plugin by [@sagebind](https://github.com/sagebind).


## Prerequisites
Expand Down
25 changes: 19 additions & 6 deletions functions/asp.fish
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
function asp -d 'Switches AWS profile' -a '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)
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 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 "$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 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)

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 json \
(aws sts assume-role --profile "$source_profile" --role-arn "$role_arn" \
--role-session-name "$aws_profile" --output json)

set access_key (echo $json | jq -r '.Credentials.AccessKeyId')
set secret_key (echo $json | jq -r '.Credentials.SecretAccessKey')
Expand All @@ -34,6 +46,7 @@ function asp -d 'Switches AWS profile' -a 'aws_profile'
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"
else
echo "No $aws_profile profile found in ~/.aws/config"
return 1
Expand Down

0 comments on commit 8399887

Please sign in to comment.