forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindInMap.py
126 lines (111 loc) · 3.88 KB
/
FindInMap.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
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
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from __future__ import annotations
from typing import Any
from cfnlint.jsonschema import ValidationResult, Validator
from cfnlint.rules.functions._BaseFn import BaseFn, singular_types
class FindInMap(BaseFn):
"""Check if FindInMap values are correct"""
id = "E1011"
shortdesc = "FindInMap validation of configuration"
description = "Making sure the function is a list of appropriate config"
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html"
tags = ["functions", "findinmap"]
def __init__(self) -> None:
super().__init__("Fn::FindInMap", ("array",) + singular_types)
def schema(self, validator: Validator, instance: Any) -> dict[str, Any]:
scalar_schema = {
"functions": [
"Fn::FindInMap",
"Ref",
],
"schema": {
"type": ["string"],
},
}
schema = {
"type": "array",
"minItems": 3,
"maxItems": 3,
"fn_items": [
scalar_schema,
scalar_schema,
scalar_schema,
],
}
if validator.context.transforms.has_language_extensions_transform():
scalar_schema["functions"] = [
"Fn::FindInMap",
"Fn::Join",
"Fn::Sub",
"Fn::If",
"Fn::Select",
"Fn::Length",
"Fn::ToJsonString",
"Ref",
]
schema["maxItems"] = 4
schema["fn_items"] = [
scalar_schema,
scalar_schema,
scalar_schema,
{
"functions": [],
"schema": {
"findinmap_parameters": True,
"type": ["object"],
"properties": {
"DefaultValue": {
"default_value": True,
}
},
"additionalProperties": False,
"required": ["DefaultValue"],
},
},
]
return schema
def _default_value(
self, validator: Validator, s: Any, instance: Any, schema: Any
) -> ValidationResult:
validator = validator.evolve(
context=validator.context.evolve(
functions=["Ref"],
),
)
yield from validator.descend(
instance,
{
"type": ("array",) + singular_types,
},
)
def fn_findinmap(
self, validator: Validator, s: Any, instance: Any, schema: Any
) -> ValidationResult:
if validator.context.transforms.has_language_extensions_transform():
# we have to use a special validator for this
# as we don't want DefaultValue: !Ref AWS::NoValue
# is valid
mapping_validator = validator.extend(
validators={
"default_value": self._default_value,
},
)(validator.schema)
validator = mapping_validator.evolve(
context=validator.context.evolve(
resources={},
),
cfn=validator.cfn,
function_filter=validator.function_filter,
resolver=validator.resolver,
)
yield from super().validate(validator, s, instance, schema)
return
validator = validator.evolve(
context=validator.context.evolve(
resources={},
)
)
yield from super().validate(validator, s, instance, schema)