-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathPattern.py
70 lines (60 loc) · 2.27 KB
/
Pattern.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from typing import Any
import regex as re
from cfnlint.jsonschema import ValidationResult, Validator
from cfnlint.jsonschema._keywords import pattern
from cfnlint.rules import CloudFormationLintRule
class Pattern(CloudFormationLintRule):
"""Check if properties have a valid value"""
id = "E3031"
shortdesc = "Check if property values adhere to a specific pattern"
description = (
"Check if properties have a valid value in case of a pattern (Regular"
" Expression)"
)
source_url = "https://github.com/aws-cloudformation/cfn-lint/blob/main/docs/cfn-schema-specification.md#pattern"
tags = ["resources", "property", "allowed pattern", "regex"]
child_rules = {
"W2031": None,
}
def __init__(self):
"""Init"""
super().__init__()
self.config_definition = {
"exceptions": {
"default": [],
"type": "list",
"itemtype": "string",
}
}
self.configure()
def _is_exception(self, instance: str) -> bool:
for exception in self.config["exceptions"]:
if re.match(exception, instance):
return True
return False
# pylint: disable=unused-argument, arguments-renamed
def pattern(
self, validator: Validator, patrn: str, instance: Any, schema: Any
) -> ValidationResult:
# https://github.com/aws-cloudformation/cfn-lint/issues/3640
if validator.cfn.has_serverless_transform():
for _, param in validator.context.parameters.items():
if param.is_ssm_parameter():
if param.ssm_path == instance:
return
if (
len(validator.context.path.value_path) > 0
and validator.context.path.value_path[0] == "Parameters"
):
if self.child_rules.get("W2031"):
yield from self.child_rules["W2031"].pattern( # type: ignore
validator, patrn, instance, schema
)
return
for err in pattern(validator, patrn, instance, schema):
if not self._is_exception(instance):
yield err