-
Notifications
You must be signed in to change notification settings - Fork 68
/
whitelist_ami.rego
65 lines (56 loc) · 1.88 KB
/
whitelist_ami.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
# This policy introduces AMI ids whitelist for AWS instances.
# There are two rules: the first one disallows the usage of
# all AMIs that are not from allowed list,
# while the second rule whitelists only directly (or via variable) specified AMIs
# thus allowing them to be pulled from aws_ami data source.
# You will probably want to keep only one rule that is relevant for you,
# removing/commenting out another.
package terraform
import input.tfplan as tfplan
# A whitelist of AMI ids
allowed_amis = [
"ami-07d0cf3af28718ef8",
"ami-0a9b2a20d7dc001e0"
]
array_contains(arr, elem) {
arr[_] = elem
}
eval_expression(plan, expr) = constant_value {
constant_value := expr.constant_value
} else = reference {
ref = expr.references[0]
startswith(ref, "var.")
var_name := replace(ref, "var.", "")
reference := plan.variables[var_name].value
}
get_address(value) = address {
address := value.address
} else = source {
source := value.source
}
# Force all found AMIs to belong to allowed list
deny[reason] {
resource := tfplan.resource_changes[_]
action := resource.change.actions[count(resource.change.actions) - 1]
array_contains(["create", "update"], action)
ami := resource.change.after.ami
not array_contains(allowed_amis, ami)
reason := sprintf(
"%s: AMI %q is not allowed. Expected values are: %v",
[resource.address, ami, allowed_amis]
)
}
# Force directly specified AMIs to belong to allowed list,
# but allow AMIs from data source
deny[reason] {
walk(tfplan.configuration.root_module, [path, value])
ami := eval_expression(tfplan, value.expressions.ami)
not array_contains(allowed_amis, ami)
reason := sprintf(
`%s: AMI %q is not allowed.
AMI id should be pulled from aws_ami data source
or otherwise be one of the allowed ones when specified directly:
%v`,
[get_address(value), ami, allowed_amis]
)
}