-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start of state machine validation * Move rule to E2532 to E3601 * Make sure state machine functions are covered
- Loading branch information
Showing
8 changed files
with
1,440 additions
and
445 deletions.
There are no files selected for viewing
Empty file.
850 changes: 850 additions & 0 deletions
850
src/cfnlint/data/schemas/other/step_functions/statemachine.json
Large diffs are not rendered by default.
Oops, something went wrong.
202 changes: 0 additions & 202 deletions
202
src/cfnlint/rules/resources/stepfunctions/StateMachine.py
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
src/cfnlint/rules/resources/stepfunctions/StateMachineDefinition.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import json | ||
from typing import Any | ||
|
||
import cfnlint.data.schemas.other.resources | ||
import cfnlint.data.schemas.other.step_functions | ||
import cfnlint.helpers | ||
from cfnlint.jsonschema import ValidationError, ValidationResult, Validator | ||
from cfnlint.rules.jsonschema.CfnLintJsonSchema import CfnLintJsonSchema, SchemaDetails | ||
from cfnlint.schema.resolver import RefResolver | ||
|
||
|
||
class StateMachineDefinition(CfnLintJsonSchema): | ||
id = "E3601" | ||
shortdesc = "Validate the structure of a StateMachine definition" | ||
description = ( | ||
"Validate the Definition or DefinitionString inside a " | ||
"AWS::StepFunctions::StateMachine resource" | ||
) | ||
source_url = "https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-state-machine-structure.html" | ||
tags = ["resources", "statemachine"] | ||
|
||
def __init__(self): | ||
super().__init__( | ||
keywords=[ | ||
"Resources/AWS::StepFunctions::StateMachine/Properties/Definition", | ||
"Resources/AWS::StepFunctions::StateMachine/Properties/DefinitionString", | ||
], | ||
schema_details=SchemaDetails( | ||
cfnlint.data.schemas.other.step_functions, "statemachine.json" | ||
), | ||
all_matches=True, | ||
) | ||
|
||
store = { | ||
"definition": self.schema, | ||
} | ||
|
||
self.resolver = RefResolver.from_schema(self.schema, store=store) | ||
|
||
def _fix_message(self, err: ValidationError) -> ValidationError: | ||
if len(err.path) > 1: | ||
err.message = f"{err.message} at {'/'.join(err.path)!r}" | ||
for i, c_err in enumerate(err.context): | ||
err.context[i] = self._fix_message(c_err) | ||
return err | ||
|
||
def validate( | ||
self, validator: Validator, keywords: Any, instance: Any, schema: dict[str, Any] | ||
) -> ValidationResult: | ||
# First time child rules are configured against the rule | ||
# so we can run this now | ||
add_path_to_message = False | ||
if validator.is_type(instance, "string"): | ||
try: | ||
step_validator = validator.evolve( | ||
context=validator.context.evolve( | ||
functions=[], | ||
), | ||
resolver=self.resolver, | ||
schema=self.schema, | ||
) | ||
instance = json.loads(instance) | ||
add_path_to_message = True | ||
except json.JSONDecodeError: | ||
return | ||
else: | ||
step_validator = validator.evolve( | ||
resolver=self.resolver, | ||
schema=self.schema, | ||
) | ||
|
||
for err in step_validator.iter_errors(instance): | ||
if add_path_to_message: | ||
err = self._fix_message(err) | ||
if not err.validator.startswith("fn_") and err.validator not in ["cfnLint"]: | ||
err.rule = self | ||
|
||
yield self._clean_error(err) |
47 changes: 0 additions & 47 deletions
47
test/fixtures/templates/bad/resources/stepfunctions/state_machine.yaml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.