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.

The debugger of the demo paused at a breakpoint on line 12 of approval.rego, not money.within_limit(...), for an order of 2500 over the limit. The code view marks the line, and the Input panel marks the part of the input that the line reads.The debugger of the demo paused at a breakpoint on line 12 of approval.rego, not money.within_limit(...), for an order of 2500 over the limit. The code view marks the line, and the Input panel marks the part of the input that the line reads.

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:

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:

sh
just demo                         # http://localhost:8181
./gradlew :demo:run -Pport=9000   # another port

Then do these steps:

  1. Click RBAC for an HTTP API, then the example carol-deletes-a-user, then Ask the decision server.
  2. Click Decide or press Ctrl+Enter. The page shows the decision, and below it the signed decision.
  3. 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:

Then do these steps:

  1. Press Ctrl+Enter (or click Debug) to start a replay of the query data.app.
  2. 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 at input.user.roles[0] and highlights it, and the locals show role.
  3. 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 highlights input.items[0].price, then input.items[1].price.
  4. Press F8 again. Open Output document in the variables. It shows deny as it grows: "item b costs too much".
  5. 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.

Alone.kt
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 GitHub

Most applications want the debugger on one of their own pages. Give the debugger these parts:

AdminPages.kt
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 GitHub

The 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.

ServerDebugger.kt
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 GitHub

Example inputs

The example inputs show as buttons above the input editor, in this order:

  1. The inputs of the test rules: each constant with input as {...} in a rule whose name starts with test_.
  2. 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.