KMS Key Policy Allows Public Access
kms-key-policy-public
What this rule checks
Detects KMS keys whose key policy grants access to a wildcard or public principal.
How to fix it
- 1Remove wildcard/public principals from the key policy
- 2Scope key access to specific IAM principals or accounts
import { Key } from 'aws-cdk-lib/aws-kms';
import { PolicyStatement, Effect, AnyPrincipal } from 'aws-cdk-lib/aws-iam';
const key = new Key(this, 'DataKey');
key.addToResourcePolicy(new PolicyStatement({
effect: Effect.ALLOW,
principals: [new AnyPrincipal()],
actions: ['kms:Decrypt', 'kms:GenerateDataKey'],
resources: ['*'],
}));import { Key } from 'aws-cdk-lib/aws-kms';
import { Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
const role = new Role(this, 'AppServiceRole', { assumedBy: new ServicePrincipal('lambda.amazonaws.com') });
const key = new Key(this, 'DataKey', { enableKeyRotation: true });
key.grantDecrypt(role);CDK Insights pinpoints the exact file and line in your CDK source for every finding, so you can jump straight to the fix.
Affected resource types
AWS::KMS::KeyIntentional? Suppress this finding
Sometimes a flag is deliberate โ a genuinely public endpoint, say. You can dismiss kms-key-policy-public and the reason is kept in the report, not silently hidden.
In .cdk-insights.json:
{
"ignoreRules": [
{ "id": "kms-key-policy-public", "reason": "Why this is intentional" }
]
}Or inline in your CDK code:
Validations.of(scope).acknowledge({
id: 'cdk-insights::kms-key-policy-public',
reason: 'Why this is intentional',
});Use the rule ID kms-key-policy-public shown above โ not the CDK-* ID from SARIF / GitHub code scanning. To dismiss every finding on one construct instead, use ignorePaths. Suppression docs โ
Catch this in your stack
$ npx cdk-insights scanCDK Insights runs this and 126+ other rules locally against your synthesised CDK app โ free, no account, your code never leaves your machine.