Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 1x 1x 1x 1x 1x 1x 1x 1x | /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /* eslint-disable @typescript-eslint/naming-convention */ import { Construct } from "constructs"; import { IAspect, CfnResource } from "aws-cdk-lib"; const METADATA_TYPE = "cfn_nag"; const SUPRESSION_KEY = "rules_to_suppress"; export interface CfnNagRuleSuppression { id: string; reason: string; } /** * Adds cfn nag suppressions to the given construct */ export const addCfnNagSuppressionMeta = ( construct: CfnResource, rulesToSuppress: CfnNagRuleSuppression[] ): void => { construct.cfnOptions.metadata = { ...construct.cfnOptions.metadata, [METADATA_TYPE]: { ...construct.cfnOptions.metadata?.cfn_nag, [SUPRESSION_KEY]: [ ...(construct.cfnOptions.metadata?.cfn_nag?.rules_to_suppress || []), ...rulesToSuppress, ], }, }; }; export const addCfnNagSuppression = ( construct: Construct, rulesToSuppress: CfnNagRuleSuppression[], resourceName?: string ): void => { const child = resourceName ? construct.node.findChild(resourceName) : construct; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (child) { addCfnNagSuppressionMeta( child.node.defaultChild as CfnResource, rulesToSuppress ); } }; export class CfnNagCustomResourceSuppressionAspect implements IAspect { public visit(construct: Construct): void { if ( construct.node.path.endsWith( "/Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler" ) || construct.node.path.endsWith("Provider/framework-onEvent/Resource") || construct.node.path.match( /\/Custom::CDKBucketDeployment[0-9A-Za-z]+\/Resource$/ ) || construct.node.path.match(/\/LogRetention[0-9A-Za-z]+\/Resource$/) ) { // Enabling auto delete objects on an s3 bucket creates a lambda, which is created via CfnResource addCfnNagSuppressionMeta(construct as CfnResource, [ { id: "W58", reason: "Lambda already has the required permission to write CloudWatch Logs via arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole.", }, { id: "W89", reason: "Custom resource lambda functions is not necessary to be deployed in a VPC", }, { id: "W92", reason: "Custom resource lambda function is created by CDK and CloudFormation", }, ]); } } } export class CfnNagServiceRoleDefaultPolicyResourceSuppressionAspect implements IAspect { public visit(construct: Construct): void { if (construct.node.path.endsWith("/ServiceRole/DefaultPolicy/Resource")) { addCfnNagSuppressionMeta(construct as CfnResource, [ { id: "W12", reason: "The policy is automatically generated by CDK.", }, ]); } } } export class CfnNagResourcePathEndingWithSuppressionAspect implements IAspect { public constructor( private readonly resourcePathEndingWith: string, private readonly rulesToSuppress: CfnNagRuleSuppression[] ) {} public visit(construct: Construct): void { if (construct.node.path.endsWith(this.resourcePathEndingWith)) { addCfnNagSuppressionMeta(construct as CfnResource, this.rulesToSuppress); } } } export class CfnNagResourcePathRegexSuppressionAspect implements IAspect { public constructor( private readonly resourcePathEndingWith: RegExp, private readonly rulesToSuppress: CfnNagRuleSuppression[] ) {} public visit(construct: Construct): void { if (construct.node.path.match(this.resourcePathEndingWith)) { addCfnNagSuppressionMeta(construct as CfnResource, this.rulesToSuppress); } } } |