Skip to content

Commit

Permalink
fix(yaml-cfn): do not deserialize year-month-date as strings
Browse files Browse the repository at this point in the history
Currently, we are using the `yaml-1.1` schema when de-serializing YAML documents,
Unfortunately, this has the side effect of treating unquoted parts of the template like '2010-09-09'
as Date objects, instead of just simple strings.
This has been noted by a customer using the cloudformation-include module,
but I assume a very similar problem exists for other places we parse YAML,
like cloudformation-diff.

Switch to the `core` schema from `yaml-1.1`,
where those are treated as strings, instead of dates.

Fixes #13709
  • Loading branch information
skinny85 committed Mar 26, 2021
1 parent 6743e3b commit 40f8067
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
AWSTemplateFormatVersion: 2010-09-09
Resources:
Role:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ describe('CDK Include', () => {
);
});

test('can ingest a template with year-month-date parsed as string instead of Date', () => {
includeTestTemplate(stack, 'year-month-date-as-strings.yaml');

expect(stack).toMatchTemplate({
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": ["ec2.amazonaws.com"],
},
"Action": ["sts:AssumeRole"],
},
],
},
},
},
},
});
});

test('can ingest a template with the short form Base64 function', () => {
includeTestTemplate(stack, 'short-form-base64.yaml');

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/yaml-cfn/lib/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ const shortForms: yaml_types.Schema.CustomTag[] = [
function parseYamlStrWithCfnTags(text: string): any {
return yaml.parse(text, {
customTags: shortForms,
schema: 'yaml-1.1',
schema: 'core',
});
}
34 changes: 34 additions & 0 deletions packages/@aws-cdk/yaml-cfn/test/deserialization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import '@aws-cdk/assert/jest';
import * as yaml_cfn from '../lib';

test('Unquoted year-month-day is treated as a string, not a Date', () => {
const value = yaml_cfn.deserialize('Key: 2020-12-31');

expect(value).toEqual({
Key: '2020-12-31',
});
});

test("Unquoted 'No' is treated as a string, not a boolean", () => {
const value = yaml_cfn.deserialize('Key: No');

expect(value).toEqual({
Key: 'No',
});
});

test("Short-form 'Ref' is deserialized correctly", () => {
const value = yaml_cfn.deserialize('!Ref Resource');

expect(value).toEqual({
Ref: 'Resource',
});
});

test("Short-form 'Fn::GetAtt' is deserialized correctly", () => {
const value = yaml_cfn.deserialize('!GetAtt Resource.Attribute');

expect(value).toEqual({
'Fn::GetAtt': 'Resource.Attribute',
});
});
8 changes: 8 additions & 0 deletions packages/@aws-cdk/yaml-cfn/test/serialization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import '@aws-cdk/assert/jest';
import * as yaml_cfn from '../lib';

test('An object with a single string value is serialized as a simple string', () => {
const value = yaml_cfn.serialize({ key: 'some string' });

expect(value).toEqual('key: some string\n');
});
5 changes: 0 additions & 5 deletions packages/@aws-cdk/yaml-cfn/test/yaml.test.ts

This file was deleted.

0 comments on commit 40f8067

Please sign in to comment.