We are using conftest to validate if our terraform changeset applies to certain rules & compliances. One thing we want to validate is wether our AWS resources are tagged according to the AWS tagging convention, which specifies certain tags to use (e.g. Owner, ApplicationRole, Project) and specifies that all tags and values are in CamelCase.
in terraform the changeset is portrayed in the following (simplified) json output:
{
"resource_changes":{
"provider_name":"aws",
"change":{
"before":{
},
"after":{
"tags":{
"ApplicationRole":"SomeValue",
"Owner":"SomeValue",
"Project":"SomeValue"
}
}
}
}
}
What I am now trying to do is to validate the following:
- Check wether tags are set.
- Validate if the keys and values are all camelcase.
- Check that the keys include the set (ApplicationRole, Owner, Project) in the minimum.
However, I am having trouble defining that in Rego (I am quite new to OPA).
Is there a way to "loop" over the keys and values of an object, and validate if they are formatted correctly?
in pseudo code:
for key, value in tags {
re_match(`([A-Z][a-z0-9]+)+`, key)
re_match(`([A-Z][a-z0-9]+)+`, value)
}
I have tried the following:
tags_camel_case(tags) {
some key
val := tags[key]
re_match(`^([A-Z][a-z0-9]+)+`, key) # why is key not evaluated?
re_match(`^([A-Z][a-z0-9]+)+`, val)
}
However, when evaluating against the following test json:
{
"AppRole": "SomeValue",
"appRole": "SomeValue"
}
the rule returns true, even though I am checking both key and value vs the regex