cfn-check 0.2.2__py3-none-any.whl → 0.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cfn-check might be problematic. Click here for more details.
- cfn_check/cli/validate.py +5 -9
- cfn_check/evaluation/evaluator.py +85 -0
- cfn_check/evaluation/parsing/__init__.py +1 -0
- cfn_check/evaluation/parsing/query_parser.py +145 -0
- cfn_check/evaluation/parsing/token.py +269 -0
- cfn_check/evaluation/parsing/token_type.py +14 -0
- cfn_check/evaluation/validate.py +56 -47
- cfn_check-0.3.1.dist-info/METADATA +541 -0
- {cfn_check-0.2.2.dist-info → cfn_check-0.3.1.dist-info}/RECORD +14 -11
- example/rules.py +65 -6
- cfn_check/evaluation/check.py +0 -20
- cfn_check/evaluation/search.py +0 -137
- cfn_check-0.2.2.dist-info/METADATA +0 -247
- {cfn_check-0.2.2.dist-info → cfn_check-0.3.1.dist-info}/WHEEL +0 -0
- {cfn_check-0.2.2.dist-info → cfn_check-0.3.1.dist-info}/entry_points.txt +0 -0
- {cfn_check-0.2.2.dist-info → cfn_check-0.3.1.dist-info}/licenses/LICENSE +0 -0
- {cfn_check-0.2.2.dist-info → cfn_check-0.3.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cfn-check
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Validate Cloud Formation
|
|
5
|
+
Author-email: Ada Lundhe <adalundhe@lundhe.audio>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Ada Lündhé
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/adalundhe/cfn-check
|
|
29
|
+
Keywords: cloud-formation,testing,aws,cli
|
|
30
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
31
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
32
|
+
Classifier: Operating System :: OS Independent
|
|
33
|
+
Requires-Python: >=3.12
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Requires-Dist: pydantic
|
|
37
|
+
Requires-Dist: pyyaml
|
|
38
|
+
Requires-Dist: hyperlight-cocoa
|
|
39
|
+
Requires-Dist: async-logging
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
# <b>CFN-Check</b>
|
|
43
|
+
<b>A tool for checking CloudFormation</b>
|
|
44
|
+
|
|
45
|
+
[](https://pypi.org/project/cfn-check/)
|
|
46
|
+
[](https://github.com/adalundhe/cfn-check/blob/main/LICENSE)
|
|
47
|
+
[](https://github.com/adalundhe/cfn-check/blob/main/CODE_OF_CONDUCT.md)
|
|
48
|
+
[](https://pypi.org/project/cfn-check/)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
| Package | cfn-check |
|
|
52
|
+
| ----------- | ----------- |
|
|
53
|
+
| Version | 0.2.0 |
|
|
54
|
+
| Download | https://pypi.org/project/cfn-check/ |
|
|
55
|
+
| Source | https://github.com/adalundhe/cfn-check |
|
|
56
|
+
| Keywords | cloud-formation, testing, aws, cli |
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
CFN-Check is a small, fast, friendly tool for validating AWS CloudFormation YAML templates. It is code-driven, with
|
|
60
|
+
rules written as simple, `Rule` decorator wrapped python class methods for `Collection`-inheriting classes.
|
|
61
|
+
|
|
62
|
+
<br/>
|
|
63
|
+
|
|
64
|
+
# Why CFN-Check?
|
|
65
|
+
|
|
66
|
+
AWS has its own tools for validating Cloud Formation - `cfn-lint` and `cfn-guard`. `cfn-check` aims to solve
|
|
67
|
+
problems inherint to `cfn-lint` more than `cfn-guard`, primarily:
|
|
68
|
+
|
|
69
|
+
- Confusing, unclear syntax around rules configuration
|
|
70
|
+
- Inability to parse non-resource wildcards
|
|
71
|
+
- Inability to validate non-resource template data
|
|
72
|
+
- Inabillity to use structured models to validate input
|
|
73
|
+
|
|
74
|
+
In comparison to `cfn-guard`, `cfn-check` is pure Python, thus
|
|
75
|
+
avoiding YADSL (Yet Another DSL) headaches. It also proves
|
|
76
|
+
significantly more configurable/modular/hackable as a result.
|
|
77
|
+
|
|
78
|
+
CFN-Check uses a combination of simple depth-first-search tree
|
|
79
|
+
parsing, friendly `cfn-lint` like query syntax, `Pydantic` models,
|
|
80
|
+
and `pytest`-like assert-driven checks to make validating your
|
|
81
|
+
Cloud Formation easy while offering both CLI and Python API interfaces.
|
|
82
|
+
|
|
83
|
+
<br/>
|
|
84
|
+
|
|
85
|
+
# Getting Started
|
|
86
|
+
|
|
87
|
+
`cfn-check` requires:
|
|
88
|
+
|
|
89
|
+
- `Python 3.12`
|
|
90
|
+
- Any number of valid CloudFormation templates or a path to said templates.
|
|
91
|
+
- A `.py` file containing at least one `Collection` class with at least one valid `@Rule()` decorated method
|
|
92
|
+
|
|
93
|
+
To get started (we recommend using `uv`), run:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
uv venv
|
|
97
|
+
source .venv/bin/activate
|
|
98
|
+
|
|
99
|
+
uv pip install cfn-check
|
|
100
|
+
|
|
101
|
+
touch rules.py
|
|
102
|
+
touch template.yaml
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Next open the `rules.py` file and create a basic Python class
|
|
106
|
+
as below.
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from cfn_check import Collection, Rule
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class ValidateResourceType(Collection):
|
|
113
|
+
|
|
114
|
+
@Rule(
|
|
115
|
+
"Resources::*::Type",
|
|
116
|
+
"It checks Resource::Type is correctly definined",
|
|
117
|
+
)
|
|
118
|
+
def validate_test(self, value: str):
|
|
119
|
+
assert value is not None, '❌ Resource Type not defined'
|
|
120
|
+
assert isinstance(value, str), '❌ Resource Type not a string'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
This provides us a basic rule set that validates that the `Type` field of our CloudFormation template(s) exists and is the correct data type.
|
|
124
|
+
|
|
125
|
+
> [!NOTE]
|
|
126
|
+
> Don't worry about adding an `__init__()` method to this class!
|
|
127
|
+
|
|
128
|
+
Next open the `template.yaml` file and paste the following CloudFormation:
|
|
129
|
+
|
|
130
|
+
```yaml
|
|
131
|
+
AWSTemplateFormatVersion: '2010-09-09'
|
|
132
|
+
Parameters:
|
|
133
|
+
ExistingSecurityGroups:
|
|
134
|
+
Type: List<AWS::EC2::SecurityGroup::Id>
|
|
135
|
+
ExistingVPC:
|
|
136
|
+
Type: AWS::EC2::VPC::Id
|
|
137
|
+
Description: The VPC ID that includes the security groups in the ExistingSecurityGroups parameter.
|
|
138
|
+
InstanceType:
|
|
139
|
+
Type: String
|
|
140
|
+
Default: t2.micro
|
|
141
|
+
AllowedValues:
|
|
142
|
+
- t2.micro
|
|
143
|
+
- m1.small
|
|
144
|
+
Mappings:
|
|
145
|
+
AWSInstanceType2Arch:
|
|
146
|
+
t2.micro:
|
|
147
|
+
Arch: HVM64
|
|
148
|
+
m1.small:
|
|
149
|
+
Arch: HVM64
|
|
150
|
+
AWSRegionArch2AMI:
|
|
151
|
+
us-east-1:
|
|
152
|
+
HVM64: ami-0ff8a91507f77f867
|
|
153
|
+
HVMG2: ami-0a584ac55a7631c0c
|
|
154
|
+
Resources:
|
|
155
|
+
SecurityGroup:
|
|
156
|
+
Type: AWS::EC2::SecurityGroup
|
|
157
|
+
Properties:
|
|
158
|
+
GroupDescription: Allow HTTP traffic to the host
|
|
159
|
+
VpcId: !Ref ExistingVPC
|
|
160
|
+
SecurityGroupIngress:
|
|
161
|
+
- IpProtocol: tcp
|
|
162
|
+
FromPort: 80
|
|
163
|
+
ToPort: 80
|
|
164
|
+
CidrIp: 0.0.0.0/0
|
|
165
|
+
SecurityGroupEgress:
|
|
166
|
+
- IpProtocol: tcp
|
|
167
|
+
FromPort: 80
|
|
168
|
+
ToPort: 80
|
|
169
|
+
CidrIp: 0.0.0.0/0
|
|
170
|
+
AllSecurityGroups:
|
|
171
|
+
Type: Custom::Split
|
|
172
|
+
Properties:
|
|
173
|
+
ServiceToken: !GetAtt AppendItemToListFunction.Arn
|
|
174
|
+
List: !Ref ExistingSecurityGroups
|
|
175
|
+
AppendedItem: !Ref SecurityGroup
|
|
176
|
+
AppendItemToListFunction:
|
|
177
|
+
Type: AWS::Lambda::Function
|
|
178
|
+
Properties:
|
|
179
|
+
Handler: index.handler
|
|
180
|
+
Role: !GetAtt LambdaExecutionRole.Arn
|
|
181
|
+
Code:
|
|
182
|
+
ZipFile: !Join
|
|
183
|
+
- ''
|
|
184
|
+
- - var response = require('cfn-response');
|
|
185
|
+
- exports.handler = function(event, context) {
|
|
186
|
+
- ' var responseData = {Value: event.ResourceProperties.List};'
|
|
187
|
+
- ' responseData.Value.push(event.ResourceProperties.AppendedItem);'
|
|
188
|
+
- ' response.send(event, context, response.SUCCESS, responseData);'
|
|
189
|
+
- '};'
|
|
190
|
+
Runtime: nodejs20.x
|
|
191
|
+
MyEC2Instance:
|
|
192
|
+
Type: AWS::EC2::Instance
|
|
193
|
+
Properties:
|
|
194
|
+
ImageId: !FindInMap
|
|
195
|
+
- AWSRegionArch2AMI
|
|
196
|
+
- !Ref AWS::Region
|
|
197
|
+
- !FindInMap
|
|
198
|
+
- AWSInstanceType2Arch
|
|
199
|
+
- !Ref InstanceType
|
|
200
|
+
- Arch
|
|
201
|
+
SecurityGroupIds: !GetAtt AllSecurityGroups.Value
|
|
202
|
+
InstanceType: !Ref InstanceType
|
|
203
|
+
LambdaExecutionRole:
|
|
204
|
+
Type: AWS::IAM::Role
|
|
205
|
+
Properties:
|
|
206
|
+
AssumeRolePolicyDocument:
|
|
207
|
+
Version: '2012-10-17'
|
|
208
|
+
Statement:
|
|
209
|
+
- Effect: Allow
|
|
210
|
+
Principal:
|
|
211
|
+
Service:
|
|
212
|
+
- lambda.amazonaws.com
|
|
213
|
+
Action:
|
|
214
|
+
- sts:AssumeRole
|
|
215
|
+
Path: /
|
|
216
|
+
Policies:
|
|
217
|
+
- PolicyName: root
|
|
218
|
+
PolicyDocument:
|
|
219
|
+
Version: '2012-10-17'
|
|
220
|
+
Statement:
|
|
221
|
+
- Effect: Allow
|
|
222
|
+
Action:
|
|
223
|
+
- logs:*
|
|
224
|
+
Resource: arn:aws:logs:*:*:*
|
|
225
|
+
Outputs:
|
|
226
|
+
AllSecurityGroups:
|
|
227
|
+
Description: Security Groups that are associated with the EC2 instance
|
|
228
|
+
Value: !Join
|
|
229
|
+
- ', '
|
|
230
|
+
- !GetAtt AllSecurityGroups.Value
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
This represents a basic configuration for an AWS Lambda function.
|
|
234
|
+
|
|
235
|
+
Finally, run:
|
|
236
|
+
|
|
237
|
+
```bash
|
|
238
|
+
cfn-check validate -r rules.py template.yaml
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
which outputs:
|
|
242
|
+
|
|
243
|
+
```
|
|
244
|
+
2025-09-17T01:46:41.542078+00:00 - INFO - 19783474 - /Users/adalundhe/Documents/adalundhe/cfn-check/cfn_check/cli/validate.py:validate.80 - ✅ 1 validations met for 1 templates
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Congrats! You've just made the cloud a bit better place!
|
|
248
|
+
|
|
249
|
+
<br/>
|
|
250
|
+
|
|
251
|
+
# Queries, Tokens, and Syntax
|
|
252
|
+
|
|
253
|
+
A `cfn-check` Query is a string made up of double-colon (`::`) delimited "Tokens" centered around three primary types:
|
|
254
|
+
|
|
255
|
+
- <b>`Keys`</b> - `<KEY>`: String name key Tokens that perform exact matching on keys of key/value pairs in a CloudFormation document.
|
|
256
|
+
- <b>`Patterns`</b> - `(\d+)`: Paren-enclosed regex pattern Tokens that perform pattern-based matching on keys of key/value pairs in a CloudFormation document.
|
|
257
|
+
- <b>`Ranges`</b> - `[]`: Brackets enclosed Tokens that perform array selection and filtering in a CloudFormation document.
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
In addition to `Key`, `Pattern`, and `Range` selection, you can also incorporate:
|
|
261
|
+
|
|
262
|
+
- <b>`Bounded Ranges`</b> - `[<A>-<B>]`: Exact matches from the starting position (if specified) to the end position (if specified) of an array
|
|
263
|
+
- <b>`Indicies`</b> - `[<A>]`: Exact matches the specified indicies of an array
|
|
264
|
+
- <b>`Key Ranges`</b> - `[<KEY>]`: Exact matches keys of objects within an array
|
|
265
|
+
- <b>`Pattern Ranges`</b> (`[(\d+)]`): Matches they keys of objects within an array based on the specified pattern
|
|
266
|
+
- <b>`Wildcards`</b> (`*`): Selects all values for a given object or array or returns the non-object/array value at the specified path
|
|
267
|
+
- <b>`Wildcard Ranges`</b> (`[*]`): Selects all values for a given array and ensures that *only* the values of a valid array type are returned (any other type will be treated as a mismatch).
|
|
268
|
+
|
|
269
|
+
### Working with Keys
|
|
270
|
+
|
|
271
|
+
Keys likely the most commos Token type you'll use in your queries. In fact, if you ran the example above, you already have! For example, with:
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
Resources
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
as your query, you'll select all items within the CloudFormation document under the `Resources` key.
|
|
278
|
+
|
|
279
|
+
### Working with Patterns
|
|
280
|
+
|
|
281
|
+
If an object within a CloudFormation document contains multiple similar keys you want to select, `Pattern` Tokens are your go-to solution. Consider this segment of CloudFormation:
|
|
282
|
+
|
|
283
|
+
```yaml
|
|
284
|
+
Resources:
|
|
285
|
+
SecurityGroup:
|
|
286
|
+
Type: AWS::EC2::SecurityGroup
|
|
287
|
+
Properties:
|
|
288
|
+
GroupDescription: Allow HTTP traffic to the host
|
|
289
|
+
VpcId: !Ref ExistingVPC
|
|
290
|
+
SecurityGroupIngress:
|
|
291
|
+
- IpProtocol: tcp
|
|
292
|
+
FromPort: 80
|
|
293
|
+
ToPort: 80
|
|
294
|
+
CidrIp: 0.0.0.0/0
|
|
295
|
+
SecurityGroupEgress:
|
|
296
|
+
- IpProtocol: tcp
|
|
297
|
+
FromPort: 80
|
|
298
|
+
ToPort: 80
|
|
299
|
+
CidrIp: 0.0.0.0/0
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
We want to select <i>both</i> `SecurityGroupIngress` and `SecurityGroupEgress` to perform the same rule evaluations. Since the keys for both blocks start with `SecurityGroup`, we could write a Query using a Pattern Token like:
|
|
303
|
+
|
|
304
|
+
```
|
|
305
|
+
Resources::SecurityGroup::Properties::(SecurityGroup)
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
which would allow us to use a single rule to evaluate both:
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
class ValidateSecurityGroups(Collection):
|
|
312
|
+
|
|
313
|
+
@Rule(
|
|
314
|
+
"Resources::SecurityGroup::Properties::(SecurityGroup)",
|
|
315
|
+
"It checks Security Groups are correctly definined",
|
|
316
|
+
)
|
|
317
|
+
def validate_test(self, value: list[dict]):
|
|
318
|
+
assert len(value) > 0
|
|
319
|
+
|
|
320
|
+
for item in value:
|
|
321
|
+
protocol = item.get("IpProtocol")
|
|
322
|
+
assert isinstance(protocol, str)
|
|
323
|
+
assert protocol == "tcp"
|
|
324
|
+
|
|
325
|
+
from_port = item.get("FromPort")
|
|
326
|
+
assert isinstance(from_port, int)
|
|
327
|
+
assert from_port == 80
|
|
328
|
+
|
|
329
|
+
to_port = item.get('ToPort')
|
|
330
|
+
assert isinstance(to_port, int)
|
|
331
|
+
assert to_port == 80
|
|
332
|
+
|
|
333
|
+
cidr_ip = item.get('CidrIp')
|
|
334
|
+
assert isinstance(cidr_ip, str)
|
|
335
|
+
assert cidr_ip == '0.0.0.0/0'
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### Working with Wildcards
|
|
339
|
+
|
|
340
|
+
Wildcard Tokens allow you to select all matching objects, array entries, or values (given preceding tokens) within a CloudFormation document. Wildcard Tokens are powerful, allowing you to effectively destructure objects into their respective keys and values or arrays into their entries for easier filtering and checking.
|
|
341
|
+
|
|
342
|
+
In fact, you've already used one! In the first example, we use a Wildcard Token in the below query:
|
|
343
|
+
|
|
344
|
+
```
|
|
345
|
+
Resources::*::Type
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
To select all `Resource` objects, then further extract the `Type` field from each object. This helps us avoid copy-paste rules at the potential cost of deferring more work to individual `Rule` methods if we aren't careful and select too much!
|
|
349
|
+
|
|
350
|
+
### Working with Ranges
|
|
351
|
+
|
|
352
|
+
Ranges allow you to perform sophisticated selection of objects or data within a CloudFormation document.
|
|
353
|
+
|
|
354
|
+
> [!IMPORTANT]
|
|
355
|
+
> Range Tokens *only* work on list items. This means that any
|
|
356
|
+
> values or other objects/data in the selected section of the
|
|
357
|
+
> CloudFormation document will be *ignored* and filtered out.
|
|
358
|
+
|
|
359
|
+
#### Unbounded Ranges
|
|
360
|
+
|
|
361
|
+
Unbounded ranges allow you to select and return an array in its entirety. For example:
|
|
362
|
+
|
|
363
|
+
```
|
|
364
|
+
Resources::SecurityGroup::Properties::SecurityGroupIngress::[]
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
Would return all SecurityGroupIngress objects in the CloudFormation document as a list, allowing you to check that the array of ingresses has been both defined *and* populated.
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
#### Indexes
|
|
371
|
+
|
|
372
|
+
Indexes allow you to select specific positions within an array. For example:
|
|
373
|
+
|
|
374
|
+
```
|
|
375
|
+
Resources::SecurityGroup::Properties::SecurityGroupIngress::[0]
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Would return the first SecurityGroupIngress objects in the document.
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
#### Bounded Ranges
|
|
382
|
+
|
|
383
|
+
Bounded Ranges allow you to select subsets of indicies within an array (much like Python slicing). Unlike Python slicing, Bounded Ranges do *not* allow you to select a "step", however like Python slicing, starting positions are inclusive and end positions are exclusive (i.e. `0-10` will select from indexes `0` to `9`)
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
As an example:
|
|
387
|
+
|
|
388
|
+
```
|
|
389
|
+
Resources::SecurityGroup::Properties::SecurityGroupIngress::[1-3]
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
Would select the second and third SecurityGroupIngress objects in the document.
|
|
393
|
+
|
|
394
|
+
Start or end positions are optional for Bounded Ranges. If a starting position is not defined, `cfn-check` will default to `0`. Likewise, if an end position is not defined, `cfn-check` will default to the end of given list. For example:
|
|
395
|
+
|
|
396
|
+
```
|
|
397
|
+
Resources::SecurityGroup::Properties::SecurityGroupIngress::[-3]
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
selects the first through third SecurityGroupIngress objects in the document while:
|
|
401
|
+
|
|
402
|
+
```
|
|
403
|
+
Resources::SecurityGroup::Properties::SecurityGroupIngress::[3-]
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
selects the remaining SecurityGroupIngress objects starting from the third.
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
#### Key Ranges
|
|
410
|
+
|
|
411
|
+
Often times it's easier to match based upon an array's contents than by exact index. Key Ranges allow you to do this by matching the contents of each item in an array by:
|
|
412
|
+
|
|
413
|
+
- Exact match value comparison if the array value is not an object or array
|
|
414
|
+
- Single exact match value comparison if the array value is an array (i.e. there is at least one value exactly matching the Token in the array)
|
|
415
|
+
- Single exact match key comparison if the array value is an object
|
|
416
|
+
|
|
417
|
+
For example:
|
|
418
|
+
|
|
419
|
+
```
|
|
420
|
+
Resources::MyEC2Instance::Properties::ImageId::[AWSRegionArch2AMI]
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
returns only the EC2 ImageIds where the ImageId exactly matches `AWSRegionArch2AMI`.
|
|
424
|
+
|
|
425
|
+
|
|
426
|
+
#### Pattern Ranges
|
|
427
|
+
|
|
428
|
+
Pattern Ranges function much like Key Ranges, but utilize regex-based pattern matching for comparison. Adapting the above example:
|
|
429
|
+
|
|
430
|
+
```
|
|
431
|
+
Resources::MyEC2Instance::Properties::ImageId::[(^AWSRegion)]
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
returns only the EC2 ImageIds where the ImageId begins with `AWSRegion`. This can be helpful in checking for and enforcing naming standards, etc.
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
#### Wildcard Ranges
|
|
438
|
+
|
|
439
|
+
Wildcard Ranges extend the powerful functionality of Wildcard Tokens with the added safety of ensuring *only* arrays selected for further filtering or checks.
|
|
440
|
+
|
|
441
|
+
For example we know:
|
|
442
|
+
|
|
443
|
+
```
|
|
444
|
+
Resources::*::Type
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
Selects all `Resource` objects. If we convert the Wildcard Token in the query to a Wildcard Range Token:
|
|
448
|
+
|
|
449
|
+
```
|
|
450
|
+
Resources::*::Type
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
The Rule will fail as below:
|
|
454
|
+
|
|
455
|
+
```
|
|
456
|
+
error: ❌ No results matching results for query Resources::[*]::Type
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
As we're selecting objects, not an array! A valid use would be in validating the deeply nested zipfile code of a Lambda's `AppendItemToListFunction`:
|
|
460
|
+
|
|
461
|
+
```yaml
|
|
462
|
+
AppendItemToListFunction:
|
|
463
|
+
Type: AWS::Lambda::Function
|
|
464
|
+
Properties:
|
|
465
|
+
Handler: index.handler
|
|
466
|
+
Role: !GetAtt LambdaExecutionRole.Arn
|
|
467
|
+
Code:
|
|
468
|
+
ZipFile: !Join
|
|
469
|
+
- ''
|
|
470
|
+
- - var response = require('cfn-response');
|
|
471
|
+
- exports.handler = function(event, context) {
|
|
472
|
+
- ' var responseData = {Value: event.ResourceProperties.List};'
|
|
473
|
+
- ' responseData.Value.push(event.ResourceProperties.AppendedItem);'
|
|
474
|
+
- ' response.send(event, context, response.SUCCESS, responseData);'
|
|
475
|
+
- '};'
|
|
476
|
+
Runtime: nodejs20.x
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
Note that the array we want is nested within another array, and we need to make sure we don't select the empty string that is the first element of the outer array!
|
|
480
|
+
|
|
481
|
+
We can accomplish this by using a Wildcard Range Token in our Query as below:
|
|
482
|
+
|
|
483
|
+
```
|
|
484
|
+
Resources::AppendItemToListFunction::Properties::Code::ZipFile::[*]::[]
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
Which allows us to then evaluate the Unbounded Range token against each array item, returning only the array we want.
|
|
488
|
+
|
|
489
|
+
### Using Multiple Tokens in Ranges
|
|
490
|
+
|
|
491
|
+
You can use multiple Tokens within a Range Token by seperating each token with a comma.
|
|
492
|
+
|
|
493
|
+
> [!NOTE]
|
|
494
|
+
> While YAML does allow commas in keys, CloudFormation does not.
|
|
495
|
+
> As such, the case where a Pattern or Pattern Range might
|
|
496
|
+
> contain a comma is non-existent.
|
|
497
|
+
|
|
498
|
+
For example:
|
|
499
|
+
|
|
500
|
+
```
|
|
501
|
+
Resources::SecurityGroup::Properties::SecurityGroupIngress::[0, -2]
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
Would select all except the last element of an array.
|
|
505
|
+
|
|
506
|
+
This also applies to Bounded Ranges, Key Ranges, Pattern Ranges, and Wildcard Ranges! For example:
|
|
507
|
+
|
|
508
|
+
```
|
|
509
|
+
Resources::MyEC2Instance::Properties::ImageId::[(^AWSRegion),(^),(^Custom)]
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
will select any EC2 ImageIds that start with either `AWSRegion` or `Custom`.
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
### Nested Ranges
|
|
516
|
+
|
|
517
|
+
CloudFormation often involes nested arrays, and navigates these can make for long and difficult-to-read Queries. To help reduce Query length, `cfn-check` supports nesting Range Tokens. For example, when evaluating:
|
|
518
|
+
|
|
519
|
+
```yaml
|
|
520
|
+
ZipFile: !Join
|
|
521
|
+
- ''
|
|
522
|
+
- - var response = require('cfn-response');
|
|
523
|
+
- exports.handler = function(event, context) {
|
|
524
|
+
- ' var responseData = {Value: event.ResourceProperties.List};'
|
|
525
|
+
- ' responseData.Value.push(event.ResourceProperties.AppendedItem);'
|
|
526
|
+
- ' response.send(event, context, response.SUCCESS, responseData);'
|
|
527
|
+
- '};'
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
from our previous examples, we used the below query to select the nested array:
|
|
531
|
+
|
|
532
|
+
```
|
|
533
|
+
Resources::AppendItemToListFunction::Properties::Code::ZipFile::[*]::[]
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
With Nested Ranges, this can be shortened to:
|
|
537
|
+
|
|
538
|
+
```
|
|
539
|
+
Resources::AppendItemToListFunction::Properties::Code::ZipFile::[[]]
|
|
540
|
+
|
|
541
|
+
Which is both more concise *and* more representitave of our intention to select only the array.
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
cfn_check/__init__.py,sha256=ccUo2YxBmuEmak1M5o-8J0ECLXNkDDUsLJ4mkm31GvU,96
|
|
2
2
|
cfn_check/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
cfn_check/cli/root.py,sha256=dHq9zzXyj-Wrj-fzirtFjptShzWbBGsO3n9tspm-pec,1688
|
|
4
|
-
cfn_check/cli/validate.py,sha256=
|
|
4
|
+
cfn_check/cli/validate.py,sha256=EY6-YgORApOxCgFAmPdwmfDg1UA80GLsRzQv44zSuY4,1954
|
|
5
5
|
cfn_check/cli/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
6
|
cfn_check/cli/utils/attributes.py,sha256=iIUIgl6cT5XEUOW7D54-xxmMpTis84ySQY1b9osB47E,339
|
|
7
7
|
cfn_check/cli/utils/files.py,sha256=QMYYR7C7mXdDx_6jj1Ye9w7ol1twAzUEZ9hxSa-O4-k,2563
|
|
8
8
|
cfn_check/collection/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
9
|
cfn_check/collection/collection.py,sha256=wNxahoOqQge3C56blz5VtOq6lX5MZ9F2JjQIyZ3_SxU,27
|
|
10
10
|
cfn_check/evaluation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
|
-
cfn_check/evaluation/check.py,sha256=5GgoZY0YfyOeOP92VJVH4Y3AYB7jUGjxwsr0qsbVGeo,414
|
|
12
11
|
cfn_check/evaluation/errors.py,sha256=yPJdtRYo67le4yMC9sYqcboCnkqKsJ3KPbSPFY2-Pi8,773
|
|
13
|
-
cfn_check/evaluation/
|
|
14
|
-
cfn_check/evaluation/validate.py,sha256=
|
|
12
|
+
cfn_check/evaluation/evaluator.py,sha256=weFBAYKVc9TjTB2zNMIVX77Bi9kIaWp2KRJa1ISSLNs,2240
|
|
13
|
+
cfn_check/evaluation/validate.py,sha256=yy8byYAoHxFqkS2HfewHup22B3bYtrUH2PhPuNAc--A,1547
|
|
14
|
+
cfn_check/evaluation/parsing/__init__.py,sha256=s5TxU4mzsbNIpbMynbwibGR8ac0dTcf_2qUfGkAEDvQ,52
|
|
15
|
+
cfn_check/evaluation/parsing/query_parser.py,sha256=4J3CJQKAyb11gugfx6OZT-mfSdNDB5Al8Jiy9DbJZMw,3459
|
|
16
|
+
cfn_check/evaluation/parsing/token.py,sha256=Z-KRbQdMmbJTTLqobpzCG-fH5_VzDJIG2mnChhCwub8,6561
|
|
17
|
+
cfn_check/evaluation/parsing/token_type.py,sha256=E5AVBerinBszMLjjc7ejwSSWEc0p0Ju_CNFhpoZi63c,325
|
|
15
18
|
cfn_check/loader/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
16
19
|
cfn_check/loader/loader.py,sha256=7yiDLLW_vNp_8O47erLXjQQtAB47fU3nimb91N5N_R8,532
|
|
17
20
|
cfn_check/logging/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -22,10 +25,10 @@ cfn_check/shared/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,
|
|
|
22
25
|
cfn_check/shared/types.py,sha256=-om3DyZsjK_tJd-I8SITkoE55W0nB2WA3LOc87Cs7xI,414
|
|
23
26
|
cfn_check/validation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
27
|
cfn_check/validation/validator.py,sha256=FGPeb8Uc8lvX3Y5rs-fxeJKIOqzUXwXh_gCFcy6d3b0,1182
|
|
25
|
-
cfn_check-0.
|
|
26
|
-
example/rules.py,sha256
|
|
27
|
-
cfn_check-0.
|
|
28
|
-
cfn_check-0.
|
|
29
|
-
cfn_check-0.
|
|
30
|
-
cfn_check-0.
|
|
31
|
-
cfn_check-0.
|
|
28
|
+
cfn_check-0.3.1.dist-info/licenses/LICENSE,sha256=EbCpGNzOkyQ53ig7J2Iwgmy4Og0dgHe8COo3WylhIKk,1069
|
|
29
|
+
example/rules.py,sha256=-xr-X0tnkV-ill2VRfJKMjctRXeuE-ehTpJ3LKt8dio,2809
|
|
30
|
+
cfn_check-0.3.1.dist-info/METADATA,sha256=GEq5W-bVyzD0gVulqugwSnoDFl1wWjDL3sIPoef-HQ0,19039
|
|
31
|
+
cfn_check-0.3.1.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
|
|
32
|
+
cfn_check-0.3.1.dist-info/entry_points.txt,sha256=B4lCHoDHmwisABxKgRLShwqqFv7QwwDAFXoAChOnkwg,53
|
|
33
|
+
cfn_check-0.3.1.dist-info/top_level.txt,sha256=hUn9Ya50yY1fpgWxEhG5iMgfMDDVX7qWQnM1xrgZnhM,18
|
|
34
|
+
cfn_check-0.3.1.dist-info/RECORD,,
|
example/rules.py
CHANGED
|
@@ -1,12 +1,71 @@
|
|
|
1
1
|
from cfn_check import Collection, Rule
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
class
|
|
4
|
+
class ResourcesChecks(Collection):
|
|
5
|
+
|
|
6
|
+
@Rule("Resources", "Resources is not empty")
|
|
7
|
+
def validate_resources(self, value: dict):
|
|
8
|
+
assert value is not None
|
|
9
|
+
assert isinstance(value, dict)
|
|
5
10
|
|
|
6
|
-
@Rule(
|
|
7
|
-
|
|
8
|
-
"It checks Resource::Type is correctly definined",
|
|
9
|
-
)
|
|
10
|
-
def validate_test(self, value: str):
|
|
11
|
+
@Rule("Resources::*::Type", "Resources::*::Type is correctly definined")
|
|
12
|
+
def validate_resource_type(self, value: str):
|
|
11
13
|
assert value is not None, '❌ Resource Type not defined'
|
|
12
14
|
assert isinstance(value, str), '❌ Resource Type not a string'
|
|
15
|
+
|
|
16
|
+
@Rule(
|
|
17
|
+
"Resources::LambdaExecutionRole::Properties::Policies::[]",
|
|
18
|
+
"Lambad Execution Role Policies are defined",
|
|
19
|
+
)
|
|
20
|
+
def validate_lambda_execution_role_policies(self, value: list[dict]):
|
|
21
|
+
assert isinstance(value, list), '❌ Lambda execution roles is not a list'
|
|
22
|
+
assert len(value) > 0, '❌ No policies specified'
|
|
23
|
+
|
|
24
|
+
@Rule(
|
|
25
|
+
"Resources::LambdaExecutionRole::Properties::Policies::[*]::(Policy*)",
|
|
26
|
+
"Lambad Execution Role Policies are defined",
|
|
27
|
+
)
|
|
28
|
+
def validate_lambda_execution_role_policy_defined(self, value: str):
|
|
29
|
+
assert value is not None, '❌ Lambda execution role policy not defined'
|
|
30
|
+
|
|
31
|
+
@Rule(
|
|
32
|
+
"Resources::AppendItemToListFunction::Properties::Code::ZipFile::[*]::[]",
|
|
33
|
+
"Lambda List Function ZipFile Code is defined",
|
|
34
|
+
)
|
|
35
|
+
def validate_lambda_code_zipfile_is_defined(self, value: list[str]):
|
|
36
|
+
assert value is not None, '❌ Lambda zipfile code is not defined'
|
|
37
|
+
assert isinstance(value, list), '❌ Lambda execution zipfile code not a list'
|
|
38
|
+
assert len(value) > 0,'❌ Lambda execution zipfile code empty'
|
|
39
|
+
|
|
40
|
+
@Rule(
|
|
41
|
+
"Resources::SecurityGroup::Properties::(SecurityGroup)::[]",
|
|
42
|
+
"It checks Security Groups are correctly definined",
|
|
43
|
+
)
|
|
44
|
+
def validate_test(self, value: list[dict]):
|
|
45
|
+
assert len(value) > 0
|
|
46
|
+
|
|
47
|
+
for item in value:
|
|
48
|
+
protocol = item.get("IpProtocol")
|
|
49
|
+
assert isinstance(protocol, str)
|
|
50
|
+
assert protocol == "tcp"
|
|
51
|
+
|
|
52
|
+
from_port = item.get("FromPort")
|
|
53
|
+
assert isinstance(from_port, int)
|
|
54
|
+
assert from_port == 80
|
|
55
|
+
|
|
56
|
+
to_port = item.get('ToPort')
|
|
57
|
+
assert isinstance(to_port, int)
|
|
58
|
+
assert to_port == 80
|
|
59
|
+
|
|
60
|
+
cidr_ip = item.get('CidrIp')
|
|
61
|
+
assert isinstance(cidr_ip, str)
|
|
62
|
+
assert cidr_ip == '0.0.0.0/0'
|
|
63
|
+
|
|
64
|
+
@Rule(
|
|
65
|
+
"Resources::AppendItemToListFunction::Properties::Code::ZipFile::[[]]",
|
|
66
|
+
"Lambda List Function ZipFile Code is defined",
|
|
67
|
+
)
|
|
68
|
+
def validate_lambda_code_zipfile(self, value: list[str]):
|
|
69
|
+
assert value is not None, '❌ Lambda zipfile code is not defined'
|
|
70
|
+
assert isinstance(value, list), '❌ Lambda execution zipfile code not a list'
|
|
71
|
+
assert len(value) > 0,'❌ Lambda execution zipfile code empty'
|
cfn_check/evaluation/check.py
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
from typing import TypeVar
|
|
2
|
-
|
|
3
|
-
from pydantic import ValidationError
|
|
4
|
-
|
|
5
|
-
from cfn_check.validation.validator import Validator
|
|
6
|
-
from cfn_check.shared.types import (
|
|
7
|
-
YamlList,
|
|
8
|
-
YamlObject,
|
|
9
|
-
YamlValueBase,
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
T = TypeVar("T")
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def check(
|
|
16
|
-
matched: YamlList | YamlObject | YamlValueBase,
|
|
17
|
-
assertion: Validator[T],
|
|
18
|
-
) -> Exception | ValidationError | None:
|
|
19
|
-
if err := assertion(matched):
|
|
20
|
-
return err
|