@@ -19,7 +19,6 @@ import * as iam from 'aws-cdk-lib/aws-iam';
1919import * as s3 from 'aws-cdk-lib/aws-s3' ;
2020import { NagSuppressions } from 'cdk-nag' ;
2121import { Construct } from 'constructs' ;
22- import * as crypto from 'crypto' ;
2322
2423/**
2524 * Interface that defines properties required for the Static Website
@@ -107,7 +106,62 @@ export class StaticWebsite extends Construct {
107106 defaultRootObject : 'login.html'
108107 }
109108 } ) ;
110- cloudfrontToS3 . cloudFrontLoggingBucket ?. node . addDependency ( bucketPolicyUpdateCustomResource ) ;
109+
110+ const cloudFrontLogsLoggingPrefix = 'cloudfrontlogs-logging' ;
111+
112+ const cloudFrontLoggingUpdateBucketPolicy = new cdk . CustomResource (
113+ this ,
114+ 'CloudFrontLoggingUpdateBucketPolicy' ,
115+ {
116+ resourceType : 'Custom::UpdateBucketPolicy' ,
117+ serviceToken : props . customResourceLambdaArn ,
118+ properties : {
119+ Resource : 'UPDATE_BUCKET_POLICY' ,
120+ SOURCE_BUCKET_NAME : cloudfrontToS3 . cloudFrontLoggingBucket ?. bucketName ,
121+ LOGGING_BUCKET_NAME : props . accessLoggingBucket . bucketName ,
122+ SOURCE_PREFIX : cloudFrontLogsLoggingPrefix
123+ }
124+ }
125+ ) ;
126+
127+ cloudFrontLoggingUpdateBucketPolicy . node . addDependency ( bucketPolicyForLambda ) ;
128+
129+ const cfnCloudFrontLoggingBucket = cloudfrontToS3 . cloudFrontLoggingBucket ?. node . defaultChild as s3 . CfnBucket ;
130+ cfnCloudFrontLoggingBucket . addPropertyOverride ( 'LoggingConfiguration' , {
131+ DestinationBucketName : {
132+ 'Fn::Select' : [
133+ 0 ,
134+ {
135+ 'Fn::Split' : [
136+ '/' ,
137+ {
138+ 'Fn::Select' : [
139+ 5 ,
140+ {
141+ 'Fn::Split' : [
142+ ':' ,
143+ {
144+ 'Ref' : 'AccessLoggingBucketArn'
145+ }
146+ ]
147+ }
148+ ]
149+ }
150+ ]
151+ }
152+ ]
153+ } ,
154+ LogFilePrefix : `${ cloudFrontLogsLoggingPrefix } /`
155+ } ) ;
156+ // disabling versioning, since it was disabled in a previous release, enabling
157+ // versioning now will create a new bucket with an update from the previous version
158+ cfnCloudFrontLoggingBucket . addPropertyDeletionOverride ( 'VersioningConfiguration' ) ;
159+
160+ cloudfrontToS3 . node . tryFindChild ( 'CloudfrontLoggingBucketAccessLog' ) ?. node . tryRemoveChild ( 'Resource' ) ;
161+ cloudfrontToS3 . node
162+ . tryFindChild ( 'CloudfrontLoggingBucketAccessLog' )
163+ ?. node . tryFindChild ( 'Policy' )
164+ ?. node . tryRemoveChild ( 'Resource' ) ;
111165
112166 const cloudfrontFunction = cloudfrontToS3 . node
113167 . tryFindChild ( 'SetHttpSecurityHeaders' )
@@ -120,73 +174,6 @@ export class StaticWebsite extends Construct {
120174 ) ;
121175
122176 this . cloudFrontDistribution = cloudfrontToS3 . cloudFrontWebDistribution ;
123- this . cloudFrontDistribution . node
124- . tryFindChild ( 'Origin1' )
125- ?. node . tryFindChild ( 'S3Origin' )
126- ?. node . tryRemoveChild ( 'Resource' ) ;
127-
128- const originAccessControl = new cloudfront . CfnOriginAccessControl ( this , 'OAC' , {
129- originAccessControlConfig : {
130- name : `BucketOriginAccessControl-${ cdk . Aws . REGION } ` ,
131- originAccessControlOriginType : 's3' ,
132- signingBehavior : 'always' ,
133- signingProtocol : 'sigv4'
134- }
135- } ) ;
136-
137- // prettier-ignore
138- let l1CloudFrontDistribution = this . cloudFrontDistribution . node . defaultChild as cdk . aws_cloudfront . CfnDistribution ;
139- // prettier-ignore
140- l1CloudFrontDistribution . addPropertyOverride ( 'DistributionConfig.Origins.0.OriginAccessControlId' , originAccessControl . getAtt ( 'Id' ) ) ;
141- // prettier-ignore
142- l1CloudFrontDistribution . addPropertyOverride ( 'DistributionConfig.Origins.0.S3OriginConfig.OriginAccessIdentity' , '' ) ;
143-
144- this . node . tryFindChild ( 'CloudfrontLoggingBucket' ) ?. node . tryRemoveChild ( 'Resource' ) ;
145- this . node . tryFindChild ( 'CloudfrontLoggingBucket' ) ?. node . tryFindChild ( 'Policy' ) ?. node . tryRemoveChild ( 'Resource' ) ;
146-
147- let l1BucketPolicy = this . webS3Bucket . node . tryFindChild ( 'Policy' ) ?. node . defaultChild as s3 . CfnBucketPolicy ;
148- l1BucketPolicy . addPropertyOverride ( 'PolicyDocument' , {
149- Statement : [
150- {
151- Action : 's3:*' ,
152- Condition : {
153- Bool : {
154- 'aws:SecureTransport' : 'false'
155- }
156- } ,
157- Effect : 'Deny' ,
158- Principal : {
159- AWS : '*'
160- } ,
161- Resource : [ `${ this . webS3Bucket . bucketArn } ` , `${ this . webS3Bucket . bucketArn } /*` ]
162- } ,
163- {
164- Action : 's3:GetObject' ,
165- Condition : {
166- StringEquals : {
167- 'AWS:SourceArn' : {
168- 'Fn::Join' : [
169- '' ,
170- [
171- 'arn:' ,
172- `${ cdk . Aws . PARTITION } ` ,
173- ':cloudfront::' ,
174- `${ cdk . Aws . ACCOUNT_ID } ` ,
175- ':distribution/' ,
176- `${ this . cloudFrontDistribution . distributionId } `
177- ]
178- ]
179- }
180- }
181- } ,
182- Effect : 'Allow' ,
183- Principal : {
184- Service : 'cloudfront.amazonaws.com'
185- } ,
186- Resource : `${ this . webS3Bucket . bucketArn } /*`
187- }
188- ]
189- } ) ;
190177
191178 // prettier-ignore
192179 new cdk . CfnOutput ( cdk . Stack . of ( this ) , 'WebUrl' , { // NOSONAR - Typescript construct instantiation
0 commit comments