From 00c6312ee43aba75ca19e6f68b6014c08c62c0e8 Mon Sep 17 00:00:00 2001 From: robertd Date: Fri, 11 Feb 2022 22:34:59 -0700 Subject: [PATCH 1/2] fix(cloudfront): trim autogenerated cache policy name --- packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts | 9 +++++++-- packages/@aws-cdk/aws-cloudfront/lib/distribution.ts | 2 +- .../@aws-cdk/aws-cloudfront/test/cache-policy.test.ts | 7 ++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts b/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts index 533ccbd5d78b9..5236a86e72936 100644 --- a/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts +++ b/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts @@ -129,9 +129,14 @@ export class CachePolicy extends Resource implements ICachePolicy { physicalName: props.cachePolicyName, }); - const cachePolicyName = props.cachePolicyName ?? `${Names.uniqueId(this)}-${Stack.of(this).region}`; + const cachePolicyName = props.cachePolicyName ?? `${Names.uniqueId(this)}-${Stack.of(this).region}`.slice(0, 128); + if (!Token.isUnresolved(cachePolicyName) && !cachePolicyName.match(/^[\w-]+$/i)) { - throw new Error(`'cachePolicyName' can only include '-', '_', and alphanumeric characters, got: '${props.cachePolicyName}'`); + throw new Error(`'cachePolicyName' can only include '-', '_', and alphanumeric characters, got: '${cachePolicyName}'`); + } + + if (cachePolicyName.length > 128) { + throw new Error(`'cachePolicyName' cannot be longer than 128 characters, got: '${cachePolicyName.length}'`); } const minTtl = (props.minTtl ?? Duration.seconds(0)).toSeconds(); diff --git a/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts b/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts index c593edd9efec7..bd9fb1cb50202 100644 --- a/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts +++ b/packages/@aws-cdk/aws-cloudfront/lib/distribution.ts @@ -285,7 +285,7 @@ export class Distribution extends Resource implements IDistribution { // Comments have an undocumented limit of 128 characters const trimmedComment = props.comment && props.comment.length > 128 - ? `${props.comment.substr(0, 128 - 3)}...` + ? `${props.comment.slice(0, 128 - 3)}...` : props.comment; const distribution = new CfnDistribution(this, 'Resource', { diff --git a/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts b/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts index 47e9b0ee46fb9..2abeffd3cc39b 100644 --- a/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts +++ b/packages/@aws-cdk/aws-cloudfront/test/cache-policy.test.ts @@ -85,7 +85,12 @@ describe('CachePolicy', () => { }); }); - test('throws if given a cachePolicyName with invalid characters', () => { + test('throws on long policy names over 128 characters', () => { + const errorMessage = /'cachePolicyName' cannot be longer than 128 characters/; + expect(() => new CachePolicy(stack, 'CachePolicy1', { cachePolicyName: 'FooBarBaz'.repeat(15) })).toThrow(errorMessage); + }); + + test('throws if cachePolicyName contains invalid characters', () => { const errorMessage = /'cachePolicyName' can only include '-', '_', and alphanumeric characters/; expect(() => new CachePolicy(stack, 'CachePolicy1', { cachePolicyName: 'My Policy' })).toThrow(errorMessage); expect(() => new CachePolicy(stack, 'CachePolicy2', { cachePolicyName: 'MyPolicy!' })).toThrow(errorMessage); From bd65ae40a685bde22f41a7566f7e88171d90379f Mon Sep 17 00:00:00 2001 From: robertd Date: Sun, 20 Feb 2022 20:43:45 -0700 Subject: [PATCH 2/2] slice only uniqueId --- packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts b/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts index 5236a86e72936..e870a538fc887 100644 --- a/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts +++ b/packages/@aws-cdk/aws-cloudfront/lib/cache-policy.ts @@ -129,7 +129,7 @@ export class CachePolicy extends Resource implements ICachePolicy { physicalName: props.cachePolicyName, }); - const cachePolicyName = props.cachePolicyName ?? `${Names.uniqueId(this)}-${Stack.of(this).region}`.slice(0, 128); + const cachePolicyName = props.cachePolicyName ?? `${Names.uniqueId(this).slice(0, 110)}-${Stack.of(this).region}`; if (!Token.isUnresolved(cachePolicyName) && !cachePolicyName.match(/^[\w-]+$/i)) { throw new Error(`'cachePolicyName' can only include '-', '_', and alphanumeric characters, got: '${cachePolicyName}'`);