-
Notifications
You must be signed in to change notification settings - Fork 240
/
tfplan-functions.sentinel
1153 lines (1091 loc) · 43.6 KB
/
tfplan-functions.sentinel
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
# Common functions that use the tfplan/v2 import
# The filter functions all accept a collection of resource changes, an attribute,
# a value or a list of values, and a boolean, prtmsg, which can be true or false
# and indicates whether the filter function should print violation messages.
# The filter functions return a map consisting of 2 items:
# * "resources": a map consisting of resource changes that violate a condition
# * "messages": a map of violation messages associated with the resources
# Note that both the resources and messages collections are indexed by the
# address of the resources, so they will have the same order and length.
# The filter functions all call evaluate_attribute() to evaluate attributes
# of resources even if nested deep within them.
##### Imports #####
import "tfplan/v2" as tfplan
import "strings"
import "types"
##### Functions #####
### find_resources ###
# Find all resources of a specific type using the tfplan/v2 import.
# Include resources that are not being permanently deleted.
# Technically, this returns a map of resource changes.
find_resources = func(type) {
resources = filter tfplan.resource_changes as address, rc {
rc.type is type and
rc.mode is "managed" and
(rc.change.actions contains "create" or rc.change.actions contains "update" or
rc.change.actions contains "read" or (rc.change.actions contains "no-op" and
rc.change.after is not null))
}
return resources
}
### find_resources_by_provider ###
# Find all resources for a specific provider using the tfplan/v2 import.
# Include resources that are not being permanently deleted.
# Technically, this returns a map of resource changes.
# Terraform 0.12 and earlier set `rc.provider_name` to short text like "null";
# but Terraform 0.13 and higher set it to something like
# "registry.terraform.io/hashicorp/null".
# You can pass in the long form or short form.
find_resources_by_provider = func(provider) {
parsed_provider = strings.split(provider, "/")
segment_count = length(parsed_provider)
v = strings.split(tfplan.terraform_version, ".")
v_major = int(v[1])
# If v_major is 12, we know short form was passed to Sentinel
if v_major is 12 {
resources = filter tfplan.resource_changes as address, rc {
rc.provider_name is provider and
rc.mode is "managed" and
(rc.change.actions contains "create" or rc.change.actions contains "update" or
rc.change.actions contains "read" or (rc.change.actions contains "no-op" and
rc.change.after is not null))
}
} else {
# v_major must be higher than 12 since v2 imports being used
# So, we know long form like "registry.terraform.io/hashicorp/null" given
if segment_count is 1 {
# Function was passed short form, so we want to reduce each occurence of
# rc.provider_name to its short form.
resources = filter tfplan.resource_changes as address, rc {
strings.split(rc.provider_name, "/")[2] is provider and
rc.mode is "managed" and
(rc.change.actions contains "create" or rc.change.actions contains "update" or
rc.change.actions contains "read" or (rc.change.actions contains "no-op" and
rc.change.after is not null))
}
} else {
# Function was passed long form, so we use full rc.provider_name
resources = filter tfplan.resource_changes as address, rc {
rc.provider_name is provider and
rc.mode is "managed" and
(rc.change.actions contains "create" or rc.change.actions contains "update" or
rc.change.actions contains "read" or (rc.change.actions contains "no-op" and
rc.change.after is not null))
}
} // end segment_count
} // end v_major
return resources
}
### find_datasources ###
# Find all data sources of a specific type using the tfplan/v2 import.
# Include data sources that are not being permanently deleted.
# Technically, this returns a map of resource changes.
find_datasources = func(type) {
datasources = filter tfplan.resource_changes as address, rc {
rc.type is type and
rc.mode is "data" and
(rc.change.actions contains "create" or
rc.change.actions contains "update" or
rc.change.actions contains "read" or
(rc.change.actions contains "no-op" and rc.change.after is not null))
}
return datasources
}
### find_datasources_by_provider ###
# Find all data sources for a specific provider using the tfplan/v2 import.
# Include data sources that are not being permanently deleted.
# Technically, this returns a map of resource changes.
# Terraform 0.12 and earlier set `rc.provider_name` to short text like "null";
# but Terraform 0.13 and higher set it to something like
# "registry.terraform.io/hashicorp/null".
# You can pass in the long form or short form.
find_datasources_by_provider = func(provider) {
parsed_provider = strings.split(provider, "/")
segment_count = length(parsed_provider)
v = strings.split(tfplan.terraform_version, ".")
v_major = int(v[1])
# If v_major is 12, we know short form was passed to Sentinel
if v_major is 12 {
datasources = filter tfplan.resource_changes as address, rc {
rc.provider_name is provider and
rc.mode is "data" and
(rc.change.actions contains "create" or rc.change.actions contains "update" or
rc.change.actions contains "read" or (rc.change.actions contains "no-op" and
rc.change.after is not null))
}
} else {
# v_major must be higher than 12 since v2 imports being used
# So, we know long form like "registry.terraform.io/hashicorp/null" given
if segment_count is 1 {
# Function was passed short form, so we want to reduce each occurence of
# rc.provider_name to its short form.
datasources = filter tfplan.resource_changes as address, rc {
strings.split(rc.provider_name, "/")[2] is provider and
rc.mode is "data" and
(rc.change.actions contains "create" or rc.change.actions contains "update" or
rc.change.actions contains "read" or (rc.change.actions contains "no-op" and
rc.change.after is not null))
}
} else {
# Function was passed long form, so we use full rc.provider_name
datasources = filter tfplan.resource_changes as address, rc {
rc.provider_name is provider and
rc.mode is "data" and
(rc.change.actions contains "create" or rc.change.actions contains "update" or
rc.change.actions contains "read" or (rc.change.actions contains "no-op" and
rc.change.after is not null))
}
} // end segment_count
} // end v_major
return datasources
}
### find_resources_being_destroyed ###
# Find all resources being destroyed but not recreated using the tfplan/v2 import.
# Technically, this returns a map of resource changes.
find_resources_being_destroyed = func() {
resources = filter tfplan.resource_changes as address, rc {
rc.mode is "managed" and
rc.change.actions contains "delete" and
not (rc.change.actions contains "create" or rc.change.actions contains "update")
}
return resources
}
### find_datasources_being_destroyed ###
# Find all data sources being destroyed but not recreated using the tfplan/v2 import.
# Technically, this returns a map of resource changes.
find_datasources_being_destroyed = func() {
datasources = filter tfplan.resource_changes as address, rc {
rc.mode is "data" and
rc.change.actions contains "delete" and
not (rc.change.actions contains "create" or
rc.change.actions contains "update")
}
return datasources
}
### find_blocks ###
# Find all blocks of a specific type from a resource using the tfplan/v2 import.
# parent should be a single resource or block of a resource or a data source
# or a block of a data source.
# If parent is a resource, you can pass it in the form rc.change.after or just rc.
# child should be a string representing a block of parent
# that contains a list of objects.
find_blocks = func(parent, child) {
# Use parent.change.after if it exists
if (types.type_of(parent) is "map" and
"change" in keys(parent)) and
(types.type_of(parent.change) is "map" and
"after" in keys(parent.change)) {
if types.type_of(parent.change.after[child] else null) is "list" {
return parent.change.after[child]
} else {
return []
}
} else {
if types.type_of(parent[child] else null) is "list" {
return parent[child]
} else {
return []
}
}
}
### to_string ###
# Convert objects of unknown type to string
# It is used to build messages added to the messages map returned by the
# filter functions
to_string = func(obj) {
case types.type_of(obj) {
when "string":
return obj
when "int", "float", "bool":
return string(obj)
when "null":
return "null"
when "undefined":
return "undefined"
when "list":
output = "["
lastIndex = length(obj) - 1
for obj as index, value {
if index < lastIndex {
output += to_string(value) + ", "
} else {
output += to_string(value)
}
}
output += "]"
return output
when "map":
output = "{"
theKeys = keys(obj)
lastIndex = length(theKeys) - 1
for theKeys as index, key {
if index < lastIndex {
output += to_string(key) + ": " + to_string(obj[key]) + ", "
} else {
output += to_string(key) + ": " + to_string(obj[key])
}
}
output += "}"
return output
else:
return ""
}
}
### evaluate_attribute ###
# Evaluates the value of a resource's or block's attribute even if nested.
# The resource should be derived by applying filters to tfplan.resource_changes.
# It can be given in the initial call in the form rc.change.after or just rc.
# If you want to evaluate previous values, pass `rc.change.before` instead of
# `rc` since the function converts `rc` by itself to `rc.change.after`.
# Indices of lists should be given as 0, 1, 2, and so on.
# For example: boot_disk.0.initialize_params.0.image
evaluate_attribute = func(r, attribute) {
# Split the attribute into a list, using "." as the separator
attributes = strings.split(attribute, ".")
# Convert numeric strings to integers for indices
if attributes[0] matches "^[0-9]+$" {
a = int(attributes[0])
# Make sure r is of type list
if types.type_of(r) is not "list" {
return undefined
}
} else {
a = attributes[0]
}
# Append the current attribute to the resource instance
if (types.type_of(r) is "map" and "change" in keys(r)) and
(types.type_of(r.change) is "map" and "after" in keys(r.change)) {
new_r = r.change.after[a] else null
} else {
new_r = r[a] else null
}
# Process based on length of attributes
# being greater than or equal to 1
if length(attributes) > 1 {
# Strip first element from attributes
attributes = attributes[1:length(attributes)]
attribute = strings.join(attributes, ".")
# Make recursive call
return evaluate_attribute(new_r, attribute)
} else {
# We reached the end of the attribute and can stop the
# recursive calls and return the value of the attribute
return new_r
}
}
### print_violations ###
# Prints violations returned by any of the filter functions defined below.
# This would normally only be called if the filter function had been called
# with prtmsg set to false, which is sometimes done when processing resources
# and their blocks.
# If the result of a filter function is assigned to a map like violatingIRs,
# then you should pass violatingIRs["message"] as the first argument.
# The prefix argument is printed before the message of each resource.
print_violations = func(messages, prefix) {
for messages as address, message {
print(prefix, message)
}
return true
}
### filter_attribute_not_in_list ###
# Filter a list of resources to those with a specified
# attribute (attr) that is not in a given list of allowed values (allowed).
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to allow null, include "null" in the list (allowed).
filter_attribute_not_in_list = func(resources, attr, allowed, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
# Evaluate the value (v) of the attribute
v = evaluate_attribute(rc, attr) else null
# Convert null to "null"
if v is null {
v = "null"
}
# Check if the value is not in the allowed list
if v not in allowed {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is not in the allowed list: " +
to_string(allowed)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} // end if
} // end for
return {"resources":violators,"messages":messages}
}
### filter_attribute_in_list ###
# Filter a list of resources to those with a specified
# attribute (attr) that is in a given list of forbidden values (forbidden).
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to disallow null, include "null" in the list (forbidden).
filter_attribute_in_list = func(resources, attr, forbidden, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
# Evaluate the value (v) of the attribute
v = evaluate_attribute(rc, attr) else null
# Check if the value is null
if v is null {
v = "null"
}
# Check if the value is in the forbidden list
if v in forbidden {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is in the forbidden list: " +
to_string(forbidden)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_not_contains_list ###
# Filter a list of resources to those with a specified
# attribute (attr) that does not contain a given list of required values (required).
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# Resources should be derived by applying filters to tfplan.resource_changes.
filter_attribute_not_contains_list = func(resources, attr, required, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
# Evaluate the value (v) of the attribute
v = evaluate_attribute(rc, attr) else null
# Check if the value contains the desired allowed list
if v is null or
not (types.type_of(v) is "list" or types.type_of(v) is "map") {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is missing, null, or is not a map or a list. " + "It should have had these items: " + to_string(required)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else {
missing_values = []
for required as rv {
if v not contains rv {
append(missing_values, rv)
} // end if
} // end for required
if length(missing_values) > 0 {
# Build warning message when v is a map
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is missing the required items " +
to_string(missing_values) + " from the list: " +
to_string(required)
# Add the resource and warning message to the violators list
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} // end length(missing_values)
} // end else v not null
} // end for
return {"resources":violators,"messages":messages}
}
### filter_attribute_contains_items_from_list ###
# Filter a list of resources to those with a specified
# attribute (attr) that contains any items from a given list of
# forbidden values (forbidden).
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to disallow null, include "null" in the list (forbidden).
filter_attribute_contains_items_from_list = func(resources, attr, forbidden, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
# Evaluate the value (v) of the attribute
v = evaluate_attribute(rc, attr) else null
# Check if the value contains the desired allowed list
if not (types.type_of(v) is "list" or types.type_of(v) is "map" or
types.type_of(v) is "null") {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is not a map, a list, or null"
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if v is null {
if "null" in forbidden {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" with value null from the forbidden list: " +
to_string(forbidden)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
} else {
forbidden_values = []
for forbidden as fv {
if v contains fv {
append(forbidden_values, fv)
} // end if
} // end for forbidden
if length(forbidden_values) > 0 {
# Build warning message when v is a list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that has items " + to_string(forbidden_values) +
" from the forbidden list: " + to_string(forbidden)
# Add the resource and a warning message to the violators list
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} // end length(forbidden_values)
} // end v not null
} // end for
return {"resources":violators,"messages":messages}
}
### filter_attribute_contains_items_not_in_list ###
# Filter a list of resources to those with a specified
# attribute (attr) that contains items not in a given list of allowed values.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to allow null, include "null" in the list (allowed).
filter_attribute_contains_items_not_in_list = func(resources, attr, allowed, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
# Evaluate the value (vals) of the attribute
vals = evaluate_attribute(rc, attr) else null
# Check if the value contains items not in allowed list
if not (types.type_of(vals) is "list" or types.type_of(vals) is "map" or
types.type_of(vals) is "null") {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(vals) + " that is not a map, a list, or null"
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if vals is null {
if "null" not in allowed {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" with value null that is not in the allowed list: " +
to_string(allowed)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
} else {
forbidden_values = []
for vals as v {
if v not in allowed {
append(forbidden_values, v)
} // end if v not allowed
} // end for vals
if length(forbidden_values) > 0 {
# Build warning message when vals is a map
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(vals) + " with items " + to_string(forbidden_values) +
" that are not in the allowed list: " + to_string(allowed)
# Add the resource and a warning message to the violators list
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} // end length(forbidden_values)
} // end if null
} // end for
return {"resources":violators,"messages":messages}
}
### filter_attribute_is_not_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that does not have a given value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_is_not_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It is supposed to be " +
to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if v is not value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is not equal to " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_was_not_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that did not have a given value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to match null, set value to "null".
# Note that it this function passes `rc.change.before` instead of `rc`
# to the evaluate_attribute() function which converts `rc` to `rc.change.after`.
filter_attribute_was_not_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc.change.before, attr) else null
if v is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " had " + to_string(attr) +
" that was null or undefined. " + "It was supposed to be " +
to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if v is not value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " had " + to_string(attr) + " with value " +
to_string(v) + " that was not equal to " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_is_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that has a given value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to match null, set value to "null".
filter_attribute_is_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
v = "null"
}
if v is value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is not allowed."
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_was_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that had a given value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to match null, set value to "null".
# Note that it this function passes `rc.change.before` instead of `rc`
# to the evaluate_attribute() function which converts `rc` to `rc.change.after`.
filter_attribute_was_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc.change.before, attr) else null
if v is null {
v = "null"
}
if v is value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " had " + to_string(attr) + " with value " +
to_string(v) + " that was not allowed."
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_greater_than_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that is greater than a given numeric value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_greater_than_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if float(v) else null is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It is supposed to be less " +
"than or equal to " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if float(v) > value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is greater than " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_greater_than_equal_to_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that is greater than a given numeric value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_greater_than_equal_to_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if float(v) else null is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It is supposed to be less " +
"than " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if float(v) >= value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is greater than or equal to " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_less_than_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that is less than a given numeric value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_less_than_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if float(v) else null is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It is supposed to be greater " +
"than or equal to " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if float(v) < value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is less than " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_less_than_equal_to_value ###
# Filter a list of resources to those with a specified
# attribute (attr) that is less than a given numeric value.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_less_than_equal_to_value = func(resources, attr, value, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if float(v) else null is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It is supposed to be greater " +
"than " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if float(v) <= value {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is less than or equal to " + to_string(value)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_does_not_match_regex ###
# Filter a list of resources to those with a specified
# attribute (attr) that does not match a regular expression (expr).
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to allow null, set `expr` to "(<regex>|null)".
filter_attribute_does_not_match_regex = func(resources, attr, expr, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
v = "null"
}
if v not matches expr {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that does not match the regex " + to_string(expr)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_matches_regex ###
# Filter a list of resources to those with a specified
# attribute (attr) that matches a regular expression (expr).
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
# If you want to match null, set expr to "null".
filter_attribute_matches_regex = func(resources, attr, expr, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
v = "null"
}
if v matches expr {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that matches the regex " + to_string(expr)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_does_not_have_prefix ###
# Filter a list of resources to those with a specified
# attribute (attr) that does not have a given prefix.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_does_not_have_prefix = func(resources, attr, prefix, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It must have a value that " +
"starts with " + to_string(prefix)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if not strings.has_prefix(v, prefix) {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that does not have the prefix " + to_string(prefix)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_has_prefix ###
# Filter a list of resources to those with a specified
# attribute (attr) that has a given prefix.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_has_prefix = func(resources, attr, prefix, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It must have a value that " +
"does not start with " + to_string(prefix)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if strings.has_prefix(v, prefix) {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that has the prefix " + to_string(prefix)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_does_not_have_suffix ###
# Filter a list of resources to those with a specified
# attribute (attr) that does not have a given suffix.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_does_not_have_suffix = func(resources, attr, suffix, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It must have a value that " +
"ends with " + to_string(prefix)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if not strings.has_suffix(v, suffix) {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that does not have the suffix " + to_string(suffix)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
}
}
return {"resources":violators,"messages":messages}
}
### filter_attribute_has_suffix ###
# Filter a list of resources to those with a specified
# attribute (attr) that has a given suffix.
# Resources should be derived by applying filters to tfplan.resource_changes.
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_has_suffix = func(resources, attr, suffix, prtmsg) {
violators = {}
messages = {}
for resources as address, rc {
v = evaluate_attribute(rc, attr) else null
if v is null {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) +
" that is null or undefined. " + "It must have a value that " +
"does not end with " + to_string(suffix)
violators[address] = rc
messages[address] = message
if prtmsg {
print(message)
}
} else if strings.has_suffix(v, suffix) {
# Add the resource and a warning message to the violators list
message = to_string(address) + " has " + to_string(attr) + " with value " +
to_string(v) + " that has the suffix " + to_string(suffix)
violators[address] = rc
messages[address] = message
if prtmsg {