Skip to content

Commit

Permalink
Created agp; ~/.aws/config is now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mgoodness committed Mar 28, 2017
1 parent d168cc7 commit a37a546
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 52 deletions.
8 changes: 8 additions & 0 deletions functions/agp.fish
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

98 changes: 46 additions & 52 deletions functions/asp.fish
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

0 comments on commit a37a546

Please sign in to comment.