Skip to content

Instantly share code, notes, and snippets.

@joelthompson
Last active May 7, 2022 21:13
Show Gist options
  • Save joelthompson/c3bed45b520c7de318569fa2e308e2e3 to your computer and use it in GitHub Desktop.
Save joelthompson/c3bed45b520c7de318569fa2e308e2e3 to your computer and use it in GitHub Desktop.
#!/bin/bash
MFA_ARN=$(aws sts get-caller-identity --query "Arn" --output text | sed -e "s#:user/#:mfa/#")
read -p "Enter MFA Code: " MFA_CODE
read -a CREDS <<< $(aws sts get-session-token --serial-number $MFA_ARN --token-code $MFA_CODE --output text)
export AWS_ACCESS_KEY_ID="${CREDS[1]}"
export AWS_SECRET_ACCESS_KEY="${CREDS[3]}"
export AWS_SECURITY_TOKEN="${CREDS[4]}"
export AWS_SESSION_TOKEN="${CREDS[4]}"
echo "Credentials expiration: ${CREDS[2]}"
[account1]
aws_access_key_id = AKIAblahblahblah
aws_secret_access_key = some_secret_keyy
[account2]
aws_access_key_id = AKIAblahblahblah2
aws_secret_access_key = some_other_secret_key
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAllMfa",
"Effect": "Allow",
"Action": "*",
"Resource": [
"*"
],
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
}
}
},
{
"Sid": "DenyAllNonMfa",
"Effect": "Deny",
"Action": "*",
"Resource": [
"*"
],
"Condition": {
"BoolIfExists": {
"aws:MultiFactorAuthPresent": "false"
}
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment