diff --git a/app.py b/app.py index b6b09df7..b74df539 100644 --- a/app.py +++ b/app.py @@ -20,6 +20,7 @@ ) from routes.config_snapshots import register_config_snapshot_routes from routes.enterprise_preview import register_enterprise_preview_routes +from routes.tenants import register_tenant_routes from routes.core import register_core_routes from routes.dashboard_security import register_dashboard_security_routes from routes.intelligence_media import register_intelligence_media_routes @@ -147,6 +148,7 @@ def create_app() -> Flask: register_workbench_routes(app) register_config_snapshot_routes(app) register_enterprise_preview_routes(app) + register_tenant_routes(app, csrf=csrf) register_shadow_eval_routes(app, csrf, AuthService, MetricsService, ProxyService) register_knowledge_routes(app, csrf) register_gateway_mcp_routes(app, csrf) diff --git a/docs/tenant-hierarchy.md b/docs/tenant-hierarchy.md new file mode 100644 index 00000000..e6bff089 --- /dev/null +++ b/docs/tenant-hierarchy.md @@ -0,0 +1,109 @@ +# Organisations and teams + +`ORGANISATIONS_ENABLED` is off by default. Empty values mean off. Malformed values +mean off and produce one warning without the value. With the feature off, the +organisation and workspace endpoints return JSON 404 with `Cache-Control: no-store` +before authentication, CSRF checks or JSON parsing. Existing accounts, key records, +provider traffic and storage formats remain unchanged. Every principal has the +legacy tenant context and an empty namespace. + +Enable with `ORGANISATIONS_ENABLED=true` only after applying +`intelligence-migrations/0033_tenant_hierarchy.sql` to the configured account SQLite +database, or to D1 when `INTELLIGENCE_STORAGE_BACKEND=d1`. Missing tables or columns +return JSON 503 `tenant_storage_unavailable`, including for accounts without a +binding. There is no storage fallback or automatic account migration. + +The hierarchy has exactly two levels: organisation and team. IDs are generated, +opaque and immutable. Names may have up to 200 characters. Status is `active` or +`deactivated`. Each organisation retains at most 100 teams and 1,000 memberships, +including deactivated rows. The limit returns 409 `tenant_limit_reached`. +Membership roles are `admin`, `billing` and `member`; these labels do not grant +access to the gateway administrator dashboard. Each membership links an existing +account to one organisation and optionally one team in that organisation. + +## Administration + +The existing administrator dashboard session and CSRF token protect these routes: + +- `GET /admin/organisations` lists organisations; `POST` creates one with + `{"name":"Example"}` and returns 201. +- `GET /admin/organisations/` reads one; `PATCH` changes its name or status. +- `GET /admin/organisations//teams` lists teams; `POST` creates one with a name. +- `PATCH /admin/organisations//teams/` changes a team's name or status. +- `GET /admin/organisations//members` lists memberships. +- `PUT /admin/organisations//members/` sets role, team and status. + +All updates require `If-Match: ""`. A new membership requires `"0"`; +existing resources start at revision 1. Missing revisions return 428 +`revision_required`; malformed revisions return 400 `invalid_revision`; stale +revisions return 412 `revision_conflict`. Resource and binding responses expose +`ETag: ""`. Each successful update increments the revision. Team creation +does not change its parent organisation's revision. + +For example, to grant a team membership and explicitly select it for that account, +PUT a membership with `If-Match: "0"` and this body, using IDs returned by creation: + +```json +{"role":"member","team_id":"","status":"active","bind":true,"binding_revision":0} +``` + +`binding_revision` is the account's current binding revision (zero when unbound). +The membership, binding and audit rows commit together. A stale binding revision +leaves both old records intact. Set `status` to `deactivated` to revoke access; +rows and IDs remain stored. There is no deletion endpoint. Every successful +mutation records actor, action, target IDs, old/new revisions and UTC time. Audit +records exclude names, request bodies, keys and provider content. + +## Workspaces + +`GET /v1/workspaces` uses a gateway key with the `models` scope and returns only +its active memberships plus its current binding. Administrators also hold this +scope. Unbound accounts have binding revision zero and remain in the legacy +namespace even when they have memberships. + +`POST /v1/workspaces/switch` uses the same key and scope, requires +`If-Match: ""`, and accepts exactly +`{"org_id":"","team_id":""}`. Use `null` for an organisation-only +membership. The caller can select only its own active membership and its exact +team. The stored binding applies to the next request; existing request contexts +are unchanged. Dashboard CSRF applies to admin mutations; gateway-key workspace +selection uses the existing API CSRF exemption. + +Knowledge and Realtime resolve the workspace only after verifying the credential +for one principal. Accounts sharing a key prefix do not resolve each other's +workspaces. Their authenticated principals carry the validated `tenant_context`; +with organisations off, resolution returns the legacy context without querying +tenant storage. + +Resolution uses server storage and ignores `X-Org`, `X-Team`, +`X-MultiLLM-Workspace` and tenant fields in provider request bodies. A missing or +foreign organisation/team is 404 `workspace_not_found`; a deactivated +organisation/team, revoked membership or mismatched membership team is 403 +`workspace_forbidden`. A rejected binding prevents provider dispatch and later +policy, cache, claim and admission hooks. An administrator can repair a revoked +binding through the members endpoint. No automatic reparenting takes place. + +Python consumers use `current_tenant()` and `tenant_namespace()` from +`services.tenant_hierarchy`; native consumers use the validated `TenantContext` +from the tenant resolver. Namespaces are `org:` or `org:/team:`; the +legacy namespace is the empty string. Resolution rereads authoritative revisions +every request and has no process-local tenant cache. + +## Operational limits + +The private D1 domain is POST-only at +`http://intelligence.internal/v1/managed-state/tenants`, with a maximum request +size of 8,192 bytes. Writes use conditional SQL and an atomic batch with their +audit rows. Private Flask calls have bounded concurrency and wall time and never +replay an uncertain write. Standalone SQLite uses the configured account database +and serializes writes with an immediate transaction. + +Workspace identity does not introduce quotas, credits, payment collection, cost +allocation or provider discounts. Existing cache, idempotency, hosted-state and +usage ownership formats remain unchanged; a tenant binding alone does not isolate +those records. Consumers must incorporate the verified namespace before using +organisation isolation for shared records. Existing record ownership never moves +because an account selects a workspace. Audit and hierarchy rows are retained +indefinitely; content-retention policies do not erase them. Deactivation keeps +rows and continues to count toward the caps. There are no live-provider capability +or metering guarantees from these APIs. diff --git a/intelligence-migrations/0033_tenant_hierarchy.sql b/intelligence-migrations/0033_tenant_hierarchy.sql new file mode 100644 index 00000000..0b4dc73e --- /dev/null +++ b/intelligence-migrations/0033_tenant_hierarchy.sql @@ -0,0 +1,30 @@ +-- Optional two-level hierarchy; existing accounts and owned records are unchanged. +CREATE TABLE IF NOT EXISTS tenant_organisations ( + id TEXT PRIMARY KEY, name TEXT NOT NULL, + status TEXT NOT NULL CHECK (status IN ('active', 'deactivated')), + revision INTEGER NOT NULL CHECK (revision >= 1) +); +CREATE TABLE IF NOT EXISTS tenant_teams ( + id TEXT PRIMARY KEY, org_id TEXT NOT NULL, name TEXT NOT NULL, + status TEXT NOT NULL CHECK (status IN ('active', 'deactivated')), + revision INTEGER NOT NULL CHECK (revision >= 1) +); +CREATE INDEX IF NOT EXISTS tenant_teams_org ON tenant_teams (org_id, id); +CREATE TABLE IF NOT EXISTS tenant_memberships ( + org_id TEXT NOT NULL, principal TEXT NOT NULL, team_id TEXT, + role TEXT NOT NULL CHECK (role IN ('admin', 'billing', 'member')), + status TEXT NOT NULL CHECK (status IN ('active', 'deactivated')), + revision INTEGER NOT NULL CHECK (revision >= 1), + PRIMARY KEY (org_id, principal) +); +CREATE INDEX IF NOT EXISTS tenant_memberships_principal ON tenant_memberships (principal, org_id); +CREATE TABLE IF NOT EXISTS tenant_bindings ( + principal TEXT PRIMARY KEY, org_id TEXT NOT NULL, team_id TEXT, + revision INTEGER NOT NULL CHECK (revision >= 1) +); +CREATE TABLE IF NOT EXISTS tenant_audit ( + id TEXT PRIMARY KEY, actor TEXT NOT NULL, action TEXT NOT NULL, + org_id TEXT, team_id TEXT, principal TEXT, + old_revision INTEGER NOT NULL, new_revision INTEGER NOT NULL, at TEXT NOT NULL +); +CREATE INDEX IF NOT EXISTS tenant_audit_time ON tenant_audit (at, id); diff --git a/package.json b/package.json index c66e4a8f..9d3f9b48 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint": "ruff check . --select E9,F63,F7,F82", "typecheck": "mypy --ignore-missing-imports --follow-imports=skip --disable-error-code import-untyped --disable-error-code var-annotated --disable-error-code arg-type --disable-error-code assignment --disable-error-code operator app.py routes services providers streaming", "test": "python -m pytest -q", - "test:worker": "node --test tests/test_cloudflare_worker.mjs tests/test_cascades_worker.mjs tests/test_cascades_dashboard.mjs tests/test_image_quality_worker.mjs tests/test_secret_scan_worker.mjs tests/test_secret_firewall_worker.mjs tests/test_secret_firewall_coverage_worker.mjs tests/test_roleplay_compaction_budget_worker.mjs tests/test_roleplay_compaction_model_worker.mjs tests/test_roleplay_stream_validation_worker.mjs tests/test_aihubmix_worker.mjs tests/test_gpt_image_moderation_worker.mjs tests/test_dashboard_scripts.mjs tests/test_operations_dashboard.mjs tests/test_opencode_reasoning_worker.mjs tests/test_opencode_thinking_worker.mjs tests/test_nanogpt_reasoning_worker.mjs tests/test_nanogpt_speed_routing_worker.mjs tests/test_sse_heartbeat_worker.mjs tests/test_roleplay_worker.mjs tests/test_roleplay_config_worker.mjs tests/test_roleplay_glm_failover_worker.mjs tests/test_roleplay_intelligence_worker.mjs tests/test_roleplay_memory_worker.mjs tests/test_roleplay_compatibility_worker.mjs tests/test_roleplay_history_reconciliation_worker.mjs tests/test_roleplay_output_contract_analysis.mjs tests/test_roleplay_output_contract_cleanup_worker.mjs tests/test_roleplay_output_contract_nonstream_worker.mjs tests/test_roleplay_output_contract_worker.mjs tests/test_roleplay_performance_worker.mjs tests/test_roleplay_quality_routing_worker.mjs tests/test_roleplay_reasoning_worker.mjs tests/test_roleplay_session_storage_worker.mjs tests/test_roleplay_transport_failure_worker.mjs tests/test_roleplay_unlimited_worker.mjs tests/test_roleplay_workbench_worker.mjs tests/test_roleplay_candidate_context_worker.mjs tests/test_roleplay_operator_worker.mjs tests/test_workbench_stream.mjs tests/test_intelligence_worker.mjs tests/test_intelligence_d1_worker.mjs tests/test_intelligence_auth_worker.mjs tests/test_intelligence_outbound_worker.mjs tests/test_intelligence_operator.mjs tests/test_control_users_worker.mjs tests/test_control_audit_worker.mjs tests/test_access_sso_worker.mjs tests/test_d1_schema_worker.mjs tests/test_shadow_eval_worker.mjs tests/test_auto_routes_worker.mjs tests/test_ai_outbound_worker.mjs tests/test_knowledge_*.mjs tests/test_control_state_worker.mjs tests/test_route_health_worker.mjs tests/test_gateway_mcp_worker.mjs tests/test_usage_ledger_worker.mjs tests/test_media_outbound_worker.mjs tests/test_media_files_worker.mjs tests/test_media_jobs_worker.mjs tests/test_tool_repair_worker.mjs tests/test_prometheus_worker.mjs tests/test_evaluation_noise_floor_worker.mjs tests/test_prompt_templates_worker.mjs tests/test_config_snapshots_worker.mjs tests/test_managed_idempotency_worker.mjs tests/test_wave3_hub_worker.mjs tests/test_wave3_transport_worker.mjs tests/test_admission_leases_worker.mjs tests/test_edge_request_metrics_worker.mjs tests/test_request_cancellation_worker.mjs tests/test_mcp_contract_drift_worker.mjs tests/test_zero_content_retention_worker.mjs tests/test_prompt_cache_cost_worker.mjs tests/test_config_revision_sync_worker.mjs tests/test_gateway_alerts_worker.mjs tests/test_session_tiers_worker.mjs tests/test_usage_reservations_worker.mjs tests/test_shared_generation_cache_worker.mjs tests/test_deferred_tools_worker.mjs tests/test_generation_deadline_worker.mjs tests/test_wave4_worker.mjs tests/test_responses_state_worker.mjs tests/test_gateway_batches_worker.mjs tests/test_semantic_generation_cache_worker.mjs tests/test_canary_traffic_worker.mjs tests/test_context_pages_worker.mjs tests/test_observability_exports_worker.mjs tests/test_prompt_injection_worker.mjs tests/test_pii_rehydration_worker.mjs tests/test_wave5_worker.mjs tests/test_usage_receipts_worker.mjs tests/test_learned_cooldown_worker.mjs tests/test_stream_cost_breaker_worker.mjs tests/test_realtime_worker.mjs tests/test_latency_slo_worker.mjs tests/test_context_canary_worker.mjs tests/test_enterprise_contracts_worker.mjs tests/test_wave6_worker.mjs", + "test:worker": "node --test tests/test_cloudflare_worker.mjs tests/test_cascades_worker.mjs tests/test_cascades_dashboard.mjs tests/test_image_quality_worker.mjs tests/test_secret_scan_worker.mjs tests/test_secret_firewall_worker.mjs tests/test_secret_firewall_coverage_worker.mjs tests/test_roleplay_compaction_budget_worker.mjs tests/test_roleplay_compaction_model_worker.mjs tests/test_roleplay_stream_validation_worker.mjs tests/test_aihubmix_worker.mjs tests/test_gpt_image_moderation_worker.mjs tests/test_dashboard_scripts.mjs tests/test_operations_dashboard.mjs tests/test_opencode_reasoning_worker.mjs tests/test_opencode_thinking_worker.mjs tests/test_nanogpt_reasoning_worker.mjs tests/test_nanogpt_speed_routing_worker.mjs tests/test_sse_heartbeat_worker.mjs tests/test_roleplay_worker.mjs tests/test_roleplay_config_worker.mjs tests/test_roleplay_glm_failover_worker.mjs tests/test_roleplay_intelligence_worker.mjs tests/test_roleplay_memory_worker.mjs tests/test_roleplay_compatibility_worker.mjs tests/test_roleplay_history_reconciliation_worker.mjs tests/test_roleplay_output_contract_analysis.mjs tests/test_roleplay_output_contract_cleanup_worker.mjs tests/test_roleplay_output_contract_nonstream_worker.mjs tests/test_roleplay_output_contract_worker.mjs tests/test_roleplay_performance_worker.mjs tests/test_roleplay_quality_routing_worker.mjs tests/test_roleplay_reasoning_worker.mjs tests/test_roleplay_session_storage_worker.mjs tests/test_roleplay_transport_failure_worker.mjs tests/test_roleplay_unlimited_worker.mjs tests/test_roleplay_workbench_worker.mjs tests/test_roleplay_candidate_context_worker.mjs tests/test_roleplay_operator_worker.mjs tests/test_workbench_stream.mjs tests/test_intelligence_worker.mjs tests/test_intelligence_d1_worker.mjs tests/test_intelligence_auth_worker.mjs tests/test_intelligence_outbound_worker.mjs tests/test_intelligence_operator.mjs tests/test_control_users_worker.mjs tests/test_control_audit_worker.mjs tests/test_access_sso_worker.mjs tests/test_d1_schema_worker.mjs tests/test_shadow_eval_worker.mjs tests/test_auto_routes_worker.mjs tests/test_ai_outbound_worker.mjs tests/test_knowledge_*.mjs tests/test_control_state_worker.mjs tests/test_route_health_worker.mjs tests/test_gateway_mcp_worker.mjs tests/test_usage_ledger_worker.mjs tests/test_media_outbound_worker.mjs tests/test_media_files_worker.mjs tests/test_media_jobs_worker.mjs tests/test_tool_repair_worker.mjs tests/test_prometheus_worker.mjs tests/test_evaluation_noise_floor_worker.mjs tests/test_prompt_templates_worker.mjs tests/test_config_snapshots_worker.mjs tests/test_managed_idempotency_worker.mjs tests/test_wave3_hub_worker.mjs tests/test_wave3_transport_worker.mjs tests/test_admission_leases_worker.mjs tests/test_edge_request_metrics_worker.mjs tests/test_request_cancellation_worker.mjs tests/test_mcp_contract_drift_worker.mjs tests/test_zero_content_retention_worker.mjs tests/test_prompt_cache_cost_worker.mjs tests/test_config_revision_sync_worker.mjs tests/test_gateway_alerts_worker.mjs tests/test_session_tiers_worker.mjs tests/test_usage_reservations_worker.mjs tests/test_shared_generation_cache_worker.mjs tests/test_deferred_tools_worker.mjs tests/test_generation_deadline_worker.mjs tests/test_wave4_worker.mjs tests/test_responses_state_worker.mjs tests/test_gateway_batches_worker.mjs tests/test_semantic_generation_cache_worker.mjs tests/test_canary_traffic_worker.mjs tests/test_context_pages_worker.mjs tests/test_observability_exports_worker.mjs tests/test_prompt_injection_worker.mjs tests/test_pii_rehydration_worker.mjs tests/test_wave5_worker.mjs tests/test_usage_receipts_worker.mjs tests/test_learned_cooldown_worker.mjs tests/test_stream_cost_breaker_worker.mjs tests/test_realtime_worker.mjs tests/test_latency_slo_worker.mjs tests/test_context_canary_worker.mjs tests/test_enterprise_contracts_worker.mjs tests/test_wave6_worker.mjs tests/test_tenant_hierarchy_worker.mjs", "db:migrate": "python scripts/validate_sqlite_schema.py --apply", "db:validate": "python scripts/validate_sqlite_schema.py", "security:secrets": "python scripts/check_static_secrets.py", diff --git a/routes/tenants.py b/routes/tenants.py new file mode 100644 index 00000000..668b463d --- /dev/null +++ b/routes/tenants.py @@ -0,0 +1,127 @@ +"""Hierarchy administration uses dashboard sessions; workspace selection uses gateway keys.""" +import re +from functools import wraps + +from flask import g, jsonify, request + +from route_helpers import api_authenticate_only, login_required +from routes.config_snapshots import json_errors +from routes.core import require_admin_dashboard_user +from services import tenant_hierarchy as tenants + + +def _revision(): + value = request.headers.get("If-Match") + if value is None: + raise tenants.TenantError("revision_required", 428) + if not re.fullmatch(r'"[0-9]{1,16}"', value): + raise tenants.TenantError("invalid_revision") + return tenants._revision(int(value[1:-1])) + + +def _data(): + request.max_content_length = tenants.MAX_BODY_BYTES + value = request.get_json(silent=True) + if not isinstance(value, dict): + raise tenants.TenantError("invalid_request") + return value + + +def _reply(value, status=200): + response = jsonify(value) + response.status_code = status + response.headers["Cache-Control"] = "no-store" + if "revision" in value: + response.headers["ETag"] = f'"{value["revision"]}"' + return response + + +def register_tenant_routes(app, *, csrf=None, store=None): + app.extensions["tenant_store"] = store or tenants.TenantStore() + app.register_error_handler(tenants.TenantError, tenants.error_response) + + def is_tenant_route(): + return (request.path == "/admin/organisations" or request.path.startswith("/admin/organisations/") + or request.path == "/v1/workspaces" or request.path.startswith("/v1/workspaces/")) + + def disabled_tenants(): + if is_tenant_route() and not tenants.enabled(): + return jsonify({"error": "not_found"}), 404, {"Cache-Control": "no-store"} + if is_tenant_route(): + request.max_content_length = tenants.MAX_BODY_BYTES + return None + + app.before_request_funcs.setdefault(None, []).insert(0, disabled_tenants) + + @app.after_request + def tenant_no_store(response): + if is_tenant_route(): + response.headers["Cache-Control"] = "no-store" + return response + + def call(operation, **values): + return app.extensions["tenant_store"].request(operation, **values) + + def administrator(view): + @wraps(view) + @json_errors + @login_required + def guarded(*args, **kwargs): + user = require_admin_dashboard_user() + return view(user["username"], *args, **kwargs) + return guarded + + @app.route("/admin/organisations", methods=["GET", "POST"]) + @administrator + def organisations(actor): + if request.method == "GET": + return _reply(call("org_list")) + return _reply(call("org_create", actor=actor, data=_data()), 201) + + @app.route("/admin/organisations/", methods=["GET", "PATCH"]) + @administrator + def organisation(actor, org_id): + if request.method == "GET": + return _reply(call("org_get", org_id=org_id)) + return _reply(call("org_update", actor=actor, org_id=org_id, revision=_revision(), data=_data())) + + @app.route("/admin/organisations//teams", methods=["GET", "POST"]) + @administrator + def teams(actor, org_id): + if request.method == "GET": + return _reply(call("team_list", org_id=org_id)) + return _reply(call("team_create", actor=actor, org_id=org_id, data=_data()), 201) + + @app.patch("/admin/organisations//teams/") + @administrator + def team(actor, org_id, team_id): + return _reply(call("team_update", actor=actor, org_id=org_id, team_id=team_id, + revision=_revision(), data=_data())) + + @app.get("/admin/organisations//members") + @administrator + def members(actor, org_id): + return _reply(call("member_list", org_id=org_id)) + + @app.put("/admin/organisations//members/") + @administrator + def member(actor, org_id, principal): + return _reply(call("member_set", actor=actor, org_id=org_id, principal=principal, + revision=_revision(), data=_data())) + + @app.get("/v1/workspaces") + @api_authenticate_only(required_scope="models") + def workspaces(): + user = g.authenticated_user + return _reply(call("workspaces", principal=user.get("username") or user["id"])) + + @app.post("/v1/workspaces/switch") + @api_authenticate_only(required_scope="models") + def switch_workspace(): + user = g.authenticated_user + principal = user.get("username") or user["id"] + return _reply(call("binding_set", principal=principal, actor=principal, + revision=_revision(), data=_data())) + + # Existing API-key endpoints use this exemption; dashboard mutations retain CSRF. + (csrf or app.extensions.get("csrf")).exempt(switch_workspace) diff --git a/services/auth_service.py b/services/auth_service.py index 9c910254..4b575062 100644 --- a/services/auth_service.py +++ b/services/auth_service.py @@ -943,6 +943,8 @@ def _update_login(cls, username: str, last_login: datetime) -> None: @classmethod def _update_key_usage(cls, username: str, remote_addr: Optional[str] = None) -> None: + from services.tenant_hierarchy import verify_before_key_usage + verify_before_key_usage(username) user = cls._users.get(username) if not user: return @@ -1003,6 +1005,8 @@ def verify_api_key(cls, api_key: Optional[str], remote_addr: Optional[str] = Non and admin_api_key and hmac.compare_digest(api_key, admin_api_key) ): + from services.tenant_hierarchy import verify_before_key_usage + verify_before_key_usage(default_username) return cls._verify_bootstrap_admin(default_username, api_key, remote_addr) if api_key.startswith(KEY_NAMESPACE): diff --git a/services/gateway_extensions.py b/services/gateway_extensions.py index b281f37c..7652d26f 100644 --- a/services/gateway_extensions.py +++ b/services/gateway_extensions.py @@ -7,7 +7,7 @@ from services.provider_catalog_refresh import refresh_provider_catalog_revision AUTHENTICATED_HOOK_ORDER = ( - "request_policy_hook", "prompt_injection_request_hook", "spillover_hook", "pii_request_hook", + "tenant_context_hook", "request_policy_hook", "prompt_injection_request_hook", "spillover_hook", "pii_request_hook", "responses_state_hook", "context_canary_request_hook", "generation_deadline_hook", "latency_slo_request_hook", "idempotency_request_hook", "admit", ) @@ -33,6 +33,14 @@ def after_authentication(): if getattr(g, "gateway_authenticated_hooks_ran", False): return None g.gateway_authenticated_hooks_ran = True + from services.tenant_hierarchy import current_tenant, enabled, tenant_context_hook + # Preserve the default registrar list while resolving legacy identity on every request. + if not enabled(): + g.tenant_context = current_tenant() + elif not any(hook.__name__ == "tenant_context_hook" for hook in current_app.extensions.get("gateway_after_authentication", ())): + refused = tenant_context_hook() + if refused is not None: + return refused for hook in current_app.extensions.get("gateway_after_authentication", ()): refused = hook() if refused is not None: @@ -41,6 +49,13 @@ def after_authentication(): return None +def register_tenants(app): + from services.tenant_hierarchy import TenantError, enabled, error_response, tenant_context_hook + app.register_error_handler(TenantError, error_response) + if enabled(): + register_authenticated_hook(app, tenant_context_hook) + + def register_retention(app): register_authenticated_hook(app, request_policy_hook) @@ -95,7 +110,7 @@ def gateway_callbacks(*, csrf=None): from middleware.rate_limit_headers import register_rate_limit_headers from services.context_canary_hook import register_context_canary from services.pii_redaction import register_pii_redaction - return (register_retention, register_injection_decision, register_batch_spillover, register_pii_redaction, + return (register_tenants, register_retention, register_injection_decision, register_batch_spillover, register_pii_redaction, partial(register_hosted_responses, csrf=csrf), register_context_canary, register_deadline, register_latency_slo, register_managed_idempotency, register_admission, register_cooldown_errors, register_rate_limit_headers) diff --git a/services/tenant_hierarchy.py b/services/tenant_hierarchy.py new file mode 100644 index 00000000..6e1e3db5 --- /dev/null +++ b/services/tenant_hierarchy.py @@ -0,0 +1,366 @@ +"""Server-owned tenant bindings and atomic, content-free hierarchy administration.""" +from __future__ import annotations + +import hashlib +import json +import logging +import os +import queue +import re +import sqlite3 +import threading +import time +import uuid +from contextlib import closing +from datetime import datetime, timezone +from functools import lru_cache + +from flask import current_app, g, has_request_context, jsonify + +from services.enterprise_contract import AuthorityOperation, TenantContext, legacy_tenant +from services.sqlite_store import connect, storage_path + +logger = logging.getLogger(__name__) +PRIVATE_URL = "http://intelligence.internal/v1/managed-state/tenants" +MAX_BODY_BYTES = 8192 +_SLOTS = threading.BoundedSemaphore(8) +_ID = re.compile(r"[A-Za-z0-9_:.\-]{1,128}\Z") +OPERATIONS = frozenset({"org_list", "org_get", "org_create", "org_update", "team_list", "team_create", + "team_update", "member_list", "member_set", "workspaces", "binding_set", "resolve"}) +ERROR_STATUSES = {"workspace_not_found": 404, "workspace_forbidden": 403, "principal_not_found": 404, + "revision_required": 428, "revision_conflict": 412, "tenant_limit_reached": 409, + "invalid_request": 400, "invalid_revision": 400, "tenant_storage_unavailable": 503} + + +class TenantError(Exception): + def __init__(self, code, status=400): + super().__init__(code) + self.code, self.status = code, status + + +def error_response(error): + return jsonify({"error": {"code": error.code, "message": "The workspace operation could not be authorized or stored."}}), error.status, {"Cache-Control": "no-store"} + + +@lru_cache(maxsize=1) +def _warn_once(): + logger.warning("Invalid ORGANISATIONS_ENABLED; organisations disabled") + + +def enabled(): + flag = os.environ.get("ORGANISATIONS_ENABLED", "").strip().lower() + if flag in {"", "0", "false", "no", "off"}: + return False + if flag in {"1", "true", "yes", "on"}: + return True + _warn_once() + return False + + +def _text(value, maximum=128): + if not isinstance(value, str) or not value.strip() or len(value) > maximum or any(ord(c) < 32 or ord(c) == 127 for c in value): + raise TenantError("invalid_request") + return value + + +def _identifier(value): + if not isinstance(value, str) or not _ID.fullmatch(value): + raise TenantError("invalid_request") + return value + + +def _revision(value): + if value is None: + raise TenantError("revision_required", 428) + if type(value) is not int or not 0 <= value < 9007199254740000: + raise TenantError("invalid_revision") + return value + + +def _legacy(principal): + try: + context = TenantContext(principal) + except ValueError: + context = TenantContext("principal:" + hashlib.sha256(principal.encode()).hexdigest()) + return legacy_tenant(AuthorityOperation(context, "workspace", 0, "resolve")) + + +def private_call(document): + """Reuse the bounded private transport, with one submission and no replay.""" + from services import intelligence_d1_store as transport + try: + body = json.dumps(document, separators=(",", ":"), allow_nan=False).encode() + except (ValueError, TypeError): + raise TenantError("invalid_request") from None + if len(body) > MAX_BODY_BYTES or not _SLOTS.acquire(blocking=False): + raise TenantError("tenant_storage_unavailable", 503) + stopped, results = threading.Event(), queue.Queue(maxsize=1) + deadline = time.monotonic() + 5 + thread = threading.Thread(target=transport._submit, + args=(PRIVATE_URL, body, stopped, deadline, results, _SLOTS, (200,), (2, 3)), daemon=True) + try: + thread.start() + except RuntimeError: + _SLOTS.release() + raise TenantError("tenant_storage_unavailable", 503) from None + try: + success, result = results.get(timeout=max(0, deadline - time.monotonic())) + if not success: + if isinstance(result, transport.PrivateIntelligenceError): + raise TenantError(result.code, result.status) + raise TenantError("tenant_storage_unavailable", 503) + return result + except queue.Empty: + raise TenantError("tenant_storage_unavailable", 503) from None + finally: + stopped.set() + + +def _principal_exists(principal): + from services.auth_service import AuthService + return AuthService._load_user_by_username(principal) is not None + + +class TenantStore: + def __init__(self, *, call=None, principal_exists=None): + self.call = call or private_call + self.principal_exists = principal_exists or _principal_exists + + def request(self, operation, **values): + if not enabled(): + raise TenantError("not_found", 404) + if operation not in OPERATIONS: + raise TenantError("invalid_request") + if os.environ.get("INTELLIGENCE_STORAGE_BACKEND", "").strip() == "d1": + return self._remote(operation, values) + try: + path = storage_path("AUTH_DB_PATH", "auth.sqlite3") + if not path.is_file(): + raise TenantError("tenant_storage_unavailable", 503) + with closing(connect(path)) as db: + db.execute("BEGIN IMMEDIATE" if operation.endswith(("create", "update", "set")) else "BEGIN") + # Verify all tables and columns before a mutation or a legacy resolution. + for sql in ("SELECT id,name,status,revision FROM tenant_organisations LIMIT 0", + "SELECT id,org_id,name,status,revision FROM tenant_teams LIMIT 0", + "SELECT org_id,principal,team_id,role,status,revision FROM tenant_memberships LIMIT 0", + "SELECT principal,org_id,team_id,revision FROM tenant_bindings LIMIT 0", + "SELECT id,actor,action,org_id,team_id,principal,old_revision,new_revision,at FROM tenant_audit LIMIT 0"): + db.execute(sql) + result = self._execute(db, operation, values) + db.commit() + return result + except TenantError: + raise + except Exception: + raise TenantError("tenant_storage_unavailable", 503) from None + + def _remote(self, operation, values): + try: + document = self.call({"version": 1, "operation": operation, **values}) + if not isinstance(document, dict) or document.get("version") != 1: + raise ValueError() + if "error" in document: + code, status = document["error"]["code"], document["status"] + if ERROR_STATUSES.get(code) != status: + raise ValueError() + raise TenantError(code, status) + result = document["result"] + if not isinstance(result, dict): + raise ValueError() + return result + except TenantError: + raise + except Exception: + raise TenantError("tenant_storage_unavailable", 503) from None + + @staticmethod + def _one(db, sql, params=()): + row = db.execute(sql, params).fetchone() + return dict(row) if row else None + + def _org(self, db, org_id, *, active=False): + row = self._one(db, "SELECT * FROM tenant_organisations WHERE id=?", (_identifier(org_id),)) + if row is None: + raise TenantError("workspace_not_found", 404) + if active and row["status"] != "active": + raise TenantError("workspace_forbidden", 403) + return row + + def _team(self, db, org_id, team_id, *, active=False): + row = self._one(db, "SELECT * FROM tenant_teams WHERE org_id=? AND id=?", (org_id, _identifier(team_id))) + if row is None: + raise TenantError("workspace_not_found", 404) + if active and row["status"] != "active": + raise TenantError("workspace_forbidden", 403) + return row + + def _workspace(self, db, principal, org_id, team_id): + org = self._org(db, org_id, active=True) + team = self._team(db, org_id, team_id, active=True) if team_id is not None else None + membership = self._one(db, "SELECT * FROM tenant_memberships WHERE org_id=? AND principal=?", (org_id, principal)) + if not membership or membership["status"] != "active" or membership["team_id"] != team_id: + raise TenantError("workspace_forbidden", 403) + return org["revision"] + (team["revision"] if team else 0) + membership["revision"] + + @staticmethod + def _audit(db, actor, action, org_id, team_id, principal, old, new): + db.execute("INSERT INTO tenant_audit VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", + (uuid.uuid4().hex, _text(actor), action, org_id, team_id, principal, old, new, + datetime.now(timezone.utc).isoformat())) + + @staticmethod + def _cas(row, revision): + if (row["revision"] if row else 0) != _revision(revision): + raise TenantError("revision_conflict", 412) + + def _execute(self, db, operation, v): + if operation == "org_list": + return {"organisations": [dict(row) for row in db.execute("SELECT * FROM tenant_organisations ORDER BY id")]} + if operation in {"workspaces", "resolve", "binding_set"}: + return self._principal_operation(db, operation, v) + if operation == "org_create": + return self._named_write(db, operation, v, None) + org_id = v.get("org_id") + org = self._org(db, org_id) + if operation == "org_get": + return org + if operation in {"org_update", "team_create", "team_update"}: + return self._named_write(db, operation, v, org) + if operation == "team_list": + return {"teams": [dict(row) for row in db.execute("SELECT * FROM tenant_teams WHERE org_id=? ORDER BY id", (org_id,))]} + if operation == "member_list": + return {"members": [dict(row) for row in db.execute("SELECT * FROM tenant_memberships WHERE org_id=? ORDER BY principal", (org_id,))]} + return self._member_write(db, v) + + def _named_write(self, db, operation, v, org): + data = v.get("data") + creating, is_team = operation.endswith("create"), operation.startswith("team") + if not isinstance(data, dict) or not data or set(data) - {"name", "status"}: + raise TenantError("invalid_request") + if creating and ("name" not in data or data.get("status", "active") != "active"): + raise TenantError("invalid_request") + if is_team and creating: + self._org(db, v["org_id"], active=True) + if db.execute("SELECT count(*) FROM tenant_teams WHERE org_id=?", (v["org_id"],)).fetchone()[0] >= 100: + raise TenantError("tenant_limit_reached", 409) + row = None if creating else self._team(db, v["org_id"], v["team_id"]) if is_team else org + if not creating: + self._cas(row, v.get("revision")) + name = _text(data.get("name", row["name"] if row else None), 200) + status = data.get("status", row["status"] if row else "active") + if not isinstance(status, str) or status not in {"active", "deactivated"}: + raise TenantError("invalid_request") + identifier = uuid.uuid4().hex if creating else row["id"] + old, new = (row["revision"] if row else 0), (row["revision"] + 1 if row else 1) + if is_team: + db.execute("INSERT INTO tenant_teams VALUES (?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET name=excluded.name,status=excluded.status,revision=excluded.revision", + (identifier, v["org_id"], name, status, new)) + else: + db.execute("INSERT INTO tenant_organisations VALUES (?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET name=excluded.name,status=excluded.status,revision=excluded.revision", + (identifier, name, status, new)) + org_id, team_id = (v["org_id"], identifier) if is_team else (identifier, None) + self._audit(db, v["actor"], operation, org_id, team_id, None, old, new) + return self._team(db, org_id, team_id) if is_team else self._org(db, org_id) + + def _member_write(self, db, v): + org_id, principal, data = v["org_id"], _text(v.get("principal")), v.get("data") + if not isinstance(data, dict) or not data or set(data) - {"role", "team_id", "status", "bind", "binding_revision"}: + raise TenantError("invalid_request") + if not self.principal_exists(principal): + raise TenantError("principal_not_found", 404) + row = self._one(db, "SELECT * FROM tenant_memberships WHERE org_id=? AND principal=?", (org_id, principal)) + self._cas(row, v.get("revision")) + if row is None and db.execute("SELECT count(*) FROM tenant_memberships WHERE org_id=?", (org_id,)).fetchone()[0] >= 1000: + raise TenantError("tenant_limit_reached", 409) + team_id = data.get("team_id", row["team_id"] if row else None) + role, status = data.get("role", row["role"] if row else "member"), data.get("status", row["status"] if row else "active") + if not isinstance(role, str) or role not in {"admin", "billing", "member"} or not isinstance(status, str) or status not in {"active", "deactivated"}: + raise TenantError("invalid_request") + if status == "active": + self._org(db, org_id, active=True) + if team_id is not None: + self._team(db, org_id, team_id, active=status == "active") + if "bind" in data and type(data["bind"]) is not bool: + raise TenantError("invalid_request") + if "binding_revision" in data and not data.get("bind"): + raise TenantError("invalid_request") + old = row["revision"] if row else 0 + db.execute("INSERT INTO tenant_memberships VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT(org_id,principal) DO UPDATE SET team_id=excluded.team_id,role=excluded.role,status=excluded.status,revision=excluded.revision", + (org_id, principal, team_id, role, status, old + 1)) + self._audit(db, v["actor"], "member_set", org_id, team_id, principal, old, old + 1) + if data.get("bind"): + self._binding_write(db, principal, v["actor"], data.get("binding_revision"), {"org_id": org_id, "team_id": team_id}) + return self._one(db, "SELECT * FROM tenant_memberships WHERE org_id=? AND principal=?", (org_id, principal)) + + def _principal_operation(self, db, operation, v): + principal = _text(v.get("principal")) + binding = self._one(db, "SELECT * FROM tenant_bindings WHERE principal=?", (principal,)) + if operation == "binding_set": + if v.get("actor") != principal: + raise TenantError("workspace_forbidden", 403) + return self._binding_write(db, principal, principal, v.get("revision"), v.get("data")) + if operation == "workspaces": + rows = db.execute("SELECT m.* FROM tenant_memberships m JOIN tenant_organisations o ON o.id=m.org_id LEFT JOIN tenant_teams t ON t.id=m.team_id AND t.org_id=m.org_id WHERE m.principal=? AND m.status='active' AND o.status='active' AND (m.team_id IS NULL OR t.status='active') ORDER BY m.org_id", (principal,)) + return {"memberships": [dict(row) for row in rows], "binding": binding or {"principal": principal, "org_id": None, "team_id": None, "revision": 0}} + if binding is None: + return {"principal_id": _legacy(principal).principal_id, "org_id": None, "team_id": None, "grants_revision": 0} + revision = self._workspace(db, principal, binding["org_id"], binding["team_id"]) + return {"principal_id": _legacy(principal).principal_id, "org_id": binding["org_id"], "team_id": binding["team_id"], "grants_revision": revision + binding["revision"]} + + def _binding_write(self, db, principal, actor, revision, data): + if not isinstance(data, dict) or set(data) != {"org_id", "team_id"}: + raise TenantError("invalid_request") + self._workspace(db, principal, data["org_id"], data["team_id"]) + row = self._one(db, "SELECT * FROM tenant_bindings WHERE principal=?", (principal,)) + self._cas(row, revision) + old = row["revision"] if row else 0 + db.execute("INSERT INTO tenant_bindings VALUES (?, ?, ?, ?) ON CONFLICT(principal) DO UPDATE SET org_id=excluded.org_id,team_id=excluded.team_id,revision=excluded.revision", + (principal, data["org_id"], data["team_id"], old + 1)) + self._audit(db, actor, "binding_set", data["org_id"], data["team_id"], principal, old, old + 1) + return self._one(db, "SELECT * FROM tenant_bindings WHERE principal=?", (principal,)) + + +def resolve_principal(principal, *, store=None): + legacy = _legacy(principal) + if not enabled(): + return legacy + value = (store or TenantStore()).request("resolve", principal=principal) + try: + context = TenantContext(**value) + if context.principal_id != legacy.principal_id: + raise ValueError() + return context + except (ValueError, TypeError): + raise TenantError("tenant_storage_unavailable", 503) from None + + +def current_tenant(): + if has_request_context() and hasattr(g, "tenant_context"): + return g.tenant_context + user = getattr(g, "authenticated_user", {}) if has_request_context() else {} + return _legacy(user.get("username") or user.get("id") or "unknown") + + +def tenant_namespace(context=None): + context = context or current_tenant() + if context.org_id is None: + return "" + return "org:" + context.org_id + ("/team:" + context.team_id if context.team_id else "") + + +def tenant_context_hook(): + user = getattr(g, "authenticated_user", {}) + principal = user.get("username") or user.get("id") or "unknown" + try: + # Verified context was resolved just before usage metadata in authentication. + verified = getattr(g, "verified_tenant", None) + g.tenant_context = verified if verified and verified.principal_id == _legacy(principal).principal_id else resolve_principal( + principal, store=current_app.extensions.get("tenant_store")) + except TenantError as error: + return error_response(error) + return None + + +def verify_before_key_usage(principal): + if enabled() and has_request_context(): + g.verified_tenant = resolve_principal(principal, store=current_app.extensions.get("tenant_store")) diff --git a/tests/fixtures/secret_firewall_coverage.json b/tests/fixtures/secret_firewall_coverage.json index 423bb4f5..c46a2612 100644 --- a/tests/fixtures/secret_firewall_coverage.json +++ b/tests/fixtures/secret_firewall_coverage.json @@ -178,6 +178,16 @@ "app.get('/status.json')", "app.route('/v1/health/checks', methods=['POST', 'OPTIONS'])" ], + "routes/tenants.py": [ + "app.get('/admin/organisations//members')", + "app.get('/v1/workspaces')", + "app.patch('/admin/organisations//teams/')", + "app.post('/v1/workspaces/switch')", + "app.put('/admin/organisations//members/')", + "app.route('/admin/organisations', methods=['GET', 'POST'])", + "app.route('/admin/organisations/', methods=['GET', 'PATCH'])", + "app.route('/admin/organisations//teams', methods=['GET', 'POST'])" + ], "routes/unified.py": [ "app.route('/admin/models//disable', methods=['POST'])", "app.route('/v1/chat/completions', methods=['POST', 'OPTIONS'])", @@ -409,6 +419,7 @@ "worker/sse-heartbeat.mjs", "worker/status-page.mjs", "worker/stream-cost-breaker.mjs", + "worker/tenants-d1.mjs", "worker/upstream-cancellation.mjs", "worker/usage-buckets-d1.mjs", "worker/usage-ledger-d1.mjs", diff --git a/tests/test_d1_integration.mjs b/tests/test_d1_integration.mjs index 16642b95..a011f622 100644 --- a/tests/test_d1_integration.mjs +++ b/tests/test_d1_integration.mjs @@ -24,7 +24,8 @@ const bundled = await build({ stdin: { resolveDir: process.cwd(), sourcefile: "p async function privateStore(t, skip = []) { const mf = new Miniflare(convertV4MiniflareOptions({ modules: true, script: bundled.outputFiles[0].text, - compatibilityDate: "2026-07-30", host: "127.0.0.1", port: 0, d1Databases: ["INTELLIGENCE_DB"] })); + compatibilityDate: "2026-07-30", compatibilityFlags: ["nodejs_compat"], host: "127.0.0.1", port: 0, + d1Databases: ["INTELLIGENCE_DB"] })); t.after(() => mf.dispose()); const db = await mf.getD1Database("INTELLIGENCE_DB"); await applyMigrations(db, { skip }); diff --git a/tests/test_tenant_hierarchy.py b/tests/test_tenant_hierarchy.py new file mode 100644 index 00000000..535ee815 --- /dev/null +++ b/tests/test_tenant_hierarchy.py @@ -0,0 +1,353 @@ +"""Registered tenant boundaries and atomic hierarchy storage with synthetic accounts.""" +import importlib +import sqlite3 +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path +from unittest.mock import Mock, patch + +import pytest +from flask import Flask, g, jsonify +from flask_wtf.csrf import CSRFProtect + +from services import tenant_hierarchy as th + + +@pytest.fixture +def store(monkeypatch, tmp_path): + monkeypatch.setenv("ORGANISATIONS_ENABLED", "true") + monkeypatch.setenv("INTELLIGENCE_STORAGE_BACKEND", "") + monkeypatch.setenv("CONTROL_PLANE_DATABASE_URL", "") + path = tmp_path / "auth.sqlite3" + monkeypatch.setenv("AUTH_DB_PATH", str(path)) + with sqlite3.connect(path) as db: + db.executescript((Path(__file__).resolve().parents[1] / "intelligence-migrations/0033_tenant_hierarchy.sql").read_text()) + db.execute("CREATE TABLE old_records (owner TEXT)") + db.execute("INSERT INTO old_records VALUES ('alice')") + return th.TenantStore(principal_exists=lambda principal: principal in {"alice", "bob", "operator"}) + + +def hierarchy(store): + org = store.request("org_create", actor="operator", data={"name": "Example"}) + team = store.request("team_create", actor="operator", org_id=org["id"], data={"name": "Support"}) + return org, team + + +def member(store, org, team=None, **data): + return store.request("member_set", actor="operator", org_id=org["id"], principal="alice", revision=0, + data={"role": "member", "team_id": team["id"] if team else None, "status": "active", **data}) + + +def test_disabled_is_legacy_and_does_not_touch_store(monkeypatch): + for flag in ("", "false", "malformed-secret-value"): + monkeypatch.setenv("ORGANISATIONS_ENABLED", flag) + fake = Mock(side_effect=AssertionError("No storage")) + context = th.resolve_principal("alice", store=Mock(request=fake)) + assert context == th.TenantContext("alice") + assert th.tenant_namespace(context) == "" + fake.assert_not_called() + + +def test_flag_warns_once_without_value(monkeypatch, caplog): + th._warn_once.cache_clear() + monkeypatch.setenv("ORGANISATIONS_ENABLED", "private-bad-value") + assert not th.enabled() and not th.enabled() + assert caplog.text.count("Invalid ORGANISATIONS_ENABLED") == 1 + assert "private-bad-value" not in caplog.text + + +def test_ids_are_generated_and_schema_is_additive(store): + org, team = hierarchy(store) + assert org["id"] != team["id"] and org["revision"] == team["revision"] == 1 + with pytest.raises(th.TenantError) as error: + store.request("org_create", actor="operator", data={"name": "Other", "id": org["id"]}) + assert error.value.status == 400 + with sqlite3.connect(th.storage_path("AUTH_DB_PATH", "auth.sqlite3")) as db: + assert db.execute("SELECT owner FROM old_records").fetchone()[0] == "alice" + assert db.execute("SELECT count(*) FROM tenant_bindings").fetchone()[0] == 0 + + +def test_binding_is_explicit_and_membership_isolated(store): + org, team = hierarchy(store) + member(store, org, team) + assert th.resolve_principal("alice", store=store).org_id is None + assert store.request("workspaces", principal="bob")["memberships"] == [] + binding = store.request("binding_set", actor="alice", principal="alice", revision=0, + data={"org_id": org["id"], "team_id": team["id"]}) + assert binding["revision"] == 1 + context = th.resolve_principal("alice", store=store) + assert th.tenant_namespace(context) == f"org:{org['id']}/team:{team['id']}" + assert store.request("workspaces", principal="alice")["binding"] == binding + with pytest.raises(th.TenantError, match="workspace_forbidden"): + store.request("binding_set", actor="bob", principal="bob", revision=0, + data={"org_id": org["id"], "team_id": team["id"]}) + + +def test_foreign_team_missing_and_revoked_binding_fail_closed(store): + org, team = hierarchy(store) + other, foreign = hierarchy(store) + with pytest.raises(th.TenantError, match="workspace_not_found"): + member(store, org, foreign) + member(store, org, team, bind=True, binding_revision=0) + before = th.resolve_principal("alice", store=store) + store.request("member_set", actor="operator", org_id=org["id"], principal="alice", revision=1, + data={"status": "deactivated"}) + with pytest.raises(th.TenantError, match="workspace_forbidden"): + th.resolve_principal("alice", store=store) + store.request("member_set", actor="operator", org_id=org["id"], principal="alice", revision=2, + data={"status": "active"}) + assert th.resolve_principal("alice", store=store).grants_revision > before.grants_revision + store.request("team_update", actor="operator", org_id=org["id"], team_id=team["id"], revision=1, + data={"status": "deactivated"}) + with pytest.raises(th.TenantError, match="workspace_forbidden"): + th.resolve_principal("alice", store=store) + with sqlite3.connect(th.storage_path("AUTH_DB_PATH", "auth.sqlite3")) as db: + db.execute("UPDATE tenant_bindings SET team_id='missing' WHERE principal='alice'") + with pytest.raises(th.TenantError, match="workspace_not_found"): + th.resolve_principal("alice", store=store) + + +def test_cas_one_winner_and_content_free_audit(store): + org, _ = hierarchy(store) + def write(name): + try: + return store.request("org_update", actor="operator", org_id=org["id"], revision=1, data={"name": name}) + except th.TenantError as error: + return error.status + with ThreadPoolExecutor(max_workers=2) as pool: + results = list(pool.map(write, ["confidential-one", "confidential-two"])) + assert sum(isinstance(result, dict) for result in results) == 1 and 412 in results + with sqlite3.connect(th.storage_path("AUTH_DB_PATH", "auth.sqlite3")) as db: + rows = db.execute("SELECT * FROM tenant_audit").fetchall() + assert len(rows) == 3 + assert "confidential" not in repr(rows) + + +def test_caps_and_unknown_principals(store): + org, team = hierarchy(store) + with sqlite3.connect(th.storage_path("AUTH_DB_PATH", "auth.sqlite3")) as db: + db.executemany("INSERT INTO tenant_teams VALUES (?, ?, ?, 'deactivated', 1)", + [(f"team_{i}", org["id"], "Example") for i in range(99)]) + db.executemany("INSERT INTO tenant_memberships VALUES (?, ?, NULL, 'member', 'deactivated', 1)", + [(org["id"], f"principal_{i}") for i in range(1000)]) + with pytest.raises(th.TenantError, match="tenant_limit_reached"): + store.request("team_create", actor="operator", org_id=org["id"], data={"name": "Extra"}) + with pytest.raises(th.TenantError, match="tenant_limit_reached"): + member(store, org) + with pytest.raises(th.TenantError, match="principal_not_found"): + store.request("member_set", actor="operator", org_id=org["id"], principal="unknown", revision=0, + data={"role": "member"}) + + +@pytest.fixture +def registered(store, monkeypatch): + routes = importlib.import_module("routes.tenants") + registrar = importlib.import_module("services.gateway_extensions") + app = Flask(__name__) + app.config.update(TESTING=True, SECRET_KEY="synthetic-tenant-session", WTF_CSRF_ENABLED=False) + csrf = CSRFProtect(app) + routes.register_tenant_routes(app, csrf=csrf, store=store) + registrar.register_tenants(app) + auth = importlib.import_module("services.auth_service").AuthService + monkeypatch.setattr(auth, "is_authenticated", lambda: True) + monkeypatch.setattr(auth, "get_current_user", lambda: {"username": "operator", "is_admin": True}) + monkeypatch.setattr(auth, "verify_api_key", lambda *_: {"username": "alice", "scopes": ["models", "chat"]}) + for guard in (routes.login_required, routes.api_authenticate_only, routes.require_admin_dashboard_user): + monkeypatch.setitem(guard.__globals__, "AuthService", auth) + monkeypatch.setattr(routes.api_authenticate_only.__globals__["request_accounting"], + "check_key_controls", lambda *_: None) + @app.get("/probe") + def probe(): + g.authenticated_user = {"username": "alice"} + refused = registrar.after_authentication() + return refused if refused is not None else jsonify(namespace=th.tenant_namespace()) + return app, app.test_client(), auth, registrar + + +def test_registered_disabled_routes_precede_auth_csrf_and_json(registered, monkeypatch): + app, client, auth, _ = registered + app.config["WTF_CSRF_ENABLED"] = True + monkeypatch.setattr(auth, "is_authenticated", Mock(side_effect=AssertionError("No authentication"))) + monkeypatch.setenv("ORGANISATIONS_ENABLED", "") + for path in ("/admin/organisations", "/admin/organisations/absent", "/admin/organisations/absent/teams", + "/admin/organisations/absent/teams/absent", "/admin/organisations/absent/members", + "/admin/organisations/absent/members/alice", "/v1/workspaces", "/v1/workspaces/switch"): + for method in ("GET", "POST", "PATCH", "PUT"): + result = client.open(path, method=method, data="not json") + assert result.status_code == 404 and result.is_json + assert result.headers["Cache-Control"] == "no-store" + + +def test_registered_admin_cas_guards_and_csrf(registered, monkeypatch): + app, client, auth, _ = registered + org = client.post("/admin/organisations", json={"name": "Example"}).json + assert org["revision"] == 1 + path = f"/admin/organisations/{org['id']}" + assert client.patch(path, json={"name": "Next"}).status_code == 428 + assert client.patch(path, json={"name": "Next"}, headers={"If-Match": '"0"'}).status_code == 412 + response = client.patch(path, json={"name": "Next"}, headers={"If-Match": '"1"'}) + assert response.json["revision"] == 2 and response.headers["ETag"] == '"2"' + monkeypatch.setattr(auth, "get_current_user", lambda: {"username": "operator", "is_admin": False}) + assert client.get(path).status_code == 403 + app.config["WTF_CSRF_ENABLED"] = True + assert client.post("/admin/organisations", json={"name": "Example"}).status_code == 400 + + +def test_workspace_switch_and_spoofing_registered(registered, store): + _, client, _, registrar = registered + org, team = hierarchy(store) + member(store, org, team) + headers = {"Authorization": "Bearer synthetic", "X-Org": org["id"], "X-Team": team["id"], + "X-MultiLLM-Workspace": org["id"]} + assert client.get("/probe", headers=headers).json == {"namespace": ""} + assert client.get("/v1/workspaces", headers=headers).json["memberships"][0]["org_id"] == org["id"] + response = client.post("/v1/workspaces/switch", headers={**headers, "If-Match": '"0"'}, + json={"org_id": org["id"], "team_id": team["id"]}) + assert response.status_code == 200 + assert client.get("/probe", headers={"X-Org": "foreign"}).json["namespace"] == th.tenant_namespace(th.resolve_principal("alice", store=store)) + assert registrar.AUTHENTICATED_HOOK_ORDER[0] == "tenant_context_hook" + + +def test_missing_schema_and_private_errors_are_json_503(registered, monkeypatch, tmp_path): + _, client, _, _ = registered + monkeypatch.setenv("AUTH_DB_PATH", str(tmp_path / "missing.sqlite3")) + response = client.get("/probe") + assert response.status_code == 503 and response.json["error"]["code"] == "tenant_storage_unavailable" + monkeypatch.setenv("INTELLIGENCE_STORAGE_BACKEND", "d1") + fake = Mock(return_value={"version": 1, "error": {"code": "workspace_forbidden"}, "status": 403}) + remote = th.TenantStore(call=fake) + with pytest.raises(th.TenantError, match="workspace_forbidden"): + th.resolve_principal("alice", store=remote) + assert fake.call_count == 1 + + +def test_registered_tenant_failure_stops_later_hooks_and_metadata(registered, store, monkeypatch): + app, client, auth, registrar = registered + org, team = hierarchy(store) + member(store, org, team, bind=True, binding_revision=0) + store.request("org_update", actor="operator", org_id=org["id"], revision=1, data={"status": "deactivated"}) + later = Mock(side_effect=AssertionError("No downstream storage or provider")) + later.__name__ = "request_policy_hook" + registrar.register_authenticated_hook(app, later) + response = client.get("/probe") + assert response.status_code == 403 and response.json["error"]["code"] == "workspace_forbidden" + later.assert_not_called() + monkeypatch.setattr(auth, "_users", {"alice": {"last_used_at": None}}) + with app.test_request_context("/probe"): + with pytest.raises(th.TenantError, match="workspace_forbidden"): + auth._update_key_usage("alice") + assert auth._users["alice"]["last_used_at"] is None + + +def test_binding_cas_and_admin_binding_are_atomic(store): + org, team = hierarchy(store) + member(store, org, team, bind=True, binding_revision=0) + with pytest.raises(th.TenantError, match="revision_conflict"): + store.request("member_set", actor="operator", org_id=org["id"], principal="alice", revision=1, + data={"role": "billing", "bind": True, "binding_revision": 0}) + assert store.request("member_list", org_id=org["id"])["members"][0]["role"] == "member" + with pytest.raises(th.TenantError, match="revision_conflict"): + store.request("binding_set", actor="alice", principal="alice", revision=0, + data={"org_id": org["id"], "team_id": team["id"]}) + + +def test_create_app_mounts_routes_with_no_live_side_effects(store, monkeypatch): + for name, value in {"FLASK_SECRET_KEY": "synthetic-tenant-session", "JWT_SECRET": "synthetic-tenant-jwt", + "ADMIN_API_KEY": "synthetic-tenant-admin", "CONFIG_REVISION_SYNC_ENABLED": "false"}.items(): + monkeypatch.setenv(name, value) + with patch("config.load_runtime_env"), patch("env_loader.load_runtime_env"), \ + patch("requests.sessions.Session.send", side_effect=AssertionError("No network")): + module = importlib.import_module("app") + with patch.object(module, "load_runtime_env"), patch.object(module.AuthService, "initialize"), patch.object(module.usage_ledger, "start"): + app = module.create_app() + app.config.update(TESTING=True, WTF_CSRF_ENABLED=False) + app.extensions["tenant_store"] = store + with patch.object(module.AuthService, "is_authenticated", return_value=True), \ + patch.object(module.AuthService, "get_current_user", return_value={"username": "operator", "is_admin": True}): + response = app.test_client().post("/admin/organisations", json={"name": "Registered"}) + assert response.status_code == 201 + assert app.extensions["gateway_after_authentication"][0].__name__ == "tenant_context_hook" + + +@pytest.mark.parametrize("data", [{"role": None}, {"role": ["admin"]}, {"status": None}, {"status": ["active"]}, {"bind": "true"}]) +def test_membership_invalid_types_are_rejected_without_mutation(store, data): + org, _ = hierarchy(store) + with pytest.raises(th.TenantError) as error: + member(store, org, **data) + assert error.value.status == 400 + assert store.request("member_list", org_id=org["id"])["members"] == [] + + +def test_registered_full_admin_lifecycle_and_workspace_scope(registered, store, monkeypatch): + _, client, auth, _ = registered + org = client.post("/admin/organisations", json={"name": "Example"}).json + base = f"/admin/organisations/{org['id']}" + team = client.post(base + "/teams", json={"name": "Support"}).json + assert client.get(base + "/teams").json["teams"] == [team] + response = client.put(base + "/members/alice", headers={"If-Match": '\"0\"'}, + json={"role": "billing", "team_id": team["id"], "status": "active"}) + assert response.status_code == 200 + assert client.get(base + "/members").json["members"] == [response.json] + assert client.patch(base + f"/teams/{team['id']}", headers={"If-Match": '\"1\"'}, json={"name": "Renamed"}).status_code == 200 + assert client.post("/v1/workspaces/switch", headers={"Authorization": "Bearer synthetic"}, + json={"org_id": org["id"], "team_id": team["id"]}).status_code == 428 + monkeypatch.setattr(auth, "verify_api_key", lambda *_: {"username": "bob", "scopes": ["chat"]}) + assert client.get("/v1/workspaces", headers={"Authorization": "Bearer synthetic"}).status_code == 403 + + +def test_account_tenant_check_follows_matching_hash_and_cached_verification(monkeypatch): + module = importlib.import_module("services.auth_service") + auth = module.AuthService + key = "shared01-synthetic-matching" + users = {"alice": {"api_key_hash": "other", "revoked_at": None}, + "bob": {"api_key_hash": "matching", "revoked_at": None}} + checks = [] + monkeypatch.setenv("ADMIN_API_KEY", "") + monkeypatch.setattr(auth, "_users", users) + monkeypatch.setattr(auth, "_verified_keys", {}) + monkeypatch.setattr(auth, "_load_users_by_api_key_prefix", lambda _: list(users.items())) + monkeypatch.setattr(auth, "_public_user", lambda username, _: {"username": username}) + monkeypatch.setattr(module.key_controls, "verify_revisioned_key", lambda *_: module.key_controls.LEGACY_AUTH) + monkeypatch.setattr(module.user_store, "using_d1", lambda: True) + touch = Mock() + monkeypatch.setattr(module.user_store, "touch_user", touch) + + def check_hash(stored, provided): + checks.append(("hash", stored, provided)) + return stored == "matching" and provided == key + + def verify_tenant(principal): + checks.append(("tenant", principal)) + assert principal == "bob" + + monkeypatch.setattr(module, "check_password_hash", check_hash) + monkeypatch.setattr(th, "verify_before_key_usage", verify_tenant) + assert auth.verify_api_key(key) == {"username": "bob"} + assert checks == [("hash", "other", key), ("hash", "matching", key), ("tenant", "bob")] + checks.clear() + assert auth.verify_api_key(key) == {"username": "bob"} + assert checks == [("tenant", "bob")] + checks.clear() + assert auth.verify_api_key("shared01-wrong-key") is None + assert all(check[0] == "hash" for check in checks) + assert touch.call_count == 1 + assert touch.call_args.args[0] == "bob" + + +def test_bootstrap_tenant_check_follows_environment_key_verification(monkeypatch): + module = importlib.import_module("services.auth_service") + auth = module.AuthService + monkeypatch.setenv("ADMIN_USERNAME", "operator") + monkeypatch.setenv("ADMIN_API_KEY", "synthetic-bootstrap") + monkeypatch.setattr(auth, "_verified_keys", {}) + monkeypatch.setattr(auth, "_load_users_by_api_key_prefix", lambda _: []) + monkeypatch.setattr(module.key_controls, "verify_revisioned_key", lambda *_: module.key_controls.LEGACY_AUTH) + tenant = Mock() + verified = Mock(return_value={"username": "operator"}) + monkeypatch.setattr(th, "verify_before_key_usage", tenant) + monkeypatch.setattr(auth, "_verify_bootstrap_admin", verified) + assert auth.verify_api_key("synthetic-bootstrap-wrong") is None + tenant.assert_not_called() + verified.assert_not_called() + assert auth.verify_api_key("synthetic-bootstrap") == {"username": "operator"} + tenant.assert_called_once_with("operator") + verified.assert_called_once_with("operator", "synthetic-bootstrap", None) diff --git a/tests/test_tenant_hierarchy_worker.mjs b/tests/test_tenant_hierarchy_worker.mjs new file mode 100644 index 00000000..a3456599 --- /dev/null +++ b/tests/test_tenant_hierarchy_worker.mjs @@ -0,0 +1,341 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { DatabaseSync } from "node:sqlite"; +import { readFileSync } from "node:fs"; +import { scryptSync } from "node:crypto"; +import { handleKnowledgeEdgeRequest } from "../worker/knowledge-edge.mjs"; +import { handleRealtimeRequest } from "../worker/realtime.mjs"; +import { activeUsersByPrefix, USER_FIELDS } from "../worker/control-users-d1.mjs"; +import { handleTenantRequest, organisationsEnabled, resolveAccountTenant, tenantNamespace } from "../worker/tenants-d1.mjs"; +import { handleManagedStateRequest } from "../worker/managed-state-dispatch.mjs"; +import { nativeGenerationFetch } from "../worker/gateway-extensions.mjs"; + +test("disabled tenant domain and resolver touch no storage", async () => { + const db = { prepare() { throw Error("No storage"); } }; + for (const flag of ["", "false", "invalid-private-value"]) { + const env = { ORGANISATIONS_ENABLED: flag, INTELLIGENCE_DB: db }; + assert.equal(organisationsEnabled(env, () => {}), false); + assert.equal(tenantNamespace(await resolveAccountTenant(db, "alice", env)), ""); + const response = await handleManagedStateRequest(new Request("http://intelligence.internal/v1/managed-state/tenants", { method: "POST", body: "broken" }), env); + assert.equal(response.status, 404); + assert.equal(response.headers.get("cache-control"), "no-store"); + } +}); + +test("tenant domain is private, bounded and fails closed on missing schema", async () => { + const env = { ORGANISATIONS_ENABLED: "true", INTELLIGENCE_DB: { prepare() { throw Error("no such table: sensitive"); } } }; + const send = (url, body) => handleTenantRequest(new Request(url, { method: "POST", headers: { "content-type": "application/json" }, body }), env); + assert.equal((await send("https://public.example/v1/managed-state/tenants", "{}")).status, 404); + assert.equal((await send("http://intelligence.internal/v1/managed-state/tenants", "x".repeat(9000))).status, 400); + const response = await send("http://intelligence.internal/v1/managed-state/tenants", JSON.stringify({ version: 1, operation: "resolve", principal: "alice" })); + assert.equal(response.status, 503); + assert.equal((await response.json()).error.code, "tenant_storage_unavailable"); +}); + +test("native generation rejects unavailable tenant authority before provider or hooks", async () => { + let calls = 0; + const result = await nativeGenerationFetch(new Request("https://gateway.example/v1/chat/completions", { method: "POST", body: JSON.stringify({ org_id: "spoofed" }), headers: { "X-Org": "spoofed" } }), + { ORGANISATIONS_ENABLED: "true" }, {}, { authenticated: true, principal: { id: "alice" }, provider: "openai" }, () => { calls++; return new Response("provider"); }); + assert.equal(result.status, 503); + assert.equal(calls, 0); +}); + +function database() { + const sqlite = new DatabaseSync(":memory:"); + sqlite.exec(readFileSync(new URL("../intelligence-migrations/0033_tenant_hierarchy.sql", import.meta.url), "utf8")); + sqlite.exec("CREATE TABLE control_users (username TEXT PRIMARY KEY)"); + sqlite.exec("INSERT INTO control_users VALUES ('alice'),('bob'),('operator')"); + const db = { + prepare(sql) { + let values = []; + return { bind(...params) { values = params; return this; }, + async all() { return { results: sqlite.prepare(sql).all(...values) }; }, + async first() { return sqlite.prepare(sql).get(...values) ?? null; }, + async run() { return { meta: { changes: Number(sqlite.prepare(sql).run(...values).changes) } }; } }; + }, + async batch(statements) { + sqlite.exec("BEGIN IMMEDIATE"); + try { const results = []; for (const statement of statements) results.push(await statement.run()); sqlite.exec("COMMIT"); return results; } + catch (error) { sqlite.exec("ROLLBACK"); throw error; } + }, + }; + const env = { ORGANISATIONS_ENABLED: "true", INTELLIGENCE_DB: db }; + const call = async (operation, values = {}) => { + const response = await handleManagedStateRequest(new Request("http://intelligence.internal/v1/managed-state/tenants", { + method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ version: 1, operation, ...values }) }), env); + return { status: response.status, body: await response.json() }; + }; + return { sqlite, db, env, call, close() { sqlite.close(); } }; +} +async function hierarchy(f) { + const org = (await f.call("org_create", { actor: "operator", data: { name: "Example" } })).body.result; + const team = (await f.call("team_create", { actor: "operator", org_id: org.id, data: { name: "Support" } })).body.result; + return { org, team }; +} +async function member(f, org, team, data = {}) { + return f.call("member_set", { actor: "operator", org_id: org.id, principal: "alice", revision: 0, + data: { role: "member", team_id: team?.id ?? null, status: "active", ...data } }); +} + +test("D1 hierarchy has explicit bindings, matching namespaces and content-free audits", async () => { + const f = database(); + try { + const { org, team } = await hierarchy(f); + assert.equal((await member(f, org, team)).status, 200); + assert.equal(tenantNamespace(await resolveAccountTenant(f.db, "alice", f.env)), ""); + assert.deepEqual((await f.call("workspaces", { principal: "bob" })).body.result.memberships, []); + const selected = await f.call("binding_set", { actor: "alice", principal: "alice", revision: 0, data: { org_id: org.id, team_id: team.id } }); + assert.equal(selected.status, 200); + const context = await resolveAccountTenant(f.db, "alice", f.env); + assert.equal(tenantNamespace(context), `org:${org.id}/team:${team.id}`); + assert.equal(context.grants_revision, 4); + const copied = await f.call("binding_set", { actor: "alice", principal: "bob", revision: 0, data: { org_id: org.id, team_id: team.id } }); + assert.equal(copied.status, 403); + const audit = f.sqlite.prepare("SELECT * FROM tenant_audit").all(); + assert.equal(audit.length, 4); + assert.equal(JSON.stringify(audit).includes("Support"), false); + assert.equal(f.sqlite.prepare("SELECT count(*) AS n FROM tenant_bindings").get().n, 1); + } finally { f.close(); } +}); + +test("D1 atomic membership plus binding and stale updates retain both old rows", async () => { + const f = database(); + try { + const { org, team } = await hierarchy(f); + assert.equal((await member(f, org, team, { bind: true, binding_revision: 0 })).status, 200); + const result = await f.call("member_set", { actor: "operator", org_id: org.id, principal: "alice", revision: 1, + data: { role: "billing", bind: true, binding_revision: 0 } }); + assert.equal(result.status, 412); + assert.equal(f.sqlite.prepare("SELECT role FROM tenant_memberships").get().role, "member"); + assert.equal(f.sqlite.prepare("SELECT count(*) AS n FROM tenant_audit").get().n, 4); + const missing = await f.call("org_update", { actor: "operator", org_id: org.id, data: { name: "Other" } }); + assert.equal(missing.status, 428); + assert.equal((await f.call("org_update", { actor: "operator", org_id: org.id, revision: 0, data: { name: "Other" } })).status, 412); + } finally { f.close(); } +}); + +test("D1 revision changes are fresh and foreign, revoked and missing workspaces are refused", async () => { + const f = database(); + try { + const { org, team } = await hierarchy(f), foreign = await hierarchy(f); + assert.equal((await member(f, org, foreign.team)).status, 404); + await member(f, org, team, { bind: true, binding_revision: 0 }); + const before = await resolveAccountTenant(f.db, "alice", f.env); + await f.call("member_set", { actor: "operator", org_id: org.id, principal: "alice", revision: 1, data: { role: "billing" } }); + assert.ok((await resolveAccountTenant(f.db, "alice", f.env)).grants_revision > before.grants_revision); + await f.call("team_update", { actor: "operator", org_id: org.id, team_id: team.id, revision: 1, data: { status: "deactivated" } }); + await assert.rejects(resolveAccountTenant(f.db, "alice", f.env), error => error.code === "workspace_forbidden" && error.status === 403); + f.sqlite.prepare("UPDATE tenant_bindings SET team_id='absent'").run(); + await assert.rejects(resolveAccountTenant(f.db, "alice", f.env), error => error.code === "workspace_not_found" && error.status === 404); + assert.equal(f.sqlite.prepare("SELECT count(*) AS n FROM tenant_teams").get().n, 2); + } finally { f.close(); } +}); + +test("D1 caps include deactivated rows and remain inside conditional writes", async () => { + const f = database(); + try { + const { org } = await hierarchy(f); + for (let i = 0; i < 99; i++) f.sqlite.prepare("INSERT INTO tenant_teams VALUES (?,?,?,'deactivated',1)").run(`team_${i}`, org.id, "Example"); + assert.equal((await f.call("team_create", { actor: "operator", org_id: org.id, data: { name: "Extra" } })).body.error.code, "tenant_limit_reached"); + for (let i = 0; i < 1000; i++) f.sqlite.prepare("INSERT INTO tenant_memberships VALUES (?,?,NULL,'member','deactivated',1)").run(org.id, `user_${i}`); + const capped = await member(f, org, null); + assert.equal(capped.status, 409); + assert.equal(capped.body.error.code, "tenant_limit_reached"); + } finally { f.close(); } +}); + +test("D1 CAS blocks a lost update and audit failure rolls the mutation back", async () => { + const f = database(); + try { + const { org } = await hierarchy(f); + // Interleave a competing write after the read but before the guarded atomic batch. + const original = f.db.batch; + f.db.batch = async statements => { + f.sqlite.prepare("UPDATE tenant_organisations SET revision=revision+1 WHERE id=?").run(org.id); + return original(statements); + }; + const conflict = await f.call("org_update", { actor: "operator", org_id: org.id, revision: 1, data: { name: "confidential-change" } }); + assert.equal(conflict.status, 412); + assert.equal(f.sqlite.prepare("SELECT name FROM tenant_organisations").get().name, "Example"); + f.db.batch = original; + f.sqlite.exec("CREATE TRIGGER reject_audit BEFORE INSERT ON tenant_audit BEGIN SELECT RAISE(ABORT,'audit unavailable'); END"); + const failed = await f.call("org_update", { actor: "operator", org_id: org.id, revision: 2, data: { name: "confidential-change" } }); + assert.equal(failed.status, 503); + assert.equal(f.sqlite.prepare("SELECT revision FROM tenant_organisations").get().revision, 2); + assert.equal(f.sqlite.prepare("SELECT name FROM tenant_organisations").get().name, "Example"); + } finally { f.close(); } +}); + +test("native requests ignore headers and body scope and resolve before dispatch", async () => { + const f = database(); + try { + const { org, team } = await hierarchy(f); + await member(f, org, team, { bind: true, binding_revision: 0 }); + const contexts = []; + const request = () => new Request("https://gateway.example/v1/chat/completions", { method: "POST", headers: { "X-Org": "foreign", "X-Team": "foreign", "X-MultiLLM-Workspace": "foreign" }, + body: JSON.stringify({ model: "test", org_id: "foreign", team_id: "foreign" }) }); + const authority = { authenticated: true, principal: { id: "alice" }, provider: "openai" }; + const result = await nativeGenerationFetch(request(), f.env, {}, authority, async (req, env, resolved) => { + contexts.push(resolved.tenantContext); return new Response("unchanged"); + }); + assert.equal(await result.text(), "unchanged"); + assert.equal(contexts[0].org_id, org.id); + assert.equal(contexts[0].team_id, team.id); + await f.call("org_update", { actor: "operator", org_id: org.id, revision: 1, data: { status: "deactivated" } }); + const rejected = await nativeGenerationFetch(request(), f.env, {}, authority, () => { throw Error("No provider"); }); + assert.equal(rejected.status, 403); + assert.equal((await rejected.json()).error.code, "workspace_forbidden"); + } finally { f.close(); } +}); + +test("prefix lookup leaves tenant resolution to the verified principal", async () => { + const row = { username: "alice" }; + const db = { prepare(sql) { + assert.ok(sql.includes("api_key_prefix=?")); + return { bind() { return this; }, async all() { return { results: [row] }; } }; + } }; + assert.deepEqual(await activeUsersByPrefix(db, "fixture", { ORGANISATIONS_ENABLED: "true" }), [row]); + assert.equal(Object.hasOwn(row, "tenant_context"), false); +}); + +const accountKey = "shared01-synthetic-matching"; +const otherKey = "shared01-synthetic-other"; +const integrationKey = "mllm_intelligence_" + "s".repeat(32); +const hash = key => `scrypt:32768:8:1$syntheticsalt$${scryptSync(key, "syntheticsalt", 64, + { N: 32768, r: 8, p: 1, maxmem: 64 * 1024 * 1024 }).toString("hex")}`; +const accountRow = (username, key) => ({ ...Object.fromEntries(USER_FIELDS.map(name => [name, null])), + username, api_key_hash: hash(key), api_key_prefix: `mllm_${key.slice(0, 8)}`, + scopes: "knowledge:read,chat,audio", is_admin: 0, created_at: "2026-10-09T00:00:00Z" }); +const rows = [accountRow("alice", otherKey), accountRow("bob", accountKey)]; +const integrationRow = { id: "integration:synthetic", scopes: JSON.stringify(["knowledge:read", "chat", "audio"]), + version: 1, created_at: "2026-10-09T00:00:00Z", revoked_at: null, + key_prefix: integrationKey.slice(0, "mllm_intelligence_".length + 16), key_hash: hash(integrationKey) }; +function authFixture() { + const f = database(), calls = { tenants: [], writes: 0, dispatches: 0 }; + const db = { prepare(sql) { + if (sql.includes("tenant_")) { + calls.tenants.push(sql); + const stmt = f.db.prepare(sql), bind = stmt.bind; + stmt.bind = (...values) => { if (sql.includes("WHERE b.principal=?")) calls.principal = values[0]; return bind.apply(stmt, values); }; + return stmt; + } + return { bind() { return this; }, async all() { return { results: sql.includes("control_users") ? rows : [] }; }, + async first() { return sql.includes("intelligence_credentials") ? integrationRow : null; }, + async run() { calls.writes++; return { meta: { changes: 1 } }; } }; + } }; + const env = { ...f.env, INTELLIGENCE_DB: db, AUTH_STORAGE_BACKEND: "d1", ADMIN_API_KEY: "synthetic-bootstrap", + ADMIN_USERNAME: "operator", JWT_SECRET: "synthetic-signing", REALTIME_ENABLED: "true", + REALTIME_PROVIDERS_JSON: '{"openai:realtime-test":{"url":"wss://approved.example/realtime"}}', + KNOWLEDGE_SERVICE: { async fetch() { calls.dispatches++; return Response.json({ version: 1, result: { status: "ok" } }); } } }; + return { ...f, env, calls }; +} +const knowledgeRequest = (key, mcp = false) => new Request(mcp ? "https://gateway.example/mcp" : "https://gateway.example/v1/knowledge/artifacts/synthetic", + { method: mcp ? "POST" : "GET", headers: { authorization: `Bearer ${key}`, "content-type": "application/json", "X-Org": "spoofed" }, + ...(mcp ? { body: JSON.stringify({ jsonrpc: "2.0", id: 7, method: "ping" }) } : {}) }); +const realtimeRequest = key => new Request("https://gateway.example/v1/realtime/client_secrets", { + method: "POST", headers: { authorization: `Bearer ${key}`, "content-type": "application/json", "X-Team": "spoofed" }, + body: JSON.stringify({ model: "openai:realtime-test" }) }); +const authRoutes = [ + ["knowledge", (f, key) => handleKnowledgeEdgeRequest(knowledgeRequest(key), f.env)], + ["realtime", (f, key) => handleRealtimeRequest(realtimeRequest(key), f.env)], +]; +for (const [route, send] of authRoutes) { + test(`${route} resolves only the matching account despite a forbidden prefix candidate`, async () => { + const f = authFixture(); + try { + const { org } = await hierarchy(f); + await member(f, org, null, { bind: true, binding_revision: 0 }); + await f.call("org_update", { actor: "operator", org_id: org.id, revision: 1, data: { status: "deactivated" } }); + assert.equal((await send(f, accountKey)).status, 200); + assert.equal(f.calls.principal, "bob"); + } finally { f.close(); } + }); + for (const kind of ["account", "bootstrap", "integration"]) { + test(`${route} classifies workspace failures for verified ${kind} credentials`, async () => { + const f = authFixture(), key = kind === "account" ? otherKey : kind === "bootstrap" ? f.env.ADMIN_API_KEY : integrationKey; + const principal = kind === "account" ? "alice" : kind === "bootstrap" ? "operator" : integrationRow.id; + try { + const { org } = await hierarchy(f); + await f.call("member_set", { actor: "operator", org_id: org.id, principal: "alice", revision: 0, + data: { role: "member", bind: true, binding_revision: 0 } }); + f.sqlite.prepare("UPDATE tenant_memberships SET principal=?").run(principal); + f.sqlite.prepare("UPDATE tenant_bindings SET principal=?").run(principal); + await f.call("org_update", { actor: "operator", org_id: org.id, revision: 1, data: { status: "deactivated" } }); + for (const [status, code, mutate] of [ + [403, "workspace_forbidden", () => {}], + [404, "workspace_not_found", () => f.sqlite.prepare("UPDATE tenant_bindings SET org_id='absent'").run()], + [503, "tenant_storage_unavailable", () => f.sqlite.exec("DROP TABLE tenant_bindings")], + ]) { + mutate(); const response = await send(f, key); + assert.equal(response.status, status); assert.equal((await response.json()).error.code, code); + assert.equal(response.headers.get("cache-control"), "no-store"); + assert.equal(f.calls.writes, 0); assert.equal(f.calls.dispatches, 0); + } + } finally { f.close(); } + }); + } + test(`${route} with organisations off never queries tenants or changes responses`, async () => { + const f = authFixture(); + try { + for (const flag of [undefined, "", "false", "malformed"]) { + f.env.ORGANISATIONS_ENABLED = flag; + for (const key of [accountKey, f.env.ADMIN_API_KEY, integrationKey]) assert.equal((await send(f, key)).status, 200); + } + assert.deepEqual(f.calls.tenants, []); + } finally { f.close(); } + }); + test(`${route} invalid keys never resolve a tenant`, async () => { + const f = authFixture(); + try { + assert.equal((await send(f, "shared01-wrong-key")).status, 401); + assert.deepEqual(f.calls.tenants, []); assert.equal(f.calls.writes, 0); + } finally { f.close(); } + }); +} + +test("Knowledge MCP preserves its JSON-RPC error envelope for forbidden workspaces", async () => { + const f = authFixture(); + try { + const { org } = await hierarchy(f); + await member(f, org, null, { bind: true, binding_revision: 0 }); + await f.call("org_update", { actor: "operator", org_id: org.id, revision: 1, data: { status: "deactivated" } }); + const response = await handleKnowledgeEdgeRequest(knowledgeRequest(otherKey, true), f.env); + assert.equal(response.status, 403); + const body = await response.json(); + assert.equal(body.jsonrpc, "2.0"); assert.equal(body.error.code, -32000); + assert.equal(body.error.message, "workspace_forbidden"); assert.equal(f.calls.dispatches, 0); + } finally { f.close(); } +}); + +test("D1 rechecks team cap inside the write when another team wins the last slot", async () => { + const f = database(); + try { + const { org } = await hierarchy(f); + for (let i = 0; i < 98; i++) f.sqlite.prepare("INSERT INTO tenant_teams VALUES (?,?,?,'active',1)").run(`team_${i}`, org.id, "Example"); + const original = f.db.batch; + f.db.batch = async statements => { + f.sqlite.prepare("INSERT INTO tenant_teams VALUES (?,?,?,'active',1)").run("last_slot", org.id, "Other"); + return original(statements); + }; + const response = await f.call("team_create", { actor: "operator", org_id: org.id, data: { name: "Extra" } }); + assert.equal(response.status, 409); + assert.equal(response.body.error.code, "tenant_limit_reached"); + assert.equal(f.sqlite.prepare("SELECT count(*) AS n FROM tenant_teams").get().n, 100); + assert.equal(f.sqlite.prepare("SELECT count(*) AS n FROM tenant_audit").get().n, 2); + } finally { f.close(); } +}); + +test("D1 malformed fields and unlisted operations never produce state changes", async () => { + const f = database(); + try { + const { org } = await hierarchy(f); + for (const data of [{ name: null }, { name: "Example", id: "caller-chosen" }, { name: "Example", status: null }, { name: "Example", parent_id: org.id }]) { + assert.equal((await f.call("org_create", { actor: "operator", data })).status, 400); + } + for (const data of [{ role: null }, { role: ["admin"] }, { status: null }, { status: ["active"] }]) { + assert.equal((await member(f, org, null, data)).status, 400); + } + assert.equal((await f.call("unknown", { statement: "DROP TABLE tenant_organisations" })).status, 400); + assert.equal(f.sqlite.prepare("SELECT count(*) AS n FROM tenant_memberships").get().n, 0); + } finally { f.close(); } +}); diff --git a/worker/container-env.mjs b/worker/container-env.mjs index e1394018..157d9ddc 100644 --- a/worker/container-env.mjs +++ b/worker/container-env.mjs @@ -40,6 +40,7 @@ const DIRECT_ENV_KEYS = [ "CONNECTION_PROFILES_DB_PATH", "CONFIG_REVISION_SYNC_ENABLED", "CONFIG_SYNC_TTL_SECONDS", + "ORGANISATIONS_ENABLED", "CONTEXT_CANARY_MODE", "CONTEXT_CANARY_POLICY_JSON", "ENTERPRISE_PREVIEW_ENABLED", diff --git a/worker/d1-schema.mjs b/worker/d1-schema.mjs index fb20de73..79880fbf 100644 --- a/worker/d1-schema.mjs +++ b/worker/d1-schema.mjs @@ -10,7 +10,7 @@ export const REQUIRED_D1_TABLES = Object.freeze(["intelligence_policy", "intelli "usage_events", "usage_daily", "usage_batches", "media_jobs", "media_job_items", "shadow_eval_samples", "shadow_eval_results", "shadow_eval_config", "shadow_eval_policy_backups", "prompt_templates", - "config_snapshot_revisions", "config_snapshots", "config_snapshot_applications", "control_revisions", "tool_grants", "generation_cache", "usage_reservation_budgets", "usage_reservations", "usage_reservation_transitions", "session_tiers", "gateway_alert_rules", "gateway_alert_events", "managed_idempotency", "context_pages", "canary_traffic", "semantic_generation_cache", "gateway_batch_files", "gateway_batches", "gateway_batch_items", "hosted_responses", "realtime_tickets", "realtime_sessions", "learned_cooldown", "usage_receipt_keys", "usage_receipt_heads", "usage_receipts"]); + "config_snapshot_revisions", "config_snapshots", "config_snapshot_applications", "control_revisions", "tool_grants", "generation_cache", "usage_reservation_budgets", "usage_reservations", "usage_reservation_transitions", "session_tiers", "gateway_alert_rules", "gateway_alert_events", "managed_idempotency", "context_pages", "canary_traffic", "semantic_generation_cache", "gateway_batch_files", "gateway_batches", "gateway_batch_items", "hosted_responses", "realtime_tickets", "realtime_sessions", "learned_cooldown", "usage_receipt_keys", "usage_receipt_heads", "usage_receipts", "tenant_organisations", "tenant_teams", "tenant_memberships", "tenant_bindings", "tenant_audit"]); /** * Readiness for the D1 schema. A deploy that skipped `wrangler d1 migrations apply` reports diff --git a/worker/gateway-extensions.mjs b/worker/gateway-extensions.mjs index 3eed5a3f..59a47807 100644 --- a/worker/gateway-extensions.mjs +++ b/worker/gateway-extensions.mjs @@ -1,4 +1,5 @@ /** Static registration boundary for native dispatch; later integrations supply named hooks. */ +import { organisationsEnabled, resolveAccountTenant, tenantNamespace, TenantError } from "./tenants-d1.mjs"; import { generationCacheSettings, prepareNativeCache, cacheServedEvent } from "./exact-generation-cache.mjs"; import { createGatewayLifecycle } from "./gateway-lifecycle.mjs"; import { observeNativeResponse, appendNativeMetrics, PARSER_LIMIT } from "./request-telemetry.mjs"; @@ -72,6 +73,7 @@ async function nativeContext(request, env, authority, metrics) { .map(([name, state]) => [name, state.revision])) : {}; return Object.freeze({ provider: authority.provider, principal, model: typeof model === "string" && /^[A-Za-z0-9][A-Za-z0-9._:/+@-]{0,223}$/.test(model) ? model : null, + ...(organisationsEnabled(env) ? { tenantContext: authority.tenantContext, tenantNamespace: tenantNamespace(authority.tenantContext) } : {}), modelGroup: admissionModelGroup(authority.provider, model), requestId: crypto.randomUUID(), endpoint: authority.route, startedAt, retentionPolicy, cacheRevisions }); } @@ -322,6 +324,16 @@ async function runNativeLifecycle(request, env, ctx, authority, fetcher, registr } export async function nativeGenerationFetch(request, env, ctx, authority, fetcher, collaborators = []) { + if (organisationsEnabled(env) && authority.authenticated === true) { + try { + const tenantContext = await resolveAccountTenant(env.INTELLIGENCE_DB, authority.principal?.id ?? authority.keyId, env); + authority = { ...authority, tenantContext, tenantNamespace: tenantNamespace(tenantContext) }; + } catch (error) { + authority.deadlineHook?.deadline?.stop(); + if (error instanceof TenantError) return error.response(); + throw error; + } + } const path = authority.route ?? new URL(request.url).pathname; if (!generationPath(request, path)) return fetcher(request, env, authority); // The route has already authenticated the bootstrap key. Only public route metadata is trusted. diff --git a/worker/knowledge-edge.mjs b/worker/knowledge-edge.mjs index cb4a6a24..8ea18665 100644 --- a/worker/knowledge-edge.mjs +++ b/worker/knowledge-edge.mjs @@ -8,6 +8,7 @@ import { Buffer } from "node:buffer"; import { createHash, scrypt, timingSafeEqual } from "node:crypto"; import catalogue from "./knowledge-mcp-catalogue.json" with { type: "json" }; +import { organisationsEnabled, resolveAccountTenant, TenantError } from "./tenants-d1.mjs"; import { authStorageBackend } from "./container-env.mjs"; import { activeUsersByPrefix, adminUsernames, grantsAdmin, keyControlsPermit, validUser } from "./control-users-d1.mjs"; import { INTEGRATION_SCOPES, lookupIntegrationPrincipal } from "./intelligence-auth-d1.mjs"; @@ -158,6 +159,19 @@ function accountScopes(user, env) { return user.scopes.split(",").map(scope => scope.trim()).filter(scope => KNOWLEDGE_SCOPES.includes(scope)); } +async function tenantPrincipal(env, principal) { + if (!organisationsEnabled(env)) return { principal }; + try { + Object.defineProperty(principal, "tenant_context", { + value: await resolveAccountTenant(env.INTELLIGENCE_DB, principal.id, env), enumerable: false, + }); + return { principal }; + } catch (error) { + if (error instanceof TenantError) throw new KnowledgeEdgeError(error.code, error.message, error.status); + throw error; + } +} + // Dashboard accounts in D1 use the Container's key prefix and Werkzeug hashes. async function accountPrincipal(env, key, clientAddress) { if (key.length > MAX_KEY_LENGTH) return { denied: true }; @@ -174,7 +188,7 @@ async function accountPrincipal(env, key, clientAddress) { // An expired key, or one used outside its address ranges, is never served here; the // Container refuses it with key_expired or ip_not_allowed. if (index >= 0 && !keyControlsPermit(verifiable[index], clientAddress)) return null; - if (index >= 0) return { principal: { id: verifiable[index].username, scopes: accountScopes(verifiable[index], env), secret_scan_mode: verifiable[index].secret_scan_mode } }; + if (index >= 0) return tenantPrincipal(env, { id: verifiable[index].username, scopes: accountScopes(verifiable[index], env), secret_scan_mode: verifiable[index].secret_scan_mode }); // A hash format the edge cannot check is left to the Container. return verifiable.length < users.length ? null : { denied: true }; } @@ -188,7 +202,7 @@ async function resolvePrincipal(request, env) { if (!key) return null; if (env.ADMIN_API_KEY && sameSecret(key, env.ADMIN_API_KEY)) { const id = adminUsername(env); - return id ? { principal: { id, scopes: [...KNOWLEDGE_SCOPES] } } : null; + return id ? tenantPrincipal(env, { id, scopes: [...KNOWLEDGE_SCOPES] }) : null; } if (!env.INTELLIGENCE_DB) return null; if (!key.startsWith(KEY_NAMESPACE)) { @@ -205,7 +219,7 @@ async function resolvePrincipal(request, env) { if (record === null) return { denied: true }; if (!validRecord(record, prefix)) return null; if (record.revokedAt !== null || await matchingHash(key, [record.keyHash], prefix) < 0) return { denied: true }; - return { principal: { id: record.id, scopes: record.scopes.filter(scope => KNOWLEDGE_SCOPES.includes(scope)) } }; + return tenantPrincipal(env, { id: record.id, scopes: record.scopes.filter(scope => KNOWLEDGE_SCOPES.includes(scope)) }); } const permits = (principal, scope) => principal.scopes.includes(scope); diff --git a/worker/managed-state-dispatch.mjs b/worker/managed-state-dispatch.mjs index 6c6da481..66f6ee16 100644 --- a/worker/managed-state-dispatch.mjs +++ b/worker/managed-state-dispatch.mjs @@ -1,4 +1,5 @@ /** Fixed private managed-state domains; sibling authorities are injected explicitly. */ +import { handleTenantRequest, TENANT_STORE_PATH } from "./tenants-d1.mjs"; import { handleIdempotencyRequest } from "./idempotency-d1.mjs"; import { handleResponsesStateRequest } from "./responses-state-d1.mjs"; import { handleContextPageStateRequest } from "./context-pages-d1.mjs"; @@ -6,6 +7,7 @@ import { handleUsageReceiptStoreRequest } from "./usage-receipts.mjs"; export function handleManagedStateRequest(request, env, { reservations, toolGrants } = {}) { const url = new URL(request.url); + if (url.pathname === TENANT_STORE_PATH) return handleTenantRequest(request, env); if (!url.pathname.startsWith("/v1/managed-state/")) return null; if (url.origin !== "http://intelligence.internal" || url.search || url.hash || url.username || url.password || request.method !== "POST") { return Response.json({ version: 1, error: { code: "invalid_store_target", message: "Invalid managed storage target." } }, { status: 400 }); diff --git a/worker/realtime.mjs b/worker/realtime.mjs index 2c3fe957..e1fa50ba 100644 --- a/worker/realtime.mjs +++ b/worker/realtime.mjs @@ -2,6 +2,7 @@ import { Buffer } from "node:buffer"; import { createHash, scrypt, timingSafeEqual } from "node:crypto"; import { activeUsersByPrefix, boundedBody, keyControlsPermit, validUser } from "./control-users-d1.mjs"; +import { organisationsEnabled, resolveAccountTenant, TenantError } from "./tenants-d1.mjs"; import { lookupIntegrationPrincipal } from "./intelligence-auth-d1.mjs"; import { authStorageBackend } from "./container-env.mjs"; import { acquireAdmission, admissionSettings, principalHash } from "./admission-do.mjs"; @@ -92,10 +93,22 @@ function integration(row) { return { owner: row.id, scopes: row.scopes, revoked_at: row.revokedAt, credential_kind: "integration", credential_fingerprint: digest(row.keyHash) }; } +async function tenantPrincipal(env, principal) { + if (!organisationsEnabled(env)) return principal; + try { + Object.defineProperty(principal, "tenant_context", { + value: await resolveAccountTenant(env.INTELLIGENCE_DB, principal.owner, env), enumerable: false, + }); + return principal; + } catch (error) { + if (error instanceof TenantError) throw new RealtimeError(error.code, error.status); + throw error; + } +} async function authenticate(request, env, model, now) { const key = bearer(request); if (!key || key.length > 1024) throw new RealtimeError("invalid_api_key", 401); - if (key.startsWith("rt1.")) return consumeTicket(key, request, env, model, now); + if (key.startsWith("rt1.")) return tenantPrincipal(env, await consumeTicket(key, request, env, model, now)); let principal; if (same(key, env.ADMIN_API_KEY)) principal = bootstrap(env); else if (/^mllm_intelligence_[A-Za-z0-9_-]{32,128}$/.test(key)) { @@ -109,6 +122,7 @@ async function authenticate(request, env, model, now) { if (await checkHash(key, row.api_key_hash)) { principal = account(row); break; } } } if (!principal) throw new RealtimeError("invalid_api_key", 401); + await tenantPrincipal(env, principal); principal.principal_hash = await principalHash(`realtime-key:${digest(key)}`); return permit(principal, request, model, now); } diff --git a/worker/tenants-d1.mjs b/worker/tenants-d1.mjs new file mode 100644 index 00000000..6c310fd7 --- /dev/null +++ b/worker/tenants-d1.mjs @@ -0,0 +1,262 @@ +/** Fixed private tenant operations; no caller-selected identity or executable SQL. */ +import { createHash } from "node:crypto"; +import { AuthorityOperation, TenantContext, legacyTenant } from "./enterprise-contract.mjs"; + +export const TENANT_STORE_PATH = "/v1/managed-state/tenants"; +const MAX_BYTES = 8192; +const OBJECT = value => value !== null && typeof value === "object" && !Array.isArray(value); +const IDENTIFIER = /^[A-Za-z0-9_:.\-]{1,128}$/; +const text = (value, maximum = 128) => typeof value === "string" && value.trim().length > 0 && value.length <= maximum && !/[\x00-\x1f\x7f]/.test(value); +const id = () => crypto.randomUUID().replaceAll("-", ""); +let warned = false; +export function organisationsEnabled(env = {}, warn = message => console.warn(message)) { + const flag = env.ORGANISATIONS_ENABLED === undefined ? "" : typeof env.ORGANISATIONS_ENABLED === "string" ? env.ORGANISATIONS_ENABLED.trim().toLowerCase() : "invalid"; + if (["", "false", "0", "no", "off"].includes(flag)) return false; + if (["true", "1", "yes", "on"].includes(flag)) return true; + if (!warned) { warned = true; warn("Invalid ORGANISATIONS_ENABLED; organisations disabled"); } + return false; +} +export class TenantError extends Error { + constructor(code, status = 400) { super(code); this.code = code; this.status = status; } + response(privateReply = false) { + return Response.json({ ...(privateReply ? { version: 1, status: this.status } : {}), + error: { code: this.code, message: "The workspace operation could not be authorized or stored." } }, + { status: this.status, headers: { "cache-control": "no-store" } }); + } +} +function identifier(value) { if (typeof value !== "string" || !IDENTIFIER.test(value)) throw new TenantError("invalid_request"); return value; } +function revision(value) { + if (value === undefined || value === null) throw new TenantError("revision_required", 428); + if (!Number.isSafeInteger(value) || value < 0 || value >= 9007199254740000) throw new TenantError("invalid_revision"); + return value; +} +function requireText(value, maximum) { if (!text(value, maximum)) throw new TenantError("invalid_request"); return value; } +function fields(value, allowed, required = []) { + if (!OBJECT(value) || Object.keys(value).some(name => !allowed.includes(name)) || required.some(name => !Object.hasOwn(value, name))) throw new TenantError("invalid_request"); +} +function cas(row, value) { if ((row?.revision ?? 0) !== revision(value)) throw new TenantError("revision_conflict", 412); } +function legacy(principal) { + const principal_id = IDENTIFIER.test(principal) ? principal : `principal:${createHash("sha256").update(principal).digest("hex")}`; + return legacyTenant(new AuthorityOperation({ context: new TenantContext({ principal_id }), scoped_id: "workspace", revision: 0, operation_id: "resolve" })); +} +export function tenantNamespace(context) { + return context.org_id === null ? "" : `org:${context.org_id}${context.team_id === null ? "" : `/team:${context.team_id}`}`; +} +const one = (db, sql, values = []) => db.prepare(sql).bind(...values).first(); +const rows = async (db, sql, values = []) => (await db.prepare(sql).bind(...values).all()).results; +async function requireSchema(db) { + if (!db) throw new TenantError("tenant_storage_unavailable", 503); + // Reads also verify the complete authority, so an absent binding table cannot mean legacy. + for (const sql of ["SELECT id,name,status,revision FROM tenant_organisations LIMIT 0", + "SELECT id,org_id,name,status,revision FROM tenant_teams LIMIT 0", + "SELECT org_id,principal,team_id,role,status,revision FROM tenant_memberships LIMIT 0", + "SELECT principal,org_id,team_id,revision FROM tenant_bindings LIMIT 0", + "SELECT id,actor,action,org_id,team_id,principal,old_revision,new_revision,at FROM tenant_audit LIMIT 0"]) { + await db.prepare(sql).all(); + } +} +async function organisation(db, orgId, active = false) { + const org = await one(db, "SELECT * FROM tenant_organisations WHERE id=?", [identifier(orgId)]); + if (!org) throw new TenantError("workspace_not_found", 404); + if (active && org.status !== "active") throw new TenantError("workspace_forbidden", 403); + return org; +} +async function team(db, orgId, teamId, active = false) { + const value = await one(db, "SELECT * FROM tenant_teams WHERE org_id=? AND id=?", [orgId, identifier(teamId)]); + if (!value) throw new TenantError("workspace_not_found", 404); + if (active && value.status !== "active") throw new TenantError("workspace_forbidden", 403); + return value; +} +async function workspace(db, principal, orgId, teamId) { + await organisation(db, orgId, true); + if (teamId !== null) await team(db, orgId, teamId, true); + const membership = await one(db, "SELECT * FROM tenant_memberships WHERE org_id=? AND principal=?", [orgId, principal]); + if (!membership || membership.status !== "active" || membership.team_id !== teamId) throw new TenantError("workspace_forbidden", 403); +} +async function resolve(db, principal) { + const value = await one(db, `SELECT b.org_id,b.team_id,b.revision AS binding_revision, + o.id AS found_org,o.status AS org_status,o.revision AS org_revision, + t.id AS found_team,t.status AS team_status,t.revision AS team_revision, + m.status AS member_status,m.team_id AS member_team,m.revision AS member_revision + FROM tenant_bindings b LEFT JOIN tenant_organisations o ON o.id=b.org_id + LEFT JOIN tenant_teams t ON t.id=b.team_id AND t.org_id=b.org_id + LEFT JOIN tenant_memberships m ON m.org_id=b.org_id AND m.principal=b.principal WHERE b.principal=?`, [principal]); + if (!value) return legacy(principal); + if (!value.found_org || value.team_id !== null && !value.found_team) throw new TenantError("workspace_not_found", 404); + if (value.org_status !== "active" || value.team_id !== null && value.team_status !== "active" + || value.member_status !== "active" || value.member_team !== value.team_id) throw new TenantError("workspace_forbidden", 403); + return new TenantContext({ principal_id: legacy(principal).principal_id, org_id: value.org_id, team_id: value.team_id, + grants_revision: value.binding_revision + value.org_revision + (value.team_revision ?? 0) + value.member_revision }); +} +export async function resolveAccountTenant(db, principal, env = {}) { + if (!organisationsEnabled(env)) return legacy(principal); + try { requireText(principal); await requireSchema(db); return await resolve(db, principal); } + catch (error) { if (error instanceof TenantError) throw error; throw new TenantError("tenant_storage_unavailable", 503); } +} +function audit(db, mutationId, v, action, orgId, teamId, principal, old, next) { + return db.prepare(`INSERT INTO tenant_audit (id,actor,action,org_id,team_id,principal,old_revision,new_revision,at) + SELECT ?,?,?,?,?,?,?,?,? WHERE changes()=1`).bind(mutationId, requireText(v.actor), action, orgId, teamId, principal, old, next, new Date().toISOString()); +} +async function mutate(db, statement, v, action, orgId, teamId, principal, old, next, extra = []) { + const mutationId = id(); + const statements = [statement, audit(db, mutationId, v, action, orgId, teamId, principal, old, next), ...extra.map(factory => factory(mutationId))]; + const result = await db.batch(statements); + if (result[0].meta.changes !== 1) throw new TenantError("revision_conflict", 412); +} +const ACTIVE_ORG = "EXISTS (SELECT 1 FROM tenant_organisations WHERE id=? AND status='active')"; +const ACTIVE_TEAM = "(? IS NULL OR EXISTS (SELECT 1 FROM tenant_teams WHERE id=? AND org_id=? AND status='active'))"; +const ACTIVE_MEMBER = "EXISTS (SELECT 1 FROM tenant_memberships WHERE org_id=? AND principal=? AND status='active' AND team_id IS ?)"; +const BINDING_CAS = "COALESCE((SELECT revision FROM tenant_bindings WHERE principal=?),0)=?"; + +async function namedWrite(db, operation, v) { + const creating = operation.endsWith("create"), isTeam = operation.startsWith("team"); + fields(v.data, ["name", "status"], creating ? ["name"] : []); + if (!Object.keys(v.data).length || creating && Object.hasOwn(v.data, "status") && v.data.status !== "active") throw new TenantError("invalid_request"); + const org = operation === "org_create" ? null : await organisation(db, v.org_id, creating && isTeam); + const previous = creating ? null : isTeam ? await team(db, v.org_id, v.team_id) : org; + if (!creating) cas(previous, v.revision); + const name = requireText(Object.hasOwn(v.data, "name") ? v.data.name : previous?.name, 200); + const status = Object.hasOwn(v.data, "status") ? v.data.status : previous?.status ?? "active"; + if (!["active", "deactivated"].includes(status)) throw new TenantError("invalid_request"); + const old = previous?.revision ?? 0, next = old + 1, target = creating ? id() : previous.id; + let statement; + if (isTeam && creating) { + if ((await one(db, "SELECT count(*) AS count FROM tenant_teams WHERE org_id=?", [v.org_id])).count >= 100) throw new TenantError("tenant_limit_reached", 409); + statement = db.prepare(`INSERT INTO tenant_teams SELECT ?,?,?,?,? WHERE ${ACTIVE_ORG} + AND (SELECT count(*) FROM tenant_teams WHERE org_id=?)<100`).bind(target, v.org_id, name, status, next, v.org_id, v.org_id); + } else if (creating) statement = db.prepare("INSERT INTO tenant_organisations VALUES (?,?,?,?)").bind(target, name, status, next); + else if (isTeam) statement = db.prepare("UPDATE tenant_teams SET name=?,status=?,revision=? WHERE id=? AND org_id=? AND revision=?").bind(name, status, next, target, v.org_id, old); + else statement = db.prepare("UPDATE tenant_organisations SET name=?,status=?,revision=? WHERE id=? AND revision=?").bind(name, status, next, target, old); + try { await mutate(db, statement, v, operation, isTeam ? v.org_id : target, isTeam ? target : null, null, old, next); } + catch (error) { + if (isTeam && creating && error instanceof TenantError) { + await organisation(db, v.org_id, true); + if ((await one(db, "SELECT count(*) AS count FROM tenant_teams WHERE org_id=?", [v.org_id])).count >= 100) throw new TenantError("tenant_limit_reached", 409); + } + throw error; + } + return { id: target, ...(isTeam ? { org_id: v.org_id } : {}), name, status, revision: next }; +} +async function memberWrite(db, v) { + const orgId = identifier(v.org_id), principal = requireText(v.principal); + fields(v.data, ["role", "team_id", "status", "bind", "binding_revision"]); + if (!Object.keys(v.data).length || "bind" in v.data && typeof v.data.bind !== "boolean" || "binding_revision" in v.data && !v.data.bind) throw new TenantError("invalid_request"); + await organisation(db, orgId); + if (!await one(db, "SELECT username FROM control_users WHERE username=?", [principal])) throw new TenantError("principal_not_found", 404); + const previous = await one(db, "SELECT * FROM tenant_memberships WHERE org_id=? AND principal=?", [orgId, principal]); + cas(previous, v.revision); + if (!previous && (await one(db, "SELECT count(*) AS count FROM tenant_memberships WHERE org_id=?", [orgId])).count >= 1000) throw new TenantError("tenant_limit_reached", 409); + const role = Object.hasOwn(v.data, "role") ? v.data.role : previous?.role ?? "member"; + const status = Object.hasOwn(v.data, "status") ? v.data.status : previous?.status ?? "active"; + const teamId = Object.hasOwn(v.data, "team_id") ? v.data.team_id : previous?.team_id ?? null; + if (!["admin", "billing", "member"].includes(role) || !["active", "deactivated"].includes(status)) throw new TenantError("invalid_request"); + if (status === "active") await organisation(db, orgId, true); + if (teamId !== null) await team(db, orgId, teamId, status === "active"); + if (v.data.bind) { + if (status !== "active") throw new TenantError("workspace_forbidden", 403); + cas(await one(db, "SELECT * FROM tenant_bindings WHERE principal=?", [principal]), v.data.binding_revision); + } + const old = previous?.revision ?? 0, next = old + 1; + const conditions = `EXISTS (SELECT 1 FROM control_users WHERE username=?) AND + COALESCE((SELECT revision FROM tenant_memberships WHERE org_id=? AND principal=?),0)=? + AND (? > 0 OR (SELECT count(*) FROM tenant_memberships WHERE org_id=?)<1000) + AND (? <> 'active' OR (${ACTIVE_ORG} AND ${ACTIVE_TEAM})) + AND (? IS NULL OR EXISTS (SELECT 1 FROM tenant_teams WHERE id=? AND org_id=?)) + AND (?=0 OR ${BINDING_CAS})`; + const statement = db.prepare(`INSERT INTO tenant_memberships (org_id,principal,team_id,role,status,revision) + SELECT ?,?,?,?,?,? WHERE ${conditions} + ON CONFLICT(org_id,principal) DO UPDATE SET team_id=excluded.team_id,role=excluded.role,status=excluded.status,revision=excluded.revision`).bind( + orgId, principal, teamId, role, status, next, principal, orgId, principal, old, old, orgId, + status, orgId, teamId, teamId, orgId, teamId, teamId, orgId, v.data.bind ? 1 : 0, principal, v.data.binding_revision ?? 0); + const extra = v.data.bind ? [mutation => db.prepare(`INSERT INTO tenant_bindings (principal,org_id,team_id,revision) + SELECT ?,?,?,? WHERE EXISTS (SELECT 1 FROM tenant_audit WHERE id=?) + ON CONFLICT(principal) DO UPDATE SET org_id=excluded.org_id,team_id=excluded.team_id,revision=excluded.revision`) + .bind(principal, orgId, teamId, v.data.binding_revision + 1, mutation), + () => audit(db, id(), v, "binding_set", orgId, teamId, principal, v.data.binding_revision, v.data.binding_revision + 1)] : []; + try { await mutate(db, statement, v, "member_set", orgId, teamId, principal, old, next, extra); } + catch (error) { + if (error instanceof TenantError && error.code === "revision_conflict" && !previous + && (await one(db, "SELECT count(*) AS count FROM tenant_memberships WHERE org_id=?", [orgId])).count >= 1000) throw new TenantError("tenant_limit_reached", 409); + throw error; + } + return { org_id: orgId, principal, team_id: teamId, role, status, revision: next }; +} +async function bindingWrite(db, v) { + const principal = requireText(v.principal); + if (v.actor !== principal) throw new TenantError("workspace_forbidden", 403); + fields(v.data, ["org_id", "team_id"], ["org_id", "team_id"]); + const orgId = identifier(v.data.org_id), teamId = v.data.team_id; + await workspace(db, principal, orgId, teamId); + const previous = await one(db, "SELECT * FROM tenant_bindings WHERE principal=?", [principal]); + cas(previous, v.revision); + const old = previous?.revision ?? 0, next = old + 1; + const statement = db.prepare(`INSERT INTO tenant_bindings (principal,org_id,team_id,revision) + SELECT ?,?,?,? WHERE ${BINDING_CAS} AND ${ACTIVE_ORG} AND ${ACTIVE_TEAM} AND ${ACTIVE_MEMBER} + ON CONFLICT(principal) DO UPDATE SET org_id=excluded.org_id,team_id=excluded.team_id,revision=excluded.revision`).bind( + principal, orgId, teamId, next, principal, old, orgId, teamId, teamId, orgId, orgId, principal, teamId); + try { await mutate(db, statement, v, "binding_set", orgId, teamId, principal, old, next); } + catch (error) { if (error instanceof TenantError) await workspace(db, principal, orgId, teamId); throw error; } + return { principal, org_id: orgId, team_id: teamId, revision: next }; +} +const OPERATIONS = Object.freeze({ + org_list: [], org_get: ["org_id"], org_create: ["actor", "data"], org_update: ["actor", "org_id", "revision", "data"], + team_list: ["org_id"], team_create: ["actor", "org_id", "data"], team_update: ["actor", "org_id", "team_id", "revision", "data"], + member_list: ["org_id"], member_set: ["actor", "org_id", "principal", "revision", "data"], + workspaces: ["principal"], binding_set: ["actor", "principal", "revision", "data"], resolve: ["principal"], +}); +async function execute(db, v) { + if (v.operation === "resolve") return resolve(db, requireText(v.principal)); + if (v.operation === "org_list") return { organisations: await rows(db, "SELECT * FROM tenant_organisations ORDER BY id") }; + if (v.operation === "workspaces") { + const principal = requireText(v.principal); + const memberships = await rows(db, `SELECT m.* FROM tenant_memberships m JOIN tenant_organisations o ON o.id=m.org_id + LEFT JOIN tenant_teams t ON t.id=m.team_id AND t.org_id=m.org_id + WHERE m.principal=? AND m.status='active' AND o.status='active' AND (m.team_id IS NULL OR t.status='active') ORDER BY m.org_id`, [principal]); + return { memberships, binding: await one(db, "SELECT * FROM tenant_bindings WHERE principal=?", [principal]) + ?? { principal, org_id: null, team_id: null, revision: 0 } }; + } + if (v.operation === "binding_set") return bindingWrite(db, v); + if (v.operation.endsWith("create") || v.operation.endsWith("update")) return namedWrite(db, v.operation, v); + const org = await organisation(db, v.org_id); + if (v.operation === "org_get") return org; + if (v.operation === "team_list") return { teams: await rows(db, "SELECT * FROM tenant_teams WHERE org_id=? ORDER BY id", [v.org_id]) }; + if (v.operation === "member_list") return { members: await rows(db, "SELECT * FROM tenant_memberships WHERE org_id=? ORDER BY principal", [v.org_id]) }; + return memberWrite(db, v); +} +async function boundedJson(request) { + if (request.headers.get("content-type")?.split(";", 1)[0].trim().toLowerCase() !== "application/json" || !request.body) throw new TenantError("invalid_request"); + const length = request.headers.get("content-length"); + if (length !== null && (!/^\d+$/.test(length) || Number(length) > MAX_BYTES)) throw new TenantError("invalid_request"); + const reader = request.body.getReader(), chunks = []; let size = 0; + try { + while (true) { + const { value, done } = await reader.read(); if (done) break; + size += value.byteLength; + if (size > MAX_BYTES) { void reader.cancel().catch(() => {}); throw new TenantError("invalid_request"); } + chunks.push(value); + } + const data = new Uint8Array(size); let offset = 0; + for (const chunk of chunks) { data.set(chunk, offset); offset += chunk.length; } + return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(data)); + } catch { throw new TenantError("invalid_request"); } + finally { reader.releaseLock(); } +} +export async function handleTenantRequest(request, env) { + if (!organisationsEnabled(env)) return new TenantError("not_found", 404).response(true); + const url = new URL(request.url); + if (url.origin !== "http://intelligence.internal" || url.pathname !== TENANT_STORE_PATH || url.search || url.hash || url.username || url.password || request.method !== "POST") return new TenantError("not_found", 404).response(true); + let v; + try { + v = await boundedJson(request); + if (!OBJECT(v) || v.version !== 1 || !Object.hasOwn(OPERATIONS, v.operation)) throw new TenantError("invalid_request"); + const required = OPERATIONS[v.operation]; + fields(v, ["version", "operation", ...required], ["version", "operation", ...required.filter(name => name !== "revision")]); + if (required.includes("revision")) revision(v.revision); + if (required.includes("actor")) requireText(v.actor); + await requireSchema(env.INTELLIGENCE_DB); + const result = await execute(env.INTELLIGENCE_DB, v); + return Response.json({ version: 1, result }, { headers: { "cache-control": "no-store" } }); + } catch (error) { + return (error instanceof TenantError ? error : new TenantError("tenant_storage_unavailable", 503)).response(true); + } +}