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:
Re2Engineofrego4k-builtins-re2gives the regex behaviour of OPA.JavaRegexEngineofrego4k-builtins-stdlib-pureusesjava.util.regex. It needs no dependency, but it differs from OPA in syntax, in semantics and in running time (see Regex withjava.util.regex).
Pure, provided and impure
Each builtin has one of three kinds:
Builtin.Puredepends only on its arguments, for examplecountandsprintf.Builtin.Providedalso depends on a value that the runtime gives: the time of the query, the time zone data, the local zone or the runtime. The engine records each value that the builtin reads, so it can give the value again.Builtin.Impuredepends on something that the runtime cannot give again, for example the network or randomness.
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:
- a name;
- a type (or a number of arguments of any type);
- a function.
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.
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 GitHubThe 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:
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 GitHubOnly 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:
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 GitHubhttp.send on an http4k handler
Builtins.httpSend(handler) sends each request of a policy to an http4k HttpHandler, with
the results of OPA.
- In production, give an http4k client with your filters for authentication, time-outs and metrics.
- In a test, give a handler in memory. See Tests of Rego.
The README of each builtins module has the details. For example:
- the differences of
java.util.regexfrom RE2 inrego4k-builtins-stdlib-pure; - the rules of
http.sendinrego4k-builtins-http4k-send.