Skip to content

Commit ab7855b

Browse files
committed
use functions instead of rules
1 parent 05820a4 commit ab7855b

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

governance/second-generation/cloud-agnostic/require-all-resources-from-pmr.sentinel

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,51 @@
66
import "tfconfig"
77
import "strings"
88

9-
##### Global Variables #####
10-
# Define the address of the TFE server
11-
address = "app.terraform.io"
9+
#####Functions#####
1210

13-
# Define organization variable
14-
organization = "Cloud-Operations"
11+
#Prevent resources in root module
12+
prevent_resources_in_root_module = func() {
1513

16-
##### Rules #####
17-
# Don't allow resources in root module
18-
no_resources_in_root_module = rule {
19-
length(tfconfig.resources) is 0 or not
20-
(print("Resources not allowed in root module") and
21-
print("Your root module has", length(tfconfig.resources), "resources."))
14+
validated = true
15+
16+
if length(tfconfig.resources) != 0 {
17+
print("Resources are not allowed in the root module.")
18+
print("Your root module has", length(tfconfig.resources), "type(s) of resources.")
19+
validated = false
20+
}
2221

22+
return validated
2323
}
2424

2525
# Require all modules directly under root module to come from PMR
26-
require_modules_from_pmr = rule {
27-
all tfconfig.modules as name, m {
28-
strings.has_prefix(m.source, address + "/" + organization) or not
29-
(print("All modules must from the private module registry",
30-
address + "/" + organization) and
31-
print("You included module", name, "with source", m.source))
26+
require_modules_from_pmr = func(address, organization) {
27+
28+
validated = true
29+
30+
for tfconfig.modules as name, m {
31+
if not strings.has_prefix(m.source, address + "/" + organization) {
32+
print("All non-root modules must come from the private module registry",
33+
address + "/" + organization)
34+
print("You included module,", name, ", with source,", m.source)
35+
validated = false
36+
}
3237
}
38+
39+
return validated
3340
}
3441

42+
##### Global Variables #####
43+
# Define the address of the TFE server
44+
address = "app.terraform.io"
45+
46+
# Define organization variable
47+
organization = "Cloud-Operations"
48+
49+
##### Rules #####
50+
3551
# Main rule that requires other rules to be true
52+
no_resources_in_root_module = prevent_resources_in_root_module()
53+
all_non_root_modules_from_pmr = require_modules_from_pmr(address, organization)
3654
main = rule {
37-
print("modules", tfconfig.modules) and
38-
no_resources_in_root_module and
39-
require_modules_from_pmr
55+
no_resources_in_root_module and all_non_root_modules_from_pmr
4056
}

governance/second-generation/cloud-agnostic/test/require-all-resources-from-pmr/mock-tfconfig-fail-0.12.sentinel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ _modules = {
7171
},
7272
},
7373
},
74+
"aws_security_group_rule": {
75+
"allow_all": {
76+
"config": {
77+
"cidr_blocks": [
78+
"0.0.0.0/0",
79+
],
80+
"from_port": 0,
81+
"protocol": "tcp",
82+
"security_group_id": "sg-0ecaf664fe45ff737",
83+
"to_port": 65535,
84+
"type": "ingress",
85+
},
86+
"provisioners": null,
87+
},
88+
},
7489
},
7590
"variables": {},
7691
},

0 commit comments

Comments
 (0)