How does OPA evaluate a Rego policy? Give an example.
IntermediateConceptTool Internals and Comparisons
Answer
OPA is a general-purpose policy engine: you send JSON input, it evaluates Rego rules against input plus optional data, and returns a JSON decision. Rules are declarative queries; a rule is true when all its expressions are true for some variable binding.
package k8s.images
deny contains msg if {
some c in input.spec.containers
endswith(c.image, ":latest")
msg := sprintf("container %s uses :latest", [c.name])
}
opa eval -d policy.rego -i pod.json "data.k8s.images.deny"
opa test ./policy -v
OPA 1.0 made the if and contains keywords mandatory, so old Rego examples without them need updating.
More on Tool Internals and Comparisons
How does Trivy get its vulnerability data, and how do you run it air-gapped?AdvancedWhich scan targets and scanners does Trivy support?IntermediateHow does Falco capture system calls?AdvancedWhat are the ways to deploy OPA, and what are decision logs?AdvancedHow does Vault achieve high availability with integrated storage?AdvancedWhat are Vault secrets engines and auth methods? What changes in KV v2?Intermediate