11import { Stack , CfnResource } from "aws-cdk-lib"
22import { IConstruct } from "constructs"
33
4+ /**
5+ * Find all CfnResources whose logicalId matches any provided pattern.
6+ */
47const findResourcesByPattern = ( construct : IConstruct , patterns : Array < string > ) : Array < CfnResource > => {
58 const matches : Array < CfnResource > = [ ]
69 const seen = new Set < string > ( )
7-
810 const search = ( node : IConstruct ) : void => {
911 if ( node instanceof CfnResource ) {
1012 for ( const pattern of patterns ) {
@@ -19,40 +21,58 @@ const findResourcesByPattern = (construct: IConstruct, patterns: Array<string>):
1921 search ( child )
2022 }
2123 }
24+ search ( construct )
25+ return matches
26+ }
2227
28+ /**
29+ * Find all CfnResources of a specific CloudFormation type.
30+ */
31+ const findResourcesByType = ( construct : IConstruct , type : string ) : Array < CfnResource > => {
32+ const matches : Array < CfnResource > = [ ]
33+ const search = ( node : IConstruct ) : void => {
34+ if ( node instanceof CfnResource && node . cfnResourceType === type ) {
35+ matches . push ( node )
36+ }
37+ for ( const child of node . node . children ) {
38+ search ( child )
39+ }
40+ }
2341 search ( construct )
2442 return matches
2543}
2644
45+ /**
46+ * Add/merge cfn-guard suppressions to resources for the given rules.
47+ */
2748const addSuppressions = ( resources : Array < CfnResource > , rules : Array < string > ) : void => {
2849 resources . forEach ( resource => {
2950 if ( ! resource . cfnOptions . metadata ) {
3051 resource . cfnOptions . metadata = { }
3152 }
32-
3353 const existing = resource . cfnOptions . metadata . guard ?. SuppressedRules || [ ]
3454 const combined = [ ...new Set ( [ ...existing , ...rules ] ) ]
35-
3655 resource . cfnOptions . metadata . guard = { SuppressedRules : combined }
3756 } )
3857}
3958
59+ /**
60+ * Apply cfn-guard suppressions for Lambda, S3, and API Gateway resources.
61+ */
4062export 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" ] )
63+ // Suppress all cfn-guard checks for all Lambda functions (including implicit CDK-generated ones)
64+ const allLambdas = findResourcesByType ( stack , "AWS::Lambda::Function" )
65+ addSuppressions ( allLambdas , [ "LAMBDA_DLQ_CHECK" , "LAMBDA_INSIDE_VPC" , "LAMBDA_CONCURRENCY_CHECK" ] )
4666
47- // S3 bucket suppressions
67+ // Suppress S3 bucket guard checks
4868 const bucketResources = findResourcesByPattern ( stack , [ "Bucket" , "Docs" , "Storage" ] )
4969 addSuppressions ( bucketResources , [ "S3_BUCKET_REPLICATION_ENABLED" , "S3_BUCKET_LOGGING_ENABLED" ] )
5070
51- // S3 policy suppressions
71+ // Suppress S3 policy guard checks
5272 const policyResources = findResourcesByPattern ( stack , [ "Policy" , "BucketPolicy" ] )
5373 addSuppressions ( policyResources , [ "S3_BUCKET_SSL_REQUESTS_ONLY" ] )
5474
55- // API Gateway suppressions
75+ // Suppress API Gateway stage guard checks
5676 const stageResources = findResourcesByPattern ( stack , [ "Stage" , "DeploymentStage" ] )
5777 addSuppressions ( stageResources , [ "API_GW_CACHE_ENABLED_AND_ENCRYPTED" ] )
5878}
0 commit comments