forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedStackParameters.py
95 lines (80 loc) · 4.32 KB
/
NestedStackParameters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import os
import six
from cfnlint.decode import decode
from cfnlint.rules import CloudFormationLintRule
from cfnlint.rules import RuleMatch
class NestedStackParameters(CloudFormationLintRule):
"""Check that nested stack parameters are specified as needed"""
id = 'E3043'
shortdesc = 'Validate parameters for in a nested stack'
description = 'Evalute if parameters for a nested stack are specified and ' \
'if parameters are specified for a nested stack that aren\'t required.'
source_url = 'https://github.com/awslabs/cfn-python-lint'
tags = ['resources', 'cloudformation']
def __init__(self):
"""Init"""
super(NestedStackParameters, self).__init__()
self.resource_property_types.append('AWS::CloudFormation::Stack')
def __get_template_parameters(self, filename):
try:
(tmp, matches) = decode(filename)
except: #pylint: disable=bare-except
return None
if matches:
return None
return tmp.get('Parameters', {})
def __compare_objects(self, template_parameters, nested_parameters, scenario, path):
matches = []
for key in set(template_parameters.keys()) - set(nested_parameters.keys()):
if scenario is None:
message = 'Specified parameter "{0}" doesn\'t exist in nested stack template at {1}'
matches.append(
RuleMatch(path + [key], message.format(key, '/'.join(map(str, path + [key])))))
else:
message = 'Specified parameter "{0}" doesn\'t exist in nested stack template {1}'
scenario_text = ' and '.join(
['when condition "%s" is %s' % (k, v) for (k, v) in scenario.items()])
matches.append(
RuleMatch(path, message.format(key, scenario_text)))
for key in set(nested_parameters.keys())- set(template_parameters.keys()):
if nested_parameters.get(key).get('Default') is None:
if scenario is None:
message = 'Nested stack template parameter "{0}" is not specified at {1}'
matches.append(
RuleMatch(path, message.format(key, '/'.join(map(str, path)))))
else:
message = 'Nested stack template parameter "{0}" is not specified {1}'
scenario_text = ' and '.join(
['when condition "%s" is %s' % (k, v) for (k, v) in scenario.items()])
matches.append(
RuleMatch(path, message.format(key, scenario_text)))
return matches
def match_resource_properties(self, properties, _, path, cfn):
"""Check CloudFormation Properties"""
matches = []
# when template is passed via cat (or equivalent filename is none)
if cfn.filename:
base_dir = os.path.dirname(os.path.abspath(cfn.filename))
parameter_groups = cfn.get_object_without_conditions(obj=properties, property_names=['TemplateURL', 'Parameters'])
for parameter_group in parameter_groups:
obj = parameter_group.get('Object')
template_url = obj.get('TemplateURL')
if isinstance(template_url, six.string_types):
if not (template_url.startswith('http://') or template_url.startswith('https://') or template_url.startswith('s3://')):
template_path = os.path.normpath(os.path.join(base_dir, template_url))
nested_parameters = self.__get_template_parameters(template_path)
template_parameters = obj.get('Parameters')
if isinstance(nested_parameters, dict) and isinstance(template_parameters, dict):
matches.extend(
self.__compare_objects(
template_parameters=template_parameters,
nested_parameters=nested_parameters,
path=path + ['Parameters'],
scenario=parameter_group.get('Scenario'),
)
)
return matches