feat: tenant registry and tenant-scoped requests (multi-tenant foundation, part 1) #8

Merged
crapougnax merged 2 commits from feat/7-tenant-registry into develop 2026-11-15 18:25:49 +00:00
Member

Summary

First part of the multi-tenant foundation. Without DATABASE_HOST nothing changes: the instance runs without tenants, as before.

  • Instance database: optional PostgreSQL, set up by SQL migrations (migrations/).
  • Tenant registry: tenants and memberships with roles, on @quatrain/tenancy.
  • Tenant-scoped URLs /t/<tenant>/…: the tenant is read from the path and checked against the membership of the user in the database, never taken from identity provider claims. The roles of the user become those held in that tenant. A tenant the user may not enter (unknown, suspended, not a member) is answered 404.
  • Administration command yarn admin: migrate, tenant:create, tenant:list, member:add, member:remove, member:list.
  • Two database roles: the command connects as the owner; the application uses a restricted role that can only read the registry, and refuses to open tenancy with a role that bypasses row-level security.

Documentation: docs/MULTI-TENANCY.md and docs/MULTI-TENANCY.fr.md.

Deliberate limit

Only /t/<tenant>/api/auth/me is served under a tenant URL. Upload, curation, extraction and git routes still read one repository, one storage and one AI provider for the whole instance; they answer 404 under a tenant URL until each serves the resources of the tenant. A tenant URL therefore never shows data shared with another tenant.

Dependencies

@quatrain/tenancy, @quatrain/backend-postgres and @quatrain/identifiers are beta releases needing the beta of core, backend and types. The other Quatrain packages only accept the previous stable versions, so resolutions pins those three to one version each. To remove once they have a stable release.

Checks

  • yarn test: 36 tests pass (20 new: settings, tenant requests, pipeline step, migrations).
  • Against a PostgreSQL 17 container: migrations applied twice, tenants and members created, reserved and duplicate slugs refused, unknown role refused; with the restricted role, registry reads work and a registry write is refused; with a superuser as application role, tenancy refuses to open.
  • Built server (yarn build, node dist/server/entry.mjs) with a stand-in identity provider: a member gets 200 with the roles of the tenant; another tenant, an unknown tenant, a non-member and a route that is not tenant-aware get 404; an anonymous request is redirected to the login page.

Not checked

  • The container image was not built; the Containerfile now copies bin/, migrations/ and src/instance/.
  • yarn dev fails on develop sources as well as here (exports is not defined in a Quatrain package under Vite), so the run above used the built server.

Notes for review

  • The migration creates a "user" table (two columns, empty): the backend adapter joins it for the author columns of every record and tries to create it when it cannot see it. Its name is imposed by the adapter and is a reserved word, hence the only quoted identifier.
  • .yarn/install-state.gz is tracked and changes at each install; it is left out of this pull request.

Next parts

  1. Local accounts and a generic OpenID Connect client, in place of the current provider.
  2. Per-tenant git repository, object storage and AI provider.
  3. Tenant-owned data with row isolation.

Closes #7

🤖 Generated with Claude Code

## Summary First part of the multi-tenant foundation. Without `DATABASE_HOST` nothing changes: the instance runs without tenants, as before. - **Instance database**: optional PostgreSQL, set up by SQL migrations (`migrations/`). - **Tenant registry**: tenants and memberships with roles, on `@quatrain/tenancy`. - **Tenant-scoped URLs** `/t/<tenant>/…`: the tenant is read from the path and checked against the membership of the user in the database, never taken from identity provider claims. The roles of the user become those held in that tenant. A tenant the user may not enter (unknown, suspended, not a member) is answered `404`. - **Administration command** `yarn admin`: `migrate`, `tenant:create`, `tenant:list`, `member:add`, `member:remove`, `member:list`. - **Two database roles**: the command connects as the owner; the application uses a restricted role that can only read the registry, and refuses to open tenancy with a role that bypasses row-level security. Documentation: `docs/MULTI-TENANCY.md` and `docs/MULTI-TENANCY.fr.md`. ## Deliberate limit Only `/t/<tenant>/api/auth/me` is served under a tenant URL. Upload, curation, extraction and git routes still read one repository, one storage and one AI provider for the whole instance; they answer `404` under a tenant URL until each serves the resources of the tenant. A tenant URL therefore never shows data shared with another tenant. ## Dependencies `@quatrain/tenancy`, `@quatrain/backend-postgres` and `@quatrain/identifiers` are beta releases needing the beta of `core`, `backend` and `types`. The other Quatrain packages only accept the previous stable versions, so `resolutions` pins those three to one version each. To remove once they have a stable release. ## Checks - `yarn test`: 36 tests pass (20 new: settings, tenant requests, pipeline step, migrations). - Against a PostgreSQL 17 container: migrations applied twice, tenants and members created, reserved and duplicate slugs refused, unknown role refused; with the restricted role, registry reads work and a registry write is refused; with a superuser as application role, tenancy refuses to open. - Built server (`yarn build`, `node dist/server/entry.mjs`) with a stand-in identity provider: a member gets `200` with the roles of the tenant; another tenant, an unknown tenant, a non-member and a route that is not tenant-aware get `404`; an anonymous request is redirected to the login page. ## Not checked - The container image was not built; the `Containerfile` now copies `bin/`, `migrations/` and `src/instance/`. - `yarn dev` fails on `develop` sources as well as here (`exports is not defined` in a Quatrain package under Vite), so the run above used the built server. ## Notes for review - The migration creates a `"user"` table (two columns, empty): the backend adapter joins it for the author columns of every record and tries to create it when it cannot see it. Its name is imposed by the adapter and is a reserved word, hence the only quoted identifier. - `.yarn/install-state.gz` is tracked and changes at each install; it is left out of this pull request. ## Next parts 1. Local accounts and a generic OpenID Connect client, in place of the current provider. 2. Per-tenant git repository, object storage and AI provider. 3. Tenant-owned data with row isolation. Closes #7 🤖 Generated with [Claude Code](https://claude.com/claude-code)
@quatrain/tenancy, @quatrain/backend-postgres and @quatrain/identifiers
are beta releases that need the beta of core, backend and types. The
other Quatrain packages still accept the previous stable versions only,
so a resolution pins the three shared packages to one version each until
their stable release.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
feat: tenant registry and tenant-scoped requests (#7)
All checks were successful
Build and Publish OCI Image / Build & Push ARM64 OCI Image (pull_request) Successful in 3m49s
e60e9c9b2b
First part of the multi-tenant foundation:

- instance PostgreSQL database, optional, with SQL migrations;
- tenant registry (tenants, memberships with roles) on @quatrain/tenancy;
- under /t/<tenant>/..., the tenant is read from the path and checked
  against the membership of the user in the database; the roles of the
  user become those held in that tenant; a tenant the user may not enter
  is answered 404;
- administration command (yarn admin): migrations, tenants, members. It
  connects as the owner role; the application uses a restricted role and
  refuses to start tenancy with a role that bypasses row-level security.

Only /api/auth/me is served under a tenant path for now: the other
routes still read instance-wide resources and are refused there.

Without DATABASE_HOST the instance runs without tenants, as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
crapougnax merged commit bbcdb25923 into develop 2026-11-15 18:25:49 +00:00
crapougnax deleted branch feat/7-tenant-registry 2026-11-15 18:25:49 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
modaka/studio!8
No description provided.