Testing Policies with opa test
A policy is code that can block every deploy in the company. A typo in a rule doesn't fail loudly: a misspelled field is simply undefined, the rule never fires, and the policy silently allows everything. Unit tests are the only way to know a rule does what it says.
Tests are rules too
A test is a rule whose name starts with test_, usually in a _test package next to the policy. with replaces input (or any part of data) for one expression:
bash · try it yourself
mkdir -p policy && cd policy
cat > db.rego <<'REGO'
package terraform.db
deny contains msg if {
some rc in input.resource_changes
rc.type == "aws_db_instance"
"create" in rc.change.actions
not rc.change.after.storage_encrypted
msg := sprintf("%s: storage must be encrypted", [rc.address])
}
deny contains msg if {
some rc in input.resource_changes
rc.type == "aws_db_instance"
rc.change.after.publicly_accessible
msg := sprintf("%s: must not be publicly accessible", [rc.address])
}
REGO
cat > db_test.rego <<'REGO'
package terraform.db_test
import data.terraform.db
db_change(after) := {"resource_changes": [{
"address": "aws_db_instance.main",
"type": "aws_db_instance",
"change": {"actions": ["create"], "after": after},
}]}
test_unencrypted_denied if {
db.deny == {"aws_db_instance.main: storage must be encrypted"} with input as db_change({"storage_encrypted": false})
}
test_missing_flag_denied if {
# the attribute absent entirely: must still be denied
count(db.deny) == 1 with input as db_change({})
}
test_public_denied if {
some msg in db.deny with input as db_change({"storage_encrypted": true, "publicly_accessible": true})
contains(msg, "publicly accessible")
}
test_compliant_allowed if {
count(db.deny) == 0 with input as db_change({"storage_encrypted": true, "publicly_accessible": false})
}
test_other_types_ignored if {
count(db.deny) == 0 with input as {"resource_changes": [{"address": "aws_s3_bucket.b", "type": "aws_s3_bucket",
"change": {"actions": ["create"], "after": {}}}]}
}
REGO
opa test . ; echo "exit $?"PASS: 5/5
exit 0
Each policy rule has at least one test that proves it fires and one that proves it doesn't fire on compliant input. test_missing_flag_denied covers the case that matters most in practice: an attribute missing altogether.
What a failing test looks like
Introduce the classic bug, a misspelled attribute, and the tests catch it:
bash · try it yourself
mkdir -p policy && cd policy
cat > db.rego <<'REGO'
package terraform.db
deny contains msg if {
some rc in input.resource_changes
rc.type == "aws_db_instance"
rc.change.after.storage_encryption == false # typo: the attribute is storage_encrypted
msg := sprintf("%s: storage must be encrypted", [rc.address])
}
REGO
cat > db_test.rego <<'REGO'
package terraform.db_test
import data.terraform.db
test_unencrypted_denied if {
count(db.deny) == 1 with input as {"resource_changes": [{"address": "aws_db_instance.main",
"type": "aws_db_instance", "change": {"actions": ["create"], "after": {"storage_encrypted": false}}}]}
}
REGO
opa test . 2>&1 | grep -vE '^-+