Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(s3): add bucket policy dependency to notification resource #30053

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@
"Managed": true
},
"DependsOn": [
"BAllowBucketNotificationsTolambdaeventsources3F741608059EF9F709"
"BAllowBucketNotificationsTolambdaeventsources3F741608059EF9F709",
"BPolicy3F02723E"
]
},
"BAllowBucketNotificationsTolambdaeventsources3F741608059EF9F709": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@
"Managed": true
},
"DependsOn": [
"Bucket2Policy945B22E3",
"MyQueuePolicy6BBEDDAC",
"MyQueueE6CA6235"
]
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,53 @@
"UpdateReplacePolicy": "Delete",
"DeletionPolicy": "Delete"
},
"MyEventBridgeBucketPolicy8F5346E3": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
"Ref": "MyEventBridgeBucket1ABD5C2A"
},
"PolicyDocument": {
"Statement": [
{
"Action": "s3:*",
"Condition": {
"Bool": {
"aws:SecureTransport": "false"
}
},
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Resource": [
{
"Fn::GetAtt": [
"MyEventBridgeBucket1ABD5C2A",
"Arn"
]
},
{
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"MyEventBridgeBucket1ABD5C2A",
"Arn"
]
},
"/*"
]
]
}
]
}
],
"Version": "2012-10-17"
}
}
},
"MyEventBridgeBucketNotifications19C0453F": {
"Type": "Custom::S3BucketNotifications",
"Properties": {
Expand All @@ -21,7 +68,10 @@
"EventBridgeConfiguration": {}
},
"Managed": true
}
},
"DependsOn": [
"MyEventBridgeBucketPolicy8F5346E3"
]
},
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC": {
"Type": "AWS::IAM::Role",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const stack = new cdk.Stack(app, 'aws-cdk-s3-notifications');

new s3.Bucket(stack, 'MyEventBridgeBucket', {
eventBridgeEnabled: true,
enforceSSL: true, // Adding dummy bucket policy for testing that bucket policy is created before bucket notification
removalPolicy: cdk.RemovalPolicy.DESTROY,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Construct } from 'constructs';
import { Construct, IConstruct } from 'constructs';
import { NotificationsResourceHandler } from './notifications-resource-handler';
import * as iam from '../../../aws-iam';
import * as cdk from '../../../core';
Expand Down Expand Up @@ -135,6 +135,20 @@ export class BucketNotifications extends Construct {
Managed: managed,
},
});

// Add dependency on bucket policy if it exists to avoid race conditions
// S3 does not allow calling PutBucketPolicy and PutBucketNotification APIs at the same time
// See https://github.com/aws/aws-cdk/issues/27600
// Aspects are used here because bucket policy maybe added to construct after addition of notification resource.
const bucket = this.bucket;
const resource = this.resource;
cdk.Aspects.of(this).add({
visit(node: IConstruct) {
if (node === resource && bucket.policy) {
node.node.addDependency(bucket.policy);
}
},
});
}

return this.resource;
Expand Down
64 changes: 63 additions & 1 deletion packages/aws-cdk-lib/aws-s3/test/notification.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Template } from '../../assertions';
import { Match, Template } from '../../assertions';
import * as iam from '../../aws-iam';
import * as cdk from '../../core';
import * as s3 from '../lib';
Expand Down Expand Up @@ -121,6 +121,68 @@ describe('notification', () => {
});
});

test('custom resource must not depend on bucket policy if it bucket policy does not exists', () => {
const stack = new cdk.Stack();

const bucket = new s3.Bucket(stack, 'MyBucket');

bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
});

Template.fromStack(stack).hasResource('Custom::S3BucketNotifications', {
Type: 'Custom::S3BucketNotifications',
DependsOn: Match.absent(),
});
});

test('custom resource must depend on bucket policy to prevent executing too early', () => {
const stack = new cdk.Stack();

const bucket = new s3.Bucket(stack, 'MyBucket', {
enforceSSL: true, // adds bucket policy for test
});

bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
});

Template.fromStack(stack).hasResource('Custom::S3BucketNotifications', {
Type: 'Custom::S3BucketNotifications',
DependsOn: ['MyBucketPolicyE7FBAC7B'],
});
});

test('custom resource must depend on bucket policy even if bucket policy is added after notification', () => {
const stack = new cdk.Stack();

const bucket = new s3.Bucket(stack, 'MyBucket');

bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC,
}),
});

bucket.addToResourcePolicy(new iam.PolicyStatement({
resources: [bucket.bucketArn],
actions: ['s3:GetBucketAcl'],
principals: [new iam.AnyPrincipal()],
}));

Template.fromStack(stack).hasResource('Custom::S3BucketNotifications', {
Type: 'Custom::S3BucketNotifications',
DependsOn: ['MyBucketPolicyE7FBAC7B'],
});
});

test('throws with multiple prefix rules in a filter', () => {
const stack = new cdk.Stack();

Expand Down