From 7b0f8ecb6f4ead350a57cd16fc7fa0e57358162e Mon Sep 17 00:00:00 2001 From: Otavio Macedo <288203+otaviomacedo@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:55:16 +0000 Subject: [PATCH 1/3] fix(cloudformation-include): string arrays inside unknown properties cannot be parsed --- .../non-existent-policy-attribute.json | 23 +++++++++++++++---- .../core/lib/helpers-internal/cfn-parse.ts | 8 +++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/non-existent-policy-attribute.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/non-existent-policy-attribute.json index a2f1c2b9e230c..ec1b6b655605f 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/non-existent-policy-attribute.json +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/non-existent-policy-attribute.json @@ -1,17 +1,32 @@ { + "AWSTemplateFormatVersion": "2010-09-09", "Resources": { - "ASG": { + "AutoScalingGroup": { "Type": "AWS::AutoScaling::AutoScalingGroup", "Properties": { + "DesiredCapacity": "1", "MinSize": "1", "MaxSize": "5" }, "CreationPolicy": { - "NonExistentResourceAttribute": "Bucket1" + "ResourceSignal": { + "Count": 1, + "Timeout": "PT10M" + } }, "UpdatePolicy": { - "NonExistentResourceAttribute": "Bucket1" + "AutoScalingRollingUpdate": { + "PauseTime": "PT10M", + "SuspendProcesses": [ + "HealthCheck", + "ReplaceUnhealthy", + "AZRebalance", + "AlarmNotification", + "ScheduledActions" + ], + "WaitOnResourceSignals": true + } } } } -} +} \ No newline at end of file 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..de56654e8ad8c 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 { @@ -233,7 +233,7 @@ export class FromCloudFormation { * Return a function that, when applied to a value, will return the first validly deserialized one */ public static getTypeUnion(validators: Validator[], mappers: Array<(x: any) => FromCloudFormationResult>): - (x: any) => FromCloudFormationResult { + (x: any) => FromCloudFormationResult { return (value: any) => { for (let i = 0; i < validators.length; i++) { const candidate = mappers[i](value); @@ -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; } From d53044ccd700c322050db7cf70771c13853b5b66 Mon Sep 17 00:00:00 2001 From: Otavio Macedo <288203+otaviomacedo@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:10:07 +0000 Subject: [PATCH 2/3] Space --- packages/aws-cdk-lib/core/lib/helpers-internal/cfn-parse.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 de56654e8ad8c..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 @@ -233,7 +233,7 @@ export class FromCloudFormation { * Return a function that, when applied to a value, will return the first validly deserialized one */ public static getTypeUnion(validators: Validator[], mappers: Array<(x: any) => FromCloudFormationResult>): - (x: any) => FromCloudFormationResult { + (x: any) => FromCloudFormationResult { return (value: any) => { for (let i = 0; i < validators.length; i++) { const candidate = mappers[i](value); From 9a24c5b892e6d9bf052d35355529050b58a784a8 Mon Sep 17 00:00:00 2001 From: Otavio Macedo <288203+otaviomacedo@users.noreply.github.com> Date: Wed, 11 Dec 2024 09:37:15 +0000 Subject: [PATCH 3/3] New test --- .../non-existent-policy-attribute.json | 23 +++---------- .../string-arrays-in-policy.json | 32 +++++++++++++++++++ .../test/valid-templates.test.ts | 7 ++++ 3 files changed, 43 insertions(+), 19 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/non-existent-policy-attribute.json b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/non-existent-policy-attribute.json index ec1b6b655605f..a2f1c2b9e230c 100644 --- a/packages/aws-cdk-lib/cloudformation-include/test/test-templates/non-existent-policy-attribute.json +++ b/packages/aws-cdk-lib/cloudformation-include/test/test-templates/non-existent-policy-attribute.json @@ -1,32 +1,17 @@ { - "AWSTemplateFormatVersion": "2010-09-09", "Resources": { - "AutoScalingGroup": { + "ASG": { "Type": "AWS::AutoScaling::AutoScalingGroup", "Properties": { - "DesiredCapacity": "1", "MinSize": "1", "MaxSize": "5" }, "CreationPolicy": { - "ResourceSignal": { - "Count": 1, - "Timeout": "PT10M" - } + "NonExistentResourceAttribute": "Bucket1" }, "UpdatePolicy": { - "AutoScalingRollingUpdate": { - "PauseTime": "PT10M", - "SuspendProcesses": [ - "HealthCheck", - "ReplaceUnhealthy", - "AZRebalance", - "AlarmNotification", - "ScheduledActions" - ], - "WaitOnResourceSignals": true - } + "NonExistentResourceAttribute": "Bucket1" } } } -} \ No newline at end of file +} 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();