Soldato
I am trying to write a fairly simple script to help people switch AWS IAM roles and use MFA on the cli.
I have it working but now I am down to the fine details and final touches.
I have a problem capturing the output of a command and testing the exit status.
Here is an excerpt of the code:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
read -p "Please specify in seconds your session duration: " SESSION_DURATION
read -p "Please enter MFA token: " MFA_TOKEN
MFA_AUTH=$(aws sts get-session-token --duration-seconds $SESSION_DURATION --serial-number $MFA_DEVICE_ARN --token-code $MFA_TOKEN)
if [ $? -eq 0 ]; then
export AWS_ACCESS_KEY_ID=$(echo $MFA_AUTH | jq -r .Credentials.AccessKeyId)
export AWS_SECRET_ACCESS_KEY=$(echo $MFA_AUTH | jq -r .Credentials.SecretAccessKey)
export AWS_SESSION_TOKEN=$(echo $MFA_AUTH | jq -r .Credentials.SessionToken)
else
echo "MFA Authentication failure. Exiting"
return 1
fi
As you can see, for me to extract the keys I have to assign the command to a variable. That's fine. But If the user puts in the wrong MFA token currently I have no way of testing if the exit status of the previous command succeeded because it is assigned to a variable.
Any ideas?
I can test the exit status of: aws sts get-session-token --duration-seconds $SESSION_DURATION --serial-number $MFA_DEVICE_ARN --token-code $MFA_TOKEN but it will display on stdout which I don't want.
----
I know it will be hard for you guys to help as your need an mfa device and access to aws.
But I think I have found something that might work:
if [ ! -z "$MFA_AUTH" ]; then
I have it working but now I am down to the fine details and final touches.
I have a problem capturing the output of a command and testing the exit status.
Here is an excerpt of the code:
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
read -p "Please specify in seconds your session duration: " SESSION_DURATION
read -p "Please enter MFA token: " MFA_TOKEN
MFA_AUTH=$(aws sts get-session-token --duration-seconds $SESSION_DURATION --serial-number $MFA_DEVICE_ARN --token-code $MFA_TOKEN)
if [ $? -eq 0 ]; then
export AWS_ACCESS_KEY_ID=$(echo $MFA_AUTH | jq -r .Credentials.AccessKeyId)
export AWS_SECRET_ACCESS_KEY=$(echo $MFA_AUTH | jq -r .Credentials.SecretAccessKey)
export AWS_SESSION_TOKEN=$(echo $MFA_AUTH | jq -r .Credentials.SessionToken)
else
echo "MFA Authentication failure. Exiting"
return 1
fi
As you can see, for me to extract the keys I have to assign the command to a variable. That's fine. But If the user puts in the wrong MFA token currently I have no way of testing if the exit status of the previous command succeeded because it is assigned to a variable.
Any ideas?
I can test the exit status of: aws sts get-session-token --duration-seconds $SESSION_DURATION --serial-number $MFA_DEVICE_ARN --token-code $MFA_TOKEN but it will display on stdout which I don't want.
----
I know it will be hard for you guys to help as your need an mfa device and access to aws.
But I think I have found something that might work:
if [ ! -z "$MFA_AUTH" ]; then
Last edited: