From 0c2f98b00f70bae8995f0a593e1853e93b3fb706 Mon Sep 17 00:00:00 2001 From: Otavio Macedo <288203+otaviomacedo@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:49:01 +0000 Subject: [PATCH] fix(cloudformation-include): string arrays inside unknown properties cannot be parsed (#32461) We are using `this` to refer to static methods, which fails at runtime. Use the class name instead. Fixes https://github.com/aws/aws-cdk/issues/32454. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../string-arrays-in-policy.json | 32 +++++++++++++++++++ .../test/valid-templates.test.ts | 7 ++++ .../core/lib/helpers-internal/cfn-parse.ts | 6 ++-- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 packages/aws-cdk-lib/cloudformation-include/test/test-templates/string-arrays-in-policy.json diff --git a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/string-arrays-in-policy.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/string-arrays-in-policy.json new file mode 100644 index 0000000000000..ec1b6b655605f --- /dev/null +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/string-arrays-in-policy.json @@ -0,0 +1,32 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Resources": { + "AutoScalingGroup": { + "Type": "AWS::AutoScaling::AutoScalingGroup", + "Properties": { + "DesiredCapacity": "1", + "MinSize": "1", + "MaxSize": "5" + }, + "CreationPolicy": { + "ResourceSignal": { + "Count": 1, + "Timeout": "PT10M" + } + }, + "UpdatePolicy": { + "AutoScalingRollingUpdate": { + "PauseTime": "PT10M", + "SuspendProcesses": [ + "HealthCheck", + "ReplaceUnhealthy", + "AZRebalance", + "AlarmNotification", + "ScheduledActions" + ], + "WaitOnResourceSignals": true + } + } + } + } +} \ No newline at end of file diff --git a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts index 7332314f0576f..b8aa19871b8eb 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts +++ b/packages/aws-cdk-lib/cloudformation-include/test/valid-templates.test.ts @@ -714,6 +714,13 @@ describe('CDK Include', () => { ); }); + test('correctly handles string arrays in policy attributes', () => { + const cfnTemplate = includeTestTemplate(stack, 'string-arrays-in-policy.json'); + Template.fromStack(stack).templateMatches( + loadTestFileToJsObject('string-arrays-in-policy.json'), + ); + }); + test("correctly handles referencing the ingested template's resources across Stacks", () => { // for cross-stack sharing to work, we need an App const app = new core.App(); diff --git a/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts b/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts index ccdf76efb2240..28055405bb313 100644 --- a/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts +++ b/packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts @@ -173,7 +173,7 @@ export class FromCloudFormation { } // in all other cases, delegate to the standard mapping logic - return this.getArray(this.getString)(value); + return FromCloudFormation.getArray(FromCloudFormation.getString)(value); } public static getArray(mapper: (arg: any) => FromCloudFormationResult): (x: any) => FromCloudFormationResult { @@ -716,8 +716,8 @@ export class CfnParser { const key = objectKeys[0]; return key === 'Ref' || key.startsWith('Fn::') || - // special intrinsic only available in the 'Conditions' section - (this.options.context === CfnParsingContext.CONDITIONS && key === 'Condition') + // special intrinsic only available in the 'Conditions' section + (this.options.context === CfnParsingContext.CONDITIONS && key === 'Condition') ? key : undefined; }