Interview Prep/DevSecOps/Tool Internals and Comparisons

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.

All DevSecOps questions

More on Tool Internals and Comparisons