forked from hashicorp/terraform-sentinel-policies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfconfig-functions.sentinel
643 lines (584 loc) · 21 KB
/
tfconfig-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
# Common functions that use the tfconfig/v2 import
##### Imports #####
import "tfconfig/v2" as tfconfig
import "strings"
import "types"
##### Functions #####
### find_all_resources ###
# Find all resources of all types using the tfconfig/v2 import.
find_all_resources = func() {
resources = filter tfconfig.resources as address, r {
r.mode is "managed"
}
return resources
}
### find_resources_by_type ###
# Find all resources of a specific type using the tfconfig/v2 import.
# The parameter, type, should be a string like "aws_instance".
find_resources_by_type = func(type) {
resources = filter tfconfig.resources as address, r {
r.type is type and
r.mode is "managed"
}
return resources
}
### find_resources_in_module ###
# Find all resources from a specific module using the tfconfig/v2 import.
find_resources_in_module = func(module_address) {
resources = filter tfconfig.resources as address, r {
r.module_address is module_address and
r.mode is "managed"
}
return resources
}
### find_resources_by_provider ###
# Find all resources from a specific provider using the tfconfig/v2 import.
# The parameter, provider, should be given as a string such as "aws".
find_resources_by_provider = func(provider) {
resources = filter tfconfig.resources as address, r {
r.provider_config_key matches "(.*:)?" + provider + "(\\..*)?" and
r.mode is "managed"
}
return resources
}
### find_all_datasources ###
# Find all data sources of all types using the tfconfig/v2 import.
find_all_datasources = func() {
datasources = filter tfconfig.resources as address, d {
d.mode is "data"
}
return datasources
}
### find_datasources_by_type ###
# Find all data sources of a specific type using the tfconfig/v2 import.
# The parameter, type, should be a string like "aws_ami".
find_datasources_by_type = func(type) {
datasources = filter tfconfig.resources as address, d {
d.type is type and
d.mode is "data"
}
return datasources
}
### find_datasources_in_module ###
# Find all data sources from a specific module using the tfconfig/v2 import.
find_datasources_in_module = func(module_address) {
datasources = filter tfconfig.resources as address, d {
d.module_address is module_address and
d.mode is "data"
}
return datasources
}
### find_datasources_by_provider ###
# Find all data sources from a specific provider using the tfconfig/v2 import.
# The parameter, provider, should be given as a string such as "aws".
find_datasources_by_provider = func(provider) {
datasources = filter tfconfig.resources as address, d {
d.provider_config_key matches "(.*:)?" + provider + "(\\..*)?" and
d.mode is "data"
}
return datasources
}
### find_all_provisioners ###
# Find all provisioners using the tfconfig/v2 import.
find_all_provisioners = func() {
return tfconfig.provisioners
}
### find_provisioners_by_type ###
# Find all provisioners of a specific type using the tfconfig/v2 import.
# The parameter, type, should be a string like "local_exec".
find_provisioners_by_type = func(type) {
provisioners = filter tfconfig.provisioners as address, p {
p.type is type
}
return provisioners
}
### find_all_providers ###
# Find all providers using the tfconfig/v2 import.
find_all_providers = func() {
return tfconfig.providers
}
### find_providers_by_type ###
# Find all providers of a specific type using the tfconfig/v2 import.
# The parameter, provider, should be given as a string such as "aws".
find_providers_by_type = func(type) {
providers = filter tfconfig.providers as address, p {
p.provider_config_key matches "(.*:)?" + type + "(\\..*)?"
}
return providers
}
### find_providers_in_module ###
# Find all providers from a specific module using the tfconfig/v2 import.
find_providers_in_module = func(module_address) {
providers = filter tfconfig.providers as address, p {
p.module_address is module_address
}
return providers
}
### find_all_variables ###
# Find all variables using the tfconfig/v2 import.
find_all_variables = func() {
return tfconfig.variables
}
### find_variables_in_module ###
# Find all variables from a specific module using the tfconfig/v2 import.
find_variables_in_module = func(module_address) {
variables = filter tfconfig.variables as address, v {
v.module_address is module_address
}
return variables
}
### find_all_outputs ###
# Find all outputs using the tfconfig/v2 import.
find_all_outputs = func() {
return tfconfig.outputs
}
### find_outputs_in_module ###
# Find all providers from a specific module using the tfconfig/v2 import.
find_outputs_in_module = func(module_address) {
outputs = filter tfconfig.outputs as address, o {
o.module_address is module_address
}
return outputs
}
### find_outputs_by_sensitivity ###
# Find all providers of specific sensitivity using the tfconfig/v2 import.
# The parameter, sensitive, should be true or false (without quotes)
find_outputs_by_sensitivity = func(sensitive) {
outputs = filter tfconfig.outputs as address, o {
o.sensitive is sensitive
}
return outputs
}
### find_all_module_calls ###
# Find all module calls using the tfconfig/v2 import.
find_all_module_calls = func() {
return tfconfig.module_calls
}
### find_module_calls_in_module ###
# Find all direct module calls made from a specific module
# using the tfconfig/v2 import.
# The parameter, module_address, should be "" for the root module,
# "module.A" for a module A called by the root module,
# "module.A.module.B" for module B called by module A called by the root module,
# and so on.
find_module_calls_in_module = func(module_address) {
module_calls = filter tfconfig.module_calls as address, mc {
mc.module_address is module_address
}
return module_calls
}
### find_descendant_modules ###
# Find addresses of all modules called directly or indirectly by a module.
# The provided module address is included.
# To find all module addresses, call find_descendant_modules("")
# After calling this function against "", you can call find_module_calls_in_module
# against any item in the list that is returned.
find_descendant_modules = func(module_address) {
module_addresses = [module_address]
mcs = find_module_calls_in_module(module_address)
if length(mcs) > 0 {
for mcs as ma, mc {
if mc.module_address is "" {
new_module_address = "module." + mc.name
} else {
new_module_address = mc.module_address + ".module." + mc.name
}
module_addresses += find_descendant_modules(new_module_address)
}
}
return module_addresses
}
### 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
}
### 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 an attribute
# In general, the attribute should be a top-level attribute of item, but
# we do special processing for attributes with form "config.x"
# `item` is the item with the attribute
# `attribute` is the attribute
evaluate_attribute = func(item, attribute) {
# Split the attribute into a list, using "." as the separator
attributes = strings.split(attribute, ".")
if length(attributes) > 2 {
print("An attribute passed to evaluate_attribute can only have 1 or 2 fields")
return null
}
if attributes[0] is "config" {
config = item.config[attributes[1]] else {}
if "constant_value" in config {
# Found constant_value in config
return config.constant_value
} else if "references" in config {
# Found references in config
return config.references
} else {
# Did not find constant_value or references in config
return null
}
} else {
# Return the original attribute or the item
return item[attribute]
}
}
### filter_attribute_not_in_list ###
# Filter a list of items such as providers to those with a specified
# attribute (attr) that is not in a given list of allowed values (allowed).
# The parameter, attr, can only be a top-level attribute of the collection, items.
# 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_not_in_list = func(items, attr, allowed, prtmsg) {
violators = {}
messages = {}
# Iterate over items
for items as index, item {
val = evaluate_attribute(item, attr) else null
# Check if the value is null
if val is null {
val = "null"
}
# Process lists and maps
if types.type_of(val) in ["list", "map"] {
message = ""
# Check each item of list or map
for val as i, v {
if v not in allowed {
# Add the item and a warning message to the violators list
message = to_string(index) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is not in the allowed list: " +
to_string(allowed)
}
if message is not "" {
# Add the item and warning message to the violators list
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end message not ""
} // end for
} else {
# Process single item
if val not in allowed {
# Add the item and a warning message to the violators list
message = to_string(index) + " has " + to_string(attr) +
" with value " + to_string(val) +
" that is not in the allowed list: " +
to_string(allowed)
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end if single item not matches
} // end single item
} // end for items
return {"items":violators,"messages":messages}
}
### filter_attribute_in_list ###
# Filter a list of items such as providers to those with a specified
# attribute (attr) that is in a given list of forbidden values (forbidden).
# The parameter, attr, can only be a top-level attribute of the collection, items.
# 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(items, attr, forbidden, prtmsg) {
violators = {}
messages = {}
# Iterate over items
for items as index, item {
val = evaluate_attribute(item, attr) else null
# Check if the value is null
if val is null {
val = "null"
}
# Process lists and maps
if types.type_of(val) in ["list", "map"] {
message = ""
# Check each item of list or map
for val as i, v {
if v in forbidden {
# Add the item and a warning message to the violators list
message = to_string(index) + " has " + to_string(attr) + " with value " +
to_string(v) + " that is in the forbidden list: " +
to_string(forbidden)
}
if message is not "" {
# Add the item and warning message to the violators list
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end message not ""
} // end for
} else {
# Process single item
if val in forbidden {
# Add the item and a warning message to the violators list
message = to_string(index) + " has " + to_string(attr) +
" with value " + to_string(val) +
" that is in the forbidden list: " +
to_string(forbidden)
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end if single item not matches
} // end single item
} // end for items
return {"items":violators,"messages":messages}
}
### filter_attribute_does_not_match_regex ###
# Filter a list of items such as resources to those with a specified
# attribute (attr) that does not match a regular expression (expr).
# The parameter, attr, can only be a top-level attribute of items or
# an attribute in the form "config.x".
# Set prtmsg to `true` (without quotes) if you want to print violation messages.
filter_attribute_does_not_match_regex = func(items, attr, expr, prtmsg) {
violators = {}
messages = {}
for items as index, item {
val = evaluate_attribute(item, attr) else null
if val is null {
# Add the item and a warning message to the violators list
message = to_string(index) + " has " + to_string(attr) +
" that is null or undefined. " + "It is supposed to " +
"match the regex " + to_string(expr)
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} else {
# Process lists and maps
if types.type_of(val) in ["list", "map"] {
message = ""
# Check each item of list or map
for val as i, v {
if v not matches expr {
# Add to the warning message
message += to_string(index) + " has " + to_string(attr) +
" with value " + to_string(v) +
" that does not match the regex " + to_string(expr) + "\n"
}
if message is not "" {
# Add the item and warning message to the violators list
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end message not ""
} // end for
} else {
# Process single item
if val not matches expr {
# Add the item and a warning message to the violators list
message = to_string(index) + " has " + to_string(attr) +
" with value " + to_string(val) +
" that does not match the regex " + to_string(expr)
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end if single item not matches
} // end single item
} // end not null
} // end for items
return {"items":violators,"messages":messages}
}
### filter_attribute_matches_regex ###
# Filter a list of items such as resources to those with a specified
# attribute (attr) that matches a regular expression (expr).
# The parameter, attr, can only be a top-level attribute of items or
# an attribute in the form "config.x".
# 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(items, attr, expr, prtmsg) {
violators = {}
messages = {}
for items as index, item {
val = evaluate_attribute(item, attr) else null
if val is null {
val = "null"
}
# Process lists and maps
if types.type_of(val) in ["list", "map"] {
message = ""
# Check each item of list or map
for val as i, v {
if v matches expr {
# Add to the warning message
message += to_string(index) + " has " + to_string(attr) +
" with value " + to_string(v) +
" that matches the regex " + to_string(expr) + "\n"
}
if message is not "" {
# Add the item and warning message to the violators list
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end message not ""
} // end for
} else {
# Process single item
if val matches expr {
# Add the item and a warning message to the violators list
message = to_string(index) + " has " + to_string(attr) +
" with value " + to_string(val) +
" that matches the regex " + to_string(expr)
violators[index] = item
messages[index] = message
if prtmsg {
print(message)
}
} // end if single item not matches
} // end single item
} // end for items
return {"items":violators,"messages":messages}
}
### get_module_source ###
# Get the module source from a module address
# Note that the module_address in many collections in the tfplan/v2, tfconfig/v2,
# and tfstate/v2 imports gives the labels used in the module blocks.
# For instance, a module_address like "module.A.module.B" means that the current
# item is in a module with label "B" that is a module with label "A". But that
# does not give you the source the module labeled "B".
# But if you want to limit creation of resources to specific modules based on
# their source, you need the module source. This function computes it.
get_module_source = func(module_address) {
# Check for root module
if module_address is "" {
return "root"
} else {
# Find parent module
module_segments = strings.split(module_address, ".")
num_segments = length(module_segments)
parent_module = strings.join(module_segments[0:num_segments-2], ".")
current_module_name = module_segments[num_segments-1]
# Find module call that called current module
if parent_module is "" {
# parent module is root module
mc = tfconfig.module_calls[current_module_name]
} else {
# parent module is not root module
mc = tfconfig.module_calls[parent_module + ":" + current_module_name]
}
# Set source from the module call
module_source = mc.source
return module_source
}
}
### get_ancestor_module_source ###
# Get the module source of the first ancestor module from a module address that
# is not a local module (one having source starting with "./" or "../").
# Note that the module_address in many collections in the tfplan/v2, tfconfig/v2,
# and tfstate/v2 imports gives the labels used in the module blocks.
# For instance, a module_address like "module.A.module.B" means that the current
# item is in a module with label "B" that is a module with label "A". But that
# does not give you the source the module labeled "B".
# But if you want to limit creation of resources to specific modules based on
# their source, you need the module source. This function computes it for
# the first ancestor module that is not a local module.
get_ancestor_module_source = func(module_address) {
# Check for root module
if module_address is "" {
return "root"
} else {
# Find parent module
module_segments = strings.split(module_address, ".")
num_segments = length(module_segments)
parent_module = strings.join(module_segments[0:num_segments-2], ".")
current_module_name = module_segments[num_segments-1]
# Find module call that called current module
if parent_module is "" {
# parent module is root module
mc = tfconfig.module_calls[current_module_name]
} else {
# parent module is not root module
mc = tfconfig.module_calls[parent_module + ":" + current_module_name]
}
# Set source from the module call
module_source = mc.source
# Check to see if current module source is a nested module
if strings.has_prefix(module_source, "./") or strings.has_prefix(module_source, "../") {
return get_ancestor_module_source(parent_module)
} else {
return module_source
}
}
}
### get_parent_module_address ###
# Get the address of the parent module of a module from the module address
# return "root" if the given module_address is "" (the root module).
get_parent_module_address = func(module_address) {
# Check for root module
if module_address is "" {
return "root"
} else {
# Find parent module
module_segments = strings.split(module_address, ".")
num_segments = length(module_segments)
parent_module = strings.join(module_segments[0:num_segments-2], ".")
current_module_name = module_segments[num_segments-1]
return parent_module
}
}