Skip to content

Commit 0232e66

Browse files
authored
Handle CFN template values being represented as symbols (#100)
Psych will interpret unquoted YAML values starting with a ':' character as a Ruby symbol. Avoid raising an error in this scenario, add Symbol to the list of permitted classes when loading CloudFormation YAML templates.
1 parent 674e223 commit 0232e66

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

lib/cfn-model/parser/cfn_parser.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def parse_without_parameters(cloudformation_yml, with_line_numbers=false, condit
6565
if with_line_numbers
6666
parse_with_line_numbers(cloudformation_yml)
6767
else
68-
YAML.safe_load cloudformation_yml, permitted_classes: [Date]
68+
YAML.safe_load cloudformation_yml, permitted_classes: [Date, Symbol]
6969
end
7070

7171
# Transform raw resources in template as performed by

lib/cfn-model/validator/cloudformation_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def validate(cloudformation_string)
1212

1313
schema = SchemaGenerator.new.generate cloudformation_string
1414
validator = Kwalify::Validator.new(schema)
15-
validator.validate(YAML.safe_load(cloudformation_string, permitted_classes: [Date]))
15+
validator.validate(YAML.safe_load(cloudformation_string, permitted_classes: [Date, Symbol]))
1616
rescue ArgumentError, IOError, NameError => e
1717
raise ParserError, e.inspect
1818
end

lib/cfn-model/validator/resource_type_validator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class ResourceTypeValidator
66

77
def self.validate(cloudformation_yml)
8-
hash = YAML.safe_load cloudformation_yml, permitted_classes: [Date]
8+
hash = YAML.safe_load cloudformation_yml, permitted_classes: [Date, Symbol]
99
if hash == false || hash.nil?
1010
raise ParserError.new 'yml empty'
1111
end

spec/parser/cfn_parser_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@
271271
Type: String
272272
NoEcho: true
273273
Default: none
274+
TestIPV6:
275+
Type: String
276+
Default: ::/0
274277
Conditions:
275278
IsNone: !Or
276279
- !Equals

spec/validator/cloudformation_validator_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,23 @@
176176

177177
expect(CloudFormationValidator.new.validate(valid_yaml)).to eq []
178178
end
179+
180+
it 'does not raise an error when template is YAML with date and symbol values' do
181+
valid_yaml = <<~TEMPLATE
182+
---
183+
AWSTemplateFormatVersion: 2010-09-09
184+
Resources:
185+
SecurityGroupIngress:
186+
Type: AWS::EC2::SecurityGroupIngress
187+
Properties:
188+
GroupId: sg-12341234
189+
CidrIpv6: ::/0
190+
FromPort: 22
191+
ToPort: 22
192+
IpProtocol: tcp
193+
TEMPLATE
194+
195+
expect(CloudFormationValidator.new.validate(valid_yaml)).to eq []
196+
end
179197
end
180198
end

spec/validator/resource_type_validator_spec.rb

+38
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,42 @@
123123
expect(actual_hash).to eq expected_hash
124124
end
125125
end
126+
127+
context 'given a template with date and symbol values' do
128+
let(:template) { <<~TEMPLATE }
129+
---
130+
AWSTemplateFormatVersion: 2010-09-09
131+
Resources:
132+
SecurityGroupIngress:
133+
Type: AWS::EC2::SecurityGroupIngress
134+
Properties:
135+
GroupId: sg-12341234
136+
CidrIpv6: ::/0
137+
FromPort: 22
138+
ToPort: 22
139+
IpProtocol: tcp
140+
TEMPLATE
141+
142+
it 'successfully returns the Hash of the parsed document' do
143+
parsed_template = ResourceTypeValidator.validate(template)
144+
145+
expect(parsed_template).to eq(
146+
{
147+
'AWSTemplateFormatVersion' => Date.new(2010, 9, 9),
148+
'Resources' => {
149+
'SecurityGroupIngress' => {
150+
'Type' => 'AWS::EC2::SecurityGroupIngress',
151+
'Properties' => {
152+
'GroupId' => 'sg-12341234',
153+
'CidrIpv6' => :':/0',
154+
'FromPort' => 22,
155+
'ToPort' => 22,
156+
'IpProtocol' => 'tcp'
157+
}
158+
}
159+
}
160+
}
161+
)
162+
end
163+
end
126164
end

0 commit comments

Comments
 (0)