The debugger and the demo
rego4k-server-debugger steps through a policy in the browser. It shows the part of the input that each step reads,
the bindings, and the output document as the rules make it.
Each step evaluates the query again from the start, so the server keeps no state, and a step back costs the same as a step forward.


Try it in the demo
The demo at demo.rego4k.dev runs the decision server with all its modules and 5 bundles that show real uses of Rego. Its pages do these things:
- list the policies;
- show the files and the example inputs of each policy;
- get a decision from the server;
- open the debugger with an example.
To run the demo on your computer, clone the repository and run just demo (JDK 21). just
is a command runner. The demo starts on http://localhost:8181:
just demo # http://localhost:8181
./gradlew :demo:run -Pport=9000 # another portThen do these steps:
- Click RBAC for an HTTP API, then the example
carol-deletes-a-user, then Ask the decision server. - Click Decide or press Ctrl+Enter. The page shows the decision, and below it the signed decision.
- Click Open in the debugger. Press Ctrl+Enter, then F11 to step.
See the README of the demo.
Step through a policy
./gradlew :server-debugger:runExample (or just demo-debugger) opens the debugger in your browser, with an example
policy:
- The rule
allowreads the roles of the user. - The rule
denyreads the price of each item.
Then do these steps:
- Press Ctrl+Enter (or click Debug) to start a replay of the query
data.app. - Press F11 a few times. The code view marks each step, and the call stack on the right gets a row for each step. At
some role in input.user.roles, the tree of the input opens atinput.user.roles[0]and highlights it, and the locals showrole. - Click the line number of
item.price > 100(line 12) to set a breakpoint, then press F8. The debugger stops for each item. Press F11 to the read of the price: the input highlightsinput.items[0].price, theninput.items[1].price. - Press F8 again. Open Output document in the variables. It shows
denyas it grows:"item b costs too much". - Click Step back.
The first start compiles the modules, so it can take a minute. After that, the page opens in about 3 s.
Put it in your application
The debugger is an http4k HttpHandler. http4k is a Kotlin library for HTTP. Give the
debugger a Rego with a RegoCache, because each step compiles the modules again, and the cache makes that free.
Each block of Kotlin below is a part of a complete program in
site/examples, and the test DebuggerGuideTest runs it.
val rego = Rego(
Builtins.Pure.stdlib(Re2Engine) + Builtins.Provided.stdlib,
cache = RegoCache(maxBytes = 64L * 1024 * 1024, maxEntries = 256),
)
val server = Debugger(rego).asServer(SunHttp(port)).start()Alone.kt, lines 17 to 21, on GitHubMost applications want the debugger on one of their own pages. Give the debugger these parts:
- a
BundleCatalog: your bundles, with their modules, data files and example inputs; - a
PageFrame: your page around the debugger; - a path.
val catalog = object : BundleCatalog {
override fun bundles() =
listOf(CatalogBundle("authz", revision = "42", hash = "6c1f"))
override fun files(name: String) =
if (name != "authz") null else listOf(
SourceFile("authz/policy.rego", authzPolicy),
SourceFile(
"authz/examples/viewer-reads.input.json",
"""{"method": "GET", "user": {"roles": ["viewer"]}}""",
),
)
}
val frame = PageFrame { debugger ->
head {
title("Admin: policies")
link(rel = "stylesheet", href = "/assets/admin.css")
debugger.head(this)
}
body {
nav { a("/admin") { +"Admin" }; a("/admin/policies") { +"Policies" } }
ol("breadcrumbs") { li { a("/admin") { +"Admin" } }; li { +"Debug" } }
main { debugger.content(this) }
}
}
val debugger = Debugger(rego, catalog = catalog, frame = frame)
val application = routes(
"/admin" bind GET to adminHome,
adminLogin.then(Mount("/admin/policies/debug", debugger).route),
)AdminPages.kt, lines 32 to 62, on GitHubThe frame must call debugger.head in the head and debugger.content in the body. Each action of the
page patches only elements inside the content, so your navigation and breadcrumbs stay.
The URL of the page must end with /, and Mount redirects the path without it.
GET /admin/policies/debug/?bundle=authz&query=data.authz.allow opens the bundle authz with that query.
On the decision server
DebuggerModule.module puts the debugger on the decision server, with the hosted bundles as its catalog. The
authentication filter of the server checks each request to the debugger too.
val bundles = HostedBundles(rego, { Bundle.read(policies) })
val server = DecisionServer(
authentication,
listOf(
OpaApi.module(bundles),
DebuggerModule.module(bundles, "/debugger", frame)
)
)ServerDebugger.kt, lines 16 to 23, on GitHubExample inputs
The example inputs show as buttons above the input editor, in this order:
- The inputs of the test rules: each constant
with input as {...}in a rule whose name starts withtest_. - The files
examples/<name>.input.json, in any directory of the bundle. OPA ignores these files in a bundle.
See the README of rego4k-server-debugger for the keys, your own look
(DebuggerComponents) and the parts of the debugger on your own pages.