Skip to content

Instantly share code, notes, and snippets.

@mitio
Last active October 22, 2019 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitio/3cb49109d2ad7a99d1022da141580816 to your computer and use it in GitHub Desktop.
Save mitio/3cb49109d2ad7a99d1022da141580816 to your computer and use it in GitHub Desktop.
Easily authenticate against AWS for CLI or API access when MFA is activated and forced
aws-auth() {
aws_profile="$1"
if [ -z "$aws_profile" ]; then
echo "Usage: $0 <aws-profile>" >&2
return 1
fi
echo "Obtaining MFA ARN for profile '$aws_profile'..."
if ! response=`aws --profile "$aws_profile" iam list-mfa-devices` || \
! mfa_arn=`echo "$response" | jq -re '.MFADevices[0].SerialNumber'`; then
echo "Failed obtaining the MFA ARN for profile '$aws_profile'." >&2
return 2
fi
echo -n "Enter MFA token for $mfa_arn: "
read mfa_token
if ! response=`aws --profile "$aws_profile" sts get-session-token --serial-number "$mfa_arn" --token-code "$mfa_token"`; then
echo "Failed obtaining a session token." >&2
return 3
fi
if ! secret_access_key=`echo $response | jq -re '.Credentials.SecretAccessKey'` || \
! session_token=`echo $response | jq -re '.Credentials.SessionToken'` || \
! access_key_id=`echo $response | jq -re '.Credentials.AccessKeyId'` || \
! expiration=`echo $response | jq -re '.Credentials.Expiration'`; then
echo "Failed obtaining credentials. Raw response was: $response" >&2
return 4
fi
export AWS_PROFILE="$aws_profile"
export AWS_SECRET_ACCESS_KEY="$secret_access_key"
export AWS_ACCESS_KEY_ID="$access_key_id"
export AWS_SESSION_TOKEN="$session_token"
echo "AWS auth ENV vars set for access key ID $access_key_id. Credentials are valid until: $expiration"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment