Skip to content

Commit

Permalink
fix(s3): fail early with bad notification filters (#3397)
Browse files Browse the repository at this point in the history
Avoid CF deploy time errors when specifying multiple prefixes or suffixes in notification filters.

Closes #3347
Possibly something to fix in v2.0 (#3398)
  • Loading branch information
jogold authored and Elad Ben-Israel committed Jul 23, 2019
1 parent 820575b commit cd0e9bd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
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();
}
};

0 comments on commit cd0e9bd

Please sign in to comment.