Why is the exact difference between "violation" and "deny" in OPA/Rego?

2.1k Views Asked by At

In Open Policy Agent (https://www.openpolicyagent.org/)

regarding to Kubernetes, depending which engine is used:

OR

There are different ways to define validation rules:

It seems to be the OPA constraint framework defines it as violation: https://github.com/open-policy-agent/frameworks/tree/master/constraint#rule-schema

So what is the exact "story" behind this, why it is not consistent between the different engines?

Notes:

1

There are 1 best solutions below

2
On BEST ANSWER

Plain OPA has no opinion on how you choose to name your rules. Using deny is just a convention in the tutorial. The real Kubernetes admission review response is going to look something like this:

{
  "kind": "AdmissionReview",
  "apiVersion": "admission.k8s.io/v1beta1",
  "response": {
    "allowed": false,
    "status": {
      "reason": "container image refers to illegal registry (must be hooli.com)"
    }
  }
}

So whatever you choose to name your rules the response will need to be transformed into a response like the above before it's sent back to the Kubernetes API server. If you scroll down a bit in the Detailed Admission Control Flow section of the Kubernetes primer docs, you'll see how this transformation is accomplished in the system.main rule:

package system

import data.kubernetes.admission

main = {
  "apiVersion": "admission.k8s.io/v1beta1",
  "kind": "AdmissionReview",
  "response": response,
}

default response = {"allowed": true}

response = {
    "allowed": false,
    "status": {
        "reason": reason,
    },
} {
    reason = concat(", ", admission.deny)
    reason != ""
}

Note in particular how the "reason" attribute is just built by concatenating all the strings found in admission.deny:

reason = concat(", ", admission.deny)

If you'd rather use violation or some other rule name using plain OPA, this is where you would change it.