This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
Arbitrary names for the default profile #21
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e7b475c
Add default profile option for when .aws/credentials does not necesar…
brainstorm b9780fd
Use _ARGS variables and tidy up a bit, thanks @reisingerf for bash de…
brainstorm e6d049e
Motivation/documentation for this change, /ping @reisingerf
brainstorm ca0f431
Thanks @reisingerf for fiding the ROLE_SESSION_ARGS gotcha
brainstorm a2dc5ce
Bump up session timeout to 12h as pointed out by: https://aws.amazon.…
brainstorm e30fe29
Added configurable ROLE_SESSION_TIMEOUT (fixes issue #19), thanks @gr…
brainstorm 4bf90b4
Gotcha with session tokens and chained roles, thanks @reisingerf for …
brainstorm b69c32a
Mention the specific AWS error message when that happens
brainstorm f1ecea2
Tests passing now, thanks @grahamjenson
brainstorm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -47,6 +47,7 @@ assume-role(){ | |
####### | ||
|
||
# exports | ||
export AWS_DEFAULT_PROFILE_ASSUME_ROLE | ||
export AWS_SESSION_START | ||
export AWS_SESSION_ACCESS_KEY_ID | ||
export AWS_SESSION_SECRET_ACCESS_KEY | ||
|
@@ -81,6 +82,19 @@ assume-role(){ | |
# SETUP | ||
####### | ||
|
||
# load default assume-role profile if available, use "default" otherwise | ||
if [ "$AWS_DEFAULT_PROFILE_ASSUME_ROLE" ]; then | ||
echo "Using assume-role default profile: $AWS_DEFAULT_PROFILE_ASSUME_ROLE" | ||
default_profile=${AWS_DEFAULT_PROFILE_ASSUME_ROLE} | ||
else | ||
default_profile="default" | ||
fi | ||
|
||
# load user-set ROLE_SESSION_TIMEOUT (up to 12h, 43200 seconds), use default 1h defined above otherwise | ||
if [ "$AWS_ROLE_SESSION_TIMEOUT" ]; then | ||
ROLE_SESSION_TIMEOUT=${AWS_ROLE_SESSION_TIMEOUT} | ||
fi | ||
|
||
# set account_name | ||
if [ -z "$account_name_input" ] && [ -z "$OUTPUT_TO_EVAL" ]; then | ||
echo -n "Assume Into Account [default]:" | ||
|
@@ -122,7 +136,7 @@ assume-role(){ | |
fi | ||
|
||
# set region | ||
AWS_CONFIG_REGION="$(aws configure get region)" | ||
AWS_CONFIG_REGION="$(aws configure get region --profile ${default_profile})" | ||
if [ -z "$aws_region_input" ] && [ -z "$AWS_REGION" ] && [ -z "$AWS_DEFAULT_REGION" ] && [ -z "$AWS_CONFIG_REGION" ] && [ -z "$OUTPUT_TO_EVAL" ]; then | ||
echo -n "Assume Into Region [us-east-1]: " | ||
read -r region | ||
|
@@ -173,12 +187,13 @@ assume-role(){ | |
fi | ||
|
||
# get the username attached to your default creds | ||
AWS_USERNAME=$(aws iam get-user --query User.UserName --output text) | ||
AWS_USERNAME=$(aws iam get-user --query User.UserName --output text --profile $default_profile) | ||
|
||
# get MFA device attached to default creds | ||
MFA_DEVICE_ARGS=(--user-name $AWS_USERNAME) | ||
MFA_DEVICE_ARGS+=(--query 'MFADevices[0].SerialNumber') | ||
MFA_DEVICE_ARGS+=(--output text) | ||
MFA_DEVICE_ARGS+=(--profile ${default_profile}) | ||
MFA_DEVICE=$(aws iam list-mfa-devices "${MFA_DEVICE_ARGS[@]}") | ||
MFA_DEVICE_STATUS=$? | ||
|
||
|
@@ -191,7 +206,10 @@ assume-role(){ | |
SESSION_ARGS=(--duration-seconds $SESSION_TIMEOUT) | ||
SESSION_ARGS+=(--serial-number ${MFA_DEVICE}) | ||
SESSION_ARGS+=(--token-code ${mfa_token}) | ||
SESSION_ARGS+=(--profile ${default_profile}) | ||
|
||
SESSION=$(aws sts get-session-token "${SESSION_ARGS[@]}") | ||
|
||
SESSION_STATUS=$? | ||
|
||
if [ $SESSION_STATUS -ne 0 ]; then | ||
|
@@ -216,6 +234,7 @@ assume-role(){ | |
ROLE_SESSION_ARGS+=(--external-id ${account_id}) | ||
ROLE_SESSION_ARGS+=(--duration-seconds ${ROLE_SESSION_TIMEOUT}) | ||
ROLE_SESSION_ARGS+=(--role-session-name $(date +%s)) | ||
|
||
ROLE_SESSION=$(aws sts assume-role "${ROLE_SESSION_ARGS[@]}" || echo "fail") | ||
|
||
if [ "$ROLE_SESSION" = "fail" ]; then | ||
|
@@ -235,6 +254,7 @@ assume-role(){ | |
|
||
# OUTPUTS ALL THE EXPORTS for eval $(assume-role [args]) | ||
if [ "$OUTPUT_TO_EVAL" = "true" ]; then | ||
echo "export AWS_DEFAULT_PROFILE_ASSUME_ROLE=\"$AWS_DEFAULT_PROFILE_ASSUME_ROLE\";" | ||
echo "export AWS_REGION=\"$AWS_REGION\";" | ||
echo "export AWS_DEFAULT_REGION=\"$AWS_DEFAULT_REGION\";" | ||
echo "export AWS_ACCESS_KEY_ID=\"$AWS_ACCESS_KEY_ID\";" | ||
|
@@ -252,14 +272,17 @@ assume-role(){ | |
|
||
# USED FOR TESTING AND DEBUGGING | ||
if [ "$DEBUG_ASSUME_ROLE" = "true" ]; then | ||
echo "AWS_DEFAULT_PROFILE_ASSUME_ROLE=\"$AWS_DEFAULT_PROFILE_ASSUME_ROLE\";" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this output to the end, as the majority of tests take the lines to ensure their order. |
||
echo "AWS_CONFIG_REGION=\"$AWS_CONFIG_REGION\";" | ||
echo "AWS_USERNAME=\"$AWS_USERNAME\";" | ||
echo "MFA_DEVICE_ARGS=\"${MFA_DEVICE_ARGS[*]}\";" | ||
echo "MFA_DEVICE=\"$MFA_DEVICE\";" | ||
echo "SESSION_ARGS=\"${SESSION_ARGS[*]}\";" | ||
echo "SESSION='$SESSION';" | ||
echo "SESSION=\"$SESSION\";" | ||
echo "ROLE_SESSION_ARGS=\"${ROLE_SESSION_ARGS[*]}\";" | ||
echo "ROLE_SESSION='$ROLE_SESSION';" | ||
echo "ROLE_SESSION=\"$ROLE_SESSION\";" | ||
echo "SESSION_TIMEOUT=\"$SESSION_TIMEOUT\";" | ||
echo "ROLE_SESSION_TIMEOUT=\"$ROLE_SESSION_TIMEOUT\";" | ||
fi | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this to the end. Not sure if it is necessary.