forked from cloud-custodian/cel-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc7n_interface.feature
More file actions
1251 lines (1039 loc) · 39.9 KB
/
c7n_interface.feature
File metadata and controls
1251 lines (1039 loc) · 39.9 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Feature: Libraries are required for C7N integration.
These scenarios are extracted from policy documents in use,
so they can reflect actual policies in use to assure that
C7N constructs can be implemented in CEL.
Scenario Outline: this is the template; fill in the policy and the examples table.
Given policy text
"""
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
Examples: resource_count True
| expected | document |
###########
# Operators
###########
Scenario Outline: EQ Test
Given policy text
"""
filters:
- key: Engine
op: eq
type: value
value: redis
- key: AtRestEncryptionEnabled
op: eq
type: value
value: false
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["Engine"] == "redis" && ! resource["AtRestEncryptionEnabled"]
Examples: EQ True
| expected | document |
| True | {"Engine": "redis", "AtRestEncryptionEnabled": false} |
Examples: EQ False
| expected | document |
| False | {"Engine": "not redis", "AtRestEncryptionEnabled": false} |
| False | {"Engine": "redis", "AtRestEncryptionEnabled": true} |
| False | {"Engine": "not redis", "AtRestEncryptionEnabled": true} |
Scenario Outline: equal Test
Given policy text
"""
filters:
- key: SuspendedProcesses
op: equal
type: value
value: []
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["SuspendedProcesses"] == []
Examples: equal True
| expected | document |
| True | {"SuspendedProcesses": []} |
Examples: Equal False
| expected | document |
| False | {"SuspendedProcesses": ["a", "b"]} |
Scenario Outline: ne Test
Given policy text
"""
filters:
- key: InstanceLifecycle
op: ne
type: value
value: spot
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["InstanceLifecycle"] != "spot"
Examples: ne True
| expected | document |
| True | {"InstanceLifecycle": "not spot"} |
Examples: ne False
| expected | document |
| False | {"InstanceLifecycle": "spot"} |
Scenario Outline: not-equal Test
Given policy text
"""
filters:
- key: length(SuspendedProcesses)
op: not-equal
type: value
value: 8
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is size(resource["SuspendedProcesses"]) != 8
Examples: not-equal True
| expected | document |
| True | {"SuspendedProcesses": ["this", "that"]} |
Examples: not-equal False
| expected | document |
| False | {"SuspendedProcesses": ["this", "that", "this", "that", "this", "that", "this", "that"]} |
Scenario Outline: gt Test
Given policy text
"""
filters:
- key: MinSize
op: gt
type: value
value: 1
- key: DesiredCapacity
op: gt
type: value
value: 1
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["MinSize"] > 1 && resource["DesiredCapacity"] > 1
Examples: gt True
| expected | document |
| True | {"MinSize": 2, "DesiredCapacity": 3} |
Examples: gt False
| expected | document |
| False | {"MinSize": 1, "DesiredCapacity": 3} |
| False | {"MinSize": 1, "DesiredCapacity": 1} |
| False | {"MinSize": 2, "DesiredCapacity": 1} |
Scenario Outline: lt Test
Given policy text
"""
filters:
- key: BackupRetentionPeriod
op: lt
type: value
value: 7
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["BackupRetentionPeriod"] < 7
Examples: lt True
| expected | document |
| True | {"BackupRetentionPeriod": 0} |
Examples: lt False
| expected | document |
| False | {"BackupRetentionPeriod": 7} |
Scenario Outline: glob Test
Given policy text
"""
filters:
- key: Name
op: glob
type: value
value: PRE-*
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["Name"].glob("PRE-*")
Examples: glob True
| expected | document |
| True | {"Name": "PRE-Demo"} |
Examples: glob True
| expected | document |
| False | {"Name": "NOTPRE"} |
Scenario Outline: regex Test
Given policy text
"""
filters:
- key: InstanceType
op: regex
type: value
value: ([cmr]3.*)
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["InstanceType"].matches("([cmr]3.*)")
Examples: regex True
| expected | document |
| True | {"InstanceType": "c3.xxl"} |
Examples: regex False
| expected | document |
| False | {"InstanceType": "doesn't match"} |
Scenario Outline: in Test
Given policy text
"""
filters:
- key: VpcId
op: in
type: value
value:
- vpc-redacted1
- vpc-redacted2
- vpc-redacted3
- vpc-redacted4
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is ['vpc-redacted1', 'vpc-redacted2', 'vpc-redacted3', 'vpc-redacted4'].contains(resource["VpcId"])
Examples: in True
| expected | document |
| True | {"VpcId": "vpc-redacted2"} |
Examples: in False
| expected | document |
| False | {"VpcId": "vpc-not-included"} |
Scenario Outline: ni Test
Given policy text
"""
filters:
- key: InternetGatewayId
op: ni
type: value
value:
- igw-redacted1
- igw-redacted2
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is ! ['igw-redacted1', 'igw-redacted2'].contains(resource["InternetGatewayId"])
Examples: ni True
| expected | document |
| True | {"InternetGatewayId": "igw-some-other-gateway"} |
Examples: ni False
| expected | document |
| False | {"InternetGatewayId": "igw-redacted1"} |
Scenario Outline: not-in Test
Given policy text
"""
filters:
- key: tag:ASSET
op: not-in
type: value
value:
- CLOUDCUSTODIAN
- REDACTED1
- REDACTED2
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is ! ['CLOUDCUSTODIAN', 'REDACTED1', 'REDACTED2'].contains(resource["Tags"].filter(x, x["Key"] == "ASSET")[0]["Value"])
Examples: not-in True
| expected | document |
| True | {"Tags": [{"Key": "ASSET", "Value": "NOTINTHELIST"}]} |
Examples: not-in False
| expected | document |
| False | {"Tags": [{"Key": "ASSET", "Value": "CLOUDCUSTODIAN"}]} |
| CELEvalError | {"Tags": [{"Key": "NOT_ASSET", "Value": "CLOUDCUSTODIAN"}]} |
Scenario Outline: contains Test
Given policy text
"""
filters:
- not:
- key: Engine
op: contains
type: value
value: aurora
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is ! (resource["Engine"].contains("aurora"))
Examples: contains False
| expected | document |
| False | {"Engine": ["this", "that", "aurora"]} |
#################################
# Value_Type Conversion Functions
#################################
Scenario Outline: - 'age' -- ``parse_date(value), datetime.datetime.now(tz=tzutc()) - timedelta(sentinel)``
Note that these are reversed to make it easier to compare age against a given value.
A global ``now`` variable removes the need for an implicit age computation.
Given policy text
"""
description: I think this this older than 2 hours and less than one day?
filters:
- key: CreatedTimestamp
op: gte
type: value
value: 0.084
value_type: age
- key: CreatedTimestamp
op: lte
type: value
value: 1
value_type: age
"""
And resource value <document>
And now value <now>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is now - duration("2h57s") >= timestamp(resource["CreatedTimestamp"]) && now - duration("1d") <= timestamp(resource["CreatedTimestamp"])
Examples: age True
| expected | now | document |
| True | 2020-09-10T13:14:15Z | {"CreatedTimestamp": "2020-09-10T11:12:13Z"} |
Scenario Outline: - 'integer' -- ``sentinel, int(str(value).strip())``
Given policy text
"""
filters:
- key: ProvisionedThroughput.ReadCapacityUnits
op: ne
type: value
value: 0
value_type: integer
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is int(resource["ProvisionedThroughput"]["ReadCapacityUnits"]) != 0
Examples: integer True
| expected | document |
| True | {"ProvisionedThroughput": {"ReadCapacityUnits": "2 "}} |
Examples: integer False
| expected | document |
| False | {"ProvisionedThroughput": {"ReadCapacityUnits": " 0"}} |
Scenario Outline: - 'expiration' -- ``datetime.datetime.now(tz=tzutc()) + timedelta(sentinel), parse_date(value)``
A global ``now`` variable removes the need for an implicit expiration computation.
Given policy text
"""
filters:
- key: NotAfter
op: lt
type: value
value: 10
value_type: expiration
"""
And resource value <document>
And now value <now>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is timestamp(resource["NotAfter"]) < now + duration("10d")
Examples: expiration True
| expected | now | document |
| True | 2020-09-12T13:14:15Z | {"NotAfter": "2020-09-10T11:12:13Z"} |
Examples: expiration False
| expected | now | document |
| True | 2020-10-12T13:14:15Z | {"NotAfter": "2020-09-10T11:12:13Z"} |
Scenario Outline: - 'normalize' -- ``sentinel, value.strip().lower()``
Given policy text
"""
filters:
- key: tag:Uptime
op: in
type: value
value:
- 08-19-weekend-off
- 8x5
value_type: normalize
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is ['08-19-weekend-off', '8x5'].contains(normalize(resource["Tags"].filter(x, x["Key"] == "Uptime")[0]["Value"]))
Examples: normalize True
| expected | document |
| True | {"Tags": [{"Key": "Uptime", "Value": "08-19-WEEKEND-OFF"}]} |
Examples: normalize False
| expected | document |
| False | {"Tags": [{"Key": "Uptime", "Value": "24x7"}]} |
Scenario Outline: - 'size' -- ``sentinel, len(value)``
Given policy text
"""
filters:
- key: VpcConfig.SubnetIds
op: gt
type: value
value: 3
value_type: size
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is size(resource["VpcConfig"]["SubnetIds"]) > 3
Examples: size True
| expected | document |
| True | {"VpcConfig": {"SubnetIds": ["one", "two", "three", "four"]}} |
Examples: size False
| expected | document |
| False | {"VpcConfig": {"SubnetIds": ["one", "two", "three"]}} |
Scenario Outline: - 'cidr' -- ``parse_cidr(sentinel), parse_cidr(value)``
There are no examples, currently. This is a fabricated test case.
Given policy text
"""
filters:
- key: Address
op: in
type: value
value: "127.0.0.0/22"
value_type: cidr
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is parse_cidr("127.0.0.0/22").contains(parse_cidr(resource["Address"]))
Examples: cidr True
| expected | document |
| True | {"Address": "127.0.0.1"} |
Scenario Outline: - 'cidr_size' -- ``sentinel, parse_cidr(value).prefixlen``
There are only two examples of this, and they're not in ``type: value`` filters.
This is a fabricated test case.
Given policy text
"""
filters:
- key: Egress.Cidr
op: lt
type: value
value: 24
value_type: cidr_size
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is size_parse_cidr(resource["Egress"]["Cidr"]) < 24
Examples: cidr_size True
| expected | document |
| True | {"Egress": {"Cidr": "127.0.0.0/22"}} |
Scenario Outline: - 'swap' -- ``value, sentinel``
This was needed because the implied order of DSL operands.
Without ``swap``, the operation is *resource OP filter-value*.
With ``swap`` it's *filter-value OP resource*.
Given policy text
"""
filters:
- key: tag:Name
op: not-in
type: value
value: Default
value_type: swap
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is ! resource["Tags"].filter(x, x["Key"] == "Name")[0]["Value"].contains("Default")
Examples: swap True
| expected | document |
| True | {"Tags": [{"Key": "Name", "Value": "SomeTagName"}]} |
Examples: swap False
| expected | document |
| False | {"Tags": [{"Key": "Name", "Value": "ADefaultTagName"}]} |
Scenario Outline: - 'unique_size' -- ``len(set(value))``
There are no examples, currently. This is a fabricated test case.
Given policy text
"""
filters:
- key: VpcConfig.SubnetIds
op: gt
type: value
value: 3
value_type: unique_size
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is unique_size(resource["VpcConfig"]["SubnetIds"]) > 3
Examples: unique_size True
| expected | document |
| True | {"VpcConfig": {"SubnetIds": ["one", "two", "two", "three", "four"]}} |
Examples: unique_size False
| expected | document |
| False | {"VpcConfig": {"SubnetIds": ["one", "two", "two", "three", "three", "three"]}} |
Scenario Outline: - 'date' -- ``parse_date(sentinel), parse_date(value)``
There are no examples, currently. This is a fabricated test case.
Given policy text
"""
filters:
- key: CreatedTimestamp
op: lte
type: value
value: "2020-09-10T11:12:13Z"
value_type: date
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is timestamp(resource["CreatedTimestamp"]) <= timestamp("2020-09-10T11:12:13Z")
Examples: date True
| expected | document |
| True | {"CreatedTimestamp": "2020-09-10T11:12:12Z"} |
Scenario Outline: - 'version' -- ``ComparableVersion(sentinel), ComparableVersion(value)``
There are no examples, currently. This is a fabricated test case.
Given policy text
"""
filters:
- key: Version
op: gte
type: value
value: "3.6"
value_type: version
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is version(resource["Version"]) >= version("3.6")
Examples: version True
| expected | document |
| True | {"Version": "3.7.18"} |
Examples: version False
| expected | document |
| False | {"Version": "2.7.18"} |
Scenario Outline: - 'expr' -- ``self.get_resource_value(sentinel, resource)``
This is part of value_from processing more than it is part of filter
processing.
Given policy text
"""
filters:
- key: IamInstanceProfile.Arn
op: ni
type: value
value_from:
url: "s3://c7n-resources/exemptions.json"
format: json
expr: exemptions.ec2.rehydration.["IamInstanceProfile.Arn"][].*[].*[]
"""
And resource value <document>
And url s3://c7n-resources/exemptions.json has text
"""
{
"exemptions": {
"ec2": {
"rehydration": [
{
"IamInstanceProfile": {
"Arn": ["list", "of", "arn"]
}
}
]
}
}
}
"""
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is ! value_from("s3://c7n-resources/exemptions.json", "json").jmes_path('exemptions.ec2.rehydration.["IamInstanceProfile.Arn"][].*[].*[]').contains(resource["IamInstanceProfile"]["Arn"])
Examples: expr True
| expected | document |
| True | {"IamInstanceProfile": {"Arn": "arn-account-id-etc"}} |
Scenario Outline: - 'resource_count' -- the op is applied to len(resources) instead of the resources.
The semantic difference between resource_count and size is whether or not a number of resources
must be queried or the filter examines a number of values available from a describe.
There are no examples, currently; this is a place-holder for future tests.
Given policy text
"""
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is some_cel_code
Examples: resource_count True
| expected | document |
##########################
# Marked-for-op processing
##########################
Scenario Outline: Marked-for-Op
Given policy text
"""
filters:
- op: terminate
skew: 4
tag: c7n-tag-compliance
type: marked-for-op
"""
And resource value <document>
And now value <now>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource["Tags"].marked_key("c7n-tag-compliance").action == "terminate" && now >= resource["Tags"].marked_key("c7n-tag-compliance").action_date - duration("4d0h")
Examples: marked-for-op True
| expected | now | document |
| True | 2020-09-10T11:12:13Z | {"Tags": [{"Key": "c7n-tag-compliance", "Value": "hello:terminate@2020-09-01"}]} |
| True | 2020-09-10T11:12:13Z | {"Tags": [{"Key": "c7n-tag-compliance", "Value": "hello:terminate@2020-09-13"}]} |
Examples: marked-for-op False
| expected | now | document |
| False | 2020-09-10T11:12:13Z | {"Tags": [{"Key": "c7n-tag-compliance", "Value": "hello:terminate@2020-09-15"}]} |
###########
# Image-age
###########
Scenario Outline: EC2 and ASG resources have an associated Image resource.
The Image resource, has a CreationDate attribute.
Note that this test includes a common feature of C7N policies: an OR clause with one term.
Given policy text
"""
filters:
- or:
- days: 60
op: gt
type: image-age
"""
And resource value <document>
And now value <now>
And C7N.filter has get_instance_image result with CreateDate of <image CreateDate>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is now - resource.image().CreationDate > duration("60d")
Examples: image-age True
| expected | now | document | image CreateDate |
| True | 2020-09-10T11:12:13Z | {"ResourceType": "ec2"} | 2019-09-10T11:12:13Z |
Examples: image-age False
| expected | now | document | image CreateDate |
| False | 2020-09-10T11:12:13Z | {"ResourceType": "ec2"} | 2020-07-12T11:12:13Z |
#######
# event
#######
Scenario Outline: Lambda resources have an associated Cloud Trail event resource.
We only provide the barest minimum of an event-like document to mock C7N's event details.
Given policy text
"""
filters:
- key: detail.responseElements.functionName
op: regex
type: event
value: ^(custodian-.*)
"""
And resource value <document>
And event value <event>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is event.detail.responseElements.functionName.matches("^(custodian-.*)")
Examples: image-age True
| expected | document | event |
| True | {"ResourceType": "lambda"} | {"detail": {"responseElements": {"functionName": "custodian-yes"}}} |
Examples: image-age False
| expected | document | event |
| False | {"ResourceType": "lambda"} | {"detail": {"responseElements": {"functionName": "nope"}}} |
#########
# metrics
#########
# get_raw_metrics isn't tested directly, since it's not clear it needs to be exposed.
Scenario Outline: resources have associated CloudWatch metrics and metrics statistics.
We only provide the barest minimum of an event-like document to mock C7N's event details.
Given policy text
"""
filters:
- type: metrics
name: CPUUtilization
days: 4
period: 86400
value: 30
op: less-than
"""
And resource value <document>
And now value <now>
And C7N.filter manager has get_model result of InstanceId
And C7N.filter has get_metric_statistics result with <raw_metrics>
And C7N.filter has resource type of ec2
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource.get_metrics({"MetricName": "CPUUtilization", "Statistic": "Average", "StartTime": now - duration("4d"), "EndTime": now, "Period": duration("1d")}).exists(m, m < 30)
Examples: metrics True
| expected | document | now | raw_metrics |
| True | {"ResourceType": "ec2", "InstanceId": "i-123456789012"} | 2020-09-10T11:12:13Z | {"Datapoints": [{"Average": 1}, {"Average": 3}, {"Average": 5}]} |
Examples: metrics False
| expected | document | now | raw_metrics |
| False | {"ResourceType": "ec2", "InstanceId": "i-123456789012"} | 2020-09-10T11:12:13Z | {"Datapoints": [{"Average": 31}, {"Average": 33}, {"Average": 35}]} |
######################
# age
######################
Scenario Outline: Snapshot Age Filters for a variety of resource types:
- Filter ASG launch configuration by age (in days) date_attribute = "CreatedTime"
- Filters an EBS snapshot based on the age of the snapshot (in days) date_attribute = 'StartTime'
- Filters elasticache snapshots based on their age (in days)
The earliest of the node snaphot creation times; requires a yet-to-be implemented min() macro.
resource.NodeSnaphots.min(x, x.SnapshotCreateTime)
- Filters RDS snapshots based on age (in days) date_attribute = 'SnapshotCreateTime'
- Filters rds cluster snapshots based on age (in days) date_attribute = 'SnapshotCreateTime'
- Filters redshift snapshots based on age (in days) date_attribute = 'SnapshotCreateTime'
Given policy text
"""
filters:
- days: 21
op: gt
type: age
resource: ebs-snapshot
"""
And resource value <document>
And now value <now>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is now - timestamp(resource.StartTime) > duration("21d")
Examples: age True
| expected | now | document |
| True | 2020-09-10T11:12:13Z | {"ResourceType": "ebs-snapshot", "StartTime": "2020-01-18T19:20:21Z"} |
Examples: age False
| expected | now | document |
| False | 2020-09-10T11:12:13Z | {"ResourceType": "ebs-snapshot", "StartTime": "2020-09-09T11:12:13Z"} |
######################
# security-group
######################
Scenario Outline: Security Group Details for a variety of resource types.
Each has a slight variation in the reference to the related item.
- "app-elb": "resource.SecurityGroups.map(sg, sg.security_group())",
- "asg": "resource.get_related_ids().map(sg. sg.security_group())",
- "lambda": "VpcConfig.SecurityGroupIds.map(sg, sg.security_group())",
- "batch-compute": "resource.computeResources.securityGroupIds.map(sg, sg.security_group())",
- "codecommit": "resource.vpcConfig.securityGroupIds.map(sg, sg.security_group())",
- "directory": "resource.VpcSettings.SecurityGroupId.security_group()",
- "dms-instance": "resource.VpcSecurityGroups.map(sg, sg.VpcSecurityGroupId.security_group())",
- "dynamodb-table": "resource.SecurityGroups.map(sg, sg.SecurityGroupIdentifier.security_group())",
- "ec2": "resource.SecurityGroups.map(sg. sg.GroupId.security_group())",
- "efs": "resource.get_related_ids().map(sg. sg.security_group())",
- "eks": "resource.resourcesVpcConfig.securityGroupIds.map(sg, sg.security_group())",
- "cache-cluster": "resource.SecurityGroups.map(sg, sg.SecurityGroupId.security_group())",
- "elasticsearch": "resource.VPCOptions.SecurityGroupIds.map(sg, sg.security_group())",
- "elb": "resource.SecurityGroups.map(sg, sg.security_group())",
- "glue-connection": "resource.PhysicalConnectionRequirements.SecurityGroupIdList.map(sg, sg.security_group())",
- "kafka": "resource.BrokerNodeGroupInfo.SecurityGroups[.map(sg, sg.security_group())",
- "message-broker": "resource.SecurityGroups[.map(sg, sg.security_group())",
- "rds": "resource.VpcSecurityGroups.map(sg, sg.VpcSecurityGroupId.security_group())",
- "rds-cluster": "resource.VpcSecurityGroups.map(sg, sg.VpcSecurityGroupId.security_group())",
- "redshift": "resource.VpcSecurityGroups.map(sg, sg.VpcSecurityGroupId.security_group())",
- "sagemaker-notebook": "resource.SecurityGroups[.map(sg, sg.security_group())",
- "vpc": "resource.get_related_ids().map(sg. sg.security_group())",
- "eni": "resource.Groups.map(sg, sg.GroupId.security_group())",
- "vpc-endpoint": "resource.Groups.map(sg, sg.GroupId.security_group())",
Given policy text
"""
filters:
- key: tag:ASSET
op: eq
type: security-group
value: SPECIALASSETNAME
resource: app-elb
"""
And resource value <document>
And C7N.filter has get_related result with <sg>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is resource.SecurityGroups.map(sg, sg.security_group()).exists(sg, sg["Tags"].filter(x, x["Key"] == "ASSET")[0]["Value"] == 'SPECIALASSETNAME')
Examples: security-group True
| expected | document | sg |
| True | {"ResourceType": "ebs-snapshot", "SecurityGroups": ["sg-12345678"]} | {"SecurityGroup": "sg-12345678", "Tags": [{"Key": "ASSET", "Value": "SPECIALASSETNAME"}]} |
######################
# subnet
######################
Scenario Outline: Subnet Details for a variety of resource types.
Each can have a slight variation in the reference to the related item.
However, since there's an explicit `Key` field in the filter clause, the underlying
resource type may not matter.
- aws.cache-cluster,
- aws.codebuild,
- aws.asg,
- aws.route-table,
- aws.vpc-endpoint,
- aws.eks,
- aws.efs-mount-target,
- aws.elasticsearch,
- aws.message-broker,
- aws.redshift,
- aws.rds,
- aws.glue-connection,
- aws.sagemaker-notebook,
- aws.directory,
- aws.eni,
- aws.app-elb,
- aws.lambda,
- aws.network-acl,
- aws.dax,
- aws.rds-cluster,
- aws.batch-compute,
- aws.ec2,
- aws.elb,
- aws.dms-instance
Given policy text
"""
filters:
- key: SubnetId
op: in
type: subnet
value_from:
format: txt
url: s3://path-to-resource/subnets.txt
value_type: normalize
"""
And resource value <document>
And url s3://path-to-resource/subnets.txt has text
"""
some
list
subnet-12345678
subnet-23456789
"""
And C7N.filter has get_related result with <subnet>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is value_from("s3://path-to-resource/subnets.txt", "txt").map(v, normalize(v)).contains(resource.SubnetId.subnet().SubnetID)
Examples: subnet True
| expected | document | subnet |
| True | {"ResourceType": "asg", "SubnetId": "subnet-12345678"} | {"SubnetID": "subnet-12345678"} |
Examples: subnet False
| expected | document | subnet |
| False | {"ResourceType": "asg", "SubnetId": "subnet-87654321"} | {"SubnetID": "subnet-87654321"} |
######################
# flow-logs
######################
Scenario Outline: Some resource types (vpc, eni, and subnet) have flow-log settings.
C7N can check a variety of attributes: destination, destination-type, enabled,
log-group, status, and traffic-type. Pragmatically, we see only enabled and destination-type
Given policy text
"""
filters:
- or:
- enabled: false
type: flow-logs
- not:
- destination-type: s3
enabled: true
type: flow-logs
name: enterprise-enable-vpc-flow-logs-s3
resource: vpc
"""
And resource value <document>
And C7N.filter manager has get_model result of InstanceId
And C7N.filter has flow_logs result with <flow-logs>
When CEL filter is built and evaluated
Then CEL text is size(resource.flow_logs()) == 0 || ! (size(resource.flow_logs()) != 0 && (resource.flow_logs().exists(x, x.LogDestinationType == "s3")))
Then result is <expected>
Examples: flow-logs True
| expected | document | flow-logs |
| True | {"InstanceId": "i-123456789", "ResourceType": "vpc"} | [{"ResourceId": "i-123456789", "LogDestinationType": "cloud-watch-logs"}] |
######################
# tag-count
######################
Scenario Outline: For resource types with tags, we can check the number of tags.
C7N filters out tags that start with "aws:".
Given policy text
"""
filters:
- type: tag-count
count: 8
"""
And resource value <document>
When CEL filter is built and evaluated
Then result is <expected>
And CEL text is size(resource["Tags"].filter(x, ! matches(x.Key, "^aws:.*"))) >= 8
Examples: tag-count True
| expected | document |