Sub topic 9
Combining IAM policies and grants with key policies
This chapter assumes you can read IAM policy JSON and have seen a KMS key policy. It covers how the three access mechanisms combine and which mistakes cause AccessDeniedException.
The key policy comes first
Every KMS key has exactly one key policy. Unlike most AWS resources, a KMS key does not trust IAM by default. IAM policies in the same account affect a key only if the key policy allows that. The usual way is the statement from the previous chapter that grants kms:* to arn:aws:iam::111122223333:root. That statement does not give the root user access. It hands the decision to the account's IAM policies.
With that statement in place, a same-account principal needs an allow from either the key policy or an IAM policy. Without it, only the key policy counts. In both cases, an explicit deny in any policy overrides every allow.
IAM policies for key use
When the key policy delegates to IAM, you can manage access in IAM as you do for other services. The resource must be the key's ARN. An alias name does not work as a resource.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey"],
"Resource": "arn:aws:kms:us-east-1:111122223333:key/<key-id>"
}
]
}For cross-account use, both sides must allow the call. The key policy must name the other account or its principal. An IAM policy in the caller's account must also allow the action on the key's ARN.
Grants: temporary, programmatic access
A grant is a policy-like object attached to a key. It gives one grantee principal a limited set of operations. Grants suit access that changes often, such as a service acting for a user. You create a grant with CreateGrant, so the caller needs kms:CreateGrant through the key policy or IAM.
aws kms create-grant \
--key-id <key-id> \
--grantee-principal arn:aws:iam::111122223333:role/batch-role \
--operations Decrypt GenerateDataKey \
--constraints EncryptionContextSubset={purpose=demo}The constraint limits the grant to requests whose encryption context includes purpose=demo. The command returns a GrantId and a GrantToken. Grants can take a short time to propagate. If a call fails right after creation, pass the grant token in the GrantTokens parameter. End a grant with aws kms retire-grant (the grantee or a retiring principal) or aws kms revoke-grant (a key administrator).
Choosing a mechanism
| Mechanism | Best for | Changed by |
|---|---|---|
| Key policy | Baseline owners, admins, and users | Editing the policy |
| IAM policy | Many keys managed centrally | IAM administrators |
| Grant | Short-lived, narrow access | `CreateGrant` and `RetireGrant` |
Common errors
- IAM allow ignored. The key policy lacks the root-principal delegation statement, so the IAM policy has no effect.
- Alias in
Resource. IAM policy resources must be key ARNs. - Missing cross-account half. Only one account allows the call.
- Grant fails right after creation. Propagation is eventually consistent, so use the grant token.
- Grant never retired. Retire grants you no longer need, because each key has a grant quota.
Exercise
A role has an IAM policy allowing kms:Decrypt on a key ARN in the same account, but calls fail with AccessDeniedException. The key policy contains only a statement for a different role. What is the likely cause and the fix?
Show answer
The key policy does not delegate to IAM, so the IAM policy is ignored. Add a key policy statement allowing the account root principal (arn:aws:iam::111122223333:root) kms:*, or allow the role directly in the key policy.