Skip to content

Commit 493cfd5

Browse files
authored
tests: extend hcl cases: tag verification (open-policy-agent#955)
tests: add an hcl case: tag verification: make sure all aws resources are tagged Signed-off-by: boranx <[email protected]>
1 parent 31700e1 commit 493cfd5

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

examples/hcl2/policy/deny.rego

+14
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ deny[msg] {
2626
disk.encryption_settings.enabled != true
2727
msg = sprintf("Azure disk `%v` is not encrypted", [name])
2828
}
29+
30+
# Required tags for all AWS resources
31+
required_tags := {"environment", "owner"}
32+
missing_tags(resource) := {tag | tag := required_tags[_]; not resource.tags[tag]}
33+
34+
deny[msg] {
35+
some aws_resource, name
36+
resource := input.resource[aws_resource][name] # all resources
37+
startswith(aws_resource, "aws_") # only AWS resources
38+
missing := missing_tags(resource)
39+
count(missing) > 0
40+
41+
msg = sprintf("AWS resource: %q named %q is missing required tags: %v", [aws_resource, name, missing])
42+
}

examples/hcl2/policy/deny_test.rego

+14
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@ test_fails_with_http_alb {
3131
`)
3232
deny["ALB `name` is using HTTP rather than HTTPS"] with input as cfg
3333
}
34+
35+
test_fails_with_aws_resource_is_missing_required_tags {
36+
cfg := parse_config("hcl2", `
37+
resource "aws_s3_bucket" "invalid" {
38+
bucket = "InvalidBucket"
39+
acl = "private"
40+
41+
tags = {
42+
environment = "prod"
43+
}
44+
}
45+
`)
46+
deny["AWS resource: \"aws_s3_bucket\" named \"invalid\" is missing required tags: {\"owner\"}"] with input as cfg
47+
}

examples/hcl2/terraform.tf

+10
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ resource "azurerm_managed_disk" "source" {
1717
enabled = false
1818
}
1919
}
20+
21+
resource "aws_s3_bucket" "valid" {
22+
bucket = "validBucket"
23+
acl = "private"
24+
25+
tags = {
26+
environment = "prod"
27+
owner = "devops"
28+
}
29+
}

0 commit comments

Comments
 (0)