Sub topic 8
Controlling access with key policies
This chapter assumes you know IAM users, roles, and policy documents (Effect, Principal, Action, Resource, Condition). You should also know how to create a KMS key and call Encrypt, Decrypt, and GenerateDataKey. Here you learn how a key policy decides who can use or manage a key, and which mistakes lock people out.
Why KMS keys need their own policy
Most AWS resources can be reached through IAM policies alone. A KMS key cannot. Every KMS key has exactly one key policy, a resource-based policy attached to the key. It is the primary way to control access to that key.
A key policy is required. If you create a key without supplying one, KMS attaches a default policy. If you supply one, KMS uses it as written, with one safety check: the policy must not leave the key impossible to manage.
The policy document uses the same JSON grammar as IAM policies. Two details differ from other resource policies:
- The
Resourceelement is always"*". It means "this key", because the policy is attached to a single key. - The
Actionvalues start withkms:, for examplekms:Decrypt.
How KMS evaluates a request
For most AWS services, an IAM policy in the caller's account is enough to allow access. KMS is stricter. A principal can use a key only if the key policy permits it, either directly or by delegating to IAM.
Delegation happens through a statement that names the account root as the principal:
{
"Sid": "EnableIAMPolicies",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "kms:*",
"Resource": "*"
}This does not grant access to the root user alone. It tells KMS to trust the account's IAM policies for this key. With the statement in place, an IAM policy that allows kms:Decrypt on the key's ARN works. Without it, IAM policies have no effect on the key, and only principals named in the key policy can use it.
An explicit Deny always wins, in either place. For cross-account access, both sides must agree. The key policy in the key's account must allow the external principal or account, and an IAM policy in the external account must allow the action on the key's ARN.
Reading a complete key policy
The policy below combines delegation to IAM with a narrow statement for an application role.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "EnableIAMPolicies",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:root" },
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "AllowAppToUseKey",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/app-role" },
"Action": ["kms:Encrypt", "kms:Decrypt", "kms:GenerateDataKey"],
"Resource": "*",
"Condition": {
"StringEquals": { "kms:EncryptionContext:purpose": "demo" }
}
}
]
}The first statement hands day-to-day permissions to IAM. The second lets app-role call three cryptographic operations, but only when the request carries the encryption context pair purpose=demo. A call to Decrypt with a different context, or none, does not match the statement. Because the first statement still delegates to IAM, an IAM policy could grant the role more access, so this condition is not a hard limit unless you also deny. The next sections show how to make a limit firm.
Separating administrators from users
A common pattern splits two duties:
- Key administrators create, describe, tag, enable, disable, and delete the key, and edit its policy.
- Key users run cryptographic operations such as
kms:Encrypt,kms:Decrypt, andkms:GenerateDataKey.
An administrator who cannot decrypt cannot read the protected data. A user who cannot edit the policy cannot widen their own access. Here is a policy with that split and no delegation to IAM:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowKeyAdministration",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/key-admin" },
"Action": [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:Disable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Get*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
"Resource": "*"
},
{
"Sid": "AllowKeyUse",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/app-role" },
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
],
"Resource": "*"
}
]
}Two cautions apply. Wildcards such as kms:Get* and kms:Put* are convenient but can include actions added later, so review them. Also, an administrator who can call kms:PutKeyPolicy can give themselves any permission. Treat that action as full control of the key.
Note that kms:Get* covers kms:GetKeyPolicy and kms:GetKeyRotationStatus, and kms:Put* covers kms:PutKeyPolicy.
Conditions that narrow access
Conditions are where key policies gain precision. These are the ones you will use most.
| Condition key | What it checks | Typical use |
|---|---|---|
| `kms:EncryptionContext:<name>` | A specific encryption context pair on the request | Bind a role to one purpose or tenant |
| `kms:EncryptionContextKeys` | Which context names are present | Require that a context is always supplied |
| `kms:ViaService` | The AWS service making the call for the caller, such as `s3.us-east-1.amazonaws.com` | Allow use only through S3, not direct API calls |
| `kms:CallerAccount` | The account of the caller | Limit cross-account use to one account |
| `kms:GrantIsForAWSResource` | Whether a grant is created by an integrated AWS service | Let a role create grants only through services |
The kms:EncryptionContext: keys apply only to operations that accept a context, and they use the same name and value you pass to the API. Use StringEquals for exact matches. For context keys that must exist, pair kms:EncryptionContextKeys with ForAllValues:StringEquals and also a Null check, because ForAllValues is true when the key is absent.
A policy that allows use only through S3 in one Region looks like this:
{
"Sid": "AllowUseOnlyViaS3",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111122223333:role/app-role" },
"Action": ["kms:Decrypt", "kms:GenerateDataKey"],
"Resource": "*",
"Condition": {
"StringEquals": {
"kms:ViaService": "s3.us-east-1.amazonaws.com",
"kms:CallerAccount": "111122223333"
}
}
}Making a restriction firm with Deny
An Allow with a condition only grants access under that condition. It does not stop another statement or an IAM policy from granting the same action. To enforce a rule, write a Deny with the inverse condition:
{
"Sid": "DenyDecryptWithoutContext",
"Effect": "Deny",
"NotPrincipal": { "AWS": "arn:aws:iam::111122223333:role/key-admin" },
"Action": "kms:Decrypt",
"Resource": "*",
"Condition": {
"StringNotEquals": { "kms:EncryptionContext:purpose": "demo" }
}
}Be careful with Deny. It overrides everything, including your own access. A careless deny can make a key unusable until you change the policy, and if the deny blocks kms:PutKeyPolicy, you may not be able to repair it.
If you do lose access, AWS Support can restore it only for keys in your own account where the policy still allows the account root. Plan so that you never need this.
Working with policies from the CLI
Use the key policy name default, the only policy name KMS supports:
aws kms get-key-policy \
--key-id alias/tutorial-key \
--policy-name default \
--query Policy \
--output text > policy.json
# Edit policy.json, then apply it
aws kms put-key-policy \
--key-id alias/tutorial-key \
--policy-name default \
--policy file://policy.jsonput-key-policy replaces the whole document. It does not merge. Always start from the output of get-key-policy, so you do not remove a statement by accident. For policy changes to a key you depend on, use aws kms get-key-policy and save a copy first.
Common errors
AccessDeniedExceptionalthough the IAM policy allows the action. The key policy does not delegate to IAM, and it does not name the principal. Add theEnableIAMPoliciesstatement or name the role.AccessDeniedExceptionon a key in another account. Only one side was configured. Add the principal to the key policy and an IAM allow to the caller.- Alias in an IAM policy does not match. IAM
Resourceentries must use the key ARN. The aliasalias/tutorial-keydoes not work as a resource there, though you can usekms:ResourceAliasesas a condition. MalformedPolicyDocumentException. A principal in the policy does not exist yet, or the JSON has a mistake. New IAM roles may need a few seconds to propagate.- Decrypt denied after adding a context condition. The caller did not pass the same encryption context, so the condition did not match. Check the exact key and value.
Exercise
A role has an IAM policy allowing kms:Decrypt on a key's ARN, but calls fail with AccessDeniedException. The key policy has one statement that allows a different role to use the key. What is missing, and what are two ways to fix it?
Show answer
The key policy neither names the role nor delegates to IAM, so the IAM policy has no effect on this key. Fix it either by adding a statement that names the role with the needed actions, or by adding a statement that allows the account root principal (arn:aws:iam::<account-id>:root) with kms:*, which makes the account's IAM policies effective for the key.
Exercise
You want the role app-role to decrypt only when the encryption context contains purpose=demo, even if an IAM policy grants it kms:Decrypt on all keys. Why is an Allow with a condition not enough, and what statement makes the rule firm?
Show answer
An Allow with a condition only grants access when the condition matches. It does not remove access granted by another policy statement. Add a Deny statement for kms:Decrypt with a StringNotEquals condition on kms:EncryptionContext:purpose set to demo, and exclude the key administrator role so you cannot lock yourself out.