forked from QualiTorque/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.rego
133 lines (89 loc) · 2.84 KB
/
policy.rego
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
package policy
import input as tfplan
import future.keywords.every
# --- Validate allowed regions ---
default region_is_allowed = false
region_is_allowed {
region:= tfplan.configuration.provider_config.aws.expressions.region.constant_value
is_allowed_region(region)
}
region_is_allowed {
region_vars_name:= trim_prefix(input.configuration.provider_config.aws.expressions.region.references[0], "var.")
region:= tfplan.variables[region_var_name].value
is_allowed_region(region)
}
is_allowed_region(region) {
{"eu-west-1", "us-west-1"}[region]
}
# --- Validate S3 bucket is not public ---
default s3_acl_is_allowed = false
s3_acl_is_allowed {
tfplan.resource_changes[_].type!="aws_s3_bucket"
}
s3_acl_is_allowed {
resources := tfplan.resource_changes[_]
resources.type == "aws_s3_bucket_acl"
resources.change.after.acl == "private"
}
# --- Validate allowed resources ---
default resources_are_allowed = false
resources_are_allowed {
resource := tfplan.resource_changes[_]
is_allowed_resource_type(resource.type)
actions := resource.change.actions
is_resource_action_allowed(actions)
}
is_allowed_resource_type(resource) {
resource_types:={"aws_security_group", "aws_instance", "aws_s3_bucket", "aws_db_instance"}
resource_types[resource]
}
is_resource_action_allowed(actions) {
allowed_actions = ["create", "update"] # 'destroy' is irrelevant
every action in actions {
contains(allowed_actions, action)
}
}
contains(arr, elem) {
arr[_] = elem
}
# --- Validate instance types ---
default allowed_instance_type = false
allowed_instance_type {
resource := tfplan.resource_changes[_]
# registry.terraform.io/hashicorp/aws -> aws
provider_name := get_basename(resource.provider_name)
instance_type_keys:= {
"aws": ["instance_class", "instance_type"],
"azurerm": ["vm_size"]
}
is_allowed_instance_type(resource, provider_name, instance_type_keys)
}
get_basename(path) = basename{
arr := split(path, "/")
basename:= arr[count(arr)-1]
}
is_allowed_instance_type(resource, provider_name, instance_type_keys) {
every it in instance_type_keys[provider_name] {
not resource.change.after[it]
}
}
is_allowed_instance_type(resource, provider_name, instance_type_keys) {
allowed_types := {
"aws": ["t2.nano", "t2.micro", "db.t2.small"],
"azurerm": ["Standard_A0", "Standard_A1"]
}
some provider_its in instance_type_keys[provider_name]
instance_types:=[resource.change.after[provider_its]]
every it in instance_types {
contains(allowed_types[provider_name], it)
}
}
# --- Validate providers ---
default provider_is_allowed = false
provider_is_allowed {
provider_name:=get_basename(tfplan.resource_changes[_].provider_name)
is_allowed_provider(provider_name)
}
is_allowed_provider(provider_name) {
{"aws"}[provider_name]
}