KMS key policy, IAM and grants
How KMS authorizes a request. Key policy, IAM policy and grants, the root principal statement, condition keys and encryption context.
Exam tasks: 5.2.1 (choose KMS and server- or client-side encryption), 5.3.5 (create and manage KMS keys across one or many Regions)
The decision: who is allowed to use and administer this key, and through which service?
Every KMS key has exactly one key policy. It is the only policy that is always required. IAM policies and grants work only on top of it.
Key policy vs IAM policy vs grant
| Key policy | IAM policy | Grant | |
|---|---|---|---|
| Required | Yes, one per key | No | No |
| Works alone | Yes | Only if the key policy allows the account or the principal | Yes |
| Can deny | Yes | Yes | No, allow only |
| Scope | One key | Many keys, by ARN | One key |
| Typical use | Baseline: admins, users, cross-account | Day-to-day delegation inside the account | Temporary or programmatic access, AWS services |
| Revoke | Edit policy | Edit policy | RetireGrant or RevokeGrant |
- A key policy does not trust the account by default. The statement
"Principal": {"AWS": "arn:aws:iam::<acct>:root"}withkms:*is what lets IAM policies work. Therootthere means the account and its IAM principals, not the root user alone. - The default console key policy adds key administrators (manage, no crypto use) and key users (crypto use plus grants for AWS resources). Administrators can still change the key policy or create grants, so they can grant themselves use.
- An explicit deny in any policy wins.
{
"Sid": "LetAccountIamDecide",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::444455556666:root" },
"Action": "kms:*",
"Resource": "*"
}- The statement above does not grant access to everyone in the account. It lets IAM policies in that account grant access. Remove it and only principals named in the key policy can use the key.
- Split duties in the policy. Key administrators get
kms:Create*,kms:Put*,kms:ScheduleKeyDeletionandkms:EnableKeyRotation. Key users getkms:Encrypt,kms:Decrypt,kms:GenerateDataKey*andkms:ReEncrypt*. Administrators usually should not be able to decrypt. - The policy lockout safety check refuses a policy that would stop the caller from managing the key. If you bypass it and lock yourself out, only AWS Support can help, and only if the account root still has access.
- IAM Access Analyzer reports key policies that grant access outside your organization.
Grants
- Created with
CreateGrant, which returns a grant token and a grant ID. A grant has a grantee, an optional retiring principal, an operation list and optional constraints. - A grant is eventually consistent. Pass the grant token to use it immediately.
- Constraints narrow a grant by encryption context or by
SourceArn. - Limit is 50,000 grants per key.
- AWS services such as EBS and Redshift create grants for you. The key user needs
kms:CreateGrantwithkms:GrantIsForAWSResourcetrue. - The retiring principal calls
RetireGrant. A key administrator callsRevokeGrant. - Services use grants behind the scenes. EBS creates one when a volume attaches, RDS when an instance is created, and Secrets Manager and Lambda for their own use.
ListGrantsshows who holds grants. Check it after a compromise, because a grant can outlive a deleted IAM policy.
Cross-account use
- The key policy in the key account allows the external account or principal.
- An IAM policy in the external account allows the KMS actions on the key ARN.
- The caller uses the key ARN or alias ARN, never a bare alias name.
The other account cannot see the key in its console and cannot manage it. A common pattern is a central security account that owns customer managed keys per data domain, while workload accounts get use-only permissions combined with kms:ViaService.
Useful condition keys
| Condition | What it restricts | Example use |
|---|---|---|
kms:ViaService | Only requests that arrive through a named service | The analytics role can decrypt only via s3.eu-west-1.amazonaws.com, never by calling KMS directly |
kms:EncryptionContext:<key> | Only requests with a specific context value | Tenant acme can decrypt only data encrypted with tenant=acme |
kms:EncryptionContextKeys | Which context keys must be present | Require that every call carries a project key |
kms:CallerAccount | Which account's principals | Allow all principals in one account, used with kms:ViaService for AWS managed-style keys |
kms:GrantIsForAWSResource | Grants only when an integrated service creates them | Let EBS or RDS create grants for your role without letting the role hand out grants itself |
kms:RotationPeriodInDays | Allowed rotation period values | Force a 180-day rotation period across the organization |
kms:RequestAlias, kms:ResourceAliases | Access based on aliases | ABAC: roles may use any key aliased alias/finance-* |
ViaService and the direct call
A role that can call kms:Decrypt directly can pull the data key for any ciphertext it holds, bypassing the
bucket policy. When the requirement is "only through S3" or "only through Secrets Manager", add kms:ViaService.
An IAM policy that only restricts s3:* leaves the KMS path open.
Encryption context
- Optional non-secret name-value pairs bound to the ciphertext as additional authenticated data. Symmetric keys only.
- Decrypt must supply an exact, case-sensitive match or it fails with
InvalidCiphertextException. - Appears in plaintext in CloudTrail. Never put secrets in it.
- Use it for policy conditions and to stop ciphertext being replayed in the wrong place (confused deputy).
- Key policies and grants can condition on it.
Exam signal
"Grant temporary access programmatically and revoke it later" means grants. "Key only usable from S3 in one Region" means kms:ViaService. "Prevent an administrator from decrypting" means separate administrator and user statements.
Exam trap
An IAM policy that allows kms:Decrypt does nothing if the key policy lacks the account root statement and does not name the principal. Giving a key a policy that names one deleted user can leave it unmanageable.
Scenarios
kms:ViaService makes the key usable by that role only when S3 calls KMS on its behalf, so direct Decrypt calls
fail. Rotation changes future material but doesn't limit who can decrypt. SSE-S3 removes the separate KMS
permission entirely, making the problem worse. Object ACL permissions are unrelated to key use.
Related
Further reading
AWS KMS keys and envelope encryption
KMS key types, ownership models, envelope encryption, data key APIs, aliases, multi-Region keys and deletion. The base of almost every encryption question.
KMS rotation and imported key material
Automatic, on-demand and manual rotation, rotation periods, and bring-your-own-key material compared with KMS-generated material.