Skip to content

Commit 94d09a7

Browse files
authored
fix: prevent content deletion from artifact bucket (#1073)
* fix: prevent content deletion from artifact bucket Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com> * fix: add listBucket permission to s3 policy Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com> --------- Signed-off-by: Arjun Raja Yogidas <arjunry@amazon.com>
1 parent aab075a commit 94d09a7

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

lib/artifact-bucket-cloudfront.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export class ArtifactBucketCloudfrontStack extends cdk.Stack {
2424
// upload the file for integration testing puporse
2525
new s3Deployment.BucketDeployment(this, 'DeployTestFile', {
2626
sources: [s3Deployment.Source.asset('./assets')],
27-
destinationBucket: artifactBucket
27+
destinationBucket: artifactBucket,
28+
prune: false
2829
});
2930

3031
const cloudfrontCdn = new CloudfrontCdn(this, 'ArtifactCloudfrontCdn', {

lib/cloudfront_cdn.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ export class CloudfrontCdn extends Construct {
2020

2121
props.bucket.addToResourcePolicy(
2222
new iam.PolicyStatement({
23-
actions: ['s3:GetObject'],
24-
resources: [props.bucket.arnForObjects('*')],
23+
actions: ['s3:GetObject', 's3:ListBucket'],
24+
resources: [
25+
props.bucket.bucketArn, // arn:aws:s3:::bucket-name
26+
props.bucket.arnForObjects('*') // arn:aws:s3:::bucket-name/*
27+
],
2528
principals: [new iam.CanonicalUserPrincipal(cloudfrontOAI.cloudFrontOriginAccessIdentityS3CanonicalUserId)]
2629
})
2730
);

test/cloudfront_cdn.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,33 @@ describe('CloudfrontCdn', () => {
4646
Ref: bukcetLogicalId
4747
},
4848
PolicyDocument: {
49-
Statement: [
49+
Statement: Match.arrayWith([
5050
Match.objectLike({
51-
Action: 's3:GetObject',
51+
Action: ['s3:GetObject', 's3:ListBucket'],
5252
Effect: 'Allow',
5353
Principal: {
5454
CanonicalUser: {
5555
'Fn::GetAtt': [Match.anyValue(), 'S3CanonicalUserId']
5656
}
5757
},
58-
Resource: {
59-
'Fn::Join': [
60-
'',
61-
[
62-
{
63-
'Fn::GetAtt': [bukcetLogicalId, 'Arn']
64-
},
65-
'/*'
58+
Resource: [
59+
{
60+
'Fn::GetAtt': [bukcetLogicalId, 'Arn']
61+
},
62+
{
63+
'Fn::Join': [
64+
'',
65+
[
66+
{
67+
'Fn::GetAtt': [bukcetLogicalId, 'Arn']
68+
},
69+
'/*'
70+
]
6671
]
67-
]
68-
}
72+
}
73+
]
6974
})
70-
],
75+
]),
7176
Version: '2012-10-17'
7277
}
7378
});

0 commit comments

Comments
 (0)