Skip to content

Commit c894ba1

Browse files
authored
fix(cloudfront-origins): policy not added for custom OAI (#18192)
Closes [**#18185**](#18185): When creating an `S3Origin` without including an existing `Origin Access Identity`, an `OriginAccessIdentity` is created and added to the bucket's resource policy. However, since the adding to the resource policy is inside of the `if (!this.originAccessIdentity)`closure, custom OAI's are not added to the bucket policy by default. Since using `bucket.grantRead` creates an overly permissive policy (as noted in the source code comments), adding the OAI to the bucket policy by default for both cases would create a more consistent result. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c99da16 commit c894ba1

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

packages/@aws-cdk/aws-cloudfront-origins/lib/s3-origin.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,16 @@ class S3BucketOrigin extends cloudfront.OriginBase {
7373
this.originAccessIdentity = new cloudfront.OriginAccessIdentity(oaiScope, oaiId, {
7474
comment: `Identity for ${options.originId}`,
7575
});
76-
77-
// Used rather than `grantRead` because `grantRead` will grant overly-permissive policies.
78-
// Only GetObject is needed to retrieve objects for the distribution.
79-
// This also excludes KMS permissions; currently, OAI only supports SSE-S3 for buckets.
80-
// Source: https://aws.amazon.com/blogs/networking-and-content-delivery/serving-sse-kms-encrypted-content-from-s3-using-cloudfront/
81-
this.bucket.addToResourcePolicy(new iam.PolicyStatement({
82-
resources: [this.bucket.arnForObjects('*')],
83-
actions: ['s3:GetObject'],
84-
principals: [this.originAccessIdentity.grantPrincipal],
85-
}));
8676
}
77+
// Used rather than `grantRead` because `grantRead` will grant overly-permissive policies.
78+
// Only GetObject is needed to retrieve objects for the distribution.
79+
// This also excludes KMS permissions; currently, OAI only supports SSE-S3 for buckets.
80+
// Source: https://aws.amazon.com/blogs/networking-and-content-delivery/serving-sse-kms-encrypted-content-from-s3-using-cloudfront/
81+
this.bucket.addToResourcePolicy(new iam.PolicyStatement({
82+
resources: [this.bucket.arnForObjects('*')],
83+
actions: ['s3:GetObject'],
84+
principals: [this.originAccessIdentity.grantPrincipal],
85+
}));
8786
return super.bind(scope, options);
8887
}
8988

packages/@aws-cdk/aws-cloudfront-origins/test/integ.s3-origin-oai.expected.json

+39
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@
55
"UpdateReplacePolicy": "Retain",
66
"DeletionPolicy": "Retain"
77
},
8+
"BucketPolicyE9A3008A": {
9+
"Type": "AWS::S3::BucketPolicy",
10+
"Properties": {
11+
"Bucket": {
12+
"Ref": "Bucket83908E77"
13+
},
14+
"PolicyDocument": {
15+
"Statement": [
16+
{
17+
"Action": "s3:GetObject",
18+
"Effect": "Allow",
19+
"Principal": {
20+
"CanonicalUser": {
21+
"Fn::GetAtt": [
22+
"OriginAccessIdentityDF1E3CAC",
23+
"S3CanonicalUserId"
24+
]
25+
}
26+
},
27+
"Resource": {
28+
"Fn::Join": [
29+
"",
30+
[
31+
{
32+
"Fn::GetAtt": [
33+
"Bucket83908E77",
34+
"Arn"
35+
]
36+
},
37+
"/*"
38+
]
39+
]
40+
}
41+
}
42+
],
43+
"Version": "2012-10-17"
44+
}
45+
}
46+
},
847
"OriginAccessIdentityDF1E3CAC": {
948
"Type": "AWS::CloudFront::CloudFrontOriginAccessIdentity",
1049
"Properties": {

packages/@aws-cdk/aws-cloudfront-origins/test/s3-origin.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ describe('With bucket', () => {
8282
Comment: 'Identity for bucket provided by test',
8383
},
8484
});
85+
86+
expect(stack).toHaveResourceLike('AWS::S3::BucketPolicy', {
87+
PolicyDocument: {
88+
Statement: [{
89+
Action: 's3:GetObject',
90+
Principal: {
91+
CanonicalUser: { 'Fn::GetAtt': ['OriginAccessIdentityDF1E3CAC', 'S3CanonicalUserId'] },
92+
},
93+
Resource: {
94+
'Fn::Join': ['', [{ 'Fn::GetAtt': ['Bucket83908E77', 'Arn'] }, '/*']],
95+
},
96+
}],
97+
},
98+
});
8599
});
86100

87101
test('creates an OriginAccessIdentity and grants read permissions on the bucket', () => {

0 commit comments

Comments
 (0)