Build a custom decision server
The decision server of rego4k serves OPA's REST API on http4k, a Kotlin library for HTTP, so an OPA client works with it.
You build it from modules, and your application gets only the code and the dependencies of the modules that it uses. Use it in these cases:
- Other services call your policies over HTTP.
- You want signed decisions or the debugger behind your own authentication.
Each block of Kotlin on this page is a part of a complete program in site/examples, and the test
ServerGuideTest runs it.
Add the dependencies
Choose the modules with the dependencies of the application. rego4k-server-policies serves OPA's API, and
brings rego4k-server-core, the base of the server.
dependencies {
implementation(platform("dev.rego4k:rego4k-bom:$rego4kVersion"))
// OPA's REST API
implementation("dev.rego4k:rego4k-server-policies")
// signed decisions, if you need them
implementation("dev.rego4k:rego4k-server-signing")
// the debugger, if you need it
implementation("dev.rego4k:rego4k-server-debugger")
// the page of the OpenAPI descriptions
implementation("dev.rego4k:rego4k-server-docs")
implementation("dev.rego4k:rego4k-builtins-stdlib-provided")
// the regex engine of OPA
implementation("dev.rego4k:rego4k-builtins-re2")
}BuildFilesTest.kt, lines 53 to 66, on GitHubStart a server with OPA's API
HostedBundles holds the bundles of the server and the Rego that evaluates them. OpaApi.module serves
GET /health, GET /bundles, the Data API, the Query API and the read part of the Policy API.
val rego = Rego(
Builtins.Pure.stdlib(Re2Engine) + Builtins.Provided.stdlib,
cache = RegoCache(maxBytes = 64L * 1024 * 1024),
)
val bundles = HostedBundles(rego, { Bundle.read(policies) })
val server = DecisionServer(authentication, listOf(OpaApi.module(bundles)))
val running = server.asServer(SunHttp(port)).start()Start.kt, lines 22 to 28, on GitHubWith the port 8181, an OPA client then calls the server exactly as it calls OPA:
curl -X POST localhost:8181/v1/data/authz/allow -d '{"input": {"method": "GET", "user": {"roles": ["viewer"]}}}'HostedBundles can host more than one bundle. Give the others by name: each has the same API below
/bundles/{name}.
bundles.reload(name)reads a bundle again.bundles.reloadEvery(period, scheduler, failed)reads each bundle on an interval.- A bundle that fails to load keeps its policy.
Authenticate each request
The server does not authenticate a request itself. Your filter wraps the routes of each module. For signed decisions, the filter also names the principal that sent the request:
val authentication = Filter { next ->
{ request ->
// your own check
val token = request.header("Authorization")
?.removePrefix("Bearer ")
?.let(tokens::get)
if (token == null) Response(UNAUTHORIZED)
else next(
request.with(
SignedDecisions.principal of Principal(token.issuer, token.subject)
)
)
}
}Authentication.kt, lines 16 to 29, on GitHubAdd the modules that you need
Each module is a ServerModule. All the modules share one HostedBundles, so they see the same bundles and the
same Rego. GET /openapi.json describes the routes of all the modules.
val signingKey = SigningKey(
"decisions-2026",
Key(leafKey.readText(), SignatureAlgorithm.ES256),
chain
)
val server = DecisionServer(
authentication,
listOf(
// first: it reads the modules after it
ApiDocs.module(listOf("/openapi.json", "/debugger/openapi.json")),
OpaApi.module(bundles),
SignedDecisions.module(bundles, signingKey),
DebuggerModule.module(bundles, "/debugger"),
),
)Modules.kt, lines 22 to 36, on GitHubSignedDecisions.moduleservesPOST /v1/signed/data/{path}. The response is a DSSE envelope, a standard format for a signed message. The envelope holds an in-toto statement, the attestation format of the in-toto supply chain framework. See Signed decisions and the format of a signed decision.DebuggerModule.moduleserves the debugger below/debugger, with the bundles of the server. See The debugger and the demo.ApiDocs.moduleservesGET /openapi, an HTML page of the OpenAPI descriptions with no JavaScript. Put it first in the list, because it reads the descriptions of the modules after it.
Write a module of your own
A module has a name and contract routes. This module adds a route that reads the default bundle again:
val reload = ServerModule(
"reload",
listOf(
"/admin/reload" meta {
summary = "Reads the default bundle again"
} bindContract POST to { _: Request ->
bundles.reload()
Response(NO_CONTENT)
},
),
)
val server = DecisionServer(Filter.NoOp, listOf(OpaApi.module(bundles), reload))Reload.kt, lines 19 to 30, on GitHubUse the lenses, the bodies and ApiError of rego4k.server.api. A route that throws an ApiError or a
RegoException gets OPA's error body.
Serve the server below a path of your application
The routes of the server start at /. Mount serves them below a path of a host application, behind its filters:
val application = hostFilter.then(
routes(
"/home" bind GET to home,
Mount("/tools/policies/opa", server).route
)
)Mounts.kt, lines 15 to 20, on GitHubMount removes the prefix from each request. The modules use no absolute URLs and no global state.
Run the server without code
rego4k run --server runs the server with OPA's builtins and the flags of opa run --server. Flags of rego4k
add the other modules.
From the repository, with just, a command runner:
just server ./bundle 8181
just cli
rego4k run --server -b bundle.tar.gz --with-docs --with-debugger --with-shaking \
--signing-key leaf.key --signing-algorithm ES256 --signing-chain chain.pemSee the README of modules/cli/server for all the flags.
See the README of modules/server for the options and the external dependencies of each
module. The README of rego4k-server-policies lists the parts of OPA's REST API
that the server copies.