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 fast for s3 lifecycle configuration when ExpiredObjectDeleteMarker specified with ExpirationInDays, ExpirationDate, or TagFilters. #25841

Merged
merged 3 commits into from
Jun 14, 2023
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests for all the combinations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add different combinations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@corymhall Could you help review this PR?

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