File size: 10,836 Bytes
b40c49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

import logging
from datetime import datetime
from typing import Any, Iterator, Tuple

import cfnlint.helpers
import cfnlint.rules.custom
from cfnlint._typing import Path, RuleMatches
from cfnlint.match import Match
from cfnlint.template import Template

LOGGER = logging.getLogger(__name__)


def _rule_is_enabled(
    rule: "CloudFormationLintRule",
    include_experimental: bool = False,
    ignore_rules: list[str] | None = None,
    include_rules: list[str] | None = None,
    mandatory_rules: list[str] | None = None,
):
    """Is the rule enabled based on the configuration"""
    ignore_rules = ignore_rules or []
    include_rules = include_rules or []
    mandatory_rules = mandatory_rules or []

    # Evaluate experimental rules
    if rule.experimental and not include_experimental:
        return False

    # Evaluate includes first:
    include_filter = False
    for include_rule in include_rules:
        if rule.id.startswith(include_rule):
            include_filter = True
    if not include_filter:
        return False

    # Enable mandatory rules without checking for if they are ignored
    for mandatory_rule in mandatory_rules:
        if rule.id.startswith(mandatory_rule):
            return True

    # Allowing ignoring of rules based on prefix to ignore checks
    for ignore_rule in ignore_rules:
        if rule.id.startswith(ignore_rule) and ignore_rule:
            return False

    return True


class RuleMatch:
    """
    Represents a rule match found by a CloudFormationLintRule.

    Attributes:
        path (Sequence[str | int]): The path to the element that
        triggered the rule match.
        path_string (str): The string representation of the path.
        message (str): The message associated with the rule match.
        context (RuleMatches): Additional context information
        related to the rule match.

    Methods:
        __eq__(self, item) -> bool:
            Override the equality comparison operator to compare
            rule matches based on their path and message.
        __hash__(self) -> int:
            Override the hash function to allow rule matches to
            be used as keys in a dictionary.
    """

    def __init__(self, path: Path, message: str, **kwargs):
        """
        Initialize a new RuleMatch instance.

        Args:
            path (Path): The path to the element
            that triggered the rule match.
            message (str): The message associated with the rule match.
            **kwargs: Additional keyword arguments to be stored
            as attributes on the RuleMatch instance.
        """
        self.path: Path = path
        self.path_string: str = "/".join(map(str, path))
        self.message: str = message
        self.context: RuleMatches = []
        for k, v in kwargs.items():
            setattr(self, k, v)

    def __eq__(self, item):
        """
        Override the equality comparison operator to compare rule
        matches based on their path and message.

        Args:
            item (RuleMatch): The other RuleMatch instance to compare with.

        Returns:
            bool: True if the path and message of the two rule matches
            are equal, False otherwise.
        """
        return (self.path, self.message) == (item.path, item.message)

    def __hash__(self):
        """
        Override the hash function to allow rule matches to be
        used as keys in a dictionary.

        Returns:
            int: The hash value of the RuleMatch instance.
        """
        return hash((self.path, self.message))


def _rule_match_to_match(
    rule: CloudFormationLintRule,
    filename: str,
    cfn: Template,
    match: RuleMatch,
    parent_id: str | None = None,
) -> Iterator[Match]:
    error_rule = rule
    if hasattr(match, "rule"):
        error_rule = match.rule
    linenumbers: Tuple[int, int, int, int] | None = None
    if hasattr(match, "location"):
        linenumbers = match.location
    else:
        linenumbers = cfn.get_location_yaml(cfn.template, match.path)
    if linenumbers:
        result = Match.create(
            linenumber=linenumbers[0] + 1,
            columnnumber=linenumbers[1] + 1,
            linenumberend=linenumbers[2] + 1,
            columnnumberend=linenumbers[3] + 1,
            filename=filename,
            rule=error_rule,
            message=match.message,
            rulematch_obj=match,
            parent_id=parent_id,
        )
    else:
        result = Match.create(
            filename=filename,
            rule=error_rule,
            message=match.message,
            rulematch_obj=match,
            parent_id=parent_id,
        )

    yield result

    for sub_match in match.context:
        sub_match.path = list(match.path) + list(sub_match.path)
        yield from _rule_match_to_match(
            rule=rule, filename=filename, cfn=cfn, match=sub_match, parent_id=result.id
        )


def matching(match_type: Any):
    """Does Logging for match functions"""

    def decorator(match_function):
        """The Actual Decorator"""

        def wrapper(self, filename: str, cfn: Template, *args, **kwargs):
            """Wrapper"""
            if match_type == "match_resource_properties":
                if args[1] not in self.resource_property_types:
                    return

            start = datetime.now()
            LOGGER.debug("Starting match function for rule %s at %s", self.id, start)
            # pylint: disable=E1102
            for result in match_function(self, filename, cfn, *args, **kwargs):
                yield from _rule_match_to_match(self, filename, cfn, result)

            LOGGER.debug(
                "Complete match function for rule %s at %s.  Ran in %s",
                self.id,
                datetime.now(),
                datetime.now() - start,
            )

        return wrapper

    return decorator


class CloudFormationLintRule:
    """CloudFormation linter rules"""

    id: str = ""
    shortdesc: str = ""
    description: str = ""
    source_url: str = ""
    tags: list[str] = []
    experimental: bool = False

    logger = logging.getLogger(__name__)

    def __init__(self) -> None:
        self.resource_property_types: list[str] = []
        self.config: dict[str, Any] = {}  # `-X E3012:strict=false`... Show more
        self.config_definition: dict[str, Any] = {}
        self._child_rules: dict[str, "CloudFormationLintRule" | None] = {}
        # Parent IDs to do the opposite of child rules
        self._parent_rules: list[str] = []
        super().__init__()

    def __repr__(self):
        return f"{self.id}: {self.shortdesc}"

    def __eq__(self, other):
        if other is None:
            return False
        return self.id == other.id

    @property
    def child_rules(self) -> dict[str, "CloudFormationLintRule" | None]:
        return self._child_rules

    @child_rules.setter
    def child_rules(self, rules: dict[str, "CloudFormationLintRule" | None]):
        self._child_rules = rules

    @property
    def parent_rules(self):
        return self._parent_rules

    @parent_rules.setter
    def parent_rules(self, rules: list[str]):
        self._parent_rules = rules

    @property
    def severity(self):
        """Severity level"""
        levels = {
            "I": "informational",
            "E": "error",
            "W": "warning",
        }
        return levels.get(self.id[0].upper(), "unknown")

    def verbose(self):
        """Verbose output"""
        return f"{self.id}: {self.shortdesc}\n{self.description}"

    def initialize(self, cfn: Template):
        """Initialize the rule"""

    def is_enabled(
        self,
        include_experimental=False,
        ignore_rules=None,
        include_rules=None,
        mandatory_rules=None,
    ):
        return _rule_is_enabled(
            self, include_experimental, ignore_rules, include_rules, mandatory_rules
        )

    def configure(self, configs=None, experimental=False):
        """Set the configuration"""

        # set defaults
        if isinstance(self.config_definition, dict):
            for config_name, config_values in self.config_definition.items():
                self.config[config_name] = config_values["default"]

        # set experimental if the rule is asking for it
        if "experimental" in self.config_definition:
            if self.config_definition["experimental"]["type"] == "boolean":
                self.config["experimental"] = cfnlint.helpers.bool_compare(
                    experimental, True
                )

        if isinstance(configs, dict):
            for key, value in configs.items():
                if key in self.config_definition:
                    if self.config_definition[key]["type"] == "boolean":
                        self.config[key] = cfnlint.helpers.bool_compare(value, True)
                    elif self.config_definition[key]["type"] == "string":
                        self.config[key] = str(value)
                    elif self.config_definition[key]["type"] == "integer":
                        self.config[key] = int(value)
                    elif self.config_definition[key]["type"] == "list":
                        self.config[key] = []
                        for l_value in value:
                            if self.config_definition[key]["itemtype"] == "boolean":
                                self.config[key].append(
                                    cfnlint.helpers.bool_compare(l_value, True)
                                )
                            elif self.config_definition[key]["itemtype"] == "string":
                                self.config[key].append(str(l_value))
                            elif self.config_definition[key]["itemtype"] == "integer":
                                self.config[key].append(int(l_value))

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

    def match_resource_properties(
        self,
        properties: dict[str, Any],
        resourcetype: str,
        path: Path,
        cfn: Template,
    ) -> RuleMatches:
        return []

    @matching("match")
    # pylint: disable=W0613
    def matchall(self, filename: str, cfn: Template):
        """Match the entire file"""
        return self.match(cfn)  # pylint: disable=E1102

    @matching("match_resource_properties")
    # pylint: disable=W0613
    def matchall_resource_properties(
        self, filename, cfn, resource_properties, property_type, path
    ):
        """Check for resource properties type"""
        return self.match_resource_properties(  # pylint: disable=E1102
            resource_properties, property_type, path, cfn
        )