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

chore(s3): add validation on required properties for lifecycle rules #31806

Merged
merged 4 commits into from
Oct 18, 2024

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Oct 18, 2024

Issue # (if applicable)

Closes #.

Reason for this change

If there is a lifecycle rule that does not contain one of the specified properties, an error is raised.

const app = new App();
const stack = new Stack(app, 'aws-cdk-s3');

// An error occurs
new Bucket(stack, 'MyBucket', {
  lifecycleRules: [
    // is invalid
    {
      objectSizeLessThan: 300000,
      objectSizeGreaterThan: 200000,
    },
  ],
});
const app = new App();
const stack = new Stack(app, 'aws-cdk-s3');

// An error occurs
new Bucket(stack, 'MyBucket', {
  lifecycleRules: [
    // is valid
    {
      abortIncompleteMultipartUploadAfter: Duration.days(365),
    },
    // is invalid
    {
      objectSizeLessThan: 300000,
      objectSizeGreaterThan: 200000,
    },
  ],
});

A CFn message:

Invalid request provided: At least one of [ExpirationDate,ExpirationInDays,AbortIncompleteMultipartUpload,Transition,Transitions,NoncurrentVersionExpirationInDays,NoncurrentVersionTransition,NoncurrentVersionTransitions,NoncurrentVersionExpiration,ExpiredObjectDeleteMarker] needs to be specified

The properties in CFn properties:

  • AbortIncompleteMultipartUpload
  • ExpirationDate
  • ExpirationInDays
  • ExpiredObjectDeleteMarker
  • NoncurrentVersionExpirationInDays
  • NoncurrentVersionTransition
  • NoncurrentVersionTransitions
  • NoncurrentVersionExpiration
  • Transition
  • Transitions

The properties in L2 props:

  • abortIncompleteMultipartUploadAfter
  • expiration
  • expirationDate
  • expiredObjectDeleteMarker
  • noncurrentVersionExpiration
  • noncurrentVersionsToRetain
  • noncurrentVersionTransitions
  • transitions

Description of changes

Check whether a rule has required properties in lifecycleRules for L2 BucketProps.

     if (
        rule.abortIncompleteMultipartUploadAfter === undefined &&
        rule.expiration === undefined &&
        rule.expirationDate === undefined &&
        rule.expiredObjectDeleteMarker === undefined &&
        rule.noncurrentVersionExpiration === undefined &&
        rule.noncurrentVersionsToRetain === undefined &&
        rule.noncurrentVersionTransitions === undefined &&
        rule.transitions === undefined
      ) {
        throw new Error('All rules for `lifecycleRules` must have at least one of the following properties: `abortIncompleteMultipartUploadAfter`, `expiration`, `expirationDate`, `expiredObjectDeleteMarker`, `noncurrentVersionExpiration`, `noncurrentVersionsToRetain`, `noncurrentVersionTransitions`, or `transitions`');
      }

Description of how you validated changes

Unit tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team October 18, 2024 08:18
@github-actions github-actions bot added distinguished-contributor [Pilot] contributed 50+ PRs to the CDK p2 labels Oct 18, 2024
@go-to-k go-to-k marked this pull request as ready for review October 18, 2024 09:32
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Oct 18, 2024
Comment on lines +351 to +352
expect(() => {
Template.fromStack(stack);
Copy link
Contributor Author

@go-to-k go-to-k Oct 18, 2024

Choose a reason for hiding this comment

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

An error occurs in synthesize phase because the rendering is done as Lazy:

https://github.com/aws/aws-cdk/blob/v2.162.1/packages/aws-cdk-lib/aws-s3/lib/bucket.ts#L1941

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: c420097
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@@ -2254,6 +2254,19 @@ export class Bucket extends BucketBase {
throw new Error('ExpiredObjectDeleteMarker cannot be specified with expiration, ExpirationDate, or TagFilters.');
}

if (
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the PR @go-to-k and the unit testing - I'm now wondering how we missed this validation in the first place.

This is certainly a big if statement here, but I suppose it has to be for this validation. Trying to think of other ways to format this ...

const ruleProperties = [
  rule.abortIncompleteMultipartUploadAfter,
  rule.expiration,
  rule.expirationDate,
  rule.expiredObjectDeleteMarker,
  rule.noncurrentVersionExpiration,
  rule.noncurrentVersionsToRetain,
  rule.noncurrentVersionTransitions,
  rule.transitions,
];

if (ruleProperties.every(prop => prop === undefined)) {
  throw new Error('All rules for `lifecycleRules` must have at least one of the following properties: `abortIncompleteMultipartUploadAfter`, `expiration`, `expirationDate`, `expiredObjectDeleteMarker`, `noncurrentVersionExpiration`, `noncurrentVersionsToRetain`, `noncurrentVersionTransitions`, or `transitions`');
}

Approving this PR 👍

Copy link
Contributor

mergify bot commented Oct 18, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit e15626e into aws:main Oct 18, 2024
11 checks passed
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 18, 2024
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Oct 18, 2024
@go-to-k go-to-k deleted the s3-validation-rules branch October 19, 2024 02:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants