Skip to content

Commit 1fb4641

Browse files
committed
Support using MFA devices
1 parent 40036f0 commit 1fb4641

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ $ export AWS_REGION=us-west-2
3636
$ bash <( curl -Ls https://raw.githubusercontent.com/aws-containers/amazon-ecs-exec-checker/main/check-ecs-exec.sh ) <YOUR_ECS_CLUSTER_NAME> <YOUR_ECS_TASK_ID>
3737
```
3838

39-
_Example 3 - Switch AWS CLI binaries_
39+
_Example 3 - With MFA_
40+
41+
The `check-ecs-exec.sh` automatically detects your MFA configuration for the AWS CLI. But you can also explicitly specify which MFA device to use by setting the ARN of the MFA device to `AWS_MFA_SERIAL` environment variable.
42+
43+
_Example 4 - Switch AWS CLI binaries_
4044

4145
If you have multiple AWS CLI installations in your environment, both AWS CLI v1 and v2 for example, you can choose which AWS CLI binary to use by passing the `AWS_CLI_BIN` env variable.
4246

check-ecs-exec.sh

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,53 @@ if [[ ! "${status}" = 0 ]]; then
119119
fi
120120
printf "${COLOR_DEFAULT} AWS CLI | ${COLOR_GREEN}OK ${COLOR_DEFAULT}($(which "${AWS_CLI_BIN}"))\n"
121121

122+
# Find AWS region
123+
REGION=$(${AWS_CLI_BIN} configure get region || echo "")
124+
export AWS_REGION=${AWS_REGION:-$REGION}
125+
# Check region configuration in "source_profile" if the user uses MFA configurations
126+
source_profile=$(${AWS_CLI_BIN} configure get source_profile || echo "")
127+
if [ "${AWS_REGION}" = "" ] && [ "${source_profile}" != "" ]; then
128+
export AWS_REGION=$(${AWS_CLI_BIN} configure get region --profile ${source_profile} || echo "")
129+
fi
130+
if [[ "x${AWS_REGION}" = "x" ]]; then
131+
printf "${COLOR_RED}Pre-flight check failed: Missing AWS region. Use the \`aws configure set default.region\` command or set the \"AWS_REGION\" environment variable.\n" >&2
132+
exit 1
133+
fi
134+
122135
## 2. CHECK PREREQUISITES FOR USING ECS EXEC FEATURE VIA AWS CLI #########################
123136
printf "\n"
124137
printSectionHeaderLine
125138
printf "${COLOR_DEFAULT}Prerequisites for the AWS CLI to use ECS Exec\n"
126139
printSectionHeaderLine
127140
##########################################################################################
128141

129-
REGION=$(${AWS_CLI_BIN} configure get region || echo "")
130-
AWS_REGION=${AWS_REGION:-$REGION}
131-
if [[ "x${AWS_REGION}" = "x" ]]; then
132-
printf "${COLOR_RED}Pre-flight check failed: Missing AWS region. Use the \`aws configure set default.region\` command or set the \"AWS_REGION\" environment variable.\n" >&2
133-
exit 1
142+
# MFA
143+
AWS_MFA_SERIAL=${AWS_MFA_SERIAL:-$(${AWS_CLI_BIN} configure get mfa_serial || echo "")}
144+
ROLE_TO_BE_ASSUMED=$(${AWS_CLI_BIN} configure get role_arn || echo "")
145+
SOURCE_PROFILE=$(${AWS_CLI_BIN} configure get source_profile || echo "")
146+
# Normally we don't need to ask MFA code thanks to the AWS CLI
147+
# but we do need to prompt explicitly if the "AWS_MFA_SERIAL" value only exists without "role_arn" and "source_profile"
148+
if [ "${AWS_MFA_SERIAL}" != "" ] && [ "${ROLE_TO_BE_ASSUMED}" == "" ] && [ "${SOURCE_PROFILE}" == "" ]; then
149+
# Prpmpt users to enter MFA code to obtain temporary credentials
150+
mfa_code=""
151+
while true; do
152+
printf "\n"
153+
printf "Type MFA code for ${AWS_MFA_SERIAL}: "
154+
read mfa_code
155+
if [ -z "${mfa_code}" ]; then
156+
printf "${COLOR_RED}MFA code cannot be empty${COLOR_DEFAULT}"
157+
continue
158+
fi
159+
break
160+
done
161+
162+
tmpCreds=$(${AWS_CLI_BIN} sts get-session-token --serial-number "${AWS_MFA_SERIAL}" --token-code "${mfa_code}")
163+
export AWS_ACCESS_KEY_ID=$( echo "${tmpCreds}" | jq -r .Credentials.AccessKeyId )
164+
export AWS_SECRET_ACCESS_KEY=$( echo "${tmpCreds}" | jq -r .Credentials.SecretAccessKey )
165+
export AWS_SESSION_TOKEN=$( echo "${tmpCreds}" | jq -r .Credentials.SessionToken )
134166
fi
135167

168+
# Find caller identity
136169
callerIdentityJson=$(${AWS_CLI_BIN} sts get-caller-identity)
137170
ACCOUNT_ID=$(echo "${callerIdentityJson}" | jq -r ".Account")
138171
CALLER_IAM_ARN=$(echo "${callerIdentityJson}" | jq -r ".Arn")

0 commit comments

Comments
 (0)