Sub topic 8
Controlling access with key policies
This chapter assumes you know IAM users, roles, and policy JSON. It also assumes you have created a customer managed key, as in the earlier chapters. Here you will learn how a key policy decides who can use or manage a KMS key.
Why every key has a policy
A key policy is a resource policy attached to one KMS key. Every KMS key has exactly one. Most AWS resources can be reached through IAM policies alone. KMS is different, because the key policy is the primary way to control access to a key.
If the key policy does not allow access, no IAM policy can add it. The one exception is a key policy statement that delegates control to IAM, as shown below.
If you create a key with the CLI and give no policy, KMS attaches a default policy. That default has one statement. It allows the account root principal to perform kms:*.
Enabling IAM policies
The first statement in the example policy uses the account root as the principal. This does not give access to the root user alone. It tells KMS to also evaluate IAM policies in that account for this key.
Without this statement, only principals named in the key policy can use the key. Your IAM policies would have no effect on it.
Reading the example policy
The second statement names one role, app-role, and allows three actions: kms:Encrypt, kms:Decrypt, and kms:GenerateDataKey. These are the calls from the earlier chapters on direct encryption and envelope encryption.
The Condition block limits those actions to requests where the encryption context contains purpose equal to demo. A call from app-role with a different context, or none, is denied.
{
"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" }
}
}
]
}In a key policy, Resource is always "*". It means the key the policy is attached to.
How a request is evaluated
Separating administrators from users
A common pattern uses two kinds of statements. Key administrators get management actions such as kms:PutKeyPolicy, kms:ScheduleKeyDeletion, and kms:EnableKeyRotation. Key users get cryptographic actions such as kms:Encrypt and kms:Decrypt. Giving an administrator management rights does not let them decrypt data.
Common errors
- Locking yourself out. If you replace the key policy and remove every principal that can call
kms:PutKeyPolicy, you cannot fix it yourself. You would need AWS Support. Always keep the root statement or an administrator role. - Wrong account ID or role ARN. KMS rejects a policy whose principal does not exist, with a
MalformedPolicyDocumentException. - Forgetting the delegation statement. The IAM policy looks correct, yet calls return
AccessDeniedException.
Exercise
An IAM role has an IAM policy allowing kms:Decrypt on a key. The key policy has no statement for the role and no statement for the account root. Can the role decrypt with this key? Why?
Show answer
No. The key policy does not name the role, and it does not delegate to IAM through the account root statement. KMS therefore never considers the IAM policy, and the call is denied.