Rego/OPA policy engine in pure OCaml
README.md

ocaml-rego #

A Rego policy engine in OCaml.

Open Policy Agent (OPA) separates authorisation decisions from the services that enforce them: a service sends a JSON description of a request to OPA, which evaluates a policy written in Rego and answers allow or deny. OPA is written in Go and runs as a sidecar (a helper process deployed next to each instance of a service, on the same host or in the same Kubernetes pod, which the service calls over localhost) or as a library linked into Go code. rego evaluates the same policies inside an OCaml application, against JSON input and data documents, with no external process. The companion rego-server package serves an engine over OPA's REST Data API (/v1/data/{path}), so existing OPA clients can query it unchanged: Envoy, a proxy often deployed as a sidecar, or a Kubernetes admission controller, a hook the cluster calls to accept or reject each object before storing it.

The engine covers complete, default, partial set, partial object and function rules; else; the _ wildcard, unbound index variables, some ... in, every ... in, the in membership operator and comprehensions; with modifiers on input and data; and numbers as arbitrary-precision rationals. Its builtins include the aggregate, type, string, array, object, bit and number families, regex.*, glob.match, json.* (including json.patch and json.filter), time.*, the crypto.* hashes and HMAC, base64, hex and URL encoding, semver.*, units.*, net.cidr_contains and rego.metadata.*.

Installation #

Install with opam:

$ opam install rego

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install rego

Usage #

let engine = Rego.engine ()

let () =
  match
    Rego.add_policy engine ~name:"example.rego"
      {|
        package authz
        default allow = false
        allow { input.user == "admin" }
      |}
  with
  | Ok () -> ()
  | Error e -> failwith (Format.asprintf "%a" Rego.pp_error e)

let input =
  Result.get_ok (Rego.Value.of_json_string {|{"user":"admin"}|})

let () =
  match Rego.eval engine ~input "data.authz.allow" with
  | Ok v -> assert (Rego.Value.equal v (Rego.Value.Bool true))
  | Error e -> failwith (Format.asprintf "%a" Rego.pp_error e)

Rego.eval_query evaluates a query in body form and returns the binding sets that satisfy it, and Rego.set_strict makes builtin errors propagate instead of leaving a rule undefined, as OPA's strict-builtin-errors does.

OPA conformance #

test/interop/opa/traces holds 67 categories of OPA's own test cases (from v1/test/cases/testdata/v0 in the OPA module that test/interop/opa/scripts/go.mod pins), converted from YAML to JSON with OPA's own types. dune runtest replays them, one Alcotest case per category, and a category fails if any of its cases does. The upstream suite has more categories than those committed here, so a feature outside them is untested against OPA. REGEN=1 dune build @traces regenerates the traces; it needs Go.

Licence #

ISC. See LICENSE.md.