Skip to content

Commit f1240d2

Browse files
suhussaitobuck-aws
andauthored
fix: use proper pathing for nagsuppression of cdk-managed resources (#90)
Co-authored-by: Toby Buckley <74737385+tobuck-aws@users.noreply.github.com>
1 parent 9f1f6bf commit f1240d2

File tree

3 files changed

+62
-54
lines changed

3 files changed

+62
-54
lines changed

src/control-plane/auth/cognito-auth.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -477,26 +477,29 @@ export class CognitoAuth extends Construct implements IAuth {
477477
this.enableUserFunction = userManagementServices;
478478

479479
// https://github.com/aws/aws-cdk/issues/23204
480-
NagSuppressions.addResourceSuppressionsByPath(
481-
cdk.Stack.of(this),
482-
[
483-
`/${cdk.Stack.of(this).stackName}/AWS679f53fac002430cb0da5b7982bd2287/ServiceRole/Resource`,
484-
`/${cdk.Stack.of(this).stackName}/AWS679f53fac002430cb0da5b7982bd2287/Resource`,
485-
],
486-
[
487-
{
488-
id: 'AwsSolutions-IAM4',
489-
reason: 'Suppress usage of AWSLambdaBasicExecutionRole.',
490-
appliesTo: [
491-
'Policy::arn:<AWS::Partition>:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
492-
],
493-
},
494-
{
495-
id: 'AwsSolutions-L1',
496-
reason: 'NODEJS 18 is the version used in the official quickstart CFN template.',
497-
},
498-
]
499-
);
480+
if (cdk.Stack.of(this).node.tryFindChild('AWS679f53fac002430cb0da5b7982bd2287')) {
481+
const AWS679f53fac002430cb0da5b7982bd2287 = cdk.Stack.of(this).node.findChild(
482+
'AWS679f53fac002430cb0da5b7982bd2287'
483+
);
484+
NagSuppressions.addResourceSuppressionsByPath(
485+
cdk.Stack.of(this),
486+
[AWS679f53fac002430cb0da5b7982bd2287.node.path],
487+
[
488+
{
489+
id: 'AwsSolutions-IAM4',
490+
reason: 'Suppress usage of AWSLambdaBasicExecutionRole.',
491+
appliesTo: [
492+
'Policy::arn:<AWS::Partition>:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
493+
],
494+
},
495+
{
496+
id: 'AwsSolutions-L1',
497+
reason: 'NODEJS 18 is the version used in the official quickstart CFN template.',
498+
},
499+
],
500+
true
501+
);
502+
}
500503

501504
this.createAdminUserFunction = new PythonFunction(this, 'createAdminUserFunction', {
502505
entry: path.join(__dirname, '../../../resources/functions/auth-custom-resource'),

src/control-plane/aws-marketplace/sample-registration-web-page.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,39 +92,44 @@ export class SampleRegistrationWebPage extends Construct {
9292

9393
dynamicFile.node.addDependency(staticFiles);
9494

95-
NagSuppressions.addResourceSuppressionsByPath(
96-
cdk.Stack.of(this),
97-
[
98-
`${cdk.Stack.of(this).stackName}/Custom::CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C`,
99-
],
100-
[
101-
{
102-
id: 'AwsSolutions-IAM4',
103-
reason: 'Suppress usage of AWSLambdaBasicExecutionRole.',
104-
appliesTo: [
105-
'Policy::arn:<AWS::Partition>:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
106-
],
107-
},
108-
{
109-
id: 'AwsSolutions-IAM5',
110-
reason: 'Allow wildcard access for CDKBucketDeployment.',
111-
appliesTo: [
112-
'Action::s3:GetObject*',
113-
'Action::s3:GetBucket*',
114-
'Action::s3:List*',
115-
`Resource::arn:<AWS::Partition>:s3:::cdk-${cdk.DefaultStackSynthesizer.DEFAULT_QUALIFIER}-assets-<AWS::AccountId>-${region}/*`,
116-
'Action::s3:DeleteObject*',
117-
'Action::s3:Abort*',
118-
`Resource::<${cdk.Stack.of(this).getLogicalId(websiteBucket.node.defaultChild as s3.CfnBucket)}.Arn>/*`,
119-
],
120-
},
121-
{
122-
id: 'AwsSolutions-L1',
123-
reason: 'NODEJS 18 is the version used cdk maanged custom resource.',
124-
},
125-
],
126-
true
127-
);
95+
if (
96+
cdk.Stack.of(this).node.tryFindChild('CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C')
97+
) {
98+
const CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C = cdk.Stack.of(this).node.findChild(
99+
'CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C'
100+
);
101+
NagSuppressions.addResourceSuppressionsByPath(
102+
cdk.Stack.of(this),
103+
[CDKBucketDeployment8693BB64968944B69AAFB0CC9EB8756C.node.path],
104+
[
105+
{
106+
id: 'AwsSolutions-IAM4',
107+
reason: 'Suppress usage of AWSLambdaBasicExecutionRole.',
108+
appliesTo: [
109+
'Policy::arn:<AWS::Partition>:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
110+
],
111+
},
112+
{
113+
id: 'AwsSolutions-IAM5',
114+
reason: 'Allow wildcard access for CDKBucketDeployment.',
115+
appliesTo: [
116+
'Action::s3:GetObject*',
117+
'Action::s3:GetBucket*',
118+
'Action::s3:List*',
119+
`Resource::arn:<AWS::Partition>:s3:::cdk-${cdk.DefaultStackSynthesizer.DEFAULT_QUALIFIER}-assets-<AWS::AccountId>-${region}/*`,
120+
'Action::s3:DeleteObject*',
121+
'Action::s3:Abort*',
122+
`Resource::<${cdk.Stack.of(this).getLogicalId(websiteBucket.node.defaultChild as s3.CfnBucket)}.Arn>/*`,
123+
],
124+
},
125+
{
126+
id: 'AwsSolutions-L1',
127+
reason: 'NODEJS 18 is the version used cdk maanged custom resource.',
128+
},
129+
],
130+
true
131+
);
132+
}
128133

129134
const logBucket = new s3.Bucket(this, 'WebsiteS3BucketLog', {
130135
enforceSSL: true,

test/control-plane.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('No unsuppressed cdk-nag Warnings or Errors', () => {
2727

2828
const stack = new ControlPlaneStack(app, 'ControlPlaneStack');
2929

30-
cdk.Aspects.of(stack).add(new AwsSolutionsChecks({ verbose: true }));
30+
cdk.Aspects.of(app).add(new AwsSolutionsChecks({ verbose: true }));
3131

3232
it('should have no unsuppressed Warnings', () => {
3333
const warnings = Annotations.fromStack(stack).findWarning(

0 commit comments

Comments
 (0)