-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add saml2aws_generate_config.sh script (#62)
* add saml2aws_generate_config.sh script * improve and format code
- Loading branch information
1 parent
736a4e6
commit 74a0481
Showing
2 changed files
with
66 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
# | ||
# This script is based on saml2aws in order to list the available roles and | ||
# automatically generate a config for aws cli containing the profiles matching | ||
# the different accounts / roles. Generated profiles use the credential_process | ||
# feature to execute saml2aws login automatically. | ||
|
||
set -euo pipefail | ||
|
||
ACCOUNT_REGEX="^Account: ([^[:space:]]*).*" | ||
ROLE_REGEX="arn:aws:iam::.*:role\/([^[:space:]]*)" | ||
account= | ||
|
||
# Retrieve role list from saml2aws | ||
roles_list=$(saml2aws list-roles --skip-prompt) | ||
num_lines=$(echo "$roles_list" | wc -l) | ||
current_line=0 | ||
|
||
# Iterate over role list to generate aws config | ||
echo "$roles_list" | while read -r line; do | ||
current_line=$((${current_line} + 1)) | ||
|
||
# Capture AWS account alias | ||
if [[ $line =~ $ACCOUNT_REGEX ]]; then | ||
account="${BASH_REMATCH[1]}" | ||
echo "## ${line}" | ||
echo "##" | ||
fi | ||
|
||
# Capture role name | ||
if [[ $line =~ $ROLE_REGEX ]]; then | ||
role_arn=$line | ||
role_name="${BASH_REMATCH[1]}" | ||
profile="${account}/${role_name}" | ||
echo "[profile ${profile}]" | ||
echo "output = json" | ||
echo "credential_process = saml2aws login --skip-prompt --quiet --credential-process --role ${role_arn} --profile ${profile}-saml2aws" | ||
if [[ $current_line -ne $num_lines ]]; then | ||
echo "" | ||
fi | ||
fi | ||
done |