162 lines
9.3 KiB
Markdown
162 lines
9.3 KiB
Markdown
# Permissions — roles, relations, narrowed credentials
|
||
|
||
*Since 2026-11-23. Analysis and research: the homelab's
|
||
`data/log/2026-11-23-ai-agent-permission-model.md`. Gabriel's calls: form
|
||
`ai-agent-permissions-redesign_7n3ne1` (every recommendation taken). Code:
|
||
`backend/authz/`. Related: [security.md](security.md),
|
||
[multi-user.md](multi-user.md).*
|
||
|
||
## The model in one line
|
||
|
||
**RBAC for people, relationships for resources, attenuated credentials for
|
||
everything that acts on someone's behalf** — written by hand in Python, no
|
||
policy server.
|
||
|
||
```
|
||
effective(request) = role(person) ∪ allow − deny ← RBAC + overrides
|
||
∩ scope(credential) ← attenuation
|
||
then, per object, relation(person, object) ← whose conversation, which grant
|
||
```
|
||
|
||
Why not the alternatives: plain RBAC can't say "this conversation is yours"
|
||
(ownership is a relation; a role per project per person is role explosion);
|
||
a policy language (Cedar, OPA) or a Zanzibar server (OpenFGA, SpiceDB) is a
|
||
new dependency — or a new container — for 1 owner, ~10 guests and a small
|
||
team; capabilities everywhere don't suit people at a browser. The catalog /
|
||
role / relation split maps 1:1 onto Cedar's principal / action / resource if
|
||
non-developers ever have to edit policies.
|
||
|
||
## The pieces
|
||
|
||
| piece | file | what it is |
|
||
|---|---|---|
|
||
| catalog | `authz/catalog.py` | 42 named permissions in groups, each a **route** permission (the gate checks it) or a **feature** one (a handler or the run's scope reads it), in a **tier**: `person`, `admin`, `host` |
|
||
| roles | `authz/roles.py` | named bundles; built-ins **Operator** (everything, the owner, locked), **Admin** (person + admin tiers; assignable with team mode), **Member**, **Guest**, **Viewer**; custom ones by cloning (person tier only). Edits and customs in `roles.json` (`ROLES_PATH`) |
|
||
| a person | `authz/policy.py` | `users.json` `role` + `allow` / `deny` overrides (person tier only; core permissions can't be denied) |
|
||
| scopes | `authz/scopes.py` | what a credential acting for someone keeps: `cli`, `worker.read`, `worker.run`, `bot`, `phone`. A scope only removes — route permissions it lists (all methods, or safe ones only); feature permissions pass through |
|
||
| route table | `authz/routes.py` | `(method, path template) → permission` for every `/api` route, or `public` (signing in) |
|
||
| matrix | `authz/matrix.py` + `authz/matrix.golden.txt` | route × caller (anon, viewer, guest, member, cli, w.read, w.run, bot, phone, owner), checked in |
|
||
| API | `authz/api.py` | `GET/POST /api/roles`, `PUT/DELETE /api/roles/{id}`, `POST /api/roles/{id}/reset` (`users.manage`) |
|
||
| UI | `frontend/src/business/users/` | Settings → **Roles** (`pages/Roles.tsx`), a person's Access tab → **Role & permissions** (`components/RolePermissions.tsx`), helpers in `roles.ts` |
|
||
|
||
### Tiers
|
||
|
||
- **person** — anyone in `users.json` may hold it (role or override).
|
||
- **admin** — the hub's administration (people, security, every
|
||
conversation, dashboards). Only the Admin role holds it, and nobody can be
|
||
given Admin yet: ~100 handlers still tell the owner from a guest by identity
|
||
(`caller.is_owner`), so it arrives with team mode, together with converting
|
||
those checks to `caller.has(...)`.
|
||
- **host** — running code on the machine: the `.claude` editor (hooks),
|
||
workers, Claude logins, cron agents, webhooks, unscoped runs. **Operator
|
||
only** — the machine's owner. No other role can ever hold one.
|
||
|
||
### Who is the caller, then may they
|
||
|
||
`core/auth.py` resolves *who* (a guest session, the edge's Authelia user, a
|
||
read-only key, a host caller and the narrower identity it asks for), then
|
||
asks one question for everyone: `caller.allows(TABLE[route], method)`. A
|
||
refusal is a `perm_denied` line in the Security log (once per caller × route
|
||
per 10 min) — a guest sees *your role doesn't include "…" — ask Gabriel*.
|
||
|
||
| caller | identity | permissions |
|
||
|---|---|---|
|
||
| guest / member / viewer (session cookie) | their record | role ∪ allow − deny |
|
||
| their run's `ai-agent` CLI (guard broker) | the session's `runAs` | theirs ∩ `cli` |
|
||
| their AI app (MCP) | the connection's user | theirs ∩ `cli` |
|
||
| MCP bound to a worker | the owner + one profile | all ∩ `worker.read` / `worker.run` |
|
||
| `READONLY_USERS` (the `claude` bot) | the owner, read-only | Admin's reads minus people, security and the machine |
|
||
| `READONLY_API_KEY` (the desk phone) | the owner, read-only | `notifications.ledger` reads |
|
||
| `METRICS_TOKEN` bearer (Prometheus' scrape) | the owner, read-only | `metrics.read` (`GET /api/metrics`) only |
|
||
| Authelia `OWNER_USERS`, the host token, loopback | the owner | everything |
|
||
|
||
The `cli` scope keeps a run off the person's devices, drafts and settings and
|
||
never lets it **answer** forms or questions (`inbox.answer`): the human
|
||
answers.
|
||
|
||
### Relations stay in the handlers
|
||
|
||
The table says *may they call this route*; the handler decides *on which
|
||
objects*: `users/access.py` (a conversation's `owner` / `participants`; 404,
|
||
never 403, for one they can't see; `conversations.all` = see every one),
|
||
`Caller.can(slug, level)` (a project's `view < chat < ship`), each module's
|
||
`owner` field (computers, cron jobs, forms).
|
||
|
||
## The mock's copy of the catalog
|
||
|
||
`frontend/src/mock/rolesCatalog.json` is `/api/roles` with no edits; after
|
||
changing `authz/catalog.py` or `authz/roles.py`, regenerate it:
|
||
|
||
```bash
|
||
docker run --rm --entrypoint python -v "$PWD:/svc" -w /svc/backend --tmpfs /data:uid=1000 \
|
||
homelab-ai-agent -c "import json, os; from authz import catalog, roles; \
|
||
json.dump({**catalog.public(), 'tiers': list(catalog.TIERS), \
|
||
'roles': [roles.public(r) for r in roles.store().all()]}, \
|
||
open('/svc/frontend/src/mock/rolesCatalog.json', 'w'), indent=1, ensure_ascii=False); os._exit(0)"
|
||
```
|
||
|
||
(It runs as root: `chown` the file back, and remove any root-owned
|
||
`__pycache__` it leaves in `backend/`.)
|
||
|
||
## Adding a route
|
||
|
||
1. Add `(method, path) → permission` to `TABLE` in `authz/routes.py` — the
|
||
narrowest existing permission, or a new catalog entry (label, help, tier).
|
||
2. Filter by relation inside the handler if it touches someone's objects.
|
||
3. `UPDATE_GOLDEN=1 python -m unittest test_authz_matrix` and read the diff:
|
||
it is the list of callers who now reach the route.
|
||
|
||
`test_authz_matrix` fails on a route missing from the table, on an entry no
|
||
route has, and on any matrix change; `test_authz` covers roles, overrides,
|
||
the migration, scopes, the gate, the Roles API and the loopback audit.
|
||
|
||
## The migration (2026-11-23)
|
||
|
||
`UsersStore` turns each record's old switches into a role + overrides that
|
||
allow **exactly** what they did (`policy.migrate`, marked `authz: 1`, a
|
||
`users.json.bak-authz-<stamp>` copy first): `cli` → `cli.use`, `mcp` →
|
||
`mcp.connect`, `createProjects` → `projects.create` (retired 2026-11-25: New
|
||
project is the `new-project` plugin's managed grant now, and a migrating record
|
||
just drops the switch), `scratchBash` →
|
||
`runs.scratch.bash`, `codeMode` → `runs.codemode`, `computers.enabled` →
|
||
`computers.pair` (`computers.max` stays). The API still accepts the old
|
||
booleans (each becomes an override relative to the role) and still shows
|
||
them (read off the permissions).
|
||
|
||
What changed for whom, route by route (the old regex lists against the
|
||
table): guests may now rename / archive their own conversations from the
|
||
browser (their CLI already could); the CLI and MCP-guest scope reads its
|
||
person's projects' files, conversation details and forms (it could list
|
||
them); a worker-bound connection reads conversation details and plan usage
|
||
of its profile and managed its worker's scheduled turns (until 2026-11-28:
|
||
the `runs.schedule` permission and `/api/scheduled` went, and a bound
|
||
connection schedules nothing — `conversation-meta` checks the profile); the `claude` bot no longer reads people, security, accounts or
|
||
host-level pages; the phone's key reads its notifications and nothing else.
|
||
A route with a malformed id now reaches its handler (400/404) instead of a
|
||
gate 403 — the gate checks permissions, not id shapes.
|
||
|
||
## Loopback is not a credential — audit mode
|
||
|
||
Every host caller should present the **host token** (`INTERNAL_API_TOKEN`) as
|
||
`X-Internal-Auth` or `Authorization: Bearer`. The `ai-agent` and `notify`
|
||
CLIs read it from `AI_AGENT_TOKEN` or `~/.config/ai-agent/token` (0600,
|
||
written by the homelab deploy); the hub's own MCP calls send it. A loopback
|
||
or docker-gateway call **without** it is still the owner this round, but
|
||
logged as `loopback_tokenless` (Security → Access; once per method × route ×
|
||
peer × user agent an hour, never pushed). When that list is empty for a
|
||
week, refuse them (a one-line change in `_evaluate_gate`).
|
||
|
||
## Left
|
||
|
||
- **Team mode** (the devbox hub, conv `01a2090a`): Admin assignable (the
|
||
`is_owner` → `has()` pass), a Tailscale identity provider in the gate,
|
||
members on their own Claude login with no fallback, per-uid runs.
|
||
- **Refuse tokenless loopback** once the audit list is empty.
|
||
- **Per-run tokens**: an opaque token per run (`{subject, scope, expiry}`,
|
||
dying with the run) instead of the brokered `X-AIA-As-Session` header.
|
||
- **MCP scopes as real OAuth scopes** (the 2026-07-28 MCP authorization
|
||
revision: `insufficient_scope` + step-up) instead of `read` / `full`.
|
||
- The guest UI could hide what a role lacks beyond Settings (a Viewer still
|
||
sees a composer; sending explains the refusal). Settings already does: the
|
||
registry reads `me.perms` ([settings.md](settings.md)).
|