forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindInMap.py
156 lines (137 loc) · 5.47 KB
/
FindInMap.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from cfnlint.rules import CloudFormationLintRule
from cfnlint.rules import RuleMatch
class FindInMap(CloudFormationLintRule):
"""Check if FindInMap values are correct"""
id = 'E1011'
shortdesc = 'FindInMap validation of configuration'
description = 'Making sure the function is a list of appropriate config'
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html'
tags = ['functions', 'findinmap']
supported_functions = ['Fn::FindInMap', 'Ref']
def check_dict(self, obj, tree):
"""
Check that obj is a dict with Ref as the only key
Mappings only support Ref inside them
"""
matches = []
if isinstance(obj, dict):
if len(obj) == 1:
for key_name, _ in obj.items():
if key_name not in self.supported_functions:
message = 'FindInMap only supports [{0}] functions at {1}'
matches.append(
RuleMatch(
tree[:] + [key_name],
message.format(
', '.join(map(str, self.supported_functions)),
'/'.join(map(str, tree)),
),
)
)
else:
message = (
'FindInMap only supports an object of 1 of [{0}] functions at {1}'
)
matches.append(
RuleMatch(
tree[:],
message.format(
', '.join(map(str, self.supported_functions)),
'/'.join(map(str, tree)),
),
)
)
return matches
def map_name(self, map_name, mappings, tree):
"""Check the map name"""
matches = []
if isinstance(map_name, (str, dict)):
if isinstance(map_name, dict):
matches.extend(self.check_dict(map_name, tree[:] + [0]))
else:
if map_name not in mappings:
message = 'Map Name {0} does not exist for {0}'
matches.append(
RuleMatch(
tree[:] + [0],
message.format(map_name, '/'.join(map(str, tree))),
)
)
else:
message = 'Map Name should be a {0}, or string at {1}'
matches.append(
RuleMatch(
tree[:] + [0],
message.format(
', '.join(map(str, self.supported_functions)),
'/'.join(map(str, tree)),
),
)
)
return matches
def first_key(self, first_key, tree):
"""Check the validity of the first key"""
matches = []
if isinstance(first_key, (str, int)):
return matches
if isinstance(first_key, (dict)):
matches.extend(self.check_dict(first_key, tree[:] + [1]))
else:
message = 'FindInMap first key should be a {0}, string, or int at {1}'
matches.append(
RuleMatch(
tree[:] + [1],
message.format(
', '.join(map(str, self.supported_functions)),
'/'.join(map(str, tree)),
),
)
)
return matches
def second_key(self, second_key, tree):
"""Check the validity of the second key"""
matches = []
if isinstance(second_key, (str, int)):
return matches
if isinstance(second_key, (dict)):
matches.extend(self.check_dict(second_key, tree[:] + [2]))
else:
message = 'FindInMap second key should be a {0}, string, or int at {1}'
matches.append(
RuleMatch(
tree[:] + [2],
message.format(
', '.join(map(str, self.supported_functions)),
'/'.join(map(str, tree)),
),
)
)
return matches
def match(self, cfn):
matches = []
findinmaps = cfn.search_deep_keys('Fn::FindInMap')
mappings = cfn.get_mappings()
for findinmap in findinmaps:
tree = findinmap[:-1]
map_obj = findinmap[-1]
if not isinstance(map_obj, list):
message = 'FindInMap is a list with 3 values for {0}'
matches.append(RuleMatch(tree[:], message.format('/'.join(tree))))
continue
if len(map_obj) == 3:
map_name = map_obj[0]
first_key = map_obj[1]
second_key = map_obj[2]
matches.extend(self.map_name(map_name, mappings, tree))
matches.extend(self.first_key(first_key, tree))
matches.extend(self.second_key(second_key, tree))
else:
message = 'FindInMap is a list with 3 values for {0}'
matches.append(
RuleMatch(tree[:] + [1], message.format('/'.join(map(str, tree))))
)
return matches