Architecture
ADR 0017: Self-host mode and key routes
Date: 2026-12-02. Status: accepted.
Context#
Riskbound is going open source. Someone who runs it on their own machine should go from an empty database to a first policy decision in minutes, without reading our scripts. Two things stood in the way:
- Only
scripts/seed-dev.tsmade an agent key, and it refuses production. The e2e suite had the agent use the admin key because of it. - A fresh install had no tenant.
create-tenantworks, but it is a step a newcomer has to find, run inside the container, and get right.
Decision#
Key routes#
POST /v1/keysmakes anagentoradminkey.GET /v1/keyslists every key of the tenant, gateway keys included with their bound agents.POST /v1/keys/:id/revokerevokes any key. All admin only.- The full key is in the answer once. Only its SHA-256 is stored, as for every key. Listings show the name, role, prefix, created time, last-used time and revoked time, never the key or its hash.
- Making a key is an
api_key.createdevent, revoking one isapi_key.revoked, in the same transaction as the change. A gateway key revoked here is recorded asgateway_key.revoked, as before, so the evidence reads the same whichever route was used. Events name the key id and prefix, never the key. - Gateway keys are still made under
/v1/gateway-keys, since they need their agents (ADR 0012). - SSO. When a tenant requires SSO (ADR 0016), only a person signed in with SSO can make keys, gateway keys included. A leaked admin key cannot mint more keys. Revoking stays open to admin keys: it only takes access away.
- No self-lockout. A key cannot revoke itself. Another admin key or an SSO session can.
- Last used. The guard writes
api_keys.last_used_atafter a key is accepted, at most once a minute per key per process. It changes a non-key column, so the row lock isNO KEY UPDATE, which does not wait on decisions that reference the key (test/concurrency.pg.spec.ts). It is a hint for spotting unused keys, not evidence.
Self-host mode#
RISKBOUND_MODE=self_hostturns it on. The default ismulti_tenant, which changes nothing.- On start, under the same advisory lock as
create-tenant, the API asksriskbound_any_tenant(). Only when the answer is a clear no does it make one tenant (named byRISKBOUND_TENANT_NAME, defaultSelf-hosted) and one admin key, record the key as anapi_key.createdevent, and print the key once in a framed block in the logs. Anything else makes nothing. Several processes starting at once make one tenant. Once any tenant exists, by any route, it never runs again. GET /v1/setupneeds no key. In multi-tenant mode it answers only{"mode":"multi_tenant"}. In self-host mode it says, as yes or no, whether the tenant exists, a live agent key exists, and a policy is live. It reads them throughriskbound_setup_state(), aSECURITY DEFINERfunction that returns counts only (ADR 0015).- The dashboard's
/setuppage reads that state and walks through three steps: sign in with the printed admin key, register the agent and make an agent key (shown once with a copy button), and load the refunds starter policy fromdocs/use-case-refunds.md. Each step is an ordinary API call with the signed-in session. The login and overview pages link to it until setup is done.
Why#
- Keys are a normal admin task. Having them in the API and dashboard means no script with owner access to the database, and every key change is in the evidence.
- Printing the key in the logs is how a container hands a secret to the person who started it without a second channel. It happens once, because it only happens when the tenant is made.
- Counting rows across tenants through a narrow function keeps the unauthenticated route from ever seeing a row.
Consequences#
- The first admin key is in the logs. Whoever can read the API's logs
on first start can read it. That is the same person who can read
.envon the box. Rotate it from the Keys page once setup is done: make a new admin key, sign in with it, revoke the first. /v1/setuptells anyone whether a self-hosted install is set up. That is three yes or no answers about one organisation's own server, and nothing in multi-tenant mode.- A new key reaches the browser once, when it is made in the dashboard, so it can be copied. The session's own key still never does.
- Self-host mode does not stop more tenants.
create-tenantstill works against a self-hosted database. Self-host mode only decides whether the first one is made for you. seed-devstays for local development with a ready-made tenant. The docs now use the key routes.