Skip to content

Commit

Permalink
Add more-options
Browse files Browse the repository at this point in the history
  • Loading branch information
Laica-Lunasys committed Jul 28, 2021
1 parent aace841 commit fc77b5e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ awsctx() {
# Uncomment me If you want to use CLI based 2FA
# MFA_TOKEN=$(oathtool -b --totp $AWS_MFA_SECRET)
# ---

if [ "$MFA_TOKEN" != "" ]; then
bash $AWSCTX/awsctx.sh $@ $MFA_TOKEN
bash $AWSCTX/awsctx.sh $1 $MFA_TOKEN $([ "$#" -gt 1 ] && shift 1 && echo $@)
else
bash $AWSCTX/awsctx.sh $@
fi
Expand All @@ -29,7 +28,7 @@ sync-aws-profile

## Usage
```bash
awsctx <AWS_ACCOUNT> [MFA_TOKEN]
awsctx <AWS_ACCOUNT/all> [MFA_TOKEN] [login|list-roles|console|...]
# If you defined MFA_TOKEN: `awsctx myservice-production`
# or else: `awsctx myservice-production 123456`
```
Expand Down
14 changes: 11 additions & 3 deletions awsctx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ cd "$(dirname "$0")"

_TARGET="$1"
_MFA_TOKEN="$2"
_OPTIONS="$3"

_login() {
_account="$1"
_mfa="$2"
_options="$3"
echo ":: Logging in to: $_account"
saml2aws login -a $_account \
--skip-prompt \
Expand All @@ -16,16 +18,22 @@ _login() {
echo ":: Update AWS_PROFILE: $_account"
touch $PWD/aws_profile
echo "$_account" > $PWD/aws_profile

if [ "$_options" != "" ]; then
saml2aws $_options $(shift 3 && echo $@) -a $_account \
--skip-prompt \
--mfa-token="$_mfa"
fi
}

if [ "$_TARGET" == "all" ]; then
for e in $(cat ~/.saml2aws | grep "\[*\]" | cat); do
_a=$(echo $e | tr -d '[]')
_login $_a $_MFA_TOKEN
_login $_a $_MFA_TOKEN $_OPTIONS $(shift 3 && echo $@)
done
elif [ "$_TARGET" != "" ] && [ "$_MFA_TOKEN" != "" ]; then
_login $_TARGET $_MFA_TOKEN
_login $_TARGET $_MFA_TOKEN $_OPTIONS $(shift 3 && echo $@)
else
echo "Usage: awsctx <target/all> <mfa_token>"
echo "Usage: awsctx <target/all> [MFA_TOKEN] [login|list-roles|console]"
exit 1
fi

0 comments on commit fc77b5e

Please sign in to comment.