3
3
SPDX-License-Identifier: MIT-0
4
4
"""
5
5
import six
6
+ from cfnlint .helpers import VALID_PARAMETER_TYPES_LIST
6
7
from cfnlint .rules import CloudFormationLintRule
7
8
from cfnlint .rules import RuleMatch
8
9
@@ -15,53 +16,70 @@ class Equals(CloudFormationLintRule):
15
16
source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-equals'
16
17
tags = ['functions' , 'equals' ]
17
18
19
+ allowed_functions = ['Ref' , 'Fn::FindInMap' ,
20
+ 'Fn::Sub' , 'Fn::Join' , 'Fn::Select' , 'Fn::Split' ]
21
+ function = 'Fn::Equals'
22
+
23
+ def _check_equal_values (self , cfn , element , path ):
24
+ matches = []
25
+
26
+ if len (element ) == 1 :
27
+ for element_key , element_value in element .items ():
28
+ if element_key not in self .allowed_functions :
29
+ message = self .function + \
30
+ ' element must be a supported function ({0})'
31
+ matches .append (RuleMatch (
32
+ path [:] + [element_key ],
33
+ message .format (', ' .join (self .allowed_functions ))
34
+ ))
35
+ elif element_key == 'Ref' :
36
+ valid_refs = cfn .get_valid_refs ()
37
+ if element_value in valid_refs :
38
+ if valid_refs [element_value ].get ('From' ) == 'Parameters' :
39
+ if valid_refs [element_value ].get ('Type' ) in VALID_PARAMETER_TYPES_LIST :
40
+ message = 'Every Fn::Equals object requires a list of 2 string parameters'
41
+ matches .append (RuleMatch (
42
+ path , message ))
43
+ else :
44
+ message = self .function + ' element must be a supported function ({0})'
45
+ matches .append (RuleMatch (
46
+ path ,
47
+ message .format (', ' .join (self .allowed_functions ))
48
+ ))
49
+ return matches
50
+
18
51
def match (self , cfn ):
19
- function = 'Fn::Equals'
20
52
matches = []
21
53
# Build the list of functions
22
- trees = cfn .search_deep_keys (function )
23
-
24
- allowed_functions = ['Ref' , 'Fn::FindInMap' ,
25
- 'Fn::Sub' , 'Fn::Join' , 'Fn::Select' , 'Fn::Split' ]
54
+ trees = cfn .search_deep_keys (self .function )
26
55
27
56
for tree in trees :
28
57
# Test when in Conditions
29
58
if tree [0 ] == 'Conditions' :
30
59
value = tree [- 1 ]
31
60
if not isinstance (value , list ):
32
- message = function + ' must be a list of two elements'
61
+ message = self . function + ' must be a list of two elements'
33
62
matches .append (RuleMatch (
34
63
tree [:- 1 ],
35
64
message .format ()
36
65
))
37
66
elif len (value ) != 2 :
38
- message = function + ' must be a list of two elements'
67
+ message = self . function + ' must be a list of two elements'
39
68
matches .append (RuleMatch (
40
69
tree [:- 1 ],
41
70
message .format ()
42
71
))
43
72
else :
44
73
for index , element in enumerate (value ):
45
74
if isinstance (element , dict ):
46
- if len (element ) == 1 :
47
- for element_key in element .keys ():
48
- if element_key not in allowed_functions :
49
- message = function + ' element must be a supported function ({0})'
50
- matches .append (RuleMatch (
51
- tree [:- 1 ] + [index , element_key ],
52
- message .format (', ' .join (allowed_functions ))
53
- ))
54
- else :
55
- message = function + ' element must be a supported function ({0})'
56
- matches .append (RuleMatch (
57
- tree [:- 1 ] + [index ],
58
- message .format (', ' .join (allowed_functions ))
59
- ))
75
+ matches .extend (self ._check_equal_values (
76
+ cfn , element , tree [:- 1 ] + [index ]))
60
77
elif not isinstance (element , (six .string_types , bool , six .integer_types , float )):
61
- message = function + ' element must be a String, Boolean, Number, or supported function ({0})'
78
+ message = self .function + \
79
+ ' element must be a String, Boolean, Number, or supported function ({0})'
62
80
matches .append (RuleMatch (
63
81
tree [:- 1 ] + [index ],
64
- message .format (', ' .join (allowed_functions ))
82
+ message .format (', ' .join (self . allowed_functions ))
65
83
))
66
84
67
85
return matches
0 commit comments