A RequireMFA policy that locked out the people it protected
The usual deny-unless-MFA policy had a carve-out for enrolling a device, but it missed iam:TagMFADevice and the IAM console reads. Nobody in the group could enrol. Here is what broke, the two policy versions that fixed it, and how to test the policy before users hit it.
Every console user in our RequireMFA group was locked out on the same day. Not because they had skipped MFA. They tried to enrol, and AWS said no:
User: arn:aws:iam::111122223333:user/test-user is not authorized to perform:
iam:TagMFADevice on resource: arn:aws:iam::111122223333:mfa/test-user
with an explicit deny in an identity-based policy
The policy that was supposed to force MFA was stopping people from setting up MFA. In this blog we will look at the two mistakes in that policy, the versions that fixed it, and how I test this kind of policy now before a real user meets it.
The setup
The pattern is the one most of us copy from the AWS docs. One policy, attached to a group, with a big Deny on everything when aws:MultiFactorAuthPresent is false. Then a carve-out, a short list of actions a user can still do without MFA, so they can sign in with only a password and enrol a device.
The carve-out has to appear twice. Once in an Allow statement (so the user can do it at all) and once in the deny's NotAction (so the deny doesn't catch it). Our first version had the two lists written out separately. And they drifted.
If you set up a new account recently, I covered the root and Identity Center side of this in setting up a new AWS account safely. This post is about the IAM user case, which a lot of small teams still have.
Mistake 1: iam:TagMFADevice
When you add a virtual MFA device from the console, it creates the device and tags it in the same flow. So the console calls iam:TagMFADevice along with iam:CreateVirtualMFADevice. That action was in neither list. The Allow didn't grant it, and the deny caught it, so every enrolment attempt died with the explicit deny above.
Honestly this one is easy to miss. The older copies of the "manage your own MFA" policy floating around don't have it, and if you test from the CLI with create-virtual-mfa-device without --tags, you never call it.
The fix was to keep each list in one place, in Terraform locals, and build both statements from them. If the list is written once, it cannot drift:
locals {
mfa_setup_device_actions = [
"iam:CreateVirtualMFADevice",
"iam:DeleteVirtualMFADevice",
"iam:TagMFADevice",
]
# Lets the IAM console render its navigation before MFA.
iam_view_actions = [
"iam:Get*",
"iam:List*",
]
}
data "aws_iam_policy_document" "require_mfa" {
statement {
sid = "AllowManageOwnPasswordsAndMFA"
effect = "Allow"
actions = local.mfa_setup_self_actions
resources = ["arn:aws:iam::*:user/$${aws:username}"]
}
statement {
sid = "AllowManageOwnVirtualMFADevice"
effect = "Allow"
actions = local.mfa_setup_device_actions
resources = ["arn:aws:iam::*:mfa/*"]
}
statement {
sid = "DenyAllExceptListedIfNoMFA"
effect = "Deny"
not_actions = concat(
local.mfa_setup_account_actions,
local.mfa_setup_self_actions,
local.mfa_setup_device_actions,
local.iam_view_actions,
["sts:GetSessionToken"],
)
resources = ["*"]
condition {
test = "BoolIfExists"
variable = "aws:MultiFactorAuthPresent"
values = ["false"]
}
}
}
mfa_setup_self_actions holds the per-user calls: ChangePassword, EnableMFADevice, ResyncMFADevice, DeactivateMFADevice, GetUser, GetLoginProfile and the List* credential calls (ListAccessKeys, ListMFADevices, ListSSHPublicKeys, ListServiceSpecificCredentials, ListSigningCertificates). The security credentials page loads those List calls when it opens; without them the page errors out before showing the "Assign MFA device" button. mfa_setup_account_actions is the small account-wide set: GetAccountPasswordPolicy, GetAccountSummary, ListAccountAliases, ListVirtualMFADevices. The list of actions were longer than I expected; the full file is about 110 lines and passes terraform validate on Terraform 1.17.
Note the $${aws:username}. The double dollar escapes Terraform's own interpolation, so the literal ${aws:username} reaches IAM, which resolves it per caller.
DeleteVirtualMFADevice is in the list for a reason too. A half finished enrolment leaves the device behind, and when the user retries, the create collides with the name it already took. Without delete they are stuck again.
Mistake 2: the console hides its own navigation
Version 2 added TagMFADevice. People still couldn't enrol, but now for a different reason. They couldn't reach the page itself.
See, the IAM console needs account-wide List permissions to draw its left-hand navigation. Without iam:ListUsers and friends, a user with only a password lands in IAM and gets access denied banners everywhere. Our users could not click through to their own security credentials page at all, so the button they needed was never on screen.
Version 3 exempted iam:Get* and iam:List* from the deny, and we attached the AWS managed IAMReadOnlyAccess to the same group. This widens when reads apply, not what the group can read.
Is that a leak? A little. Someone holding only a password can now see the shape of the account: user names, groups, role names, policy documents. IAM never returns secret material from a Get or List call, so it's the layout only, and every write still needs MFA. I was fine with that trade. You may not be, in which case send people the direct security credentials URL and skip this step.
Policy versions as an undo button
We changed the live policy with aws iam create-policy-version --set-as-default, keeping the older versions. A managed policy keeps up to five versions, and moving the default back is one command:
POLICY_ARN="arn:aws:iam::111122223333:policy/RequireMFA"
aws iam list-policy-versions --policy-arn "$POLICY_ARN" \
--query 'Versions[].[VersionId,IsDefaultVersion,CreateDate]' --output table
# roll back if the new version breaks something
aws iam set-default-policy-version --policy-arn "$POLICY_ARN" --version-id v2
When you are editing a deny that sits on every human in the account, having that one-liner ready is calming. We didn't need it.
Since the fix went to the account by CLI first, the HCL had to match exactly what was live. I checked by rendering the Terraform policy JSON and diffing it against aws iam get-policy-version --version-id v3. It matched, so a later import gives a no-op plan instead of a surprise.
Two smaller things in the same policy
BoolIfExists, not Bool. For a caller with no MFA context at all (a plain access key, for example), the aws:MultiFactorAuthPresent key is absent. Plain Bool doesn't match a missing key, so the deny silently does nothing for exactly the callers you worry about.
Also, keep programmatic users out of this group. A CI user or a Terraform user has no device to enrol and no way to get an MFA session, so membership disables its access key quietly. The CLI user that runs our Terraform is deliberately not a member, and there is a comment above it saying why.
Testing before a human finds it
The IAM policy simulator can evaluate a user's policies with a context key set. This asks: with MFA absent, which of these actions are allowed?
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:user/test-user \
--action-names iam:CreateVirtualMFADevice iam:TagMFADevice iam:EnableMFADevice \
iam:ListUsers s3:ListAllMyBuckets \
--context-entries "ContextKeyName=aws:MultiFactorAuthPresent,ContextKeyValues=false,ContextKeyType=boolean" \
--query 'EvaluationResults[].[EvalActionName,EvalDecision]' --output table
You want allowed for the three MFA actions and ListUsers, and explicitDeny for the S3 call. Had we run this on day one, TagMFADevice would have shown explicitDeny and the lock-out never happens.
The simulator won't tell you which calls the console makes, though. For that, the honest test is a throwaway IAM user in the group, a private browser window, and a phone with an authenticator app. Ten minutes. I'd do that before adding anyone real.
How it ended
After version 3, everyone enrolled. One old user had a console password with no recorded use, so we deleted that login profile instead of enrolling it; one less password to look after. That console user was the same personal admin user whose key we pulled out of containers in getting an admin access key out of running containers and onto ECS task roles.
The credential report is the final check, and it is cheap to run each month:
aws iam generate-credential-report >/dev/null
sleep 5
aws iam get-credential-report --query Content --output text | base64 --decode \
| awk -F, 'NR==1 || ($4=="true" && $8=="false") {print $1, $4, $8}'
Column 4 is password_enabled and column 8 is mfa_active. Only the header line should print. If a name shows up under it, that person can sign in with a password and has no MFA, so go and do the needful.