Builtins

An engine has only the builtins that you give it. Operators such as ==, + and in are builtins too.

A set of builtins is a Builtins value, and + adds two sets. If two sets have a builtin with the same name, the builtin of the set on the right replaces the other.

The sets of OPA's library

Set Module What it holds
Builtins.stdlib(regex) rego4k-builtins-stdlib All the sets below, except Builtins.httpSend(handler).
Builtins.Pure.stdlib(regex) rego4k-builtins-stdlib-pure The pure builtins of OPA's standard library, without yaml.*, with the regex engine regex.
Builtins.Provided.stdlib rego4k-builtins-stdlib-provided time.now_ns, the time.* builtins with the time zone data, the expiry checks of JWTs and certificates, and opa.runtime.
Builtins.Impure.stdlib rego4k-builtins-stdlib-impure http.send, net.lookup_ip_addr, rand.intn, uuid.rfc4122, and the ES* and PS* signatures of a JWT.
Builtins.yaml rego4k-builtins-yaml The yaml.* builtins. The parser uses yaml.unmarshal for # METADATA blocks.
Builtins.httpSend(handler) rego4k-builtins-http4k-send http.send on an http4k HttpHandler.

Each module is a dependency that you choose, so an application gets only the code and the dependencies of the builtins that it uses.

The pure builtins need a regex engine, because some of them compile a regex:

Pure, provided and impure

Each builtin has one of three kinds:

A query that calls only pure and provided builtins can be reproduced from its input, its data and the provided values. Rego(..., pure = true) rejects each impure builtin. See A builtin is pure, provided or impure.

A builtin of your own

A builtin of your own uses the same API as the builtins of OPA's library. Give it:

The function gets the arguments as values, and gives a value, or null for an undefined result. A BuiltinException gives an error, as in OPA.

OwnBuiltins.kt
val hello = Builtin.Pure(
    "greetings.hello",
    Type.function(
        "name" to ScalarType.STRING,
        result = "greeting" to ScalarType.STRING,
    ),
) { args -> Str("hello ${args.str(0)}") }
val rego = Rego(Builtins.Pure.stdlib(Re2Engine) + hello)
val policy = rego.compile(
    RegoFileName("greetings.rego") to
        RegoFile("package greetings\n\nmessage := greetings.hello(input.name)\n"),
)
// "hello alice" for {"name": "alice"}
val message = policy.eval("data.greetings.message", input)
OwnBuiltins.kt, lines 26 to 39, on GitHub

The type checker of the compiler examines each call with the type, so a wrong argument is a compile error, as in OPA.

A provided builtin gets a ProvidedBuiltinContext. It reads the time of the query from the context, and the context records the read:

OwnBuiltins.kt
val open = Builtin.Provided(
    "office.open",
    Type.function(result = "open" to ScalarType.BOOLEAN),
) {
    // the time of the query: the engine records it
    Bool(time.atZone(ZoneOffset.UTC).hour in 9..17)
}
val policy = Rego(Builtins.Pure.stdlib(Re2Engine) + open).compile(
    RegoFileName("office.rego") to
        RegoFile("package office\n\nallow if office.open()\n"),
)
val result = policy.prepare("data.office.allow").result(timestamp = at)
OwnBuiltins.kt, lines 47 to 58, on GitHub

Only the builtins that a bundle needs

Tree shaking of a bundle gives the builtins that its entrypoints can call. only keeps these builtins of a set. Then the engine cannot call any other builtin, for example http.send:

OwnBuiltins.kt
val bundle = Bundle.read(policies)
val shaken = bundle.shake(Rego(Builtins.Pure.stdlib(Re2Engine)), "authz/allow")
// only "equal" (==) and "internal.member_2" (in)
val rego = Rego(Builtins.Pure.stdlib(Re2Engine).only(shaken.builtins))
OwnBuiltins.kt, lines 66 to 69, on GitHub

http.send on an http4k handler

Builtins.httpSend(handler) sends each request of a policy to an http4k HttpHandler, with the results of OPA.

The README of each builtins module has the details. For example: