Skip to content

Commit

Permalink
chore(cloudfront): add validation for length of comment in cache poli…
Browse files Browse the repository at this point in the history
…cy (#31251)

### Issue # (if applicable)

Closes #31248 .

### Reason for this change



CDK doesn't validate the comment's length in the cache policy now.

### Description of changes



Add validation for the length.

### Description of how you validated changes



unit tests.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
go-to-k authored Aug 29, 2024
1 parent 04d73ac commit 7803e06
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export interface CachePolicyProps {

/**
* A comment to describe the cache policy.
*
* The comment cannot be longer than 128 characters.
*
* @default - no comment
*/
readonly comment?: string;
Expand Down Expand Up @@ -149,6 +152,10 @@ export class CachePolicy extends Resource implements ICachePolicy {
throw new Error(`'cachePolicyName' cannot be longer than 128 characters, got: '${cachePolicyName.length}'`);
}

if (props.comment && !Token.isUnresolved(props.comment) && props.comment.length > 128) {
throw new Error(`'comment' cannot be longer than 128 characters, got: ${props.comment.length}`);
}

const minTtl = (props.minTtl ?? Duration.seconds(0)).toSeconds();
let defaultTtl = (props.defaultTtl ?? Duration.days(1)).toSeconds();
let maxTtl = (props.maxTtl ?? Duration.days(365)).toSeconds();
Expand Down
5 changes: 5 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ describe('CachePolicy', () => {
})).not.toThrow();
});

test('throws on long comment over 128 characters', () => {
const errorMessage = /'comment' cannot be longer than 128 characters, got: 129/;
expect(() => new CachePolicy(stack, 'CachePolicy1', { comment: 'a'.repeat(129) })).toThrow(errorMessage);
});

describe('TTLs', () => {
test('default TTLs', () => {
new CachePolicy(stack, 'CachePolicy', { cachePolicyName: 'MyPolicy' });
Expand Down

0 comments on commit 7803e06

Please sign in to comment.