|
1 | 1 | function asp --description 'Switches AWS profile' --argument-names 'aws_profile'
|
2 |
| - if test -n "$aws_profile" |
3 |
| - if fgrep -q "[profile $aws_profile]" ~/.aws/config |
4 |
| - set -l region \ |
5 |
| - (awk "/\[profile\ $aws_profile\]/,/^\$/ { if (\$1 == \"region\") { print \$3 }}" \ |
6 |
| - ~/.aws/config) |
7 |
| - |
8 |
| - if test -z "$region" |
9 |
| - echo "No region listed for $aws_profile profile" |
10 |
| - return 1 |
11 |
| - end |
12 |
| - |
13 |
| - set -l access_key \ |
14 |
| - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_access_key_id\") { print \$3 }}" \ |
15 |
| - ~/.aws/credentials) |
16 |
| - set -l secret_key \ |
17 |
| - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_secret_access_key\") { print \$3 }}" \ |
18 |
| - ~/.aws/credentials) |
19 |
| - set -l session_token "" |
| 2 | + if test -z "$aws_profile" |
| 3 | + echo "usage: asp <profile>" |
| 4 | + return 1 |
| 5 | + end |
20 | 6 |
|
21 |
| - if test -z "$access_key" -o -z "$secret_key" |
22 |
| - set -l role_arn \ |
23 |
| - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"role_arn\") { print \$3 }}" \ |
24 |
| - ~/.aws/credentials) |
25 |
| - set -l source_profile \ |
26 |
| - (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"source_profile\") { print \$3 }}" \ |
27 |
| - ~/.aws/credentials) |
| 7 | + set -l access_key \ |
| 8 | + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_access_key_id\") { print \$3 }}" \ |
| 9 | + $HOME/.aws/credentials) |
| 10 | + set -l secret_key \ |
| 11 | + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"aws_secret_access_key\") { print \$3 }}" \ |
| 12 | + $HOME/.aws/credentials) |
28 | 13 |
|
29 |
| - if test -n "$role_arn" -a -n "$source_profile" |
30 |
| - set -l json \ |
31 |
| - (aws sts assume-role --profile "$source_profile" --role-arn "$role_arn" \ |
32 |
| - --role-session-name "$aws_profile" --output json) |
| 14 | + set -l session_token "" |
| 15 | + if test -z "$access_key" -o -z "$secret_key" |
| 16 | + set -l role_arn \ |
| 17 | + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"role_arn\") { print \$3 }}" \ |
| 18 | + $HOME/.aws/credentials) |
| 19 | + set -l source_profile \ |
| 20 | + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"source_profile\") { print \$3 }}" \ |
| 21 | + $HOME/.aws/credentials) |
33 | 22 |
|
34 |
| - set access_key (echo $json | jq -r '.Credentials.AccessKeyId') |
35 |
| - set secret_key (echo $json | jq -r '.Credentials.SecretAccessKey') |
36 |
| - set session_token (echo $json | jq -r '.Credentials.SessionToken') |
37 |
| - else |
38 |
| - echo "Invalid $aws_profile profile in ~/.aws/credentials" |
39 |
| - return 1 |
40 |
| - end |
41 |
| - end |
| 23 | + if test -n "$role_arn" -a -n "$source_profile" |
| 24 | + set -l json \ |
| 25 | + (aws sts assume-role --profile "$source_profile" --role-arn "$role_arn" \ |
| 26 | + --role-session-name "$aws_profile" --output json) |
42 | 27 |
|
43 |
| - set -gx AWS_ACCESS_KEY_ID "$access_key" |
44 |
| - set -gx AWS_SECRET_ACCESS_KEY "$secret_key" |
45 |
| - set -gx AWS_SESSION_TOKEN "$session_token" |
46 |
| - set -gx AWS_SECURITY_TOKEN "$AWS_SESSION_TOKEN" |
47 |
| - set -gx AWS_DEFAULT_REGION "$region" |
48 |
| - set -gx AWS_DEFAULT_PROFILE "$aws_profile" |
49 |
| - set -gx aws_profile "$aws_profile" |
| 28 | + set access_key (echo $json | jq -r '.Credentials.AccessKeyId') |
| 29 | + set secret_key (echo $json | jq -r '.Credentials.SecretAccessKey') |
| 30 | + set session_token (echo $json | jq -r '.Credentials.SessionToken') |
50 | 31 | else
|
51 |
| - echo "No $aws_profile profile found in ~/.aws/config" |
| 32 | + echo "Invalid $aws_profile profile in $HOME/.aws/credentials" |
52 | 33 | return 1
|
53 | 34 | end
|
| 35 | + end |
54 | 36 |
|
55 |
| - else |
56 |
| - if test -n "$AWS_DEFAULT_PROFILE" |
57 |
| - asp "$AWS_DEFAULT_PROFILE" |
58 |
| - echo "$AWS_DEFAULT_PROFILE" |
59 |
| - else |
60 |
| - echo "No profile set" |
61 |
| - end |
| 37 | + set -gx AWS_ACCESS_KEY_ID "$access_key" |
| 38 | + set -gx AWS_SECRET_ACCESS_KEY "$secret_key" |
| 39 | + set -gx AWS_SESSION_TOKEN "$session_token" |
| 40 | + set -gx AWS_SECURITY_TOKEN "$AWS_SESSION_TOKEN" |
| 41 | + set -gx AWS_DEFAULT_PROFILE "$aws_profile" |
| 42 | + set -g aws_profile "$aws_profile" |
| 43 | + |
| 44 | + set -l region "" |
| 45 | + if fgrep -qs "$aws_profile" $HOME/.aws/config |
| 46 | + set region \ |
| 47 | + (awk "/$aws_profile/,/^\$/ { if (\$1 == \"region\") { print \$3 }}" \ |
| 48 | + $HOME/.aws/config) |
62 | 49 | end
|
| 50 | + if fgrep -qs "[$aws_profile]" $HOME/.aws/credentials |
| 51 | + set region \ |
| 52 | + (awk "/\[$aws_profile\]/,/^\$/ { if (\$1 == \"region\") { print \$3 }}" \ |
| 53 | + $HOME/.aws/credentials) |
| 54 | + end |
| 55 | + |
| 56 | + set -gx AWS_DEFAULT_REGION "$region" |
63 | 57 | end
|
0 commit comments