From cf05cb42e8ace12f505201fa43fc08c8cf680890 Mon Sep 17 00:00:00 2001 From: "pulse-triage[bot]" <249995291+pulse-triage[bot]@users.noreply.github.com> Date: Sat, 28 Nov 2026 18:51:15 +0000 Subject: [PATCH] Align Multi-Tenant, Audit, and Webhook Help with tenant access policy Update docs/MULTI_TENANT.md, docs/AUDIT_LOGGING.md, docs/WEBHOOKS.md, and their shipped mirrors in frontend-modern/public/docs/ to match current multi-organization tenant resolution and default-organization access control in internal/api/middleware_tenant.go, internal/api/org_access_policy.go, internal/api/org_handlers.go, and internal/api/router_routes_licensing.go: - Replace the outdated claim that default-organization members cannot be managed with the shipped shared-process policy: default access requires instance administration or explicit default viewer membership, instance admins manage default members via POST /api/orgs/default/members and DELETE /api/orgs/default/members/{userId}, first startup runs the one-time RBAC viewer migration recorded in default-org-access-migration.json, and active WebSocket/SSE streams re-check organization access every 10 seconds. - Document the actual tenant-context resolution order (X-Pulse-Org-ID, pulse_org_id browser cookie, single-organization-bound API token, signed-in user landing organization, then default), 403 default_org_access_denied, and organization-neutral bootstrap routes. - Add the five registered session-only invitation and incoming-share routes to the Multi-Tenant API reference table. - Bind these Help statements and mirrors to live Go source in scripts/tests/test_admin_docs.py. Contract-Neutral: Documentation and static doc-parity test alignment only with no runtime, API, schema, or subsystem contract delta. Change-source: pulse-maintainer --- docs/AUDIT_LOGGING.md | 2 +- docs/MULTI_TENANT.md | 56 ++++++++++-- docs/WEBHOOKS.md | 1 + frontend-modern/public/docs/AUDIT_LOGGING.md | 2 +- frontend-modern/public/docs/MULTI_TENANT.md | 56 ++++++++++-- frontend-modern/public/docs/WEBHOOKS.md | 1 + scripts/tests/test_admin_docs.py | 89 ++++++++++++++++++++ 7 files changed, 189 insertions(+), 18 deletions(-) diff --git a/docs/AUDIT_LOGGING.md b/docs/AUDIT_LOGGING.md index 9d7a306dfd..9061c28934 100644 --- a/docs/AUDIT_LOGGING.md +++ b/docs/AUDIT_LOGGING.md @@ -314,7 +314,7 @@ In multi-tenant deployments, most events are scoped to the active organization: - Tenant-aware events (logins, role changes, config updates) are stored per-organization. - Global events (`oidc_token_refresh`, `config_auto_import`, `websocket_multitenant_access_denied`, and `websocket_org_access_denied`) are written to the global audit logger rather than a tenant-specific database. - Switching organizations shows only that organization's tenant-scoped events. -- The tenant context is determined by `X-Pulse-Org-ID` header or session cookie. +- The tenant context is determined by the `X-Pulse-Org-ID` header, the `pulse_org_id` browser cookie, a single-organization-bound API token (when neither header nor cookie is sent), or a signed-in user's landing organization before falling back to `default`. Selection never overrides membership or a token's organization binding. See [Multi-Tenant Organizations](MULTI_TENANT.md) for details. diff --git a/docs/MULTI_TENANT.md b/docs/MULTI_TENANT.md index 96f3a15eb2..7244d3cb58 100644 --- a/docs/MULTI_TENANT.md +++ b/docs/MULTI_TENANT.md @@ -157,7 +157,30 @@ PATCHing a member URL: Only the current owner can transfer ownership, and only to an existing member after fresh sign-in. The owner cannot be demoted or removed as an ordinary -member update. Default-organization members cannot be managed here. +member update. + +**Default-organization membership:** In self-hosted shared-process +multi-organization mode, accessing the `default` organization requires either +instance administrator authority or explicit membership in `default`. Instance +administrators manage `default` members in **Settings → Organization → Access** +(or via session-authenticated `POST /api/orgs/default/members` and +`DELETE /api/orgs/default/members/{userId}`). Adding a member to `default` takes +effect immediately with HTTP `200` (not as a pending invitation) and assigns +only the `viewer` organization role, because instance RBAC roles control what +that user can do. Requesting any other organization role for `default` returns +HTTP `400` (`invalid_role`). + +On first startup under this policy, Pulse runs a one-time migration recorded in +`default-org-access-migration.json` in the base data directory. Existing RBAC +users who belong to no other organization and have no pending invitation are +added as `viewer` members of `default` (`migration:default-org-access`). +Proxy-authenticated users have no stored RBAC assignment to migrate from and +must be added explicitly by an instance administrator. + +Removing a member from any organization (including `default`) revokes their +access immediately. Active WebSocket and SSE streams re-check organization access +every 10 seconds, closing streams after membership removal, default-organization +revocation, or loss of instance-administrator authority. ### Sharing Resources @@ -229,33 +252,50 @@ When multi-tenant is enabled, **Settings → Organization** shows: | `PUT` | `/api/orgs/{id}` | Update organization (session only) | | `DELETE` | `/api/orgs/{id}` | Delete organization | | `GET` | `/api/orgs/{id}/members` | List members | -| `POST` | `/api/orgs/{id}/members` | Invite a member or update an existing member's role (session only) | -| `DELETE` | `/api/orgs/{id}/members/{userId}` | Remove a member | +| `POST` | `/api/orgs/{id}/members` | Invite a member, update an existing member's role, or grant `default` viewer access as an instance admin (session only) | +| `DELETE` | `/api/orgs/{id}/members/{userId}` | Remove a member (session only) | +| `GET` | `/api/orgs/{id}/invitations` | List pending invitations for an organization (session only) | +| `DELETE` | `/api/orgs/{id}/invitations/{userId}` | Revoke a pending invitation (session only) | +| `GET` | `/api/org-invitations` | List the current user's pending invitations (session only) | | `POST` | `/api/org-invitations/{id}/accept` | Accept your pending invitation (session only) | +| `DELETE` | `/api/org-invitations/{id}` | Decline your pending invitation (session only) | | `GET` | `/api/orgs/{id}/shares` | List outgoing shares | | `GET` | `/api/orgs/{id}/shares/incoming` | List incoming shares | | `POST` | `/api/orgs/{id}/shares` | Create or update a share (session only) | | `POST` | `/api/orgs/{id}/shares/incoming/{shareId}/accept` | Accept an incoming share (session only) | -| `DELETE` | `/api/orgs/{id}/shares/{shareId}` | Remove a share | +| `DELETE` | `/api/orgs/{id}/shares/incoming/{shareId}` | Decline an incoming share (session only) | +| `DELETE` | `/api/orgs/{id}/shares/{shareId}` | Remove a share (session only) | ### Tenant Context -All data-fetching endpoints respect the active organization context. The active org is determined by: +All data-fetching endpoints respect the active organization context. Selection is checked in this order before authorization runs: -1. `X-Pulse-Org-ID` header (API clients) -2. Session cookie (browser) -3. Falls back to the `default` organization +1. `X-Pulse-Org-ID` header (explicit request selection) +2. `pulse_org_id` browser cookie (separate from the sign-in session cookie) +3. Single-organization-bound API token (`orgId` or a single-entry `orgIds` list) when neither the header nor `pulse_org_id` is sent. Tokens bound to multiple organizations still require an explicit `X-Pulse-Org-ID` header. +4. Signed-in user's landing organization when no header, `pulse_org_id` cookie, or API token is present: their workspace in a hosted runtime, or the first organization they belong to when shared-process multi-organization mode restricts `default`. +5. Falls back to the `default` organization. Neither the active context nor a token scope overrides organization membership or a token's organization binding. A request without an explicit API token does not resolve or start a cold non-default organization monitor merely by sending `X-Pulse-Org-ID` or a session cookie. +When a signed-in user without an API token targets `default` without instance +administrator authority or explicit `default` membership, Pulse returns HTTP +`403` with error code `default_org_access_denied`. Organization-neutral +bootstrap routes (`/api/security/status`, `/api/logout`, `/api/health`, +`GET /api/orgs`, `/api/org-invitations`, `/api/org-invitations/*`, and the app +shell and static assets, excluding `/ws`, `/simple-stats`, and `/debug`) remain +accessible even with a stale `default` selection so the user can load the UI, +list their organizations, accept invitations, and sign out. + ## Storage - The **default** org uses the root data directory (backward compatible). - Non-default orgs store data in `{data-dir}/orgs/{org-id}/`. - Organization metadata is stored in `org.json` inside each org directory. +- `default-org-access-migration.json` in the base data directory records the one-time default-organization viewer migration so it does not re-run. - When multi-tenant is first enabled, legacy single-tenant data is migrated into `orgs/default/` with symlinks for compatibility. ## Troubleshooting diff --git a/docs/WEBHOOKS.md b/docs/WEBHOOKS.md index 4f8850dc81..1e63d82ebc 100644 --- a/docs/WEBHOOKS.md +++ b/docs/WEBHOOKS.md @@ -494,6 +494,7 @@ supply `X-Pulse-Org-ID`, it must name an organisation the token already allows. |---|---| | `400 invalid_org` | Confirm the Pulse instance and exact organisation ID. An ID from another installation is not a destination on this one. | | `403 access_denied` | Check the selected header/cookie and the token's organisation binding, or the signed-in user's membership. Adding scopes does not grant access to another organisation. | +| `403 default_org_access_denied` | A signed-in user without an API token targeted `default` in shared-process multi-organisation mode without instance-administrator authority or explicit `default` membership. Switch to an organisation the user belongs to, or have an instance administrator grant `default` access. | | `403 org_suspended` | The organisation is suspended or pending deletion. Stop the integration change; renaming, recreating or re-enrolling it is not access recovery. | Read the error code and message locally: HTTP `403` alone does not distinguish diff --git a/frontend-modern/public/docs/AUDIT_LOGGING.md b/frontend-modern/public/docs/AUDIT_LOGGING.md index 9d7a306dfd..9061c28934 100644 --- a/frontend-modern/public/docs/AUDIT_LOGGING.md +++ b/frontend-modern/public/docs/AUDIT_LOGGING.md @@ -314,7 +314,7 @@ In multi-tenant deployments, most events are scoped to the active organization: - Tenant-aware events (logins, role changes, config updates) are stored per-organization. - Global events (`oidc_token_refresh`, `config_auto_import`, `websocket_multitenant_access_denied`, and `websocket_org_access_denied`) are written to the global audit logger rather than a tenant-specific database. - Switching organizations shows only that organization's tenant-scoped events. -- The tenant context is determined by `X-Pulse-Org-ID` header or session cookie. +- The tenant context is determined by the `X-Pulse-Org-ID` header, the `pulse_org_id` browser cookie, a single-organization-bound API token (when neither header nor cookie is sent), or a signed-in user's landing organization before falling back to `default`. Selection never overrides membership or a token's organization binding. See [Multi-Tenant Organizations](MULTI_TENANT.md) for details. diff --git a/frontend-modern/public/docs/MULTI_TENANT.md b/frontend-modern/public/docs/MULTI_TENANT.md index 96f3a15eb2..7244d3cb58 100644 --- a/frontend-modern/public/docs/MULTI_TENANT.md +++ b/frontend-modern/public/docs/MULTI_TENANT.md @@ -157,7 +157,30 @@ PATCHing a member URL: Only the current owner can transfer ownership, and only to an existing member after fresh sign-in. The owner cannot be demoted or removed as an ordinary -member update. Default-organization members cannot be managed here. +member update. + +**Default-organization membership:** In self-hosted shared-process +multi-organization mode, accessing the `default` organization requires either +instance administrator authority or explicit membership in `default`. Instance +administrators manage `default` members in **Settings → Organization → Access** +(or via session-authenticated `POST /api/orgs/default/members` and +`DELETE /api/orgs/default/members/{userId}`). Adding a member to `default` takes +effect immediately with HTTP `200` (not as a pending invitation) and assigns +only the `viewer` organization role, because instance RBAC roles control what +that user can do. Requesting any other organization role for `default` returns +HTTP `400` (`invalid_role`). + +On first startup under this policy, Pulse runs a one-time migration recorded in +`default-org-access-migration.json` in the base data directory. Existing RBAC +users who belong to no other organization and have no pending invitation are +added as `viewer` members of `default` (`migration:default-org-access`). +Proxy-authenticated users have no stored RBAC assignment to migrate from and +must be added explicitly by an instance administrator. + +Removing a member from any organization (including `default`) revokes their +access immediately. Active WebSocket and SSE streams re-check organization access +every 10 seconds, closing streams after membership removal, default-organization +revocation, or loss of instance-administrator authority. ### Sharing Resources @@ -229,33 +252,50 @@ When multi-tenant is enabled, **Settings → Organization** shows: | `PUT` | `/api/orgs/{id}` | Update organization (session only) | | `DELETE` | `/api/orgs/{id}` | Delete organization | | `GET` | `/api/orgs/{id}/members` | List members | -| `POST` | `/api/orgs/{id}/members` | Invite a member or update an existing member's role (session only) | -| `DELETE` | `/api/orgs/{id}/members/{userId}` | Remove a member | +| `POST` | `/api/orgs/{id}/members` | Invite a member, update an existing member's role, or grant `default` viewer access as an instance admin (session only) | +| `DELETE` | `/api/orgs/{id}/members/{userId}` | Remove a member (session only) | +| `GET` | `/api/orgs/{id}/invitations` | List pending invitations for an organization (session only) | +| `DELETE` | `/api/orgs/{id}/invitations/{userId}` | Revoke a pending invitation (session only) | +| `GET` | `/api/org-invitations` | List the current user's pending invitations (session only) | | `POST` | `/api/org-invitations/{id}/accept` | Accept your pending invitation (session only) | +| `DELETE` | `/api/org-invitations/{id}` | Decline your pending invitation (session only) | | `GET` | `/api/orgs/{id}/shares` | List outgoing shares | | `GET` | `/api/orgs/{id}/shares/incoming` | List incoming shares | | `POST` | `/api/orgs/{id}/shares` | Create or update a share (session only) | | `POST` | `/api/orgs/{id}/shares/incoming/{shareId}/accept` | Accept an incoming share (session only) | -| `DELETE` | `/api/orgs/{id}/shares/{shareId}` | Remove a share | +| `DELETE` | `/api/orgs/{id}/shares/incoming/{shareId}` | Decline an incoming share (session only) | +| `DELETE` | `/api/orgs/{id}/shares/{shareId}` | Remove a share (session only) | ### Tenant Context -All data-fetching endpoints respect the active organization context. The active org is determined by: +All data-fetching endpoints respect the active organization context. Selection is checked in this order before authorization runs: -1. `X-Pulse-Org-ID` header (API clients) -2. Session cookie (browser) -3. Falls back to the `default` organization +1. `X-Pulse-Org-ID` header (explicit request selection) +2. `pulse_org_id` browser cookie (separate from the sign-in session cookie) +3. Single-organization-bound API token (`orgId` or a single-entry `orgIds` list) when neither the header nor `pulse_org_id` is sent. Tokens bound to multiple organizations still require an explicit `X-Pulse-Org-ID` header. +4. Signed-in user's landing organization when no header, `pulse_org_id` cookie, or API token is present: their workspace in a hosted runtime, or the first organization they belong to when shared-process multi-organization mode restricts `default`. +5. Falls back to the `default` organization. Neither the active context nor a token scope overrides organization membership or a token's organization binding. A request without an explicit API token does not resolve or start a cold non-default organization monitor merely by sending `X-Pulse-Org-ID` or a session cookie. +When a signed-in user without an API token targets `default` without instance +administrator authority or explicit `default` membership, Pulse returns HTTP +`403` with error code `default_org_access_denied`. Organization-neutral +bootstrap routes (`/api/security/status`, `/api/logout`, `/api/health`, +`GET /api/orgs`, `/api/org-invitations`, `/api/org-invitations/*`, and the app +shell and static assets, excluding `/ws`, `/simple-stats`, and `/debug`) remain +accessible even with a stale `default` selection so the user can load the UI, +list their organizations, accept invitations, and sign out. + ## Storage - The **default** org uses the root data directory (backward compatible). - Non-default orgs store data in `{data-dir}/orgs/{org-id}/`. - Organization metadata is stored in `org.json` inside each org directory. +- `default-org-access-migration.json` in the base data directory records the one-time default-organization viewer migration so it does not re-run. - When multi-tenant is first enabled, legacy single-tenant data is migrated into `orgs/default/` with symlinks for compatibility. ## Troubleshooting diff --git a/frontend-modern/public/docs/WEBHOOKS.md b/frontend-modern/public/docs/WEBHOOKS.md index 4f8850dc81..1e63d82ebc 100644 --- a/frontend-modern/public/docs/WEBHOOKS.md +++ b/frontend-modern/public/docs/WEBHOOKS.md @@ -494,6 +494,7 @@ supply `X-Pulse-Org-ID`, it must name an organisation the token already allows. |---|---| | `400 invalid_org` | Confirm the Pulse instance and exact organisation ID. An ID from another installation is not a destination on this one. | | `403 access_denied` | Check the selected header/cookie and the token's organisation binding, or the signed-in user's membership. Adding scopes does not grant access to another organisation. | +| `403 default_org_access_denied` | A signed-in user without an API token targeted `default` in shared-process multi-organisation mode without instance-administrator authority or explicit `default` membership. Switch to an organisation the user belongs to, or have an instance administrator grant `default` access. | | `403 org_suspended` | The organisation is suspended or pending deletion. Stop the integration change; renaming, recreating or re-enrolling it is not access recovery. | Read the error code and message locally: HTTP `403` alone does not distinguish diff --git a/scripts/tests/test_admin_docs.py b/scripts/tests/test_admin_docs.py index 16afb716d5..decf19370e 100644 --- a/scripts/tests/test_admin_docs.py +++ b/scripts/tests/test_admin_docs.py @@ -519,6 +519,95 @@ class AdminDocsTest(unittest.TestCase): auth_go = (ROOT / "internal/api/auth.go").read_text(encoding="utf-8") self.assertIn("if mtm != nil && explicitTokenProvided {", auth_go) + def test_multi_tenant_audit_and_webhooks_help_match_tenant_context_and_default_org_policy(self): + org_doc = " ".join((DOCS / "MULTI_TENANT.md").read_text(encoding="utf-8").split()) + audit_doc = " ".join((DOCS / "AUDIT_LOGGING.md").read_text(encoding="utf-8").split()) + webhooks_doc = " ".join((DOCS / "WEBHOOKS.md").read_text(encoding="utf-8").split()) + + for phrase in ( + "Default-organization membership:", + "POST /api/orgs/default/members", + "DELETE /api/orgs/default/members/{userId}", + "assigns only the `viewer` organization role", + "HTTP `400` (`invalid_role`)", + "`default-org-access-migration.json`", + "`migration:default-org-access`", + "Proxy-authenticated users have no stored RBAC assignment to migrate from", + "Active WebSocket and SSE streams re-check organization access every 10 seconds", + "`GET` | `/api/orgs/{id}/invitations`", + "`DELETE` | `/api/orgs/{id}/invitations/{userId}`", + "`GET` | `/api/org-invitations`", + "`DELETE` | `/api/org-invitations/{id}`", + "`DELETE` | `/api/orgs/{id}/shares/incoming/{shareId}`", + "`pulse_org_id` browser cookie", + "Single-organization-bound API token", + "Signed-in user's landing organization", + "`default_org_access_denied`", + "Organization-neutral bootstrap routes", + "`/api/security/status`, `/api/logout`, `/api/health`, `GET /api/orgs`, `/api/org-invitations`, `/api/org-invitations/*`", + "excluding `/ws`, `/simple-stats`, and `/debug`", + ): + with self.subTest(doc="MULTI_TENANT.md", phrase=phrase): + self.assertIn(phrase, org_doc) + self.assertNotIn("Default-organization members cannot be managed here.", org_doc) + + for phrase in ( + "the `pulse_org_id` browser cookie", + "a single-organization-bound API token", + "a signed-in user's landing organization before falling back to `default`", + "Selection never overrides membership or a token's organization binding.", + ): + with self.subTest(doc="AUDIT_LOGGING.md", phrase=phrase): + self.assertIn(phrase, audit_doc) + + self.assertIn("`403 default_org_access_denied`", webhooks_doc) + + for name in ("MULTI_TENANT.md", "AUDIT_LOGGING.md", "WEBHOOKS.md"): + with self.subTest(mirror=name): + self.assertEqual( + (DOCS / name).read_bytes(), + (ROOT / "frontend-modern/public/docs" / name).read_bytes(), + ) + + tenant_mw = (ROOT / "internal/api/middleware_tenant.go").read_text(encoding="utf-8") + for marker in ( + 'orgID = strings.TrimSpace(r.Header.Get("X-Pulse-Org-ID"))', + "if cookie, err := r.Cookie(CookieNameOrgID); err == nil {", + "else if len(t.OrgIDs) == 1 && strings.TrimSpace(t.OrgIDs[0]) != \"\" {", + "if landing := policy.LandingOrgID(r.Context(), userID); landing != \"\" {", + 'code = "default_org_access_denied"', + "if !result.Allowed && policy != nil && orgID == \"default\" && token == nil && orgNeutralBootstrapRequest(r) {", + ): + self.assertIn(marker, tenant_mw) + + org_policy = (ROOT / "internal/api/org_access_policy.go").read_text(encoding="utf-8") + for marker in ( + "var orgAccessRecheckInterval = 10 * time.Second", + 'case "/api/security/status", "/api/logout", "/api/health":', + 'case "/ws", "/simple-stats":', + 'const defaultOrgAccessMigrationFile = "default-org-access-migration.json"', + 'const defaultOrgAccessMigrationActor = "migration:default-org-access"', + ): + self.assertIn(marker, org_policy) + + org_handlers = (ROOT / "internal/api/org_handlers.go").read_text(encoding="utf-8") + for marker in ( + "func (h *OrgHandlers) handleAddDefaultOrgMember(", + "func (h *OrgHandlers) handleRemoveDefaultOrgMember(", + 'writeErrorResponse(w, http.StatusBadRequest, "invalid_role", "Default organization members are viewers; instance roles control what they can do", nil)', + ): + self.assertIn(marker, org_handlers) + + routes = (ROOT / "internal/api/router_routes_licensing.go").read_text(encoding="utf-8") + for marker in ( + 'r.mux.HandleFunc("GET /api/orgs/{id}/invitations"', + 'r.mux.HandleFunc("DELETE /api/orgs/{id}/invitations/{userId}"', + 'r.mux.HandleFunc("GET /api/org-invitations"', + 'r.mux.HandleFunc("DELETE /api/org-invitations/{id}"', + 'r.mux.HandleFunc("DELETE /api/orgs/{id}/shares/incoming/{shareId}"', + ): + self.assertIn(marker, routes) + if __name__ == "__main__": unittest.main()