Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic Match/Project/Validate pattern rule #2672

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/custom_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ The specified operator to be used for this rule. The supported values are define
| == | Identical to `EQUALS` |
| NOT_EQUALS | Checks the specified property is not equal to the value given |
| != | Identical to `NOT_EQUALS` |
| REGEX_MATCH | Checks the specified property matches regex given (using python `regex` module) |
| IN | Checks the specified property is equal to or contained by the array value |
| NOT_IN | Checks the specified property is not equal to or not contained by the array value |
| \>= | Checks the specified property is greater than or equal to the value given |
| \> | Checks the specified property is greater than the value given |
| < | Checks the specified property is less than the value given |
| <= | Checks the specified property is less than or equal to the value given |
| IS | Checks the specified property is defined or not defined, the value must be one of DEFINED or NOT_DEFINED |

Expand Down
99 changes: 96 additions & 3 deletions src/cfnlint/rules/custom/Operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
SPDX-License-Identifier: MIT-0
"""

import regex as re

# pylint: disable=cyclic-import
import cfnlint.rules

OPERATOR = [
"EQUALS",
"NOT_EQUALS",
"REGEX_MATCH",
"==",
"!=",
"IN",
"NOT_IN",
">",
"<",
">=",
"<=",
"IS DEFINED",
Expand Down Expand Up @@ -298,7 +303,29 @@ def rule_func(value, expected_values, path):
)


def CreateGreaterRule(rule_id, resourceType, prop, value, error_message):
def CreateRegexMatchRule(rule_id, resourceType, prop, value, error_message):
def rule_func(value, expected_values, path):
matches = []
if not re.match(expected_values.strip(), str(value).strip()):
matches.append(
cfnlint.rules.RuleMatch(path, error_message or "Regex does not match")
)

return matches

return CreateCustomRule(
rule_id,
resourceType,
prop,
value,
error_message,
shortdesc="Custom rule to check for regex match",
description="Created from the custom rules parameter. This rule will check if a property value match the provided regex pattern.",
rule_func=rule_func,
)


def CreateGreaterEqualRule(rule_id, resourceType, prop, value, error_message):
def rule_func(value, expected_value, path):
matches = []
if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
Expand Down Expand Up @@ -329,7 +356,69 @@ def rule_func(value, expected_value, path):
)


def CreateGreaterRule(rule_id, resourceType, prop, value, error_message):
def rule_func(value, expected_value, path):
matches = []
if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
if int(str(value).strip()) <= int(str(expected_value).strip()):
matches.append(
cfnlint.rules.RuleMatch(
path, error_message or "Greater than check failed"
)
)
else:
matches.append(
cfnlint.rules.RuleMatch(
path, error_message or "Given values are not numeric"
)
)

return matches

return CreateCustomRule(
rule_id,
resourceType,
prop,
value,
error_message,
shortdesc="Custom rule to check for if a value is greater than the specified value",
description="Created from the custom rules parameter. This rule will check if a property value is greater than the specified value.",
rule_func=rule_func,
)


def CreateLesserRule(rule_id, resourceType, prop, value, error_message):
def rule_func(value, expected_value, path):
matches = []
if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
if int(str(value).strip()) >= int(str(expected_value).strip()):
matches.append(
cfnlint.rules.RuleMatch(
path, error_message or "Lesser than check failed"
)
)
else:
matches.append(
cfnlint.rules.RuleMatch(
path, error_message or "Given values are not numeric"
)
)

return matches

return CreateCustomRule(
rule_id,
resourceType,
prop,
value,
error_message,
shortdesc="Custom rule to check for if a value is lesser than the specified value",
description="Created from the custom rules parameter. This rule will check if a property value is lesser than the specified value.",
rule_func=rule_func,
)


def CreateLesserEqualRule(rule_id, resourceType, prop, value, error_message):
def rule_func(value, expected_value, path):
matches = []
if checkInt(str(value).strip()) and checkInt(str(expected_value).strip()):
Expand Down Expand Up @@ -363,7 +452,9 @@ def rule_func(value, expected_value, path):
def CreateInSetRule(rule_id, resourceType, prop, value, error_message):
def rule_func(value, expected_values, path):
matches = []
if value not in expected_values:
if isinstance(expected_values, list):
expected_values = [str(x) for x in expected_values]
if str(value) not in expected_values:
matches.append(
cfnlint.rules.RuleMatch(path, error_message or "In set check failed")
)
Expand All @@ -385,7 +476,9 @@ def rule_func(value, expected_values, path):
def CreateNotInSetRule(rule_id, resourceType, prop, value, error_message):
def rule_func(value, expected_values, path):
matches = []
if value in expected_values:
if isinstance(expected_values, list):
expected_values = [str(x) for x in expected_values]
if str(value) in expected_values:
matches.append(
cfnlint.rules.RuleMatch(
path, error_message or "Not in set check failed"
Expand Down
16 changes: 14 additions & 2 deletions src/cfnlint/rules/custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def process_sets(raw_value):
return cfnlint.rules.custom.Operators.CreateNotEqualsRule(
error_level + str(rule_id), resourceType, prop, value, error_message
)
if operator == "REGEX_MATCH":
return cfnlint.rules.custom.Operators.CreateRegexMatchRule(
error_level + str(rule_id), resourceType, prop, value, error_message
)
if operator == "IN":
return cfnlint.rules.custom.Operators.CreateInSetRule(
error_level + str(rule_id), resourceType, prop, value, error_message
Expand All @@ -80,14 +84,22 @@ def process_sets(raw_value):
return cfnlint.rules.custom.Operators.CreateNotInSetRule(
error_level + str(rule_id), resourceType, prop, value, error_message
)
if operator == ">=":
if operator == ">":
return cfnlint.rules.custom.Operators.CreateGreaterRule(
error_level + str(rule_id), resourceType, prop, value, error_message
)
if operator == "<=":
if operator == ">=":
return cfnlint.rules.custom.Operators.CreateGreaterEqualRule(
error_level + str(rule_id), resourceType, prop, value, error_message
)
if operator == "<":
return cfnlint.rules.custom.Operators.CreateLesserRule(
error_level + str(rule_id), resourceType, prop, value, error_message
)
if operator == "<=":
return cfnlint.rules.custom.Operators.CreateLesserEqualRule(
error_level + str(rule_id), resourceType, prop, value, error_message
)
if operator == "IS":
if value in ["DEFINED", "NOT_DEFINED"]:
return cfnlint.rules.custom.Operators.CreateCustomIsDefinedRule(
Expand Down
Loading