From 7803e066b1e9b47112433a0596139a5ed63faf34 Mon Sep 17 00:00:00 2001 From: "Kenta Goto (k.goto)" <24818752+go-to-k@users.noreply.github.com> Date: Fri, 30 Aug 2024 03:34:08 +0900 Subject: [PATCH] chore(cloudfront): add validation for length of comment in cache policy (#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* --- packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts | 7 +++++++ .../aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts b/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts index ab123d9949f54..c9e1e94af0a00 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts @@ -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; @@ -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(); diff --git a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts index bd30650bc0ea5..44bfd52d19c84 100644 --- a/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts +++ b/packages/aws-cdk-lib/aws-cloudfront/test/cache-policy.test.ts @@ -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' });