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. (#25841)

Closes #25824.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Zishanwang1992 authored Jun 14, 2023
1 parent c135656 commit 1a82d85
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 @@ -2119,6 +2119,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.', () => {
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 ExpirationDate.', () => {
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 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 1a82d85

Please sign in to comment.