-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check origin access control usage for cloudfront with s3 origin (…
…#1794) Fixes #1582 CDK now supports [S3 Origin Access Control L2 construct](aws/aws-cdk#31254). Added a new rule to check if OAC is configured for CloudFront distributions using S3 as an origin. * Bumped cdk version used in development * Added missing parameters in QuickSight tests accordingly * Applied the existing OAI rule only to CloudFront Streaming distributions (CloudFront distributions will not be non-compliant if OAI is not configured any more) * Added a new rule checking OAC usage. Included the rule to AWS Solutions packs as `AwsSolutions-CFR7`
- Loading branch information
Showing
12 changed files
with
238 additions
and
145 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ const { awscdk, vscode } = require('projen'); | |
const project = new awscdk.AwsCdkConstructLibrary({ | ||
author: 'Arun Donti', | ||
authorAddress: '[email protected]', | ||
cdkVersion: '2.116.0', | ||
cdkVersion: '2.156.0', | ||
defaultReleaseBranch: 'main', | ||
majorVersion: 2, | ||
npmDistTag: 'latest', | ||
|
@@ -87,5 +87,8 @@ new vscode.DevContainer(project, { | |
}); | ||
project.package.addField('resolutions', { | ||
'jsii-rosetta': '~5.0.7', | ||
'@babel/types': '7.25.7', | ||
'@types/babel__traverse': '7.18.2', | ||
'@types/prettier': '2.6.0', | ||
}); | ||
project.synth(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/rules/cloudfront/CloudFrontDistributionS3OriginAccessControl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { parse } from 'path'; | ||
import { CfnResource, Stack } from 'aws-cdk-lib'; | ||
import { CfnDistribution } from 'aws-cdk-lib/aws-cloudfront'; | ||
import { CfnBucket } from 'aws-cdk-lib/aws-s3'; | ||
import { NagRuleCompliance, NagRules } from '../../nag-rules'; | ||
|
||
/** | ||
* CloudFront distributions use an origin access control for S3 origins | ||
* @param node the CfnResource to check | ||
*/ | ||
export default Object.defineProperty( | ||
(node: CfnResource): NagRuleCompliance => { | ||
if (node instanceof CfnDistribution) { | ||
const distributionConfig = Stack.of(node).resolve( | ||
node.distributionConfig | ||
); | ||
if (distributionConfig.origins != undefined) { | ||
const origins = Stack.of(node).resolve(distributionConfig.origins); | ||
for (const origin of origins) { | ||
const resolvedOrigin = Stack.of(node).resolve(origin); | ||
const resolvedDomainName = Stack.of(node).resolve( | ||
resolvedOrigin.domainName | ||
); | ||
const originLogicalId = NagRules.resolveResourceFromInstrinsic( | ||
node, | ||
resolvedDomainName | ||
); | ||
for (const child of Stack.of(node).node.findAll()) { | ||
if (child instanceof CfnBucket) { | ||
const childLogicalId = NagRules.resolveResourceFromInstrinsic( | ||
child, | ||
child.ref | ||
); | ||
if (originLogicalId === childLogicalId) { | ||
const resolvedAccessControlId = Stack.of(node).resolve( | ||
resolvedOrigin.originAccessControlId | ||
); | ||
const originAccessControlId = | ||
NagRules.resolveResourceFromInstrinsic( | ||
node, | ||
resolvedAccessControlId | ||
); | ||
if (originAccessControlId == undefined) { | ||
return NagRuleCompliance.NON_COMPLIANT; | ||
} | ||
if (originAccessControlId.replace(/\s/g, '').length == 0) { | ||
return NagRuleCompliance.NON_COMPLIANT; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return NagRuleCompliance.COMPLIANT; | ||
} | ||
return NagRuleCompliance.NOT_APPLICABLE; | ||
}, | ||
'name', | ||
{ value: parse(__filename).name } | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.