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): fail early with bad notification filters #3397

Merged
merged 1 commit into from
Jul 23, 2019
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 @@ -122,18 +122,28 @@ function renderFilters(filters?: NotificationKeyFilter[]): Filter | undefined {
}

const renderedRules = new Array<FilterRule>();
let hasPrefix = false;
let hasSuffix = false;

for (const rule of filters) {
if (!rule.suffix && !rule.prefix) {
throw new Error('NotificationKeyFilter must specify `prefix` and/or `suffix`');
}

if (rule.suffix) {
if (hasSuffix) {
throw new Error('Cannot specify more than one suffix rule in a filter.');
}
renderedRules.push({ Name: 'suffix', Value: rule.suffix });
hasSuffix = true;
}

if (rule.prefix) {
if (hasPrefix) {
throw new Error('Cannot specify more than one prefix rule in a filter.');
}
renderedRules.push({ Name: 'prefix', Value: rule.prefix });
hasPrefix = true;
}
}

Expand Down
74 changes: 73 additions & 1 deletion packages/@aws-cdk/aws-s3/test/test.notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,76 @@ export = {

test.done();
},
};

'can specify prefix and suffix filter rules'(test: Test) {
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
}),
}, { prefix: 'images/', suffix: '.png' });

expect(stack).to(haveResource('Custom::S3BucketNotifications', {
NotificationConfiguration: {
TopicConfigurations: [
{
Events: [
's3:ObjectCreated:*'
],
Filter: {
Key: {
FilterRules: [
{
Name: 'suffix',
Value: '.png'
},
{
Name: 'prefix',
Value: 'images/'
}
]
}
},
TopicArn: 'ARN'
}
]
}
}));

test.done();
},

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

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

test.throws(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC
}),
}, { prefix: 'images/'}, { prefix: 'archive/' }), /prefix rule/);

test.done();
},

'throws with multiple suffix rules in a filter'(test: Test) {
const stack = new cdk.Stack();

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

test.throws(() => bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
bind: () => ({
arn: 'ARN',
type: s3.BucketNotificationDestinationType.TOPIC
}),
}, { suffix: '.png'}, { suffix: '.zip' }), /suffix rule/);

test.done();
}
};