|
| 1 | +""" |
| 2 | +Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +SPDX-License-Identifier: MIT-0 |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from typing import Any, Dict |
| 9 | + |
| 10 | +from sympy import And, Implies, Symbol |
| 11 | +from sympy.logic.boolalg import BooleanFunction |
| 12 | + |
| 13 | +from cfnlint.conditions._condition import ( |
| 14 | + ConditionAnd, |
| 15 | + ConditionList, |
| 16 | + ConditionNamed, |
| 17 | + ConditionNot, |
| 18 | + ConditionOr, |
| 19 | +) |
| 20 | +from cfnlint.conditions._equals import Equal |
| 21 | +from cfnlint.helpers import FUNCTION_CONDITIONS |
| 22 | + |
| 23 | +# we leave the type hinting here |
| 24 | +_RULE = Dict[str, Any] |
| 25 | + |
| 26 | + |
| 27 | +class _Assertion: |
| 28 | + def __init__(self, condition: Any, all_conditions: dict[str, dict]) -> None: |
| 29 | + self._fn_equals: Equal | None = None |
| 30 | + self._condition: ConditionList | ConditionNamed | None = None |
| 31 | + |
| 32 | + if len(condition) == 1: |
| 33 | + for k, v in condition.items(): |
| 34 | + if k in FUNCTION_CONDITIONS: |
| 35 | + if not isinstance(v, list): |
| 36 | + raise ValueError(f"{k} value should be an array") |
| 37 | + if k == "Fn::Equals": |
| 38 | + self._fn_equals = Equal(v) |
| 39 | + elif k == "Fn::And": |
| 40 | + self._condition = ConditionAnd(v, all_conditions) |
| 41 | + elif k == "Fn::Or": |
| 42 | + self._condition = ConditionOr(v, all_conditions) |
| 43 | + elif k == "Fn::Not": |
| 44 | + self._condition = ConditionNot(v, all_conditions) |
| 45 | + elif k == "Condition": |
| 46 | + if not isinstance(v, str): |
| 47 | + raise ValueError(f"Condition value {v!r} must be a string") |
| 48 | + self._condition = ConditionNamed(v, all_conditions) |
| 49 | + else: |
| 50 | + raise ValueError(f"Unknown key ({k}) in condition") |
| 51 | + else: |
| 52 | + raise ValueError("Condition value must be an object of length 1") |
| 53 | + |
| 54 | + def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction | Symbol | None: |
| 55 | + if self._fn_equals: |
| 56 | + return self._fn_equals.hash |
| 57 | + |
| 58 | + if self._condition: |
| 59 | + return self._condition.build_cnf(params) |
| 60 | + |
| 61 | + return None |
| 62 | + |
| 63 | + @property |
| 64 | + def equals(self) -> list[Equal]: |
| 65 | + if self._fn_equals: |
| 66 | + return [self._fn_equals] |
| 67 | + if self._condition: |
| 68 | + return self._condition.equals |
| 69 | + return [] |
| 70 | + |
| 71 | + |
| 72 | +class _Assertions: |
| 73 | + def __init__(self, assertions: list[dict], all_conditions: dict[str, dict]) -> None: |
| 74 | + self._assertions: list[_Assertion] = [] |
| 75 | + for assertion in assertions: |
| 76 | + assert_ = assertion.get("Assert", {}) |
| 77 | + self._assertions.append(_Assertion(assert_, all_conditions)) |
| 78 | + |
| 79 | + def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction | Symbol | None: |
| 80 | + |
| 81 | + assertions = [] |
| 82 | + for assertion in self._assertions: |
| 83 | + assertions.append(assertion.build_cnf(params)) |
| 84 | + |
| 85 | + return And(*assertions) |
| 86 | + |
| 87 | + @property |
| 88 | + def equals(self) -> list[Equal]: |
| 89 | + |
| 90 | + results = [] |
| 91 | + for assertion in self._assertions: |
| 92 | + results.extend(assertion.equals) |
| 93 | + return results |
| 94 | + |
| 95 | + |
| 96 | +class Rule: |
| 97 | + |
| 98 | + def __init__(self, rule: _RULE, all_conditions: dict[str, dict]) -> None: |
| 99 | + self._condition: _Assertion | None = None |
| 100 | + self._assertions: _Assertions | None = None |
| 101 | + self._init_rule(rule, all_conditions) |
| 102 | + |
| 103 | + def _init_rule( |
| 104 | + self, |
| 105 | + rule: _RULE, |
| 106 | + all_conditions: dict[str, dict], |
| 107 | + ) -> None: |
| 108 | + condition = rule.get("RuleCondition") |
| 109 | + if condition: |
| 110 | + self._condition = _Assertion(condition, all_conditions) |
| 111 | + |
| 112 | + assertions = rule.get("Assertions") |
| 113 | + if not assertions: |
| 114 | + raise ValueError("Rule must have Assertions") |
| 115 | + self._assertions = _Assertions(assertions, all_conditions) |
| 116 | + |
| 117 | + @property |
| 118 | + def equals(self) -> list[Equal]: |
| 119 | + result = [] |
| 120 | + if self._condition: |
| 121 | + result.extend(self._condition.equals) |
| 122 | + if self._assertions: |
| 123 | + result.extend(self._assertions.equals) |
| 124 | + return result |
| 125 | + |
| 126 | + def build_cnf(self, params: dict[str, Symbol]) -> BooleanFunction | Symbol | None: |
| 127 | + |
| 128 | + if self._assertions: |
| 129 | + if self._condition: |
| 130 | + return Implies( |
| 131 | + self._condition.build_cnf(params), |
| 132 | + self._assertions.build_cnf(params), |
| 133 | + ) |
| 134 | + return self._assertions.build_cnf(params) |
| 135 | + return None |
0 commit comments