Sub topic 9
Combining IAM policies and grants with key policies
This chapter assumes you know what a KMS key is, have read a key policy like the one in the previous chapter, and understand IAM users, roles, and identity-based policies. It covers how IAM policies and grants combine with the key policy to decide whether a request to a KMS key succeeds.
KMS has three authorization paths
Most AWS resources are governed by IAM policies. A KMS key is different: the key policy is always the starting point. A request is allowed only if one of these paths permits it and nothing denies it.
- The key policy names the caller directly in an
Allowstatement. - The key policy delegates to IAM, and an IAM policy attached to the caller allows the action.
- A grant, created on the key, gives the caller specific permissions.
An explicit Deny in any policy always wins.
| Mechanism | Where it lives | Typical use | Lifetime |
|---|---|---|---|
| Key policy | On the key | Baseline rules, cross-account access | Until edited |
| IAM policy | On the user or role | Day-to-day permissions for your own principals | Until edited |
| Grant | On the key | Temporary, programmatic access, often for AWS services | Until retired or revoked |
Delegating to IAM
A key policy does not trust IAM by default. In a key you create with the API or CLI and no policy, KMS applies a default policy that includes this statement:
{
"Sid": "EnableIAMPolicies",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "kms:*",
"Resource": "*"
}The principal is the account root, but this does not give the root user special power. It means "this account's IAM policies may grant access to this key." Without this statement, IAM policies in the account have no effect on the key. Only principals named in the key policy can use it.
This is the most common cause of confusing AccessDeniedException errors. A role has kms:Decrypt in its IAM policy, yet the call fails because the key policy never enabled IAM.
Writing the IAM side
With IAM enabled on the key, you grant permissions in the role's policy. Name the key by ARN, not by alias, in the Resource element.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey"
],
"Resource": "arn:aws:kms:us-east-1:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
}
]
}The Resource must be the key ARN. An alias ARN in Resource does not match the key a request resolves to. To restrict by alias, use the condition key kms:ResourceAliases instead.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["kms:Encrypt", "kms:Decrypt"],
"Resource": "*",
"Condition": {
"ForAnyValue:StringEquals": {
"kms:ResourceAliases": "alias/tutorial-key"
}
}
}
]
}Cross-account access needs both sides
Within one account, the key policy or an IAM policy is enough. Across accounts, both accounts must agree:
- The key policy in the key's account must allow the external account or principal.
- An IAM policy in the external account must allow the caller to use the key, using the key's full ARN.
Allowing the external account's root in the key policy only delegates to that account's administrators. Those administrators still must attach IAM policies to their principals. Aliases do not work for cross-account calls, so callers must pass the key ARN.
Grants
A grant is a policy instrument attached to a key. It gives a grantee principal a defined set of operations, optionally narrowed by constraints. You create one with CreateGrant.
aws kms create-grant \
--key-id <key-id> \
--grantee-principal arn:aws:iam::111122223333:role/app-role \
--operations Decrypt GenerateDataKey \
--constraints EncryptionContextSubset={purpose=demo} \
--retiring-principal arn:aws:iam::111122223333:role/admin-roleThe response contains a GrantId and a GrantToken.
Grants differ from policies in useful ways:
- You create and remove them with API calls, so applications can manage access at runtime without editing a policy document.
- They are allow-only. A grant cannot contain a deny.
- They support only a fixed list of operations, such as
Encrypt,Decrypt,GenerateDataKey,ReEncryptFrom,ReEncryptTo,DescribeKey, andCreateGrant. - Their constraints use
EncryptionContextSubsetorEncryptionContextEqualsto bind them to an encryption context.
AWS services use grants heavily. When you attach an encrypted EBS volume to an instance, EBS creates a grant so the instance can decrypt the volume's data key. You never wrote a policy for it.
Eventual consistency and grant tokens
A new grant can take a few minutes to take effect everywhere. If you must use it immediately, pass the grant token returned by CreateGrant in the --grant-tokens parameter of the request.
aws kms decrypt \
--ciphertext-blob fileb://secret.enc \
--encryption-context purpose=demo \
--grant-tokens <grant-token> \
--query Plaintext \
--output text | base64 --decodeRetiring and revoking grants
Grants stay on the key until removed, so clean them up.
- Retire (
retire-grant) is done by the grantee, the retiring principal, or an account principal permitted to do so. It is meant for the end of normal use. - Revoke (
revoke-grant) is done by an administrator withkms:RevokeGrant. It forcibly ends access.
aws kms list-grants --key-id <key-id>
aws kms revoke-grant --key-id <key-id> --grant-id <grant-id>Because grants are separate from the key policy, reading only the policy does not show who can use the key. Always run list-grants when auditing access.
Giving a role control without full admin rights
A common pattern separates key administrators from key users. The key policy lists administrators who manage the key but cannot use it for cryptography, and users who can encrypt and decrypt but cannot change the policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AdminsManageKey",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/key-admin" },
"Action": [
"kms:Describe*",
"kms:Put*",
"kms:Create*",
"kms:Update*",
"kms:Enable*",
"kms:Disable*",
"kms:List*",
"kms:Revoke*",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
"Resource": "*"
},
{
"Sid": "UsersUseKey",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/app-role" },
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey",
"kms:DescribeKey"
],
"Resource": "*"
}
]
}This policy omits the root delegation statement. That makes the key policy the only authority, so IAM policies cannot add access. It is stricter, but a mistake can lock everyone out, because only the principals named here can fix the policy. If none of them exist anymore, you must contact AWS Support.
Administrators who hold kms:Create* or kms:Put* can still grant themselves access by changing the policy or creating a grant. Separation of duties in KMS limits accidents, but it does not stop a determined administrator.
Troubleshooting checklist
When a call is denied, check these in order:
- Does the key policy either name the caller or contain the root delegation statement?
- If you rely on IAM, does the caller's policy allow the action on the full key ARN?
- Is there an explicit
Denyin the key policy, an IAM policy, a permissions boundary, a service control policy, or a VPC endpoint policy? - For a cross-account call, are both sides allowed, and is the caller using a key ARN rather than an alias?
- Does a condition such as
kms:EncryptionContext:purposematch what the caller sent? - If you depend on a grant that was just created, did you pass the grant token?
The error message and the CloudTrail event for the failed call (covered in a later chapter) usually point at the layer that denied it.
Exercise
A role has an IAM policy allowing kms:Decrypt on a key's ARN. The key policy contains only a statement that lets the key-admin role manage the key, with no root delegation statement. The role calls Decrypt and receives AccessDeniedException. Give two different changes that would fix this, and say which one you would use if the role should get access only for a short time.
Show answer
The IAM policy has no effect because the key policy does not enable IAM. Fix 1: add the EnableIAMPolicies statement (principal arn:aws:iam::<account>:root, action kms:*) to the key policy so the IAM policy takes effect. Fix 2: add an Allow statement in the key policy that names the role directly with kms:Decrypt, or create a grant for the role with the Decrypt operation. For short-term access, use a grant, because you can create it on demand and then retire or revoke it without editing the key policy.
Exercise
Why does listing a key's policy not tell you everything about who can use the key, and what command fills the gap?
Show answer
Access can also come from IAM policies, when the key policy enables IAM, and from grants, which are stored separately from the policy. Run aws kms list-grants --key-id <key-id> to see grants, and review IAM policies that reference the key to see the rest.