Skip to content

Commit

Permalink
fix(s3): fail fast for s3 lifecycle configuration when ExpiredObjectD…
Browse files Browse the repository at this point in the history
…eleteMarker specified with ExpirationInDays, ExpirationDate, or TagFilters.
  • Loading branch information
Zishanwang1992 committed Jun 13, 2023
1 parent 3bd973a commit a43967a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/aws-cdk-lib/aws-s3/lib/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,11 @@ export class Bucket extends BucketBase {

function parseLifecycleRule(rule: LifecycleRule): CfnBucket.RuleProperty {
const enabled = rule.enabled ?? true;
if ((rule.expiredObjectDeleteMarker)
&& (rule.expiration || rule.expirationDate || self.parseTagFilters(rule.tagFilters))) {
// ExpiredObjectDeleteMarker cannot be specified with ExpirationInDays, ExpirationDate, or TagFilters.
throw new Error('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.');
}

const x: CfnBucket.RuleProperty = {
// eslint-disable-next-line max-len
Expand Down
45 changes: 45 additions & 0 deletions packages/aws-cdk-lib/aws-s3/test/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,51 @@ describe('rules', () => {
});
});

test('ExpiredObjectDeleteMarker cannot be specified with ExpirationInDays, ExpirationDate, or TagFilters.', () => {
const stack = new Stack();
new Bucket(stack, 'Bucket', {
lifecycleRules: [{
expiration: Duration.days(30),
expiredObjectDeleteMarker: true,
}],
});

expect(() => {
Template.fromStack(stack).toJSON();
}).toThrow('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.');
});

test('ExpiredObjectDeleteMarker cannot be specified with ExpirationInDays, ExpirationDate, or TagFilters.', () => {
const stack = new Stack();
new Bucket(stack, 'Bucket', {
lifecycleRules: [{
expirationDate: new Date('2018-01-01'),
expiredObjectDeleteMarker: true,
}],
});

expect(() => {
Template.fromStack(stack).toJSON();
}).toThrow('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.');
});

test('ExpiredObjectDeleteMarker cannot be specified with ExpirationInDays, ExpirationDate, or TagFilters.', () => {
const stack = new Stack();
new Bucket(stack, 'Bucket', {
lifecycleRules: [{
tagFilters: [
{ Key: 'tagname1', Value: 'tagvalue1' },
{ Key: 'tagname2', Value: 'tagvalue2' },
],
expiredObjectDeleteMarker: true,
}],
});

expect(() => {
Template.fromStack(stack).toJSON();
}).toThrow('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.');
});

test('Can use addLifecycleRule() to add a lifecycle rule', () => {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit a43967a

Please sign in to comment.