Declaring access on a route
Every API route says, where it is registered, what the caller needs. There is one table, routes() in crates/probeward-server/src/routes.rs, and one guard, crates/probeward-server/src/authz/need.rs, that enforces what the table says before the handler runs. The roles and actions themselves are described in access control.
Adding a route
Add one line to the table with the r! macro:
r!(post, "POST", "/api/v1/sensors/{id}/scan-now", On(Control, Locate::Sensor), sensors::scan_now),The macro has no form without a need, so a route cannot be added without deciding who may call it. The router, the OpenAPI document (each operation carries x-probeward-access) and the tests all read the same table.
Pick the first need in this list that fits:
| Need | Use it for | What the guard does |
|---|---|---|
On(action, Locate::…) | A route whose path names the object: /sensors/{id}, /nodes/{kind}/{id}, /incidents/{id}. | Finds the node the object hangs on, then answers 404 if the caller cannot read it and 403 if they can read it but lack action. The handler runs only when the caller may act. |
Global(action) | Things outside the tree: users, credentials, destinations, global settings, the audit record, remote probes, configuration files. Almost always Global(Administer); Global(Read) for what a root viewer may read. | 403 without the action on the root. The route's existence is not secret. |
Body(action) | A route whose body or query names the node: creating a group under a parent, a dependency between two nodes, a discovery job into a group. | Nothing before the handler. The handler must call Authz::require(action, node) for every node in the request. |
Listing | Lists and aggregates: the tree, alarms, the timeline, series, deliveries, jobs. | Nothing before the handler. The handler must filter to what the caller can read, using Authz::actions, sees, sees_everything or a store Scope. |
Session | The caller's own account and tokens, and shared definitions every user reads (sensor kinds, schedules, time zones). | Any signed-in user. |
Public, Pending | Health, setup, login, the push endpoint, the second factor. | None, or a session waiting for its second factor. |
Locate covers probe, group, device, sensor, any node by {kind}/{id}, incident, maintenance window, discovery job and dependency. A new kind of object that hangs on a node needs a new Locate variant and a lookup in locate(), not a check in the handler.
Handlers that decide for themselves
For Body and Listing the guard cannot know the answer, so it checks that the handler asked. Every Authz method that makes a decision marks the request; when such a route answers with a success and nothing was marked, the guard logs an error and counts it in authz_misses. The access matrix fails if that counter is not zero at the end. A listing that is genuinely the same for everyone still calls sees_everything() or mark() to say so.
Inside a handler:
- answer 404 with
not_found_entityfor an object the caller cannot read, exactly as if it did not exist; never 403, which would confirm the id; - answer 403 only for an object the caller can read but not change;
- filter every list and count by what the caller can read; never return a total that includes hidden objects;
- for anything that sends events, give the event a subject (sensor, device, incident, probe, discovery job) so the event stream can filter it. An event without a subject reaches root readers only;
- for a change that can narrow what anyone sees (a grant or global role removed, a user disabled, a session ended, a node moved or deleted, an import applied), take
crate::api::access_change(&state)before the change is committed, before any transaction opens, and keep the guard until the change is announced. Open event streams judge every event under the read side of the same lock, so no event is judged against the old access once the change is visible; dropping the guard makes them re-read the session, user, grants and tree.
Shared definitions: schedules
Schedules are shared weekly calendars. A schedule changes monitoring for every node that names it, across branches, so only root admins create, edit and delete them (Global(Administer)). Every signed-in user reads them (Session), so an operator can attach one in node settings, which is configure on the node; the usage list of a schedule shows only the nodes the caller can see. Maintenance windows hang on a node: creating, changing and deleting one needs configure there, ending or extending one needs control. In short: admins edit schedules; operators attach them and run windows; viewers see both.
Configuration files
Export, import planning and import of configuration files (/api/v1/config/...) are Global(Administer), even for a branch: an export carries credential references, destinations and schedules, which are global objects, and an import can create them. The probeward admin commands run on the server host and are outside the HTTP authorisation layer: whoever can run them already holds the database credentials. The file import there applies only with --as <administrator>, a user with the admin role on the root, who is named in the audit record. import-librenms writes template files into the template directory and touches no database row; discovery reads those templates, and who may run discovery where is decided as for any other job. Opening exports and imports to branch admins is an open decision.
Tests that hold this together
tests/api/access_matrix.rswalks the route table. Seven principals (a root admin, a branch admin, operator and viewer, a user with no grants, a token limited to the branch and a token limited to two actions) call every route on an object inside the branch and one outside it. The expected status follows from the declared need alone: 404 outside, 403 without the action, anything but 401, 403 or 404 when allowed. Every answer to a limited principal is searched for names and addresses from outside the branch. A new route is in the matrix without anyone adding it. A path parameter the matrix does not know is filled with an id that does not exist, so a route on a new kind of object should get its own case infill()to be tested on a real one.every_route_declares_what_it_needs_and_nothing_new_is_openin the same file lists the routes that may bePublicorSession. Adding one there is a deliberate, reviewed change.tests/api/access.rscovers inheritance and moves (access follows a node when it moves, and the open event stream follows both moves and grant changes),tests/api/stream_access.rsthe stream after a grant is removed, a device moves out, the user is disabled, the session ends or a token is revoked, and the window between a change's commit and its announcement, what a branch user learns about the rest of the tree, credentials staying on their targets, scoped tokens and the last-administrator rules.tests/api/leaks.rschecks that secrets appear nowhere outside the vault.ui/e2e/19-scoped-access.spec.tschecks the same through the UI.
Known limit
A change written by another process, which today means only the command-line import with --apply, does not pass through the server's lock. The server notices it at its next configuration poll (every five seconds) and its open event streams re-read the access then; until that poll an event about a node the import moved out of a branch can still reach that branch's users. Import through the API or the UI while the server runs to avoid that window.