-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created agp; ~/.aws/config is now optional
- Loading branch information
Showing
2 changed files
with
54 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <profile>" | ||
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 |