oparego

Can this Rego policy be simplified


I have this Rego policy that I want to simplify (fewer lines of code) more if possible especially the two findings blocks. I tried but failed miserably given that I'm a Rego noob.

package test

compliant(resource) {
  resource.good
}

findings[x] {
    resource := input.resources[_]
    compliant(resource)
    x := {
        "compliant": true,
        "resource": resource,
    }
}

findings[x] {
    resource := input.resources[_]
    not compliant(resource)
    x := {
        "compliant": false,
        "resource": resource,
    }
}

Link to the OPA playground

https://play.openpolicyagent.org/p/0xYWI7Q0fZ


Solution

  • You seem to want to output both the true and false evaluations.

    You can introduce a helper function that will allow you to display both

    package test
    
    f(x) := true { compliant(x) } 
    f(x) := false { not compliant(x) } 
    
    compliant(resource) {
      resource.good
    }
    
    findings[x] {
        resource := input.resources[_]
        x := {
            "compliant": f(resource),
            "resource": resource,
        }
    }