Skip to content

Commit

Permalink
Support step fn Definition in E1029 (#3792)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Oct 25, 2024
1 parent 223132c commit 8164ed9
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/cfnlint/rules/functions/SubNeeded.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ def _variable_custom_excluded(self, value):
return re.match(custom_search, value)
return False

def _validate_step_functions(self, var, parameter_string_path, cfn) -> bool:
# Step Function State Machine has a Definition Substitution
# that allows usage of special variables outside of a !Sub
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stepfunctions-statemachine-definitionsubstitutions.html

for key in ["DefinitionString", "Definition"]:
if key in parameter_string_path:
modified_parameter_string_path = copy.copy(parameter_string_path)
index = parameter_string_path.index(key)
modified_parameter_string_path[index] = "DefinitionSubstitutions"
modified_parameter_string_path = modified_parameter_string_path[
: index + 1
]
modified_parameter_string_path.append(var[2:-1])

if reduce(
lambda c, k: c.get(k, {}),
modified_parameter_string_path,
cfn.template,
):
return True

return False

def match(self, cfn: Template) -> RuleMatches:
matches = []

Expand All @@ -93,24 +117,8 @@ def match(self, cfn: Template) -> RuleMatches:
# Get variable
var = parameter_string_path[-1]

# Step Function State Machine has a Definition Substitution
# that allows usage of special variables outside of a !Sub
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-stepfunctions-statemachine-definitionsubstitutions.html

if "DefinitionString" in parameter_string_path:
modified_parameter_string_path = copy.copy(parameter_string_path)
index = parameter_string_path.index("DefinitionString")
modified_parameter_string_path[index] = "DefinitionSubstitutions"
modified_parameter_string_path = modified_parameter_string_path[
: index + 1
]
modified_parameter_string_path.append(var[2:-1])
if reduce(
lambda c, k: c.get(k, {}),
modified_parameter_string_path,
cfn.template,
):
continue
if self._validate_step_functions(var, parameter_string_path, cfn):
continue

# Exclude variables that match custom exclude filters, if configured
# (for third-party tools that pre-process templates
Expand Down

0 comments on commit 8164ed9

Please sign in to comment.