Skip to content

Commit 41327d8

Browse files
authored
fix(aws-s3): log delivery may be incorrectly configured when target bucket is imported (#23552)
This resolves issues with log delivery for situations where the target bucket for S3 Access Logs is in another stack. This situation was previously missing from unit and integration tests. This adds additional checks to make sure that this behavior works. There are two primary issues here: 1. Regardless of `@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy`, if the target bucket was imported (not a concrete `Bucket`), the grant would be applied to the _source_ bucket for logs improperly. This resulted in the ACL or Bucket Policy being added to the wrong stack. 2. When `@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy` was _enabled_ the created bucket policy resulted in a cyclical dependency because of the conditions; these conditions are not necessary for successful delivery. This omits them unless the bucket is concrete and in the same stack. Unit tests and integration tests now cover importing a bucket between stacks. Closes: #23547 Closes: #23588 ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Construct Runtime Dependencies: * [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 59a16d5 commit 41327d8

15 files changed

+1759
-9
lines changed

packages/@aws-cdk/aws-s3/lib/bucket.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Tags,
2020
Token,
2121
Tokenization,
22+
Annotations,
2223
} from '@aws-cdk/core';
2324
import { CfnReference } from '@aws-cdk/core/lib/private/cfn-reference';
2425
import * as cxapi from '@aws-cdk/cx-api';
@@ -1833,8 +1834,19 @@ export class Bucket extends BucketBase {
18331834

18341835
if (props.serverAccessLogsBucket instanceof Bucket) {
18351836
props.serverAccessLogsBucket.allowLogDelivery(this, props.serverAccessLogsPrefix);
1836-
} else if (props.serverAccessLogsPrefix) {
1837+
// It is possible that `serverAccessLogsBucket` was specified but is some other `IBucket`
1838+
// that cannot have the ACLs or bucket policy applied. In that scenario, we should only
1839+
// setup log delivery permissions to `this` if a bucket was not specified at all, as documented.
1840+
// For example, we should not allow log delivery to `this` if given an imported bucket or
1841+
// another situation that causes `instanceof` to fail
1842+
} else if (!props.serverAccessLogsBucket && props.serverAccessLogsPrefix) {
18371843
this.allowLogDelivery(this, props.serverAccessLogsPrefix);
1844+
} else if (props.serverAccessLogsBucket) {
1845+
// A `serverAccessLogsBucket` was provided but it is not a concrete `Bucket` and it
1846+
// may not be possible to configure the ACLs or bucket policy as required.
1847+
Annotations.of(this).addWarning(
1848+
`Unable to add necessary logging permissions to imported target bucket: ${props.serverAccessLogsBucket}`,
1849+
);
18381850
}
18391851

18401852
for (const inventory of props.inventories ?? []) {
@@ -2201,19 +2213,26 @@ export class Bucket extends BucketBase {
22012213
*/
22022214
private allowLogDelivery(from: IBucket, prefix?: string) {
22032215
if (FeatureFlags.of(this).isEnabled(cxapi.S3_SERVER_ACCESS_LOGS_USE_BUCKET_POLICY)) {
2204-
this.addToResourcePolicy(new iam.PolicyStatement({
2205-
effect: iam.Effect.ALLOW,
2206-
principals: [new iam.ServicePrincipal('logging.s3.amazonaws.com')],
2207-
actions: ['s3:PutObject'],
2208-
resources: [this.arnForObjects(prefix ? `${prefix}*`: '*')],
2209-
conditions: {
2216+
let conditions = undefined;
2217+
// The conditions for the bucket policy can be applied only when the buckets are in
2218+
// the same stack and a concrete bucket instance (not imported). Otherwise, the
2219+
// necessary imports may result in a cyclic dependency between the stacks.
2220+
if (from instanceof Bucket && Stack.of(this) === Stack.of(from)) {
2221+
conditions = {
22102222
ArnLike: {
22112223
'aws:SourceArn': from.bucketArn,
22122224
},
22132225
StringEquals: {
22142226
'aws:SourceAccount': from.env.account,
22152227
},
2216-
},
2228+
};
2229+
}
2230+
this.addToResourcePolicy(new iam.PolicyStatement({
2231+
effect: iam.Effect.ALLOW,
2232+
principals: [new iam.ServicePrincipal('logging.s3.amazonaws.com')],
2233+
actions: ['s3:PutObject'],
2234+
resources: [this.arnForObjects(prefix ? `${prefix}*`: '*')],
2235+
conditions: conditions,
22172236
}));
22182237
} else if (this.accessControl && this.accessControl !== BucketAccessControl.LOG_DELIVERY_WRITE) {
22192238
throw new Error("Cannot enable log delivery to this bucket because the bucket's ACL has been set and can't be changed");

packages/@aws-cdk/aws-s3/test/bucket.test.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EOL } from 'os';
2-
import { Match, Template } from '@aws-cdk/assertions';
2+
import { Annotations, Match, Template } from '@aws-cdk/assertions';
33
import * as iam from '@aws-cdk/aws-iam';
44
import * as kms from '@aws-cdk/aws-kms';
55
import * as cdk from '@aws-cdk/core';
@@ -2205,6 +2205,31 @@ describe('bucket', () => {
22052205
).toThrow(/Cannot enable log delivery to this bucket because the bucket's ACL has been set and can't be changed/);
22062206
});
22072207

2208+
test('Bucket skips setting up access log ACL but configures delivery for an imported target bucket', () => {
2209+
// GIVEN
2210+
const stack = new cdk.Stack();
2211+
2212+
// WHEN
2213+
const accessLogBucket = s3.Bucket.fromBucketName(stack, 'TargetBucket', 'target-logs-bucket');
2214+
new s3.Bucket(stack, 'TestBucket', {
2215+
serverAccessLogsBucket: accessLogBucket,
2216+
serverAccessLogsPrefix: 'test/',
2217+
});
2218+
2219+
// THEN
2220+
const template = Template.fromStack(stack);
2221+
template.hasResourceProperties('AWS::S3::Bucket', {
2222+
LoggingConfiguration: {
2223+
DestinationBucketName: stack.resolve(accessLogBucket.bucketName),
2224+
LogFilePrefix: 'test/',
2225+
},
2226+
});
2227+
template.allResourcesProperties('AWS::S3::Bucket', {
2228+
AccessControl: Match.absent(),
2229+
});
2230+
Annotations.fromStack(stack).hasWarning('*', Match.stringLikeRegexp('Unable to add necessary logging permissions to imported target bucket'));
2231+
});
2232+
22082233
test('Bucket Allow Log delivery should use the recommended policy when flag enabled', () => {
22092234
// GIVEN
22102235
const stack = new cdk.Stack();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "22.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "ServerAccessLogsImportTestDefaultTestDeployAssert076DA7F5.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}

packages/@aws-cdk/aws-s3/test/integ.bucket-import-server-access-logs.js.snapshot/asset.33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c/__entrypoint__.js

+144
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-s3/test/integ.bucket-import-server-access-logs.js.snapshot/asset.33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c/index.js

+78
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"version": "22.0.0",
3+
"files": {
4+
"33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c": {
5+
"source": {
6+
"path": "asset.33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c",
7+
"packaging": "zip"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "33e2651435a0d472a75c1e033c9832b21321d9e56711926b04c5705e5f63874c.zip",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
},
17+
"f7cd2df39343e955bf8713c630fe64d9469d0fb8da00f7858ca7874cf6aac7de": {
18+
"source": {
19+
"path": "aws-cdk-s3-access-logs-delivery.template.json",
20+
"packaging": "file"
21+
},
22+
"destinations": {
23+
"current_account-current_region": {
24+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
25+
"objectKey": "f7cd2df39343e955bf8713c630fe64d9469d0fb8da00f7858ca7874cf6aac7de.json",
26+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
27+
}
28+
}
29+
}
30+
},
31+
"dockerImages": {}
32+
}

0 commit comments

Comments
 (0)