forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualsIsUseful.py
More file actions
44 lines (34 loc) · 1.65 KB
/
EqualsIsUseful.py
File metadata and controls
44 lines (34 loc) · 1.65 KB
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
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import json
from cfnlint.rules import CloudFormationLintRule, RuleMatch
class EqualsIsUseful(CloudFormationLintRule):
"""Validate that the Equals will return true/false and not always be true or false"""
id = "W8003"
shortdesc = "Fn::Equals will always return true or false"
description = "Validate Fn::Equals to see if its comparing two strings or two equal items. While this works it may not be intended."
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#intrinsic-function-reference-conditions-equals"
tags = ["functions", "equals"]
function = "Fn::Equals"
def _check_equal_values(self, values, path):
matches = []
if json.dumps(values[0]) == json.dumps(values[1]):
message = self.function + " element will alway return true"
matches.append(RuleMatch(path, message))
elif isinstance(values[0], str) and isinstance(values[1], str):
message = self.function + " element will alway return false"
matches.append(RuleMatch(path, message))
return matches
def match(self, cfn):
matches = []
# Build the list of functions
trees = cfn.search_deep_keys(self.function)
for tree in trees:
# Test when in Conditions
if tree[0] == "Conditions":
value = tree[-1]
if isinstance(value, list) and len(value) == 2:
matches.extend(self._check_equal_values(value, tree[:-1]))
return matches