|
1 | 1 | import {Stack, CfnResource} from "aws-cdk-lib" |
2 | 2 | import {IConstruct} from "constructs" |
3 | 3 |
|
4 | | -/** |
5 | | - * Adds cfn-guard metadata to suppress rules on a resource. |
6 | | - */ |
7 | | -export const addCfnGuardMetadata = ( |
8 | | - stack: Stack, |
9 | | - path: string, |
10 | | - childPath?: string, |
11 | | - suppressedRules: Array<string> = [] |
12 | | -) => { |
13 | | - console.log(`🔍 Looking for construct at path: ${path}${childPath ? "/" + childPath : ""}`) |
14 | | - |
15 | | - const parent = stack.node.tryFindChild(path) |
16 | | - if (!parent) { |
17 | | - console.warn(`❌ Could not find path /${stack.stackName}/${path}`) |
18 | | - // List available children for debugging |
19 | | - console.log("Available children:", stack.node.children.map(c => c.node.id)) |
20 | | - return |
21 | | - } |
22 | | - |
23 | | - let target: IConstruct |
24 | | - |
25 | | - if (childPath) { |
26 | | - const child = parent.node.tryFindChild(childPath) |
27 | | - if (!child) { |
28 | | - console.warn(`❌ Could not find path /${stack.stackName}/${path}/${childPath}`) |
29 | | - // List available children for debugging |
30 | | - console.log("Available children of parent:", parent.node.children.map(c => c.node.id)) |
31 | | - return |
| 4 | +const findResourcesByPattern = (construct: IConstruct, patterns: Array<string>): Array<CfnResource> => { |
| 5 | + const matches: Array<CfnResource> = [] |
| 6 | + const seen = new Set<string>() |
| 7 | + |
| 8 | + const search = (node: IConstruct): void => { |
| 9 | + if (node instanceof CfnResource) { |
| 10 | + for (const pattern of patterns) { |
| 11 | + if (node.logicalId.includes(pattern) && !seen.has(node.logicalId)) { |
| 12 | + matches.push(node) |
| 13 | + seen.add(node.logicalId) |
| 14 | + break |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + for (const child of node.node.children) { |
| 19 | + search(child) |
32 | 20 | } |
33 | | - target = child |
34 | | - } else { |
35 | | - target = parent |
36 | 21 | } |
37 | 22 |
|
38 | | - let cfnResource: CfnResource | undefined |
| 23 | + search(construct) |
| 24 | + return matches |
| 25 | +} |
39 | 26 |
|
40 | | - if (target instanceof CfnResource) { |
41 | | - cfnResource = target |
42 | | - } else if ("defaultChild" in target.node && target.node.defaultChild) { |
43 | | - const defaultChild = target.node.defaultChild |
44 | | - if (defaultChild instanceof CfnResource) { |
45 | | - cfnResource = defaultChild |
| 27 | +const addSuppressions = (resources: Array<CfnResource>, rules: Array<string>): void => { |
| 28 | + resources.forEach(resource => { |
| 29 | + if (!resource.cfnOptions.metadata) { |
| 30 | + resource.cfnOptions.metadata = {} |
46 | 31 | } |
47 | | - } |
48 | 32 |
|
49 | | - if (!cfnResource) { |
50 | | - console.warn(`⚠️ Target at ${path}${childPath ? "/" + childPath : ""} is not a CfnResource`) |
51 | | - console.log(`Target type: ${target.constructor.name}`) |
52 | | - if ("defaultChild" in target.node && target.node.defaultChild) { |
53 | | - console.log(`Default child type: ${target.node.defaultChild.constructor.name}`) |
54 | | - } |
55 | | - return |
56 | | - } |
| 33 | + const existing = resource.cfnOptions.metadata.guard?.SuppressedRules || [] |
| 34 | + const combined = [...new Set([...existing, ...rules])] |
57 | 35 |
|
58 | | - // Initialize metadata if it doesn't exist |
59 | | - if (!cfnResource.cfnOptions.metadata) { |
60 | | - cfnResource.cfnOptions.metadata = {} |
61 | | - } |
| 36 | + resource.cfnOptions.metadata.guard = {SuppressedRules: combined} |
| 37 | + }) |
| 38 | +} |
62 | 39 |
|
63 | | - // Preserve existing guard metadata and merge with new rules |
64 | | - const existingGuard = cfnResource.cfnOptions.metadata.guard || {} |
65 | | - const existingSuppressed = existingGuard.SuppressedRules || [] |
66 | | - const allSuppressedRules = [...new Set([...existingSuppressed, ...suppressedRules])] |
| 40 | +export const applyCfnGuardSuppressions = (stack: Stack): void => { |
| 41 | + // Lambda suppressions |
| 42 | + const lambdaResources = findResourcesByPattern(stack, [ |
| 43 | + "Handler", "Function", "CreateIndex", "SlackBot", "CustomResourceProvider" |
| 44 | + ]) |
| 45 | + addSuppressions(lambdaResources, ["LAMBDA_DLQ_CHECK", "LAMBDA_INSIDE_VPC", "LAMBDA_CONCURRENCY_CHECK"]) |
67 | 46 |
|
68 | | - cfnResource.cfnOptions.metadata = { |
69 | | - ...cfnResource.cfnOptions.metadata, |
70 | | - guard: { |
71 | | - SuppressedRules: allSuppressedRules |
72 | | - } |
73 | | - } |
| 47 | + // S3 bucket suppressions |
| 48 | + const bucketResources = findResourcesByPattern(stack, ["Bucket", "Docs", "Storage"]) |
| 49 | + addSuppressions(bucketResources, ["S3_BUCKET_REPLICATION_ENABLED", "S3_BUCKET_LOGGING_ENABLED"]) |
| 50 | + |
| 51 | + // S3 policy suppressions |
| 52 | + const policyResources = findResourcesByPattern(stack, ["Policy", "BucketPolicy"]) |
| 53 | + addSuppressions(policyResources, ["S3_BUCKET_SSL_REQUESTS_ONLY"]) |
74 | 54 |
|
75 | | - console.log(`✅ Suppressed rules for ${cfnResource.logicalId}: [${allSuppressedRules.join(", ")}]`) |
| 55 | + // API Gateway suppressions |
| 56 | + const stageResources = findResourcesByPattern(stack, ["Stage", "DeploymentStage"]) |
| 57 | + addSuppressions(stageResources, ["API_GW_CACHE_ENABLED_AND_ENCRYPTED"]) |
76 | 58 | } |
0 commit comments