Skip to content

Commit 7adb5ea

Browse files
rix0rrrmergify[bot]
authored andcommitted
fix(cloudtrail): fix use of imported bucket with CloudTrail (#4270)
Stop the CloudTaril construct from reaching into the S3 Bucket construct and touching its implementation detail, go through the public interface instead. Fixes #4256.
1 parent 1381b2d commit 7adb5ea

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/@aws-cdk/aws-cloudtrail/lib/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,10 @@ export class Trail extends Resource {
206206
});
207207
this.trailSnsTopicArn = trail.attrSnsTopicArn;
208208

209-
const s3BucketPolicy = this.s3bucket.node.findChild("Policy").node.findChild("Resource") as s3.CfnBucketPolicy;
210-
trail.node.addDependency(s3BucketPolicy);
209+
// Add a dependency on the bucket policy being updated, CloudTrail will test this upon creation.
210+
if (this.s3bucket.policy) {
211+
trail.node.addDependency(this.s3bucket.policy);
212+
}
211213

212214
// If props.sendToCloudWatchLogs is set to true then the trail needs to depend on the created logsRole
213215
// so that it can create the log stream for the log group. This ensures the logsRole is created and propagated

packages/@aws-cdk/aws-cloudtrail/test/test.cloudtrail.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ export = {
9696
expect(stack).to(not(haveResource("AWS::Logs::LogGroup")));
9797
test.done();
9898
},
99+
100+
'with imported s3 bucket'(test: Test) {
101+
// GIVEN
102+
const stack = getTestStack();
103+
const bucket = s3.Bucket.fromBucketName(stack, 'S3', 'SomeBucket');
104+
105+
// WHEN
106+
new Trail(stack, 'Trail', { bucket });
107+
108+
expect(stack).to(haveResource('AWS::CloudTrail::Trail', {
109+
S3BucketName: 'SomeBucket'
110+
}));
111+
112+
test.done();
113+
},
114+
99115
'with cloud watch logs': {
100116
'enabled'(test: Test) {
101117
const stack = getTestStack();

0 commit comments

Comments
 (0)