diff --git a/CHANGELOG.md b/CHANGELOG.md index 8941863a..eb623d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -117,6 +117,12 @@ Everything on `main` before the first tagged release. AgentOS.NET has not shippe without a tenant claim is refused instead of falling back to the `default` tenant. - Sign-out is a POST with an antiforgery token, the VS Code approve form is antiforgery-protected, and plugins load only when their SHA-256 is listed in `Plugins:AllowedSha256` (GAP-19). +- Invitations are single-use, bound to the invited address and revocable from Settings › Users (GAP-11); the + audit trail is append-only for the application's database role and hash-chained per tenant, with **Verify + chain** in Settings › Activity log (GAP-11b). +- Row-level security stage 2 (GAP-14): a connection with no tenant sees nothing; settings, the audit trail and + invitations are under RLS; cross-tenant platform work runs in explicit, counted platform scopes; a failed RLS + bootstrap stops the host outside Development and `/health` reports `row-level-security`. ### Migration notes @@ -128,5 +134,19 @@ Everything on `main` before the first tagged release. AgentOS.NET has not shippe [docs/CONFIG.md](docs/CONFIG.md#set-at-deploy-bootstrap). - The `approver` realm role (E1.5) is created by the azd post-deploy hook and the Helm realm job; a local realm imported before it needs the role added through the Keycloak admin API, or its volume dropped. +- Invitation links minted before invitations became single-use rows stop working — send new ones. +- Audit events recorded before the hash chain stay readable but are reported as "not chained"; the chain starts + with the next event. +- **Row-level security stage 2 is not a rolling upgrade.** The first new replica rewrites the database policies when + it boots; a replica of the previous version still running then reads platform settings, model keys and scheduled + work as nothing, and the audit rows it writes have no chain link (**Verify chain** reports a break there). Stop + every old Api / Web replica before the first new one starts — a stop-then-start cut-over: compose recreates + single containers that way already; on Helm scale the `api` / `web` Deployments to 0 before `helm upgrade`; on + Azure Container Apps deactivate the active revisions before `azd deploy`. Steps in + [docs/ONPREM.md](docs/ONPREM.md#upgrading-to-row-level-security-stage-2--stop-the-old-version-first) and + [docs/DEPLOYMENT.md](docs/DEPLOYMENT.md#a4--subsequent-deploys). +- Outside Development the hosts now refuse to start when the row-level-security bootstrap fails (the database user + needs `CREATEROLE`). Tests or scripts that delete audit rows will make **Verify chain** report a break — that is + the point; don't clean audit rows up. [Unreleased]: https://github.com/hoangsnowy/AgentOS.NET/commits/main diff --git a/README.md b/README.md index 65322064..eba9d2d3 100644 --- a/README.md +++ b/README.md @@ -455,7 +455,10 @@ Roles: `admin` (tenant admin), `member`, and `platform-admin` (platform-wide set Row-level isolation: pipeline runs, run metrics and orchestrations, sessions/runners and workspaces carry a `TenantId` with an EF global query filter on `ITenantContext.TenantId`; writes stamp the -tenant. Settings and keys are stored per `(tenant, key)` and always read for an explicit tenant. +tenant. Settings and keys are stored per `(tenant, key)` and always read for an explicit tenant. Below the +application, Postgres row-level security binds every connection to its tenant on every tenant table — settings, +audit trail and invitations included — and a connection with no tenant sees nothing +([ADR-0012](docs/adr/0012-postgres-row-level-security.md)). ## API diff --git a/SECURITY.md b/SECURITY.md index 243a69f5..d04b7288 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -46,11 +46,16 @@ When reporting, include: affected component, reproduction steps, and impact. - **Audit trail** — every settings change (old → new, secrets masked), every direct config write (LLM keys, tokens, prompts, tool policy, budget — recorded as *set / cleared*, never the value), member lifecycle, invitations and sign-in land in an append-only audit log, viewable in **Settings → Audit log** and - exportable as formula-safe CSV. Platform-scope changes are visible to platform admins only. + exportable as formula-safe CSV. Platform-scope changes are visible to platform admins only. The application's + database role may only add and read audit rows (no update / delete), and each tenant's events form a SHA-256 + hash chain that **Verify chain** checks — it names the first event that was changed or removed. +- **Invitations** — single-use, accepted only for the address they were sent to, revocable by a tenant admin until + used, and expiring after 1–168 hours (default 24); refused attempts are audited. - **Tenant isolation** — row-level EF query filters **and Postgres row-level security** on the tenant tables ([ADR-0012](docs/adr/0012-postgres-row-level-security.md)): every connection runs as a restricted role bound to - the caller's tenant, so a forgotten filter returns only the caller's rows and a write for another tenant is - refused. Per-tenant keys are never mixed with another tenant's or the platform's. Cross-tenant endpoints (list / provision tenants) require `platform-admin`, + the caller's tenant, so a forgotten filter returns only the caller's rows, a write for another tenant is + refused, and a connection with no tenant sees nothing; the few cross-tenant platform operations run in an + explicit, counted platform scope. Outside Development the hosts refuse to start without it. Per-tenant keys are never mixed with another tenant's or the platform's. Cross-tenant endpoints (list / provision tenants) require `platform-admin`, never the per-tenant `admin` role; a real-auth E2E proves a second tenant sees none of the first's data. - **QA gate before every PR** — a pipeline result becomes a pull request only when QA passed on real model output (`PrGate`, enforced inside the PR service itself): failed runs, the iteration cap with QA still diff --git a/docs/CONFIG.md b/docs/CONFIG.md index de09be5c..80e83866 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -35,7 +35,7 @@ to the next request. |---|---|---| | `ConnectionStrings:DefaultConnection` | — | Postgres. Absent → modules fall back to no-op repositories (stateless boot) unless `RequireDatabase` is true. Aspire injects it on the full stack. | | `Persistence:RequireDatabase` | `true` (code); Web base `false` for the standalone run, `true` in Web Production | Fail-loud guard: refuse to boot stateless when a DB is expected. | -| `Persistence:RowLevelSecurity` | `true` | Postgres RLS on the tenant tables ([ADR-0012](adr/0012-postgres-row-level-security.md)); the DB user needs `CREATEROLE`. `false` = escape hatch (application filters only). | +| `Persistence:RowLevelSecurity` | `true` | Postgres RLS on the tenant tables ([ADR-0012](adr/0012-postgres-row-level-security.md)); a connection with no tenant sees nothing. The DB user needs `CREATEROLE`. A failed bootstrap **stops the host outside Development** (Development logs an error and runs on application filters). `false` = escape hatch (application filters only; logged as a warning every boot, `/health` reports `row-level-security` Degraded). | | `Auth:Keycloak:Authority` | empty | **Required outside Development.** Dev fallback `http://localhost:8080/realms/agentic` applies in Development only. | | `Auth:Keycloak:Audience` | `agentic-api` | JWT audience (Api). | | `Auth:Keycloak:ClientId` / `ClientSecret` | `agentic-web` / empty | Web OIDC client. Secret required outside Development; the dev default secret is rejected by `DevSecretGuard`. | diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index 48775d47..2d0d2c9a 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -115,6 +115,23 @@ azd deploy # re-build + re-push images only, no infra changes azd up # provision + deploy (safe to re-run; Postgres and ACA env are idempotent) ``` +**Upgrading to row-level security stage 2** (the release that brings [ADR-0012](adr/0012-postgres-row-level-security.md) +stage 2 + the audit hash chain) is the one deploy that must not overlap: the new revision rewrites the database +policies when it boots, and a revision of the previous version still serving traffic then reads platform settings, +model keys and scheduled work as *nothing* and writes audit rows the chain verifier reports as a break. Container Apps +keeps the old revision live until the new one is healthy, so deactivate the running revisions first, then deploy: + +```bash +for app in api web; do + for rev in $(az containerapp revision list -n $app -g --query "[?properties.active].name" -o tsv); do + az containerapp revision deactivate -n $app -g --revision "$rev" + done +done +azd deploy # the new revisions start against the stage-2 policies; nothing old is left to read through them +``` + +Expect a few minutes of downtime. Later deploys roll as usual. (On-prem compose / Helm: [ONPREM.md §4](ONPREM.md#4-upgrade).) + Tear down when done: ```bash diff --git a/docs/ONPREM.md b/docs/ONPREM.md index becc90c3..9156af2d 100644 --- a/docs/ONPREM.md +++ b/docs/ONPREM.md @@ -221,8 +221,9 @@ external Keycloak without its URL, several Web replicas without sticky sessions) - **External Postgres** — `postgresql.enabled=false` and `externalDatabase.{host,port,database,username,sslMode}` (password: `postgres-password` in the secret). Create the `agentos` and `keycloak` databases first. The user must **own** them and hold **`CREATEROLE`**: the hosts create the `agentos_rls` role, its grants and the - row-level-security policies at every boot. Without it RLS stays off and the hosts log - `Row-level security bootstrap FAILED` (isolation falls back to application filters only). + row-level-security policies at every boot. Without it the hosts **refuse to start** with "Row-level security + could not be set up, so AgentOS.NET will not start" (`Persistence:RowLevelSecurity=false` runs without it, on + application filters only — on purpose, and reported Degraded by `/health`). - **External Keycloak** — `keycloak.enabled=false`, `keycloak.publicUrl=https://sso.corp.example`. Import [`infra/keycloak/agentic-realm.json`](../infra/keycloak/agentic-realm.json) and copy [`infra/keycloak/themes/agentos`](../infra/keycloak/themes/agentos) into its themes directory first; the realm @@ -285,9 +286,34 @@ Nothing in the stack calls the internet at runtime except the LLM providers you - compose: set `AGENTOS_VERSION` in `.env` (bundle installs) → `docker compose up -d --wait` - Helm: `helm upgrade agentos -n agentos -f my-values.yaml --wait --timeout 15m` 4. Database migrations run at boot, per module, serialised by an advisory lock — rolling several replicas is - safe. The realm step re-runs (idempotent; seed passwords are never re-applied). Verify with `/health` and a + safe **between versions on the same row-level-security stage**; the one upgrade that is not rolling-safe is + below. The realm step re-runs (idempotent; seed passwords are never re-applied). Verify with `/health` and a sign-in. +### Upgrading to row-level security stage 2 — stop the old version first + +The release that brings RLS stage 2 ([ADR-0012](adr/0012-postgres-row-level-security.md): a connection with no +tenant sees **nothing**; settings, the audit trail and invitations under RLS; the audit hash chain) rewrites the +database policies when its **first** replica boots. From that moment an Api or Web replica still running the +previous version reads through the new policies with its old assumptions: its tenant-less reads (platform settings +and model keys resolved off a request, the scheduler, the Autopilot scan, the runner handshake) come back empty, and +the audit rows it writes carry no chain link, so **Verify chain** reports the trail broken from there. Do this +upgrade as a stop-then-start cut-over, never side by side: + +- **compose:** `docker compose up -d --wait` recreates each container (the old one stops before the new one starts), + which is enough with one `api` and one `web`. If you scaled them, run `docker compose stop api web` first. +- **Helm:** a Deployment rolls new pods up before the old ones go. Scale the old version to zero first, then + upgrade (the chart puts `replicaCount` back): + + ```bash + kubectl -n agentos scale deployment -l app.kubernetes.io/component=api --replicas=0 + kubectl -n agentos scale deployment -l app.kubernetes.io/component=web --replicas=0 + kubectl -n agentos wait --for=delete pod -l 'app.kubernetes.io/component in (api,web)' --timeout=5m + helm upgrade agentos -n agentos -f my-values.yaml --wait --timeout 15m + ``` + +Expect a few minutes without the desktop and the Api. Later upgrades on stage 2 roll as usual. + Notes: - **Keycloak never re-imports an existing realm.** Realm changes shipped in a release (a new role, client @@ -393,7 +419,7 @@ card without a version under the service account. | Sign-in loops, *IDX10205 issuer invalid* | `KEYCLOAK_PUBLIC_URL` differs from how browsers reach Keycloak — the issuer is fixed to it. | | Api/Web exit: *RequireKeyWrapping is on but the key ring is unwrapped* | The certificate is not mounted or its path/password is wrong. Check `dataprotection/` and `DATAPROTECTION_CERTIFICATE_*`, or the Helm Secret. | | HTTP 431 on Keycloak | Oversized cookie jar on a shared host name — compose/Helm raise Keycloak's limit to 128K; use dedicated host names per install (and `COOKIE_PREFIX` / `web.cookiePrefix`). | -| Log: `Row-level security bootstrap FAILED` | The database user lacks `CREATEROLE`/ownership (external Postgres). Grant it, restart. | +| Host exits: `Row-level security could not be set up` (log: `Row-level security bootstrap FAILED`) | The database user lacks `CREATEROLE`/ownership (external Postgres). Grant it (or have a DBA create `agentos_rls` and grant it), restart. | | Log: `libgssapi_krb5.so.2: cannot open shared object file` | Harmless — Npgsql probing Kerberos; the app uses password auth. | | Api/Web exit: *Hosting:PublicBaseUrl is required outside Development* / *must use https://* | Set `WEB_PUBLIC_URL` / `web.publicUrl` to the desktop's `https://` URL (`http://` only for `localhost`). | | HTTP 400 with an empty body on every page | The browser's host name is not the public URL's (nor `localhost`) — fix `WEB_PUBLIC_URL` / `web.publicUrl`, or add the extra name to `AllowedHosts`. | diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.md index 16bddac0..d73f0739 100644 --- a/docs/OPERATIONS.md +++ b/docs/OPERATIONS.md @@ -56,7 +56,10 @@ dashboard or rule references a series no instrument emits. | | `agentos.evidence.remote_refused` (`agentos_evidence_remote_refused_total`) | counter | `reason` = `unknown_run` · `not_dispatched` · `wrong_connection` · `grace_passed` · `run_cap` · `batch_cap` | Tool calls a paired runner reported that the server refused to record (each refusal is also logged). Cap hits on very long runs are expected; the rest mean a runner is out of step. | | | `agentos.remote.result_refused` (`agentos_remote_result_refused_total`) | counter | `kind` = `request` · `tool_call` · `progress` · `unknown` | Runner results (`CompleteRequest`, `CompleteToolCall`) and progress steps (`ReportProgress`) the server refused (GAP-06). `request` / `tool_call` / `progress`: it came from a connection the work was not dispatched to — neither that runner nor its own reconnect (same runner id, same tenant); each is also logged as a warning. `unknown`: no one waits for that id any more — a runner finishing after the timeout or a Stop, a duplicate, or an id this replica never dispatched (logged at Information). | | | `agentos.tools.output.saved_chars` (`agentos_tools_output_saved_chars_total`) | counter `{char}` | `kind` = `builtin` · `mcp` · `remote` | Tool-output characters the gateway kept away from the model (log shaping + size cap). Dashboards show **≈ tokens = chars ÷ 4** — an estimate of tokens not sent, never billed usage. | -| `AgentOs.Health` | `agentos.health.status` (`agentos_health_status`) | gauge | `agentos.health.check` = `self` · `postgres` · `keycloak` · `overall` | 1 healthy · 0.5 degraded · 0 unhealthy, published every 60 s (each publish runs every check, so not more often). | +| `AgentOs.Health` | `agentos.health.status` (`agentos_health_status`) | gauge | `agentos.health.check` = `self` · `postgres` · `row-level-security` · `keycloak` · `overall` | 1 healthy · 0.5 degraded · 0 unhealthy, published every 60 s (each publish runs every check, so not more often). | +| `AgentOs.Persistence` | `agentos.rls.connections` (`agentos_rls_connections_total`) | counter | `scope` = `tenant` · `platform` · `none` | Connections bound by row-level security (ADR-0012). `none` sees **nothing** (stage 2): a rising `none` is a code path that lost its tenant. | +| | `agentos.rls.platform_scope` (`agentos_rls_platform_scope_total`) | counter | `reason` = `autopilot-scan` · `scheduler` · `runner-handshake` · `runner-revalidation` · `platform-settings` · `audit-platform` | Explicit cross-tenant scopes entered. A new reason is a new cross-tenant code path — review it. | +| `AgentOs.Audit` | `agentos.audit.lost` (`agentos_audit_lost_total`) | counter `{event}` | `reason` = `no_tenant` · `database` | Audit rows that were not recorded. Writes are best-effort (the surrounding operation still completes), so every loss is counted and logged instead. **Should stay 0**: `no_tenant` is a code path that lost its tenant; `database` an outage or a refused insert. | | `AgentOs.Http` | `agentos.http.rate_limited` (`agentos_http_rate_limited_total`) | counter `{request}` | `authenticated` = `true` · `false` | Requests the global request limiter refused with 429 (Settings → Platform → Request limits). A steady anonymous rate is scraping or a sign-up flood; an authenticated one means a limit too low for real use. | Traces: the `AgentOs.Llm` ActivitySource emits one `chat {model}` client span per provider attempt (attributes @@ -81,7 +84,7 @@ Traces: the `AgentOs.Llm` ActivitySource emits one `chat {model}` client span pe | Endpoint | Checks | Use | |---|---|---| | `/alive` | `self` (tag `live`) | Liveness probe — restart the container when it fails. | -| `/health` | `self` + `postgres` (when `ConnectionStrings:DefaultConnection` is set) + `keycloak` (OIDC discovery, outside Development) | Readiness probe + the outside-in availability probe. JSON body names each check. | +| `/health` | `self` + `postgres` and `row-level-security` (when `ConnectionStrings:DefaultConnection` is set; Degraded = turned off by `Persistence:RowLevelSecurity=false`) + `keycloak` (OIDC discovery, outside Development) | Readiness probe + the outside-in availability probe. JSON body names each check. | Every check is also the `agentos_health_status` gauge, so an unhealthy dependency alerts even when nobody probes. diff --git a/docs/adr/0012-postgres-row-level-security.md b/docs/adr/0012-postgres-row-level-security.md index b16f8959..c94409cb 100644 --- a/docs/adr/0012-postgres-row-level-security.md +++ b/docs/adr/0012-postgres-row-level-security.md @@ -1,6 +1,6 @@ # ADR-0012 — Postgres row-level security for tenant tables -- **Status:** Accepted (stage 1) · 2026-10-17 +- **Status:** Accepted — stage 1 · 2026-10-17; stage 2 · 2026-11-03 (GAP-14, [threat model](../security/threat-model.md)) - **Context source:** enterprise readiness review E0.1 ([enterprise-readiness.md](../plans/enterprise-readiness.md)), plan 0.9 ## Context @@ -14,53 +14,100 @@ Three facts shape the design: - **Dev and lab connect as the Postgres superuser**, and superusers bypass RLS even when it is forced. - **Azure Flexible Server connects as the table owner** (not a superuser). Owners bypass RLS unless - `FORCE ROW LEVEL SECURITY` is set. -- Not every database call knows its tenant yet: runner pairing on the SignalR hub, background drains and boot - run with no signed-in user, and two tables (`config.app_config`, `tenants.audit_events`) hold platform-scope - rows every tenant reads. + `FORCE ROW LEVEL SECURITY` is set — and a role with `BYPASSRLS` can't be created there without a superuser. +- Not every database call knows its tenant from the caller: the runner handshake on the SignalR hub, background + workers (scheduler, autopilot, evidence drain, runner revalidation), anonymous sign-up and boot run with no + signed-in user, and `config.app_config` holds platform-scope rows every tenant reads. ## Decision -**Stage 1 (this ADR):** +**Stage 1 (2026-10-17)** introduced the mechanism: a bootstrap step, a restricted role, a per-connection switch, +and one policy per tenant table that let a session with **no tenant see every row** (so the paths above kept +working). **Stage 2 (2026-11-03)** closes that escape. The mechanism, as it stands: -1. A bootstrap step runs once per boot, after every module migrated, under an advisory lock. It is idempotent: - - creates a `NOLOGIN NOSUPERUSER NOBYPASSRLS` role `agentos_rls`; - - makes the connection user a member with `SET` (PG16+ syntax where available); +1. **Bootstrap** — once per boot, after every module migrated, under an advisory lock, idempotent + (`RowLevelSecurity.BootstrapSql`): + - creates a `NOLOGIN NOSUPERUSER NOBYPASSRLS` role `agentos_rls` and makes the connection user a member with + `SET` (PG16+ syntax where available); - grants DML on every module schema, with default privileges for future tables; - - enables **and forces** RLS on the nine stage-1 tenant tables, each with one policy: + - **append-only tables** (`RowLevelSecurity.AppendOnlyTables` = `tenants.audit_events`, GAP-11b): + `REVOKE UPDATE, DELETE, TRUNCATE` from the role — after the schema-wide grants, which run every boot; + - enables **and forces** RLS on every table in `RowLevelSecurity.Tables`, each with one policy: ```sql - USING / WITH CHECK ("TenantId" = current_setting('app.tenant_id', true) - OR coalesce(current_setting('app.tenant_id', true), '') = '') + USING / WITH CHECK ("TenantId" = NULLIF(current_setting('app.tenant_id', true), '') + OR current_setting('app.platform', true) = 'on') ``` -2. **Per connection**, the module contexts (EF `RowLevelSecurityInterceptor`) and the Dapper read paths - (`OpenWithTenantAsync`) run `SET ROLE agentos_rls` and set `app.tenant_id`. Npgsql resets session state - when a pooled connection returns. Migrations and boot run before RLS is enabled. -3. **The tenant** comes from the ambient identity, else the authenticated HTTP request's `tenant` claim: - - a run pushes the ambient identity; - - every Blazor circuit event pushes it too, through the new `CircuitIdentityHandler`, so desktop queries - are bound as well as Api requests and runs. -4. Stage-1 tables: - - `pipeline.pipeline_runs`, `run_metrics`, `orchestrations`; - - `sessions.sessions`, `session_repos`, `runners`; + An empty or unset tenant matches **no row**; only an explicit platform scope widens the session. + - `config.app_config` also gets `agentos_shared_read`: `FOR SELECT USING ("TenantId" = '__platform')` — every + tenant reads the platform settings its own resolve against; writing them needs the platform scope (an + `UPDATE` / `DELETE` of a platform row by a tenant session touches nothing, an `INSERT` is refused). + Each table's statements run in one `DO` block, so a policy is never missing while live traffic queries the + table; a deadlock or lock timeout against live traffic is retried twice. +2. **Per connection** — the module contexts (EF `RowLevelSecurityInterceptor`) and the Dapper reads + (`OpenWithTenantAsync`, `OpenForTenantAsync`) run `SET ROLE agentos_rls` and set `app.tenant_id` and + `app.platform` from the flow's scope. Npgsql resets session state when a pooled connection returns. Every + Dapper read now goes through this switch (stage 1 left several on the login user, which an owner login would + see nothing through once "no tenant" means nothing). +3. **The scope** of a flow, innermost first: + - `RowLevelSecurity.PlatformScope(reason)` — every tenant's rows, for work that is cross-tenant by nature. + Counted on `agentos.rls.platform_scope{reason}`. The connection still runs as the restricted role, so the + audit trail stays append-only even here. Uses: `autopilot-scan` (`WorkspaceRepository.ListAutopilotAsync`), + `scheduler` (`EfScheduledRunStore.ClaimDueAsync`), `runner-handshake` (`EfRunnerDirectory.FindForPairingAsync` + — the token is verified against the returned hash afterwards), `runner-revalidation` + (`EfRunnerDirectory.FindStatusesAsync`), `platform-settings` (the desktop's `SettingsFields` around a + platform-scope write, after re-checking the signed-in principal's `platform-admin` role — `SettingsService` + never enters the scope itself and refuses a platform write outside one), `audit-platform` (the `_platform` + audit chain); + - `RowLevelSecurity.TenantScope(tenant)` — binds the flow to a tenant it learned from a trusted source other than + the caller: a signed invitation token (sign-up is anonymous), a verified runner row (the rest of the hub + handshake), an audit entry's own tenant (the audit writer), a tenant export / purge; + - `RowLevelSecurity.TenantScopeIfUnbound(tenant)` — an explicit tenant **fills** a flow that has none (a + background worker, the runner hub) and never overrides a flow's own: used by tenant-explicit stores + (`EfAppConfigStore`, `EfApprovalStore`, the Dapper repositories, the evidence drain, the autopilot log, preview re-attach). A flow + bound to tenant A that names tenant B still sees nothing of B; + - otherwise the ambient identity (runs, Board sessions, the scheduler's run; every Blazor circuit event through + `CircuitIdentityHandler`), else the authenticated HTTP request's `tenant` claim, else **none — nothing**. +4. **Tables** — `RowLevelSecurity.Tables` is the single list (20): + - `pipeline.pipeline_runs`, `run_metrics`, `orchestrations`, `scheduled_runs`; + - `sessions.sessions`, `session_repos`, `runners`, `ticket_files`, `design_reviews`, `approvals` (E1.5); - `tools.tool_invocations`; - - `workspaces.workspaces`, `workspace_repos`. -5. If the bootstrap fails (missing privileges), the host still starts, RLS stays off and an error is logged; - application filters still apply. `Persistence:RowLevelSecurity=false` is the escape hatch. + - `workspaces.workspaces`, `workspace_repos`, `autopilot_events`, `ticket_flows`; + - `memory.nodes`, `memory.edges`; + - `config.app_config` (stage 2), `tenants.audit_events` (stage 2), `tenants.invitations` (stage 2, GAP-11). -**Stage 2 (next):** - -- bring `config.app_config` and `tenants.audit_events` under RLS, with explicit platform-row rules; -- tighten "no tenant ⇒ visible" to "no tenant ⇒ nothing" once every remaining path (hub, drains) pushes a - tenant or runs under an explicit, audited bypass role. + `RowLevelSecurity.ExemptTables` lists the tables with a `TenantId` column deliberately outside RLS, with the + reason: `tenants.api_tokens` (looked up by prefix while authenticating, before any tenant is known; EF filter + + explicit predicates, only the SHA-256 of a token is stored). A real-Postgres test fails on a tenant table that is + in neither list. `RowLevelSecurityDocsTests` reads this list back and fails when it and the code diverge. +5. **Before the bootstrap ran** (boot: migrations, module seeds) **and when RLS is off**, connections keep the login + user and set `app.platform = 'on'`, so policies an earlier boot left behind never hide rows from a process that + runs without the database wall on purpose (a forced policy binds an owner login). +6. **Failure is not silent.** `RowLevelSecurity.EnsureAtStartupAsync` (called by both hosts through + `UseAgentOsRowLevelSecurityAsync`): outside Development a failed bootstrap **stops the host** with a + plain-language `RowLevelSecurityBootstrapException`; in Development it logs an error and runs on application + filters only. `Persistence:RowLevelSecurity=false` remains the deliberate escape hatch (logged as a warning on + every boot). The `row-level-security` readiness check reports Healthy when bound, **Degraded** when turned off + (readiness still passes; `agentos_health_status` shows 0.5) and Unhealthy when it failed. ## Consequences -- A query that forgets its tenant predicate, inside a request, run or desktop event, returns only the - caller's rows. Writing a row for another tenant fails with `42501`. Both are proven on real Postgres by - `RowLevelSecurityRealDbTests`, and the two-tenant UI test still passes. -- Every connection open costs one extra round-trip (`SET ROLE` + `set_config`). -- Stage 1 is permissive when no tenant is known. That is a deliberate step, recorded here and closed by stage 2. -- The circuit identity push also makes ambient-tenant lookups (settings, stores) correct in the desktop - without hand-plumbing. Audit "who" columns use the pushed display name. -- **Operations:** the database user needs `CREATEROLE` (or a DBA pre-creates `agentos_rls` and grants it). - Azure Flexible Server's admin user has it. +- A query that forgets its tenant predicate, inside a request, run or desktop event, returns only the caller's + rows; writing a row for another tenant fails with `42501`; and a connection that lost its tenant returns + **nothing** instead of everything — a missed push fails closed. `agentos.rls.connections{scope}` counts bound + connections by `tenant` / `platform` / `none`; a rising `none` points at a path that lost its tenant. +- Proven on real Postgres: `RowLevelSecurityStageTwoRealPostgresTests` (every table in the list, catalog-driven + seeding: tenant, none, platform, cross-tenant writes, append-only audit, shared platform settings, a non-superuser + owner bound by FORCE), `RowLevelSecurityEnabledPathsRealPostgresTests` (the platform scans, Dapper reads, evidence + drain and app_config through the real module code with RLS switched on), `AuditAndInvitationsRealPostgresTests`, + and the E2E `RowLevelSecurityRealDbTests` on the running stack. +- Every connection open costs one extra round-trip (`SET ROLE` + two `set_config`). +- RLS binds sessions against **forgotten predicates**; it is not a boundary against SQL injection (an injected + statement could set the session settings). Platform scopes are counted, not audited per call. +- **Upgrade from stage 1 is a cut-over, not a rolling deploy.** The bootstrap replaces the policies as soon as the + first stage-2 replica boots; stage-1 replicas still running then see nothing on their tenant-less paths and write + unchained audit rows. Stop them first (docs/ONPREM.md §4, docs/DEPLOYMENT.md A.4, CHANGELOG migration notes). +- **Operations:** the database user needs `CREATEROLE` (or a DBA pre-creates `agentos_rls` and grants it). Azure + Flexible Server's admin user has it. A user without it now stops the host outside Development instead of running + without RLS — grant it, or set `Persistence:RowLevelSecurity=false` knowingly. +- The audit trail is append-only for the application; the tenant purge (a platform-admin erasure, recorded on the + `_platform` chain) deletes as the login user, not the restricted role. diff --git a/docs/adr/README.md b/docs/adr/README.md index 7ddfeacc..79343ca5 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -11,7 +11,7 @@ Format: Status · Context · Decision · Consequences. Keep them terse. | [0005](0005-build-verifier-sandbox.md) | `build_verifier` execution sandbox | Accepted | | [0008](0008-memory-and-knowledge-graph.md) | Shared memory and knowledge graph (layers, bi-temporal graph in Postgres, retrieval, visualisation) | Proposed | | [0011](0011-ui-first-configuration.md) | UI-first configuration: settings registry, scopes, precedence | Accepted | -| [0012](0012-postgres-row-level-security.md) | Postgres row-level security for tenant tables (stage 1) | Accepted | +| [0012](0012-postgres-row-level-security.md) | Postgres row-level security for tenant tables (stage 2: no tenant sees nothing) | Accepted | | [0013](0013-teams-and-projects.md) | Teams and projects inside a tenant (org → team → project → repo) | Proposed | | [0014](0014-ticket-flow-is-data.md) | Ticket flow is data: Autopilot runs what the Workflow app shows | Accepted | | [0015](0015-ticket-files.md) | Ticket files: the only way files reach an agent | Accepted | diff --git a/docs/plans/enterprise-readiness.md b/docs/plans/enterprise-readiness.md index 97594bce..45d3b51f 100644 --- a/docs/plans/enterprise-readiness.md +++ b/docs/plans/enterprise-readiness.md @@ -40,6 +40,7 @@ real services: | Item | Shipped | Proof | |---|---|---| | E0.1 Postgres RLS (stage 1) | #205, [ADR-0012](../adr/0012-postgres-row-level-security.md) | Real-DB test: predicate-less query sees only its tenant, cross-tenant write → 42501; two-tenant UI test green with RLS on | +| E0.1 RLS stage 2 + GAP-11 / 11b 🟡 | #326, [ADR-0012](../adr/0012-postgres-row-level-security.md) | Shipped, full-stack two-tenant pass pending. Real Postgres: every RLS table — no tenant sees nothing, tenant sees its own, platform scope all, a non-superuser owner bound by FORCE; tenant-less paths (scheduler, autopilot, runner handshake, evidence drain, app_config) through the real code with RLS on; audit append-only + hash chain under concurrent writers; single-use invitations under concurrent accepts. Desktop (DB-backed run): invite → used / revoked, Verify chain intact (light + dark) | | E0.2 key-ring wrapping | #203 | Real certificate: key file holds `encryptedSecret`, round-trip OK; About shows protection | | E0.3 evidence never dropped silently | #199 | Flood test: 10/10 persisted (6 inline), lost counter + metric | | E0.4 Human node fails closed | #196 | Headless run stops at the checkpoint | @@ -140,7 +141,7 @@ behaviour, unit + E2E, real integrations verified on the lab, docs updated in th | PR | Change | |---|---| | **E2.1** `feat(identity)` | SCIM 2.0 (users + groups → roles/tenants), deprovision revokes sessions + runner tokens | -| **E2.2** `feat(audit)` | Tamper-evident audit (hash chain + periodic anchor), SIEM export (OTLP logs / syslog / webhook), audit of agent actions (tool calls, PRs) in the same trail | +| **E2.2** `feat(audit)` | Tamper-evident audit (~~hash chain~~ shipped with GAP-11b + periodic anchor), SIEM export (OTLP logs / syslog / webhook), audit of agent actions (tool calls, PRs) in the same trail | | **E2.3** `feat(ops)` | HA: ≥2 Web replicas (SignalR backplane or sticky + documented), HA Postgres, geo-redundant backups, an **executed** DR drill with timings | | **E2.4** `feat(integration)` | GitLab (SCM + issues), Bitbucket, inbound/outbound webhooks (= plan Phase 7) | | **E2.5** `feat(agents)` | Tenant-defined custom agents (role, prompt, tools, model) usable in Workflow + Board; per-agent tool allowlists | diff --git a/docs/security/data-flow.md b/docs/security/data-flow.md index 1025ae64..d34a0032 100644 --- a/docs/security/data-flow.md +++ b/docs/security/data-flow.md @@ -115,7 +115,7 @@ sequenceDiagram | **TB1** Browser ↔ Web | both | UI events, rendered diffs, ticket text, code previews, uploaded ticket files, tenant export / audit CSV downloads | TLS at the ingress (`allowInsecure: false`); circuit over the same TLS | Auth cookie (HttpOnly, Secure, Lax) holds the OIDC tokens encrypted by DataProtection | `src/AgentOs.Web/Program.cs:139-187`; `infra/AgentOs.AppHost/infra/web.tmpl.yaml:15-19` | | **TB2** Browser / Web ↔ Keycloak | both | Credentials (typed at Keycloak only), authorization code, ID / access / refresh tokens, `tenant` + realm roles claims | TLS; code flow + PKCE; PAR disabled | Keycloak DB (Postgres in the cloud) | `src/AgentOs.Web/Program.cs:164-175`; `infra/keycloak/agentic-realm.json` | | **TB2** Api ↔ Keycloak | Api → KC | JWKS / discovery fetch; Keycloak admin calls (create user, reset password, delete) from the Tenants module | TLS (`RequireHttpsMetadata` default on) | — | `src/AgentOs.Modules.Identity/Auth/JwtAuthExtensions.cs:29-70`; `src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminClient.cs` | -| **TB3** Hosts ↔ Postgres | both | Every persisted row: runs, metrics, evidence, sessions, ticket file bytes, memory, workspaces, audit, encrypted settings, key ring | TLS only by server default (no `SslMode` in the app connection string) | RLS on 16 tables (`agentos_rls`); DataProtection on `app_config`; Azure-managed storage encryption | `src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs`; `infra/postgres/postgres.module.bicep` | +| **TB3** Hosts ↔ Postgres | both | Every persisted row: runs, metrics, evidence, sessions, ticket file bytes, memory, workspaces, audit, encrypted settings, key ring | TLS only by server default (no `SslMode` in the app connection string) | RLS on 20 tables (`agentos_rls`; no tenant ⇒ nothing; audit trail append-only + hash-chained); DataProtection on `app_config`; Azure-managed storage encryption | `src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs`; `infra/postgres/postgres.module.bicep` | | **TB4** Server → runner | push | System + user prompt (ticket text, rules, practices, file list), model id, CLI name, autonomy mode, file manifest + bytes, `shell` / `write_files` tool calls, Cancel | TLS when the hub URL is `https://` (not enforced) | Per-run folder, deleted after the run; `write_files` folders after 3 days; **no vendor key, no PAT sent** | `src/AgentOs.Modules.RemoteAgent/IRemoteAgentBroker.cs:35-68`; `src/AgentOs.RemoteAgent/ScopedAutonomy.cs:118-157` | | **TB4** Runner → server | push | Result text (CLI final answer, stderr on failure), token usage, progress step keys, tool output (≤ 32 KB), preview state, Design-gate screenshots (`UploadRunFile`, [ADR-0016](../adr/0016-design-gate.md)) | same | Stored in `sessions.*` error/outcome columns, broadcast on the tenant's session feed, unredacted | `src/AgentOs.RemoteAgent/Program.cs:327-380`; `src/AgentOs.Domain/Sessions/InMemorySessionRunFeed.cs` | | **TB4** Runner CLI → vendor / SCM | runner → third party | Whatever the member's CLI sends under the member's own account (prompt, repo content it reads) | Vendor TLS | Member's own vendor terms; outside AgentOS.NET | `src/AgentOs.Modules.RemoteAgent/RemoteAgentLlmClient.cs:1-3` | diff --git a/docs/security/hardening.md b/docs/security/hardening.md index 18e3ab2a..8e5eba93 100644 --- a/docs/security/hardening.md +++ b/docs/security/hardening.md @@ -73,11 +73,14 @@ Tick every box before exposing a deployment to people outside the team that oper ## 4. Database and row-level security - [ ] **RLS is on.** Leave `Persistence:RowLevelSecurity=true` (default). The DB user needs `CREATEROLE`, or a DBA - pre-creates `agentos_rls` ([ADR-0012](../adr/0012-postgres-row-level-security.md)). After first boot, confirm - the log has **no** RLS bootstrap error — on failure the host keeps running with RLS off - (`src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs:132-136`). **(manual check — GAP-14)** + pre-creates `agentos_rls` ([ADR-0012](../adr/0012-postgres-row-level-security.md)). Outside Development a failed + bootstrap stops the host (GAP-14); `/health` shows `row-level-security` **Healthy** — **Degraded** means someone + set the escape hatch `Persistence:RowLevelSecurity=false`. - [ ] Verify in SQL: `select relname, relrowsecurity, relforcerowsecurity from pg_class where relrowsecurity;` - lists the 16 tables in `RowLevelSecurity.Tables` (`RowLevelSecurity.cs:39-47`). + lists the 20 tables in `RowLevelSecurity.Tables`, and + `select has_table_privilege('agentos_rls', 'tenants.audit_events', 'DELETE');` is `false` (append-only, GAP-11b). +- [ ] Record the audit chain's head hash outside the database now and then (Settings › Activity log › Verify chain + shows it): without an external anchor, a database owner who rewrites the newest events is not detected. - [ ] **Least-privilege app login.** The shipped deployment connects as the server administrator (`postgres.module.bicep:78`). Prefer a dedicated login that owns the schemas, has `CREATEROLE`, and is not a superuser. **(manual — GAP-15)** @@ -161,7 +164,9 @@ Tick every box before exposing a deployment to people outside the team that oper **(GAP-15)** - [ ] Set a retention on the Log Analytics workspace (none set in `infra/resources.bicep:37-46`). - [ ] Configure real SMTP — the null sender (no `Email:SmtpHost`) logs invitation links including the token - (`src/AgentOs.Modules.Tenants/Email/NullEmailSender.cs:21-26`). Use STARTTLS (`Email__UseStartTls=true`) or 465. + (`src/AgentOs.Modules.Tenants/Email/NullEmailSender.cs:21-26`). A link works once and only for the invited + address, and can be revoked in Settings › Users (GAP-11), but treat it as a credential until then. Use STARTTLS + (`Email__UseStartTls=true`) or 465. - [ ] Tool evidence stores full tool input / output (`tools.tool_invocations`); restrict database read access and watch `agentos.evidence.lost` / `agentos.evidence.inline_writes` (meter `AgentOs.Tools`). diff --git a/docs/security/threat-model.md b/docs/security/threat-model.md index 2edfade0..80663dda 100644 --- a/docs/security/threat-model.md +++ b/docs/security/threat-model.md @@ -83,16 +83,16 @@ Diagrams of these boundaries and the data that crosses them are in [data-flow.md | K-4 | E | Role or tenant claim forged / mis-mapped | Roles flattened from `realm_access.roles` (`JwtAuthExtensions.cs:119-148`); tenant from the `tenant` claim, authenticated user without the claim gets `""` (fail-closed), anonymous gets `default` (`src/AgentOs.Modules.Identity/HttpTenantContext.cs:36-37`); the `tenant` user attribute is admin-editable only (`infra/keycloak/agentic-realm.json:35`); policies `Admin` / `Member` / `PlatformAdmin` (`src/AgentOs.Modules.Identity/IdentityModule.cs:23-25`) | ~~No fallback authorization policy~~ — **Fixed (GAP-13):** `FallbackPolicy = RequireAuthenticatedUser` (`IdentityModule.cs`); public routes opt out with an explicit `AllowAnonymous`, and `HostSecurityTests` pins the reviewed anonymous inventory of both hosts. Low | | K-5 | S / E | Account takeover at the IdP | Keycloak handles credentials; `verifyEmail: true`, `sslRequired: external`, direct access grants off for the web client (`infra/keycloak/agentic-realm.json:11,14,81`) | Realm has **no brute-force protection, no password policy, no MFA / OTP policy, no identity providers** (keys absent from `infra/keycloak/agentic-realm.json`); `registrationAllowed: true` (`:7`); seeded `operator`/`operator` (admin + platform-admin) and `member`/`member` (`:147-171`) — the [Keycloak runbook](../keycloak-prod-runbook.md) strips them, nothing enforces it. High — **GAP-03** | | K-6 | E | Anyone on the internet creates a tenant and becomes its admin | Password ≥ 12 chars, 4 classes (`src/AgentOs.Modules.Tenants/SignupValidation.cs:30-49`); Keycloak user rolled back if the DB write fails (`TenantSignupService.cs:111-142`); signup audited | ~~Open self-signup on by default~~ — **fixed (GAP-01)**: creating a new workspace needs the platform setting `Tenants:OpenSignup`, which is off by default outside Development; `TenantSignupService` refuses slug / auto-create with `SignupClosedException` (Api → 403) and fails closed without a settings registry; `/signup` shows "Sign-up is closed" and `/login` hides "Create one"; invitations are unaffected (`TenantsSettings.cs`; `OpenSignupAndAzureEndpointTests`). Residual when an operator turns it **on**: still no CAPTCHA, no Web rate limit, no email required (no email ⇒ `emailVerified=true`, `KeycloakAdminClient.cs:62-66`), and the caller becomes **admin** of the new tenant; this closes signup on AgentOS.NET's own routes (`/signup`, `POST /tenants/register`) only — Keycloak realm self-registration (`registrationAllowed`) is a separate door, tracked under GAP-03. Medium | -| K-7 | S / E | Invitation token reuse | Stateless `ITimeLimitedDataProtector` token, purpose-scoped, TTL 1-168 h (default 24) (`TenantSignupService.cs:22,42,159-169`; `TenantEndpoints.cs:146-151`) | Tokens are **not single-use, not revocable** and **not bound to the invited email** (`TenantSignupService.cs:66-75`); preview is anonymous. Medium — **GAP-11** | +| K-7 | S / E | Invitation token reuse | An invitation is a **row** (`tenants.invitations`, under RLS); the emailed token is a time-limited DataProtection envelope around its id + tenant only (purpose `…Invitation.v2`; TTL 1-168 h, default 24) (`TenantSignupService.cs`; `Invitations/InvitationStore.cs`) | ~~Tokens are not single-use, not revocable and not bound to the invited email~~ — **fixed (GAP-11)**: accepting **claims** the row with one conditional `UPDATE … WHERE not used AND not revoked AND not expired`, so a second use, a revoke or the expiry can't race it (a failed account creation releases the claim); the address is **required** when minting and an accept succeeds only for that address, case-insensitive with surrounding whitespace ignored (`InvitationRules`); an admin lists and **revokes** unused invitations in Settings › Users (`POST /tenants/{id}/invitations/{invitationId}/revoke`); minted / accepted / revoked / **refused** attempts are audited; pre-upgrade (v1, stateless) links no longer open (`InvitationRulesTests`, `TenantSignupServiceTests`, `AuditAndInvitationsRealPostgresTests`, E2E `InvitationsTests`). Residual: preview is anonymous by design (the link holder sees the tenant, role and invited address); a leaked link can still be used by whoever controls the invited mailbox — Keycloak's `verifyEmail` blocks sign-in until the address is confirmed. Low | | K-8 | R | Sign-in not attributable | Sign-in and member lifecycle land in the audit log (SECURITY.md "Audit trail") | Signup audit has no IP / user agent (`TenantSignupService.cs:144-154`). Low | ## 5. TB3 — Api / Web ↔ Postgres | ID | S T R I D E | Threat | Existing mitigation | Residual risk / gap | |---|---|---|---|---| -| D-1 | I / T | A query or write reaches another tenant's rows | Three layers: EF global query filters (`PipelineDbContext.cs:54-99`, `SessionsDbContext.cs:51-114`, `WorkspacesDbContext.cs:55-112`, `MemoryDbContext.cs:83,94`); explicit `TenantId` predicates; **Postgres RLS** forced on 16 tables (`src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs:39-47`) with every connection `SET ROLE agentos_rls` + `app.tenant_id` (`:88`) via `RowLevelSecurityInterceptor` / `OpenWithTenantAsync`. Proven by `tests/AgentOs.E2E.Tests/AgentOsUi/RowLevelSecurityRealDbTests.cs` ([ADR-0012](../adr/0012-postgres-row-level-security.md)) | Policy is still **"no tenant ⇒ visible"** (`RowLevelSecurity.cs:161`) — stage 2 pending. `config.app_config` and `tenants.audit_events` are **not** under RLS (`tests/AgentOs.Tests/Persistence/RowLevelSecurityTests.cs:37-40`). The real-DB test lists 11 of the 16 tables (missing `pipeline.scheduled_runs`, `workspaces.autopilot_events`, `memory.nodes`, `memory.edges`, `sessions.design_reviews`). Most repository reads use `IgnoreQueryFilters()` + explicit predicates, so RLS is the real backstop. Medium — **GAP-14** | -| D-2 | E | RLS silently off | Bootstrap under an advisory lock, idempotent (`RowLevelSecurity.cs:120-178`) | Bootstrap failure **logs an error and keeps running with RLS off** (`:131-135`); `Persistence:RowLevelSecurity=false` disables it (`src/AgentOs.Api/Program.cs:146`, `src/AgentOs.Web/Program.cs:319`). No health signal fails the deployment. Medium — **GAP-14** | -| D-3 | E | Cross-tenant bypass misused | One explicit `RowLevelSecurity.Bypass()` call, for the autopilot cross-tenant scan (`src/AgentOs.Modules.Workspaces/Persistence/Repositories/WorkspaceRepository.cs:125`); tenant export/purge use explicit `TenantId` parameters on catalog-quoted identifiers (`src/AgentOs.Modules.Tenants/Data/TenantDataService.cs:55-157`) | Bypass is not audited. Low | +| D-1 | I / T | A query or write reaches another tenant's rows | Three layers: EF global query filters (`PipelineDbContext.cs:54-99`, `SessionsDbContext.cs:51-114`, `WorkspacesDbContext.cs:55-112`, `MemoryDbContext.cs:83,94`); explicit `TenantId` predicates; **Postgres RLS** forced on the 20 tables of `RowLevelSecurity.Tables` with every connection — EF and Dapper — `SET ROLE agentos_rls` + `app.tenant_id` / `app.platform` via `RowLevelSecurityInterceptor` / `OpenWithTenantAsync` ([ADR-0012](../adr/0012-postgres-row-level-security.md)) | ~~Policy "no tenant ⇒ visible"; `config.app_config` and `tenants.audit_events` outside RLS; the real-DB test lists 11 of 16 tables~~ — **fixed (GAP-14, stage 2)**: an empty or unset tenant matches **no row** (`NULLIF`); every tenant-less path pushes its tenant or runs in an explicit `PlatformScope(reason)` (scheduler, autopilot scan, runner handshake + revalidation, platform settings, `_platform` audit — counted on `agentos.rls.platform_scope`); `app_config` (+ a read-only policy sharing the `__platform` rows), `audit_events` and `invitations` are under RLS; `tenants.api_tokens` is exempt with its reason (`RowLevelSecurity.ExemptTables`); a real-Postgres test covers **every** listed table and fails on a tenant table in neither list (`RowLevelSecurityStageTwoRealPostgresTests`, `RowLevelSecurityEnabledPathsRealPostgresTests`, E2E `RowLevelSecurityRealDbTests`). Residual: RLS binds against forgotten predicates, not SQL injection (an injected statement could set the session settings). Low | +| D-2 | E | RLS silently off | Bootstrap under an advisory lock, idempotent, one transaction per table, deadlock / lock timeout retried (`RowLevelSecurity.EnsureAsync`) | ~~Bootstrap failure logs an error and keeps running with RLS off; no health signal~~ — **fixed (GAP-14)**: outside Development a failed bootstrap **stops the host** (`RowLevelSecurityBootstrapException`, `EnsureAtStartupAsync`, both hosts via `UseAgentOsRowLevelSecurityAsync`); the `row-level-security` readiness check reports it (Healthy / **Degraded** when turned off / Unhealthy), so `agentos_health_status` alerts. Residual: `Persistence:RowLevelSecurity=false` is still an operator escape hatch — logged as a warning every boot and Degraded on `/health`, not refused. Low | +| D-3 | E | Cross-tenant bypass misused | ~~`RowLevelSecurity.Bypass()`~~ replaced by `RowLevelSecurity.PlatformScope(reason)`: the connection still runs as the restricted role (the audit trail stays append-only), and every scope is counted by reason (`agentos.rls.platform_scope`); six call sites, each cross-tenant by nature (ADR-0012 §3); tenant export/purge bind their own connection to the one tenant they act on (`TenantDataService.OpenForTenantAsync`) | Platform scopes are counted, not written to the audit trail per call. Low | | D-4 | I | Database credentials / traffic exposed | Connection strings stored as Key Vault secrets (`infra/postgres/postgres.module.bicep:67-89`); Keycloak JDBC uses `sslmode=require` (`infra/AgentOs.AppHost/Program.cs:248`) | App connection strings carry **no `SslMode`** (relies on the Azure server default); the app connects as the **server administrator** (`postgres.module.bicep:78`); firewall `AllowAllAzureIps` (`:44-51`); password also stored as a plain ACA secret (`web.tmpl.yaml:28-31`). Medium — **GAP-15** | | D-5 | D / R | Data loss, no recovery | Azure Flexible Server automated backups, 7-day retention (`postgres.module.bicep:22-25`); [DataProtection DR runbook](../dataprotection-dr-runbook.md) | `geoRedundantBackup: 'Disabled'`, HA disabled, single replica (`minReplicas: 1`, `web.tmpl.yaml:103`); no retention policy for runs / evidence / audit (searched `retention|Prune|Purge`). Medium — **GAP-16** | | D-6 | I | Tenant export leaks secrets | Export is admin-only for the own tenant, platform-admin for another, audited (`src/AgentOs.Web/Program.cs:442-456`); `sessions.runners.TokenHash` removed | ~~The redaction map named `Value` for `config.app_config` (the column is `EncryptedValue`) and `tenants.api_tokens.TokenHash` was exported~~ — **fixed (GAP-08)**: the map names `config.app_config.EncryptedValue`, `sessions.runners.TokenHash` and `tenants.api_tokens.TokenHash`, is checked against the catalog on every export (a missing mapped column fails the export closed), any column named `…TokenHash` / `…PasswordHash` / `…EncryptedValue` is dropped too (a narrow rule, so ordinary data such as a content or commit hash is still exported; every dropped column is logged), a refused export returns a plain-language **409** and is not audited as an export, and the keys go in as a `text[]` parameter (`TenantDataService.RedactedColumns`; `TenantExportRedactionTests` exports seeded secrets from real Postgres and asserts they are absent). Residual: an export is still the tenant's full business data — treat it as sensitive. Low | @@ -181,7 +181,7 @@ instruction files are written by people (or agents) other than the operator. |---|---|---|---|---| | MT-1 | E | Tenant admin acts on another tenant | Route `tenantId` must equal the caller's tenant on every tenant-admin route, target user's tenant attribute verified (`src/AgentOs.Modules.Tenants/Endpoints/TenantEndpoints.cs:142-455`); cross-tenant list / provision require `PlatformAdmin` (`:37-94`); tenant-scope writes to platform settings refused (`src/AgentOs.Modules.AppConfig/Settings/SettingsService.cs:92-95`) | None known | | MT-2 | I | Cache or feed bleeds across tenants | AppConfig cache keyed `tenant:key` (`EfAppConfigStore.cs:25,159`); session feed per tenant (`InMemorySessionRunFeed.cs`); per-circuit services are scoped | Session feed is per tenant, not per member (R-8). Low | -| MT-3 | R | Audit trail altered | Append-only by API (`src/AgentOs.Modules.Tenants/IAuditLog.cs:13-20`); formula-safe CSV export (`AuditCsv.cs:34-46`); platform-scope rows visible to platform admins only | **No DB-level protection** (the `agentos_rls` role has `UPDATE/DELETE` on `tenants`), **no hash chain**, writes are best-effort (exceptions swallowed, `AuditLog.cs:42-53`), tenant purge deletes the tenant's audit rows (`TenantDataService.cs:99-105`). Medium — **GAP-11b** (E2.2) | +| MT-3 | R | Audit trail altered | Append-only by API (`src/AgentOs.Modules.Tenants/IAuditLog.cs`); formula-safe CSV export (`AuditCsv.cs:34-46`); platform-scope rows visible to platform admins only | ~~No DB-level protection, no hash chain~~ — **fixed (GAP-11b)**: the app role may only `INSERT` and `SELECT` audit rows (`REVOKE UPDATE, DELETE, TRUNCATE` in the RLS bootstrap, `RowLevelSecurity.AppendOnlyTables`), the table is under RLS, and each tenant's events (and `_platform`'s) form a **SHA-256 hash chain** — `Sequence`, `PrevHash`, `Hash` over a length-prefixed encoding, writers serialised per tenant (transaction advisory lock + unique `(TenantId, Sequence)`). **Verify chain** in Settings › Activity log (and `GET /tenants/{id}/audit/verify`) walks it and says "intact" or names the first changed / missing event, or a row the app role inserted without a chain link after the chain started (`AuditChain`, `AuditChainTests`, `AuditAndInvitationsRealPostgresTests`). Residual: the database **owner** can still rewrite an event together with every later hash, or delete the newest events — not detectable without an external anchor (record the head hash the verifier shows; E2.2 periodic anchor); events recorded before the upgrade are not chained (reported as such), and an unchained row back-dated to before the chain's first event reads as such history; writes are still best-effort (`AuditLog.cs`, GAP-09); the tenant purge (an audited platform-admin erasure) deletes the tenant's whole chain. Low | ## 14. STRIDE per module (summary) @@ -224,9 +224,9 @@ internet-facing pilot**. | 8 | ~~**GAP-02 / GAP-12** Web host: no global per-tenant rate limit; no HSTS / CSP / `frame-ancestors` / `nosniff` on either host~~ — **Fixed**: one limiter for both hosts in `ServiceDefaults` (per tenant + per user, anonymous per IP; platform settings; circuit transport, health and static assets exempt) and a security-headers middleware with an enforced CSP the desktop runs under (`script-src 'self'`, `frame-ancestors 'none'`; documented exceptions: `style-src 'unsafe-inline'`, `https://rsms.me` webfont, Scalar's inline boot on the dev-only docs path) | ~~Medium~~ | `RequestRateLimiting.cs`; `SecurityHeaders.cs`; `Web/Program.cs`; `Api/Program.cs` | Done — `RequestRateLimitingTests`, `SecurityHeadersTests`, `HostSecurityTests` | | 9 | **GAP-09** Governance fails open: budget gate proceeds on store errors and blocks only with `budget/enforce=true`; evidence DB failures swallowed before the lost counter | Medium | `AgentBudgetGate.cs:54-69`; `EfToolInvocationLog.cs:60-66` | Fail closed (configurable) on budget errors; let `EfToolInvocationLog` throw so the buffered retry + lost metric see it | | 10 | **GAP-10** Untrusted content handling: exact-match fence escaping for `repo_guidance` / `workspace_rules`, bidi / zero-width chars kept, MCP output not fenced, tool evidence and runner output stored unredacted and broadcast tenant-wide | Medium | `RepoInstructions.cs`; `WorkspaceRules.cs`; `UntrustedText.cs`; `EfToolInvocationLog.cs:44-57`; `InMemorySessionRunFeed.cs` | Reuse `UntrustedText` neutralisation for every fence; strip Unicode `Cf`; fence MCP output; apply `LogSafe.MaskSecrets` before storing evidence / feed text | -| 11 | **GAP-11 / 11b** Invitation tokens reusable, unbound to the email, unrevocable; audit trail not tamper-evident and deletable | Medium | `TenantSignupService.cs:66-75`; `AuditLog.cs`; `RowLevelSecurity.cs:153` | Single-use invitation ids with revocation; bind to email; `REVOKE UPDATE, DELETE` on `audit_events` for the app role + hash chain (E2.2) | +| 11 | ~~**GAP-11 / 11b** Invitation tokens reusable, unbound to the email, unrevocable; audit trail not tamper-evident and deletable~~ — **Fixed**: invitations are single-use rows bound to the invited address and revocable from Settings › Users; the audit trail is append-only for the app role (`REVOKE UPDATE, DELETE, TRUNCATE`) and hash-chained per tenant, with a verifier in the desktop. **Still open:** an external anchor for the chain head (E2.2) | ~~Medium~~ Low | `TenantSignupService.cs`; `Invitations/`; `AuditChain.cs`; `AuditLog.cs`; `RowLevelSecurity.AppendOnlyTables` | Done — `InvitationRulesTests`, `TenantSignupServiceTests`, `AuditChainTests`, `AuditAndInvitationsRealPostgresTests`, E2E `InvitationsTests`; periodic anchor (E2.2) | | 12 | ~~**GAP-13** No fallback authorization policy; JWT algorithm not pinned; `/health/llm` anonymous~~ — **Fixed**: `FallbackPolicy = RequireAuthenticatedUser` with explicit `AllowAnonymous` on health, `/version`, OIDC login/logout, sign-in/sign-up pages, invitation preview + register, runner download/version, pairing exchange, the self-authenticating runner hub, Blazor's `/_blazor` circuit endpoints and static assets; RS256 pinned (Api + Web); `/health/llm` signed-in only | ~~Medium~~ | `IdentityModule.cs`; `JwtAuthExtensions.cs`; `Api/Program.cs`; `Web/Program.cs` | Done — `AuthorizationDefaultsTests`, `HostSecurityTests` (anonymous inventory) | -| 13 | **GAP-14** RLS stage 2: "no tenant ⇒ visible", `app_config` / `audit_events` outside RLS, bootstrap failure leaves RLS off silently, real-DB test covers 11/16 tables, ADR lists 9 | Medium | `RowLevelSecurity.cs:132-161`; `RowLevelSecurityRealDbTests.cs:19-24` | Stage 2 per ADR-0012; fail startup outside Development when bootstrap fails; derive the test's table list from `RowLevelSecurity.Tables` | +| 13 | ~~**GAP-14** RLS stage 2: "no tenant ⇒ visible", `app_config` / `audit_events` outside RLS, bootstrap failure leaves RLS off silently, real-DB test covers 11/16 tables, ADR lists 9~~ — **Fixed**: no tenant ⇒ nothing, with counted `PlatformScope(reason)`s for the six cross-tenant operations; `app_config`, `audit_events` and `invitations` under RLS (20 tables, the exemption listed with its reason); a failed bootstrap stops the host outside Development and `/health` reports RLS; the real-DB tests derive their tables from `RowLevelSecurity.Tables`; the ADR's list is the code's | ~~Medium~~ | `RowLevelSecurity.cs`; `RowLevelSecurityStartup.cs`; ADR-0012 | Done — `RowLevelSecurityTests`, `RowLevelSecurityStageTwoRealPostgresTests`, `RowLevelSecurityEnabledPathsRealPostgresTests`, E2E `RowLevelSecurityRealDbTests` | | 14 | **GAP-15** Data-tier and log hygiene: no `SslMode=Require`, app connects as server admin, `AllowAllAzureIps`; console / App Insights logs unmasked; dev email sender logs tokens | Medium | `postgres.module.bicep:44-89`; `SecretMaskingLogProcessor.cs`; `NullEmailSender.cs` | `SslMode=VerifyFull`; a least-privilege app login; private endpoint / VNet; mask at the logger provider level | | 15 | **GAP-16** Availability / DR: geo-redundant backup off, HA off, single replica, no retention policies, no executed DR drill | Medium | `postgres.module.bicep:22-28`; `web.tmpl.yaml:103` | E2.3 + E1.4 retention | | 16 | ~~**GAP-17** Authorization in UI only: `TenantDataService.PurgeAsync` unguarded; Web `?? "default"` tenant fallbacks~~ — **Fixed**: `PurgeAsync(tenantId, ClaimsPrincipal caller)` checks platform-admin (and not the caller's own tenant) itself, before any database / Keycloak / audit call; a missing tenant claim fails closed everywhere in the Web — 403 on `/cost`, `/tenant`, `/audit/export`, a clear message in API tokens / Audit log / Data | ~~Low~~ | `TenantDataService.cs`; `ExportEndpoints.cs`; `ApiTokensApp.razor`, `AuditLogApp.razor`, `DataApp.razor` | Done — `TenantPurgeAuthorizationTests` (incl. a source guard against `?? "default"`), `WebAccountAndExportEndpointTests` | diff --git a/src/AgentOs.Api/Program.cs b/src/AgentOs.Api/Program.cs index 5aedfa69..29da7c7f 100644 --- a/src/AgentOs.Api/Program.cs +++ b/src/AgentOs.Api/Program.cs @@ -113,18 +113,11 @@ await app.Services.InitializeModulesAsync(); -// E0.1 / ADR-0012: Postgres row-level security (stage 1), after every module migrated. Each tenant-table -// connection then switches to the restricted role with the caller's tenant — an authenticated request's -// claim, or the ambient identity a run / desktop circuit pushed. Escape hatch: Persistence:RowLevelSecurity=false. -{ - var httpAccessor = app.Services.GetRequiredService(); - AgentOs.SharedKernel.Persistence.RowLevelSecurity.HttpTenant = () => - httpAccessor.HttpContext?.User is { Identity.IsAuthenticated: true } user ? user.GetTenantId() : null; - if (app.Configuration.GetValue("Persistence:RowLevelSecurity", true)) - { - await AgentOs.SharedKernel.Persistence.RowLevelSecurity.EnsureAsync(app.Configuration.GetConnectionString("DefaultConnection"), app.Logger); - } -} +// E0.1 / ADR-0012 / GAP-14: Postgres row-level security (stage 2), after every module migrated. Each tenant-table +// connection then switches to the restricted role with the caller's tenant — an authenticated request's claim, or the +// ambient identity a run / desktop circuit pushed; no tenant sees nothing. A failed bootstrap stops the host outside +// Development. Escape hatch: Persistence:RowLevelSecurity=false (reported Degraded by /health). +await app.UseAgentOsRowLevelSecurityAsync(); // FIRST middleware: honour X-Forwarded-Proto/For from the Container Apps ingress so auth + link // generation see the original https scheme + client IP. diff --git a/src/AgentOs.Domain/Configuration/Settings.cs b/src/AgentOs.Domain/Configuration/Settings.cs index 2e06383a..322eb04d 100644 --- a/src/AgentOs.Domain/Configuration/Settings.cs +++ b/src/AgentOs.Domain/Configuration/Settings.cs @@ -160,7 +160,10 @@ public interface ISettings /// Every descriptor in (or all when null) with its effective value; secrets masked. ValueTask> ListAsync(string? tenantId, string? category = null, CancellationToken cancellationToken = default); - /// Validate and store at ; empty clears the override. + /// Validate and store at ; empty clears the override. + /// A write is accepted only from a flow that is already a platform operation + /// (the caller verified the platform-admin role and entered the platform RLS scope) — the service never elevates + /// on its own; otherwise it is refused. ValueTask SetAsync( string key, string? value, SettingScope scope, string? tenantId, string actor, CancellationToken cancellationToken = default); } diff --git a/src/AgentOs.Modules.AppConfig/AppConfigModule.cs b/src/AgentOs.Modules.AppConfig/AppConfigModule.cs index 6f4b7c14..fe210f9e 100644 --- a/src/AgentOs.Modules.AppConfig/AppConfigModule.cs +++ b/src/AgentOs.Modules.AppConfig/AppConfigModule.cs @@ -42,9 +42,11 @@ public void AddServices(IServiceCollection services, IConfiguration configuratio var connectionString = configuration.GetConnectionString("DefaultConnection"); if (!string.IsNullOrWhiteSpace(connectionString)) { + // GAP-14: config.app_config is under RLS (ADR-0012 stage 2) — every connection runs as the restricted role. services.AddDbContext(opt => opt.UseNpgsql(connectionString, npg => - npg.MigrationsHistoryTable("__EFMigrationsHistory", schema: "config"))); + npg.MigrationsHistoryTable("__EFMigrationsHistory", schema: "config")) + .AddInterceptors(AgentOs.SharedKernel.Persistence.RowLevelSecurityInterceptor.Instance)); services.AddSingleton(); services.AddSingleton(sp => new AuditingAppConfigStore(sp.GetRequiredService(), sp, sp.GetService())); return; diff --git a/src/AgentOs.Modules.AppConfig/EfAppConfigStore.cs b/src/AgentOs.Modules.AppConfig/EfAppConfigStore.cs index 368b882b..7e65ccf3 100644 --- a/src/AgentOs.Modules.AppConfig/EfAppConfigStore.cs +++ b/src/AgentOs.Modules.AppConfig/EfAppConfigStore.cs @@ -2,6 +2,11 @@ // owns its own LLM keys / GitHub PAT / etc. Singleton; per-op DI scope is created here and the // tenant id is read out of the OUTER request scope via the captured IServiceProvider. A 15-second // read cache keeps the LLM hot path off the DB on every call; cache keys include the tenant. +// RLS (ADR-0012 stage 2, GAP-14): config.app_config is bound like every tenant table. A flow that acts for a tenant +// reads and writes that tenant's rows only (plus the "__platform" rows every tenant may read); a flow with no tenant +// of its own (a background worker, an anonymous request) that names the tenant it works for is bound to that tenant +// for the one call — an explicit tenant FILLS a missing one, it never overrides the flow's own. Platform rows are +// written only inside RowLevelSecurity.PlatformScope (SettingsService does that for platform-scope settings). using System; using System.Collections.Concurrent; @@ -12,6 +17,7 @@ using AgentOs.Modules.AppConfig.Persistence; using AgentOs.Modules.AppConfig.Persistence.Entities; using AgentOs.SharedKernel.Identity; +using AgentOs.SharedKernel.Persistence; using Microsoft.AspNetCore.DataProtection; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; @@ -43,37 +49,45 @@ public EfAppConfigStore(IServiceProvider rootProvider, IDataProtectionProvider d { await using var scope = _rootProvider.CreateAsyncScope(); var tenantId = ResolveTenant(scope.ServiceProvider); - var cacheKey = CacheKey(tenantId, key); - if (_cache.TryGetValue(cacheKey, out var hit) && hit.FetchedUtc + CacheTtl > DateTime.UtcNow) - { - return hit.Value; - } - - var db = scope.ServiceProvider.GetRequiredService(); - var row = await db.AppConfig.AsNoTracking() - .FirstOrDefaultAsync(x => x.TenantId == tenantId && x.Key == key, cancellationToken) - .ConfigureAwait(false); - var value = row is null ? null : TryUnprotect(row.EncryptedValue); - _cache[cacheKey] = new CacheEntry(value, DateTime.UtcNow); - return value; + return TryCached(tenantId, key, out var cached) + ? cached + : await ReadAsync(scope.ServiceProvider, tenantId, key, cancellationToken).ConfigureAwait(false); } /// public async ValueTask GetForTenantAsync(string tenantId, string key, CancellationToken cancellationToken = default) { - var cacheKey = CacheKey(tenantId, key); - if (_cache.TryGetValue(cacheKey, out var hit) && hit.FetchedUtc + CacheTtl > DateTime.UtcNow) + if (TryCached(tenantId, key, out var cached)) { - return hit.Value; + return cached; } - await using var scope = _rootProvider.CreateAsyncScope(); - var db = scope.ServiceProvider.GetRequiredService(); + return await ReadAsync(scope.ServiceProvider, tenantId, key, cancellationToken).ConfigureAwait(false); + } + + private bool TryCached(string tenantId, string key, out string? value) + { + if (Cacheable(tenantId) && _cache.TryGetValue(CacheKey(tenantId, key), out var hit) && hit.FetchedUtc + CacheTtl > DateTime.UtcNow) + { + value = hit.Value; + return true; + } + value = null; + return false; + } + + private async ValueTask ReadAsync(IServiceProvider scoped, string tenantId, string key, CancellationToken cancellationToken) + { + using var bound = BindTenant(tenantId); + var db = scoped.GetRequiredService(); var row = await db.AppConfig.AsNoTracking() .FirstOrDefaultAsync(x => x.TenantId == tenantId && x.Key == key, cancellationToken) .ConfigureAwait(false); var value = row is null ? null : TryUnprotect(row.EncryptedValue); - _cache[cacheKey] = new CacheEntry(value, DateTime.UtcNow); + if (Cacheable(tenantId)) + { + _cache[CacheKey(tenantId, key)] = new CacheEntry(value, DateTime.UtcNow); + } return value; } @@ -88,6 +102,7 @@ public async ValueTask SetForTenantAsync(string tenantId, string key, string val ArgumentException.ThrowIfNullOrEmpty(key); ArgumentNullException.ThrowIfNull(value); + using var bound = BindTenant(tenantId); await using var scope = _rootProvider.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); var cipher = _protector.Protect(value); @@ -115,6 +130,7 @@ public ValueTask DeleteAsync(string key, CancellationToken cancellationToken = d public async ValueTask DeleteForTenantAsync(string tenantId, string key, CancellationToken cancellationToken = default) { ArgumentException.ThrowIfNullOrEmpty(tenantId); + using var bound = BindTenant(tenantId); await using var scope = _rootProvider.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); var row = await db.AppConfig @@ -149,6 +165,7 @@ public async ValueTask> ListAsync(string prefix, Cancellat { await using var scope = _rootProvider.CreateAsyncScope(); var tenantId = ResolveTenant(scope.ServiceProvider); + using var bound = BindTenant(tenantId); var db = scope.ServiceProvider.GetRequiredService(); var keys = await db.AppConfig.AsNoTracking() .Where(x => x.TenantId == tenantId && x.Key.StartsWith(prefix)) @@ -158,6 +175,22 @@ public async ValueTask> ListAsync(string prefix, Cancellat return keys; } + /// Bind a flow that has no tenant of its own to the tenant it names (see the file header); null when the flow + /// already acts for a tenant, is a platform operation, or the rows are the shared platform rows. + internal static IDisposable? BindTenant(string tenantId) + => string.Equals(tenantId, RowLevelSecurity.PlatformSettingsTenant, StringComparison.Ordinal) + ? null + : RowLevelSecurity.TenantScopeIfUnbound(tenantId); + + /// False for a read the database refuses (RLS on, the flow acts for ANOTHER tenant): it must neither be served + /// from the per-process cache (that would hand one tenant's value to another flow) nor pin its refused null there. + internal static bool Cacheable(string tenantId) + => !RowLevelSecurity.Enabled + || RowLevelSecurity.InPlatformScope + || string.Equals(tenantId, RowLevelSecurity.PlatformSettingsTenant, StringComparison.Ordinal) + || RowLevelSecurity.CurrentTenant() is not { } flow + || string.Equals(flow, tenantId, StringComparison.Ordinal); + private static (string Tenant, string Key) CacheKey(string tenantId, string key) => (tenantId, key); private string? TryUnprotect(string cipher) diff --git a/src/AgentOs.Modules.AppConfig/Settings/SettingsService.cs b/src/AgentOs.Modules.AppConfig/Settings/SettingsService.cs index 3c373455..7e19213d 100644 --- a/src/AgentOs.Modules.AppConfig/Settings/SettingsService.cs +++ b/src/AgentOs.Modules.AppConfig/Settings/SettingsService.cs @@ -115,6 +115,15 @@ public async ValueTask SetAsync( return SettingWriteResult.Fail(error); } + // GAP-14: the "__platform" rows are readable by every tenant but writable only inside an explicit platform + // operation (RLS stage 2). This service never opens that scope itself — it has no principal to check, so it would + // be elevating on the caller's word. The surface that verified the platform-admin role enters + // RowLevelSecurity.PlatformScope("platform-settings") around the call; outside one, a platform write is refused. + if (scope == SettingScope.Platform && !AgentOs.SharedKernel.Persistence.RowLevelSecurity.InPlatformScope) + { + return SettingWriteResult.Fail($"{d.Label} is platform-wide; only a platform admin can change it."); + } + var (rowTenant, rowKey) = Row(d.Key, scope, tenantId); var old = await _store.GetForTenantAsync(rowTenant, rowKey, cancellationToken).ConfigureAwait(false); if (string.IsNullOrEmpty(trimmed)) diff --git a/src/AgentOs.Modules.Pipeline/Persistence/Repositories/OrchestrationRepository.cs b/src/AgentOs.Modules.Pipeline/Persistence/Repositories/OrchestrationRepository.cs index 6073b681..0acef5a4 100644 --- a/src/AgentOs.Modules.Pipeline/Persistence/Repositories/OrchestrationRepository.cs +++ b/src/AgentOs.Modules.Pipeline/Persistence/Repositories/OrchestrationRepository.cs @@ -29,8 +29,9 @@ FROM pipeline.orchestrations WHERE "TenantId" = @tenantId ORDER BY "Name" """; - await using var c = Conn.Create(); - await c.OpenAsync(ct).ConfigureAwait(false); + // RLS (ADR-0012 stage 2): Dapper reads switch to the restricted role like the EF context — a plain connection ran as + // the login user, which an owner login (Azure) sees nothing through once "no tenant" means nothing. + await using var c = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var rows = await c.QueryAsync( new CommandDefinition(sql, new { tenantId }, cancellationToken: ct)).ConfigureAwait(false); return rows.AsList(); @@ -43,8 +44,7 @@ ORDER BY "Name" FROM pipeline.orchestrations WHERE "Id" = @id AND "TenantId" = @tenantId """; - await using var c = Conn.Create(); - await c.OpenAsync(ct).ConfigureAwait(false); + await using var c = await Conn.OpenForTenantAsync(tenant.TenantId, ct).ConfigureAwait(false); return await c.QueryFirstOrDefaultAsync( new CommandDefinition(sql, new { id, tenantId = tenant.TenantId }, cancellationToken: ct)).ConfigureAwait(false); } diff --git a/src/AgentOs.Modules.Pipeline/Persistence/Repositories/PipelineRunRepository.cs b/src/AgentOs.Modules.Pipeline/Persistence/Repositories/PipelineRunRepository.cs index eb8f8222..74e2f390 100644 --- a/src/AgentOs.Modules.Pipeline/Persistence/Repositories/PipelineRunRepository.cs +++ b/src/AgentOs.Modules.Pipeline/Persistence/Repositories/PipelineRunRepository.cs @@ -22,9 +22,11 @@ internal sealed class PipelineRunRepository( ITenantContext tenant, INpgsqlConnectionFactory? connectionFactory = null) : IPipelineRunRepository { - // Ambient-tenant write: the API request path has a populated ITenantContext, so stamp from it. + // Ambient-tenant write: the run's pushed identity first (a scheduled run or a circuit has no HttpContext, so + // ITenantContext alone said "default" while RLS was bound to the run's tenant — the save was refused), then the + // request's ITenantContext. public Task SaveAsync(PipelineRunRecord record, CancellationToken ct = default) - => SaveAsync(record, tenant.TenantId, ct); + => SaveAsync(record, AmbientIdentity.Resolve(null, null, tenant).TenantId, ct); // Tenant-explicit write: the caller (e.g. a Blazor circuit, which has a blank ITenantContext) supplies // the owning tenant directly, so the run + its run_metrics rows are billed to the right tenant. diff --git a/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunStores.cs b/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunStores.cs index e6b809f3..91938afa 100644 --- a/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunStores.cs +++ b/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunStores.cs @@ -1,13 +1,15 @@ // IScheduledRunStore implementations. EF (Postgres): tenant-explicit methods use IgnoreQueryFilters() + an explicit // TenantId predicate (the global filter reads ITenantContext, blank off-request), and RLS still binds each // connection to the ambient tenant. ClaimDueAsync is the one deliberate cross-tenant read — the scheduler must see -// every tenant's due rows; it runs with no ambient tenant (RLS stage 1 is permissive then) and each claim is an -// optimistic single-row UPDATE (… WHERE Id = @id AND NextRunAtUtc = @seen), so only one host wins an occurrence. +// every tenant's due rows; it runs inside an explicit RowLevelSecurity.PlatformScope("scheduler") (RLS stage 2: no +// tenant sees nothing) and each claim is an optimistic single-row UPDATE (… WHERE Id = @id AND NextRunAtUtc = @seen), +// so only one host wins an occurrence. // In-memory: the stateless standalone Web (no connection string) — same semantics, process-local. using System.Collections.Concurrent; using AgentOs.Modules.Pipeline.Persistence; using AgentOs.Modules.Pipeline.Persistence.Entities; +using AgentOs.SharedKernel.Persistence; using Microsoft.EntityFrameworkCore; namespace AgentOs.Modules.Pipeline.Scheduling; @@ -75,6 +77,9 @@ await db.ScheduledRuns.IgnoreQueryFilters().Where(x => x.Id == id && x.TenantId public async Task> ClaimDueAsync(DateTimeOffset now, int max, CancellationToken ct = default) { + // Cross-tenant by nature: the scheduler serves every tenant. Disposed before the caller starts any run, so the + // runs themselves execute under their own tenant only. + using var platform = RowLevelSecurity.PlatformScope("scheduler"); var due = await db.ScheduledRuns.IgnoreQueryFilters().AsNoTracking() .Where(x => x.Enabled && x.NextRunAtUtc != null && x.NextRunAtUtc <= now) .OrderBy(x => x.NextRunAtUtc).Take(max).ToListAsync(ct).ConfigureAwait(false); diff --git a/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunWorker.cs b/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunWorker.cs index 2c46165f..20ea8b72 100644 --- a/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunWorker.cs +++ b/src/AgentOs.Modules.Pipeline/Scheduling/ScheduledRunWorker.cs @@ -67,7 +67,9 @@ internal async Task> RunDueAsync(CancellationToken ct) private async Task ExecuteOneAsync(DueRun due, CancellationToken ct) { string status; - using (AmbientIdentity.Push(due.TenantId, due.Run.CreatedBy)) + // The whole occurrence — the run AND its outcome row — acts for the schedule's tenant: with RLS stage 2 a write + // made after the identity is gone would bind to no tenant and touch nothing. + using var identity = AmbientIdentity.Push(due.TenantId, due.Run.CreatedBy); using (var scope = scopes.CreateScope()) { try diff --git a/src/AgentOs.Modules.RemoteAgent/RemoteAgentHub.cs b/src/AgentOs.Modules.RemoteAgent/RemoteAgentHub.cs index a3830931..30e5b815 100644 --- a/src/AgentOs.Modules.RemoteAgent/RemoteAgentHub.cs +++ b/src/AgentOs.Modules.RemoteAgent/RemoteAgentHub.cs @@ -98,10 +98,15 @@ public override async Task OnConnectedAsync() // the documented Pending → Paired transition existed only as a comment. Deliberately NOT // Context.ConnectionAborted — the pairing fact has already happened, and a connection that drops // in the next millisecond must not roll it back. - await _runners.MarkPairedAsync(identity.RunnerId, CancellationToken.None).ConfigureAwait(false); - // Re-pairing the same computer (a new code, a reinstall) used to leave one Offline row — and one still-valid - // token — per pairing on Board › Computers. Its older offline entries retire now. - await _runners.RetireReplacedAsync(identity.RunnerId, _broker.IsConnected, CancellationToken.None).ConfigureAwait(false); + // The token is verified, so the row's tenant is now trusted: the rest of the handshake writes as that tenant (RLS + // stage 2 — a connection with no tenant touches nothing). + using (AgentOs.SharedKernel.Persistence.RowLevelSecurity.TenantScope(identity.TenantId)) + { + await _runners.MarkPairedAsync(identity.RunnerId, CancellationToken.None).ConfigureAwait(false); + // Re-pairing the same computer (a new code, a reinstall) used to leave one Offline row — and one still-valid + // token — per pairing on Board › Computers. Its older offline entries retire now. + await _runners.RetireReplacedAsync(identity.RunnerId, _broker.IsConnected, CancellationToken.None).ConfigureAwait(false); + } await base.OnConnectedAsync().ConfigureAwait(false); } diff --git a/src/AgentOs.Modules.Sessions/Persistence/Repositories/ApprovalStores.cs b/src/AgentOs.Modules.Sessions/Persistence/Repositories/ApprovalStores.cs index 83286316..cd89abc5 100644 --- a/src/AgentOs.Modules.Sessions/Persistence/Repositories/ApprovalStores.cs +++ b/src/AgentOs.Modules.Sessions/Persistence/Repositories/ApprovalStores.cs @@ -1,6 +1,9 @@ // Approval policies (E1.5) — the two stores of approval requests: EF Core over sessions.approvals, and a process-local map // for the no-database path (standalone Development, its E2E). Every EF query is tenant-explicit (IgnoreQueryFilters + a // TenantId predicate — a background pass or a Blazor circuit has no ambient HTTP tenant), with RLS as the second wall. +// RLS stage 2 (ADR-0012): a flow with no tenant of its own (the Autopilot pass, a circuit event that lost its identity) +// sees NOTHING, so every EF path binds the flow to the tenant it names — TenantScopeIfUnbound, which fills a missing +// tenant and never overrides the flow's own (a flow bound to tenant B that names A still sees nothing of A). // A decision is a narrow conditional write: only a PENDING request changes, so two approvers clicking at once (or a // stale page) can never flip a decision that was already made. @@ -12,6 +15,7 @@ using System.Threading.Tasks; using AgentOs.Domain.Governance; using AgentOs.Modules.Sessions.Persistence.Entities; +using AgentOs.SharedKernel.Persistence; using Microsoft.EntityFrameworkCore; namespace AgentOs.Modules.Sessions.Persistence.Repositories; @@ -72,6 +76,7 @@ internal sealed class EfApprovalStore(SessionsDbContext db) : IApprovalStore public async Task GetAsync(string tenantId, Guid sessionId, ApprovalGate gate, int run, CancellationToken ct = default) { + using var bound = RowLevelSecurity.TenantScopeIfUnbound(tenantId); var name = gate.ToString(); var row = await Of(tenantId).AsNoTracking().FirstOrDefaultAsync(r => r.SessionId == sessionId && r.Gate == name && r.Run == run, ct) .ConfigureAwait(false); @@ -80,6 +85,7 @@ internal sealed class EfApprovalStore(SessionsDbContext db) : IApprovalStore public async Task FindAsync(string tenantId, Guid id, CancellationToken ct = default) { + using var bound = RowLevelSecurity.TenantScopeIfUnbound(tenantId); var row = await Of(tenantId).AsNoTracking().FirstOrDefaultAsync(r => r.Id == id, ct).ConfigureAwait(false); return row is null ? null : ApprovalRows.ToDomain(row); } @@ -87,6 +93,7 @@ internal sealed class EfApprovalStore(SessionsDbContext db) : IApprovalStore public async Task RequestAsync(ApprovalRequest request, CancellationToken ct = default) { ApprovalRows.EnsureTenant(request); + using var bound = RowLevelSecurity.TenantScopeIfUnbound(request.TenantId); if (await GetAsync(request.TenantId, request.SessionId, request.Gate, request.Run, ct).ConfigureAwait(false) is { } existing) { return existing; } var row = ApprovalRows.NewRow(request); db.Approvals.Add(row); @@ -107,6 +114,7 @@ public async Task RequestAsync(ApprovalRequest request, Cancell public async Task DecideAsync(string tenantId, Guid id, ApprovalDecision decision, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(decision); + using var bound = RowLevelSecurity.TenantScopeIfUnbound(tenantId); var row = await Of(tenantId).FirstOrDefaultAsync(r => r.Id == id, ct).ConfigureAwait(false); if (row is null) { return null; } if (!ApprovalRows.Apply(row, decision)) { return ApprovalRows.ToDomain(row); } @@ -126,6 +134,7 @@ public async Task RequestAsync(ApprovalRequest request, Cancell public async Task> ListRecentAsync(string tenantId, int max = 50, CancellationToken ct = default) { + using var bound = RowLevelSecurity.TenantScopeIfUnbound(tenantId); var rows = await Of(tenantId).AsNoTracking() .OrderByDescending(r => r.RequestedAtUtc) .Take(Math.Clamp(max, 1, 200)) diff --git a/src/AgentOs.Modules.Sessions/Persistence/Repositories/EfRunnerDirectory.cs b/src/AgentOs.Modules.Sessions/Persistence/Repositories/EfRunnerDirectory.cs index 0e6cbb97..e2e68fe5 100644 --- a/src/AgentOs.Modules.Sessions/Persistence/Repositories/EfRunnerDirectory.cs +++ b/src/AgentOs.Modules.Sessions/Persistence/Repositories/EfRunnerDirectory.cs @@ -2,6 +2,9 @@ // authenticated tenant yet — so the lookup bypasses the tenant query filter (IgnoreQueryFilters) and // finds the runner by its unguessable id. The caller MUST then verify the presented token against the // returned TokenHash before trusting TenantId / OwnerUserId. The id alone grants nothing. +// RLS (ADR-0012 stage 2): the two cross-tenant reads by id — the handshake lookup and the revalidation sweep — run in +// an explicit RowLevelSecurity.PlatformScope; the hub binds the rest of the handshake (MarkPaired, RetireReplaced) to +// the runner's tenant once the token is verified. using System; using System.Collections.Generic; @@ -9,6 +12,7 @@ using System.Threading; using System.Threading.Tasks; using AgentOs.Domain.Sessions; +using AgentOs.SharedKernel.Persistence; using Microsoft.EntityFrameworkCore; namespace AgentOs.Modules.Sessions.Persistence.Repositories; @@ -21,6 +25,9 @@ internal sealed class EfRunnerDirectory : IRunnerDirectory public async Task FindForPairingAsync(Guid runnerId, CancellationToken cancellationToken = default) { + // Before authentication no tenant is known: the unguessable id is looked up across tenants, then the hub verifies + // the token against the returned hash before it trusts anything in the row. + using var platform = RowLevelSecurity.PlatformScope("runner-handshake"); return await _db.Runners .IgnoreQueryFilters() .AsNoTracking() @@ -109,6 +116,8 @@ public async Task> FindStatusesAsync(IReadOnly ArgumentNullException.ThrowIfNull(runnerIds); if (runnerIds.Count == 0) { return new Dictionary(); } // One WHERE Id IN (…) per revalidation sweep, tenant-unfiltered like the handshake lookup (the sweep spans tenants). + // A status read only — but an empty answer means "gone", so it must see every tenant's row (platform scope). + using var platform = RowLevelSecurity.PlatformScope("runner-revalidation"); var ids = runnerIds.ToArray(); return await _db.Runners .IgnoreQueryFilters() diff --git a/src/AgentOs.Modules.Sessions/Persistence/Repositories/RunnerRepository.cs b/src/AgentOs.Modules.Sessions/Persistence/Repositories/RunnerRepository.cs index c9594a41..62bae95f 100644 --- a/src/AgentOs.Modules.Sessions/Persistence/Repositories/RunnerRepository.cs +++ b/src/AgentOs.Modules.Sessions/Persistence/Repositories/RunnerRepository.cs @@ -43,14 +43,15 @@ public async Task> ListAsync(int limit = Page.Defaul ORDER BY "CreatedAtUtc" DESC LIMIT @lim OFFSET @off """; - return await QueryRunnersAsync(sql, new { tenantId = _tenant.TenantId, lim, off }, ct).ConfigureAwait(false); + return await QueryRunnersAsync(_tenant.TenantId, sql, new { tenantId = _tenant.TenantId, lim, off }, ct).ConfigureAwait(false); } public async Task GetAsync(Guid id, CancellationToken ct = default) { const string sql = """SELECT * FROM sessions.runners WHERE "Id" = @id AND "TenantId" = @tenantId"""; - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + // RLS (ADR-0012 stage 2): Dapper reads switch to the restricted role like the EF context — a plain connection ran as + // the login user, which an owner login (Azure) sees nothing through once "no tenant" means nothing. + await using var conn = await Conn.OpenForTenantAsync(_tenant.TenantId, ct).ConfigureAwait(false); return await conn.QueryFirstOrDefaultAsync( new CommandDefinition(sql, new { id, tenantId = _tenant.TenantId }, cancellationToken: ct)).ConfigureAwait(false); } @@ -82,13 +83,12 @@ public async Task> ListForTenantAsync(string tenantI WHERE "TenantId" = @tenantId ORDER BY "CreatedAtUtc" DESC """; - return await QueryRunnersAsync(sql, new { tenantId }, ct).ConfigureAwait(false); + return await QueryRunnersAsync(tenantId, sql, new { tenantId }, ct).ConfigureAwait(false); } - private async Task> QueryRunnersAsync(string sql, object parms, CancellationToken ct) + private async Task> QueryRunnersAsync(string tenantId, string sql, object parms, CancellationToken ct) { - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var rows = await conn.QueryAsync( new CommandDefinition(sql, parms, cancellationToken: ct)).ConfigureAwait(false); return rows.AsList(); diff --git a/src/AgentOs.Modules.Sessions/Persistence/Repositories/SessionRepository.cs b/src/AgentOs.Modules.Sessions/Persistence/Repositories/SessionRepository.cs index 99e60bbe..e39a4e3e 100644 --- a/src/AgentOs.Modules.Sessions/Persistence/Repositories/SessionRepository.cs +++ b/src/AgentOs.Modules.Sessions/Persistence/Repositories/SessionRepository.cs @@ -43,14 +43,15 @@ public async Task> ListAsync(int limit = Page ORDER BY "CreatedAtUtc" DESC LIMIT @lim OFFSET @off """; - return await QuerySessionsAsync(sql, new { tenantId = _tenant.TenantId, lim, off }, ct).ConfigureAwait(false); + return await QuerySessionsAsync(_tenant.TenantId, sql, new { tenantId = _tenant.TenantId, lim, off }, ct).ConfigureAwait(false); } public async Task GetAsync(Guid id, CancellationToken ct = default) { const string sql = """SELECT * FROM sessions.sessions WHERE "Id" = @id AND "TenantId" = @tenantId"""; - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + // RLS (ADR-0012 stage 2): Dapper reads switch to the restricted role like the EF context — a plain connection + // ran as the login user, which an owner login (Azure) sees nothing through once "no tenant" means nothing. + await using var conn = await Conn.OpenForTenantAsync(_tenant.TenantId, ct).ConfigureAwait(false); return await conn.QueryFirstOrDefaultAsync( new CommandDefinition(sql, new { id, tenantId = _tenant.TenantId }, cancellationToken: ct)).ConfigureAwait(false); } @@ -83,13 +84,12 @@ public async Task> ListForTenantAsync(string WHERE "TenantId" = @tenantId ORDER BY "CreatedAtUtc" DESC """; - return await QuerySessionsAsync(sql, new { tenantId }, ct).ConfigureAwait(false); + return await QuerySessionsAsync(tenantId, sql, new { tenantId }, ct).ConfigureAwait(false); } - private async Task> QuerySessionsAsync(string sql, object parms, CancellationToken ct) + private async Task> QuerySessionsAsync(string tenantId, string sql, object parms, CancellationToken ct) { - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var rows = await conn.QueryAsync( new CommandDefinition(sql, parms, cancellationToken: ct)).ConfigureAwait(false); return rows.AsList(); @@ -154,19 +154,18 @@ public async Task> ListReposForTenantAsync(stri WHERE "TenantId" = @tenantId AND "SessionId" = @sessionId ORDER BY "Owner", "Repo" """; - return await QueryReposAsync(sql, new { tenantId, sessionId }, ct).ConfigureAwait(false); + return await QueryReposAsync(tenantId, sql, new { tenantId, sessionId }, ct).ConfigureAwait(false); } public async Task> ListAllReposForTenantAsync(string tenantId, CancellationToken ct = default) { const string sql = """SELECT * FROM sessions.session_repos WHERE "TenantId" = @tenantId"""; - return await QueryReposAsync(sql, new { tenantId }, ct).ConfigureAwait(false); + return await QueryReposAsync(tenantId, sql, new { tenantId }, ct).ConfigureAwait(false); } - private async Task> QueryReposAsync(string sql, object parms, CancellationToken ct) + private async Task> QueryReposAsync(string tenantId, string sql, object parms, CancellationToken ct) { - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var rows = await conn.QueryAsync( new CommandDefinition(sql, parms, cancellationToken: ct)).ConfigureAwait(false); return rows.AsList(); diff --git a/src/AgentOs.Modules.Tenants/AuditChain.cs b/src/AgentOs.Modules.Tenants/AuditChain.cs new file mode 100644 index 00000000..e577d00e --- /dev/null +++ b/src/AgentOs.Modules.Tenants/AuditChain.cs @@ -0,0 +1,232 @@ +// GAP-11b — the audit trail's hash chain. Each tenant's events (and the "_platform" scope's) form one chain: event N +// stores the hash of event N-1 (Genesis for the first) and its own SHA-256 over a canonical, length-prefixed encoding of +// its content plus that previous hash. Changing an event breaks its own hash; removing or re-ordering one breaks the +// sequence or the next link. The verifier walks a chain in order (streaming, O(1) memory) and names the first event +// that doesn't hold. +// Unchained rows (no Sequence / Hash) are legitimate only from before the chain existed. Once a tenant's chain has +// started, an unchained row recorded at or after its first event is an insert that bypassed the writer (the app role may +// INSERT): the verifier reports the chain broken at the position that row takes in time (AuditChainProblem.Inserted). +// Limits, by design (docs/security/threat-model.md MT-3): a database owner who rewrites an event AND every later hash +// is not detected without an external anchor — the head hash is shown so it can be recorded elsewhere; deleting the +// newest events (truncating the tail) is not detected by the chain alone either. The app role cannot do either: it may +// only INSERT and SELECT audit rows (REVOKE UPDATE, DELETE, TRUNCATE — RowLevelSecurity.AppendOnlyTables). An unchained +// row back-dated to before the chain's first event reads as pre-chain history — it is counted, never verified. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Globalization; +using System.Security.Cryptography; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace AgentOs.Modules.Tenants; + +/// One chained audit event, as stored (the verifier's input). +public sealed record AuditChainLink( + long Sequence, + Guid Id, + string TenantId, + DateTimeOffset TimestampUtc, + string? UserId, + string Action, + string? Target, + string? IpAddress, + string? UserAgent, + string PrevHash, + string Hash); + +/// Why a chain doesn't hold. +public enum AuditChainProblem +{ + /// Every event checked out. + None = 0, + + /// An event is missing: the sequence jumps (an event was deleted). + Missing = 1, + + /// An event's content no longer matches its own hash (it was changed after it was written). + Altered = 2, + + /// An event doesn't link to the one before it (an earlier event was changed or replaced). + Relinked = 3, + + /// An event without a chain link was recorded after the chain started (it was added outside the audit + /// writer). + Inserted = 4, +} + +/// The earliest unchained event recorded at or after a chain's first event — an insert that bypassed the +/// writer (the verifier's input; null when there is none). +/// The event's id. +/// When it claims to have happened. +public sealed record AuditChainStray(Guid Id, DateTimeOffset TimestampUtc); + +/// The verifier's answer for one tenant's chain. +/// The chain (a tenant id, or ). +/// Chained events checked before the first problem (all of them when intact). +/// Events recorded before the chain existed — kept, but not verifiable. +/// The newest event's hash when intact (record it elsewhere to anchor the chain), else null. +/// The sequence number of the first event that doesn't hold, else null. For an +/// event: the position it takes in time (after event BrokenAt - 1). +/// That event's id, when it exists (null for a missing one). +/// What is wrong at . +public sealed record AuditChainReport( + string TenantId, + long Verified, + long Unchained, + string? HeadHash, + long? BrokenAt, + Guid? BrokenEventId, + AuditChainProblem Problem) +{ + /// True when no event failed. + public bool Intact => Problem == AuditChainProblem.None; + + /// One plain-language line for the desktop and the Api. + public string Summary => Problem switch + { + AuditChainProblem.None when Verified == 0 => Unchained == 0 + ? "Nothing to verify yet — no events recorded." + : $"Nothing to verify yet — the {Unchained} recorded events predate the chain.", + AuditChainProblem.None => $"Chain intact — {Verified} events verified{(Unchained > 0 ? $"; {Unchained} earlier events predate the chain" : string.Empty)}.", + AuditChainProblem.Missing => $"Chain broken at event {BrokenAt}: it is missing — an event was deleted.", + AuditChainProblem.Altered => $"Chain broken at event {BrokenAt}: its content no longer matches its hash — it was changed after it was written.", + AuditChainProblem.Inserted => $"Chain broken at event {BrokenAt}: an event without a chain link was recorded there — it was added outside the audit log.", + _ => $"Chain broken at event {BrokenAt}: it doesn't follow event {BrokenAt - 1} — an earlier event was changed or replaced.", + }; +} + +/// Computes and verifies the audit hash chain (see the file header). +public static class AuditChain +{ + /// The previous hash of a chain's first event. + public const string Genesis = "0000000000000000000000000000000000000000000000000000000000000000"; + + // Bumped only with a new encoding; the verifier would then pick the encoding by version. + private const string Version = "agentos-audit-chain-v1"; + + /// Postgres keeps a timestamp to the microsecond: hash exactly what is stored, in UTC. + public static DateTimeOffset Normalize(DateTimeOffset timestamp) + { + var ticks = timestamp.UtcTicks; + return new DateTimeOffset(ticks - (ticks % 10), TimeSpan.Zero); + } + + /// The hash of (its own is not an input). + public static string Compute(AuditChainLink link) + { + ArgumentNullException.ThrowIfNull(link); + using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); + Field(hash, Version); + Field(hash, link.PrevHash); + Field(hash, link.TenantId); + Field(hash, link.Sequence.ToString(CultureInfo.InvariantCulture)); + Field(hash, link.Id.ToString("D")); + Field(hash, Normalize(link.TimestampUtc).UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.ffffff'Z'", CultureInfo.InvariantCulture)); + Field(hash, link.UserId); + Field(hash, link.Action); + Field(hash, link.Target); + Field(hash, link.IpAddress); + Field(hash, link.UserAgent); + Span digest = stackalloc byte[32]; + hash.GetHashAndReset(digest); + return Convert.ToHexStringLower(digest); + } + + /// Walk a chain in sequence order and report the first event that doesn't hold. is + /// the earliest unchained event recorded at or after the chain's first event (see the file header), if any. + public static AuditChainReport Verify(string tenantId, IEnumerable links, long unchained, AuditChainStray? stray = null) + { + ArgumentNullException.ThrowIfNull(links); + var walk = new Walk(tenantId, unchained, stray); + foreach (var link in links) + { + if (walk.Step(link) is { } broken) { return broken; } + } + return walk.Done(); + } + + /// Streaming twin of (a database cursor; O(1) memory). + public static async Task VerifyAsync( + string tenantId, IAsyncEnumerable links, long unchained, AuditChainStray? stray = null, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(links); + var walk = new Walk(tenantId, unchained, stray); + await foreach (var link in links.WithCancellation(cancellationToken).ConfigureAwait(false)) + { + if (walk.Step(link) is { } broken) { return broken; } + } + return walk.Done(); + } + + // ":\n", or "-\n" for null: unambiguous whatever a value contains. + private static void Field(IncrementalHash hash, string? value) + { + if (value is null) + { + hash.AppendData("-\n"u8); + return; + } + var count = Encoding.UTF8.GetByteCount(value); + Span prefix = stackalloc byte[16]; + _ = count.TryFormat(prefix, out var written, default, CultureInfo.InvariantCulture); // ≤ 10 digits: always fits + prefix[written++] = (byte)':'; + hash.AppendData(prefix[..written]); + byte[]? rented = null; + Span buffer = count <= 256 ? stackalloc byte[256] : (rented = ArrayPool.Shared.Rent(count)); + try + { + var bytes = Encoding.UTF8.GetBytes(value, buffer); + hash.AppendData(buffer[..bytes]); + } + finally + { + if (rented is not null) { ArrayPool.Shared.Return(rented); } + } + hash.AppendData("\n"u8); + } + + private sealed class Walk(string tenantId, long unchained, AuditChainStray? stray) + { + private long _expected = 1; + private string _previous = Genesis; + private AuditChainStray? _stray = stray; + + public AuditChainReport? Step(AuditChainLink link) + { + if (_stray is { } s && s.TimestampUtc < link.TimestampUtc) + { + // Before the first event: pre-chain history. Later: it sits in front of this event — reported here. + if (_expected > 1) { return Broken(_expected, s.Id, AuditChainProblem.Inserted); } + _stray = null; + } + if (link.Sequence != _expected) + { + // A jump: event _expected is gone. (A repeat can't happen — the (tenant, sequence) index is unique.) + return Broken(_expected, link.Sequence > _expected ? null : link.Id, AuditChainProblem.Missing); + } + if (!string.Equals(Compute(link), link.Hash, StringComparison.Ordinal)) + { + return Broken(link.Sequence, link.Id, AuditChainProblem.Altered); + } + if (!string.Equals(link.PrevHash, _previous, StringComparison.Ordinal)) + { + return Broken(link.Sequence, link.Id, AuditChainProblem.Relinked); + } + _previous = link.Hash; + _expected++; + return null; + } + + public AuditChainReport Done() + => _stray is { } s && _expected > 1 + ? Broken(_expected, s.Id, AuditChainProblem.Inserted) + : new(tenantId, _expected - 1, unchained, _expected == 1 ? null : _previous, null, null, AuditChainProblem.None); + + private AuditChainReport Broken(long at, Guid? id, AuditChainProblem problem) + => new(tenantId, _expected - 1, unchained, null, at, id, problem); + } +} diff --git a/src/AgentOs.Modules.Tenants/AuditTelemetry.cs b/src/AgentOs.Modules.Tenants/AuditTelemetry.cs new file mode 100644 index 00000000..5754ac2a --- /dev/null +++ b/src/AgentOs.Modules.Tenants/AuditTelemetry.cs @@ -0,0 +1,36 @@ +// Audit-trail health. Audit writes are best-effort (IAuditLog: a write failure never rolls back the surrounding +// operation), so a row that could not be recorded must never vanish silently: it is logged as an error AND counted +// here — the same pattern as the tool-evidence `agentos.evidence.lost` counter. Exported with the host's OTel metrics +// (ServiceDefaults registers the meter); docs/OPERATIONS.md lists the instrument. + +using System.Collections.Generic; +using System.Diagnostics.Metrics; + +namespace AgentOs.Modules.Tenants; + +/// Meter and instruments for the audit trail. +public static class AuditTelemetry +{ + /// The meter name (registered with .AddMeter("AgentOs.Audit") in ServiceDefaults). + public const string MeterName = "AgentOs.Audit"; + + /// Counter of audit rows that were not recorded, tagged reason (). + public const string Lost = "agentos.audit.lost"; + + /// Values of the reason tag on . + public static class Reasons + { + /// The entry named no tenant, so it belongs to no chain (a caller bug). + public const string NoTenant = "no_tenant"; + + /// The database refused or was unreachable. + public const string Database = "database"; + } + + private static readonly Meter Meter = new(MeterName); + private static readonly Counter LostCounter = + Meter.CreateCounter(Lost, unit: "{event}", description: "Audit rows that were not recorded (best-effort writes), by reason."); + + /// Count one audit row that was not recorded. + internal static void RecordLost(string reason) => LostCounter.Add(1, new KeyValuePair("reason", reason)); +} diff --git a/src/AgentOs.Modules.Tenants/Data/TenantDataService.cs b/src/AgentOs.Modules.Tenants/Data/TenantDataService.cs index 71495149..16f775ff 100644 --- a/src/AgentOs.Modules.Tenants/Data/TenantDataService.cs +++ b/src/AgentOs.Modules.Tenants/Data/TenantDataService.cs @@ -13,7 +13,9 @@ // audited under _platform. GAP-17: the service authorizes the purge itself — the caller must hold platform-admin and // may not purge their own tenant — so a future route or job can't skip the check the desktop does // (TenantPurgeDeniedException, raised before any connection is opened). -// Runs on a raw connection (no RLS role switch); every statement is filtered by the tenant explicitly. +// Runs on a raw connection as the login user (no RLS role switch: the purge must delete the tenant's audit trail, +// which the app role may not — GAP-11b); every statement is filtered by the tenant explicitly AND the session is bound +// to that tenant, so the forced policies (RLS stage 2, ADR-0012) let an owner login (Azure) see exactly its rows. using System; using System.Collections.Generic; @@ -129,8 +131,7 @@ public async Task ExportAsync(string tenantId, Stream output, CancellationToken ArgumentException.ThrowIfNullOrWhiteSpace(tenantId); ArgumentNullException.ThrowIfNull(output); EnsureAvailable(); - await using var conn = new NpgsqlConnection(connectionString); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var tables = await TenantTablesAsync(conn, ct).ConfigureAwait(false); var columns = await ColumnsAsync(conn, tables, ct).ConfigureAwait(false); // Resolve every table's redaction before the first byte is written: a refusal leaves no partial export. @@ -186,9 +187,8 @@ public async Task PurgeAsync(string tenantId, ClaimsPrincipal var actor = AuthorizePurge(tenantId, caller); EnsureAvailable(); var deleted = new List<(string Table, long Rows)>(); - await using (var conn = new NpgsqlConnection(connectionString)) + await using (var conn = await OpenForTenantAsync(tenantId, ct).ConfigureAwait(false)) { - await conn.OpenAsync(ct).ConfigureAwait(false); var tables = await TenantTablesAsync(conn, ct).ConfigureAwait(false); var order = DeleteOrder(tables, await ForeignKeysAsync(conn, ct).ConfigureAwait(false)); await using var tx = await conn.BeginTransactionAsync(ct).ConfigureAwait(false); @@ -226,6 +226,28 @@ await audit.WriteAsync(new AuditEntry(Guid.NewGuid(), AuditScopes.Platform, acto return report; } + // The login user's own connection, bound to the one tenant it exports or purges: with no session tenant the forced + // policies would show an owner login nothing — an export missing every RLS table, a purge that deleted no rows yet + // reported success. + private async Task OpenForTenantAsync(string tenantId, CancellationToken ct) + { + var conn = new NpgsqlConnection(connectionString); + try + { + await conn.OpenAsync(ct).ConfigureAwait(false); + using (AgentOs.SharedKernel.Persistence.RowLevelSecurity.TenantScope(tenantId)) + { + await AgentOs.SharedKernel.Persistence.RowLevelSecurity.ApplySessionAsync(conn, switchRole: false, ct).ConfigureAwait(false); + } + return conn; + } + catch + { + await conn.DisposeAsync().ConfigureAwait(false); + throw; + } + } + private void EnsureAvailable() { if (!IsAvailable) { throw new InvalidOperationException("No database is configured — there is no tenant data to export or purge."); } diff --git a/src/AgentOs.Modules.Tenants/Endpoints/TenantEndpoints.cs b/src/AgentOs.Modules.Tenants/Endpoints/TenantEndpoints.cs index fc98366f..46de60ee 100644 --- a/src/AgentOs.Modules.Tenants/Endpoints/TenantEndpoints.cs +++ b/src/AgentOs.Modules.Tenants/Endpoints/TenantEndpoints.cs @@ -8,17 +8,22 @@ using System.Threading; using System.Threading.Tasks; using AgentOs.Modules.Tenants.Email; +using AgentOs.Modules.Tenants.Invitations; using AgentOs.Modules.Tenants.Keycloak; using AgentOs.SharedKernel.Hosting; using AgentOs.SharedKernel.Identity; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Logging; namespace AgentOs.Modules.Tenants.Endpoints; internal static class TenantEndpoints { + // Log category of public sign-up failures (the detail the anonymous caller never sees). + private const string SignupLogCategory = "AgentOs.Modules.Tenants.Signup"; + public static IEndpointRouteBuilder MapTenantEndpoints(this IEndpointRouteBuilder app) { ArgumentNullException.ThrowIfNull(app); @@ -97,11 +102,20 @@ public static IEndpointRouteBuilder MapTenantEndpoints(this IEndpointRouteBuilde // Self-service sign-up: dispatches to ITenantSignupService which picks one of three modes // (invite / slug / auto-create) by request shape. Creating a NEW workspace needs the platform setting // Tenants:OpenSignup (off outside Development — GAP-01); an invitation always works. + // The caller is anonymous: our own validation messages are returned as they are, but an identity-service + // failure never is — Keycloak's raw response body stays in the server log (SignupErrors). app.MapPost("/tenants/register", async ( RegisterTenantRequest body, ITenantSignupService signup, + ILoggerFactory loggers, CancellationToken ct) => { + IResult IdentityFailed(Exception ex) + { + loggers.CreateLogger(SignupLogCategory).LogError(ex, "Sign-up failed in the identity service; the caller got a generic message."); + return Results.Json(new { error = SignupErrors.ForPublic(ex) }, statusCode: SignupErrors.StatusFor(ex)); + } + try { var outcome = await signup.SignupAsync(new TenantSignupRequest( @@ -119,6 +133,12 @@ public static IEndpointRouteBuilder MapTenantEndpoints(this IEndpointRouteBuilde { return Results.Problem(detail: ex.Message, statusCode: StatusCodes.Status403Forbidden); } + catch (KeycloakAdminException ex) { return IdentityFailed(ex); } + catch (KeycloakUserLeftBehindException ex) { return IdentityFailed(ex); } + catch (System.Net.Http.HttpRequestException ex) { return IdentityFailed(ex); } + catch (TimeoutException ex) { return IdentityFailed(ex); } + // HttpClient reports its own timeout as a cancellation; a caller that went away is not a failure to report. + catch (TaskCanceledException ex) when (!ct.IsCancellationRequested) { return IdentityFailed(ex); } catch (InvalidOperationException ex) { return Results.BadRequest(new { error = ex.Message }); @@ -129,14 +149,13 @@ public static IEndpointRouteBuilder MapTenantEndpoints(this IEndpointRouteBuilde .WithTags("Tenants") .AllowAnonymous(); - // Admin-only: mint a stateless invitation token. The returned `url` (signup page + ?invite=) - // is what the inviter pastes into an email or chat. Tokens are signed + time-limited; they - // cannot be revoked before expiry — keep TTL short for high-trust environments. + // Admin-only: create an invitation (GAP-11). The returned `url` (signup page + ?invite=) is what the inviter + // pastes into an email or chat. The invitation is single-use, bound to `email` (required), revocable until it is + // used (POST …/invitations/{id}/revoke), and expires after `ttlHours` (1–168, default 24). app.MapPost("/tenants/{tenantId}/invitations", async ( string tenantId, CreateInvitationRequest body, ITenantSignupService signup, - IAuditLog audit, IEmailSender email, ITenantContext ctx, PublicOrigin origin, @@ -154,59 +173,114 @@ public static IEndpointRouteBuilder MapTenantEndpoints(this IEndpointRouteBuilde { return Results.BadRequest(new { error = "role must be 'admin', 'approver' or 'member'" }); } + // GAP-11: an invitation is bound to the address it is sent to — no address, no invitation. + if (string.IsNullOrWhiteSpace(body.Email)) + { + return Results.BadRequest(new { error = "email is required: an invitation can only be accepted by the address it was sent to" }); + } var ttl = TimeSpan.FromHours(body.TtlHours is > 0 and <= 168 ? body.TtlHours.Value : 24); - var minted = signup.CreateInvitation(tenantId, role, body.Email, ttl); + InvitationToken minted; + try + { + // Audited by the service (invitation.minted) — the row joins the tenant's hash chain there. + minted = await signup.CreateInvitationAsync(tenantId, role, body.Email, ttl, + new AuditActor(ctx.UserId, http.Connection.RemoteIpAddress?.ToString(), http.Request.Headers.UserAgent.ToString()), + ct).ConfigureAwait(false); + } + catch (ArgumentException ex) + { + return Results.BadRequest(new { error = ex.Message }); + } // GAP-05: the link points at the configured public desktop URL — never at the request's Host header, which // the caller controls (an admin's request through a spoofed Host would mint a link to someone else's server). - var url = origin.Link($"/signup?invite={Uri.EscapeDataString(minted.Token)}", http.Request); - await audit.WriteAsync(new AuditEntry( - Guid.NewGuid(), tenantId, ctx.UserId, AuditActions.InvitationMinted, - Target: $"role={role}{(body.Email is null ? "" : $",email={body.Email}")}", - IpAddress: http.Connection.RemoteIpAddress?.ToString(), - UserAgent: http.Request.Headers.UserAgent.ToString(), - TimestampUtc: DateTimeOffset.UtcNow), ct).ConfigureAwait(false); + var url = origin.Link(InvitationLinks.PathFor(minted.Token), http.Request); - // Best-effort: email the link when an address is supplied. The URL is returned regardless - // so the admin can still copy/paste it if delivery is unconfigured or fails. + // Best-effort: email the link to the invited address. The URL is returned regardless so the admin can still + // copy/paste it if delivery is unconfigured or fails. var emailed = false; - if (!string.IsNullOrWhiteSpace(body.Email)) + // Already logged by the sender; fall back to the returned URL. The address may also be + // unparseable (MimeKit.ParseException) — same outcome: emailed stays false. + static void Handle(Exception e) => _ = e.Message; + try { - // Already logged by the sender; fall back to the returned URL. The address may also be - // unparseable (MimeKit.ParseException) — same outcome: emailed stays false. - static void Handle(Exception e) => _ = e.Message; - try - { - var (subject, htmlBody, textBody) = InvitationEmail.Build(tenantId, role, url); - await email.SendAsync(body.Email!, subject, htmlBody, textBody, ct).ConfigureAwait(false); - emailed = true; - } - // ServiceNotConnectedException derives from InvalidOperationException; CommandException → - // ProtocolException — derived clauses precede their bases (CS0160). - catch (MailKit.ServiceNotConnectedException ex) { Handle(ex); } - catch (MailKit.CommandException ex) { Handle(ex); } - catch (MailKit.ProtocolException ex) { Handle(ex); } - catch (MailKit.Security.AuthenticationException ex) { Handle(ex); } - catch (System.Net.Sockets.SocketException ex) { Handle(ex); } - catch (System.IO.IOException ex) { Handle(ex); } - catch (InvalidOperationException ex) { Handle(ex); } - catch (MimeKit.ParseException ex) { Handle(ex); } - } - return Results.Ok(new CreatedInvitationResponse(minted.Token, url, minted.ExpiresAtUtc, emailed)); + var (subject, htmlBody, textBody) = InvitationEmail.Build(tenantId, role, url); + await email.SendAsync(body.Email.Trim(), subject, htmlBody, textBody, ct).ConfigureAwait(false); + emailed = true; + } + // ServiceNotConnectedException derives from InvalidOperationException; CommandException → + // ProtocolException — derived clauses precede their bases (CS0160). + catch (MailKit.ServiceNotConnectedException ex) { Handle(ex); } + catch (MailKit.CommandException ex) { Handle(ex); } + catch (MailKit.ProtocolException ex) { Handle(ex); } + catch (MailKit.Security.AuthenticationException ex) { Handle(ex); } + catch (System.Net.Sockets.SocketException ex) { Handle(ex); } + catch (System.IO.IOException ex) { Handle(ex); } + catch (InvalidOperationException ex) { Handle(ex); } + catch (MimeKit.ParseException ex) { Handle(ex); } + return Results.Ok(new CreatedInvitationResponse(minted.Token, url, minted.ExpiresAtUtc, emailed, minted.Id)); }) .WithName("TenantsCreateInvitation") - .WithSummary("Mint a signup invitation URL for the tenant (admin only)") + .WithSummary("Create a single-use, email-bound invitation URL for the tenant (admin only)") + .WithTags("Tenants") + .RequireAuthorization("Admin"); + + // The tenant's invitations, newest first, with their state (pending / accepted / revoked / expired). + app.MapGet("/tenants/{tenantId}/invitations", async ( + string tenantId, + ITenantSignupService signup, + ITenantContext ctx, + int? max, + CancellationToken ct) => + { + if (!string.Equals(ctx.TenantId, tenantId, StringComparison.Ordinal)) + { + return Results.Forbid(); + } + return Results.Ok(await signup.ListInvitationsAsync(tenantId, Math.Clamp(max ?? 100, 1, 500), ct).ConfigureAwait(false)); + }) + .WithName("TenantsListInvitations") + .WithSummary("List the tenant's invitations and their state (tenant Admin only)") .WithTags("Tenants") .RequireAuthorization("Admin"); - // Public preview: decode an invitation token (no side effects). Lets the signup page show - // the inviter's tenant + email before the user fills the form. - app.MapGet("/tenants/invitations/preview", ( + // Withdraw an unused invitation (GAP-11): its link stops working at once. 409 when it was already used or revoked. + app.MapPost("/tenants/{tenantId}/invitations/{invitationId:guid}/revoke", async ( + string tenantId, + Guid invitationId, + ITenantSignupService signup, + ITenantContext ctx, + HttpContext http, + CancellationToken ct) => + { + if (!string.Equals(ctx.TenantId, tenantId, StringComparison.Ordinal)) + { + return Results.Forbid(); + } + var actor = new AuditActor(ctx.UserId, http.Connection.RemoteIpAddress?.ToString(), http.Request.Headers.UserAgent.ToString()); + if (await signup.RevokeInvitationAsync(tenantId, invitationId, actor, ct).ConfigureAwait(false)) + { + return Results.NoContent(); + } + var known = (await signup.ListInvitationsAsync(tenantId, 500, ct).ConfigureAwait(false)).Any(i => i.Id == invitationId); + return known + ? Results.Conflict(new { error = "Only an invitation that has not been used or revoked can be revoked." }) + : Results.NotFound(); + }) + .WithName("TenantsRevokeInvitation") + .WithSummary("Revoke an unused invitation (tenant Admin only)") + .WithTags("Tenants") + .RequireAuthorization("Admin"); + + // Public preview: read an invitation link (no side effects). Lets the signup page show the tenant, role and the + // (bound) email — or why the link can't be used (status accepted / revoked / expired). + app.MapGet("/tenants/invitations/preview", async ( string token, - ITenantSignupService signup) => + ITenantSignupService signup, + CancellationToken ct) => { - var preview = signup.PreviewInvitation(token); + var preview = await signup.PreviewInvitationAsync(token, ct).ConfigureAwait(false); return preview is null - ? Results.NotFound(new { error = "Invitation is invalid or expired" }) + ? Results.NotFound(new { error = "Invitation is invalid" }) : Results.Ok(preview); }) .WithName("TenantsPreviewInvitation") @@ -351,6 +425,26 @@ await audit.WriteAsync(new AuditEntry( .WithTags("Tenants") .RequireAuthorization("Admin"); + // GAP-11b: walk the tenant's audit hash chain — intact, or the first event that was changed or removed. + app.MapGet("/tenants/{tenantId}/audit/verify", async ( + string tenantId, + [Microsoft.AspNetCore.Mvc.FromServices] IAuditChainVerifier verifier, + ITenantContext ctx, + CancellationToken ct) => + { + if (!string.Equals(ctx.TenantId, tenantId, StringComparison.Ordinal)) + { + return Results.Forbid(); + } + var report = await verifier.VerifyAsync(tenantId, ct).ConfigureAwait(false); + return Results.Ok(new AuditChainResponse(report.TenantId, report.Intact, report.Verified, report.Unchained, + report.HeadHash, report.BrokenAt, report.BrokenEventId, report.Problem.ToString(), report.Summary)); + }) + .WithName("TenantsAuditVerify") + .WithSummary("Verify the tenant's tamper-evident audit chain (tenant Admin only)") + .WithTags("Tenants") + .RequireAuthorization("Admin"); + app.MapPost("/tenants/{tenantId}/members", async ( string tenantId, InviteMemberRequest body, @@ -518,4 +612,9 @@ public sealed record CreateInvitationRequest(string? Email, string? Role, int? T /// Response for POST /tenants/{id}/invitations. /// True when the invitation link was emailed to the supplied address. -public sealed record CreatedInvitationResponse(string Token, string Url, DateTimeOffset ExpiresAtUtc, bool Emailed = false); +/// The invitation's id (revoke it with POST /tenants/{id}/invitations/{invitationId}/revoke). +public sealed record CreatedInvitationResponse(string Token, string Url, DateTimeOffset ExpiresAtUtc, bool Emailed = false, Guid? Id = null); + +/// Response for GET /tenants/{id}/audit/verify. +public sealed record AuditChainResponse( + string TenantId, bool Intact, long Verified, long Unchained, string? HeadHash, long? BrokenAt, Guid? BrokenEventId, string Problem, string Summary); diff --git a/src/AgentOs.Modules.Tenants/IAuditLog.cs b/src/AgentOs.Modules.Tenants/IAuditLog.cs index 47fc150a..55f6aa6a 100644 --- a/src/AgentOs.Modules.Tenants/IAuditLog.cs +++ b/src/AgentOs.Modules.Tenants/IAuditLog.cs @@ -1,6 +1,8 @@ // Append-only audit trail for tenant-scoped mutations. Best-effort: a write failure must NEVER // roll back the surrounding operation — the audit row is observability, not durability. Reads // are tenant-scoped; the implementation enforces the filter on the way out. +// GAP-11b: append-only in the database too (the app role may only INSERT and SELECT) and tamper-evident — each tenant's +// events form a hash chain (AuditChain) that IAuditChainVerifier walks on demand (Settings › Activity log › Verify). using System; using System.Collections.Generic; @@ -19,7 +21,17 @@ public interface IAuditLog Task> ListAsync(string tenantId, int max = 100, CancellationToken ct = default); } -/// One row in the audit trail. +/// Verifies a tenant's audit hash chain (GAP-11b). +public interface IAuditChainVerifier +{ + /// Walk 's chain (or 's) oldest first and report + /// the first event that doesn't hold. The caller authorizes: a tenant admin their own tenant, a platform admin the + /// platform scope. + Task VerifyAsync(string tenantId, CancellationToken ct = default); +} + +/// One row in the audit trail. and are its place in the +/// tenant's hash chain — set by the store on read, ignored on write (null for an event recorded before the chain). public sealed record AuditEntry( Guid Id, string TenantId, @@ -28,7 +40,9 @@ public sealed record AuditEntry( string? Target, string? IpAddress, string? UserAgent, - DateTimeOffset TimestampUtc); + DateTimeOffset TimestampUtc, + long? Sequence = null, + string? Hash = null); /// Canonical action strings; keep in sync with anything consuming the audit table. public static class AuditActions @@ -40,6 +54,15 @@ public static class AuditActions public const string MemberRemoved = "member.removed"; public const string MemberPasswordReset = "member.password_reset"; public const string InvitationMinted = "invitation.minted"; + + /// An administrator withdrew an unused invitation (GAP-11). + public const string InvitationRevoked = "invitation.revoked"; + + /// An invitation was used: it created this account (single-use, GAP-11). + public const string InvitationAccepted = "invitation.accepted"; + + /// Someone tried to use an invitation that was used, revoked, expired, or sent to another address (GAP-11). + public const string InvitationRefused = "invitation.refused"; public const string LoginFailed = "login.failed"; public const string SettingChanged = "setting.changed"; public const string TenantPurged = "tenant.purged"; diff --git a/src/AgentOs.Modules.Tenants/ITenantSignupService.cs b/src/AgentOs.Modules.Tenants/ITenantSignupService.cs index 876ef403..e70748dd 100644 --- a/src/AgentOs.Modules.Tenants/ITenantSignupService.cs +++ b/src/AgentOs.Modules.Tenants/ITenantSignupService.cs @@ -1,13 +1,15 @@ // Self-service signup orchestrator. Dispatches one of three modes based on the request shape: -// - Invite: caller supplies an opaque, signed invitation token; joins an existing tenant. +// - Invite: caller supplies an invitation token; joins an existing tenant. // - Slug: caller supplies an explicit tenant id; first user becomes the tenant Admin. // - AutoCreate: no slug, no invite; service generates a unique slug and the user is Admin. // All three modes provision the Keycloak user FIRST and then write the registry row, with a -// Keycloak rollback if the registry write throws. Invitations are stateless DataProtection- -// signed payloads (no DB table) — they cannot be revoked before TTL but otherwise behave like a -// claim check. +// Keycloak rollback if the registry write throws. +// Invitations (GAP-11) are rows, not stateless tokens: the token is a signed, time-limited envelope around the row's id +// and tenant. An invitation is single-use (consumed on accept), bound to the address it was sent to (accepted only for +// that address, case-insensitive), revocable by a tenant admin until it is used, and expires as before (1–168 h). using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -26,14 +28,28 @@ public interface ITenantSignupService /// otherwise. Task IsOpenSignupEnabledAsync(CancellationToken ct = default); - /// Mint a signed, time-limited invitation token for the given tenant + role. - InvitationToken CreateInvitation(string tenantId, string role, string? email, TimeSpan ttl); + /// Create a single-use invitation to as for + /// , valid for (1–168 h), and audit it. Throws + /// for a bad role, address or lifetime. + Task CreateInvitationAsync( + string tenantId, string role, string email, TimeSpan ttl, AuditActor actor, CancellationToken ct = default); - /// Decode an invitation token; returns null when the token is missing, malformed, - /// or expired. Never throws — callers can branch on null to fall back to a different mode. - InvitationPreview? PreviewInvitation(string? token); + /// Read an invitation link without using it: null when the token is missing, malformed, tampered, from + /// before invitations were single-use, or names no invitation. A used, revoked or expired one IS returned, with its + /// , so the page can say why it can't be used. Never throws for a bad token. + Task PreviewInvitationAsync(string? token, CancellationToken ct = default); + + /// The tenant's invitations, newest first (any state). + Task> ListInvitationsAsync(string tenantId, int max = 100, CancellationToken ct = default); + + /// Withdraw an unused invitation and audit it. False when it doesn't exist in this tenant, was already + /// used, or was already revoked. + Task RevokeInvitationAsync(string tenantId, Guid invitationId, AuditActor actor, CancellationToken ct = default); } +/// Who did something, for the audit row. +public sealed record AuditActor(string? UserId, string? IpAddress = null, string? UserAgent = null); + /// Open self-signup is turned off on this server (GAP-01): only an invitation can create an account. public sealed class SignupClosedException : InvalidOperationException { @@ -62,11 +78,44 @@ public sealed record TenantSignupRequest( /// Result of a successful signup. public sealed record TenantSignupOutcome(string TenantId, string KeycloakUserId, SignupMode Mode); -/// Encrypted invitation payload + the URL the inviter sends out. -public sealed record InvitationToken(string Token, DateTimeOffset ExpiresAtUtc); +/// A minted invitation: its id, the token for the link, and when it expires. +public sealed record InvitationToken(Guid Id, string Token, DateTimeOffset ExpiresAtUtc); + +/// Where an invitation stands. +public enum InvitationStatus +{ + /// Not used yet, not revoked, not expired: it can be accepted. + Pending = 0, + + /// Used — it created an account (single-use). + Accepted = 1, + + /// Withdrawn by an administrator before it was used. + Revoked = 2, + + /// Past its expiry without being used. + Expired = 3, +} + +/// An invitation link, decoded and looked up. The page pre-fills the (bound) email from it. +public sealed record InvitationPreview(Guid Id, string TenantId, string Role, string Email, DateTimeOffset ExpiresAtUtc, InvitationStatus Status) +{ + /// True when the invitation can still be accepted. + public bool CanAccept => Status == InvitationStatus.Pending; +} -/// Decoded invitation payload. The caller may pre-fill a signup form from these values. -public sealed record InvitationPreview(string TenantId, string Role, string? Email, DateTimeOffset ExpiresAtUtc); +/// One invitation in a tenant admin's list. +public sealed record InvitationInfo( + Guid Id, + string TenantId, + string Email, + string Role, + string? CreatedBy, + DateTimeOffset CreatedAtUtc, + DateTimeOffset ExpiresAtUtc, + DateTimeOffset? AcceptedAtUtc, + DateTimeOffset? RevokedAtUtc, + InvitationStatus Status); /// Which branch of ran. public enum SignupMode diff --git a/src/AgentOs.Modules.Tenants/Invitations/InvitationLinks.cs b/src/AgentOs.Modules.Tenants/Invitations/InvitationLinks.cs new file mode 100644 index 00000000..e2da640a --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Invitations/InvitationLinks.cs @@ -0,0 +1,30 @@ +// GAP-05 / GAP-11 — the one place an invitation link is built, for the Api (POST /tenants/{id}/invitations) and the +// desktop (Settings › Users). The link carries a single-use token, so it must point at the configured public desktop +// (Hosting:PublicBaseUrl) — never at the Host header of the request, or the base URI of the Blazor circuit, that minted +// it: both are client input, and a poisoned origin would hand the token to someone else's server. Only in Development +// (no configured URL) does the caller's own origin stand in. + +using System; +using AgentOs.SharedKernel.Hosting; + +namespace AgentOs.Modules.Tenants.Invitations; + +/// Builds invitation links from the public origin. +public static class InvitationLinks +{ + /// The sign-up path that accepts (relative to the desktop). + public static string PathFor(string token) + { + ArgumentException.ThrowIfNullOrWhiteSpace(token); + return $"/signup?invite={Uri.EscapeDataString(token)}"; + } + + /// The absolute link for : under , or — in + /// Development only — under (the request's origin or the circuit's base URI). + /// Throws when neither applies. + public static string Absolute(PublicOrigin origin, string token, string? callerOrigin) + { + ArgumentNullException.ThrowIfNull(origin); + return origin.BaseUrl(callerOrigin) + PathFor(token); + } +} diff --git a/src/AgentOs.Modules.Tenants/Invitations/InvitationRules.cs b/src/AgentOs.Modules.Tenants/Invitations/InvitationRules.cs new file mode 100644 index 00000000..4862d4e2 --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Invitations/InvitationRules.cs @@ -0,0 +1,88 @@ +// GAP-11 — the rules an invitation obeys, as pure functions (unit-tested without a database or Keycloak): +// • its state: Revoked (an admin withdrew it) > Accepted (used once — single-use) > Expired > Pending; +// • acceptance: only a Pending invitation, and only for the address it was sent to — compared case-insensitively +// with surrounding whitespace ignored (NormalizeEmail), so "Bob@Acme.test " accepts an invitation to bob@acme.test +// and nothing else does; +// • the plain-language reason a person sees when it can't be used. + +using System; + +namespace AgentOs.Modules.Tenants.Invitations; + +/// Why an invitation can or cannot be accepted. +public enum InvitationAcceptance +{ + /// It can be accepted. + Ok = 0, + + /// No such invitation (an unknown, tampered or pre-upgrade link). + NotFound = 1, + + /// Past its expiry. + Expired = 2, + + /// An administrator revoked it. + Revoked = 3, + + /// It was already used (invitations are single-use). + AlreadyUsed = 4, + + /// No email address was given. + EmailRequired = 5, + + /// The address is not the one the invitation was sent to. + EmailMismatch = 6, +} + +/// The pure invitation rules (see the file header). +public static class InvitationRules +{ + /// Longest address accepted (RFC 5321 path limit is 256; 320 = 64 local + @ + 255 domain). + public const int MaxEmailLength = 320; + + /// The form an address is compared in: trimmed and lower-cased (culture-invariant). + public static string NormalizeEmail(string? email) + => string.IsNullOrWhiteSpace(email) ? string.Empty : email.Trim().ToLowerInvariant(); + + /// Where an invitation stands at . + public static InvitationStatus StatusOf(DateTimeOffset expiresAtUtc, DateTimeOffset? acceptedAtUtc, DateTimeOffset? revokedAtUtc, DateTimeOffset now) + { + if (revokedAtUtc is not null) { return InvitationStatus.Revoked; } + if (acceptedAtUtc is not null) { return InvitationStatus.Accepted; } + return now >= expiresAtUtc ? InvitationStatus.Expired : InvitationStatus.Pending; + } + + /// May the person presenting accept an invitation in + /// sent to ? The state is checked first: a used invitation says "already + /// used" whoever asks. + public static InvitationAcceptance CheckAcceptance(InvitationStatus status, string invitedEmailNormalized, string? presentedEmail) + { + if (status != InvitationStatus.Pending) { return ForStatus(status); } + var presented = NormalizeEmail(presentedEmail); + if (presented.Length == 0) { return InvitationAcceptance.EmailRequired; } + return string.Equals(presented, invitedEmailNormalized, StringComparison.Ordinal) + ? InvitationAcceptance.Ok + : InvitationAcceptance.EmailMismatch; + } + + /// The acceptance outcome for a state alone (no address checked) — what a link preview shows. + public static InvitationAcceptance ForStatus(InvitationStatus status) => status switch + { + InvitationStatus.Revoked => InvitationAcceptance.Revoked, + InvitationStatus.Accepted => InvitationAcceptance.AlreadyUsed, + InvitationStatus.Expired => InvitationAcceptance.Expired, + _ => InvitationAcceptance.Ok, + }; + + /// What the invitee is told, in plain words. + public static string Describe(InvitationAcceptance outcome) => outcome switch + { + InvitationAcceptance.Ok => "This invitation can be used.", + InvitationAcceptance.Expired => "This invitation has expired. Ask an administrator of the workspace for a new one.", + InvitationAcceptance.Revoked => "This invitation was withdrawn by an administrator. Ask them for a new one if you still need access.", + InvitationAcceptance.AlreadyUsed => "This invitation has already been used. Each invitation creates one account — sign in instead, or ask for a new invitation.", + InvitationAcceptance.EmailRequired => "Enter the email address the invitation was sent to.", + InvitationAcceptance.EmailMismatch => "This invitation was sent to a different email address. Use the address it was sent to, or ask for an invitation to yours.", + _ => "This invitation link isn't valid. Ask an administrator of the workspace for a new one.", + }; +} diff --git a/src/AgentOs.Modules.Tenants/Invitations/InvitationStore.cs b/src/AgentOs.Modules.Tenants/Invitations/InvitationStore.cs new file mode 100644 index 00000000..cdabe0e9 --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Invitations/InvitationStore.cs @@ -0,0 +1,192 @@ +// GAP-11 — where invitations live. EF (Postgres, tenants.invitations, under RLS) when a database is configured; a +// process-local store for the stateless standalone Web, with the same semantics. Every state change is ONE conditional +// write, so two people racing for one link can't both get in, and a revoke can't be overwritten by an accept: +// • TryClaim: pending → consumed (… WHERE not accepted AND not revoked AND not expired) — the accept's first step; +// • Release: undo a claim whose account creation failed (only while no account is bound to it); +// • Complete: bind the created account to the consumed invitation; +// • TryRevoke: pending/expired → revoked (… WHERE not accepted AND not revoked). + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AgentOs.Modules.Tenants.Persistence; +using AgentOs.Modules.Tenants.Persistence.Entities; +using Microsoft.EntityFrameworkCore; + +namespace AgentOs.Modules.Tenants.Invitations; + +/// One invitation as stored. +internal sealed record InvitationRecord( + Guid Id, + string TenantId, + string Email, + string EmailNormalized, + string Role, + string? CreatedBy, + DateTimeOffset CreatedAtUtc, + DateTimeOffset ExpiresAtUtc, + DateTimeOffset? AcceptedAtUtc = null, + string? AcceptedUserId = null, + DateTimeOffset? RevokedAtUtc = null, + string? RevokedBy = null) +{ + public InvitationStatus StatusAt(DateTimeOffset now) => InvitationRules.StatusOf(ExpiresAtUtc, AcceptedAtUtc, RevokedAtUtc, now); + + public InvitationInfo ToInfo(DateTimeOffset now) => + new(Id, TenantId, Email, Role, CreatedBy, CreatedAtUtc, ExpiresAtUtc, AcceptedAtUtc, RevokedAtUtc, StatusAt(now)); +} + +/// Invitation persistence (see the file header). +internal interface IInvitationStore +{ + Task AddAsync(InvitationRecord invitation, CancellationToken ct = default); + + Task GetAsync(string tenantId, Guid id, CancellationToken ct = default); + + /// The tenant's invitations, newest first. + Task> ListAsync(string tenantId, int max, CancellationToken ct = default); + + /// Consume a pending invitation. False when it was already used, revoked or expired — or another accept won. + Task TryClaimAsync(string tenantId, Guid id, DateTimeOffset now, CancellationToken ct = default); + + /// Bind the account the accept created to the consumed invitation. + Task CompleteAsync(string tenantId, Guid id, string userId, CancellationToken ct = default); + + /// Undo a claim whose accept failed before an account existed, so the invitee can try again. + Task ReleaseAsync(string tenantId, Guid id, CancellationToken ct = default); + + /// Withdraw an invitation that was not used yet. False when it was used or already revoked (or doesn't exist). + Task TryRevokeAsync(string tenantId, Guid id, DateTimeOffset now, string? revokedBy, CancellationToken ct = default); +} + +/// tenants.invitations through the module's DbContext (RLS-bound like every tenant table). +internal sealed class EfInvitationStore(TenantsDbContext db) : IInvitationStore +{ + public async Task AddAsync(InvitationRecord invitation, CancellationToken ct = default) + { + ArgumentNullException.ThrowIfNull(invitation); + var entity = new InvitationEntity + { + Id = invitation.Id, + TenantId = invitation.TenantId, + Email = invitation.Email, + EmailNormalized = invitation.EmailNormalized, + Role = invitation.Role, + CreatedBy = invitation.CreatedBy, + CreatedAtUtc = invitation.CreatedAtUtc, + ExpiresAtUtc = invitation.ExpiresAtUtc, + }; + db.Invitations.Add(entity); + try + { + await db.SaveChangesAsync(ct).ConfigureAwait(false); + } + finally + { + // The context is shared with the rest of the request's scope: never leave this row tracked (a failed save + // would otherwise be retried by the next SaveChanges of an unrelated write). + db.Entry(entity).State = EntityState.Detached; + } + } + + public async Task GetAsync(string tenantId, Guid id, CancellationToken ct = default) + => await db.Invitations.AsNoTracking() + .Where(i => i.Id == id && i.TenantId == tenantId) + .Select(ToRecord) + .FirstOrDefaultAsync(ct).ConfigureAwait(false); + + public async Task> ListAsync(string tenantId, int max, CancellationToken ct = default) + => await db.Invitations.AsNoTracking() + .Where(i => i.TenantId == tenantId) + .OrderByDescending(i => i.CreatedAtUtc) + .Take(Math.Clamp(max, 1, 500)) + .Select(ToRecord) + .ToListAsync(ct).ConfigureAwait(false); + + public async Task TryClaimAsync(string tenantId, Guid id, DateTimeOffset now, CancellationToken ct = default) + => await db.Invitations + .Where(i => i.Id == id && i.TenantId == tenantId && i.AcceptedAtUtc == null && i.RevokedAtUtc == null && i.ExpiresAtUtc > now) + .ExecuteUpdateAsync(s => s.SetProperty(i => i.AcceptedAtUtc, now), ct) + .ConfigureAwait(false) == 1; + + public async Task CompleteAsync(string tenantId, Guid id, string userId, CancellationToken ct = default) + => await db.Invitations + .Where(i => i.Id == id && i.TenantId == tenantId && i.AcceptedAtUtc != null) + .ExecuteUpdateAsync(s => s.SetProperty(i => i.AcceptedUserId, userId), ct) + .ConfigureAwait(false); + + public async Task ReleaseAsync(string tenantId, Guid id, CancellationToken ct = default) + { + DateTimeOffset? unclaimed = null; + await db.Invitations + .Where(i => i.Id == id && i.TenantId == tenantId && i.AcceptedUserId == null) + .ExecuteUpdateAsync(s => s.SetProperty(i => i.AcceptedAtUtc, unclaimed), ct) + .ConfigureAwait(false); + } + + public async Task TryRevokeAsync(string tenantId, Guid id, DateTimeOffset now, string? revokedBy, CancellationToken ct = default) + => await db.Invitations + .Where(i => i.Id == id && i.TenantId == tenantId && i.AcceptedAtUtc == null && i.RevokedAtUtc == null) + .ExecuteUpdateAsync(s => s.SetProperty(i => i.RevokedAtUtc, now).SetProperty(i => i.RevokedBy, revokedBy), ct) + .ConfigureAwait(false) == 1; + + private static readonly System.Linq.Expressions.Expression> ToRecord = i => + new InvitationRecord(i.Id, i.TenantId, i.Email, i.EmailNormalized, i.Role, i.CreatedBy, i.CreatedAtUtc, i.ExpiresAtUtc, + i.AcceptedAtUtc, i.AcceptedUserId, i.RevokedAtUtc, i.RevokedBy); +} + +/// Process-local invitations for a run with no database (the standalone Web) — same rules, one lock. +internal sealed class InMemoryInvitationStore : IInvitationStore +{ + private readonly ConcurrentDictionary _rows = new(); + private readonly Lock _gate = new(); + + public Task AddAsync(InvitationRecord invitation, CancellationToken ct = default) + { + ArgumentNullException.ThrowIfNull(invitation); + _rows[invitation.Id] = invitation; + return Task.CompletedTask; + } + + public Task GetAsync(string tenantId, Guid id, CancellationToken ct = default) + => Task.FromResult(_rows.TryGetValue(id, out var r) && r.TenantId == tenantId ? r : null); + + public Task> ListAsync(string tenantId, int max, CancellationToken ct = default) + => Task.FromResult>( + [.. _rows.Values.Where(r => r.TenantId == tenantId).OrderByDescending(r => r.CreatedAtUtc).Take(Math.Clamp(max, 1, 500))]); + + public Task TryClaimAsync(string tenantId, Guid id, DateTimeOffset now, CancellationToken ct = default) + => Task.FromResult(Update(tenantId, id, + r => r.StatusAt(now) == InvitationStatus.Pending, + r => r with { AcceptedAtUtc = now })); + + public Task CompleteAsync(string tenantId, Guid id, string userId, CancellationToken ct = default) + { + _ = Update(tenantId, id, r => r.AcceptedAtUtc is not null, r => r with { AcceptedUserId = userId }); + return Task.CompletedTask; + } + + public Task ReleaseAsync(string tenantId, Guid id, CancellationToken ct = default) + { + _ = Update(tenantId, id, r => r.AcceptedUserId is null, r => r with { AcceptedAtUtc = null }); + return Task.CompletedTask; + } + + public Task TryRevokeAsync(string tenantId, Guid id, DateTimeOffset now, string? revokedBy, CancellationToken ct = default) + => Task.FromResult(Update(tenantId, id, + r => r.AcceptedAtUtc is null && r.RevokedAtUtc is null, + r => r with { RevokedAtUtc = now, RevokedBy = revokedBy })); + + private bool Update(string tenantId, Guid id, Func when, Func change) + { + lock (_gate) + { + if (!_rows.TryGetValue(id, out var row) || row.TenantId != tenantId || !when(row)) { return false; } + _rows[id] = change(row); + return true; + } + } +} diff --git a/src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminClient.cs b/src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminClient.cs index 227eaf6c..6542d21a 100644 --- a/src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminClient.cs +++ b/src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminClient.cs @@ -95,13 +95,37 @@ public async Task CreateUserAsync( if (!createResp.IsSuccessStatusCode) { var body = await createResp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak create-user failed ({(int)createResp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("create-user", createResp.StatusCode, body); } var location = createResp.Headers.Location?.ToString() - ?? throw new InvalidOperationException("Keycloak create-user response missing Location header."); + ?? throw new KeycloakAdminException("Keycloak create-user response missing Location header."); var userId = location.TrimEnd('/').Split('/').Last(); + // The account exists from here on. Whatever fails next (a role grant, a cancelled request, a dropped + // connection) must not leave it behind: callers take "CreateUserAsync threw" as "no account was created" and + // e.g. hand an invitation back (GAP-11). Delete it before rethrowing; if even that fails, say so with a typed + // exception so the caller can keep its own state consumed instead of offering a second account. + try + { + await FinishUserAsync(userId, realmRoles, sendVerifyEmail, password, token, ct).ConfigureAwait(false); + } + catch (Exception ex) + { + if (!await TryDeleteLeftBehindUserAsync(userId, ex).ConfigureAwait(false)) + { + throw KeycloakUserLeftBehindException.For(userId, ex); + } + throw; + } + + return userId; + } + + // Role grants + the actions email for a user that already exists. Split out so CreateUserAsync can compensate. + private async Task FinishUserAsync( + string userId, IReadOnlyList realmRoles, bool sendVerifyEmail, string? password, string token, CancellationToken ct) + { if (realmRoles.Count > 0) { var roleMappings = new List(realmRoles.Count); @@ -130,7 +154,7 @@ public async Task CreateUserAsync( if (!grantResp.IsSuccessStatusCode) { var body = await grantResp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak grant-roles failed ({(int)grantResp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("grant-roles", grantResp.StatusCode, body); } } } @@ -151,8 +175,27 @@ public async Task CreateUserAsync( userId, actionsResp.StatusCode); } } + } - return userId; + // Compensating delete for a half-created user. Runs under CancellationToken.None: the caller's request may be the + // very thing that was cancelled. Returns false (and logs) when the account is still there. + private async Task TryDeleteLeftBehindUserAsync(string userId, Exception cause) + { + try + { + await DeleteUserAsync(userId, CancellationToken.None).ConfigureAwait(false); + _logger.LogWarning(cause, "Keycloak user {UserId} was removed again because its set-up failed.", LogSafe.Scrub(userId)); + return true; + } + // HttpClient reports its own timeout as a TaskCanceledException even with CancellationToken.None. + catch (Exception deleteEx) when (deleteEx is HttpRequestException or InvalidOperationException or TimeoutException + or TaskCanceledException or JsonException) + { + _logger.LogError(deleteEx, + "Keycloak user {UserId} was created, its set-up failed, and removing it failed too — the account is left behind.", + LogSafe.Scrub(userId)); + return false; + } } /// @@ -168,7 +211,7 @@ public async Task> ListUsersByTenantAsync(string ten if (!listResp.IsSuccessStatusCode) { var body = await listResp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak list-users failed ({(int)listResp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("list-users", listResp.StatusCode, body); } var dtos = await listResp.Content.ReadFromJsonAsync>(ct).ConfigureAwait(false) ?? new List(); @@ -207,7 +250,7 @@ public async Task DeleteUserAsync(string userId, CancellationToken ct = default) if (!resp.IsSuccessStatusCode && resp.StatusCode != System.Net.HttpStatusCode.NotFound) { var body = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak delete-user failed ({(int)resp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("delete-user", resp.StatusCode, body); } } @@ -230,7 +273,7 @@ public async Task UpdateUserRolesAsync(string userId, IReadOnlyList role if (!curResp.IsSuccessStatusCode) { var body = await curResp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak get-role-mappings failed ({(int)curResp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("get-role-mappings", curResp.StatusCode, body); } var current = await curResp.Content.ReadFromJsonAsync>(ct).ConfigureAwait(false) ?? new List(); @@ -247,7 +290,7 @@ public async Task UpdateUserRolesAsync(string userId, IReadOnlyList role if (!delResp.IsSuccessStatusCode) { var body = await delResp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak revoke-roles failed ({(int)delResp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("revoke-roles", delResp.StatusCode, body); } } @@ -274,7 +317,7 @@ public async Task UpdateUserRolesAsync(string userId, IReadOnlyList role if (!grantResp.IsSuccessStatusCode) { var body = await grantResp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak grant-roles failed ({(int)grantResp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("grant-roles", grantResp.StatusCode, body); } } } @@ -291,7 +334,7 @@ public async Task SendPasswordResetEmailAsync(string userId, CancellationToken c if (!resp.IsSuccessStatusCode) { var body = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak password-reset email failed ({(int)resp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("password-reset email", resp.StatusCode, body); } } @@ -303,7 +346,7 @@ public async Task SendPasswordResetEmailAsync(string userId, CancellationToken c using var resp = await _http.SendAsync(req, ct).ConfigureAwait(false); if (!resp.IsSuccessStatusCode) { - throw new InvalidOperationException($"Keycloak serverinfo failed ({(int)resp.StatusCode})."); + throw KeycloakAdminException.Refused("serverinfo", resp.StatusCode); } using var doc = await JsonDocument.ParseAsync(await resp.Content.ReadAsStreamAsync(ct).ConfigureAwait(false), cancellationToken: ct).ConfigureAwait(false); return doc.RootElement.TryGetProperty("systemInfo", out var info) && info.TryGetProperty("version", out var v) @@ -325,7 +368,7 @@ public async Task SendPasswordResetEmailAsync(string userId, CancellationToken c if (!resp.IsSuccessStatusCode) { var body = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak get-user failed ({(int)resp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("get-user", resp.StatusCode, body); } var dto = await resp.Content.ReadFromJsonAsync(ct).ConfigureAwait(false); if (dto?.Attributes is null) @@ -343,7 +386,7 @@ private async Task GetAdminTokenAsync(CancellationToken ct) // every public method funnels through here, so this is the single guard for the whole client. if (_http.BaseAddress is null) { - throw new InvalidOperationException( + throw new KeycloakAdminException( "User management requires Keycloak, which isn't configured in this environment. " + "Run the full stack with `dotnet run --project infra/AgentOs.AppHost` (Aspire wires Keycloak); " + "the standalone Web has no identity backend."); @@ -381,10 +424,10 @@ private async Task GetAdminTokenAsync(CancellationToken ct) if (!resp.IsSuccessStatusCode) { var body = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false); - throw new InvalidOperationException($"Keycloak admin token request failed ({(int)resp.StatusCode}): {body}"); + throw KeycloakAdminException.Refused("admin token request", resp.StatusCode, body); } var dto = await resp.Content.ReadFromJsonAsync(ct).ConfigureAwait(false) - ?? throw new InvalidOperationException("Keycloak admin token response is empty."); + ?? throw new KeycloakAdminException("Keycloak admin token response is empty."); _cachedToken = dto.AccessToken; _cachedTokenExpiresAt = DateTimeOffset.UtcNow.AddSeconds(Math.Max(30, dto.ExpiresIn - 30)); return _cachedToken; diff --git a/src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminException.cs b/src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminException.cs new file mode 100644 index 00000000..a9eafe44 --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Keycloak/KeycloakAdminException.cs @@ -0,0 +1,34 @@ +// Keycloak's admin REST API refused a call. The message carries Keycloak's status and raw response body — for the +// server log and for administrators' own screens, NEVER for an anonymous caller: the body can name realm internals, +// validation rules or other accounts (SignupErrors maps it to a plain product message on the public sign-up paths). +// Derives from InvalidOperationException so existing administrator-side handlers keep catching it. + +using System; +using System.Net; + +namespace AgentOs.Modules.Tenants.Keycloak; + +/// A Keycloak admin API call failed (or Keycloak isn't configured, null). +public sealed class KeycloakAdminException : InvalidOperationException +{ + /// Keycloak's HTTP status, when it answered. + public HttpStatusCode? StatusCode { get; init; } + + /// + public KeycloakAdminException() { } + + /// + public KeycloakAdminException(string message) : base(message) { } + + /// + public KeycloakAdminException(string message, Exception innerException) : base(message, innerException) { } + + /// A refused call: answered with . + public static KeycloakAdminException Refused(string operation, HttpStatusCode status, string? body = null) => + new(body is null + ? $"Keycloak {operation} failed ({(int)status})." + : $"Keycloak {operation} failed ({(int)status}): {body}") + { + StatusCode = status, + }; +} diff --git a/src/AgentOs.Modules.Tenants/Keycloak/KeycloakUserLeftBehindException.cs b/src/AgentOs.Modules.Tenants/Keycloak/KeycloakUserLeftBehindException.cs new file mode 100644 index 00000000..2aee613a --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Keycloak/KeycloakUserLeftBehindException.cs @@ -0,0 +1,34 @@ +// CreateUserAsync is several Keycloak calls (POST /users, then role grants and the actions email). When a step after +// the POST fails, the client deletes the account it just made so that "CreateUserAsync threw" keeps meaning "no account +// exists" — the sign-up saga relies on that to give an invitation back. This exception is what it throws when that +// compensating delete itself failed: the account is still there, and the caller must NOT treat the failure as a clean +// refusal (GAP-11: one invitation ⇔ at most one audited account). + +using System; + +namespace AgentOs.Modules.Tenants.Keycloak; + +/// Creating a Keycloak account failed after the account existed, and removing it failed too: a user with the +/// given id is left behind in the realm. +public sealed class KeycloakUserLeftBehindException : InvalidOperationException +{ + /// The Keycloak id of the account that still exists. + public string? UserId { get; init; } + + /// + public KeycloakUserLeftBehindException() { } + + /// + public KeycloakUserLeftBehindException(string message) : base(message) { } + + /// + public KeycloakUserLeftBehindException(string message, Exception innerException) : base(message, innerException) { } + + /// Names the account that could not be removed; is the failure that made the + /// creation abort. + public static KeycloakUserLeftBehindException For(string userId, Exception cause) => + new($"Keycloak user '{userId}' was created, the remaining set-up failed, and the account could not be removed.", cause) + { + UserId = userId, + }; +} diff --git a/src/AgentOs.Modules.Tenants/Persistence/Entities/AuditEventEntity.cs b/src/AgentOs.Modules.Tenants/Persistence/Entities/AuditEventEntity.cs index e51871e2..e71a7a08 100644 --- a/src/AgentOs.Modules.Tenants/Persistence/Entities/AuditEventEntity.cs +++ b/src/AgentOs.Modules.Tenants/Persistence/Entities/AuditEventEntity.cs @@ -1,4 +1,7 @@ -// Audit-trail row for tenant-scoped mutations. Append-only: writers never UPDATE/DELETE these. +// Audit-trail row for tenant-scoped mutations. Append-only: writers never UPDATE/DELETE these, and the app role may not +// (REVOKE UPDATE, DELETE, TRUNCATE in the RLS bootstrap — GAP-11b). Tamper-evident: each row carries its place in its +// tenant's chain (Sequence 1, 2, 3 …), the previous row's hash and its own (AuditChain). Rows written before the chain +// existed have no Sequence/Hash and are reported as "not chained". // Stored in the tenants schema alongside the registry so the same DbContext owns the migration. using System; @@ -15,4 +18,13 @@ internal sealed class AuditEventEntity public string? IpAddress { get; set; } public string? UserAgent { get; set; } public DateTimeOffset TimestampUtc { get; set; } + + /// Position in the tenant's chain, from 1; null for a row written before the chain (legacy). + public long? Sequence { get; set; } + + /// The previous row's ( for the first). + public string? PrevHash { get; set; } + + /// SHA-256 (hex) over this row's content and — see . + public string? Hash { get; set; } } diff --git a/src/AgentOs.Modules.Tenants/Persistence/Entities/InvitationEntity.cs b/src/AgentOs.Modules.Tenants/Persistence/Entities/InvitationEntity.cs new file mode 100644 index 00000000..1da50435 --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Persistence/Entities/InvitationEntity.cs @@ -0,0 +1,33 @@ +// GAP-11 — one invitation = one row. The emailed token carries only this row's id (and its tenant, signed), so an +// invitation is single-use (consumed on accept), bound to the invited address (EmailNormalized), revocable by a tenant +// admin before it is used, and expires as before. Tenant-scoped: TenantId on every row, under RLS (ADR-0012). + +using System; + +namespace AgentOs.Modules.Tenants.Persistence.Entities; + +internal sealed class InvitationEntity +{ + public Guid Id { get; set; } + public string TenantId { get; set; } = string.Empty; + + /// The invited address as the admin typed it (trimmed) — what the invitee sees and signs up with. + public string Email { get; set; } = string.Empty; + + /// of — what acceptance compares. + public string EmailNormalized { get; set; } = string.Empty; + + public string Role { get; set; } = "member"; + public string? CreatedBy { get; set; } + public DateTimeOffset CreatedAtUtc { get; set; } + public DateTimeOffset ExpiresAtUtc { get; set; } + + /// Set when the invitation is consumed (claimed at the start of an accept; released if that accept fails). + public DateTimeOffset? AcceptedAtUtc { get; set; } + + /// The account the invitation created (Keycloak user id), once the accept completed. + public string? AcceptedUserId { get; set; } + + public DateTimeOffset? RevokedAtUtc { get; set; } + public string? RevokedBy { get; set; } +} diff --git a/src/AgentOs.Modules.Tenants/Persistence/Migrations/20261103132559_AddInvitationsAndAuditChain.Designer.cs b/src/AgentOs.Modules.Tenants/Persistence/Migrations/20261103132559_AddInvitationsAndAuditChain.Designer.cs new file mode 100644 index 00000000..c22caf6f --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Persistence/Migrations/20261103132559_AddInvitationsAndAuditChain.Designer.cs @@ -0,0 +1,221 @@ +// +using System; +using AgentOs.Modules.Tenants.Persistence; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AgentOs.Modules.Tenants.Persistence.Migrations +{ + [DbContext(typeof(TenantsDbContext))] + [Migration("20261103132559_AddInvitationsAndAuditChain")] + partial class AddInvitationsAndAuditChain + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("tenants") + .HasAnnotation("ProductVersion", "10.0.14") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("AgentOs.Modules.Tenants.ApiTokens.ApiTokenEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CreatedBy") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("ExpiresAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("LastUsedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("Prefix") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("RevokedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Role") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("TenantId") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("TokenHash") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.HasKey("Id"); + + b.HasIndex("Prefix") + .IsUnique(); + + b.HasIndex("TenantId"); + + b.ToTable("api_tokens", "tenants"); + }); + + modelBuilder.Entity("AgentOs.Modules.Tenants.Persistence.Entities.AuditEventEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Action") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("Hash") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("IpAddress") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("PrevHash") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("Sequence") + .HasColumnType("bigint"); + + b.Property("Target") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("TenantId") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("TimestampUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("UserAgent") + .HasMaxLength(512) + .HasColumnType("character varying(512)"); + + b.Property("UserId") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("TenantId", "Sequence") + .IsUnique(); + + b.HasIndex("TenantId", "TimestampUtc"); + + b.ToTable("audit_events", "tenants"); + }); + + modelBuilder.Entity("AgentOs.Modules.Tenants.Persistence.Entities.InvitationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AcceptedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("AcceptedUserId") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("CreatedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CreatedBy") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("Email") + .IsRequired() + .HasMaxLength(320) + .HasColumnType("character varying(320)"); + + b.Property("EmailNormalized") + .IsRequired() + .HasMaxLength(320) + .HasColumnType("character varying(320)"); + + b.Property("ExpiresAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("RevokedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("RevokedBy") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("Role") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("TenantId") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.HasKey("Id"); + + b.HasIndex("TenantId", "CreatedAtUtc"); + + b.ToTable("invitations", "tenants"); + }); + + modelBuilder.Entity("AgentOs.Modules.Tenants.Persistence.Entities.TenantEntity", b => + { + b.Property("Id") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("CreatedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.ToTable("tenants", "tenants"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/AgentOs.Modules.Tenants/Persistence/Migrations/20261103132559_AddInvitationsAndAuditChain.cs b/src/AgentOs.Modules.Tenants/Persistence/Migrations/20261103132559_AddInvitationsAndAuditChain.cs new file mode 100644 index 00000000..8efc53da --- /dev/null +++ b/src/AgentOs.Modules.Tenants/Persistence/Migrations/20261103132559_AddInvitationsAndAuditChain.cs @@ -0,0 +1,102 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace AgentOs.Modules.Tenants.Persistence.Migrations +{ + /// + public partial class AddInvitationsAndAuditChain : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Hash", + schema: "tenants", + table: "audit_events", + type: "character varying(64)", + maxLength: 64, + nullable: true); + + migrationBuilder.AddColumn( + name: "PrevHash", + schema: "tenants", + table: "audit_events", + type: "character varying(64)", + maxLength: 64, + nullable: true); + + migrationBuilder.AddColumn( + name: "Sequence", + schema: "tenants", + table: "audit_events", + type: "bigint", + nullable: true); + + migrationBuilder.CreateTable( + name: "invitations", + schema: "tenants", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + TenantId = table.Column(type: "character varying(64)", maxLength: 64, nullable: false), + Email = table.Column(type: "character varying(320)", maxLength: 320, nullable: false), + EmailNormalized = table.Column(type: "character varying(320)", maxLength: 320, nullable: false), + Role = table.Column(type: "character varying(16)", maxLength: 16, nullable: false), + CreatedBy = table.Column(type: "character varying(128)", maxLength: 128, nullable: true), + CreatedAtUtc = table.Column(type: "timestamp with time zone", nullable: false), + ExpiresAtUtc = table.Column(type: "timestamp with time zone", nullable: false), + AcceptedAtUtc = table.Column(type: "timestamp with time zone", nullable: true), + AcceptedUserId = table.Column(type: "character varying(128)", maxLength: 128, nullable: true), + RevokedAtUtc = table.Column(type: "timestamp with time zone", nullable: true), + RevokedBy = table.Column(type: "character varying(128)", maxLength: 128, nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_invitations", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_audit_events_TenantId_Sequence", + schema: "tenants", + table: "audit_events", + columns: new[] { "TenantId", "Sequence" }, + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_invitations_TenantId_CreatedAtUtc", + schema: "tenants", + table: "invitations", + columns: new[] { "TenantId", "CreatedAtUtc" }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "invitations", + schema: "tenants"); + + migrationBuilder.DropIndex( + name: "IX_audit_events_TenantId_Sequence", + schema: "tenants", + table: "audit_events"); + + migrationBuilder.DropColumn( + name: "Hash", + schema: "tenants", + table: "audit_events"); + + migrationBuilder.DropColumn( + name: "PrevHash", + schema: "tenants", + table: "audit_events"); + + migrationBuilder.DropColumn( + name: "Sequence", + schema: "tenants", + table: "audit_events"); + } + } +} diff --git a/src/AgentOs.Modules.Tenants/Persistence/Migrations/TenantsDbContextModelSnapshot.cs b/src/AgentOs.Modules.Tenants/Persistence/Migrations/TenantsDbContextModelSnapshot.cs index 5a5fe6a3..6d5af767 100644 --- a/src/AgentOs.Modules.Tenants/Persistence/Migrations/TenantsDbContextModelSnapshot.cs +++ b/src/AgentOs.Modules.Tenants/Persistence/Migrations/TenantsDbContextModelSnapshot.cs @@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasDefaultSchema("tenants") - .HasAnnotation("ProductVersion", "10.0.13") + .HasAnnotation("ProductVersion", "10.0.14") .HasAnnotation("Relational:MaxIdentifierLength", 63); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); @@ -92,10 +92,21 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasMaxLength(64) .HasColumnType("character varying(64)"); + b.Property("Hash") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + b.Property("IpAddress") .HasMaxLength(64) .HasColumnType("character varying(64)"); + b.Property("PrevHash") + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("Sequence") + .HasColumnType("bigint"); + b.Property("Target") .HasMaxLength(256) .HasColumnType("character varying(256)"); @@ -118,11 +129,71 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasKey("Id"); + b.HasIndex("TenantId", "Sequence") + .IsUnique(); + b.HasIndex("TenantId", "TimestampUtc"); b.ToTable("audit_events", "tenants"); }); + modelBuilder.Entity("AgentOs.Modules.Tenants.Persistence.Entities.InvitationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AcceptedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("AcceptedUserId") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("CreatedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("CreatedBy") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("Email") + .IsRequired() + .HasMaxLength(320) + .HasColumnType("character varying(320)"); + + b.Property("EmailNormalized") + .IsRequired() + .HasMaxLength(320) + .HasColumnType("character varying(320)"); + + b.Property("ExpiresAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("RevokedAtUtc") + .HasColumnType("timestamp with time zone"); + + b.Property("RevokedBy") + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.Property("Role") + .IsRequired() + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("TenantId") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.HasKey("Id"); + + b.HasIndex("TenantId", "CreatedAtUtc"); + + b.ToTable("invitations", "tenants"); + }); + modelBuilder.Entity("AgentOs.Modules.Tenants.Persistence.Entities.TenantEntity", b => { b.Property("Id") diff --git a/src/AgentOs.Modules.Tenants/Persistence/Repositories/AuditLog.cs b/src/AgentOs.Modules.Tenants/Persistence/Repositories/AuditLog.cs index 3fde1a2f..21c60178 100644 --- a/src/AgentOs.Modules.Tenants/Persistence/Repositories/AuditLog.cs +++ b/src/AgentOs.Modules.Tenants/Persistence/Repositories/AuditLog.cs @@ -1,6 +1,20 @@ -// EF-backed IAuditLog. Writes are best-effort: a DB outage must not break the surrounding -// signup / invitation flow, so the writer catches every exception and logs it. Reads are -// tenant-scoped and bounded by `max` — no cross-tenant peek by construction. +// EF-backed IAuditLog + IAuditChainVerifier. Writes are best-effort: a DB outage must not break the surrounding +// signup / invitation flow, so the writer catches every database exception and logs it; an entry that names no tenant +// (a caller bug — it belongs to no chain) is refused the same way. Either way the lost row is counted +// (AuditTelemetry: `agentos.audit.lost{reason}`) and the caller never sees an exception. Reads are tenant-scoped and +// bounded by `max` — no cross-tenant peek by construction. +// GAP-11b — append-only and tamper-evident: +// • each write extends its tenant's hash chain (AuditChain): under a per-tenant transaction advisory lock it reads the +// chain's head, takes the next Sequence and stores PrevHash + Hash. A unique (TenantId, Sequence) index is the +// backstop — two writers can never both take a position, so the chain never forks (a loser re-reads and retries). +// Values are clipped to their columns and the timestamp to the microsecond BEFORE hashing, so the stored row is +// exactly what was hashed; +// • the app role may only INSERT and SELECT here (REVOKE UPDATE, DELETE, TRUNCATE — RowLevelSecurity.AppendOnlyTables); +// • RLS (ADR-0012 stage 2): an entry is written under its OWN tenant (it names it — a sign-up is anonymous, a platform +// admin's export of another tenant is filed under that tenant); "_platform" rows are written and read only inside a +// platform scope. A tenant read binds a flow that has no tenant of its own to the tenant asked for. +// • the verifier also looks for an unchained row (Sequence null) recorded at or after the chain's first event: the +// writer never makes one, so it is an insert that bypassed the chain and breaks it (AuditChainProblem.Inserted). using System; using System.Collections.Generic; @@ -13,37 +27,75 @@ using Dapper; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; +using Npgsql; namespace AgentOs.Modules.Tenants.Persistence.Repositories; internal sealed class EfAuditLog( TenantsDbContext db, ILogger logger, - INpgsqlConnectionFactory? connectionFactory = null) : IAuditLog + INpgsqlConnectionFactory? connectionFactory = null) : IAuditLog, IAuditChainVerifier { + // Writers queue on the chain lock; the retries only cover one that didn't take it (e.g. mid rolling deploy). + private const int MaxAttempts = 5; + private const string ChainLockPrefix = "agentos:audit-chain:"; + public async Task WriteAsync(AuditEntry entry, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(entry); + if (string.IsNullOrWhiteSpace(entry.TenantId)) + { + // Never throw out of a best-effort write: the surrounding operation already happened. Not echoing the + // action label, for the reason given in Handle below. + AuditTelemetry.RecordLost(AuditTelemetry.Reasons.NoTenant); + logger.LogError("Audit entry {EntryId} names no tenant, so it was not recorded — surrounding op continues.", entry.Id); + return; + } + using var scope = WriteScope(entry.TenantId); try { - db.AuditEvents.Add(new AuditEventEntity + for (var attempt = 1; ; attempt++) { - Id = entry.Id == Guid.Empty ? Guid.NewGuid() : entry.Id, - TenantId = entry.TenantId, - UserId = entry.UserId, - Action = entry.Action, - Target = entry.Target, - IpAddress = entry.IpAddress, - UserAgent = entry.UserAgent, - TimestampUtc = entry.TimestampUtc == default ? DateTimeOffset.UtcNow : entry.TimestampUtc, - }); - await db.SaveChangesAsync(ct).ConfigureAwait(false); + // One writer per chain at a time, across hosts and replicas: a transaction-scoped advisory lock keyed on the + // tenant serialises "read the head → insert the next link". EF Core has no API for advisory locks, hence + // this one parameterized raw statement (a system function — no table, no tenant data). The unique + // (TenantId, Sequence) index stays the backstop: a writer that skipped the lock collides and retries. + await using var tx = db.Database.IsRelational() + ? await db.Database.BeginTransactionAsync(ct).ConfigureAwait(false) + : null; + if (tx is not null) + { + await db.Database.ExecuteSqlAsync( + $"SELECT pg_advisory_xact_lock(hashtextextended({ChainLockPrefix + entry.TenantId}, 0))", ct).ConfigureAwait(false); + } + var row = await NextLinkAsync(entry, ct).ConfigureAwait(false); + db.AuditEvents.Add(row); + try + { + await db.SaveChangesAsync(ct).ConfigureAwait(false); + if (tx is not null) { await tx.CommitAsync(ct).ConfigureAwait(false); } + return; + } + catch (DbUpdateException ex) when (attempt < MaxAttempts && IsChainCollision(ex)) + { + // Another writer took this position first: re-read the head and try the next one. + continue; + } + finally + { + // The context is shared with the request's other writes: never leave this row tracked (a failed save + // would otherwise be retried — with a stale chain position — by the next unrelated SaveChanges). + db.Entry(row).State = EntityState.Detached; + } + } } catch (DbUpdateException ex) { Handle(ex); } catch (System.Data.Common.DbException ex) { Handle(ex); } catch (InvalidOperationException ex) { Handle(ex); } - void Handle(Exception e) => + void Handle(Exception e) + { + AuditTelemetry.RecordLost(AuditTelemetry.Reasons.Database); // The action label is intentionally NOT echoed here: some action constants are named // after sensitive operations (e.g. password reset), which trips static-analysis secret // heuristics, and the action is already captured in the persisted audit record. Tenant + @@ -51,11 +103,38 @@ void Handle(Exception e) => logger.LogWarning(e, "Audit write failed for tenant={TenantId} — surrounding op continues.", LogSafe.Scrub(entry.TenantId)); + } + } + + // The next row of the entry's tenant chain: head + 1, linked to the head's hash, hashed over the stored values. + private async Task NextLinkAsync(AuditEntry entry, CancellationToken ct) + { + var head = await db.AuditEvents.AsNoTracking() + .Where(e => e.TenantId == entry.TenantId && e.Sequence != null) + .OrderByDescending(e => e.Sequence) + .Select(e => new { e.Sequence, e.Hash }) + .FirstOrDefaultAsync(ct).ConfigureAwait(false); + var row = new AuditEventEntity + { + Id = entry.Id == Guid.Empty ? Guid.NewGuid() : entry.Id, + TenantId = entry.TenantId, + UserId = Clip(entry.UserId, 128), + Action = Clip(entry.Action, 64)!, + Target = Clip(entry.Target, 256), + IpAddress = Clip(entry.IpAddress, 64), + UserAgent = Clip(entry.UserAgent, 512), + TimestampUtc = AuditChain.Normalize(entry.TimestampUtc == default ? DateTimeOffset.UtcNow : entry.TimestampUtc), + Sequence = (head?.Sequence ?? 0) + 1, + PrevHash = head?.Hash ?? AuditChain.Genesis, + }; + row.Hash = AuditChain.Compute(LinkOf(row)); + return row; } public async Task> ListAsync(string tenantId, int max = 100, CancellationToken ct = default) { ArgumentException.ThrowIfNullOrWhiteSpace(tenantId); + using var scope = ReadScope(tenantId); if (connectionFactory is not null) { @@ -66,38 +145,103 @@ public async Task> ListAsync(string tenantId, int max .Where(e => e.TenantId == tenantId) .OrderByDescending(e => e.TimestampUtc) .Take(max) - .Select(e => new AuditEntry(e.Id, e.TenantId, e.UserId, e.Action, e.Target, e.IpAddress, e.UserAgent, e.TimestampUtc)) + .Select(e => new AuditEntry(e.Id, e.TenantId, e.UserId, e.Action, e.Target, e.IpAddress, e.UserAgent, e.TimestampUtc, e.Sequence, e.Hash)) .ToListAsync(ct).ConfigureAwait(false); return rows; } + public async Task VerifyAsync(string tenantId, CancellationToken ct = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(tenantId); + using var scope = ReadScope(tenantId); + var unchained = await db.AuditEvents.AsNoTracking() + .LongCountAsync(e => e.TenantId == tenantId && e.Sequence == null, ct).ConfigureAwait(false); + var stray = unchained == 0 ? null : await StrayAsync(tenantId, ct).ConfigureAwait(false); + // Oldest first, streamed off one cursor (the (TenantId, Sequence) index serves it): O(1) memory however long. + var links = db.AuditEvents.AsNoTracking() + .Where(e => e.TenantId == tenantId && e.Sequence != null) + .OrderBy(e => e.Sequence) + .Select(e => new AuditChainLink(e.Sequence!.Value, e.Id, e.TenantId, e.TimestampUtc, e.UserId, e.Action, e.Target, + e.IpAddress, e.UserAgent, e.PrevHash ?? string.Empty, e.Hash ?? string.Empty)) + .AsAsyncEnumerable(); + return await AuditChain.VerifyAsync(tenantId, links, unchained, stray, ct).ConfigureAwait(false); + } + + // The earliest unchained row recorded at or after the chain's first event (both reads served by the tenant indexes). + private async Task StrayAsync(string tenantId, CancellationToken ct) + { + var start = await db.AuditEvents.AsNoTracking() + .Where(e => e.TenantId == tenantId && e.Sequence != null) + .OrderBy(e => e.Sequence) + .Select(e => new { e.TimestampUtc }) + .FirstOrDefaultAsync(ct).ConfigureAwait(false); + if (start is null) { return null; } + var row = await db.AuditEvents.AsNoTracking() + .Where(e => e.TenantId == tenantId && e.Sequence == null && e.TimestampUtc >= start.TimestampUtc) + .OrderBy(e => e.TimestampUtc).ThenBy(e => e.Id) + .Select(e => new { e.Id, e.TimestampUtc }) + .FirstOrDefaultAsync(ct).ConfigureAwait(false); + return row is null ? null : new AuditChainStray(row.Id, row.TimestampUtc); + } + // Dapper fast-path: audit_events under the "tenants" schema; the (TenantId, TimestampUtc) index - // serves the filter + ordering. LIMIT is clamped to ≥0 to match EF's Take semantics. + // serves the filter + ordering. LIMIT is clamped to ≥0 to match EF's Take semantics. RLS-bound like the EF path. private async Task> ListViaDapperAsync(string tenantId, int max, CancellationToken ct) { const string sql = """ - SELECT "Id", "TenantId", "UserId", "Action", "Target", "IpAddress", "UserAgent", "TimestampUtc" + SELECT "Id", "TenantId", "UserId", "Action", "Target", "IpAddress", "UserAgent", "TimestampUtc", "Sequence", "Hash" FROM tenants.audit_events WHERE "TenantId" = @tenantId ORDER BY "TimestampUtc" DESC LIMIT @max """; - await using var conn = connectionFactory!.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await connectionFactory!.OpenWithTenantAsync(ct).ConfigureAwait(false); var rows = await conn.QueryAsync( new CommandDefinition(sql, new { tenantId, max = Math.Max(0, max) }, cancellationToken: ct)) .ConfigureAwait(false); return rows.Select(e => new AuditEntry( - e.Id, e.TenantId, e.UserId, e.Action, e.Target, e.IpAddress, e.UserAgent, e.TimestampUtc)).ToList(); + e.Id, e.TenantId, e.UserId, e.Action, e.Target, e.IpAddress, e.UserAgent, e.TimestampUtc, e.Sequence, e.Hash)).ToList(); + } + + internal static AuditChainLink LinkOf(AuditEventEntity e) + => new(e.Sequence ?? 0, e.Id, e.TenantId, e.TimestampUtc, e.UserId, e.Action, e.Target, e.IpAddress, e.UserAgent, + e.PrevHash ?? string.Empty, e.Hash ?? string.Empty); + + // An entry is filed under its own tenant; "_platform" rows only in a platform scope. + private static IDisposable WriteScope(string tenantId) + => string.Equals(tenantId, AuditScopes.Platform, StringComparison.Ordinal) + ? RowLevelSecurity.PlatformScope("audit-platform") + : RowLevelSecurity.TenantScope(tenantId); + + // A tenant read never widens a flow that acts for a tenant; it binds one that acts for none. + private static IDisposable? ReadScope(string tenantId) + => string.Equals(tenantId, AuditScopes.Platform, StringComparison.Ordinal) + ? RowLevelSecurity.PlatformScope("audit-platform") + : RowLevelSecurity.TenantScopeIfUnbound(tenantId); + + // A unique violation on (TenantId, Sequence): a concurrent writer extended the chain first. + private static bool IsChainCollision(DbUpdateException ex) + => ex.InnerException is PostgresException { SqlState: PostgresErrorCodes.UniqueViolation } pg + && (pg.ConstraintName?.Contains("Sequence", StringComparison.OrdinalIgnoreCase) ?? true); + + // Clip to the column (never splitting a surrogate pair) so the stored value is exactly the hashed one. + internal static string? Clip(string? value, int max) + { + if (value is null || value.Length <= max) { return value; } + var cut = char.IsHighSurrogate(value[max - 1]) ? max - 1 : max; + return value[..cut]; } } -internal sealed class NullAuditLog : IAuditLog +internal sealed class NullAuditLog : IAuditLog, IAuditChainVerifier { public Task WriteAsync(AuditEntry entry, CancellationToken ct = default) => Task.CompletedTask; public Task> ListAsync(string tenantId, int max = 100, CancellationToken ct = default) => Task.FromResult>([]); + + public Task VerifyAsync(string tenantId, CancellationToken ct = default) => + Task.FromResult(new AuditChainReport(tenantId, 0, 0, null, null, null, AuditChainProblem.None)); } diff --git a/src/AgentOs.Modules.Tenants/Persistence/TenantsDbContext.cs b/src/AgentOs.Modules.Tenants/Persistence/TenantsDbContext.cs index 448264e6..d376d1fc 100644 --- a/src/AgentOs.Modules.Tenants/Persistence/TenantsDbContext.cs +++ b/src/AgentOs.Modules.Tenants/Persistence/TenantsDbContext.cs @@ -1,5 +1,7 @@ // EF Core DbContext for the Tenants module. Owns `tenants.tenants` (admin-scoped registry, no -// global query filter — every admin endpoint guards on the Admin policy). +// global query filter — every admin endpoint guards on the Admin policy), the audit trail (append-only, hash-chained) +// and invitations; audit_events + invitations are under RLS (ADR-0012), api_tokens is exempt (looked up by prefix +// before any tenant is known). using AgentOs.Modules.Tenants.Persistence.Entities; using Microsoft.EntityFrameworkCore; @@ -16,6 +18,7 @@ public TenantsDbContext(DbContextOptions options) public DbSet Tenants => Set(); public DbSet AuditEvents => Set(); public DbSet ApiTokens => Set(); + public DbSet Invitations => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { @@ -42,6 +45,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) e.Property(x => x.IpAddress).HasMaxLength(64); e.Property(x => x.UserAgent).HasMaxLength(512); e.HasIndex(x => new { x.TenantId, x.TimestampUtc }); + // GAP-11b hash chain: one row per (tenant, position) — two writers racing for the same position cannot both + // land (the loser retries on the new head), so the chain never forks. Legacy rows (Sequence NULL) are exempt: + // NULLs are distinct in a Postgres unique index. + e.Property(x => x.PrevHash).HasMaxLength(64); + e.Property(x => x.Hash).HasMaxLength(64); + e.HasIndex(x => new { x.TenantId, x.Sequence }).IsUnique(); + }); + + // GAP-11: single-use, email-bound, revocable invitations (the token carries only the id + tenant). + modelBuilder.Entity(e => + { + e.ToTable("invitations"); + e.HasKey(x => x.Id); + e.Property(x => x.TenantId).HasMaxLength(64).IsRequired(); + e.Property(x => x.Email).HasMaxLength(320).IsRequired(); + e.Property(x => x.EmailNormalized).HasMaxLength(320).IsRequired(); + e.Property(x => x.Role).HasMaxLength(16).IsRequired(); + e.Property(x => x.CreatedBy).HasMaxLength(128); + e.Property(x => x.AcceptedUserId).HasMaxLength(128); + e.Property(x => x.RevokedBy).HasMaxLength(128); + e.HasIndex(x => new { x.TenantId, x.CreatedAtUtc }); }); // E1.3: API tokens — only the SHA-256 is stored; Prefix is the (unique) lookup key at authentication. diff --git a/src/AgentOs.Modules.Tenants/SignupErrors.cs b/src/AgentOs.Modules.Tenants/SignupErrors.cs new file mode 100644 index 00000000..c3fca99d --- /dev/null +++ b/src/AgentOs.Modules.Tenants/SignupErrors.cs @@ -0,0 +1,53 @@ +// What an anonymous person sees when sign-up fails (POST /tenants/register and the desktop's /signup page). Our own +// validation messages ("Username is required.", an invitation's plain-language refusal) are written for them and pass +// through; a failure of the identity service never does — Keycloak's raw response body can name realm internals, +// password-policy details or other accounts. Those become one plain product message and the detail stays in the +// server log (the caller logs the exception). + +using System; +using System.Net; +using System.Net.Http; +using AgentOs.Modules.Tenants.Keycloak; +using Microsoft.AspNetCore.Http; + +namespace AgentOs.Modules.Tenants; + +/// Maps a sign-up failure to the message an anonymous caller may see. +public static class SignupErrors +{ + /// Shown when the username or address is already taken (Keycloak 409). + public const string Taken = "That username or email address is already in use. Choose another one, or sign in."; + + /// Shown when the identity service refused or failed for any other reason. + public const string Unavailable = "Your account couldn't be created right now. Please try again in a few minutes."; + + /// Shown when the identity service could not be reached. + public const string Unreachable = "Could not reach the identity service. Please try again."; + + /// True when is a failure of the identity service (its detail is for the log only), + /// false for the product's own validation messages. + public static bool IsIdentityFailure(Exception ex) => + ex is KeycloakAdminException or KeycloakUserLeftBehindException or HttpRequestException or TimeoutException + or OperationCanceledException; + + /// The message an anonymous caller sees for . + public static string ForPublic(Exception ex) + { + ArgumentNullException.ThrowIfNull(ex); + return ex switch + { + KeycloakAdminException { StatusCode: HttpStatusCode.Conflict } => Taken, + KeycloakAdminException or KeycloakUserLeftBehindException => Unavailable, + HttpRequestException or TimeoutException or OperationCanceledException => Unreachable, + _ => ex.Message, + }; + } + + /// The HTTP status for on the public sign-up endpoint. + public static int StatusFor(Exception ex) => ex switch + { + KeycloakAdminException { StatusCode: HttpStatusCode.Conflict } => StatusCodes.Status409Conflict, + _ when IsIdentityFailure(ex) => StatusCodes.Status503ServiceUnavailable, + _ => StatusCodes.Status400BadRequest, + }; +} diff --git a/src/AgentOs.Modules.Tenants/TenantSignupService.cs b/src/AgentOs.Modules.Tenants/TenantSignupService.cs index 3c02140f..cb98006d 100644 --- a/src/AgentOs.Modules.Tenants/TenantSignupService.cs +++ b/src/AgentOs.Modules.Tenants/TenantSignupService.cs @@ -1,6 +1,14 @@ // Default impl of ITenantSignupService. Keycloak-first ordering with rollback so a registry-row -// failure never leaves the tenant claim orphaned without a corresponding row. Invitations are -// time-limited DataProtection payloads serialized as JSON, no persistence required. +// failure never leaves the tenant claim orphaned without a corresponding row. +// Invitations (GAP-11) are rows in IInvitationStore. The emailed token is a time-limited DataProtection envelope around +// {invitation id, tenant} only — the row is the authority for role, address and state. Accepting one: +// read the signed token → bind RLS to the tenant it names (the invitee is anonymous) → the row must be Pending and the +// address must be the invited one (InvitationRules) → CLAIM it (one conditional UPDATE: a second accept, a revoke or +// the expiry can't race it) → create the Keycloak account (under CancellationToken.None: the claim is the point of no +// return) → bind the account to the row. The claim is given back only when Keycloak refused and no account exists +// (KeycloakAdminClient deletes a half-made one before throwing); a cancelled or left-behind creation keeps the +// invitation used so one link can never yield two accounts. Every refused or failed attempt is audited. +// Tokens minted before this change (purpose …Invitation.v1: stateless and reusable) no longer open — re-invite. using System; using System.Collections.Generic; @@ -10,8 +18,10 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; +using AgentOs.Modules.Tenants.Invitations; using AgentOs.Modules.Tenants.Keycloak; using AgentOs.SharedKernel.Logging; +using AgentOs.SharedKernel.Persistence; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Logging; @@ -19,7 +29,10 @@ namespace AgentOs.Modules.Tenants; internal sealed partial class TenantSignupService : ITenantSignupService { - private const string ProtectorPurpose = "AgentOs.Modules.Tenants.Invitation.v1"; + private const string ProtectorPurpose = "AgentOs.Modules.Tenants.Invitation.v2"; + + /// The longest an invitation may live (7 days). + public static readonly TimeSpan MaxInvitationLifetime = TimeSpan.FromHours(168); private readonly ITenantsRepository _repo; private readonly IKeycloakAdminClient _kc; @@ -27,6 +40,8 @@ internal sealed partial class TenantSignupService : ITenantSignupService private readonly ITimeLimitedDataProtector _protector; private readonly ILogger _logger; private readonly AgentOs.Domain.Configuration.ISettings? _settings; + private readonly IInvitationStore _invitations; + private readonly TimeProvider _clock; public TenantSignupService( ITenantsRepository repo, @@ -34,7 +49,9 @@ public TenantSignupService( IAuditLog audit, IDataProtectionProvider dp, ILogger logger, - AgentOs.Domain.Configuration.ISettings? settings = null) + AgentOs.Domain.Configuration.ISettings? settings = null, + IInvitationStore? invitations = null, + TimeProvider? clock = null) { ArgumentNullException.ThrowIfNull(dp); _repo = repo; @@ -42,6 +59,8 @@ public TenantSignupService( _audit = audit; _logger = logger; _settings = settings; + _invitations = invitations ?? new InMemoryInvitationStore(); + _clock = clock ?? TimeProvider.System; _protector = dp.CreateProtector(ProtectorPurpose).ToTimeLimitedDataProtector(); } @@ -81,27 +100,18 @@ public async Task SignupAsync(TenantSignupRequest request, } if (!string.IsNullOrEmpty(request.Email)) { - var (emailOk, emailErr) = SignupValidation.ValidateEmail(request.Email); + var (emailOk, emailErr) = SignupValidation.ValidateEmail(request.Email.Trim()); if (!emailOk) { throw new InvalidOperationException(emailErr!); } } - string tenantId; - string role; - SignupMode mode; - bool createTenantRow; - if (!string.IsNullOrWhiteSpace(request.InviteToken)) { - var preview = PreviewInvitation(request.InviteToken) - ?? throw new InvalidOperationException("Invitation is invalid or expired."); - var existing = await _repo.GetAsync(preview.TenantId, ct).ConfigureAwait(false) - ?? throw new InvalidOperationException($"Invitation tenant '{preview.TenantId}' no longer exists."); - tenantId = existing.Id; - role = preview.Role; - mode = SignupMode.Invite; - createTenantRow = false; + return await SignupWithInvitationAsync(request, request.InviteToken, ct).ConfigureAwait(false); } - else if (!string.IsNullOrWhiteSpace(request.TenantId)) + + string tenantId; + SignupMode mode; + if (!string.IsNullOrWhiteSpace(request.TenantId)) { var (slugOk, slugErr) = SignupValidation.ValidateTenantId(request.TenantId); if (!slugOk) { throw new InvalidOperationException(slugErr!); } @@ -111,115 +121,286 @@ public async Task SignupAsync(TenantSignupRequest request, throw new InvalidOperationException($"Workspace '{request.TenantId}' already exists."); } tenantId = request.TenantId; - role = "admin"; mode = SignupMode.Slug; - createTenantRow = true; } else { tenantId = await GenerateUniqueSlugAsync(request.Username, ct).ConfigureAwait(false); - role = "admin"; mode = SignupMode.AutoCreate; - createTenantRow = true; } - // An invited approver is a member too (TenantRoles.Expand). - var roles = TenantRoles.Expand([role]).ToArray(); - var sendVerify = !string.IsNullOrWhiteSpace(request.Email); - string kcUserId = await _kc.CreateUserAsync( username: request.Username, - email: request.Email ?? string.Empty, + email: request.Email?.Trim() ?? string.Empty, tenantId: tenantId, - realmRoles: roles, - sendVerifyEmail: sendVerify, + realmRoles: AdminRoles, + sendVerifyEmail: !string.IsNullOrWhiteSpace(request.Email), password: request.Password, ct: ct).ConfigureAwait(false); - if (createTenantRow) + try { - try - { - var record = new TenantRecord( - tenantId, - string.IsNullOrWhiteSpace(request.TenantName) ? tenantId : request.TenantName, - DateTimeOffset.UtcNow); - await _repo.AddAsync(record, ct).ConfigureAwait(false); - } - catch (Microsoft.EntityFrameworkCore.DbUpdateException dbEx) { await HandleAsync(dbEx).ConfigureAwait(false); } - catch (System.Data.Common.DbException dbEx) { await HandleAsync(dbEx).ConfigureAwait(false); } - catch (InvalidOperationException dbEx) { await HandleAsync(dbEx).ConfigureAwait(false); } + var record = new TenantRecord( + tenantId, + string.IsNullOrWhiteSpace(request.TenantName) ? tenantId : request.TenantName, + DateTimeOffset.UtcNow); + await _repo.AddAsync(record, ct).ConfigureAwait(false); + } + catch (Microsoft.EntityFrameworkCore.DbUpdateException dbEx) { await HandleAsync(dbEx).ConfigureAwait(false); } + catch (System.Data.Common.DbException dbEx) { await HandleAsync(dbEx).ConfigureAwait(false); } + catch (InvalidOperationException dbEx) { await HandleAsync(dbEx).ConfigureAwait(false); } - async Task HandleAsync(Exception dbEx) + async Task HandleAsync(Exception dbEx) + { + void RollbackKeycloak(Exception rollbackEx) => + _logger.LogError(rollbackEx, + "Tenant DB write failed for '{TenantId}', and rolling back Keycloak user '{UserId}' also failed — orphaned Keycloak user.", + LogSafe.Scrub(tenantId), LogSafe.Scrub(kcUserId)); + try { - void RollbackKeycloak(Exception rollbackEx) => - _logger.LogError(rollbackEx, - "Tenant DB write failed for '{TenantId}', and rolling back Keycloak user '{UserId}' also failed — orphaned Keycloak user.", - LogSafe.Scrub(tenantId), LogSafe.Scrub(kcUserId)); - try - { - await _kc.DeleteUserAsync(kcUserId, CancellationToken.None).ConfigureAwait(false); - } - catch (System.Net.Http.HttpRequestException rollbackEx) { RollbackKeycloak(rollbackEx); } - catch (JsonException rollbackEx) { RollbackKeycloak(rollbackEx); } - catch (TimeoutException rollbackEx) { RollbackKeycloak(rollbackEx); } - catch (InvalidOperationException rollbackEx) { RollbackKeycloak(rollbackEx); } - throw new InvalidOperationException( - $"Tenant registry write failed for '{tenantId}'; Keycloak user rolled back.", dbEx); + await _kc.DeleteUserAsync(kcUserId, CancellationToken.None).ConfigureAwait(false); } + catch (System.Net.Http.HttpRequestException rollbackEx) { RollbackKeycloak(rollbackEx); } + catch (JsonException rollbackEx) { RollbackKeycloak(rollbackEx); } + catch (TimeoutException rollbackEx) { RollbackKeycloak(rollbackEx); } + catch (InvalidOperationException rollbackEx) { RollbackKeycloak(rollbackEx); } + throw new InvalidOperationException( + $"Tenant registry write failed for '{tenantId}'; Keycloak user rolled back.", dbEx); } await _audit.WriteAsync(new AuditEntry( Guid.NewGuid(), tenantId, kcUserId, AuditActions.SignupCompleted, Target: $"mode={mode}", IpAddress: null, UserAgent: null, TimestampUtc: DateTimeOffset.UtcNow), ct).ConfigureAwait(false); - if (createTenantRow) + await _audit.WriteAsync(new AuditEntry( + Guid.NewGuid(), tenantId, kcUserId, AuditActions.TenantCreated, + Target: tenantId, IpAddress: null, UserAgent: null, + TimestampUtc: DateTimeOffset.UtcNow), ct).ConfigureAwait(false); + + return new TenantSignupOutcome(tenantId, kcUserId, mode); + } + + // The invite branch (see the file header for the order and why). + private async Task SignupWithInvitationAsync(TenantSignupRequest request, string token, CancellationToken ct) + { + var payload = ReadToken(token) + ?? throw new InvalidOperationException(InvitationRules.Describe(InvitationAcceptance.NotFound)); + // The invitee is anonymous (or signed in elsewhere): the signed token, not the caller, names the tenant. + using var bound = RowLevelSecurity.TenantScope(payload.TenantId); + var invitation = await _invitations.GetAsync(payload.TenantId, payload.Id, ct).ConfigureAwait(false) + ?? throw new InvalidOperationException(InvitationRules.Describe(InvitationAcceptance.NotFound)); + + var check = InvitationRules.CheckAcceptance(invitation.StatusAt(_clock.GetUtcNow()), invitation.EmailNormalized, request.Email); + if (check != InvitationAcceptance.Ok) + { + await RefusedAsync(invitation, check, ct).ConfigureAwait(false); + throw new InvalidOperationException(InvitationRules.Describe(check)); + } + _ = await _repo.GetAsync(invitation.TenantId, ct).ConfigureAwait(false) + ?? throw new InvalidOperationException($"The workspace '{invitation.TenantId}' this invitation is for no longer exists."); + + // Consume it before the account exists: a second accept of the same link, a revoke or the expiry landing in + // between makes this conditional write touch nothing — the invitation is used at most once. + if (!await _invitations.TryClaimAsync(invitation.TenantId, invitation.Id, _clock.GetUtcNow(), ct).ConfigureAwait(false)) + { + var now = await _invitations.GetAsync(invitation.TenantId, invitation.Id, ct).ConfigureAwait(false); + var why = now is null ? InvitationAcceptance.NotFound : InvitationRules.ForStatus(now.StatusAt(_clock.GetUtcNow())); + why = why == InvitationAcceptance.Ok ? InvitationAcceptance.AlreadyUsed : why; + await RefusedAsync(invitation, why, ct).ConfigureAwait(false); + throw new InvalidOperationException(InvitationRules.Describe(why)); + } + + // The claim is the point of no return: from here the account is created to completion even if the invitee drops + // the connection (RequestAborted). Cancelling half-way would leave a Keycloak account nobody recorded next to a + // claim this code could not tell apart from "Keycloak refused" — so the request's token stops here. + string kcUserId; + try + { + kcUserId = await _kc.CreateUserAsync( + username: request.Username, + email: invitation.Email, + tenantId: invitation.TenantId, + // An invited approver is a member too (TenantRoles.Expand). + realmRoles: TenantRoles.Expand([invitation.Role]), + sendVerifyEmail: true, + password: request.Password, + ct: CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException and not KeycloakUserLeftBehindException) + { + // Keycloak refused (a taken username, Keycloak down) and the client guarantees no account exists — an account + // it made before failing is deleted again (KeycloakAdminClient), otherwise it throws the typed exception + // handled below. Give the invitation back so the invitee can try again; never throws over the original failure. + await ReleaseQuietlyAsync(invitation).ConfigureAwait(false); + await AuditQuietlyAsync(invitation, "account_not_created").ConfigureAwait(false); + throw; + } + catch (Exception ex) when (ex is OperationCanceledException or KeycloakUserLeftBehindException) + { + // An account may exist (a left-behind user, or an HttpClient timeout with no answer): the invitation stays + // consumed so this link can never produce a second account. The operator sees it in the audit log and the + // Keycloak console; the invitee needs a new invitation. + var userId = (ex as KeycloakUserLeftBehindException)?.UserId; + _logger.LogError(ex, + "Invitation {InvitationId} was used, but creating the account did not finish cleanly (user {UserId}); it stays used.", + invitation.Id, LogSafe.Scrub(userId ?? "unknown")); + await AuditQuietlyAsync(invitation, "account_unrecorded", userId).ConfigureAwait(false); + throw; + } + + try + { + await _invitations.CompleteAsync(invitation.TenantId, invitation.Id, kcUserId, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + // The account exists and the invitation stays consumed; only the link to the account is missing. + _logger.LogWarning(ex, "Invitation {InvitationId} was used, but recording the account it created failed.", invitation.Id); + } + + var at = _clock.GetUtcNow(); + await _audit.WriteAsync(new AuditEntry( + Guid.NewGuid(), invitation.TenantId, kcUserId, AuditActions.SignupCompleted, + Target: $"mode={SignupMode.Invite}", IpAddress: null, UserAgent: null, TimestampUtc: at), ct).ConfigureAwait(false); + await _audit.WriteAsync(new AuditEntry( + Guid.NewGuid(), invitation.TenantId, kcUserId, AuditActions.InvitationAccepted, + Target: $"id={invitation.Id:N},role={invitation.Role},email={invitation.Email}", IpAddress: null, UserAgent: null, TimestampUtc: at), ct).ConfigureAwait(false); + return new TenantSignupOutcome(invitation.TenantId, kcUserId, SignupMode.Invite); + } + + private async Task ReleaseQuietlyAsync(InvitationRecord invitation) + { + try + { + await _invitations.ReleaseAsync(invitation.TenantId, invitation.Id, CancellationToken.None).ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + _logger.LogWarning(ex, "Invitation {InvitationId} could not be released after a failed sign-up; it stays used — send a new one.", invitation.Id); + } + } + + // The audit row for a claimed invitation whose account creation failed — written after the fact, under + // CancellationToken.None, and never over the original failure. + private async Task AuditQuietlyAsync(InvitationRecord invitation, string reason, string? userId = null) + { + try { await _audit.WriteAsync(new AuditEntry( - Guid.NewGuid(), tenantId, kcUserId, AuditActions.TenantCreated, - Target: tenantId, IpAddress: null, UserAgent: null, - TimestampUtc: DateTimeOffset.UtcNow), ct).ConfigureAwait(false); + Guid.NewGuid(), invitation.TenantId, userId, AuditActions.InvitationRefused, + Target: $"id={invitation.Id:N},reason={reason}", IpAddress: null, UserAgent: null, + TimestampUtc: _clock.GetUtcNow()), CancellationToken.None).ConfigureAwait(false); } + catch (Exception ex) when (ex is not OperationCanceledException) + { + _logger.LogWarning(ex, "The failed sign-up on invitation {InvitationId} could not be audited ({Reason}).", invitation.Id, reason); + } + } - return new TenantSignupOutcome(tenantId, kcUserId, mode); + private Task RefusedAsync(InvitationRecord invitation, InvitationAcceptance why, CancellationToken ct) + => _audit.WriteAsync(new AuditEntry( + Guid.NewGuid(), invitation.TenantId, UserId: null, AuditActions.InvitationRefused, + Target: $"id={invitation.Id:N},reason={why}", IpAddress: null, UserAgent: null, TimestampUtc: _clock.GetUtcNow()), ct); + + public async Task CreateInvitationAsync( + string tenantId, string role, string email, TimeSpan ttl, AuditActor actor, CancellationToken ct = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(tenantId); + ArgumentNullException.ThrowIfNull(actor); + // admin, approver or member (E1.5): the realm roles it amounts to are expanded when the account is created. + if (!TenantRoles.IsKnown(role)) + { + throw new ArgumentException("The role must be 'admin', 'approver' or 'member'.", nameof(role)); + } + var address = email?.Trim() ?? string.Empty; + var (emailOk, emailErr) = SignupValidation.ValidateEmail(address); + if (!emailOk || address.Length > InvitationRules.MaxEmailLength) + { + throw new ArgumentException(emailOk ? "The email address is too long." : emailErr, nameof(email)); + } + if (ttl <= TimeSpan.Zero || ttl > MaxInvitationLifetime) + { + throw new ArgumentOutOfRangeException(nameof(ttl), ttl, "An invitation lasts between 1 and 168 hours."); + } + + var now = _clock.GetUtcNow(); + var invitation = new InvitationRecord( + Guid.NewGuid(), tenantId, address, InvitationRules.NormalizeEmail(address), role, actor.UserId, now, now + ttl); + using (RowLevelSecurity.TenantScopeIfUnbound(tenantId)) + { + await _invitations.AddAsync(invitation, ct).ConfigureAwait(false); + } + var token = _protector.Protect(JsonSerializer.Serialize(new InvitationPayload(invitation.Id, tenantId), JsonOpts), ttl); + await _audit.WriteAsync(new AuditEntry( + Guid.NewGuid(), tenantId, actor.UserId, AuditActions.InvitationMinted, + Target: $"id={invitation.Id:N},role={role},email={address}", + actor.IpAddress, actor.UserAgent, now), ct).ConfigureAwait(false); + return new InvitationToken(invitation.Id, token, invitation.ExpiresAtUtc); } - public InvitationToken CreateInvitation(string tenantId, string role, string? email, TimeSpan ttl) + public async Task PreviewInvitationAsync(string? token, CancellationToken ct = default) + { + if (ReadToken(token) is not { } payload) { return null; } + // Anonymous by design (the signup page), and the token — signed by us — names the tenant. + using var bound = RowLevelSecurity.TenantScope(payload.TenantId); + var invitation = await _invitations.GetAsync(payload.TenantId, payload.Id, ct).ConfigureAwait(false); + return invitation is null + ? null + : new InvitationPreview(invitation.Id, invitation.TenantId, invitation.Role, invitation.Email, invitation.ExpiresAtUtc, + invitation.StatusAt(_clock.GetUtcNow())); + } + + public async Task> ListInvitationsAsync(string tenantId, int max = 100, CancellationToken ct = default) { ArgumentException.ThrowIfNullOrWhiteSpace(tenantId); - ArgumentException.ThrowIfNullOrWhiteSpace(role); - ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(ttl, TimeSpan.Zero); + using var bound = RowLevelSecurity.TenantScopeIfUnbound(tenantId); + var rows = await _invitations.ListAsync(tenantId, max, ct).ConfigureAwait(false); + var now = _clock.GetUtcNow(); + return [.. rows.Select(r => r.ToInfo(now))]; + } - var payload = new InvitationPayload(tenantId, role, email, DateTimeOffset.UtcNow); - var json = JsonSerializer.Serialize(payload, JsonOpts); - var token = _protector.Protect(json, ttl); - return new InvitationToken(token, DateTimeOffset.UtcNow.Add(ttl)); + public async Task RevokeInvitationAsync(string tenantId, Guid invitationId, AuditActor actor, CancellationToken ct = default) + { + ArgumentException.ThrowIfNullOrWhiteSpace(tenantId); + ArgumentNullException.ThrowIfNull(actor); + using var bound = RowLevelSecurity.TenantScopeIfUnbound(tenantId); + var now = _clock.GetUtcNow(); + if (!await _invitations.TryRevokeAsync(tenantId, invitationId, now, actor.UserId, ct).ConfigureAwait(false)) + { + return false; + } + var invitation = await _invitations.GetAsync(tenantId, invitationId, ct).ConfigureAwait(false); + await _audit.WriteAsync(new AuditEntry( + Guid.NewGuid(), tenantId, actor.UserId, AuditActions.InvitationRevoked, + Target: $"id={invitationId:N}{(invitation is null ? string.Empty : $",email={invitation.Email}")}", + actor.IpAddress, actor.UserAgent, now), ct).ConfigureAwait(false); + return true; } - public InvitationPreview? PreviewInvitation(string? token) + // The signed envelope: null for anything that isn't a current, intact invitation token. + private InvitationPayload? ReadToken(string? token) { if (string.IsNullOrWhiteSpace(token)) { return null; } string json; - DateTimeOffset expires; try { - json = _protector.Unprotect(token, out expires); + json = _protector.Unprotect(token, out _); } catch (CryptographicException) { return null; } - InvitationPayload? payload; try { - payload = JsonSerializer.Deserialize(json, JsonOpts); + return JsonSerializer.Deserialize(json, JsonOpts) is { } payload + && payload.Id != Guid.Empty && !string.IsNullOrWhiteSpace(payload.TenantId) + ? payload + : null; } catch (JsonException) { return null; } - if (payload is null) { return null; } - return new InvitationPreview(payload.TenantId, payload.Role, payload.Email, expires); } private async Task GenerateUniqueSlugAsync(string username, CancellationToken ct) @@ -249,7 +430,10 @@ private static string SlugifyUsername(string username) return slug; } + private static readonly string[] AdminRoles = ["admin"]; + private static readonly JsonSerializerOptions JsonOpts = new(JsonSerializerDefaults.Web); - private sealed record InvitationPayload(string TenantId, string Role, string? Email, DateTimeOffset IssuedAtUtc); + // The token carries only what finds the row; everything else comes from the row. + private sealed record InvitationPayload(Guid Id, string TenantId); } diff --git a/src/AgentOs.Modules.Tenants/TenantsModule.cs b/src/AgentOs.Modules.Tenants/TenantsModule.cs index 5b6c5f1e..113ae9d7 100644 --- a/src/AgentOs.Modules.Tenants/TenantsModule.cs +++ b/src/AgentOs.Modules.Tenants/TenantsModule.cs @@ -96,12 +96,18 @@ public void AddServices(IServiceCollection services, IConfiguration configuratio var connectionString = configuration.GetConnectionString("DefaultConnection"); if (!string.IsNullOrWhiteSpace(connectionString)) { + // GAP-11/11b/14: tenants.audit_events and tenants.invitations are under RLS (ADR-0012 stage 2), and the app + // role may only append to the audit trail — every connection runs as the restricted role. services.AddDbContext(opt => opt.UseNpgsql(connectionString, npg => - npg.MigrationsHistoryTable("__EFMigrationsHistory", schema: "tenants"))); + npg.MigrationsHistoryTable("__EFMigrationsHistory", schema: "tenants")) + .AddInterceptors(RowLevelSecurityInterceptor.Instance)); services.AddNpgsqlConnectionFactory(connectionString); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(sp => sp.GetRequiredService()); + services.AddScoped(sp => sp.GetRequiredService()); + services.AddScoped(); services.AddScoped(sp => new ApiTokens.ApiTokenService( sp.GetRequiredService(), sp.GetRequiredService(), sp.GetService() ?? TimeProvider.System)); } @@ -110,6 +116,9 @@ public void AddServices(IServiceCollection services, IConfiguration configuratio services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(); + // No database (the standalone Web): invitations live in this process, with the same single-use rules. + services.TryAddSingleton(); } } diff --git a/src/AgentOs.Modules.Tools/Evidence/EfToolInvocationLog.cs b/src/AgentOs.Modules.Tools/Evidence/EfToolInvocationLog.cs index 46476249..d46c1727 100644 --- a/src/AgentOs.Modules.Tools/Evidence/EfToolInvocationLog.cs +++ b/src/AgentOs.Modules.Tools/Evidence/EfToolInvocationLog.cs @@ -39,6 +39,9 @@ public async Task AppendAsync(ToolInvocationEvidence entry, CancellationToken ca ArgumentNullException.ThrowIfNull(entry); try { + // The buffered drain writes from a background loop that acts for no tenant: bind each row to its own tenant + // (RLS stage 2 refuses a row written with none). An inline write keeps the producer's tenant. + using var bound = RowLevelSecurity.TenantScopeIfUnbound(entry.TenantId); await using var scope = _rootProvider.CreateAsyncScope(); var db = scope.ServiceProvider.GetRequiredService(); db.ToolInvocations.Add(new ToolInvocationEvidenceEntity diff --git a/src/AgentOs.Modules.Workspaces/Persistence/Repositories/WorkspaceRepository.cs b/src/AgentOs.Modules.Workspaces/Persistence/Repositories/WorkspaceRepository.cs index 39334620..4d70079d 100644 --- a/src/AgentOs.Modules.Workspaces/Persistence/Repositories/WorkspaceRepository.cs +++ b/src/AgentOs.Modules.Workspaces/Persistence/Repositories/WorkspaceRepository.cs @@ -45,13 +45,13 @@ public async Task> ListAsync(int limit = Page.Def ORDER BY "CreatedAtUtc" DESC LIMIT @lim OFFSET @off """; - return await QueryListAsync(sql, new { tenantId = _tenant.TenantId, lim, off }, ct).ConfigureAwait(false); + return await QueryListAsync(_tenant.TenantId, sql, new { tenantId = _tenant.TenantId, lim, off }, ct).ConfigureAwait(false); } public async Task GetAsync(Guid id, CancellationToken ct = default) { const string sql = """SELECT * FROM workspaces.workspaces WHERE "Id" = @id AND "TenantId" = @tenantId"""; - return await QuerySingleAsync(sql, new { id, tenantId = _tenant.TenantId }, ct).ConfigureAwait(false); + return await QuerySingleAsync(_tenant.TenantId, sql, new { id, tenantId = _tenant.TenantId }, ct).ConfigureAwait(false); } public async Task AddAsync(WorkspaceEntity workspace, CancellationToken ct = default) @@ -69,13 +69,13 @@ public async Task> ListForTenantAsync(string tena WHERE "TenantId" = @tenantId ORDER BY "CreatedAtUtc" DESC """; - return await QueryListAsync(sql, new { tenantId }, ct).ConfigureAwait(false); + return await QueryListAsync(tenantId, sql, new { tenantId }, ct).ConfigureAwait(false); } public async Task GetForTenantAsync(string tenantId, Guid id, CancellationToken ct = default) { const string sql = """SELECT * FROM workspaces.workspaces WHERE "TenantId" = @tenantId AND "Id" = @id"""; - return await QuerySingleAsync(sql, new { tenantId, id }, ct).ConfigureAwait(false); + return await QuerySingleAsync(tenantId, sql, new { tenantId, id }, ct).ConfigureAwait(false); } public async Task SetAutopilotForTenantAsync(string tenantId, Guid id, string mode, string? userId, CancellationToken ct = default) @@ -121,8 +121,9 @@ await _db.AutopilotEvents.IgnoreQueryFilters().AsNoTracking() public async Task> ListAutopilotAsync(CancellationToken ct = default) { - // Cross-tenant on purpose: the project orchestrator is a platform worker. Explicit bypass, not an empty tenant. - using var platform = RowLevelSecurity.Bypass(); + // Cross-tenant on purpose: the project orchestrator is a platform worker. An explicit, counted platform scope — + // an empty tenant sees nothing (RLS stage 2). + using var platform = RowLevelSecurity.PlatformScope("autopilot-scan"); return await _db.Workspaces.IgnoreQueryFilters().AsNoTracking() .Where(w => w.Autopilot != "Off") .ToListAsync(ct).ConfigureAwait(false); @@ -177,8 +178,9 @@ public async Task> ListReposForTenantAsync(st WHERE "TenantId" = @tenantId AND "WorkspaceId" = @workspaceId ORDER BY "AddedAtUtc" """; - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + // RLS (ADR-0012 stage 2): Dapper reads switch to the restricted role like the EF context — a plain connection ran as + // the login user, which an owner login (Azure) sees nothing through once "no tenant" means nothing. + await using var conn = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var rows = await conn.QueryAsync( new CommandDefinition(sql, new { tenantId, workspaceId }, cancellationToken: ct)).ConfigureAwait(false); return rows.AsList(); @@ -193,8 +195,7 @@ public async Task> ListAllReposForTenantAsync WHERE "TenantId" = @tenantId ORDER BY "WorkspaceId", "AddedAtUtc" """; - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var rows = await conn.QueryAsync( new CommandDefinition(sql, new { tenantId }, cancellationToken: ct)).ConfigureAwait(false); return rows.AsList(); @@ -227,19 +228,17 @@ public async Task RemoveRepoForTenantAsync(string tenantId, Guid repoId, C } // ── Dapper read helpers (tenant-scoped by the caller's SQL) ─────────────────────────────────── - private async Task> QueryListAsync(string sql, object parms, CancellationToken ct) + private async Task> QueryListAsync(string tenantId, string sql, object parms, CancellationToken ct) { - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); var rows = await conn.QueryAsync( new CommandDefinition(sql, parms, cancellationToken: ct)).ConfigureAwait(false); return rows.AsList(); } - private async Task QuerySingleAsync(string sql, object parms, CancellationToken ct) + private async Task QuerySingleAsync(string tenantId, string sql, object parms, CancellationToken ct) { - await using var conn = Conn.Create(); - await conn.OpenAsync(ct).ConfigureAwait(false); + await using var conn = await Conn.OpenForTenantAsync(tenantId, ct).ConfigureAwait(false); return await conn.QueryFirstOrDefaultAsync( new CommandDefinition(sql, parms, cancellationToken: ct)).ConfigureAwait(false); } diff --git a/src/AgentOs.ServiceDefaults/Extensions.cs b/src/AgentOs.ServiceDefaults/Extensions.cs index 7b30e979..f59dc18d 100644 --- a/src/AgentOs.ServiceDefaults/Extensions.cs +++ b/src/AgentOs.ServiceDefaults/Extensions.cs @@ -103,6 +103,8 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati .AddMeter("AgentOs.Tools") // evidence health (inline writes, lost rows, runner-reported refusals) + tool-output savings (ToolTelemetry.MeterName) .AddMeter("AgentOs.Pipeline") // runs by outcome, run duration, budget refusals + utilization (PipelineTelemetry.MeterName) .AddMeter(HealthMetricsPublisher.MeterName) // health check status per check (agentos.health.status) + .AddMeter("AgentOs.Persistence") // RLS connections by scope + platform scopes by reason (RowLevelSecurity.MeterName) + .AddMeter("AgentOs.Audit") // audit rows not recorded, by reason (Modules.Tenants AuditTelemetry.MeterName) .AddMeter(RequestRateLimitPolicy.MeterName) // 429s from the global request limiter (agentos.http.rate_limited) .AddMeter("Npgsql")) // connection pool + command duration (db.client.*) — the Postgres side of the run SLOs .WithTracing(tracing => tracing @@ -147,6 +149,9 @@ public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicati HealthStatus.Unhealthy, tags: ["ready"], timeout: TimeSpan.FromSeconds(5))); + // GAP-14: whether the database wall is up. Degraded (still 200) when an operator turned it off on purpose, so + // the readiness probe passes but agentos.health.status and the /health body say so. + checks.AddCheck("row-level-security", HealthStatus.Unhealthy, tags: ["ready"]); } // Probe the discovery document over the backend-reachable URL: behind ACA the public diff --git a/src/AgentOs.ServiceDefaults/RowLevelSecurityStartup.cs b/src/AgentOs.ServiceDefaults/RowLevelSecurityStartup.cs new file mode 100644 index 00000000..94790515 --- /dev/null +++ b/src/AgentOs.ServiceDefaults/RowLevelSecurityStartup.cs @@ -0,0 +1,56 @@ +// GAP-14 — the one place both hosts bring up Postgres row-level security (ADR-0012), after every module migrated: +// the HTTP-tenant resolver, the bootstrap (fails the boot outside Development), and a readiness check that reports +// whether the database wall is actually up. +using AgentOs.SharedKernel.Identity; +using AgentOs.SharedKernel.Persistence; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Hosting; + +namespace AgentOs.ServiceDefaults; + +/// Host wiring for . +public static class RowLevelSecurityStartup +{ + /// The bootstrap-only escape hatch (false = application filters only). + public const string ConfigKey = "Persistence:RowLevelSecurity"; + + /// Bind connections to the caller's tenant (an authenticated request's claim, or the ambient identity a run / + /// desktop circuit pushed) and bootstrap RLS. Throws outside + /// Development when the bootstrap fails — the host must not serve tenants with RLS silently off. + public static async Task UseAgentOsRowLevelSecurityAsync(this WebApplication app) + { + ArgumentNullException.ThrowIfNull(app); + var httpAccessor = app.Services.GetRequiredService(); + RowLevelSecurity.HttpTenant = () => + httpAccessor.HttpContext?.User is { Identity.IsAuthenticated: true } user ? user.GetTenantId() : null; + return await RowLevelSecurity.EnsureAtStartupAsync( + app.Configuration.GetConnectionString("DefaultConnection"), + app.Configuration.GetValue(ConfigKey, true), + app.Environment.IsDevelopment(), + app.Logger).ConfigureAwait(false); + } +} + +/// Readiness: is row-level security bound? Registered only when a database is configured. +internal sealed class RowLevelSecurityHealthCheck : IHealthCheck +{ + public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(context); + return Task.FromResult(For(RowLevelSecurity.State, context.Registration.FailureStatus)); + } + + /// The check's answer for a state (pure, for tests). + internal static HealthCheckResult For(RowLevelSecurityState state, HealthStatus failureStatus) => state switch + { + RowLevelSecurityState.Enabled => HealthCheckResult.Healthy($"Row-level security is on for {RowLevelSecurity.Tables.Count} tenant tables."), + RowLevelSecurityState.Disabled => HealthCheckResult.Degraded($"Row-level security is turned off ({RowLevelSecurityStartup.ConfigKey}=false): tenant isolation rests on application filters only."), + RowLevelSecurityState.Failed => new HealthCheckResult(failureStatus, "Row-level security failed to start: tenant isolation rests on application filters only."), + RowLevelSecurityState.Pending => HealthCheckResult.Degraded("Row-level security has not been set up yet."), + _ => HealthCheckResult.Healthy("No database is configured."), + }; +} diff --git a/src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs b/src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs index f5f2c16b..71ca8514 100644 --- a/src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs +++ b/src/AgentOs.SharedKernel/Persistence/RowLevelSecurity.cs @@ -1,23 +1,35 @@ -// Enterprise review E0.1 / plan 0.9 — Postgres row-level security on the tenant tables, stage 1 (ADR-0012). +// Enterprise review E0.1 / plan 0.9 / GAP-14 — Postgres row-level security on the tenant tables, stage 2 (ADR-0012). // Application isolation (EF global filters + explicit tenant predicates) stays; RLS is the database-enforced // second wall, so a forgotten filter or a raw SQL query fails closed instead of leaking another tenant's rows. // // • Bootstrap (EnsureAsync, once per boot after migrations, under an advisory lock): a NOLOGIN, NOBYPASSRLS // role `agentos_rls`, DML grants on every module schema, and a policy on each tenant table: -// visible/writable ⇔ "TenantId" = app.tenant_id — or app.tenant_id is unset (stage 1: work with no -// known tenant — pairing, background drains, boot — keeps working). +// visible/writable ⇔ "TenantId" = app.tenant_id OR app.platform = 'on' +// Stage 2: NO tenant ⇒ NOTHING (stage 1 let "no tenant" see every row). Work that is cross-tenant by nature +// (the autopilot scan, the scheduler, the runner handshake, platform-scope audit rows) runs in an explicit, +// counted PlatformScope(reason); work that knows its tenant from somewhere other than the caller (a signed +// invitation, the row a background drain is writing) narrows to it with TenantScope(tenant). // FORCE ROW LEVEL SECURITY so the table owner is bound too (Azure connects as the owner, not a superuser). -// • Per connection (ApplyToConnectionAsync, from the EF interceptor and the Dapper OpenAsync helper): -// SET ROLE agentos_rls; set_config('app.tenant_id', ) +// config.app_config also lets every tenant READ the "__platform" rows (platform settings resolve under each +// tenant's own); writing them needs the platform scope. tenants.audit_events is append-only for the app role +// (REVOKE UPDATE, DELETE, TRUNCATE — GAP-11b). +// • Per connection (ApplyToConnectionAsync, from the EF interceptor and the Dapper OpenWithTenantAsync helper): +// SET ROLE agentos_rls; set_config('app.tenant_id', ); set_config('app.platform', 'on' | '') // SET ROLE matters locally: the dev/lab stack connects as the Postgres SUPERUSER, which bypasses RLS. -// The tenant comes from AmbientIdentity (runs, circuits — see the Web's CircuitIdentityHandler), else the -// authenticated HTTP request. Npgsql resets session state when a pooled connection is returned. -// • Stage 2 (not here): config.app_config and tenants.audit_events (they hold platform-scope rows), and -// tightening "unset ⇒ visible" to "unset ⇒ nothing" once every path pushes a tenant. +// The tenant comes from the innermost TenantScope, else AmbientIdentity (runs, circuits — see the Web's +// CircuitIdentityHandler), else the authenticated HTTP request. Npgsql resets session state when a pooled +// connection is returned. +// • Before the bootstrap ran (boot: migrations, seeds), and when RLS is off (Persistence:RowLevelSecurity=false, +// or a failed bootstrap in Development), connections keep the login user and set app.platform = 'on', so +// policies left by an earlier boot never hide rows from a deliberately un-isolated process. Outside Development a +// failed bootstrap stops the host (EnsureAtStartupAsync) — it never runs with RLS silently off. using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.Data.Common; +using System.Diagnostics.Metrics; +using System.Linq; using System.Threading; using System.Threading.Tasks; using AgentOs.SharedKernel.Identity; @@ -26,7 +38,39 @@ namespace AgentOs.SharedKernel.Persistence; -/// Postgres row-level security for tenant tables (stage 1). +/// Where row-level security stands in this process. +public enum RowLevelSecurityState +{ + /// The host has not run the bootstrap yet (boot). Connections run as the login user, platform-wide. + Pending = 0, + + /// Bootstrapped: every connection is bound to the restricted role and its tenant. + Enabled = 1, + + /// Turned off on purpose (Persistence:RowLevelSecurity=false): application filters only. + Disabled = 2, + + /// The bootstrap failed (Development only — elsewhere the host stops): application filters only. + Failed = 3, + + /// No database is configured. + NotConfigured = 4, +} + +/// The row-level-security bootstrap failed outside Development; the host must not start without it. +public sealed class RowLevelSecurityBootstrapException : InvalidOperationException +{ + /// Create the exception. + public RowLevelSecurityBootstrapException() { } + + /// Create the exception with a message. + public RowLevelSecurityBootstrapException(string message) : base(message) { } + + /// Create the exception with a message and inner exception. + public RowLevelSecurityBootstrapException(string message, Exception innerException) : base(message, innerException) { } +} + +/// Postgres row-level security for tenant tables (stage 2, ADR-0012). public static partial class RowLevelSecurity { /// The restricted role every application connection switches to. @@ -35,7 +79,29 @@ public static partial class RowLevelSecurity /// The session setting the policies compare "TenantId" with. public const string TenantSetting = "app.tenant_id"; - /// Tenant tables under RLS in stage 1 (schema, table). + /// The session setting that opens every row to an explicit platform operation ('on'). + public const string PlatformSetting = "app.platform"; + + /// The reserved config.app_config tenant that holds platform settings (mirrors + /// SettingsService.PlatformTenant; a test pins the two together). Every tenant may read these rows. + public const string PlatformSettingsTenant = "__platform"; + + /// Meter for the scope counters (register with .AddMeter("AgentOs.Persistence")). + public const string MeterName = "AgentOs.Persistence"; + + /// Instrument names, for tests and docs/OPERATIONS.md. + public static class Instruments + { + /// Counter of connections bound by RLS, tagged scope = tenant | platform | none. A rising + /// none is a code path that lost its tenant and now sees nothing (stage 2). + public const string Connections = "agentos.rls.connections"; + + /// Counter of explicit platform scopes entered, tagged reason. + public const string PlatformScopes = "agentos.rls.platform_scope"; + } + + /// Tenant tables under RLS (schema, table). Every table a module adds with a "TenantId" column goes + /// here or into with its reason (a real-Postgres test fails on a table in neither). public static IReadOnlyList<(string Schema, string Table)> Tables { get; } = [ ("pipeline", "pipeline_runs"), ("pipeline", "run_metrics"), ("pipeline", "orchestrations"), ("pipeline", "scheduled_runs"), @@ -44,50 +110,131 @@ public static partial class RowLevelSecurity ("tools", "tool_invocations"), ("workspaces", "workspaces"), ("workspaces", "workspace_repos"), ("workspaces", "autopilot_events"), ("workspaces", "ticket_flows"), ("memory", "nodes"), ("memory", "edges"), + ("config", "app_config"), + ("tenants", "audit_events"), ("tenants", "invitations"), ]; - private static volatile bool _enabled; - private static readonly AsyncLocal _bypass = new(); + /// Tables with a "TenantId" column that are deliberately NOT under RLS, with the reason. + public static IReadOnlyDictionary ExemptTables { get; } = new Dictionary(StringComparer.Ordinal) + { + ["tenants.api_tokens"] = "Looked up by its prefix while authenticating, before any tenant is known; EF filter + explicit tenant predicates, and only the SHA-256 of a token is stored.", + }.ToFrozenDictionary(StringComparer.Ordinal); + + /// Tables the app role may only read and append to (GAP-11b): REVOKE UPDATE, DELETE, TRUNCATE. + public static IReadOnlyList<(string Schema, string Table)> AppendOnlyTables { get; } = [("tenants", "audit_events")]; + + /// Tables whose rows under a reserved tenant id every tenant may read (schema, table, reserved id). + public static IReadOnlyList<(string Schema, string Table, string ReadableTenant)> SharedReadRows { get; } = + [("config", "app_config", PlatformSettingsTenant)]; + + private static readonly Meter Meter = new(MeterName); + private static readonly Counter ConnectionCounter = + Meter.CreateCounter(Instruments.Connections, unit: "{connection}", description: "Connections bound by row-level security, by scope (tenant, platform, none)."); + private static readonly Counter PlatformScopeCounter = + Meter.CreateCounter(Instruments.PlatformScopes, unit: "{scope}", description: "Explicit platform-wide RLS scopes entered, by reason."); + + private static readonly AsyncLocal _scope = new(); + private static int _state = (int)RowLevelSecurityState.Pending; + + /// Where RLS stands in this process. + public static RowLevelSecurityState State => (RowLevelSecurityState)Volatile.Read(ref _state); /// True once succeeded in this process. - public static bool Enabled => _enabled; + public static bool Enabled => State == RowLevelSecurityState.Enabled; /// Resolves the authenticated HTTP request's tenant when no ambient identity is pushed (set by the host). public static Func? HttpTenant { get; set; } - /// Skip the role/tenant switch for this async flow (migrations, platform maintenance). - public static IDisposable Bypass() + /// True inside (the innermost scope wins). + public static bool InPlatformScope => _scope.Value is { Platform: true }; + + /// Open every tenant's rows to this async flow — for operations that are cross-tenant by nature (a platform + /// worker's scan, the runner handshake, platform-scope audit rows). Counted by on + /// agentos.rls.platform_scope. The connection still runs as the restricted role: append-only tables stay + /// append-only. Dispose to restore the previous scope. + public static IDisposable PlatformScope(string reason) { - var previous = _bypass.Value; - _bypass.Value = true; - return new Restore(previous); + ArgumentException.ThrowIfNullOrWhiteSpace(reason); + PlatformScopeCounter.Add(1, new KeyValuePair("reason", reason)); + return Push(new Frame(null, Platform: true)); + } + + /// Bind this async flow to , whatever the ambient or HTTP tenant is — for work that + /// knows its tenant from a trusted source other than the caller (a signed invitation token, the row a background + /// drain writes, an audit entry's own tenant). Dispose to restore the previous scope. + public static IDisposable TenantScope(string tenantId) + { + ArgumentException.ThrowIfNullOrWhiteSpace(tenantId); + return Push(new Frame(tenantId, Platform: false)); } - /// The tenant the current flow acts for, or null when unknown. + /// Bind to only when this flow acts for no tenant yet (a background worker, an + /// anonymous request): an explicit tenant FILLS a missing one and never overrides the flow's own, so a flow bound to + /// tenant A that names tenant B still sees nothing of B. Null (nothing to dispose) when the flow is already bound, is + /// a platform operation, or is blank. + public static IDisposable? TenantScopeIfUnbound(string? tenantId) + => string.IsNullOrWhiteSpace(tenantId) || InPlatformScope || CurrentTenant() is not null + ? null + : TenantScope(tenantId); + + /// The tenant the current flow acts for, or null when unknown (or inside a platform scope). public static string? CurrentTenant() - => AmbientIdentity.Current?.TenantId is { Length: > 0 } ambient ? ambient : HttpTenant?.Invoke(); + => _scope.Value is { } frame + ? frame.Tenant + : AmbientIdentity.Current?.TenantId is { Length: > 0 } ambient ? ambient : HttpTenant?.Invoke(); - /// Switch an opened connection to the restricted role and the current tenant. - public static async Task ApplyToConnectionAsync(DbConnection connection, CancellationToken cancellationToken = default) + /// Bind an opened connection: the restricted role + the flow's scope once RLS is enabled; before that (boot) + /// or when RLS is off, the login user platform-wide (see the file header). + public static Task ApplyToConnectionAsync(DbConnection connection, CancellationToken cancellationToken = default) { - if (!_enabled || _bypass.Value || connection is not NpgsqlConnection npgsql) { return; } - await using var cmd = Command(npgsql); - await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + if (connection is not NpgsqlConnection npgsql || State is RowLevelSecurityState.NotConfigured) { return Task.CompletedTask; } + return Enabled ? ApplySessionAsync(npgsql, switchRole: true, cancellationToken) : ApplyUnboundAsync(npgsql, cancellationToken); } /// Synchronous twin for EF's synchronous open path. public static void ApplyToConnection(DbConnection connection) { - if (!_enabled || _bypass.Value || connection is not NpgsqlConnection npgsql) { return; } - using var cmd = Command(npgsql); + if (connection is not NpgsqlConnection npgsql || State is RowLevelSecurityState.NotConfigured) { return; } + using var cmd = Enabled ? ScopeCommand(npgsql, switchRole: true) : UnboundCommand(npgsql); cmd.ExecuteNonQuery(); } - private static NpgsqlCommand Command(NpgsqlConnection connection) + /// Bind to the current flow's scope — its tenant, an explicit platform scope, or + /// (neither) nothing — switching to when . Without the role switch the + /// login user keeps its own privileges (a privileged maintenance connection such as the tenant purge) but stays bound + /// by the forced policies when it owns the tables. Independent of . + public static async Task ApplySessionAsync(NpgsqlConnection connection, bool switchRole, CancellationToken cancellationToken = default) { + ArgumentNullException.ThrowIfNull(connection); + await using var cmd = ScopeCommand(connection, switchRole); + await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + + private static async Task ApplyUnboundAsync(NpgsqlConnection connection, CancellationToken cancellationToken) + { + await using var cmd = UnboundCommand(connection); + await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + + private static NpgsqlCommand ScopeCommand(NpgsqlConnection connection, bool switchRole) + { + var platform = InPlatformScope; + var tenant = platform ? null : CurrentTenant(); + ConnectionCounter.Add(1, new KeyValuePair("scope", platform ? "platform" : tenant is null ? "none" : "tenant")); var cmd = connection.CreateCommand(); - cmd.CommandText = $"SET ROLE {Role}; SELECT set_config('{TenantSetting}', @tenant, false)"; - cmd.Parameters.AddWithValue("tenant", CurrentTenant() ?? string.Empty); + cmd.CommandText = (switchRole ? $"SET ROLE {Role}; " : string.Empty) + + $"SELECT set_config('{TenantSetting}', @tenant, false), set_config('{PlatformSetting}', @platform, false)"; + cmd.Parameters.AddWithValue("tenant", tenant ?? string.Empty); + cmd.Parameters.AddWithValue("platform", platform ? "on" : string.Empty); + return cmd; + } + + // Not bootstrapped yet (boot: migrations, seeds) or deliberately off: the login user, platform-wide, so policies an + // earlier boot left behind never hide rows from a process that runs without the database wall on purpose. + private static NpgsqlCommand UnboundCommand(NpgsqlConnection connection) + { + var cmd = connection.CreateCommand(); + cmd.CommandText = $"SELECT set_config('{TenantSetting}', '', false), set_config('{PlatformSetting}', 'on', false)"; return cmd; } @@ -109,37 +256,102 @@ public static async Task OpenWithTenantAsync(this INpgsqlConnectio } } - /// Create the role, grants and policies (idempotent), then enable the per-connection switch. - /// Returns false (RLS stays off, logged as an error) when the database refuses — never stops the host. - public static async Task EnsureAsync(string? connectionString, ILogger logger, CancellationToken cancellationToken = default) + /// Open a Dapper connection for a tenant-explicit read: bound to the flow's own tenant, or — when the flow acts + /// for no tenant (a background worker, the runner hub) — to + /// (). The session is set when the connection opens, so the scope ends here. + public static async Task OpenForTenantAsync(this INpgsqlConnectionFactory factory, string? tenantId, CancellationToken cancellationToken = default) + { + using var bound = TenantScopeIfUnbound(tenantId); + return await factory.OpenWithTenantAsync(cancellationToken).ConfigureAwait(false); + } + + /// The host's boot step: bootstrap RLS after every module migrated. No database → nothing to do; + /// false (the Persistence:RowLevelSecurity escape hatch) → off, logged as a + /// warning on every boot; a failed bootstrap → outside Development + /// (the host must not serve tenants without the database wall), an error log and application filters only in + /// Development. + public static async Task EnsureAtStartupAsync( + string? connectionString, bool enabledByConfig, bool isDevelopment, ILogger logger, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(logger); - if (string.IsNullOrWhiteSpace(connectionString)) { return false; } - try + if (string.IsNullOrWhiteSpace(connectionString)) { - await using var gate = await PgAdvisoryLock.AcquireAsync(connectionString, "agentos:rls-bootstrap", cancellationToken).ConfigureAwait(false); - await using var conn = new NpgsqlConnection(connectionString); - await conn.OpenAsync(cancellationToken).ConfigureAwait(false); - await using (var cmd = conn.CreateCommand()) - { - cmd.CommandText = BootstrapSql(conn.PostgreSqlVersion.Major); - await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); - } - _enabled = true; - LogEnabled(logger, Tables.Count); - return true; + SetState(RowLevelSecurityState.NotConfigured); + return State; } - catch (Exception ex) when (ex is NpgsqlException or InvalidOperationException or TimeoutException) + if (!enabledByConfig) { - LogFailed(logger, ex); + SetState(RowLevelSecurityState.Disabled); + LogDisabled(logger); + return State; + } + if (await EnsureAsync(connectionString, logger, cancellationToken).ConfigureAwait(false)) + { + return State; + } + if (!isDevelopment) + { + throw new RowLevelSecurityBootstrapException( + "Row-level security could not be set up, so AgentOS.NET will not start: tenant isolation would rest on application " + + "filters alone. Give the database user CREATEROLE and ownership of the module tables (or have a DBA create the " + + $"'{Role}' role and grant it), then restart. To run without it on purpose, set Persistence:RowLevelSecurity=false."); + } + return State; + } + + /// Create the role, grants and policies (idempotent), then bind every connection. Returns false (state + /// , logged as an error) when the database refuses. A deadlock or lock + /// timeout against live traffic (a rolling deploy) is retried twice. + public static async Task EnsureAsync(string? connectionString, ILogger logger, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(logger); + if (string.IsNullOrWhiteSpace(connectionString)) + { + SetState(RowLevelSecurityState.NotConfigured); return false; } + for (var attempt = 1; ; attempt++) + { + try + { + await using var gate = await PgAdvisoryLock.AcquireAsync(connectionString, "agentos:rls-bootstrap", cancellationToken).ConfigureAwait(false); + await using var conn = new NpgsqlConnection(connectionString); + await conn.OpenAsync(cancellationToken).ConfigureAwait(false); + await using (var cmd = conn.CreateCommand()) + { + cmd.CommandText = BootstrapSql(conn.PostgreSqlVersion.Major); + await cmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); + } + SetState(RowLevelSecurityState.Enabled); + LogEnabled(logger, Tables.Count); + return true; + } + catch (PostgresException ex) when (attempt < 3 && ex.SqlState is PostgresErrorCodes.DeadlockDetected or PostgresErrorCodes.LockNotAvailable) + { + LogRetry(logger, attempt, ex.SqlState); + await Task.Delay(TimeSpan.FromSeconds(attempt), cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) when (ex is NpgsqlException or InvalidOperationException or TimeoutException) + { + SetState(RowLevelSecurityState.Failed); + LogFailed(logger, ex); + return false; + } + } } + // Internal for tests that drive the startup paths and must put the process-wide state back afterwards. + internal static void SetState(RowLevelSecurityState state) => Volatile.Write(ref _state, (int)state); + + /// The policy predicate: the row's tenant is the session's tenant (an empty setting matches nothing), or + /// the session is an explicit platform operation. + public static string PolicyPredicate { get; } = + "(\"TenantId\" = NULLIF(current_setting('" + TenantSetting + "', true), '') OR current_setting('" + PlatformSetting + "', true) = 'on')"; + /// The idempotent bootstrap script (exposed for tests and the ADR). public static string BootstrapSql(int serverMajorVersion) { - var sb = new System.Text.StringBuilder(4096); + var sb = new System.Text.StringBuilder(8192); sb.Append($""" DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '{Role}') THEN @@ -158,17 +370,33 @@ FOR s IN SELECT DISTINCT table_schema FROM information_schema.tables WHERE table END $$; """); - const string predicate = "(\"TenantId\" = current_setting('" + TenantSetting + "', true) OR coalesce(current_setting('" + TenantSetting + "', true), '') = '')"; + // GAP-11b: after the schema-wide grants above (which run every boot), take the write-back rights away again. + foreach (var (schema, table) in AppendOnlyTables) + { + sb.Append($""" + DO $$ BEGIN + IF to_regclass('{schema}.{table}') IS NOT NULL THEN + REVOKE UPDATE, DELETE, TRUNCATE ON "{schema}"."{table}" FROM {Role}; + END IF; + END $$; + + """); + } foreach (var (schema, table) in Tables) { var t = $"\"{schema}\".\"{table}\""; + var sharedRead = string.Concat(SharedReadRows + .Where(r => r.Schema == schema && r.Table == table) + .Select(r => $"\n CREATE POLICY agentos_shared_read ON {t} FOR SELECT USING (\"TenantId\" = '{r.ReadableTenant}');")); + // One DO block per table = one transaction: the policy is never missing while live traffic queries the table. sb.Append($""" DO $$ BEGIN IF to_regclass('{schema}.{table}') IS NOT NULL THEN ALTER TABLE {t} ENABLE ROW LEVEL SECURITY; ALTER TABLE {t} FORCE ROW LEVEL SECURITY; DROP POLICY IF EXISTS agentos_tenant ON {t}; - CREATE POLICY agentos_tenant ON {t} USING {predicate} WITH CHECK {predicate}; + DROP POLICY IF EXISTS agentos_shared_read ON {t}; + CREATE POLICY agentos_tenant ON {t} USING {PolicyPredicate} WITH CHECK {PolicyPredicate};{sharedRead} END IF; END $$; @@ -177,14 +405,36 @@ IF to_regclass('{schema}.{table}') IS NOT NULL THEN return sb.ToString(); } - [LoggerMessage(Level = LogLevel.Information, Message = "Row-level security enabled on {Count} tenant tables (stage 1).")] + private static Restore Push(Frame frame) + { + var previous = _scope.Value; + _scope.Value = frame; + return new Restore(previous); + } + + [LoggerMessage(Level = LogLevel.Information, Message = "Row-level security enabled on {Count} tenant tables (stage 2: no tenant sees nothing).")] private static partial void LogEnabled(ILogger logger, int count); [LoggerMessage(Level = LogLevel.Error, Message = "Row-level security bootstrap FAILED — tenant isolation falls back to application filters only. Check the database user's privileges (CREATEROLE, table ownership).")] private static partial void LogFailed(ILogger logger, Exception ex); - private sealed class Restore(bool previous) : IDisposable + [LoggerMessage(Level = LogLevel.Warning, Message = "Row-level security bootstrap hit {SqlState} on attempt {Attempt} (live traffic holds a lock) — retrying.")] + private static partial void LogRetry(ILogger logger, int attempt, string sqlState); + + [LoggerMessage(Level = LogLevel.Warning, Message = "Row-level security is OFF (Persistence:RowLevelSecurity=false): tenant isolation rests on application filters only.")] + private static partial void LogDisabled(ILogger logger); + + private sealed record Frame(string? Tenant, bool Platform); + + private sealed class Restore(Frame? previous) : IDisposable { - public void Dispose() => _bypass.Value = previous; + private bool _disposed; + + public void Dispose() + { + if (_disposed) { return; } + _disposed = true; + _scope.Value = previous; + } } } diff --git a/src/AgentOs.Web/Components/Pages/AuditLogApp.razor b/src/AgentOs.Web/Components/Pages/AuditLogApp.razor index 5d8bc0bf..02b370aa 100644 --- a/src/AgentOs.Web/Components/Pages/AuditLogApp.razor +++ b/src/AgentOs.Web/Components/Pages/AuditLogApp.razor @@ -1,11 +1,14 @@ @* AuditLogApp — Settings → Audit log. The tenant's audit trail: settings changes (with old → new; secrets masked, stored keys/prompts shown as "set (N chars)"), member lifecycle, invitations and sign-in events. Admins see their own tenant; platform admins can switch to the platform scope (platform settings). Filter - by kind, export CSV (formula-safe). Tenant is read from AuthenticationState — a circuit has no HttpContext. *@ + by kind, export CSV (formula-safe). Tenant is read from AuthenticationState — a circuit has no HttpContext. + GAP-11b: the trail is append-only in the database and hash-chained per tenant; "Verify chain" walks it and says + "intact" or names the first event that was changed or removed (each row shows its place in the chain, #N). *@ @using Microsoft.AspNetCore.Components.Authorization @using AgentOs.Modules.Tenants @inject IAuditLog Audit +@inject IAuditChainVerifier ChainVerifier @if (!_resolved) { @@ -24,7 +27,7 @@ else

Audit log · @(_platformScope ? "platform" : _tenantId)

-

Who changed what, and when — settings, members, invitations and sign-in. Append-only.

+

Who changed what, and when — settings, members, invitations and sign-in. Append-only and tamper-evident.

@if (_isPlatformAdmin) @@ -34,6 +37,9 @@ else } + + Verify chain + Export CSV @@ -41,6 +47,24 @@ else
+ @if (_chainError is not null) + { + + } + else if (_chain is not null) + { +
+ +
+ @_chain.Summary + @if (_chain.Intact && _chain.HeadHash is not null) + { + Latest event hash: @_chain.HeadHash + } +
+
+ } +
@foreach (var k in Kinds) { @@ -60,12 +84,13 @@ else - + @foreach (var e in Filtered()) { + @@ -88,15 +113,22 @@ else private bool _isPlatformAdmin; private bool _platformScope; private bool _loading; + private bool _verifying; // Null = the sign-in carries no tenant claim: fail closed (GAP-17), never the shared "default" tenant. private string? _tenantId; private string _kind = "all"; private IReadOnlyList? _rows; + private AuditChainReport? _chain; + private string? _chainError; private sealed record Kind(string Key, string Label); private static readonly Kind[] Kinds = [new("all", "All"), new("settings", "Settings"), new("members", "Members"), new("signin", "Sign-in")]; + // The scope to read and verify: the platform scope when selected, else the tenant. Null when the sign-in has no + // tenant claim — nothing is read or verified then (GAP-17), not even the platform scope. + private string? Scope => _tenantId is null ? null : _platformScope ? AuditScopes.Platform : _tenantId; + protected override async Task OnInitializedAsync() { if (AuthState is not null) @@ -112,11 +144,11 @@ else private async Task LoadAsync() { - if (_tenantId is not { } tenant) { return; } + if (Scope is not { } scope) { return; } _loading = true; try { - _rows = await Audit.ListAsync(_platformScope ? AuditScopes.Platform : tenant, Max); + _rows = await Audit.ListAsync(scope, Max); } finally { @@ -124,10 +156,24 @@ else } } + private async Task VerifyAsync() + { + if (_verifying || Scope is not { } scope) { return; } + _verifying = true; + _chainError = null; + _chain = null; + await CircuitSafe.RunAsync( + async () => _chain = await ChainVerifier.VerifyAsync(scope), + ex => _chainError = $"Couldn't verify the chain: {ex.Message}"); + _verifying = false; + } + private async Task OnScopeChanged(ChangeEventArgs e) { _platformScope = _isPlatformAdmin && e.Value?.ToString() == "platform"; _rows = null; + _chain = null; + _chainError = null; await LoadAsync(); } @@ -147,7 +193,7 @@ else private static string BadgeClass(string action) => action switch { - "login.failed" or "member.removed" => "err", + "login.failed" or "member.removed" or "invitation.refused" => "err", _ when action.StartsWith("setting.", StringComparison.Ordinal) => "info", _ when action.StartsWith("member.", StringComparison.Ordinal) || action.StartsWith("invitation.", StringComparison.Ordinal) => "warn", _ => "ok", diff --git a/src/AgentOs.Web/Components/Pages/Signup.razor b/src/AgentOs.Web/Components/Pages/Signup.razor index e692335a..6cf871dd 100644 --- a/src/AgentOs.Web/Components/Pages/Signup.razor +++ b/src/AgentOs.Web/Components/Pages/Signup.razor @@ -2,12 +2,33 @@ @layout AuthLayout @attribute [AllowAnonymous] @using AgentOs.Modules.Tenants +@using AgentOs.Modules.Tenants.Invitations +@using Microsoft.Extensions.Logging @inject AgentOs.Modules.Tenants.ITenantSignupService SignupSvc @inject NavigationManager Nav +@inject ILogger Log Create workspace — AgentOS.NET -@if (_closed) +@if (_inviteProblem is not null) +{ + @* GAP-11: an invitation link that is used, revoked, expired or not valid says so — it never falls back to + "create a workspace". *@ + +} +else if (_closed) {
WhenWhoWhatDetail
#WhenWhoWhatDetail
@(e.Sequence?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "—") @e.TimestampUtc.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") @(string.IsNullOrEmpty(e.UserId) ? "—" : e.UserId) @e.Action
+ + + + + + + + + + + @foreach (var invite in _invites) + { + + + + + + + + } + @if (_invites.Count == 0) + { + + } + +
EmailRoleStatusExpiresActions
@invite.Email@RoleLabel(invite.Role)@StatusLabel(invite.Status)@invite.ExpiresAtUtc.ToLocalTime().ToString("yyyy-MM-dd HH:mm") + @if (invite.Status == InvitationStatus.Pending) + { + + Revoke + + } +
No invitations yet — create one above.
}
} } + + + @if (_confirmRevoke is not null) + { +

Revoke the invitation for @_confirmRevoke.Email?

+

Its link stops working at once. You can send a new invitation later.

+ } +
+
+ Cancel + Revoke +
+
+ @if (_confirmRemove is not null) @@ -176,9 +242,15 @@ else private int _inviteTtl = 24; private bool _busy; private string? _inviteUrl; + private string? _invitedEmail; private DateTimeOffset _inviteExpiresAt; private string? _inviteError; + private IReadOnlyList? _invites; + private string? _invitesError; + private InvitationInfo? _confirmRevoke; + private Guid? _revokingId; + protected override async Task OnInitializedAsync() { if (AuthState is not null) @@ -196,12 +268,64 @@ else if (_isAdmin && _tenantId.Length > 0) { await LoadMembersAsync(); + await LoadInvitesAsync(); } } + private async Task LoadInvitesAsync() + { + _invitesError = null; + await CircuitSafe.RunAsync( + async () => _invites = await Signup.ListInvitationsAsync(_tenantId), + ex => _invitesError = $"Couldn't load the invitations: {ex.Message}"); + } + + private static Badge.BadgeVariant StatusVariant(InvitationStatus status) => status switch + { + InvitationStatus.Pending => Badge.BadgeVariant.Info, + InvitationStatus.Accepted => Badge.BadgeVariant.Ok, + InvitationStatus.Revoked => Badge.BadgeVariant.Err, + _ => Badge.BadgeVariant.Warn, + }; + + /// The words on an invitation's status badge (state is never colour-only). + internal static string StatusLabel(InvitationStatus status) => status switch + { + InvitationStatus.Pending => "pending", + InvitationStatus.Accepted => "used", + InvitationStatus.Revoked => "revoked", + _ => "expired", + }; + + private async Task ConfirmRevoke() + { + if (_confirmRevoke is not { } invite || _revokingId is not null) { return; } + _revokingId = invite.Id; + await CircuitSafe.RunAsync( + async () => + { + var revoked = await Signup.RevokeInvitationAsync(_tenantId, invite.Id, new AuditActor(_myUserId)); + Toasts.Show("Users", revoked + ? $"Invitation for {invite.Email} revoked — its link no longer works." + : $"The invitation for {invite.Email} was already used or revoked.", revoked ? ToastKind.Ok : ToastKind.Warn); + await LoadInvitesAsync(); + }, + ex => Toasts.Show("Users", $"Revoke failed: {ex.Message}", ToastKind.Err)); + _revokingId = null; + _confirmRevoke = null; + } + // Admin over approver over member (an approver is a member who may also approve the steps an approval policy holds). private static string PrimaryRole(KeycloakUser m) => AgentOs.Modules.Tenants.TenantRoles.Primary(m.Roles); + /// The words on an invitation's role cell — the same three roles the invite form offers. + private static string RoleLabel(string? role) => role switch + { + "admin" => "Admin", + "approver" => "Approver", + _ => "Member", + }; + private async Task LoadMembersAsync() { _loading = true; @@ -214,7 +338,11 @@ else _loading = false; } - private async Task Reload() => await LoadMembersAsync(); + private async Task Reload() + { + await LoadMembersAsync(); + await LoadInvitesAsync(); // an invitation used or expired since the page opened shows its new state + } private async Task ChangeRole(KeycloakUser m, ChangeEventArgs e) { @@ -314,13 +442,22 @@ else try { var ttl = TimeSpan.FromHours(Math.Clamp(_inviteTtl, 1, 168)); - var token = Signup.CreateInvitation(_tenantId, _inviteRole, email, ttl); + // The invitation is a stored row now (single-use, revocable): a store outage is shown inline, never a crash. + InvitationToken? token = null; + var created = await CircuitSafe.RunAsync( + async () => token = await Signup.CreateInvitationAsync(_tenantId, _inviteRole, email, ttl, new AuditActor(_myUserId)), + ex => _inviteError = ex is ArgumentException ? ex.Message : $"Couldn't create the invitation: {ex.Message}"); + if (!created || token is null) { return; } _inviteExpiresAt = token.ExpiresAtUtc; - _inviteUrl = $"/signup?invite={Uri.EscapeDataString(token.Token)}"; + _invitedEmail = email; + // GAP-05: the link points at Hosting:PublicBaseUrl, like the Api's — never at this circuit's base URI (the + // Host the browser connected with; the circuit's own origin stands in only in Development). + var absoluteUrl = InvitationLinks.Absolute(Origin, token.Token, Nav.BaseUri); + _inviteUrl = absoluteUrl; + await LoadInvitesAsync(); - // Email the absolute link. Best-effort: the URL stays on screen for copy/paste so the + // Email the link. Best-effort: the URL stays on screen for copy/paste so the // admin is never blocked if delivery is unconfigured or fails. - var absoluteUrl = Nav.ToAbsoluteUri(_inviteUrl).ToString(); try { var (subject, htmlBody, textBody) = AgentOs.Modules.Tenants.Email.InvitationEmail.Build(_tenantId, _inviteRole, absoluteUrl); @@ -356,7 +493,6 @@ else Toasts.Show("Users", $"Invitation created, but email failed: {ex.Message}. Copy the link below.", ToastKind.Warn); } } - catch (ArgumentException ex) { _inviteError = ex.Message; } finally { _busy = false; } } } diff --git a/src/AgentOs.Web/Components/UI/SettingsFields.razor b/src/AgentOs.Web/Components/UI/SettingsFields.razor index a1486b77..07a103be 100644 --- a/src/AgentOs.Web/Components/UI/SettingsFields.razor +++ b/src/AgentOs.Web/Components/UI/SettingsFields.razor @@ -70,8 +70,13 @@ value="@f.Draft" @onchange="e => f.Draft = e.Value?.ToString()?.Trim() ?? string.Empty" /> break; } - Save - Reset + @* A read-only field gets NO Save/Reset handler at all: Blazor Server dispatches a registered handler for any + element the client names, disabled attribute or not, so a disabled button is not a gate. *@ + @if (!readOnly) + { + Save + Reset + }
} @@ -90,9 +95,12 @@ [Parameter] public string Area { get; set; } = "Settings"; /// The viewer is a platform admin: platform-scope settings become editable and are written at - /// platform scope (every tenant). Tenant-scope settings still write to . + /// platform scope (every tenant). Tenant-scope settings still write to . This only + /// unlocks the controls — the write itself re-checks the signed-in principal's platform-admin role. [Parameter] public bool CanEditPlatform { get; set; } + [CascadingParameter] private Task? AuthState { get; set; } + private sealed class FieldState(SettingView view) { public SettingView View { get; set; } = view; @@ -146,6 +154,17 @@ try { var scope = WriteScope(f); + // GAP-14: a platform-wide write is a platform operation. The proof is the signed-in principal's platform-admin + // role, checked here at write time (not a parameter, not a disabled button); only then does this flow enter + // the platform RLS scope that opens the "__platform" rows — ISettings refuses a platform write outside one. + if (scope == SettingScope.Platform && !await IsPlatformAdminAsync()) + { + Toasts.Show(Area, $"{f.View.Descriptor.Label} is platform-wide; only a platform admin can change it.", AgentOs.Web.Shell.Services.ToastKind.Err); + return; + } + using var platform = scope == SettingScope.Platform + ? AgentOs.SharedKernel.Persistence.RowLevelSecurity.PlatformScope("platform-settings") + : null; var result = await Settings.SetAsync(f.View.Descriptor.Key, value, scope, scope == SettingScope.Tenant ? TenantId : null, Actor); Toasts.Show(Area, result.Ok ? okMessage : result.Error ?? "Save failed.", result.Ok ? AgentOs.Web.Shell.Services.ToastKind.Ok : AgentOs.Web.Shell.Services.ToastKind.Err); @@ -157,6 +176,16 @@ } } + private async Task IsPlatformAdminAsync() + { + if (!CanEditPlatform || AuthState is null) + { + return false; + } + var state = await AuthState; + return state.User.IsPlatformAdmin(); + } + private static string SettingValidationMask(string secret) => AgentOs.Modules.AppConfig.Settings.SettingValidation.Mask(secret); diff --git a/src/AgentOs.Web/Program.cs b/src/AgentOs.Web/Program.cs index 4399caed..7294068c 100644 --- a/src/AgentOs.Web/Program.cs +++ b/src/AgentOs.Web/Program.cs @@ -330,18 +330,11 @@ await app.Services.InitializeModulesAsync(); -// E0.1 / ADR-0012: Postgres row-level security (stage 1), after every module migrated. Each tenant-table -// connection then switches to the restricted role with the caller's tenant — an authenticated request's -// claim, or the ambient identity a run / desktop circuit pushed. Escape hatch: Persistence:RowLevelSecurity=false. -{ - var httpAccessor = app.Services.GetRequiredService(); - AgentOs.SharedKernel.Persistence.RowLevelSecurity.HttpTenant = () => - httpAccessor.HttpContext?.User is { Identity.IsAuthenticated: true } user ? user.GetTenantId() : null; - if (app.Configuration.GetValue("Persistence:RowLevelSecurity", true)) - { - await AgentOs.SharedKernel.Persistence.RowLevelSecurity.EnsureAsync(app.Configuration.GetConnectionString("DefaultConnection"), app.Logger); - } -} +// E0.1 / ADR-0012 / GAP-14: Postgres row-level security (stage 2), after every module migrated. Each tenant-table +// connection then switches to the restricted role with the caller's tenant — an authenticated request's claim, or the +// ambient identity a run / desktop circuit pushed; no tenant sees nothing. A failed bootstrap stops the host outside +// Development. Escape hatch: Persistence:RowLevelSecurity=false (reported Degraded by /health). +await app.UseAgentOsRowLevelSecurityAsync(); // Surface plugin-contributed desktop windows: collect every PluginAppDescriptor a plugin registered and // add it to the shell catalog (the WindowHost renders these via DynamicComponent). diff --git a/src/AgentOs.Web/Services/Orchestration/ProjectOrchestrator.cs b/src/AgentOs.Web/Services/Orchestration/ProjectOrchestrator.cs index 41c3d9e3..9c83b09e 100644 --- a/src/AgentOs.Web/Services/Orchestration/ProjectOrchestrator.cs +++ b/src/AgentOs.Web/Services/Orchestration/ProjectOrchestrator.cs @@ -59,6 +59,8 @@ public void Add(string tenantId, Guid boardId, string text) { try { + // RLS stage 2: a caller with no tenant of its own (the worker) is bound to the board's tenant for this read. + using var bound = AgentOs.SharedKernel.Persistence.RowLevelSecurity.TenantScopeIfUnbound(tenantId); using var scope = scopes.CreateScope(); var stored = await scope.ServiceProvider.GetRequiredService() .ListAutopilotEventsAsync(tenantId, boardId, Keep).ConfigureAwait(false); @@ -98,6 +100,9 @@ private async Task PersistAsync(string tenantId, Guid boardId, string text) { try { + // Fire-and-forget from the worker, sometimes outside the board's identity push: bind the write to the board's + // tenant (RLS stage 2 refuses a row written with no tenant). + using var bound = AgentOs.SharedKernel.Persistence.RowLevelSecurity.TenantScopeIfUnbound(tenantId); using var scope = scopes.CreateScope(); await scope.ServiceProvider.GetRequiredService().AddAutopilotEventAsync(tenantId, boardId, text).ConfigureAwait(false); } diff --git a/src/AgentOs.Web/Services/Preview/PreviewService.cs b/src/AgentOs.Web/Services/Preview/PreviewService.cs index 996d5426..08fa33a0 100644 --- a/src/AgentOs.Web/Services/Preview/PreviewService.cs +++ b/src/AgentOs.Web/Services/Preview/PreviewService.cs @@ -227,6 +227,9 @@ private async Task AttachUnknownAsync(PreviewReattach reattach) // The tenant's repo the preview names: under that board, in that tenant, with the same owner/repo. Null otherwise. private async Task FindRepoAsync(string tenantId, PreviewAnnouncement p) { + // Runs in the runner hub's flow, which has no tenant of its own: bind the read to the runner's tenant (from its + // verified connection) — RLS stage 2 shows a connection with no tenant nothing. + using var bound = AgentOs.SharedKernel.Persistence.RowLevelSecurity.TenantScopeIfUnbound(tenantId); using var scope = _scopes.CreateScope(); if (scope.ServiceProvider.GetService() is not { } store) { return null; } var repos = await store.ListReposForTenantAsync(tenantId, p.BoardId).ConfigureAwait(false); diff --git a/src/AgentOs.Web/wwwroot/app.css b/src/AgentOs.Web/wwwroot/app.css index b9824e80..f554f39e 100644 --- a/src/AgentOs.Web/wwwroot/app.css +++ b/src/AgentOs.Web/wwwroot/app.css @@ -2291,6 +2291,16 @@ input[type=range] { accent-color: var(--accent); } .audit-when { white-space: nowrap; font-family: var(--mono); font-size: 11.5px; color: var(--txt-soft); } .audit-detail { overflow-wrap: anywhere; font-size: 12px; } .audit-foot { margin-top: var(--space-2); font-size: 12px; } +/* GAP-11b — "Verify chain" result: intact (ok) or the first broken event (err). State carries text + icon, never colour alone. */ +.audit-seq { white-space: nowrap; font-family: var(--mono); font-size: 11px; color: var(--txt-dim); } +.audit-chain { display: flex; align-items: flex-start; gap: var(--space-2); margin: 0 0 var(--space-3); padding: var(--space-2) var(--space-3); border: 1px solid var(--line); border-radius: var(--r-4); background: var(--bg-3); color: var(--txt); font-size: 12px; } +.audit-chain.is-intact { background: color-mix(in srgb, var(--ok) 9%, var(--bg-2)); border-color: color-mix(in srgb, var(--ok) 35%, var(--line)); } +.audit-chain.is-intact > .icon { flex: none; color: var(--ok-text); } +.audit-chain.is-broken { background: color-mix(in srgb, var(--err) 9%, var(--bg-2)); border-color: color-mix(in srgb, var(--err) 40%, var(--line)); } +.audit-chain.is-broken > .icon { flex: none; color: var(--err); } +.audit-chain-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; } +.audit-chain-title { font-weight: var(--fw-semibold); } +.audit-chain-head { font-family: var(--mono); font-size: 11px; color: var(--txt-dim); overflow-wrap: anywhere; } /* Agent profiles (Settings → Agent profiles) — compact editable data-table; tokens only. */ .agent-profiles-scroll { overflow-x: auto; } diff --git a/tests/AgentOs.Benchmarks/Settings/SettingsBenchmarks.cs b/tests/AgentOs.Benchmarks/Settings/SettingsBenchmarks.cs index 0dd5cf3a..bafdc918 100644 --- a/tests/AgentOs.Benchmarks/Settings/SettingsBenchmarks.cs +++ b/tests/AgentOs.Benchmarks/Settings/SettingsBenchmarks.cs @@ -11,6 +11,7 @@ using AgentOs.Modules.AppConfig; using AgentOs.Modules.AppConfig.Persistence; using AgentOs.Modules.AppConfig.Settings; +using AgentOs.SharedKernel.Persistence; using BenchmarkDotNet.Attributes; using Microsoft.AspNetCore.DataProtection; using Microsoft.EntityFrameworkCore; @@ -100,6 +101,7 @@ public async Task Setup() SettingsFixture.Catalog(), store, SettingsFixture.Configuration(), new NullSettingsAuditSink(), NullLogger.Instance); (await _settings.SetAsync(_tenantKey, "claude-opus-5-5", SettingScope.Tenant, _tenant, "bench")).Ok.ShouldBeTrue(); + using var platform = RowLevelSecurity.PlatformScope("test"); (await _settings.SetAsync(_platformKey, "Claude", SettingScope.Platform, null, "bench")).Ok.ShouldBeTrue(); } diff --git a/tests/AgentOs.E2E.Tests/AgentOs.E2E.Tests.csproj b/tests/AgentOs.E2E.Tests/AgentOs.E2E.Tests.csproj index 2cf11b78..4dcc00f2 100644 --- a/tests/AgentOs.E2E.Tests/AgentOs.E2E.Tests.csproj +++ b/tests/AgentOs.E2E.Tests/AgentOs.E2E.Tests.csproj @@ -10,6 +10,9 @@ + + diff --git a/tests/AgentOs.E2E.Tests/AgentOsUi/ApiTokensRealAuthTests.cs b/tests/AgentOs.E2E.Tests/AgentOsUi/ApiTokensRealAuthTests.cs index c6cf1d61..3ee00ad6 100644 --- a/tests/AgentOs.E2E.Tests/AgentOsUi/ApiTokensRealAuthTests.cs +++ b/tests/AgentOs.E2E.Tests/AgentOsUi/ApiTokensRealAuthTests.cs @@ -69,12 +69,10 @@ public async Task Token_AuthenticatesAgainstTheApi_UntilRevoked() await using var conn = new NpgsqlConnection(cs); await conn.OpenAsync(); await using var cmd = conn.CreateCommand(); - cmd.CommandText = """ - DELETE FROM tenants.api_tokens WHERE "Name" = @n; - DELETE FROM tenants.audit_events WHERE "Action" LIKE 'apitoken.%' AND "Target" LIKE @p; - """; + // The apitoken.* audit rows stay: the trail is append-only and hash-chained (GAP-11b) — removing them + // would break the operator tenant's chain. + cmd.CommandText = """DELETE FROM tenants.api_tokens WHERE "Name" = @n"""; cmd.Parameters.AddWithValue("n", name); - cmd.Parameters.AddWithValue("p", name + " (%"); await cmd.ExecuteNonQueryAsync(); } } diff --git a/tests/AgentOs.E2E.Tests/AgentOsUi/AuditLogRealAuthTests.cs b/tests/AgentOs.E2E.Tests/AgentOsUi/AuditLogRealAuthTests.cs index f640b380..fa8c2a7a 100644 --- a/tests/AgentOs.E2E.Tests/AgentOsUi/AuditLogRealAuthTests.cs +++ b/tests/AgentOs.E2E.Tests/AgentOsUi/AuditLogRealAuthTests.cs @@ -1,13 +1,14 @@ // REAL-AUTH E2E — plan C.3 on the full stack: a governance write made in the desktop shows up in Settings → // Audit log with who / what / value. Saving Tool policy unchanged re-writes the same allowlist + enforce flag // (no state change to restore) and must produce `setting.changed` rows for `tools/enforce` and -// `tools/allowlist`, attributed to the signed-in operator. The CSV export link is served same-origin. -// Residue: the audit rows this test caused are removed afterwards when AGENTOS_PG_CONN is set. +// `tools/allowlist`, attributed to the signed-in operator. The CSV export link is served same-origin, and "Verify chain" +// reports the tenant's hash chain intact (GAP-11b). +// Residue: none in the tenant's settings (the save writes the values it read). The audit rows it causes STAY — the trail +// is append-only and hash-chained, so deleting them would (rightly) break the operator tenant's chain. using System; using System.Threading.Tasks; using Microsoft.Playwright; -using Npgsql; using Xunit; namespace AgentOs.E2E.Tests.AgentOsUi; @@ -23,45 +24,29 @@ public async Task PolicySave_ShowsUpInTheAuditLog() { if (!AgentOsRealAuthFixture.IsEnabled) { Assert.Skip(AgentOsRealAuthFixture.SkipReason); } - var started = DateTimeOffset.UtcNow.AddSeconds(-2); - try - { - await _fx.LoginAsync(); - await _fx.Page.Locator(".dock-item[title=\"Settings\"]").First.ClickAsync(); - var win = _fx.Page.Locator(".appwin.focused"); - await win.Locator(".prefs-cat", new() { HasTextString = "Tool policy" }).ClickAsync(); - // Wait for the page to switch: the Settings landing page (Model keys) has Save buttons of its own. - await Assertions.Expect(win.Locator(".page-title", new() { HasTextString = "Tool Policy" })).ToBeVisibleAsync(); - await win.GetByRole(AriaRole.Button, new() { Name = "Save", Exact = true }).ClickAsync(); - await Assertions.Expect(_fx.Page.Locator(".toast").Last).ToContainTextAsync("Policy"); - - await win.Locator(".prefs-cat", new() { HasTextString = "Activity log" }).ClickAsync(); - await win.Locator(".audit-filter", new() { HasTextString = "Settings" }).ClickAsync(); - var table = win.Locator(".audit-table"); - await Assertions.Expect(table).ToContainTextAsync("tools/enforce", new() { Timeout = 15_000 }); - await Assertions.Expect(table).ToContainTextAsync("tools/allowlist"); - await Assertions.Expect(table.Locator("tr", new() { HasTextString = "tools/enforce" }).First).ToContainTextAsync("operator"); - - // Export is a same-origin, cookie-authed CSV. - var response = await _fx.Page.APIRequest.GetAsync("/audit/export?scope=tenant"); - Assert.Equal(200, response.Status); - Assert.Contains("timestamp_utc,tenant,user,action,detail", await response.TextAsync(), StringComparison.Ordinal); - } - finally - { - if (Environment.GetEnvironmentVariable("AGENTOS_PG_CONN") is { Length: > 0 } cs) - { - await using var conn = new NpgsqlConnection(cs); - await conn.OpenAsync(); - await using var cmd = conn.CreateCommand(); - cmd.CommandText = """ - DELETE FROM tenants.audit_events - WHERE "Action" = 'setting.changed' AND "TimestampUtc" >= @since - AND ("Target" LIKE 'tools/enforce:%' OR "Target" LIKE 'tools/allowlist:%') - """; - cmd.Parameters.AddWithValue("since", started); - await cmd.ExecuteNonQueryAsync(); - } - } + await _fx.LoginAsync(); + await _fx.Page.Locator(".dock-item[title=\"Settings\"]").First.ClickAsync(); + var win = _fx.Page.Locator(".appwin.focused"); + await win.Locator(".prefs-cat", new() { HasTextString = "Tool policy" }).ClickAsync(); + // Wait for the page to switch: the Settings landing page (Model keys) has Save buttons of its own. + await Assertions.Expect(win.Locator(".page-title", new() { HasTextString = "Tool Policy" })).ToBeVisibleAsync(); + await win.GetByRole(AriaRole.Button, new() { Name = "Save", Exact = true }).ClickAsync(); + await Assertions.Expect(_fx.Page.Locator(".toast").Last).ToContainTextAsync("Policy"); + + await win.Locator(".prefs-cat", new() { HasTextString = "Activity log" }).ClickAsync(); + await win.Locator(".audit-filter", new() { HasTextString = "Settings" }).ClickAsync(); + var table = win.Locator(".audit-table"); + await Assertions.Expect(table).ToContainTextAsync("tools/enforce", new() { Timeout = 15_000 }); + await Assertions.Expect(table).ToContainTextAsync("tools/allowlist"); + await Assertions.Expect(table.Locator("tr", new() { HasTextString = "tools/enforce" }).First).ToContainTextAsync("operator"); + + // Export is a same-origin, cookie-authed CSV. + var response = await _fx.Page.APIRequest.GetAsync("/audit/export?scope=tenant"); + Assert.Equal(200, response.Status); + Assert.Contains("timestamp_utc,tenant,user,action,detail", await response.TextAsync(), StringComparison.Ordinal); + + // GAP-11b: the trail is tamper-evident — the verifier walks the tenant's chain and finds it intact. + await win.GetByRole(AriaRole.Button, new() { Name = "Verify chain" }).ClickAsync(); + await Assertions.Expect(win.Locator("[data-testid=audit-chain]")).ToContainTextAsync("Chain intact", new() { Timeout = 15_000 }); } } diff --git a/tests/AgentOs.E2E.Tests/AgentOsUi/InvitationEmailRealAuthTests.cs b/tests/AgentOs.E2E.Tests/AgentOsUi/InvitationEmailRealAuthTests.cs index 0fe8fe6b..e9bf0fca 100644 --- a/tests/AgentOs.E2E.Tests/AgentOsUi/InvitationEmailRealAuthTests.cs +++ b/tests/AgentOs.E2E.Tests/AgentOsUi/InvitationEmailRealAuthTests.cs @@ -1,6 +1,7 @@ // REAL-AUTH E2E — the invitation-email path end-to-end, fully automated (no human checking an inbox). -// operator logs in via real Keycloak → Users app → mint an invite for a unique address → the app's -// MailKit sender delivers to MailHog → we assert the message landed via the MailHog API. +// operator logs in via real Keycloak → Users app → create an invite for a unique address → the app's +// MailKit sender delivers to MailHog → we assert the message landed via the MailHog API. Then GAP-11: the invitation is +// listed as pending, the admin revokes it, and its link says it can't be used. // Gated by RUN_AGENTOS_E2E_REAL=true with the full Aspire stack running (Web + Keycloak + MailHog). using System; @@ -34,13 +35,27 @@ public async Task Invite_RealAuth_EmailLandsInMailHog() // Mint an invite for a unique address so the MailHog assertion can't collide with other runs. var email = $"e2e-invite-{Guid.NewGuid():N}@agentic.local"; await win.GetByPlaceholder("bob@acme.test").FillAsync(email); - await win.GetByRole(AriaRole.Button, new() { Name = "Mint invite URL" }).ClickAsync(); + await win.GetByRole(AriaRole.Button, new() { Name = "Create invitation" }).ClickAsync(); - // UI confirms the invite URL was minted. - await Assertions.Expect(win.Locator(".cred-readout textarea")).ToContainTextAsync("/signup?invite="); + // UI confirms the invite URL was created. + var link = win.Locator(".cred-readout textarea"); + await Assertions.Expect(link).ToContainTextAsync("/signup?invite="); + var url = await link.InputValueAsync(); // The real assertion: the MailKit sender delivered the invite to MailHog for that address. var delivered = await MailHog.WaitForMessageAsync(TimeSpan.FromSeconds(20), email); Assert.True(delivered, $"No invitation email for {email} arrived in MailHog within 20s."); + + // GAP-11: listed as pending; the admin revokes it; the link then says it can't be used. + var row = win.Locator("tr", new() { HasTextString = email }); + await Assertions.Expect(row.Locator(".badgepill")).ToHaveTextAsync("pending"); + await row.GetByRole(AriaRole.Button, new() { Name = "Revoke" }).ClickAsync(); + await _fx.Page.Locator(".dialog").GetByRole(AriaRole.Button, new() { Name = "Revoke" }).ClickAsync(); + await Assertions.Expect(row.Locator(".badgepill")).ToHaveTextAsync("revoked", new() { Timeout = 15_000 }); + + var page = await _fx.Page.Context.NewPageAsync(); + await page.GotoAsync(url); + await Assertions.Expect(page.Locator("[data-testid=invite-unusable]")).ToContainTextAsync("withdrawn by an administrator"); + await page.CloseAsync(); } } diff --git a/tests/AgentOs.E2E.Tests/AgentOsUi/InvitationsTests.cs b/tests/AgentOs.E2E.Tests/AgentOsUi/InvitationsTests.cs new file mode 100644 index 00000000..c2861646 --- /dev/null +++ b/tests/AgentOs.E2E.Tests/AgentOsUi/InvitationsTests.cs @@ -0,0 +1,89 @@ +// GAP-11 / 11b in the desktop, on the standalone Web (invitations live in the process; no audit store): +// • Settings › Users: an admin creates an invitation — the link is shown with the address it works for, and the +// invitation is listed as pending; its link opens the join form with that address fixed (not editable); +// • revoked from the list (confirmed in a dialog) it shows "revoked", and its link says it was withdrawn; +// • Settings › Activity log: "Verify chain" answers in words (standalone records nothing, so nothing to verify). +// Using an invitation once (and the refused second use) needs the tenant registry — covered by the unit and +// real-Postgres tests (TenantSignupServiceTests, AuditAndInvitationsRealPostgresTests) and the full-stack test plan. + +using System; +using System.Threading.Tasks; +using Microsoft.Playwright; +using Xunit; + +namespace AgentOs.E2E.Tests.AgentOsUi; + +public sealed class InvitationsTests : IClassFixture +{ + private readonly AgentOsPageFixture _fx; + + public InvitationsTests(AgentOsPageFixture fx) => _fx = fx; + + [Fact] + public async Task Invitation_IsListed_EmailBound_AndRevocable() + { + if (!AgentOsPageFixture.IsEnabled) { Assert.Skip(AgentOsPageFixture.SkipReason); } + await _fx.GotoDesktopAsync(); + var win = await OpenSettingsPageAsync("Users"); + + var email = $"e2e-{Guid.NewGuid():N}@acme.test"; + await win.GetByPlaceholder("bob@acme.test").FillAsync(email); + await win.GetByRole(AriaRole.Button, new() { Name = "Create invitation" }).ClickAsync(); + var link = win.GetByLabel("Invitation link"); + await Assertions.Expect(link).ToContainTextAsync("/signup?invite="); + await Assertions.Expect(win.Locator(".cred-readout-hint")).ToContainTextAsync(email); + var url = await link.InputValueAsync(); + + var row = win.Locator("tr", new() { HasTextString = email }); + await Assertions.Expect(row.Locator(".badgepill")).ToHaveTextAsync("pending"); + + // The link opens the join form for exactly that address. + var visitor = await _fx.Context.NewPageAsync(); + await visitor.GotoAsync(url); + var address = visitor.Locator("input[type=email]"); + await Assertions.Expect(address).ToHaveValueAsync(email); + await Assertions.Expect(address).ToHaveAttributeAsync("readonly", string.Empty); + + // Revoked from the list → the same link says it was withdrawn. + await row.GetByRole(AriaRole.Button, new() { Name = "Revoke" }).ClickAsync(); + var dialog = _fx.Page.Locator(".dialog"); + await Assertions.Expect(dialog).ToContainTextAsync(email); + await dialog.GetByRole(AriaRole.Button, new() { Name = "Revoke" }).ClickAsync(); + await Assertions.Expect(row.Locator(".badgepill")).ToHaveTextAsync("revoked"); + await Assertions.Expect(row.GetByRole(AriaRole.Button, new() { Name = "Revoke" })).ToHaveCountAsync(0); + + await visitor.GotoAsync(url); + await Assertions.Expect(visitor.GetByTestId("invite-unusable")).ToContainTextAsync("withdrawn by an administrator"); + await visitor.CloseAsync(); + } + + [Fact] + public async Task InvitationLink_Tampered_SaysItIsNotValid() + { + if (!AgentOsPageFixture.IsEnabled) { Assert.Skip(AgentOsPageFixture.SkipReason); } + await _fx.Page.GotoAsync("/signup?invite=not-a-real-invitation"); + await Assertions.Expect(_fx.Page.GetByTestId("invite-unusable")).ToContainTextAsync("isn't valid"); + } + + [Fact] + public async Task ActivityLog_VerifyChain_AnswersInWords() + { + if (!AgentOsPageFixture.IsEnabled) { Assert.Skip(AgentOsPageFixture.SkipReason); } + await _fx.GotoDesktopAsync(); + var win = await OpenSettingsPageAsync("Activity log"); + + await win.GetByRole(AriaRole.Button, new() { Name = "Verify chain" }).ClickAsync(); + var result = win.GetByTestId("audit-chain"); + await Assertions.Expect(result).ToBeVisibleAsync(); + await Assertions.Expect(result).ToHaveAttributeAsync("role", "status"); + await Assertions.Expect(result).ToContainTextAsync("Nothing to verify yet"); + } + + private async Task OpenSettingsPageAsync(string page) + { + await _fx.Page.Locator(".dock-item[title=\"Settings\"]").First.ClickAsync(); + var win = _fx.Page.Locator(".appwin.focused"); + await win.Locator(".prefs-cat", new() { HasTextString = page }).ClickAsync(); + return win; + } +} diff --git a/tests/AgentOs.E2E.Tests/AgentOsUi/RowLevelSecurityRealDbTests.cs b/tests/AgentOs.E2E.Tests/AgentOsUi/RowLevelSecurityRealDbTests.cs index 5999ac00..e8e57311 100644 --- a/tests/AgentOs.E2E.Tests/AgentOsUi/RowLevelSecurityRealDbTests.cs +++ b/tests/AgentOs.E2E.Tests/AgentOsUi/RowLevelSecurityRealDbTests.cs @@ -1,14 +1,17 @@ -// REAL-DB — Postgres row-level security (ADR-0012, stage 1) on the running stack's database. Proves the -// database itself — not the application's filters — keeps tenants apart: -// • every stage-1 table has the agentos_tenant policy, RLS enabled AND forced (the owner is bound too); -// • as the app role with tenant A set, a query with NO tenant predicate returns only A's rows; -// • inserting a row for tenant B is refused (WITH CHECK); -// • with no tenant set, stage 1 stays permissive (pairing / background paths keep working). -// Seeds as the connection user (superuser, bypasses RLS) and removes exactly what it inserted. +// REAL-DB — Postgres row-level security (ADR-0012, stage 2 — GAP-14) on the running stack's database, after the real +// hosts bootstrapped it. Proves the database itself — not the application's filters — keeps tenants apart: +// • EVERY table in RowLevelSecurity.Tables has the agentos_tenant policy, RLS enabled AND forced (the owner is bound +// too), with the stage-2 predicate (no "unset tenant ⇒ visible" escape); +// • the audit trail is append-only for the app role (no UPDATE / DELETE / TRUNCATE privilege — GAP-11b); +// • as the app role with tenant A set, a query with NO tenant predicate returns only A's rows; writing B's is refused; +// • with no tenant set, nothing is visible (stage 1 showed everything); an explicit platform scope sees both. +// Seeds as the connection user (superuser, bypasses RLS) and removes exactly what it inserted. Every table is also +// covered behaviourally, on a throwaway database, by AgentOs.Tests' RowLevelSecurityStageTwoRealPostgresTests. // - Gate: RUN_AGENTOS_E2E_REAL=true and AGENTOS_PG_CONN (the stack's `agentos` database). using System; using System.Threading.Tasks; +using AgentOs.SharedKernel.Persistence; using Npgsql; using Xunit; @@ -16,45 +19,64 @@ namespace AgentOs.E2E.Tests.AgentOsUi; public sealed class RowLevelSecurityRealDbTests { - private static readonly string[] Tables = - [ - "pipeline.pipeline_runs", "pipeline.run_metrics", "pipeline.orchestrations", - "sessions.sessions", "sessions.session_repos", "sessions.runners", "sessions.ticket_files", - "tools.tool_invocations", "workspaces.workspaces", "workspaces.workspace_repos", "workspaces.ticket_flows", - ]; - private static string? Conn => string.Equals(Environment.GetEnvironmentVariable("RUN_AGENTOS_E2E_REAL"), "true", StringComparison.OrdinalIgnoreCase) ? Environment.GetEnvironmentVariable("AGENTOS_PG_CONN") : null; [Fact] - public async Task Policies_ExistAndAreForced_OnEveryStageOneTable() + public async Task Policies_ExistAreForcedAndStageTwo_OnEveryTable() { var cs = Conn ?? string.Empty; if (cs.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } await using var conn = new NpgsqlConnection(cs); await conn.OpenAsync(); - foreach (var table in Tables) + foreach (var (schema, table) in RowLevelSecurity.Tables) { + var name = $"{schema}.{table}"; await using var cmd = conn.CreateCommand(); cmd.CommandText = """ - SELECT c.relrowsecurity, c.relforcerowsecurity, - EXISTS (SELECT 1 FROM pg_policies p WHERE p.schemaname = n.nspname AND p.tablename = c.relname AND p.policyname = 'agentos_tenant') + SELECT c.relrowsecurity, c.relforcerowsecurity, p.qual FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace - WHERE n.nspname || '.' || c.relname = @t + LEFT JOIN pg_policies p ON p.schemaname = n.nspname AND p.tablename = c.relname AND p.policyname = 'agentos_tenant' + WHERE n.nspname = @s AND c.relname = @t """; + cmd.Parameters.AddWithValue("s", schema); cmd.Parameters.AddWithValue("t", table); await using var r = await cmd.ExecuteReaderAsync(); - Assert.True(await r.ReadAsync(), $"{table} missing"); - Assert.True(r.GetBoolean(0), $"{table}: RLS not enabled"); - Assert.True(r.GetBoolean(1), $"{table}: RLS not forced"); - Assert.True(r.GetBoolean(2), $"{table}: agentos_tenant policy missing"); + Assert.True(await r.ReadAsync(), $"{name} missing"); + Assert.True(r.GetBoolean(0), $"{name}: RLS not enabled"); + Assert.True(r.GetBoolean(1), $"{name}: RLS not forced"); + Assert.False(r.IsDBNull(2), $"{name}: agentos_tenant policy missing"); + var qual = r.GetString(2); + Assert.Contains("NULLIF", qual, StringComparison.OrdinalIgnoreCase); + Assert.DoesNotContain("COALESCE", qual, StringComparison.OrdinalIgnoreCase); // the stage-1 escape + } + } + + [Fact] + public async Task AppendOnlyTables_TheAppRoleCannotChangeOrRemoveRows() + { + var cs = Conn ?? string.Empty; + if (cs.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } + await using var conn = new NpgsqlConnection(cs); + await conn.OpenAsync(); + foreach (var (schema, table) in RowLevelSecurity.AppendOnlyTables) + { + foreach (var (privilege, granted) in new[] { ("SELECT", true), ("INSERT", true), ("UPDATE", false), ("DELETE", false), ("TRUNCATE", false) }) + { + await using var cmd = conn.CreateCommand(); + cmd.CommandText = "SELECT has_table_privilege(@role, @table, @privilege)"; + cmd.Parameters.AddWithValue("role", RowLevelSecurity.Role); + cmd.Parameters.AddWithValue("table", $"{schema}.{table}"); + cmd.Parameters.AddWithValue("privilege", privilege); + Assert.Equal(granted, (bool)(await cmd.ExecuteScalarAsync())!); + } } } [Fact] - public async Task AppRole_SeesOnlyItsTenant_AndCannotWriteAnother() + public async Task AppRole_SeesOnlyItsTenant_NoTenantSeesNothing_AndCannotWriteAnother() { var cs = Conn ?? string.Empty; if (cs.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } @@ -71,7 +93,7 @@ INSERT INTO workspaces.workspaces ("Id","TenantId","Name","Kind","CredentialRef" """); // As the app role, tenant A, a query that FORGOT its tenant predicate: - await Exec(conn, $"SET ROLE agentos_rls; SELECT set_config('app.tenant_id', '{a}', false)"); + await Exec(conn, $"SET ROLE {RowLevelSecurity.Role}; SELECT set_config('{RowLevelSecurity.TenantSetting}', '{a}', false)"); Assert.Equal(1L, await Count(conn, marker)); Assert.Equal(0L, await Count(conn, marker, tenant: b)); // asking for B explicitly still sees nothing @@ -81,8 +103,12 @@ INSERT INTO workspaces.workspaces ("Id","TenantId","Name","Kind","CredentialRef" """)); Assert.Equal("42501", refused.SqlState); // new row violates row-level security policy - // Stage 1: no tenant known → permissive (pairing, background drains). - await Exec(conn, "SELECT set_config('app.tenant_id', '', false)"); + // Stage 2: no tenant known → nothing (it was "everything" in stage 1). + await Exec(conn, $"SELECT set_config('{RowLevelSecurity.TenantSetting}', '', false)"); + Assert.Equal(0L, await Count(conn, marker)); + + // An explicit platform scope (the scheduler, the autopilot scan) sees every tenant. + await Exec(conn, $"SELECT set_config('{RowLevelSecurity.PlatformSetting}', 'on', false)"); Assert.Equal(2L, await Count(conn, marker)); } finally diff --git a/tests/AgentOs.E2E.Tests/AgentOsUi/TenantDataRealAuthTests.cs b/tests/AgentOs.E2E.Tests/AgentOsUi/TenantDataRealAuthTests.cs index c44d73e4..4c177923 100644 --- a/tests/AgentOs.E2E.Tests/AgentOsUi/TenantDataRealAuthTests.cs +++ b/tests/AgentOs.E2E.Tests/AgentOsUi/TenantDataRealAuthTests.cs @@ -148,8 +148,9 @@ private static async Task RemoveAuditAsync(string tenant) await using var conn = new NpgsqlConnection(PgConn); await conn.OpenAsync(); await using var cmd = conn.CreateCommand(); - cmd.CommandText = "DELETE FROM tenants.audit_events WHERE (\"TenantId\" = '_platform' AND \"Target\" LIKE @p) OR \"TenantId\" = @t"; - cmd.Parameters.AddWithValue("p", tenant + ":%"); + // The throwaway tenant's whole chain goes; the platform's "tenant.purged" row stays — removing it from the middle of + // the append-only, hash-chained platform trail would break that chain (GAP-11b). + cmd.CommandText = "DELETE FROM tenants.audit_events WHERE \"TenantId\" = @t"; cmd.Parameters.AddWithValue("t", tenant); await cmd.ExecuteNonQueryAsync(); } diff --git a/tests/AgentOs.Tests/Agents/AgentProfilesTests.cs b/tests/AgentOs.Tests/Agents/AgentProfilesTests.cs index c9375b66..4453f140 100644 --- a/tests/AgentOs.Tests/Agents/AgentProfilesTests.cs +++ b/tests/AgentOs.Tests/Agents/AgentProfilesTests.cs @@ -14,6 +14,7 @@ using AgentOs.Modules.Llm; using AgentOs.Modules.Pipeline.Agents; using AgentOs.SharedKernel.Identity; +using AgentOs.SharedKernel.Persistence; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; @@ -104,6 +105,7 @@ public async Task Agent_TenantOverride_DoesNotLeakToAnotherTenant() public async Task Agent_PlatformValue_AppliesToEveryTenantWithoutOverride() { var settings = Settings(); + using var platform = RowLevelSecurity.PlatformScope("test"); await settings.SetAsync(RequirementModel, "claude-haiku-5-5", SettingScope.Platform, null, "root"); var (factory, calls) = RecordingFactory(); diff --git a/tests/AgentOs.Tests/Configuration/PlatformSettingsTests.cs b/tests/AgentOs.Tests/Configuration/PlatformSettingsTests.cs index ee3d1ebf..51282ef3 100644 --- a/tests/AgentOs.Tests/Configuration/PlatformSettingsTests.cs +++ b/tests/AgentOs.Tests/Configuration/PlatformSettingsTests.cs @@ -13,6 +13,7 @@ using AgentOs.Modules.Tools.Policy; using AgentOs.Modules.Workspaces; using AgentOs.Modules.Workspaces.Security; +using AgentOs.SharedKernel.Persistence; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging.Abstractions; @@ -45,6 +46,7 @@ public async Task ToolPolicy_PlatformEnforceByDefault_FlipsLive() (await policy.EvaluateAsync(Call())).Allowed.ShouldBeTrue(); // permissive default + using var platform = RowLevelSecurity.PlatformScope("test"); (await settings.SetAsync(ToolsSettings.EnforceByDefault, "true", SettingScope.Platform, null, "root")).Ok.ShouldBeTrue(); var decision = await policy.EvaluateAsync(Call()); // same instance — no restart decision.Allowed.ShouldBeFalse(); // fail-closed: no allowlist @@ -69,6 +71,7 @@ public async Task RemoteExecApprover_FollowsPlatformSetting() (await approver.ApproveAsync(req)).ShouldBeFalse(); + using var platform = RowLevelSecurity.PlatformScope("test"); await settings.SetAsync(ConfigGatedRemoteExecApprover.AutoApproveKey, "true", SettingScope.Platform, null, "root"); (await approver.ApproveAsync(req)).ShouldBeTrue(); } @@ -82,6 +85,7 @@ public async Task OperatorLock_PinsAutoApproveOff_AndRefusesUiWrites() var approver = new ConfigGatedRemoteExecApprover(env, new ConfigurationBuilder().Build(), NullLogger.Instance, settings); + using var platform = RowLevelSecurity.PlatformScope("test"); (await settings.SetAsync(ConfigGatedRemoteExecApprover.AutoApproveKey, "true", SettingScope.Platform, null, "root")).Ok.ShouldBeFalse(); (await approver.ApproveAsync(new RemoteExecRequest("r1", "s", "u", "m"))).ShouldBeFalse(); } @@ -94,6 +98,7 @@ public async Task HostPolicy_PlatformAllowedHosts_AddsSelfHostedHostLive_AndKeep policy.IsAllowed("https://github.mycorp.com").ShouldBeFalse(); + using var platform = RowLevelSecurity.PlatformScope("test"); (await settings.SetAsync(WorkspaceHostPolicy.SettingKey, "github.mycorp.com, ado.mycorp.com", SettingScope.Platform, null, "root")).Ok.ShouldBeTrue(); policy.IsAllowed("https://github.mycorp.com/api/v3").ShouldBeTrue(); @@ -106,6 +111,7 @@ public async Task HostPolicy_PlatformAllowedHosts_AddsSelfHostedHostLive_AndKeep public async Task HostPolicy_InvalidEntry_IsRejectedAtSave() { var (settings, _) = Build(); + using var platform = RowLevelSecurity.PlatformScope("test"); var r = await settings.SetAsync(WorkspaceHostPolicy.SettingKey, "github.mycorp.com, http://", SettingScope.Platform, null, "root"); r.Ok.ShouldBeFalse(); } diff --git a/tests/AgentOs.Tests/Configuration/SettingsServicePlatformScopeTests.cs b/tests/AgentOs.Tests/Configuration/SettingsServicePlatformScopeTests.cs new file mode 100644 index 00000000..051fccbd --- /dev/null +++ b/tests/AgentOs.Tests/Configuration/SettingsServicePlatformScopeTests.cs @@ -0,0 +1,124 @@ +// Security review of PR #326 (RLS stage 2, GAP-14): the "__platform" rows of config.app_config are writable only inside an +// explicit RowLevelSecurity.PlatformScope — that is the database wall. SettingsService.SetAsync(scope: Platform) enters +// that scope ITSELF ("the caller already proved the platform-admin role") without any proof: it takes no principal, no +// role, nothing. Any code path that can reach SetAsync with SettingScope.Platform — the Settings › Routing screen renders +// the platform-scoped Llm:OfflineFallback to EVERY tenant admin as a disabled field whose Save handler is still registered +// (SettingsFields.razor + Btn.razor), and Blazor Server dispatches a circuit event for a disabled element's handler when +// a client sends it — writes platform-wide settings, and the database accepts because the service said "platform". +// The service must never self-elevate: a platform write from a flow that is not a platform operation is refused. + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using AgentOs.Domain.Configuration; +using AgentOs.Modules.AppConfig; +using AgentOs.Modules.AppConfig.Settings; +using AgentOs.SharedKernel.Persistence; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging.Abstractions; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Configuration; + +public sealed class SettingsServicePlatformScopeTests +{ + private const string PlatformSwitch = "Test:PlatformSwitch"; + + private sealed class TestSettings : ISettingsContributor + { + public IEnumerable Describe() + { + yield return new(PlatformSwitch, SettingKind.Boolean, "T", "Platform switch") { DefaultValue = "false", Scope = SettingScope.Platform }; + } + } + + // Records, for every write, whether the flow was a platform operation at that moment — what the database policy sees. + private sealed class RecordingStore : IAppConfigStore + { + public List<(string Tenant, string Key, string Value, bool InPlatformScope)> Writes { get; } = []; + + public ValueTask GetAsync(string key, CancellationToken cancellationToken = default) => ValueTask.FromResult(null); + public ValueTask SetAsync(string key, string value, CancellationToken cancellationToken = default) => throw new NotSupportedException(); + public ValueTask DeleteAsync(string key, CancellationToken cancellationToken = default) => throw new NotSupportedException(); + public ValueTask> ListAsync(string prefix, CancellationToken cancellationToken = default) => ValueTask.FromResult>([]); + public ValueTask GetForTenantAsync(string tenantId, string key, CancellationToken cancellationToken = default) => ValueTask.FromResult(null); + + public ValueTask SetForTenantAsync(string tenantId, string key, string value, CancellationToken cancellationToken = default) + { + Writes.Add((tenantId, key, value, RowLevelSecurity.InPlatformScope)); + return ValueTask.CompletedTask; + } + + public ValueTask DeleteForTenantAsync(string tenantId, string key, CancellationToken cancellationToken = default) => ValueTask.CompletedTask; + } + + private sealed class NullSink : ISettingsAuditSink + { + public ValueTask RecordAsync(SettingChange change, CancellationToken cancellationToken = default) => ValueTask.CompletedTask; + } + + [Fact] + public async Task SetAsync_PlatformScope_FromAFlowThatIsNotAPlatformOperation_IsRefusedAndNeverSelfElevates() + { + // A tenant admin's circuit / request: bound to its own tenant, NOT a platform operation. + RowLevelSecurity.InPlatformScope.ShouldBeFalse("the test flow must start outside any platform scope"); + var store = new RecordingStore(); + var svc = new SettingsService( + new SettingsCatalog([new TestSettings()]), store, new ConfigurationBuilder().Build(), new NullSink(), + NullLogger.Instance); + + var result = await svc.SetAsync(PlatformSwitch, "true", SettingScope.Platform, tenantId: null, actor: "tenant-admin"); + + // The service has no proof the caller is a platform admin, so it must not open the platform rows for them. + store.Writes.ShouldBeEmpty( + "SettingsService entered RowLevelSecurity.PlatformScope on its own and wrote the __platform row — " + + $"the store saw InPlatformScope={store.Writes.Count switch { 0 => "n/a", _ => store.Writes[0].InPlatformScope.ToString() }}"); + result.Ok.ShouldBeFalse("a platform-scope write from an unproven caller must be refused"); + result.Error.ShouldNotBeNull().ShouldContain("platform admin"); + } + + [Fact] + public async Task SetAsync_PlatformScope_InsideAPlatformOperationEnteredByTheCaller_WritesThePlatformRow() + { + var store = new RecordingStore(); + var svc = new SettingsService( + new SettingsCatalog([new TestSettings()]), store, new ConfigurationBuilder().Build(), new NullSink(), + NullLogger.Instance); + + // The surface that verified the platform-admin role enters the scope; the service only follows it. + SettingWriteResult result; + using (RowLevelSecurity.PlatformScope("test")) + { + result = await svc.SetAsync(PlatformSwitch, "true", SettingScope.Platform, tenantId: null, actor: "platform-admin"); + } + + result.Ok.ShouldBeTrue(result.Error); + store.Writes.ShouldHaveSingleItem().ShouldBe((SettingsService.PlatformTenant, SettingsService.PlatformPrefix + PlatformSwitch, "true", true)); + RowLevelSecurity.InPlatformScope.ShouldBeFalse("the scope the caller entered must not leak past its dispose"); + } + + [Fact] + public async Task SetAsync_TenantScope_NeedsNoPlatformOperation() + { + var store = new RecordingStore(); + var svc = new SettingsService( + new SettingsCatalog([new TestSettings(), new TenantSettings()]), store, new ConfigurationBuilder().Build(), new NullSink(), + NullLogger.Instance); + + (await svc.SetAsync(TenantKnob, "7", SettingScope.Tenant, "acme", "tenant-admin")).Ok.ShouldBeTrue(); + + store.Writes.ShouldHaveSingleItem().ShouldBe(("acme", TenantKnob, "7", false)); + } + + private const string TenantKnob = "Test:TenantKnob"; + + private sealed class TenantSettings : ISettingsContributor + { + public IEnumerable Describe() + { + yield return new(TenantKnob, SettingKind.WholeNumber, "T", "Tenant knob") { DefaultValue = "1", Min = 0, Max = 100 }; + } + } +} diff --git a/tests/AgentOs.Tests/Configuration/SettingsServiceTests.cs b/tests/AgentOs.Tests/Configuration/SettingsServiceTests.cs index 1701ed95..e8512bdb 100644 --- a/tests/AgentOs.Tests/Configuration/SettingsServiceTests.cs +++ b/tests/AgentOs.Tests/Configuration/SettingsServiceTests.cs @@ -12,6 +12,7 @@ using AgentOs.Modules.AppConfig.Settings; using AgentOs.Modules.Llm; using AgentOs.Modules.Pipeline.Agents; +using AgentOs.SharedKernel.Persistence; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging.Abstractions; using Shouldly; @@ -72,6 +73,7 @@ public async Task Get_Precedence_SeedThenPlatformThenTenantThenLock() var (svc, _) = Build(config); (await svc.GetAsync(Model, "acme")).Source.ShouldBe(SettingSource.Seed); + using var platform = RowLevelSecurity.PlatformScope("test"); (await svc.SetAsync(Model, "platform-model", SettingScope.Platform, null, "root")).Ok.ShouldBeTrue(); (await svc.GetAsync(Model, "acme")).ShouldBe(new SettingValue("platform-model", SettingSource.Platform)); @@ -87,6 +89,7 @@ public async Task Get_Precedence_SeedThenPlatformThenTenantThenLock() public async Task Set_LockedKey_IsRefused() { var (svc, _) = Build(new Dictionary { [$"{SettingsService.LocksSection}:{Model}"] = "x" }); + using var platform = RowLevelSecurity.PlatformScope("test"); var r = await svc.SetAsync(Model, "claude-opus-5-5", SettingScope.Platform, null, "root"); r.Ok.ShouldBeFalse(); r.Error!.ShouldContain("locked"); @@ -97,6 +100,7 @@ public async Task Set_PlatformOnlyKey_RefusesTenantWrite() { var (svc, _) = Build(); (await svc.SetAsync(PlatformOnly, "true", SettingScope.Tenant, "acme", "admin")).Ok.ShouldBeFalse(); + using var platform = RowLevelSecurity.PlatformScope("test"); (await svc.SetAsync(PlatformOnly, "true", SettingScope.Platform, null, "root")).Ok.ShouldBeTrue(); } @@ -167,6 +171,7 @@ public async Task Get_UndeclaredKey_Throws() public async Task Set_UndeclaredKey_IsRejected() { var (svc, _) = Build(); + using var platform = RowLevelSecurity.PlatformScope("test"); (await svc.SetAsync("Nope:Key", "x", SettingScope.Platform, null, "root")).Ok.ShouldBeFalse(); } diff --git a/tests/AgentOs.Tests/Identity/KeycloakAdminClientTests.cs b/tests/AgentOs.Tests/Identity/KeycloakAdminClientTests.cs index a9de1ed7..15fcb20a 100644 --- a/tests/AgentOs.Tests/Identity/KeycloakAdminClientTests.cs +++ b/tests/AgentOs.Tests/Identity/KeycloakAdminClientTests.cs @@ -150,6 +150,60 @@ public async Task CreateUserAsync_ClientSecretSet_UsesClientCredentialsAgainstAp handler.Calls[1].Headers.Authorization.ShouldBe(new AuthenticationHeaderValue("Bearer", "sa-tok")); } + // The account exists after POST /users. A later step failing must not leave it behind — callers read "threw" as + // "no account" (an invitation is given back on it, GAP-11). + [Fact] + public async Task CreateUserAsync_RoleGrantFails_DeletesTheHalfCreatedUserBeforeThrowing() + { + using var handler = new StubHandler(); + handler.Enqueue(req => req.Method == HttpMethod.Post && req.RequestUri!.AbsolutePath.EndsWith("/protocol/openid-connect/token"), + new HttpResponseMessage(HttpStatusCode.OK) { Content = JsonContent("{\"access_token\":\"tok-1\",\"expires_in\":300}") }); + handler.Enqueue(req => req.Method == HttpMethod.Post && req.RequestUri!.AbsolutePath.EndsWith("/admin/realms/agentic/users"), + new HttpResponseMessage(HttpStatusCode.Created) { Headers = { Location = new Uri("http://kc/admin/realms/agentic/users/u-half") } }); + handler.Enqueue(req => req.Method == HttpMethod.Get && req.RequestUri!.AbsolutePath.EndsWith("/admin/realms/agentic/roles/admin"), + new HttpResponseMessage(HttpStatusCode.OK) { Content = JsonContent("{\"id\":\"role-admin\",\"name\":\"admin\"}") }); + handler.Enqueue(req => req.Method == HttpMethod.Post && req.RequestUri!.AbsolutePath.EndsWith("/users/u-half/role-mappings/realm"), + new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("boom") }); + handler.Enqueue(req => req.Method == HttpMethod.Delete && req.RequestUri!.AbsolutePath.EndsWith("/admin/realms/agentic/users/u-half"), + new HttpResponseMessage(HttpStatusCode.NoContent)); + + using var http = new HttpClient(handler) { BaseAddress = new Uri("http://kc/") }; + using var client = new KeycloakAdminClient(http, Options.Create(new KeycloakAdminOptions { BaseUrl = "http://kc", Realm = "agentic" }), + NullLogger.Instance); + + var ex = await Should.ThrowAsync(() => client.CreateUserAsync("alice", "alice@example.com", "acme", AdminRole, sendVerifyEmail: true)); + + ex.ShouldNotBeOfType(); + handler.Calls.Count.ShouldBe(5); + handler.Calls[4].Method.ShouldBe(HttpMethod.Delete); + } + + // When even the compensating delete fails, the caller must learn the account is still there. + [Fact] + public async Task CreateUserAsync_RoleGrantAndDeleteFail_ThrowsLeftBehindWithTheUserId() + { + using var handler = new StubHandler(); + handler.Enqueue(req => req.Method == HttpMethod.Post && req.RequestUri!.AbsolutePath.EndsWith("/protocol/openid-connect/token"), + new HttpResponseMessage(HttpStatusCode.OK) { Content = JsonContent("{\"access_token\":\"tok-1\",\"expires_in\":300}") }); + handler.Enqueue(req => req.Method == HttpMethod.Post && req.RequestUri!.AbsolutePath.EndsWith("/admin/realms/agentic/users"), + new HttpResponseMessage(HttpStatusCode.Created) { Headers = { Location = new Uri("http://kc/admin/realms/agentic/users/u-stuck") } }); + handler.Enqueue(req => req.Method == HttpMethod.Get && req.RequestUri!.AbsolutePath.EndsWith("/admin/realms/agentic/roles/admin"), + new HttpResponseMessage(HttpStatusCode.OK) { Content = JsonContent("{\"id\":\"role-admin\",\"name\":\"admin\"}") }); + handler.Enqueue(req => req.Method == HttpMethod.Post && req.RequestUri!.AbsolutePath.EndsWith("/users/u-stuck/role-mappings/realm"), + new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("boom") }); + handler.Enqueue(req => req.Method == HttpMethod.Delete && req.RequestUri!.AbsolutePath.EndsWith("/admin/realms/agentic/users/u-stuck"), + new HttpResponseMessage(HttpStatusCode.ServiceUnavailable) { Content = new StringContent("down") }); + + using var http = new HttpClient(handler) { BaseAddress = new Uri("http://kc/") }; + using var client = new KeycloakAdminClient(http, Options.Create(new KeycloakAdminOptions { BaseUrl = "http://kc", Realm = "agentic" }), + NullLogger.Instance); + + var ex = await Should.ThrowAsync(() => client.CreateUserAsync("alice", "alice@example.com", "acme", AdminRole, sendVerifyEmail: true)); + + ex.UserId.ShouldBe("u-stuck"); + ex.InnerException.ShouldBeOfType().Message.ShouldContain("grant-roles failed (500)"); + } + private static StringContent JsonContent(string body) { var c = new StringContent(body, System.Text.Encoding.UTF8); diff --git a/tests/AgentOs.Tests/Identity/TenantSignupServiceTests.cs b/tests/AgentOs.Tests/Identity/TenantSignupServiceTests.cs index ee1b9310..e475bb71 100644 --- a/tests/AgentOs.Tests/Identity/TenantSignupServiceTests.cs +++ b/tests/AgentOs.Tests/Identity/TenantSignupServiceTests.cs @@ -1,16 +1,23 @@ // Exercises TenantSignupService end-to-end against in-memory fakes. Covers all three modes // (slug / auto-create / invite), the Keycloak-first saga rollback when the registry write -// throws, and the invitation token's expiry + tamper-detection. +// throws, and — GAP-11 — invitations as single-use, email-bound, revocable rows: a second use, the wrong address, a +// revoked or an expired invitation is refused (and audited) before any account is created; a failed account creation +// gives the invitation back; a link minted before invitations were rows no longer opens. using System; +using System.Collections.Concurrent; using System.Collections.Generic; +using System.Linq; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; using AgentOs.Modules.Tenants; +using AgentOs.Modules.Tenants.Invitations; using AgentOs.Modules.Tenants.Keycloak; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; +using NSubstitute.ExceptionExtensions; using Shouldly; using Xunit; @@ -19,6 +26,7 @@ namespace AgentOs.Tests.Identity; public sealed class TenantSignupServiceTests { private const string ValidPassword = "Sup3rSecure!Pass"; + private static readonly AuditActor Admin = new("admin-1", "10.0.0.1", "tests"); [Fact] public async Task SignupAsync_SlugMode_CreatesTenantAndKeycloakUser() @@ -70,21 +78,10 @@ public async Task SignupAsync_AutoCreateMode_GeneratesUniqueSlugFromUsername() [Fact] public async Task SignupAsync_InviteMode_JoinsExistingTenantWithoutCreatingRow() { - var repo = new FakeRepo(); - await repo.AddAsync(new TenantRecord("acme", "Acme Corp", DateTimeOffset.UtcNow)); - var kc = Substitute.For(); - kc.CreateUserAsync(default!, default!, default!, default!, default, default, default) - .ReturnsForAnyArgs(Task.FromResult("kc-user-3")); - var svc = NewService(repo, kc); - var invite = svc.CreateInvitation("acme", "member", "bob@acme.test", TimeSpan.FromHours(1)); + var (svc, kc, repo, _, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); - var outcome = await svc.SignupAsync(new TenantSignupRequest( - Username: "bob", - Password: ValidPassword, - Email: "bob@acme.test", - TenantId: null, - TenantName: null, - InviteToken: invite.Token)); + var outcome = await svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token)); outcome.Mode.ShouldBe(SignupMode.Invite); outcome.TenantId.ShouldBe("acme"); @@ -130,32 +127,269 @@ await Should.ThrowAsync(async () => await svc.SignupA await kc.DidNotReceiveWithAnyArgs().CreateUserAsync(default!, default!, default!, default!, default); } + // ── GAP-11: invitations are single-use, bound to an address, revocable, and expire ── + [Fact] - public void PreviewInvitation_ValidToken_ReturnsPayload() + public async Task PreviewInvitationAsync_ValidToken_ReturnsThePendingRow() { - var svc = NewService(new FakeRepo(), Substitute.For()); - var minted = svc.CreateInvitation("acme", "admin", "bob@x.test", TimeSpan.FromMinutes(5)); + var (svc, _, _, _, _) = Invitable(); + var minted = await svc.CreateInvitationAsync("acme", "admin", "Bob@X.test", TimeSpan.FromMinutes(5), Admin); + + var preview = await svc.PreviewInvitationAsync(minted.Token); - var preview = svc.PreviewInvitation(minted.Token); preview.ShouldNotBeNull(); - preview!.TenantId.ShouldBe("acme"); + preview.Id.ShouldBe(minted.Id); + preview.TenantId.ShouldBe("acme"); preview.Role.ShouldBe("admin"); - preview.Email.ShouldBe("bob@x.test"); + preview.Email.ShouldBe("Bob@X.test"); + preview.Status.ShouldBe(InvitationStatus.Pending); + preview.CanAccept.ShouldBeTrue(); + } + + [Fact] + public async Task PreviewInvitationAsync_TamperedOrMissingToken_ReturnsNull() + { + var (svc, _, _, _, _) = Invitable(); + (await svc.PreviewInvitationAsync(null)).ShouldBeNull(); + (await svc.PreviewInvitationAsync("")).ShouldBeNull(); + (await svc.PreviewInvitationAsync("not-a-token")).ShouldBeNull(); + } + + // A link minted before invitations were rows (stateless, reusable, purpose v1) no longer opens: honouring it would + // keep the old gap open until it expired. + [Fact] + public async Task PreviewAndSignup_PreUpgradeStatelessToken_AreRefused() + { + var dp = new EphemeralDataProtectionProvider(); + var (svc, kc, _, _, _) = Invitable(dp); + var legacy = dp.CreateProtector("AgentOs.Modules.Tenants.Invitation.v1").ToTimeLimitedDataProtector().Protect( + JsonSerializer.Serialize(new LegacyPayload("acme", "admin", null, DateTimeOffset.UtcNow), WebJson), + TimeSpan.FromHours(1)); + + (await svc.PreviewInvitationAsync(legacy)).ShouldBeNull(); + var error = await Should.ThrowAsync(() => svc.SignupAsync(Join("mallory", "m@x.test", legacy))); + error.Message.ShouldBe(InvitationRules.Describe(InvitationAcceptance.NotFound)); + await kc.DidNotReceiveWithAnyArgs().CreateUserAsync(default!, default!, default!, default!, default); + } + + // Reuse: the second sign-up with the same link is refused before Keycloak is touched, and the attempt is audited. + [Fact] + public async Task SignupAsync_InvitationUsedTwice_SecondIsRefused() + { + var (svc, kc, _, audit, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + + await svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token)); + var error = await Should.ThrowAsync(() => svc.SignupAsync(Join("bob2", "bob@acme.test", invite.Token))); + + error.Message.ShouldBe(InvitationRules.Describe(InvitationAcceptance.AlreadyUsed)); + await kc.Received(1).CreateUserAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>(), + Arg.Any(), Arg.Any(), Arg.Any()); + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Accepted); + audit.Actions.ShouldContain(AuditActions.InvitationAccepted); + audit.Entries.ShouldContain(e => e.Action == AuditActions.InvitationRefused && e.Target!.Contains("reason=AlreadyUsed", StringComparison.Ordinal)); + } + + // Two people racing with one link: exactly one gets an account (the claim is one conditional write). + [Fact] + public async Task SignupAsync_ConcurrentUsesOfOneInvitation_OnlyOneSucceeds() + { + var (svc, kc, _, _, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + + var attempts = await Task.WhenAll(Enumerable.Range(0, 8).Select(i => Task.Run(async () => + { + try + { + await svc.SignupAsync(Join($"bob{i}", "bob@acme.test", invite.Token)); + return true; + } + catch (InvalidOperationException) + { + return false; + } + }))); + + attempts.Count(ok => ok).ShouldBe(1); + await kc.Received(1).CreateUserAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>(), + Arg.Any(), Arg.Any(), Arg.Any()); + } + + // Bound to the address: another address (or none) is refused; the invitee can still use it afterwards. + [Theory] + [InlineData("eve@evil.test", InvitationAcceptance.EmailMismatch)] + [InlineData("bob@acme.test.evil", InvitationAcceptance.EmailMismatch)] + [InlineData(null, InvitationAcceptance.EmailRequired)] + public async Task SignupAsync_WrongOrMissingEmail_IsRefused_AndTheInvitationStaysUsable(string? email, InvitationAcceptance expected) + { + var (svc, kc, _, audit, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + + var error = await Should.ThrowAsync(() => svc.SignupAsync(Join("eve", email, invite.Token))); + + error.Message.ShouldBe(InvitationRules.Describe(expected)); + await kc.DidNotReceiveWithAnyArgs().CreateUserAsync(default!, default!, default!, default!, default); + audit.Entries.ShouldContain(e => e.Action == AuditActions.InvitationRefused && e.TenantId == "acme"); + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Pending); + + // The right person, typing the address in another case and with stray spaces, still gets in. + (await svc.SignupAsync(Join("bob", " BOB@Acme.Test ", invite.Token))).Mode.ShouldBe(SignupMode.Invite); + } + + [Fact] + public async Task RevokeInvitationAsync_Pending_LinkStopsWorking_AndIsAudited() + { + var (svc, kc, _, audit, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + + (await svc.RevokeInvitationAsync("acme", invite.Id, Admin)).ShouldBeTrue(); + + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Revoked); + var error = await Should.ThrowAsync(() => svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token))); + error.Message.ShouldBe(InvitationRules.Describe(InvitationAcceptance.Revoked)); + await kc.DidNotReceiveWithAnyArgs().CreateUserAsync(default!, default!, default!, default!, default); + audit.Entries.ShouldContain(e => e.Action == AuditActions.InvitationRevoked && e.UserId == "admin-1" && e.IpAddress == "10.0.0.1"); + (await svc.RevokeInvitationAsync("acme", invite.Id, Admin)).ShouldBeFalse(); // already revoked + } + + [Fact] + public async Task RevokeInvitationAsync_UsedOrOtherTenant_ReturnsFalse() + { + var (svc, _, _, _, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + + (await svc.RevokeInvitationAsync("other", invite.Id, Admin)).ShouldBeFalse(); // not this tenant's invitation + await svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token)); + (await svc.RevokeInvitationAsync("acme", invite.Id, Admin)).ShouldBeFalse(); // used: nothing to revoke + (await svc.ListInvitationsAsync("acme")).Single().Status.ShouldBe(InvitationStatus.Accepted); + } + + [Fact] + public async Task SignupAsync_ExpiredInvitation_IsRefused() + { + var (svc, kc, _, _, clock) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(2), Admin); + + clock.Advance(TimeSpan.FromHours(2)); + + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Expired); + var error = await Should.ThrowAsync(() => svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token))); + error.Message.ShouldBe(InvitationRules.Describe(InvitationAcceptance.Expired)); + await kc.DidNotReceiveWithAnyArgs().CreateUserAsync(default!, default!, default!, default!, default); + } + + // Keycloak refused the account (a taken username): the invitation goes back to pending so the invitee can retry. + [Fact] + public async Task SignupAsync_AccountCreationFails_ReleasesTheInvitation() + { + var (svc, kc, _, audit, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + kc.CreateUserAsync("taken", Arg.Any(), Arg.Any(), Arg.Any>(), Arg.Any(), Arg.Any(), Arg.Any()) + .ThrowsAsync(new InvalidOperationException("Keycloak create-user failed (409): User exists with same username")); + + await Should.ThrowAsync(() => svc.SignupAsync(Join("taken", "bob@acme.test", invite.Token))); + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Pending); + + (await svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token))).Mode.ShouldBe(SignupMode.Invite); + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Accepted); + audit.Entries.ShouldContain(e => e.Action == AuditActions.InvitationRefused && e.Target!.Contains("reason=account_not_created", StringComparison.Ordinal)); + } + + // The claim is the point of no return: the invitee dropping the connection (RequestAborted) must not reach Keycloak + // half-way, and a cancellation that still surfaces (an HttpClient timeout) may mean the account exists — the + // invitation stays used rather than offering a second account on the same link. + [Fact] + public async Task SignupAsync_AccountCreationCancelled_RunsWithoutTheRequestToken_AndKeepsTheInvitationUsed() + { + var (svc, kc, _, audit, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + using var aborted = new CancellationTokenSource(); + await aborted.CancelAsync(); + kc.CreateUserAsync(default!, default!, default!, default!, default, default, default) + .ThrowsAsyncForAnyArgs(new OperationCanceledException("The request was aborted after Keycloak created the user.")); + + await Should.ThrowAsync(() => svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token), aborted.Token)); + + await kc.Received(1).CreateUserAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>(), + Arg.Any(), Arg.Any(), CancellationToken.None); + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Accepted); + audit.Entries.ShouldContain(e => e.Action == AuditActions.InvitationRefused && e.Target!.Contains("reason=account_unrecorded", StringComparison.Ordinal)); + await Should.ThrowAsync(() => svc.SignupAsync(Join("bob2", "bob@acme.test", invite.Token))); + } + + // Keycloak created the account, the rest failed and the compensating delete failed too: the account is there, so + // the invitation stays used and the audit row names the account for the operator. + [Fact] + public async Task SignupAsync_AccountLeftBehindInKeycloak_KeepsTheInvitationUsed_AndAuditsTheAccount() + { + var (svc, kc, _, audit, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), Admin); + kc.CreateUserAsync(default!, default!, default!, default!, default, default, default) + .ThrowsAsyncForAnyArgs(KeycloakUserLeftBehindException.For("u-stuck", new InvalidOperationException("Keycloak grant-roles failed (500): boom"))); + + await Should.ThrowAsync(() => svc.SignupAsync(Join("bob", "bob@acme.test", invite.Token))); + + (await svc.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Accepted); + audit.Entries.ShouldContain(e => e.Action == AuditActions.InvitationRefused && e.UserId == "u-stuck" + && e.Target!.Contains("reason=account_unrecorded", StringComparison.Ordinal)); + await kc.DidNotReceiveWithAnyArgs().DeleteUserAsync(default!); + } + + [Theory] + [InlineData("owner", "bob@acme.test", 1)] + [InlineData("member", "", 1)] + [InlineData("member", "not-an-address", 1)] + [InlineData("member", "bob@acme.test", 0)] + [InlineData("member", "bob@acme.test", 169)] + public async Task CreateInvitationAsync_BadRoleAddressOrLifetime_Throws(string role, string email, int hours) + { + var (svc, _, _, _, _) = Invitable(); + await Should.ThrowAsync(() => + svc.CreateInvitationAsync("acme", role, email, TimeSpan.FromHours(hours), Admin)); + (await svc.ListInvitationsAsync("acme")).ShouldBeEmpty(); } [Fact] - public void PreviewInvitation_TamperedOrMissingToken_ReturnsNull() + public async Task CreateInvitationAsync_IsListedAndAudited() { - var svc = NewService(new FakeRepo(), Substitute.For()); - svc.PreviewInvitation(null).ShouldBeNull(); - svc.PreviewInvitation("").ShouldBeNull(); - svc.PreviewInvitation("not-a-token").ShouldBeNull(); + var (svc, _, _, audit, _) = Invitable(); + var invite = await svc.CreateInvitationAsync("acme", "admin", " carol@acme.test ", TimeSpan.FromHours(3), Admin); + + var listed = (await svc.ListInvitationsAsync("acme")).ShouldHaveSingleItem(); + listed.Id.ShouldBe(invite.Id); + listed.Email.ShouldBe("carol@acme.test"); + listed.Role.ShouldBe("admin"); + listed.CreatedBy.ShouldBe("admin-1"); + listed.Status.ShouldBe(InvitationStatus.Pending); + (await svc.ListInvitationsAsync("other")).ShouldBeEmpty(); + audit.Entries.ShouldContain(e => e.Action == AuditActions.InvitationMinted && e.TenantId == "acme" + && e.Target!.Contains("email=carol@acme.test", StringComparison.Ordinal) && e.UserAgent == "tests"); } + // ── helpers ── + + private static TenantSignupRequest Join(string username, string? email, string token) + => new(username, ValidPassword, email, TenantId: null, TenantName: null, InviteToken: token); + // These tests exercise the open-signup modes, so the platform setting is on (GAP-01: it is off by default outside // Development, and the service fails closed without a settings registry — see OpenSignupAndAzureEndpointTests). private static TenantSignupService NewService(ITenantsRepository repo, IKeycloakAdminClient kc) => - new(repo, kc, new NullAuditLog(), new EphemeralDataProtectionProvider(), NullLogger.Instance, new OpenSignupSettings()); + new(repo, kc, new CapturingAuditLog(), new EphemeralDataProtectionProvider(), NullLogger.Instance, new OpenSignupSettings()); + + private static (TenantSignupService Service, IKeycloakAdminClient Kc, FakeRepo Repo, CapturingAuditLog Audit, ManualClock Clock) Invitable( + IDataProtectionProvider? dp = null) + { + var repo = new FakeRepo(); + repo.Records["acme"] = new TenantRecord("acme", "Acme", DateTimeOffset.UtcNow); + var kc = Substitute.For(); + kc.CreateUserAsync(default!, default!, default!, default!, default, default, default) + .ReturnsForAnyArgs(ci => Task.FromResult("kc-" + ci.ArgAt(0))); + var audit = new CapturingAuditLog(); + var clock = new ManualClock(new DateTimeOffset(2026, 11, 3, 9, 0, 0, TimeSpan.Zero)); + var svc = new TenantSignupService(repo, kc, audit, dp ?? new EphemeralDataProtectionProvider(), NullLogger.Instance, + new OpenSignupSettings(), new InMemoryInvitationStore(), clock); + return (svc, kc, repo, audit, clock); + } // ISettings is ValueTask-returning — a hand-written fake, never substituted. private sealed class OpenSignupSettings : AgentOs.Domain.Configuration.ISettings @@ -173,19 +407,44 @@ private sealed class OpenSignupSettings : AgentOs.Domain.Configuration.ISettings ValueTask.FromResult(AgentOs.Domain.Configuration.SettingWriteResult.Success); } - private sealed class NullAuditLog : IAuditLog + private sealed class CapturingAuditLog : IAuditLog { - public Task WriteAsync(AuditEntry entry, CancellationToken ct = default) => Task.CompletedTask; + private readonly ConcurrentQueue _entries = new(); + + public IReadOnlyList Entries => [.. _entries]; + + public IReadOnlyList Actions => [.. _entries.Select(e => e.Action)]; + + public Task WriteAsync(AuditEntry entry, CancellationToken ct = default) + { + _entries.Enqueue(entry); + return Task.CompletedTask; + } + public Task> ListAsync(string tenantId, int max = 100, CancellationToken ct = default) => - Task.FromResult>([]); + Task.FromResult>([.. _entries.Where(e => e.TenantId == tenantId)]); + } + + private sealed class ManualClock(DateTimeOffset start) : TimeProvider + { + private DateTimeOffset _now = start; + + public void Advance(TimeSpan by) => _now += by; + + public override DateTimeOffset GetUtcNow() => _now; } + private static readonly JsonSerializerOptions WebJson = new(JsonSerializerDefaults.Web); + + // The pre-upgrade (v1) invitation payload: stateless, no id. + private sealed record LegacyPayload(string TenantId, string Role, string? Email, DateTimeOffset IssuedAtUtc); + private sealed class FakeRepo : ITenantsRepository { public Dictionary Records { get; } = new(StringComparer.Ordinal); public Task> ListAsync(CancellationToken ct = default) => - Task.FromResult>(new List(Records.Values)); + Task.FromResult>([.. Records.Values]); public Task GetAsync(string id, CancellationToken ct = default) => Task.FromResult(Records.TryGetValue(id, out var r) ? r : null); diff --git a/tests/AgentOs.Tests/Integration/BuildVerifierSettingsTests.cs b/tests/AgentOs.Tests/Integration/BuildVerifierSettingsTests.cs index 9c35ba27..307b104d 100644 --- a/tests/AgentOs.Tests/Integration/BuildVerifierSettingsTests.cs +++ b/tests/AgentOs.Tests/Integration/BuildVerifierSettingsTests.cs @@ -10,6 +10,7 @@ using AgentOs.Modules.AppConfig.Settings; using AgentOs.Modules.Integration; using AgentOs.Modules.Integration.Sandbox; +using AgentOs.SharedKernel.Persistence; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging.Abstractions; using NSubstitute; @@ -54,6 +55,7 @@ public async Task OptionsSource_ReflectsPlatformChangesLive() var source = new SettingsBuildVerifierOptionsSource(settings, Defaults); (await source.CurrentAsync()).Enabled.ShouldBeFalse(); + using var platform = RowLevelSecurity.PlatformScope("test"); await settings.SetAsync(BuildVerifierSettings.Enabled, "true", SettingScope.Platform, null, "root"); await settings.SetAsync(BuildVerifierSettings.Sandbox, "Container", SettingScope.Platform, null, "root"); await settings.SetAsync(BuildVerifierSettings.MemoryLimit, "512m", SettingScope.Platform, null, "root"); @@ -69,6 +71,7 @@ public async Task OptionsSource_ReflectsPlatformChangesLive() public async Task Settings_RejectFlagLikeImage_AndTenantWrites() { var settings = Settings(); + using var platform = RowLevelSecurity.PlatformScope("test"); (await settings.SetAsync(BuildVerifierSettings.ContainerImage, "--privileged", SettingScope.Platform, null, "root")).Ok.ShouldBeFalse(); (await settings.SetAsync(BuildVerifierSettings.Enabled, "true", SettingScope.Tenant, "acme", "admin")).Ok.ShouldBeFalse(); } @@ -77,6 +80,7 @@ public async Task Settings_RejectFlagLikeImage_AndTenantWrites() public async Task ProductionLock_PinsContainerSandbox() { var settings = Settings(new Dictionary { ["Settings:Locks:Integration:BuildVerifier:Sandbox"] = "Container" }); + using var platform = RowLevelSecurity.PlatformScope("test"); (await settings.SetAsync(BuildVerifierSettings.Sandbox, "InProcess", SettingScope.Platform, null, "root")).Ok.ShouldBeFalse(); (await new SettingsBuildVerifierOptionsSource(settings, Defaults).CurrentAsync()).Sandbox.ShouldBe(BuildSandboxMode.Container); } @@ -96,6 +100,7 @@ public async Task Verifier_UsesCurrentGateAndRunnerPerBuild() (await verifier.VerifyFilesAsync(files, CancellationToken.None)).Success.ShouldBeFalse(); // gate off modesSeen.ShouldBeEmpty(); + using var platform = RowLevelSecurity.PlatformScope("test"); await settings.SetAsync(BuildVerifierSettings.Enabled, "true", SettingScope.Platform, null, "root"); await settings.SetAsync(BuildVerifierSettings.Sandbox, "Container", SettingScope.Platform, null, "root"); await verifier.VerifyFilesAsync(files, CancellationToken.None); diff --git a/tests/AgentOs.Tests/Llm/LlmClientFactoryLiveSettingsTests.cs b/tests/AgentOs.Tests/Llm/LlmClientFactoryLiveSettingsTests.cs index 172700ae..477fc952 100644 --- a/tests/AgentOs.Tests/Llm/LlmClientFactoryLiveSettingsTests.cs +++ b/tests/AgentOs.Tests/Llm/LlmClientFactoryLiveSettingsTests.cs @@ -10,6 +10,7 @@ using AgentOs.Modules.Llm; using AgentOs.SharedKernel.Identity; using AgentOs.SharedKernel.Modularity; +using AgentOs.SharedKernel.Persistence; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using NSubstitute; @@ -85,6 +86,7 @@ public async Task Create_PlatformOfflineFallback_TogglesTheCannedSafetyNetLive() using var _ = AmbientIdentity.Push("acme", "u"); IsChain(factory.Create("Claude")).ShouldBeFalse(); + using var platform = RowLevelSecurity.PlatformScope("test"); (await settings.SetAsync(LlmSettings.OfflineFallback, "true", SettingScope.Platform, null, "root")).Ok.ShouldBeTrue(); IsChain(factory.Create("Claude")).ShouldBeTrue(); // Offline appended to the failover chain diff --git a/tests/AgentOs.Tests/Persistence/RowLevelSecurityDocsTests.cs b/tests/AgentOs.Tests/Persistence/RowLevelSecurityDocsTests.cs new file mode 100644 index 00000000..2b27a2e8 --- /dev/null +++ b/tests/AgentOs.Tests/Persistence/RowLevelSecurityDocsTests.cs @@ -0,0 +1,92 @@ +// ADR-0012 promises "RowLevelSecurity.Tables is the single list" — and then its own copy drifted (sessions.approvals was +// added in code, the ADR still said 19). The ADR's table list, and every table count the security docs quote, are read +// back here and compared with the code, so the next table added to RowLevelSecurity.Tables fails the build until the +// docs say so too. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using AgentOs.SharedKernel.Persistence; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Persistence; + +public sealed partial class RowLevelSecurityDocsTests +{ + private static string Code(string schema, string table) => $"{schema}.{table}"; + + [Fact] + public void Adr0012_TableList_IsExactlyRowLevelSecurityTables() + { + var adr = File.ReadAllText(Path.Join(RepoRoot(), "docs", "adr", "0012-postgres-row-level-security.md")); + var section = TablesSection().Match(adr); + section.Success.ShouldBeTrue("ADR-0012 must keep its '**Tables** — `RowLevelSecurity.Tables` is the single list (N):' section"); + + int.Parse(section.Groups["count"].Value, System.Globalization.CultureInfo.InvariantCulture) + .ShouldBe(RowLevelSecurity.Tables.Count, "ADR-0012's table count"); + ListedTables(section.Groups["body"].Value) + .ShouldBe(RowLevelSecurity.Tables.Select(t => Code(t.Schema, t.Table)), ignoreOrder: true, + customMessage: "ADR-0012's table list must match RowLevelSecurity.Tables"); + } + + public static TheoryData SecurityDocs => ["data-flow.md", "hardening.md", "threat-model.md"]; + + // "RLS on N tables", "the N tables in/of `RowLevelSecurity.Tables`", "(N tables, the exemption …)". + [Theory] + [MemberData(nameof(SecurityDocs))] + public void SecurityDocs_QuoteTheCurrentTableCount(string doc) + { + var text = File.ReadAllText(Path.Join(RepoRoot(), "docs", "security", doc)); + + var counts = TableCount().Matches(text).Select(m => int.Parse(m.Groups["n"].Value, System.Globalization.CultureInfo.InvariantCulture)).ToList(); + + counts.ShouldNotBeEmpty($"{doc} no longer quotes the RLS table count — drop this case if that is on purpose"); + counts.ShouldAllBe(n => n == RowLevelSecurity.Tables.Count, $"{doc} quotes a stale RLS table count"); + } + + // Each bullet names tables as `schema.table` or, after the first one on its line, a bare `table` of that schema. + private static List ListedTables(string body) + { + var tables = new List(); + foreach (var line in body.Split('\n').Where(l => l.TrimStart().StartsWith('-'))) + { + string? schema = null; + foreach (Match name in Quoted().Matches(line)) + { + var value = name.Groups[1].Value; + var dot = value.IndexOf('.', StringComparison.Ordinal); + if (dot > 0) + { + schema = value[..dot]; + tables.Add(value); + } + else if (schema is not null) + { + tables.Add(Code(schema, value)); + } + } + } + return tables; + } + + private static string RepoRoot() + { + for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) + { + if (File.Exists(Path.Join(dir.FullName, "AgentOs.slnx"))) { return dir.FullName; } + } + throw new DirectoryNotFoundException("AgentOs.slnx not found above the test output directory."); + } + + [GeneratedRegex(@"\*\*Tables\*\* — `RowLevelSecurity\.Tables` is the single list \((?\d+)\):\s*\n(?(?:[ \t]+-[^\n]*\n)+)")] + private static partial Regex TablesSection(); + + [GeneratedRegex(@"`([a-z_]+(?:\.[a-z_]+)?)`")] + private static partial Regex Quoted(); + + [GeneratedRegex(@"(?:RLS on (?\d+) tables|the (?\d+) tables (?:in|of) `RowLevelSecurity\.Tables`|\((?\d+) tables, the exemption)")] + private static partial Regex TableCount(); +} diff --git a/tests/AgentOs.Tests/Persistence/RowLevelSecurityEnabledPathsRealPostgresTests.cs b/tests/AgentOs.Tests/Persistence/RowLevelSecurityEnabledPathsRealPostgresTests.cs new file mode 100644 index 00000000..fa60a6b0 --- /dev/null +++ b/tests/AgentOs.Tests/Persistence/RowLevelSecurityEnabledPathsRealPostgresTests.cs @@ -0,0 +1,228 @@ +// REAL-POSTGRES — RLS stage 2 with the process-wide switch ON (RowLevelSecurity.State = Enabled), through the real +// module code: the paths that run with NO tenant of their own keep working, and none of them crosses tenants. +// • explicit platform scopes: the autopilot board scan, the scheduler's due-run claim, the runner handshake lookup and +// the revalidation sweep each see every tenant's rows; +// • tenant-explicit Dapper reads (workspaces, runners, orchestrations): a flow with no tenant is bound to the tenant it +// names; a flow bound to tenant B sees none of tenant A's rows even when it names A; +// • the evidence drain (a background loop, no tenant): a row is written under its own tenant; +// • config.app_config: a flow bound to B reads the shared platform rows but not A's secrets, and cannot write a +// platform row outside a platform scope; +// • the hub binds the rest of the handshake to the runner's tenant — without it, "no tenant" pairs nothing. +// The state is process-wide, so this class runs in a non-parallel collection and restores it. +// Gate: RUN_AGENTOS_E2E_REAL=true and AGENTOS_PG_CONN (a server the test may CREATE / DROP DATABASE on). Zero residue. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AgentOs.Domain.Tools; +using AgentOs.Modules.AppConfig; +using AgentOs.Modules.AppConfig.Persistence; +using AgentOs.Modules.Pipeline.Persistence; +using AgentOs.Modules.Pipeline.Persistence.Repositories; +using AgentOs.Modules.Pipeline.Scheduling; +using AgentOs.Modules.Sessions.Persistence; +using AgentOs.Modules.Sessions.Persistence.Repositories; +using AgentOs.Modules.Tools.Evidence; +using AgentOs.Modules.Tools.Persistence; +using AgentOs.Modules.Workspaces.Persistence; +using AgentOs.Modules.Workspaces.Persistence.Repositories; +using AgentOs.SharedKernel.Identity; +using AgentOs.SharedKernel.Persistence; +using Microsoft.AspNetCore.DataProtection; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Npgsql; +using Shouldly; +using Xunit; +using Db = AgentOs.Tests.Persistence.RowLevelSecurityStageTwoRealPostgresTests; + +namespace AgentOs.Tests.Persistence; + +/// Tests that flip the process-wide run alone, after the parallel ones. +[CollectionDefinition(Name, DisableParallelization = true)] +public sealed class RowLevelSecurityProcessStateGroup +{ + /// The collection name. + public const string Name = "rls-process-state"; +} + +[Collection(RowLevelSecurityProcessStateGroup.Name)] +public sealed class RowLevelSecurityEnabledPathsRealPostgresTests +{ + private const string A = "enabled-tenant-a"; + private const string B = "enabled-tenant-b"; + + [Fact] + public async Task EnabledHost_TenantlessPaths_KeepWorking_AndNeverCrossTenants() + { + var server = Db.ServerConnection ?? string.Empty; + if (server.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } + + var database = "agentos_rlson_" + Guid.NewGuid().ToString("N")[..12]; + var admin = new NpgsqlConnectionStringBuilder(server) { Database = "postgres", Pooling = false }.ConnectionString; + var cs = new NpgsqlConnectionStringBuilder(server) { Database = database }.ConnectionString; + await Db.ExecAsync(admin, $"CREATE DATABASE \"{database}\""); + var previousState = RowLevelSecurity.State; + var previousHttp = RowLevelSecurity.HttpTenant; + try + { + await Db.MigrateEveryModuleAsync(cs); + var (runnerA, runnerB) = await SeedAsync(cs); + DapperConfig.EnsureConfigured(); + var conn = new NpgsqlConnectionFactory(cs); + var none = new Db.FixedTenant(string.Empty); + + RowLevelSecurity.HttpTenant = null; + RowLevelSecurity.SetState(RowLevelSecurityState.Enabled); + + // 1. Cross-tenant by nature → explicit platform scopes inside the stores (the flow itself has no tenant). + await using (var db = new WorkspacesDbContext(Bound(cs, "workspaces"))) + { + (await new WorkspaceRepository(db, none, conn).ListAutopilotAsync()).Select(w => w.TenantId).ShouldBe([A, B], ignoreOrder: true); + } + await using (var db = new PipelineDbContext(Bound(cs, "pipeline"), none)) + { + (await new EfScheduledRunStore(db).ClaimDueAsync(DateTimeOffset.UtcNow, 10)).Select(d => d.TenantId).ShouldBe([A, B], ignoreOrder: true); + } + await using (var db = new SessionsDbContext(Bound(cs, "sessions"))) + { + var directory = new EfRunnerDirectory(db); + (await directory.FindForPairingAsync(runnerB))!.TenantId.ShouldBe(B); + (await directory.FindStatusesAsync([runnerA, runnerB])).Count.ShouldBe(2); + + // The handshake's writes: bound to the verified runner's tenant they land; with no tenant they touch nothing. + (await directory.MarkPairedAsync(runnerA)).ShouldBeFalse(); + using (RowLevelSecurity.TenantScope(A)) + { + (await directory.MarkPairedAsync(runnerA)).ShouldBeTrue(); + } + } + + // 2. Tenant-explicit Dapper reads: an unbound flow is bound to the named tenant; a B flow never sees A's rows. + await using (var db = new WorkspacesDbContext(Bound(cs, "workspaces"))) + await using (var sessions = new SessionsDbContext(Bound(cs, "sessions"))) + await using (var pipeline = new PipelineDbContext(Bound(cs, "pipeline"), none)) + { + var workspaces = new WorkspaceRepository(db, none, conn); + var runners = new RunnerRepository(sessions, none, conn); + var orchestrations = new OrchestrationRepository(pipeline, none, conn); + (await workspaces.ListForTenantAsync(A)).ShouldHaveSingleItem().TenantId.ShouldBe(A); + (await runners.ListForTenantAsync(A)).ShouldHaveSingleItem().TenantId.ShouldBe(A); + (await orchestrations.ListForTenantAsync(A)).ShouldHaveSingleItem(); + using (AmbientIdentity.Push(B, "u")) + { + (await workspaces.ListForTenantAsync(A)).ShouldBeEmpty(); + (await runners.ListForTenantAsync(A)).ShouldBeEmpty(); + (await orchestrations.ListForTenantAsync(A)).ShouldBeEmpty(); + (await workspaces.ListForTenantAsync(B)).ShouldHaveSingleItem(); + } + } + + // 3. The evidence drain writes from a loop with no tenant: each row lands under its own. + await using (var tools = Provider(services => services.AddDbContext(o => o + .UseNpgsql(cs, n => n.MigrationsHistoryTable("__EFMigrationsHistory", "tools")) + .AddInterceptors(RowLevelSecurityInterceptor.Instance)))) + { + var log = new EfToolInvocationLog(tools, NullLogger.Instance); + var at = DateTimeOffset.UtcNow; + await log.AppendAsync(new ToolInvocationEvidence("call-1", "build_verifier", A, null, "{}", "ok", false, at, at)); + (await CountAsSuperuserAsync(cs, "tools.tool_invocations", A)).ShouldBe(1L); + } + + // 4. app_config: shared platform rows, bound tenant rows. + await using (var settings = Provider(services => + { + services.AddDbContext(o => o + .UseNpgsql(cs, n => n.MigrationsHistoryTable("__EFMigrationsHistory", "config")) + .AddInterceptors(RowLevelSecurityInterceptor.Instance)); + services.AddSingleton(none); + services.AddSingleton(new EphemeralDataProtectionProvider()); + services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>)); + })) + { + // A fresh store (empty per-process cache) per step, so every read below reaches the database. + EfAppConfigStore Store() => new(settings, settings.GetRequiredService(), NullLogger.Instance); + using (RowLevelSecurity.PlatformScope("test-seed")) + { + var seeder = Store(); + await seeder.SetForTenantAsync(A, "Llm:Claude:ApiKey", "sk-a"); + await seeder.SetForTenantAsync(RowLevelSecurity.PlatformSettingsTenant, "@platform/Tenants:OpenSignup", "false"); + } + (await Store().GetForTenantAsync(A, "Llm:Claude:ApiKey")).ShouldBe("sk-a"); // an unbound flow names A + using (AmbientIdentity.Push(B, "u")) + { + (await Store().GetForTenantAsync(A, "Llm:Claude:ApiKey")).ShouldBeNull(); // B never reads A's secret + (await Store().GetForTenantAsync(RowLevelSecurity.PlatformSettingsTenant, "@platform/Tenants:OpenSignup")).ShouldBe("false"); + (await Should.ThrowAsync(async () => + await Store().SetForTenantAsync(RowLevelSecurity.PlatformSettingsTenant, "@platform/Tenants:OpenSignup", "true"))) + .ShouldBeAssignableTo(); + (await Should.ThrowAsync(async () => await Store().SetForTenantAsync(A, "Llm:Claude:ApiKey", "stolen"))) + .ShouldBeAssignableTo(); + } + (await CountAsSuperuserAsync(cs, "config.app_config", A)).ShouldBe(1L); + } + } + finally + { + RowLevelSecurity.SetState(previousState); + RowLevelSecurity.HttpTenant = previousHttp; + NpgsqlConnection.ClearAllPools(); + await Db.ExecAsync(admin, $"DROP DATABASE IF EXISTS \"{database}\" WITH (FORCE)"); + } + } + + // Module contexts as the modules register them: the RLS interceptor on every connection. + private static DbContextOptions Bound(string cs, string schema) where T : DbContext + => new DbContextOptionsBuilder() + .UseNpgsql(cs, n => n.MigrationsHistoryTable("__EFMigrationsHistory", schema)) + .AddInterceptors(RowLevelSecurityInterceptor.Instance) + .Options; + + private static ServiceProvider Provider(Action configure) + { + var services = new ServiceCollection(); + configure(services); + return services.BuildServiceProvider(); + } + + // One autopilot board, one paired runner, one due schedule and one orchestration per tenant — as the superuser. + private static async Task<(Guid RunnerA, Guid RunnerB)> SeedAsync(string cs) + { + await using var conn = new NpgsqlConnection(cs); + await conn.OpenAsync(); + await Db.ExecAsync(conn, RowLevelSecurity.BootstrapSql(conn.PostgreSqlVersion.Major)); + var columns = await Db.RequiredColumnsAsync(conn); + var seed = 0; + await Db.ExecAsync(conn, "SET session_replication_role = replica"); + foreach (var table in new[] { "workspaces.workspaces", "sessions.runners", "pipeline.scheduled_runs", "pipeline.orchestrations" }) + { + await Db.InsertAsync(conn, table, columns[table], A, ++seed); + await Db.InsertAsync(conn, table, columns[table], B, ++seed); + } + await Db.ExecAsync(conn, """ + RESET session_replication_role; + UPDATE workspaces.workspaces SET "Autopilot" = 'Build'; + UPDATE sessions.runners SET "Status" = 'Pending'; + UPDATE pipeline.scheduled_runs SET "Enabled" = true, "Kind" = 'Once', "RunAtUtc" = now() - interval '2 minutes', + "NextRunAtUtc" = now() - interval '1 minute'; + """); + return (await RunnerIdAsync(conn, A), await RunnerIdAsync(conn, B)); + } + + private static async Task RunnerIdAsync(NpgsqlConnection conn, string tenant) + { + await using var cmd = new NpgsqlCommand("SELECT \"Id\" FROM sessions.runners WHERE \"TenantId\" = @t", conn); + cmd.Parameters.AddWithValue("t", tenant); + return (Guid)(await cmd.ExecuteScalarAsync())!; + } + + private static async Task CountAsSuperuserAsync(string cs, string table, string tenant) + { + await using var conn = new NpgsqlConnection(cs); + await conn.OpenAsync(); + return await Db.CountAsync(conn, table, tenant); + } +} diff --git a/tests/AgentOs.Tests/Persistence/RowLevelSecurityStageTwoRealPostgresTests.cs b/tests/AgentOs.Tests/Persistence/RowLevelSecurityStageTwoRealPostgresTests.cs new file mode 100644 index 00000000..59d8ceb9 --- /dev/null +++ b/tests/AgentOs.Tests/Persistence/RowLevelSecurityStageTwoRealPostgresTests.cs @@ -0,0 +1,317 @@ +// REAL-POSTGRES — RLS stage 2 (ADR-0012, GAP-14) on EVERY table in RowLevelSecurity.Tables, plus GAP-11b's append-only +// audit trail. A throwaway database gets every module's migrations and the real bootstrap script; one row per tenant is +// seeded in every table (values generated from the catalog, foreign-key triggers off). Then, as the app role and with +// NO tenant predicate in any query: +// • tenant A sees exactly A's rows (plus the shared "__platform" rows of config.app_config) and cannot write B's; +// • no tenant sees nothing and can write nothing (stage 1 showed it everything); +// • an explicit platform scope sees every row; +// • tenants.audit_events: INSERT works, UPDATE / DELETE / TRUNCATE are refused (permission denied); +// • config.app_config: a tenant reads the platform rows but can neither change, delete nor add one; +// • a table OWNER that is not a superuser — how Azure connects — is bound too (FORCE): no tenant ⇒ nothing. +// And the catalog check: every table with a "TenantId" column is in RowLevelSecurity.Tables or ExemptTables. +// Gate: RUN_AGENTOS_E2E_REAL=true and AGENTOS_PG_CONN (a server the test may CREATE / DROP DATABASE and ROLE on — the +// dev/lab stack's superuser, or a throwaway container). Zero residue: the database and the role are dropped. + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using AgentOs.Modules.AppConfig.Persistence; +using AgentOs.Modules.Memory.Persistence; +using AgentOs.Modules.Pipeline.Persistence; +using AgentOs.Modules.Sessions.Persistence; +using AgentOs.Modules.Tenants.Persistence; +using AgentOs.Modules.Tools.Persistence; +using AgentOs.Modules.Workspaces.Persistence; +using AgentOs.SharedKernel.Identity; +using AgentOs.SharedKernel.Persistence; +using Microsoft.EntityFrameworkCore; +using Npgsql; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Persistence; + +public sealed class RowLevelSecurityStageTwoRealPostgresTests +{ + private const string A = "rls-tenant-a"; + private const string B = "rls-tenant-b"; + private const string AppConfig = "config.app_config"; + private const string AuditEvents = "tenants.audit_events"; + + internal static string? ServerConnection => + string.Equals(Environment.GetEnvironmentVariable("RUN_AGENTOS_E2E_REAL"), "true", StringComparison.OrdinalIgnoreCase) + ? Environment.GetEnvironmentVariable("AGENTOS_PG_CONN") + : null; + + [Fact] + public async Task StageTwo_EveryTenantTable_NoTenantSeesNothing_TenantSeesItsOwn_PlatformSeesAll() + { + var server = ServerConnection ?? string.Empty; + if (server.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } + + var database = "agentos_rls2_" + Guid.NewGuid().ToString("N")[..12]; + var owner = "agentos_rls2_owner_" + Guid.NewGuid().ToString("N")[..8]; + var admin = new NpgsqlConnectionStringBuilder(server) { Database = "postgres", Pooling = false }.ConnectionString; + var cs = new NpgsqlConnectionStringBuilder(server) { Database = database, Pooling = false }.ConnectionString; + await ExecAsync(admin, $"CREATE DATABASE \"{database}\""); + try + { + await MigrateEveryModuleAsync(cs); + await using var conn = new NpgsqlConnection(cs); + await conn.OpenAsync(); + await ExecAsync(conn, RowLevelSecurity.BootstrapSql(conn.PostgreSqlVersion.Major)); + + // 1. Every tenant table is either under RLS or exempt on purpose — and every listed table exists. + var tenantTables = await TenantTablesAsync(conn); + var covered = RowLevelSecurity.Tables.Select(t => $"{t.Schema}.{t.Table}").ToHashSet(StringComparer.Ordinal); + tenantTables.Except(covered).Except(RowLevelSecurity.ExemptTables.Keys) + .ShouldBeEmpty("Tables with a TenantId column that are neither in RowLevelSecurity.Tables nor ExemptTables"); + covered.Except(tenantTables).ShouldBeEmpty("RowLevelSecurity.Tables names a table no migration creates"); + + // 2. Enabled, forced, and the stage-2 policy (no stage-1 "unset tenant ⇒ visible" escape). + foreach (var table in covered) + { + var (enabled, forced, qual, check) = await PolicyAsync(conn, table); + enabled.ShouldBeTrue($"{table}: RLS not enabled"); + forced.ShouldBeTrue($"{table}: RLS not forced"); + qual.ShouldNotBeNull($"{table}: agentos_tenant policy missing"); + foreach (var expr in new[] { qual, check! }) + { + expr.ShouldContain("NULLIF", Case.Insensitive, $"{table}: {expr}"); + expr.ShouldContain(RowLevelSecurity.PlatformSetting, Case.Sensitive, $"{table}: {expr}"); + expr.ShouldNotContain("COALESCE", Case.Insensitive, $"{table}: {expr}"); + } + } + + // 3. Seed one row for A and one for B in every table (+ a platform settings row), as the superuser. + var columns = await RequiredColumnsAsync(conn); + var seed = 0; + await ExecAsync(conn, "SET session_replication_role = replica"); // FK triggers off: no parent rows needed + foreach (var table in covered) + { + await InsertAsync(conn, table, columns[table], A, ++seed); + await InsertAsync(conn, table, columns[table], B, ++seed); + } + await InsertAsync(conn, AppConfig, columns[AppConfig], RowLevelSecurity.PlatformSettingsTenant, ++seed); + await ExecAsync(conn, "RESET session_replication_role"); + var baseline = new Dictionary(StringComparer.Ordinal); + foreach (var table in covered) + { + baseline[table] = (await CountAsync(conn, table, A), table == AppConfig ? await CountAsync(conn, table, RowLevelSecurity.PlatformSettingsTenant) : 0, + await CountAsync(conn, table)); + } + + // 4. As the app role — every query below has NO tenant predicate. + await ExecAsync(conn, $"SET ROLE {RowLevelSecurity.Role}"); + foreach (var table in covered) + { + var expected = baseline[table]; + await SessionAsync(conn, tenant: A, platform: false); + (await CountAsync(conn, table)).ShouldBe(expected.A + expected.Shared, $"{table}: tenant A"); + (await RefusedAsync(() => InsertAsync(conn, table, columns[table], B, ++seed))).ShouldContain("row-level security", Case.Insensitive, table); + + await SessionAsync(conn, tenant: null, platform: false); + (await CountAsync(conn, table)).ShouldBe(expected.Shared, $"{table}: no tenant must see nothing"); + (await RefusedAsync(() => InsertAsync(conn, table, columns[table], A, ++seed))).ShouldContain("row-level security", Case.Insensitive, table); + + await SessionAsync(conn, tenant: null, platform: true); + (await CountAsync(conn, table)).ShouldBe(expected.All, $"{table}: platform scope"); + } + + // 5. GAP-11b: the audit trail is append-only for the app role. + await SessionAsync(conn, tenant: A, platform: false); + await InsertAsync(conn, AuditEvents, columns[AuditEvents], A, ++seed); + (await RefusedAsync(() => ExecAsync(conn, "UPDATE tenants.audit_events SET \"Action\" = 'forged'"))).ShouldContain("permission denied"); + (await RefusedAsync(() => ExecAsync(conn, "DELETE FROM tenants.audit_events"))).ShouldContain("permission denied"); + (await RefusedAsync(() => ExecAsync(conn, "TRUNCATE tenants.audit_events"))).ShouldContain("permission denied"); + await SessionAsync(conn, tenant: null, platform: true); // not even a platform operation may rewrite it + (await RefusedAsync(() => ExecAsync(conn, "DELETE FROM tenants.audit_events"))).ShouldContain("permission denied"); + + // 6. Platform settings: readable by every tenant, changed only by an explicit platform operation. + await SessionAsync(conn, tenant: A, platform: false); + (await ExecCountAsync(conn, "UPDATE config.app_config SET \"EncryptedValue\" = 'x' WHERE \"TenantId\" = '__platform'")).ShouldBe(0); + (await ExecCountAsync(conn, "DELETE FROM config.app_config WHERE \"TenantId\" = '__platform'")).ShouldBe(0); + (await RefusedAsync(() => InsertAsync(conn, AppConfig, columns[AppConfig], RowLevelSecurity.PlatformSettingsTenant, ++seed))) + .ShouldContain("row-level security", Case.Insensitive); + await SessionAsync(conn, tenant: null, platform: true); + await InsertAsync(conn, AppConfig, columns[AppConfig], RowLevelSecurity.PlatformSettingsTenant, ++seed); + await ExecAsync(conn, "RESET ROLE"); + + // 7. A table owner that is not a superuser (Azure Flexible Server) is bound by FORCE as well. + await ExecAsync(conn, $"CREATE ROLE \"{owner}\" NOLOGIN; GRANT USAGE ON SCHEMA workspaces TO \"{owner}\"; ALTER TABLE workspaces.workspaces OWNER TO \"{owner}\""); + var workspaces = baseline["workspaces.workspaces"]; + await ExecAsync(conn, $"SET ROLE \"{owner}\""); + await SessionAsync(conn, tenant: null, platform: false); + (await CountAsync(conn, "workspaces.workspaces")).ShouldBe(0L, "the owner with no tenant must see nothing"); + await SessionAsync(conn, tenant: A, platform: false); + (await CountAsync(conn, "workspaces.workspaces")).ShouldBe(workspaces.A); + await SessionAsync(conn, tenant: null, platform: true); + (await CountAsync(conn, "workspaces.workspaces")).ShouldBe(workspaces.All); + await ExecAsync(conn, "RESET ROLE"); + } + finally + { + NpgsqlConnection.ClearAllPools(); + await ExecAsync(admin, $"DROP DATABASE IF EXISTS \"{database}\" WITH (FORCE)"); + await ExecAsync(admin, $"DROP ROLE IF EXISTS \"{owner}\""); + } + } + + // ── helpers (shared with the other real-Postgres RLS tests in this folder) ── + + internal static async Task MigrateEveryModuleAsync(string cs) + { + var tenant = new FixedTenant("migrations"); + await using (var db = new PipelineDbContext(Options(cs, "pipeline"), tenant)) { await db.Database.MigrateAsync(); } + await using (var db = new SessionsDbContext(Options(cs, "sessions"))) { await db.Database.MigrateAsync(); } + await using (var db = new ToolsDbContext(Options(cs, "tools"))) { await db.Database.MigrateAsync(); } + await using (var db = new WorkspacesDbContext(Options(cs, "workspaces"))) { await db.Database.MigrateAsync(); } + await using (var db = new MemoryDbContext(Options(cs, "memory"))) { await db.Database.MigrateAsync(); } + await using (var db = new AppConfigDbContext(Options(cs, "config"))) { await db.Database.MigrateAsync(); } + await using (var db = new TenantsDbContext(Options(cs, "tenants"))) { await db.Database.MigrateAsync(); } + } + + internal static DbContextOptions Options(string cs, string schema) where T : DbContext + => new DbContextOptionsBuilder().UseNpgsql(cs, n => n.MigrationsHistoryTable("__EFMigrationsHistory", schema)).Options; + + private static async Task> TenantTablesAsync(NpgsqlConnection conn) + { + const string sql = """ + SELECT n.nspname || '.' || c.relname + FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE a.attname = 'TenantId' AND c.relkind = 'r' AND NOT a.attisdropped + AND n.nspname NOT IN ('pg_catalog', 'information_schema') + """; + var set = new HashSet(StringComparer.Ordinal); + await using var cmd = new NpgsqlCommand(sql, conn); + await using var reader = await cmd.ExecuteReaderAsync(); + while (await reader.ReadAsync()) { set.Add(reader.GetString(0)); } + return set; + } + + private static async Task<(bool Enabled, bool Forced, string? Qual, string? Check)> PolicyAsync(NpgsqlConnection conn, string table) + { + await using var cmd = new NpgsqlCommand(""" + SELECT c.relrowsecurity, c.relforcerowsecurity, p.qual, p.with_check + FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_policies p ON p.schemaname = n.nspname AND p.tablename = c.relname AND p.policyname = 'agentos_tenant' + WHERE n.nspname || '.' || c.relname = @t + """, conn); + cmd.Parameters.AddWithValue("t", table); + await using var reader = await cmd.ExecuteReaderAsync(); + (await reader.ReadAsync()).ShouldBeTrue($"{table} missing"); + return (reader.GetBoolean(0), reader.GetBoolean(1), reader.IsDBNull(2) ? null : reader.GetString(2), reader.IsDBNull(3) ? null : reader.GetString(3)); + } + + internal sealed record Column(string Name, string Type, int? MaxLength); + + // The columns an INSERT must name: NOT NULL, no default, not generated / identity. + internal static async Task>> RequiredColumnsAsync(NpgsqlConnection conn) + { + const string sql = """ + SELECT table_schema || '.' || table_name, column_name, udt_name, character_maximum_length + FROM information_schema.columns + WHERE is_nullable = 'NO' AND column_default IS NULL AND is_generated = 'NEVER' AND is_identity = 'NO' + AND table_schema NOT IN ('pg_catalog', 'information_schema') + ORDER BY table_schema, table_name, ordinal_position + """; + var byTable = new Dictionary>(StringComparer.Ordinal); + await using var cmd = new NpgsqlCommand(sql, conn); + await using var reader = await cmd.ExecuteReaderAsync(); + while (await reader.ReadAsync()) + { + var table = reader.GetString(0); + if (!byTable.TryGetValue(table, out var list)) { byTable[table] = list = []; } + list.Add(new Column(reader.GetString(1), reader.GetString(2), reader.IsDBNull(3) ? null : reader.GetInt32(3))); + } + return byTable; + } + + // One row with generated values: the tenant in "TenantId", a unique value per seed in every other required column. + internal static async Task InsertAsync(NpgsqlConnection conn, string table, IReadOnlyList columns, string tenant, int seed) + { + var names = string.Join(", ", columns.Select(c => $"\"{c.Name}\"")); + var values = string.Join(", ", columns.Select(c => c.Name == "TenantId" ? "@tenant" : Literal(c, seed))); + var parts = table.Split('.'); + await using var cmd = new NpgsqlCommand($"INSERT INTO \"{parts[0]}\".\"{parts[1]}\" ({names}) VALUES ({values})", conn); + cmd.Parameters.AddWithValue("tenant", tenant); + await cmd.ExecuteNonQueryAsync(); + } + + private static string Literal(Column c, int seed) + { + var text = "r" + seed.ToString(CultureInfo.InvariantCulture); + return c.Type switch + { + "varchar" or "text" or "bpchar" or "citext" => $"'{(c.MaxLength is { } max && text.Length > max ? text[..max] : text)}'", + "uuid" => "gen_random_uuid()", + "int2" or "int4" or "int8" or "numeric" or "float4" or "float8" => seed.ToString(CultureInfo.InvariantCulture), + "bool" => "false", + "timestamptz" or "timestamp" => "now()", + "date" => "current_date", + "bytea" => "'\\x00'::bytea", + "jsonb" or "json" => "'{}'::" + c.Type, + "interval" => "interval '0'", + "tsvector" => "''::tsvector", + _ when c.Type.StartsWith('_') => $"'{{}}'::{c.Type[1..]}[]", + _ => throw new InvalidOperationException($"No seed value for {c.Name} ({c.Type}) — extend Literal()."), + }; + } + + internal static async Task SessionAsync(NpgsqlConnection conn, string? tenant, bool platform) + { + await using var cmd = new NpgsqlCommand( + $"SELECT set_config('{RowLevelSecurity.TenantSetting}', @t, false), set_config('{RowLevelSecurity.PlatformSetting}', @p, false)", conn); + cmd.Parameters.AddWithValue("t", tenant ?? string.Empty); + cmd.Parameters.AddWithValue("p", platform ? "on" : string.Empty); + await cmd.ExecuteNonQueryAsync(); + } + + internal static async Task CountAsync(NpgsqlConnection conn, string table, string? tenant = null) + { + var parts = table.Split('.'); + await using var cmd = new NpgsqlCommand( + $"SELECT count(*) FROM \"{parts[0]}\".\"{parts[1]}\"" + (tenant is null ? string.Empty : " WHERE \"TenantId\" = @t"), conn); + if (tenant is not null) { cmd.Parameters.AddWithValue("t", tenant); } + return (long)(await cmd.ExecuteScalarAsync())!; + } + + // The statement must fail with 42501 (an RLS violation or a missing privilege); returns the message. + internal static async Task RefusedAsync(Func statement) + { + var error = await Should.ThrowAsync(statement); + error.SqlState.ShouldBe(PostgresErrorCodes.InsufficientPrivilege, error.MessageText); + return error.MessageText; + } + + internal static async Task ExecAsync(string cs, string sql) + { + await using var conn = new NpgsqlConnection(cs); + await conn.OpenAsync(); + await ExecAsync(conn, sql); + } + + internal static async Task ExecAsync(NpgsqlConnection conn, string sql) + { + await using var cmd = new NpgsqlCommand(sql, conn); + await cmd.ExecuteNonQueryAsync(); + } + + private static async Task ExecCountAsync(NpgsqlConnection conn, string sql) + { + await using var cmd = new NpgsqlCommand(sql, conn); + return await cmd.ExecuteNonQueryAsync(); + } + + internal sealed class FixedTenant(string id) : ITenantContext + { + public string TenantId { get; } = id; + public string? UserId => null; + public string? UserName => null; + public IReadOnlyList Roles { get; } = []; + public bool IsAuthenticated => false; + public bool IsAdmin => false; + } +} diff --git a/tests/AgentOs.Tests/Persistence/RowLevelSecurityTests.cs b/tests/AgentOs.Tests/Persistence/RowLevelSecurityTests.cs index 37d1388a..263afefd 100644 --- a/tests/AgentOs.Tests/Persistence/RowLevelSecurityTests.cs +++ b/tests/AgentOs.Tests/Persistence/RowLevelSecurityTests.cs @@ -1,50 +1,98 @@ -// ADR-0012 stage 1 — the bootstrap script covers every stage-1 tenant table with an enabled + forced policy, uses -// the PG16 membership syntax only where it exists, and the per-connection switch never runs before bootstrap or -// inside a bypass scope. The database-level behaviour is proven on real Postgres by RowLevelSecurityRealDbTests. +// ADR-0012 stage 2 (GAP-14) — the bootstrap script covers every tenant table with an enabled + forced policy whose +// predicate shows a session with no tenant NOTHING (stage 1 showed it everything); config.app_config alone shares its +// "__platform" rows for reading; tenants.audit_events is append-only for the app role; the PG16 membership syntax is +// used only where it exists. The per-connection scope resolves innermost-first (tenant / platform scopes, then the +// ambient identity, then the HTTP request) and a fill-in scope never overrides a flow's own tenant. A failed bootstrap +// stops the host outside Development. The database-level behaviour is proven on real Postgres by +// RowLevelSecurityStageTwoRealPostgresTests (every table) and the E2E RowLevelSecurityRealDbTests (the running stack). +using System; using System.Linq; +using System.Threading.Tasks; +using AgentOs.ServiceDefaults; using AgentOs.SharedKernel.Identity; using AgentOs.SharedKernel.Persistence; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging.Abstractions; using Shouldly; using Xunit; namespace AgentOs.Tests.Persistence; +// Some tests here drive the process-wide RowLevelSecurity.State (and restore it): they run with the other tests that do. +[Collection(RowLevelSecurityProcessStateGroup.Name)] public sealed class RowLevelSecurityTests { [Fact] - public void BootstrapSql_PolicesEveryStageOneTable_EnabledAndForced() + public void BootstrapSql_PolicesEveryTenantTable_EnabledAndForced_WithTheStageTwoPredicate() { var sql = RowLevelSecurity.BootstrapSql(18); foreach (var (schema, table) in RowLevelSecurity.Tables) { sql.ShouldContain($"ALTER TABLE \"{schema}\".\"{table}\" ENABLE ROW LEVEL SECURITY;"); sql.ShouldContain($"ALTER TABLE \"{schema}\".\"{table}\" FORCE ROW LEVEL SECURITY;"); - sql.ShouldContain($"CREATE POLICY agentos_tenant ON \"{schema}\".\"{table}\" USING"); + sql.ShouldContain($"CREATE POLICY agentos_tenant ON \"{schema}\".\"{table}\" USING {RowLevelSecurity.PolicyPredicate} WITH CHECK {RowLevelSecurity.PolicyPredicate};"); } sql.ShouldContain("NOBYPASSRLS"); - sql.ShouldContain("WITH CHECK"); } + // The stage-1 escape — "no tenant set ⇒ every row" — is gone: an empty or unset tenant matches no row (NULLIF), and + // only an explicit platform flag opens every tenant. [Fact] - public void BootstrapSql_UsesTheMembershipSyntaxTheServerSupports() + public void PolicyPredicate_NoTenantMatchesNothing_OnlyAnExplicitPlatformScopeWidens() { - RowLevelSecurity.BootstrapSql(16).ShouldContain("WITH INHERIT FALSE, SET TRUE"); - RowLevelSecurity.BootstrapSql(15).ShouldNotContain("SET TRUE"); + RowLevelSecurity.PolicyPredicate.ShouldBe( + "(\"TenantId\" = NULLIF(current_setting('app.tenant_id', true), '') OR current_setting('app.platform', true) = 'on')"); + RowLevelSecurity.BootstrapSql(18).ShouldNotContain("coalesce(current_setting"); } + // Stage 2 brings the platform-scoped tables (app_config, audit_events) + invitations under the policy; the tables the + // modules added in the meantime (scheduled runs, memory, autopilot events, ticket flows/files, design reviews, + // approvals) are covered as well — a table a module adds goes into Tables or ExemptTables, never nowhere. [Fact] - public void StageOne_LeavesPlatformScopedTablesToStageTwo() + public void Tables_StageTwo_IncludeAppConfigAuditEventsAndInvitations() { - // app_config and audit_events hold platform-scope rows (__platform / _platform) read by every tenant. - RowLevelSecurity.Tables.ShouldNotContain(t => t.Table == "app_config" || t.Table == "audit_events"); - RowLevelSecurity.Tables.Count.ShouldBe(17); // + pipeline.scheduled_runs, memory.nodes, memory.edges, workspaces.autopilot_events, workspaces.ticket_flows, sessions.ticket_files, sessions.design_reviews, sessions.approvals - RowLevelSecurity.Tables.ShouldContain(("sessions", "design_reviews")); - RowLevelSecurity.Tables.ShouldContain(("sessions", "approvals")); + RowLevelSecurity.Tables.Count.ShouldBe(20); + RowLevelSecurity.Tables.ShouldContain(("config", "app_config")); + RowLevelSecurity.Tables.ShouldContain(("tenants", "audit_events")); + RowLevelSecurity.Tables.ShouldContain(("tenants", "invitations")); RowLevelSecurity.Tables.ShouldContain(("pipeline", "scheduled_runs")); RowLevelSecurity.Tables.ShouldContain(("sessions", "ticket_files")); + RowLevelSecurity.Tables.ShouldContain(("sessions", "design_reviews")); + RowLevelSecurity.Tables.ShouldContain(("sessions", "approvals")); RowLevelSecurity.Tables.ShouldContain(("workspaces", "ticket_flows")); RowLevelSecurity.Tables.ShouldContain(("memory", "nodes")); + RowLevelSecurity.Tables.Distinct().Count().ShouldBe(RowLevelSecurity.Tables.Count); + RowLevelSecurity.ExemptTables.Keys.ShouldBe(["tenants.api_tokens"]); + RowLevelSecurity.ExemptTables.Keys.ShouldNotContain(k => RowLevelSecurity.Tables.Any(t => $"{t.Schema}.{t.Table}" == k)); + } + + // Platform settings are read by every tenant (they are the defaults behind each tenant's own), never written by one. + [Fact] + public void BootstrapSql_AppConfigSharesPlatformRowsForReadingOnly() + { + var sql = RowLevelSecurity.BootstrapSql(18); + sql.ShouldContain("CREATE POLICY agentos_shared_read ON \"config\".\"app_config\" FOR SELECT USING (\"TenantId\" = '__platform');"); + sql.Split("CREATE POLICY agentos_shared_read").Length.ShouldBe(2); // created for app_config only (dropped everywhere) + RowLevelSecurity.PlatformSettingsTenant.ShouldBe(AgentOs.Modules.AppConfig.Settings.SettingsService.PlatformTenant); + } + + // GAP-11b: the revoke runs AFTER the schema-wide grants (which run every boot), or it would be undone each boot. + [Fact] + public void BootstrapSql_AuditEventsAreAppendOnly_RevokedAfterTheGrants() + { + var sql = RowLevelSecurity.BootstrapSql(18); + const string revoke = "REVOKE UPDATE, DELETE, TRUNCATE ON \"tenants\".\"audit_events\" FROM agentos_rls;"; + sql.ShouldContain(revoke); + sql.IndexOf(revoke, StringComparison.Ordinal).ShouldBeGreaterThan(sql.IndexOf("GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES", StringComparison.Ordinal)); + RowLevelSecurity.AppendOnlyTables.ShouldBe([("tenants", "audit_events")]); + } + + [Fact] + public void BootstrapSql_UsesTheMembershipSyntaxTheServerSupports() + { + RowLevelSecurity.BootstrapSql(16).ShouldContain("WITH INHERIT FALSE, SET TRUE"); + RowLevelSecurity.BootstrapSql(15).ShouldNotContain("SET TRUE"); } [Fact] @@ -67,4 +115,128 @@ public void CurrentTenant_PrefersTheAmbientIdentity_ThenTheHttpRequest() RowLevelSecurity.HttpTenant = previous; } } + + // Innermost scope wins; disposing restores the outer one. + [Fact] + public void Scopes_InnermostWins_AndRestoreOnDispose() + { + using (AmbientIdentity.Push("ambient", "u")) + { + using (RowLevelSecurity.PlatformScope("test")) + { + RowLevelSecurity.InPlatformScope.ShouldBeTrue(); + RowLevelSecurity.CurrentTenant().ShouldBeNull(); + using (RowLevelSecurity.TenantScope("signed-token-tenant")) + { + RowLevelSecurity.InPlatformScope.ShouldBeFalse(); + RowLevelSecurity.CurrentTenant().ShouldBe("signed-token-tenant"); + } + RowLevelSecurity.InPlatformScope.ShouldBeTrue(); + } + RowLevelSecurity.InPlatformScope.ShouldBeFalse(); + RowLevelSecurity.CurrentTenant().ShouldBe("ambient"); + } + } + + // An explicit tenant fills a flow that has none; it never overrides a flow's own tenant, nor a platform scope. + [Fact] + public void TenantScopeIfUnbound_FillsOnlyAMissingTenant() + { + var previous = RowLevelSecurity.HttpTenant; + try + { + RowLevelSecurity.HttpTenant = null; + using (RowLevelSecurity.TenantScopeIfUnbound("worker-row-tenant")) + { + RowLevelSecurity.CurrentTenant().ShouldBe("worker-row-tenant"); + } + RowLevelSecurity.CurrentTenant().ShouldBeNull(); + + using (AmbientIdentity.Push("tenant-a", "u")) + { + RowLevelSecurity.TenantScopeIfUnbound("tenant-b").ShouldBeNull(); + RowLevelSecurity.CurrentTenant().ShouldBe("tenant-a"); + } + using (RowLevelSecurity.PlatformScope("test")) + { + RowLevelSecurity.TenantScopeIfUnbound("tenant-b").ShouldBeNull(); + } + RowLevelSecurity.TenantScopeIfUnbound(" ").ShouldBeNull(); + } + finally + { + RowLevelSecurity.HttpTenant = previous; + } + } + + [Fact] + public async Task ScopeSet_InAnAsyncMethod_DoesNotLeakToTheCaller() + { + static async Task EnterAsync() + { + using var platform = RowLevelSecurity.PlatformScope("test"); + await Task.Yield(); + RowLevelSecurity.InPlatformScope.ShouldBeTrue(); + } + + await EnterAsync(); + RowLevelSecurity.InPlatformScope.ShouldBeFalse(); + } + + [Fact] + public void Scopes_RejectBlankArguments() + { + Should.Throw(() => RowLevelSecurity.PlatformScope(" ")); + Should.Throw(() => RowLevelSecurity.TenantScope("")); + } + + // GAP-14: a database the bootstrap can't reach stops the host outside Development; Development logs and goes on. + // (Port 1 refuses at once — no real server needed. No database / the escape hatch never touch the network.) + // The state is process-wide: these two put it back (other tests only ever see a "not enabled" state either way). + [Fact] + public async Task EnsureAtStartup_BootstrapFails_OutsideDevelopment_Throws() + { + var previous = RowLevelSecurity.State; + try + { + var error = await Should.ThrowAsync(() => + RowLevelSecurity.EnsureAtStartupAsync(Unreachable, enabledByConfig: true, isDevelopment: false, NullLogger.Instance)); + error.Message.ShouldContain("Persistence:RowLevelSecurity=false"); + RowLevelSecurity.State.ShouldBe(RowLevelSecurityState.Failed); + } + finally + { + RowLevelSecurity.SetState(previous); + } + } + + [Fact] + public async Task EnsureAtStartup_DevelopmentOrHatchOrNoDatabase_DoesNotThrow() + { + var previous = RowLevelSecurity.State; + try + { + (await RowLevelSecurity.EnsureAtStartupAsync(Unreachable, enabledByConfig: true, isDevelopment: true, NullLogger.Instance)) + .ShouldBe(RowLevelSecurityState.Failed); + (await RowLevelSecurity.EnsureAtStartupAsync(Unreachable, enabledByConfig: false, isDevelopment: false, NullLogger.Instance)) + .ShouldBe(RowLevelSecurityState.Disabled); + (await RowLevelSecurity.EnsureAtStartupAsync(null, enabledByConfig: true, isDevelopment: false, NullLogger.Instance)) + .ShouldBe(RowLevelSecurityState.NotConfigured); + } + finally + { + RowLevelSecurity.SetState(previous); + } + } + + private const string Unreachable = "Host=127.0.0.1;Port=1;Database=x;Username=x;Password=x;Timeout=2"; + + [Theory] + [InlineData(RowLevelSecurityState.Enabled, HealthStatus.Healthy)] + [InlineData(RowLevelSecurityState.Disabled, HealthStatus.Degraded)] + [InlineData(RowLevelSecurityState.Pending, HealthStatus.Degraded)] + [InlineData(RowLevelSecurityState.Failed, HealthStatus.Unhealthy)] + [InlineData(RowLevelSecurityState.NotConfigured, HealthStatus.Healthy)] + public void HealthCheck_ReportsTheState(RowLevelSecurityState state, HealthStatus expected) + => RowLevelSecurityHealthCheck.For(state, HealthStatus.Unhealthy).Status.ShouldBe(expected); } diff --git a/tests/AgentOs.Tests/Security/PublicLinkTests.cs b/tests/AgentOs.Tests/Security/PublicLinkTests.cs index 98df4b85..f417e6a2 100644 --- a/tests/AgentOs.Tests/Security/PublicLinkTests.cs +++ b/tests/AgentOs.Tests/Security/PublicLinkTests.cs @@ -1,9 +1,12 @@ // GAP-05 — links the server hands out (invitation URL, the runner's hub URL) come from Hosting:PublicBaseUrl, never from // the Host header of the request that minted them. GAP-19 — the VS Code approve form is antiforgery-protected. -// Real endpoints (TenantEndpoints, PairingEndpoints) on a TestServer; the request's Host is an attacker's. +// Real endpoints (TenantEndpoints, PairingEndpoints) on a TestServer; the request's Host is an attacker's. The desktop's +// Settings › Users builds its invitation link through the same InvitationLinks, never from the circuit's base URI. using System; using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Json; @@ -15,6 +18,7 @@ using AgentOs.Modules.Tenants; using AgentOs.Modules.Tenants.Email; using AgentOs.Modules.Tenants.Endpoints; +using AgentOs.Modules.Tenants.Invitations; using AgentOs.Modules.Tenants.Keycloak; using AgentOs.SharedKernel.Hosting; using AgentOs.SharedKernel.Identity; @@ -80,8 +84,10 @@ public async Task Invitation_UrlPointsAtThePublicBaseUrl_NotTheRequestHost() await using var app = await StartAsync(services => { var signup = Substitute.For(); - signup.CreateInvitation("acme", "member", null, Arg.Any()) - .Returns(new InvitationToken("tok/1", DateTimeOffset.UtcNow.AddHours(1))); + // GAP-11: an invitation is single-use and bound to the address it is sent to — the service mints it + // for that email and audits it with the caller as actor. + signup.CreateInvitationAsync("acme", "member", "bob@acme.test", Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(new InvitationToken(Guid.NewGuid(), "tok/1", DateTimeOffset.UtcNow.AddHours(1))); services.AddSingleton(signup); services.AddSingleton(Substitute.For()); services.AddSingleton(Substitute.For()); @@ -91,13 +97,64 @@ public async Task Invitation_UrlPointsAtThePublicBaseUrl_NotTheRequestHost() }, app => app.MapTenantEndpoints()); using var client = Client(app, "admin"); - using var response = await client.PostAsJsonAsync("/tenants/acme/invitations", new { }, TestContext.Current.CancellationToken); + using var response = await client.PostAsJsonAsync("/tenants/acme/invitations", new { email = "bob@acme.test" }, TestContext.Current.CancellationToken); response.StatusCode.ShouldBe(HttpStatusCode.OK); var body = await response.Content.ReadFromJsonAsync>(TestContext.Current.CancellationToken); body!["url"].ToString().ShouldBe(PublicUrl + "/signup?invite=tok%2F1"); } + // ---- Invitation URL in the desktop (Settings › Users) -------------------------------------------------------- + + [Fact] + public void InvitationLink_Configured_IgnoresTheCircuitBaseUri() + { + var origin = new PublicOrigin(PublicUrl, allowRequestFallback: false); + + InvitationLinks.Absolute(origin, "tok/1", $"https://{AttackerHost}/").ShouldBe(PublicUrl + "/signup?invite=tok%2F1"); + } + + [Fact] + public void InvitationLink_UnconfiguredInDevelopment_UsesTheCircuitBaseUri() + { + new PublicOrigin(null, allowRequestFallback: true).ShouldSatisfyAllConditions( + o => InvitationLinks.Absolute(o, "t", "https://localhost:5180/").ShouldBe("https://localhost:5180/signup?invite=t"), + o => InvitationLinks.PathFor("a b").ShouldBe("/signup?invite=a%20b")); + } + + [Fact] + public void InvitationLink_UnconfiguredOutsideDevelopment_IsNeverBuiltFromTheCircuit() + { + Should.Throw(() => + InvitationLinks.Absolute(new PublicOrigin(null, allowRequestFallback: false), "t", $"https://{AttackerHost}/")); + } + + // The desktop is a Blazor circuit: no HttpRequest to pass, so the guard is on the source — every invitation link in + // a .razor goes through InvitationLinks with the injected PublicOrigin, never NavigationManager's own origin. + [Fact] + public void Desktop_InvitationLinks_ComeFromThePublicOrigin_NotTheCircuit() + { + var web = Path.Join(RepoRoot(), "src", "AgentOs.Web"); + var razor = Directory.EnumerateFiles(web, "*.razor", SearchOption.AllDirectories) + .Where(f => !f.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}", StringComparison.Ordinal)) + .ToDictionary(f => Path.GetRelativePath(web, f), File.ReadAllText); + + razor.Where(kv => kv.Value.Contains("signup?invite=", StringComparison.Ordinal) || kv.Value.Contains("ToAbsoluteUri(", StringComparison.Ordinal)) + .Select(kv => kv.Key).ShouldBeEmpty("Build invitation links with InvitationLinks.Absolute(PublicOrigin, …), not from the circuit's URI"); + var users = razor.Single(kv => kv.Key.EndsWith("UsersApp.razor", StringComparison.Ordinal)).Value; + users.ShouldContain("@inject AgentOs.SharedKernel.Hosting.PublicOrigin Origin"); + users.ShouldContain("InvitationLinks.Absolute(Origin, "); + } + + private static string RepoRoot() + { + for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) + { + if (File.Exists(Path.Join(dir.FullName, "AgentOs.slnx"))) { return dir.FullName; } + } + throw new DirectoryNotFoundException("AgentOs.slnx not found above the test output directory."); + } + // ---- VS Code pairing --------------------------------------------------------------------------------------- [Fact] diff --git a/tests/AgentOs.Tests/ServiceDefaults/HealthChecksTests.cs b/tests/AgentOs.Tests/ServiceDefaults/HealthChecksTests.cs index 1fe00b72..28751f74 100644 --- a/tests/AgentOs.Tests/ServiceDefaults/HealthChecksTests.cs +++ b/tests/AgentOs.Tests/ServiceDefaults/HealthChecksTests.cs @@ -37,10 +37,10 @@ public void AddDefaultHealthChecks_NoExternalConfig_RegistersSelfOnly() => RegisteredCheckNames("Development").ShouldBe(["self"]); [Fact] - public void AddDefaultHealthChecks_WithConnectionString_RegistersPostgres() => + public void AddDefaultHealthChecks_WithConnectionString_RegistersPostgresAndRowLevelSecurity() => RegisteredCheckNames("Development", ("ConnectionStrings:DefaultConnection", "Host=localhost;Database=agentos")) - .ShouldBe(["self", "postgres"]); + .ShouldBe(["self", "postgres", "row-level-security"]); [Fact] public void AddDefaultHealthChecks_AuthorityInDevelopment_SkipsKeycloak() => diff --git a/tests/AgentOs.Tests/Sessions/ApprovalStoreRealDbTests.cs b/tests/AgentOs.Tests/Sessions/ApprovalStoreRealDbTests.cs index a9c390b0..86fa34af 100644 --- a/tests/AgentOs.Tests/Sessions/ApprovalStoreRealDbTests.cs +++ b/tests/AgentOs.Tests/Sessions/ApprovalStoreRealDbTests.cs @@ -8,7 +8,9 @@ using AgentOs.Domain.Governance; using AgentOs.Modules.Sessions.Persistence; using AgentOs.Modules.Sessions.Persistence.Repositories; +using AgentOs.SharedKernel.Identity; using AgentOs.SharedKernel.Persistence; +using AgentOs.Tests.Persistence; using Microsoft.EntityFrameworkCore; using Npgsql; using Shouldly; @@ -129,3 +131,84 @@ private static async Task Exec(NpgsqlConnection conn, string sql) await cmd.ExecuteNonQueryAsync(); } } + +// RLS stage 2 switched ON, through the module's own context (the RLS interceptor on every connection): the Autopilot +// pass and the gate check act for NO tenant, and "no tenant" now sees nothing — so the store must bind the flow to the +// tenant it names, or every approval silently reads as "not asked" and a decision never lands. A flow bound to another +// tenant still sees nothing of the named one. Process-wide RLS state → the non-parallel collection; state restored. +[Collection(RowLevelSecurityProcessStateGroup.Name)] +public sealed class ApprovalStoreRlsEnabledRealDbTests +{ + private const string A = "approvals-on-a"; + private const string B = "approvals-on-b"; + + [Fact] + public async Task Ef_RlsEnabled_UnboundFlow_AsksFindsDecidesAndLists_UnderTheNamedTenant_NeverAnother() + { + var server = RowLevelSecurityStageTwoRealPostgresTests.ServerConnection ?? string.Empty; + if (server.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } + + var database = "agentos_approvals_rls_" + Guid.NewGuid().ToString("N")[..12]; + var admin = new NpgsqlConnectionStringBuilder(server) { Database = "postgres", Pooling = false }.ConnectionString; + var cs = new NpgsqlConnectionStringBuilder(server) { Database = database, Pooling = false }.ConnectionString; + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(admin, $"CREATE DATABASE \"{database}\""); + var previousState = RowLevelSecurity.State; + var previousHttp = RowLevelSecurity.HttpTenant; + try + { + var options = new DbContextOptionsBuilder() + .UseNpgsql(cs, npg => npg.MigrationsHistoryTable("__EFMigrationsHistory", schema: "sessions")) + .AddInterceptors(RowLevelSecurityInterceptor.Instance) + .Options; + await using (var db = new SessionsDbContext(options)) + { + await db.Database.MigrateAsync(); // before the switch: the login user, platform-wide + } + await using (var conn = new NpgsqlConnection(cs)) + { + await conn.OpenAsync(); + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(conn, RowLevelSecurity.BootstrapSql(conn.PostgreSqlVersion.Major)); + } + RowLevelSecurity.HttpTenant = null; + RowLevelSecurity.SetState(RowLevelSecurityState.Enabled); + + var session = Guid.NewGuid(); + ApprovalRequest asked; + await using (var db = new SessionsDbContext(options)) + { + var store = new EfApprovalStore(db); + asked = await store.RequestAsync(ApprovalRequest.New(A, Guid.NewGuid(), session, 7, ApprovalGate.Merge, 0, + "#7 Add dark mode", null, "Autopilot", DateTimeOffset.UtcNow)); + (await store.RequestAsync(ApprovalRequest.New(A, Guid.NewGuid(), session, 7, ApprovalGate.Merge, 0, + "#7 Add dark mode", null, "Autopilot", DateTimeOffset.UtcNow))).Id.ShouldBe(asked.Id); // asked once + (await store.GetAsync(A, session, ApprovalGate.Merge, 0))!.Id.ShouldBe(asked.Id); + (await store.FindAsync(A, asked.Id))!.Status.ShouldBe(ApprovalStatus.Pending); + (await store.ListRecentAsync(A)).ShouldHaveSingleItem().Id.ShouldBe(asked.Id); + (await store.DecideAsync(A, asked.Id, new ApprovalDecision(true, "alice", null, DateTimeOffset.UtcNow)))! + .Status.ShouldBe(ApprovalStatus.Approved); + (await store.GetAsync(A, session, ApprovalGate.Merge, 0))!.Status.ShouldBe(ApprovalStatus.Approved); + } + + // A flow bound to B that names A: RLS keeps it on B — nothing of A is read or changed. + await using (var db = new SessionsDbContext(options)) + using (AmbientIdentity.Push(B, "eve")) + { + var store = new EfApprovalStore(db); + (await store.FindAsync(A, asked.Id)).ShouldBeNull(); + (await store.ListRecentAsync(A)).ShouldBeEmpty(); + (await store.DecideAsync(A, asked.Id, new ApprovalDecision(false, "eve", "x", DateTimeOffset.UtcNow))).ShouldBeNull(); + } + await using (var db = new SessionsDbContext(options)) + { + (await new EfApprovalStore(db).FindAsync(A, asked.Id))!.DecidedBy.ShouldBe("alice"); + } + } + finally + { + RowLevelSecurity.SetState(previousState); + RowLevelSecurity.HttpTenant = previousHttp; + NpgsqlConnection.ClearAllPools(); + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(admin, $"DROP DATABASE IF EXISTS \"{database}\" WITH (FORCE)"); + } + } +} diff --git a/tests/AgentOs.Tests/Sessions/ApprovalStoreRlsScopeTests.cs b/tests/AgentOs.Tests/Sessions/ApprovalStoreRlsScopeTests.cs new file mode 100644 index 00000000..1bded226 --- /dev/null +++ b/tests/AgentOs.Tests/Sessions/ApprovalStoreRlsScopeTests.cs @@ -0,0 +1,107 @@ +// RLS stage 2 (ADR-0012): "no tenant ⇒ nothing", so EfApprovalStore must bind a flow that acts for no tenant (the +// Autopilot pass, a background gate check) to the tenant it names — otherwise every unbound read returns nothing and an +// approval policy silently never sees its approvals. Without a database: a connection interceptor records the tenant the +// RLS interceptor would bind (RowLevelSecurity.CurrentTenant at connection open) and stops the open there. +// The real-Postgres twin with RLS switched on is ApprovalStoreRlsEnabledRealDbTests. + +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Threading; +using System.Threading.Tasks; +using AgentOs.Domain.Governance; +using AgentOs.Modules.Sessions.Persistence; +using AgentOs.Modules.Sessions.Persistence.Repositories; +using AgentOs.SharedKernel.Identity; +using AgentOs.SharedKernel.Persistence; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Sessions; + +public sealed class ApprovalStoreRlsScopeTests +{ + private const string A = "approvals-tenant-a"; + private const string B = "approvals-tenant-b"; + + public static TheoryData Paths => ["get", "find", "request", "decide", "list"]; + + [Theory] + [MemberData(nameof(Paths))] + public async Task EveryPath_UnboundFlow_IsBoundToTheTenantItNames(string path) + { + var probe = new TenantProbe(); + await using var db = Context(probe); + + await Should.ThrowAsync(() => Call(new EfApprovalStore(db), path, A)); + + probe.Seen.ShouldBe([A], $"{path}: the connection must open bound to the named tenant"); + } + + [Theory] + [MemberData(nameof(Paths))] + public async Task EveryPath_FlowBoundToAnotherTenant_IsNeverWidenedToTheNamedOne(string path) + { + var probe = new TenantProbe(); + await using var db = Context(probe); + + using (AmbientIdentity.Push(B, "u")) + { + await Should.ThrowAsync(() => Call(new EfApprovalStore(db), path, A)); + } + + probe.Seen.ShouldBe([B], $"{path}: a flow bound to B keeps B even when it names A"); + } + + [Fact] + public async Task Scope_EndsWithTheCall() + { + var probe = new TenantProbe(); + await using var db = Context(probe); + + await Should.ThrowAsync(() => new EfApprovalStore(db).FindAsync(A, Guid.NewGuid())); + + RowLevelSecurity.CurrentTenant().ShouldBeNull(); + } + + private static Task Call(EfApprovalStore store, string path, string tenant) => path switch + { + "get" => store.GetAsync(tenant, Guid.NewGuid(), ApprovalGate.Merge, 0), + "find" => store.FindAsync(tenant, Guid.NewGuid()), + "request" => store.RequestAsync(ApprovalRequest.New(tenant, Guid.NewGuid(), Guid.NewGuid(), 7, ApprovalGate.Merge, 0, + "#7 Add dark mode", null, "Autopilot", DateTimeOffset.UtcNow)), + "decide" => store.DecideAsync(tenant, Guid.NewGuid(), new ApprovalDecision(true, "alice", null, DateTimeOffset.UtcNow)), + _ => store.ListRecentAsync(tenant), + }; + + // Npgsql provider (so EF really opens a connection), an address that is never dialled: the probe stops the open. + private static SessionsDbContext Context(TenantProbe probe) => new(new DbContextOptionsBuilder() + .UseNpgsql("Host=approvals-probe.invalid;Database=none;Username=none;Password=none") + .AddInterceptors(probe) + .Options); + + private sealed class OpenStopped : Exception + { + public OpenStopped() : base("The probe stops every connection open.") { } + } + + private sealed class TenantProbe : DbConnectionInterceptor + { + public List Seen { get; } = []; + + public override InterceptionResult ConnectionOpening(DbConnection connection, ConnectionEventData eventData, InterceptionResult result) + { + Seen.Add(RowLevelSecurity.CurrentTenant()); + throw new OpenStopped(); + } + + public override ValueTask ConnectionOpeningAsync( + DbConnection connection, ConnectionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default) + { + Seen.Add(RowLevelSecurity.CurrentTenant()); + throw new OpenStopped(); + } + } +} diff --git a/tests/AgentOs.Tests/Sessions/ScopedWriteGuardEgressTests.cs b/tests/AgentOs.Tests/Sessions/ScopedWriteGuardEgressTests.cs index 77069996..8a34189c 100644 --- a/tests/AgentOs.Tests/Sessions/ScopedWriteGuardEgressTests.cs +++ b/tests/AgentOs.Tests/Sessions/ScopedWriteGuardEgressTests.cs @@ -260,9 +260,19 @@ public void CheckCommand_PowerShellToAnotherComputer_IsRefused(string command) [InlineData("cargo install --path .")] [InlineData("cargo build --target-dir ../../shared-target")] [InlineData("CARGO_TARGET_DIR=../../shared-target cargo build")] - [InlineData("cargo build --artifact-dir=/tmp/out")] public void CheckCommand_CargoWritesOutsideTheRunFolder_AreRefused(string command) => Bash(command).ShouldNotBeNull(command); + [Fact] + public void CheckCommand_CargoArtifactDirInGitBashTmp_IsTheRunsTempFolder() + { + var verdict = Bash("cargo build --artifact-dir=/tmp/out"); + + // Windows: Git Bash's /tmp is the CLI's TEMP = the run's temp folder (a writable root). Elsewhere /tmp is the + // shared one — outside the run (the CLI's TMPDIR is the run's temp folder). + if (OperatingSystem.IsWindows()) { verdict.ShouldBeNull(); } + else { verdict.ShouldNotBeNull(); } + } + [Fact] public void CheckCommand_CargoTargetDirInside_IsAllowed() => Bash("CARGO_TARGET_DIR=target/ci cargo test").ShouldBeNull(); diff --git a/tests/AgentOs.Tests/Tenants/AuditAndInvitationsRealPostgresTests.cs b/tests/AgentOs.Tests/Tenants/AuditAndInvitationsRealPostgresTests.cs new file mode 100644 index 00000000..1cd3a30d --- /dev/null +++ b/tests/AgentOs.Tests/Tenants/AuditAndInvitationsRealPostgresTests.cs @@ -0,0 +1,269 @@ +// REAL-POSTGRES — GAP-11 / 11b through the real stores, as the restricted app role under RLS stage 2 (every context +// here binds its connections exactly like an RLS-enabled host does: SET ROLE agentos_rls + the flow's scope): +// • the audit chain: sequential AND concurrent writers for one tenant produce one gap-free chain that verifies intact; +// "_platform" rows chain separately; a flow bound to tenant B reads none of A's events; a row changed or deleted +// behind the app's back (by the database owner) is reported at its position, and so is a row the app role inserts +// with no chain link after the chain started; +// • invitations: the row is invisible to another tenant's session; of N concurrent accepts exactly one claims it; a +// released claim can be taken again; a used invitation can't be revoked; the whole service flow on the EF store +// refuses a second use and the wrong address. +// Gate: RUN_AGENTOS_E2E_REAL=true and AGENTOS_PG_CONN (a server the test may CREATE / DROP DATABASE on). Zero residue. + +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using AgentOs.Modules.Tenants; +using AgentOs.Modules.Tenants.Invitations; +using AgentOs.Modules.Tenants.Keycloak; +using AgentOs.Modules.Tenants.Persistence; +using AgentOs.Modules.Tenants.Persistence.Repositories; +using AgentOs.SharedKernel.Identity; +using AgentOs.SharedKernel.Persistence; +using AgentOs.Tests.Persistence; +using Microsoft.AspNetCore.DataProtection; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.Extensions.Logging.Abstractions; +using Npgsql; +using NSubstitute; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Tenants; + +public sealed class AuditAndInvitationsRealPostgresTests +{ + private const string A = "chain-tenant-a"; + private const string B = "chain-tenant-b"; + + [Fact] + public async Task AuditChain_OnRealPostgres_AsTheAppRole_IsGapFree_TenantBound_AndTamperEvident() + { + var server = RowLevelSecurityStageTwoRealPostgresTests.ServerConnection ?? string.Empty; + if (server.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } + + await WithTenantsDatabaseAsync(server, async (cs, options) => + { + for (var i = 0; i < 3; i++) { await WriteAsync(options, Entry(A, $"sequential {i}")); } + await Task.WhenAll(Enumerable.Range(0, 12).Select(i => Task.Run(() => WriteAsync(options, Entry(A, $"concurrent {i}"))))); + await WriteAsync(options, Entry(B, "b1")); + await WriteAsync(options, Entry(B, "b2")); + await WriteAsync(options, Entry(AuditScopes.Platform, "tenant.purged: x")); + + (await VerifyAsync(options, A)).ShouldSatisfyAllConditions( + r => r.Intact.ShouldBeTrue(r.Summary), + r => r.Verified.ShouldBe(15), + r => r.HeadHash.ShouldNotBeNull()); + (await VerifyAsync(options, B)).Verified.ShouldBe(2); + (await VerifyAsync(options, AuditScopes.Platform)).Verified.ShouldBe(1); + (await SequencesAsync(cs, A)).ShouldBe(LongRange(1, 15)); + + // The app path never crosses tenants: a flow bound to B sees none of A's events (RLS, not the WHERE clause). + await using (var db = new TenantsDbContext(options)) + using (AmbientIdentity.Push(B, null)) + { + var log = new EfAuditLog(db, NullLogger.Instance); + (await log.ListAsync(A)).ShouldBeEmpty(); + (await log.ListAsync(B)).Select(e => e.Sequence).ShouldBe([2L, 1L]); + } + + // Tampering behind the app's back (the owner / superuser — the app role can't) is found at its position. + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(cs, + $"UPDATE tenants.audit_events SET \"Target\" = 'rewritten' WHERE \"TenantId\" = '{A}' AND \"Sequence\" = 7"); + (await VerifyAsync(options, A)).ShouldSatisfyAllConditions( + r => r.Problem.ShouldBe(AuditChainProblem.Altered), + r => r.BrokenAt.ShouldBe(7)); + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(cs, + $"DELETE FROM tenants.audit_events WHERE \"TenantId\" = '{B}' AND \"Sequence\" = 1"); + (await VerifyAsync(options, B)).ShouldSatisfyAllConditions( + r => r.Problem.ShouldBe(AuditChainProblem.Missing), + r => r.BrokenAt.ShouldBe(1)); + + // The app role may INSERT: a row with no chain link, added after the platform chain started, breaks it. + await using (var forger = new TenantsDbContext(options)) + using (RowLevelSecurity.PlatformScope("test-forgery")) + { + await forger.Database.ExecuteSqlAsync($""" + INSERT INTO tenants.audit_events ("Id","TenantId","UserId","Action","Target","TimestampUtc") + VALUES ({Guid.NewGuid()}, {AuditScopes.Platform}, 'eve', 'tenant.purged', 'forged', now()) + """); + } + (await VerifyAsync(options, AuditScopes.Platform)).ShouldSatisfyAllConditions( + r => r.Problem.ShouldBe(AuditChainProblem.Inserted), + r => r.BrokenAt.ShouldBe(2)); + }); + } + + [Fact] + public async Task InvitationStore_OnRealPostgres_IsTenantBound_SingleUse_UnderConcurrency() + { + var server = RowLevelSecurityStageTwoRealPostgresTests.ServerConnection ?? string.Empty; + if (server.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } + + await WithTenantsDatabaseAsync(server, async (_, options) => + { + var now = DateTimeOffset.UtcNow; + var id = Guid.NewGuid(); + using (RowLevelSecurity.TenantScope(A)) + { + await using var db = new TenantsDbContext(options); + await new EfInvitationStore(db).AddAsync(new InvitationRecord(id, A, "bob@acme.test", "bob@acme.test", "member", "admin-1", now, now.AddHours(1))); + } + + using (RowLevelSecurity.TenantScope(B)) + { + await using var db = new TenantsDbContext(options); + (await new EfInvitationStore(db).GetAsync(A, id)).ShouldBeNull(); // RLS: B's session can't see A's row + (await new EfInvitationStore(db).TryClaimAsync(A, id, now)).ShouldBeFalse(); + } + + using (RowLevelSecurity.TenantScope(A)) + { + var claims = await Task.WhenAll(Enumerable.Range(0, 8).Select(_ => Task.Run(async () => + { + await using var db = new TenantsDbContext(options); + return await new EfInvitationStore(db).TryClaimAsync(A, id, now); + }))); + claims.Count(won => won).ShouldBe(1); + + await using var store = new TenantsDbContext(options); + var invitations = new EfInvitationStore(store); + await invitations.ReleaseAsync(A, id); // the account creation failed: give it back + (await invitations.GetAsync(A, id))!.StatusAt(now).ShouldBe(InvitationStatus.Pending); + (await invitations.TryClaimAsync(A, id, now)).ShouldBeTrue(); + await invitations.CompleteAsync(A, id, "kc-bob"); + await invitations.ReleaseAsync(A, id); // bound to an account: no longer releasable + var used = await invitations.GetAsync(A, id); + used!.AcceptedUserId.ShouldBe("kc-bob"); + used.StatusAt(now).ShouldBe(InvitationStatus.Accepted); + (await invitations.TryRevokeAsync(A, id, now, "admin-1")).ShouldBeFalse(); + (await invitations.TryClaimAsync(A, id, now.AddMinutes(1))).ShouldBeFalse(); + } + }); + } + + [Fact] + public async Task SignupService_OnRealPostgres_EfStore_RefusesReuseAndTheWrongAddress() + { + var server = RowLevelSecurityStageTwoRealPostgresTests.ServerConnection ?? string.Empty; + if (server.Length == 0) { Assert.Skip("RUN_AGENTOS_E2E_REAL + AGENTOS_PG_CONN required."); } + + await WithTenantsDatabaseAsync(server, async (_, options) => + { + var repo = Substitute.For(); + repo.GetAsync(A, Arg.Any()).Returns(new TenantRecord(A, A, DateTimeOffset.UtcNow)); + var kc = Substitute.For(); + kc.CreateUserAsync(default!, default!, default!, default!, default, default, default).ReturnsForAnyArgs("kc-user"); + var dp = new EphemeralDataProtectionProvider(); + + // Minted by A's admin (a flow bound to A) … + InvitationToken invite; + await using (var adminScope = new TenantsDbContext(options)) + using (AmbientIdentity.Push(A, "admin-1")) + { + invite = await Service(adminScope, repo, kc, dp).CreateInvitationAsync(A, "member", "Bob@Acme.test", TimeSpan.FromHours(1), new AuditActor("admin-1")); + } + + // … opened and used by an anonymous visitor (no tenant of its own): the signed token binds RLS to A. + await using var visitorScope = new TenantsDbContext(options); + var visitor = Service(visitorScope, repo, kc, dp); + (await visitor.PreviewInvitationAsync(invite.Token))!.Status.ShouldBe(InvitationStatus.Pending); + await Should.ThrowAsync(() => visitor.SignupAsync( + new TenantSignupRequest("eve", "Sup3rSecure!Pass", "eve@evil.test", null, null, invite.Token))); + (await visitor.SignupAsync( + new TenantSignupRequest("bob", "Sup3rSecure!Pass", "bob@acme.test", null, null, invite.Token))).TenantId.ShouldBe(A); + var again = await Should.ThrowAsync(() => visitor.SignupAsync( + new TenantSignupRequest("bob2", "Sup3rSecure!Pass", "bob@acme.test", null, null, invite.Token))); + again.Message.ShouldBe(InvitationRules.Describe(InvitationAcceptance.AlreadyUsed)); + await kc.Received(1).CreateUserAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>(), + Arg.Any(), Arg.Any(), Arg.Any()); + + // Everything it did is on A's chain, intact (minted, refused, signup + accepted, refused). + var report = await VerifyAsync(options, A); + report.Intact.ShouldBeTrue(report.Summary); + report.Verified.ShouldBe(5); + }); + } + + // ── helpers ── + + // The service as a request scope wires it: the audit log and the invitation store share the scope's context. + private static TenantSignupService Service(TenantsDbContext db, ITenantsRepository repo, IKeycloakAdminClient kc, IDataProtectionProvider dp) + => new(repo, kc, new EfAuditLog(db, NullLogger.Instance), dp, NullLogger.Instance, + settings: null, new EfInvitationStore(db)); + + private static IEnumerable LongRange(long start, int count) + { + for (var i = 0; i < count; i++) { yield return start + i; } + } + + private static async Task WithTenantsDatabaseAsync(string server, Func, Task> body) + { + var database = "agentos_chain_" + Guid.NewGuid().ToString("N")[..12]; + var admin = new NpgsqlConnectionStringBuilder(server) { Database = "postgres", Pooling = false }.ConnectionString; + var cs = new NpgsqlConnectionStringBuilder(server) { Database = database }.ConnectionString; + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(admin, $"CREATE DATABASE \"{database}\""); + try + { + await using (var db = new TenantsDbContext(RowLevelSecurityStageTwoRealPostgresTests.Options(cs, "tenants"))) + { + await db.Database.MigrateAsync(); + } + await using (var conn = new NpgsqlConnection(cs)) + { + await conn.OpenAsync(); + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(conn, RowLevelSecurity.BootstrapSql(conn.PostgreSqlVersion.Major)); + } + var options = new DbContextOptionsBuilder() + .UseNpgsql(cs, n => n.MigrationsHistoryTable("__EFMigrationsHistory", "tenants")) + .AddInterceptors(new AppRole()) + .Options; + await body(cs, options); + } + finally + { + NpgsqlConnection.ClearAllPools(); + await RowLevelSecurityStageTwoRealPostgresTests.ExecAsync(admin, $"DROP DATABASE IF EXISTS \"{database}\" WITH (FORCE)"); + } + } + + private static AuditEntry Entry(string tenant, string target) + => new(Guid.NewGuid(), tenant, "user-1", AuditActions.SettingChanged, target, "203.0.113.7", "tests", DateTimeOffset.UtcNow); + + private static async Task WriteAsync(DbContextOptions options, AuditEntry entry) + { + await using var db = new TenantsDbContext(options); + await new EfAuditLog(db, NullLogger.Instance).WriteAsync(entry); + } + + private static async Task VerifyAsync(DbContextOptions options, string tenant) + { + await using var db = new TenantsDbContext(options); + return await new EfAuditLog(db, NullLogger.Instance).VerifyAsync(tenant); + } + + private static async Task> SequencesAsync(string cs, string tenant) + { + await using var conn = new NpgsqlConnection(cs); + await conn.OpenAsync(); + await using var cmd = new NpgsqlCommand("SELECT \"Sequence\" FROM tenants.audit_events WHERE \"TenantId\" = @t ORDER BY \"Sequence\"", conn); + cmd.Parameters.AddWithValue("t", tenant); + var list = new List(); + await using var reader = await cmd.ExecuteReaderAsync(); + while (await reader.ReadAsync()) { list.Add(reader.GetInt64(0)); } + return list; + } + + // Binds every connection as an RLS-enabled host does — without flipping the process-wide RowLevelSecurity.State. + private sealed class AppRole : DbConnectionInterceptor + { + public override Task ConnectionOpenedAsync(DbConnection connection, ConnectionEndEventData eventData, CancellationToken cancellationToken = default) + => RowLevelSecurity.ApplySessionAsync((NpgsqlConnection)connection, switchRole: true, cancellationToken); + + public override void ConnectionOpened(DbConnection connection, ConnectionEndEventData eventData) + => RowLevelSecurity.ApplySessionAsync((NpgsqlConnection)connection, switchRole: true).GetAwaiter().GetResult(); + } +} diff --git a/tests/AgentOs.Tests/Tenants/AuditChainTests.cs b/tests/AgentOs.Tests/Tenants/AuditChainTests.cs new file mode 100644 index 00000000..5a961d76 --- /dev/null +++ b/tests/AgentOs.Tests/Tenants/AuditChainTests.cs @@ -0,0 +1,344 @@ +// GAP-11b — the audit hash chain and its verifier, without a database: a chain built the way EfAuditLog builds it +// verifies intact; changing any field of an event, deleting one, swapping two, or re-hashing a changed event is reported +// at the first event that no longer holds; the encoding is unambiguous (moving text between fields changes the hash); +// timestamps are hashed at the microsecond Postgres stores. An unchained row recorded after the chain started (an insert +// that bypassed the writer) breaks the chain at the position it takes; one from before the chain is history. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AgentOs.Modules.Tenants; +using AgentOs.Modules.Tenants.Persistence; +using AgentOs.Modules.Tenants.Persistence.Entities; +using AgentOs.Modules.Tenants.Persistence.Repositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Abstractions; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Tenants; + +public sealed class AuditChainTests +{ + private static readonly DateTimeOffset T0 = new(2026, 11, 3, 8, 30, 15, TimeSpan.Zero); + + [Fact] + public void Verify_ChainAsWritten_IsIntact_WithTheHeadHash() + { + var chain = Chain(5); + + var report = AuditChain.Verify("acme", chain, unchained: 2); + + report.Intact.ShouldBeTrue(); + report.Verified.ShouldBe(5); + report.Unchained.ShouldBe(2); + report.HeadHash.ShouldBe(chain[^1].Hash); + report.BrokenAt.ShouldBeNull(); + report.Summary.ShouldBe("Chain intact — 5 events verified; 2 earlier events predate the chain."); + } + + [Fact] + public void Verify_EmptyChain_SaysThereIsNothingToVerify() + { + var report = AuditChain.Verify("acme", [], unchained: 0); + report.Intact.ShouldBeTrue(); + report.Verified.ShouldBe(0); + report.HeadHash.ShouldBeNull(); + report.Summary.ShouldStartWith("Nothing to verify yet"); + } + + public static TheoryData Fields => ["user", "action", "target", "ip", "agent", "time", "tenant", "id"]; + + [Theory] + [MemberData(nameof(Fields))] + public void Verify_AnyFieldChanged_IsAlteredAtThatEvent(string field) + { + var chain = Chain(5); + var e = chain[2]; + chain[2] = field switch + { + "user" => e with { UserId = "someone-else" }, + "action" => e with { Action = "member.invited" }, + "target" => e with { Target = e.Target + "!" }, + "ip" => e with { IpAddress = "10.9.9.9" }, + "agent" => e with { UserAgent = null }, + "time" => e with { TimestampUtc = e.TimestampUtc.AddSeconds(1) }, + "tenant" => e with { TenantId = "other" }, + _ => e with { Id = Guid.NewGuid() }, + }; + + var report = AuditChain.Verify("acme", chain, 0); + + report.Intact.ShouldBeFalse(); + report.Problem.ShouldBe(AuditChainProblem.Altered); + report.BrokenAt.ShouldBe(3); + report.BrokenEventId.ShouldBe(chain[2].Id); + report.Verified.ShouldBe(2); + report.HeadHash.ShouldBeNull(); + report.Summary.ShouldBe("Chain broken at event 3: its content no longer matches its hash — it was changed after it was written."); + } + + // Rewriting an event AND its own hash is caught by the next event's link. + [Fact] + public void Verify_ChangedEventRehashed_IsRelinkedAtTheNextEvent() + { + var chain = Chain(5); + var changed = chain[2] with { Target = "rewritten" }; + chain[2] = changed with { Hash = AuditChain.Compute(changed) }; + + var report = AuditChain.Verify("acme", chain, 0); + + report.Problem.ShouldBe(AuditChainProblem.Relinked); + report.BrokenAt.ShouldBe(4); + report.Summary.ShouldBe("Chain broken at event 4: it doesn't follow event 3 — an earlier event was changed or replaced."); + } + + [Fact] + public void Verify_EventDeletedInTheMiddle_IsMissing() + { + var chain = Chain(5); + chain.RemoveAt(1); // event 2 + + var report = AuditChain.Verify("acme", chain, 0); + + report.Problem.ShouldBe(AuditChainProblem.Missing); + report.BrokenAt.ShouldBe(2); + report.BrokenEventId.ShouldBeNull(); + report.Verified.ShouldBe(1); + report.Summary.ShouldBe("Chain broken at event 2: it is missing — an event was deleted."); + } + + [Fact] + public void Verify_FirstEventDeleted_IsMissingAtOne() + => AuditChain.Verify("acme", Chain(3).Skip(1), 0).BrokenAt.ShouldBe(1); + + [Fact] + public void Verify_TwoEventsSwapped_IsReported() + { + var chain = Chain(4); + (chain[1], chain[2]) = (chain[2], chain[1]); + + var report = AuditChain.Verify("acme", chain, 0); + + report.Intact.ShouldBeFalse(); + report.BrokenAt.ShouldBe(2); + } + + // Known limit (threat model MT-3): deleting the newest events is not visible to the chain alone — the head hash the + // verifier shows is what an operator records elsewhere to catch it. + [Fact] + public void Verify_TailTruncated_StillIntact_ButTheHeadMoves() + { + var chain = Chain(5); + var full = AuditChain.Verify("acme", chain, 0); + var truncated = AuditChain.Verify("acme", chain.Take(3), 0); + + truncated.Intact.ShouldBeTrue(); + truncated.HeadHash.ShouldNotBe(full.HeadHash); + } + + [Fact] + public async Task VerifyAsync_MatchesVerify() + { + var chain = Chain(6); + chain[4] = chain[4] with { Action = "x" }; + + var streamed = await AuditChain.VerifyAsync("acme", Stream(chain), 1); + + streamed.ShouldBe(AuditChain.Verify("acme", chain, 1)); + } + + // A forged row with no chain link, recorded after the newest event: the chain is broken at the next position. + [Fact] + public void Verify_UnchainedEventAfterTheChain_IsInsertedAtTheNextPosition() + { + var chain = Chain(5); + var stray = new AuditChainStray(Guid.NewGuid(), chain[^1].TimestampUtc.AddMinutes(3)); + + var report = AuditChain.Verify("acme", chain, unchained: 1, stray); + + report.Intact.ShouldBeFalse(); + report.Problem.ShouldBe(AuditChainProblem.Inserted); + report.BrokenAt.ShouldBe(6); + report.BrokenEventId.ShouldBe(stray.Id); + report.Verified.ShouldBe(5); + report.HeadHash.ShouldBeNull(); + report.Summary.ShouldBe("Chain broken at event 6: an event without a chain link was recorded there — it was added outside the audit log."); + } + + // Recorded between events 2 and 3: reported at 3, before event 3 is even checked. + [Fact] + public void Verify_UnchainedEventInsideTheChain_IsInsertedWhereItSitsInTime() + { + var chain = Chain(5); + var stray = new AuditChainStray(Guid.NewGuid(), chain[1].TimestampUtc.AddSeconds(30)); + + var report = AuditChain.Verify("acme", chain, unchained: 1, stray); + + report.Problem.ShouldBe(AuditChainProblem.Inserted); + report.BrokenAt.ShouldBe(3); + report.Verified.ShouldBe(2); + } + + // Same instant as the first event: the chain had started, so it is not history. + [Fact] + public void Verify_UnchainedEventAtTheFirstEventsInstant_IsInserted() + => AuditChain.Verify("acme", Chain(3), 1, new AuditChainStray(Guid.NewGuid(), Chain(1)[0].TimestampUtc)).BrokenAt.ShouldBe(2); + + // An earlier break still wins: the report names the first position that doesn't hold. + [Fact] + public void Verify_EarlierBreak_IsReportedBeforeALaterStray() + { + var chain = Chain(5); + chain[1] = chain[1] with { Target = "rewritten" }; + + var report = AuditChain.Verify("acme", chain, 1, new AuditChainStray(Guid.NewGuid(), chain[^1].TimestampUtc.AddMinutes(1))); + + report.Problem.ShouldBe(AuditChainProblem.Altered); + report.BrokenAt.ShouldBe(2); + } + + [Fact] + public void Verify_UnchainedEventBeforeTheChain_IsHistory_StillIntact() + { + var chain = Chain(3); + var report = AuditChain.Verify("acme", chain, 1, new AuditChainStray(Guid.NewGuid(), chain[0].TimestampUtc.AddDays(-1))); + + report.Intact.ShouldBeTrue(); + report.Verified.ShouldBe(3); + } + + [Fact] + public async Task VerifyAsync_WithAStray_MatchesVerify() + { + var chain = Chain(4); + var stray = new AuditChainStray(Guid.NewGuid(), chain[2].TimestampUtc.AddSeconds(1)); + + (await AuditChain.VerifyAsync("acme", Stream(chain), 1, stray)).ShouldBe(AuditChain.Verify("acme", chain, 1, stray)); + } + + // Through the store: legacy rows from before the chain stay history; a row inserted with no link after the chain + // started — what the app role's INSERT right allows — makes Verify report the break. + [Fact] + public async Task EfAuditLog_Verify_UnchainedRowAfterTheChainStarted_IsReportedBroken() + { + var options = new DbContextOptionsBuilder().UseInMemoryDatabase($"audit-stray-{Guid.NewGuid()}").Options; + await using (var db = new TenantsDbContext(options)) + { + db.AuditEvents.Add(Unchained(T0.AddDays(-30))); // recorded before the chain existed + await db.SaveChangesAsync(); + var log = new EfAuditLog(db, NullLogger.Instance); + for (var i = 1; i <= 3; i++) + { + await log.WriteAsync(new AuditEntry(Guid.NewGuid(), "acme", "u", AuditActions.SettingChanged, $"t{i}", null, null, T0.AddMinutes(i))); + } + (await log.VerifyAsync("acme")).ShouldSatisfyAllConditions( + r => r.Intact.ShouldBeTrue(r.Summary), + r => r.Verified.ShouldBe(3), + r => r.Unchained.ShouldBe(1)); + } + + var forged = Unchained(T0.AddMinutes(10)); + await using (var db = new TenantsDbContext(options)) + { + db.AuditEvents.Add(forged); + await db.SaveChangesAsync(); + (await new EfAuditLog(db, NullLogger.Instance).VerifyAsync("acme")).ShouldSatisfyAllConditions( + r => r.Problem.ShouldBe(AuditChainProblem.Inserted), + r => r.BrokenAt.ShouldBe(4), + r => r.BrokenEventId.ShouldBe(forged.Id), + r => r.Summary.ShouldStartWith("Chain broken at event 4")); + } + } + + private static AuditEventEntity Unchained(DateTimeOffset at) => new() + { + Id = Guid.NewGuid(), TenantId = "acme", UserId = "u", Action = AuditActions.MemberRemoved, Target = "forged", TimestampUtc = at, + }; + + // Length-prefixed encoding: the same characters split differently between fields hash differently. + [Fact] + public void Compute_MovingTextBetweenFields_ChangesTheHash() + { + var a = Link(1, AuditChain.Genesis) with { UserId = "ab", Target = "c" }; + var b = a with { UserId = "a", Target = "bc" }; + var c = a with { UserId = null, Target = "abc" }; + var d = a with { UserId = "", Target = "abc" }; + + new[] { a, b, c, d }.Select(AuditChain.Compute).Distinct().Count().ShouldBe(4); + } + + [Fact] + public void Compute_IsStable_AndHex() + { + var link = Link(1, AuditChain.Genesis); + var hash = AuditChain.Compute(link); + hash.ShouldBe(AuditChain.Compute(link)); + hash.Length.ShouldBe(64); + hash.ShouldAllBe(c => "0123456789abcdef".Contains(c)); + } + + // Postgres keeps microseconds: a sub-microsecond difference is the same stored value, so the same hash. + [Fact] + public void Compute_HashesTheTimestampAtMicrosecondPrecision_InUtc() + { + var link = Link(1, AuditChain.Genesis); + AuditChain.Compute(link with { TimestampUtc = link.TimestampUtc.AddTicks(7) }).ShouldBe(AuditChain.Compute(link)); + AuditChain.Compute(link with { TimestampUtc = link.TimestampUtc.ToOffset(TimeSpan.FromHours(7)) }).ShouldBe(AuditChain.Compute(link)); + AuditChain.Compute(link with { TimestampUtc = link.TimestampUtc.AddTicks(10) }).ShouldNotBe(AuditChain.Compute(link)); + AuditChain.Normalize(new DateTimeOffset(638_000_000_000_000_017, TimeSpan.Zero)).UtcTicks.ShouldBe(638_000_000_000_000_010); + } + + [Fact] + public void Clip_KeepsColumnLimits_WithoutSplittingASurrogatePair() + { + EfAuditLog.Clip(null, 5).ShouldBeNull(); + EfAuditLog.Clip("short", 5).ShouldBe("short"); + EfAuditLog.Clip("toolong", 5).ShouldBe("toolo"); + EfAuditLog.Clip("abcd\U0001F600", 5).ShouldBe("abcd"); // the emoji's high surrogate would sit at index 4 + } + + // The writer's row → link mapping is what the verifier reads back: a freshly built row verifies. + [Fact] + public void LinkOf_RowBuiltByTheWriter_Verifies() + { + var row = new AuditEventEntity + { + Id = Guid.NewGuid(), TenantId = "acme", UserId = "u", Action = AuditActions.InvitationMinted, Target = "t", + TimestampUtc = AuditChain.Normalize(T0), Sequence = 1, PrevHash = AuditChain.Genesis, + }; + row.Hash = AuditChain.Compute(EfAuditLog.LinkOf(row)); + + AuditChain.Verify("acme", [EfAuditLog.LinkOf(row)], 0).Intact.ShouldBeTrue(); + } + + // ── helpers ── + + private static List Chain(int count) + { + var links = new List(count); + var previous = AuditChain.Genesis; + for (var i = 1; i <= count; i++) + { + var link = Link(i, previous); + link = link with { Hash = AuditChain.Compute(link) }; + links.Add(link); + previous = link.Hash; + } + return links; + } + + private static AuditChainLink Link(long sequence, string previous) => new( + sequence, Guid.NewGuid(), "acme", T0.AddMinutes(sequence), "user-" + sequence, AuditActions.SettingChanged, + $"Agents:Coding:Model: a → b{sequence}", "203.0.113.7", "Mozilla/5.0", previous, string.Empty); + + private static async IAsyncEnumerable Stream(IEnumerable links) + { + foreach (var link in links) + { + await Task.Yield(); + yield return link; + } + } +} diff --git a/tests/AgentOs.Tests/Tenants/AuditLogBestEffortTests.cs b/tests/AgentOs.Tests/Tenants/AuditLogBestEffortTests.cs new file mode 100644 index 00000000..f2e7a82e --- /dev/null +++ b/tests/AgentOs.Tests/Tenants/AuditLogBestEffortTests.cs @@ -0,0 +1,75 @@ +// IAuditLog's contract: a write is best-effort and NEVER throws to the caller — the surrounding operation (a sign-up, a +// member removal, a settings save) already happened. An entry that names no tenant used to throw out of the writer +// (the RLS tenant scope refuses a blank tenant); it is now refused like a database failure: nothing is stored, the loss +// is logged as an error and counted on agentos.audit.lost{reason=no_tenant}. + +using System; +using System.Collections.Generic; +using System.Diagnostics.Metrics; +using System.Linq; +using System.Threading.Tasks; +using AgentOs.Modules.Tenants; +using AgentOs.Modules.Tenants.Persistence; +using AgentOs.Modules.Tenants.Persistence.Repositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Testing; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Tenants; + +public sealed class AuditLogBestEffortTests +{ + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + public async Task WriteAsync_EntryWithoutATenant_NeverThrows_StoresNothing_LogsAndCountsTheLoss(string? tenant) + { + var lost = new List(); + using var listener = new MeterListener(); + listener.InstrumentPublished = (instrument, l) => + { + if (instrument.Meter.Name == AuditTelemetry.MeterName && instrument.Name == AuditTelemetry.Lost) { l.EnableMeasurementEvents(instrument); } + }; + listener.SetMeasurementEventCallback((_, value, tags, _) => + { + string? reason = null; + foreach (var tag in tags) { if (tag.Key == "reason") { reason = tag.Value as string; } } + lock (lost) { for (var i = 0; i < value; i++) { lost.Add(reason); } } + }); + listener.Start(); + + var options = new DbContextOptionsBuilder().UseInMemoryDatabase($"audit-no-tenant-{Guid.NewGuid()}").Options; + await using var db = new TenantsDbContext(options); + var logger = new FakeLogger(); + var log = new EfAuditLog(db, logger); + var entry = new AuditEntry(Guid.NewGuid(), tenant!, "u", AuditActions.MemberRemoved, "bob", null, null, DateTimeOffset.UtcNow); + + await Should.NotThrowAsync(() => log.WriteAsync(entry, TestContext.Current.CancellationToken)); + + (await db.AuditEvents.CountAsync(TestContext.Current.CancellationToken)).ShouldBe(0); + lock (lost) { lost.ShouldContain(AuditTelemetry.Reasons.NoTenant); } + var record = logger.Collector.GetSnapshot().ShouldHaveSingleItem(); + record.Level.ShouldBe(LogLevel.Error); + record.Message.ShouldContain(entry.Id.ToString()); + } + + // The writer still works for a real tenant right after (nothing was left tracked or half-scoped). + [Fact] + public async Task WriteAsync_AfterARefusedEntry_TheNextEntryIsRecorded() + { + var options = new DbContextOptionsBuilder().UseInMemoryDatabase($"audit-after-refusal-{Guid.NewGuid()}").Options; + await using var db = new TenantsDbContext(options); + var log = new EfAuditLog(db, new FakeLogger()); + + await log.WriteAsync(new AuditEntry(Guid.NewGuid(), "", "u", AuditActions.MemberRemoved, null, null, null, DateTimeOffset.UtcNow), + TestContext.Current.CancellationToken); + await log.WriteAsync(new AuditEntry(Guid.NewGuid(), "acme", "u", AuditActions.MemberRemoved, null, null, null, DateTimeOffset.UtcNow), + TestContext.Current.CancellationToken); + + (await log.ListAsync("acme", ct: TestContext.Current.CancellationToken)).ShouldHaveSingleItem().Sequence.ShouldBe(1); + (await db.AuditEvents.Select(e => e.TenantId).ToListAsync(TestContext.Current.CancellationToken)).ShouldBe(["acme"]); + } +} diff --git a/tests/AgentOs.Tests/Tenants/InvitationRulesTests.cs b/tests/AgentOs.Tests/Tenants/InvitationRulesTests.cs new file mode 100644 index 00000000..35292c1c --- /dev/null +++ b/tests/AgentOs.Tests/Tenants/InvitationRulesTests.cs @@ -0,0 +1,89 @@ +// GAP-11 — the invitation rules as pure functions: state precedence (revoked > used > expired > pending), acceptance only +// for the invited address (case-insensitive, surrounding whitespace ignored), and a distinct plain-language reason for +// every refusal. + +using System; +using System.Linq; +using AgentOs.Modules.Tenants; +using AgentOs.Modules.Tenants.Invitations; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Tenants; + +public sealed class InvitationRulesTests +{ + private static readonly DateTimeOffset Now = new(2026, 11, 3, 12, 0, 0, TimeSpan.Zero); + + [Theory] + [InlineData("bob@acme.test", "bob@acme.test")] + [InlineData(" Bob@Acme.TEST ", "bob@acme.test")] + [InlineData("ÉLISE@Exemple.fr", "élise@exemple.fr")] + [InlineData("", "")] + [InlineData(" ", "")] + [InlineData(null, "")] + public void NormalizeEmail_TrimsAndLowerCases(string? input, string expected) + => InvitationRules.NormalizeEmail(input).ShouldBe(expected); + + [Fact] + public void StatusOf_Pending_BeforeExpiry() + => InvitationRules.StatusOf(Now.AddMinutes(1), null, null, Now).ShouldBe(InvitationStatus.Pending); + + [Fact] + public void StatusOf_Expired_AtAndAfterExpiry() + { + InvitationRules.StatusOf(Now, null, null, Now).ShouldBe(InvitationStatus.Expired); + InvitationRules.StatusOf(Now.AddHours(-1), null, null, Now).ShouldBe(InvitationStatus.Expired); + } + + // A used invitation says "used" even after it expires; a revoked one says "revoked" whatever else happened. + [Fact] + public void StatusOf_Precedence_RevokedThenUsedThenExpired() + { + InvitationRules.StatusOf(Now.AddHours(-1), acceptedAtUtc: Now.AddHours(-2), null, Now).ShouldBe(InvitationStatus.Accepted); + InvitationRules.StatusOf(Now.AddHours(1), Now.AddMinutes(-5), revokedAtUtc: Now.AddMinutes(-1), Now).ShouldBe(InvitationStatus.Revoked); + InvitationRules.StatusOf(Now.AddHours(-1), null, Now.AddHours(-2), Now).ShouldBe(InvitationStatus.Revoked); + } + + [Theory] + [InlineData("bob@acme.test")] + [InlineData("BOB@ACME.TEST")] + [InlineData(" bob@acme.test\t")] + public void CheckAcceptance_PendingForTheInvitedAddress_IsOk(string presented) + => InvitationRules.CheckAcceptance(InvitationStatus.Pending, "bob@acme.test", presented).ShouldBe(InvitationAcceptance.Ok); + + [Theory] + [InlineData("eve@acme.test")] + [InlineData("bob@acme.test.evil")] + [InlineData("bob+x@acme.test")] + [InlineData("bob@acme.tes")] + public void CheckAcceptance_AnotherAddress_IsEmailMismatch(string presented) + => InvitationRules.CheckAcceptance(InvitationStatus.Pending, "bob@acme.test", presented).ShouldBe(InvitationAcceptance.EmailMismatch); + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData(" ")] + public void CheckAcceptance_NoAddress_IsEmailRequired(string? presented) + => InvitationRules.CheckAcceptance(InvitationStatus.Pending, "bob@acme.test", presented).ShouldBe(InvitationAcceptance.EmailRequired); + + // Reuse / revoked / expired are refused whoever asks — even the invited address. + [Theory] + [InlineData(InvitationStatus.Accepted, InvitationAcceptance.AlreadyUsed)] + [InlineData(InvitationStatus.Revoked, InvitationAcceptance.Revoked)] + [InlineData(InvitationStatus.Expired, InvitationAcceptance.Expired)] + public void CheckAcceptance_NotPending_IsRefusedEvenForTheInvitedAddress(InvitationStatus status, InvitationAcceptance expected) + { + InvitationRules.CheckAcceptance(status, "bob@acme.test", "bob@acme.test").ShouldBe(expected); + InvitationRules.ForStatus(status).ShouldBe(expected); + } + + [Fact] + public void Describe_EveryRefusal_HasItsOwnPlainLanguageReason() + { + var refusals = Enum.GetValues().Where(o => o != InvitationAcceptance.Ok).ToList(); + var messages = refusals.Select(InvitationRules.Describe).ToList(); + messages.ShouldAllBe(m => m.Length > 20); + messages.Distinct(StringComparer.Ordinal).Count().ShouldBe(refusals.Count); + } +} diff --git a/tests/AgentOs.Tests/Tenants/OpenSignupAndAzureEndpointTests.cs b/tests/AgentOs.Tests/Tenants/OpenSignupAndAzureEndpointTests.cs index 03bfebe0..db7011c7 100644 --- a/tests/AgentOs.Tests/Tenants/OpenSignupAndAzureEndpointTests.cs +++ b/tests/AgentOs.Tests/Tenants/OpenSignupAndAzureEndpointTests.cs @@ -80,10 +80,11 @@ await Should.ThrowAsync(() => public async Task Signup_WithInvitation_WhenClosed_StillJoins() { var (service, kc) = Service(new FixedSettings("false"), existingTenant: "acme"); - var invite = service.CreateInvitation("acme", "member", null, TimeSpan.FromHours(1)); + var invite = await service.CreateInvitationAsync("acme", "member", "bob@acme.test", TimeSpan.FromHours(1), new AuditActor("admin-1"), + TestContext.Current.CancellationToken); var outcome = await service.SignupAsync( - new TenantSignupRequest("bob", Password, null, null, null, invite.Token), TestContext.Current.CancellationToken); + new TenantSignupRequest("bob", Password, "bob@acme.test", null, null, invite.Token), TestContext.Current.CancellationToken); outcome.Mode.ShouldBe(SignupMode.Invite); outcome.TenantId.ShouldBe("acme"); diff --git a/tests/AgentOs.Tests/Tenants/SignupErrorsTests.cs b/tests/AgentOs.Tests/Tenants/SignupErrorsTests.cs new file mode 100644 index 00000000..95304a99 --- /dev/null +++ b/tests/AgentOs.Tests/Tenants/SignupErrorsTests.cs @@ -0,0 +1,144 @@ +// Public sign-up (POST /tenants/register, the /signup page) talks to an ANONYMOUS caller. Keycloak's raw error body +// (realm internals, password-policy detail, other accounts) used to be echoed straight back as `error`; it now stays in +// the server log and the caller gets a plain product message — while our own validation messages still pass through. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using AgentOs.Modules.Tenants; +using AgentOs.Modules.Tenants.Email; +using AgentOs.Modules.Tenants.Endpoints; +using AgentOs.Modules.Tenants.Keycloak; +using AgentOs.SharedKernel.Identity; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Testing; +using NSubstitute; +using NSubstitute.ExceptionExtensions; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Tenants; + +public sealed class SignupErrorsTests +{ + private const string RawBody = "{\"errorMessage\":\"User exists with same email\",\"field\":\"email\",\"realm\":\"agentic-internal\"}"; + + public static TheoryData IdentityFailures => new() + { + { "conflict", StatusCodes.Status409Conflict, SignupErrors.Taken }, + { "refused", StatusCodes.Status503ServiceUnavailable, SignupErrors.Unavailable }, + { "left-behind", StatusCodes.Status503ServiceUnavailable, SignupErrors.Unavailable }, + { "unreachable", StatusCodes.Status503ServiceUnavailable, SignupErrors.Unreachable }, + }; + + [Theory] + [MemberData(nameof(IdentityFailures))] + public async Task Register_IdentityServiceFails_ReturnsAPlainMessage_KeepsKeycloaksBodyInTheLog(string failure, int status, string message) + { + var (code, body, logs) = await RegisterAsync(Failure(failure)); + + code.ShouldBe(status); + body.ShouldNotContain("agentic-internal"); + body.ShouldNotContain("errorMessage"); + JsonDocument.Parse(body).RootElement.GetProperty("error").GetString().ShouldBe(message); + var logged = logs.ShouldHaveSingleItem(); + logged.Level.ShouldBe(LogLevel.Error); + logged.Exception.ShouldNotBeNull(); + } + + // Our own validation messages are written for the visitor: they still come back as they are, with a 400. + [Fact] + public async Task Register_ValidationMessage_StillComesBack() + { + var (code, body, logs) = await RegisterAsync(new InvalidOperationException("Username is required.")); + + code.ShouldBe(StatusCodes.Status400BadRequest); + JsonDocument.Parse(body).RootElement.GetProperty("error").GetString().ShouldBe("Username is required."); + logs.ShouldBeEmpty(); + } + + [Fact] + public void ForPublic_MapsIdentityFailures_PassesValidationThrough() + { + SignupErrors.ForPublic(Failure("conflict")).ShouldBe(SignupErrors.Taken); + SignupErrors.ForPublic(Failure("refused")).ShouldBe(SignupErrors.Unavailable); + SignupErrors.ForPublic(new KeycloakAdminException("User management requires Keycloak, which isn't configured")).ShouldBe(SignupErrors.Unavailable); + SignupErrors.ForPublic(new TaskCanceledException()).ShouldBe(SignupErrors.Unreachable); + SignupErrors.ForPublic(new InvalidOperationException("Workspace 'acme' already exists.")).ShouldBe("Workspace 'acme' already exists."); + SignupErrors.IsIdentityFailure(new InvalidOperationException("x")).ShouldBeFalse(); + } + + // The client raises the typed exception (status kept for the mapping; the raw body for the log and admins only). + [Fact] + public void Refused_KeepsTheStatus_AndTheDetailForTheLog() + { + var ex = KeycloakAdminException.Refused("create-user", HttpStatusCode.Conflict, RawBody); + + ex.StatusCode.ShouldBe(HttpStatusCode.Conflict); + ex.Message.ShouldContain("create-user failed (409)"); + ex.Message.ShouldContain("agentic-internal"); + ex.ShouldBeAssignableTo(); // administrator-side handlers keep catching it + } + + private static Exception Failure(string kind) => kind switch + { + "conflict" => KeycloakAdminException.Refused("create-user", HttpStatusCode.Conflict, RawBody), + "refused" => KeycloakAdminException.Refused("create-user", HttpStatusCode.BadRequest, RawBody), + "left-behind" => KeycloakUserLeftBehindException.For("kc-user-9", KeycloakAdminException.Refused("grant-roles", HttpStatusCode.InternalServerError, RawBody)), + _ => new HttpRequestException("Connection refused (kc.internal:8443) " + RawBody), + }; + + // The real endpoint delegate (as OpenSignupAndAzureEndpointTests drives it), with a sign-up service that fails. + private static async Task<(int Status, string Body, IReadOnlyList Logs)> RegisterAsync(Exception failure) + { + var signup = Substitute.For(); + signup.SignupAsync(Arg.Any(), Arg.Any()).ThrowsAsync(failure); + var logs = new FakeLogCollector(); + var builder = WebApplication.CreateSlimBuilder(); + builder.Logging.ClearProviders(); + builder.Logging.AddProvider(new FakeLoggerProvider(logs)); + builder.Services.AddSingleton(Substitute.For()); + builder.Services.AddSingleton(Substitute.For()); + builder.Services.AddSingleton(Substitute.For()); + builder.Services.AddSingleton(Substitute.For()); + builder.Services.AddSingleton(Substitute.For()); + builder.Services.AddSingleton(signup); + builder.Services.AddSingleton(new AgentOs.SharedKernel.Hosting.PublicOrigin("https://agentos.example.com", allowRequestFallback: false)); + await using var app = builder.Build(); + app.MapTenantEndpoints(); + var register = ((IEndpointRouteBuilder)app).DataSources.SelectMany(d => d.Endpoints).OfType() + .Single(e => e.Metadata.GetMetadata()?.EndpointName == "TenantsRegister"); + + var request = JsonSerializer.SerializeToUtf8Bytes(new RegisterTenantRequest("acme", "Acme", "alice", "alice@acme.test", "Correct-Horse-9-Battery!", null)); + using var requestBody = new MemoryStream(request); + using var responseBody = new MemoryStream(); + var http = new DefaultHttpContext { RequestServices = app.Services }; + http.Request.Method = "POST"; + http.Request.ContentType = "application/json"; + http.Request.ContentLength = request.Length; + http.Request.Body = requestBody; + http.Response.Body = responseBody; + http.Features.Set(new HasBody()); + + await register.RequestDelegate!(http); + + return (http.Response.StatusCode, Encoding.UTF8.GetString(responseBody.ToArray()), + [.. logs.GetSnapshot().Where(r => r.Category == "AgentOs.Modules.Tenants.Signup")]); + } + + private sealed class HasBody : Microsoft.AspNetCore.Http.Features.IHttpRequestBodyDetectionFeature + { + public bool CanHaveBody => true; + } +} diff --git a/tests/AgentOs.Tests/Tools/ToolOutputShaperTests.cs b/tests/AgentOs.Tests/Tools/ToolOutputShaperTests.cs index 95456341..defa9854 100644 --- a/tests/AgentOs.Tests/Tools/ToolOutputShaperTests.cs +++ b/tests/AgentOs.Tests/Tools/ToolOutputShaperTests.cs @@ -20,6 +20,7 @@ using AgentOs.Modules.AppConfig.Settings; using AgentOs.Modules.Tools; using AgentOs.Modules.Tools.Evidence; +using AgentOs.SharedKernel.Persistence; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging.Abstractions; using Shouldly; @@ -740,7 +741,10 @@ public void Setting_IsDeclaredWithItsDefaultAndBounds() [InlineData("50001", false)] [InlineData("lots", false)] public async Task Setting_WritesOutsideTheRange_AreRejected(string value, bool ok) - => (await Settings().SetAsync(ToolsSettings.MaxOutputTokens, value, SettingScope.Platform, null, "root")).Ok.ShouldBe(ok); + { + using var platform = RowLevelSecurity.PlatformScope("test"); + (await Settings().SetAsync(ToolsSettings.MaxOutputTokens, value, SettingScope.Platform, null, "root")).Ok.ShouldBe(ok); + } // --- helpers --------------------------------------------------------------------------------------------- diff --git a/tests/AgentOs.Tests/Web/SettingsFieldsPlatformWriteGuardTests.cs b/tests/AgentOs.Tests/Web/SettingsFieldsPlatformWriteGuardTests.cs new file mode 100644 index 00000000..523a5229 --- /dev/null +++ b/tests/AgentOs.Tests/Web/SettingsFieldsPlatformWriteGuardTests.cs @@ -0,0 +1,63 @@ +// GAP-14 (PR #326 review): SettingsFields is the one desktop surface that writes settings at platform scope. Two things +// keep a tenant admin from flipping a platform-wide switch through it, and this source guard pins both (there is no +// component-render test stack in this project): +// 1. a read-only row (platform-scope for a tenant admin, or operator-locked) renders NO Save/Reset handler at all — +// Blazor Server dispatches a registered handler for any element the client names, disabled attribute or not; +// 2. the write path proves the platform-admin role from the signed-in principal and only then enters +// RowLevelSecurity.PlatformScope("platform-settings") — the scope ISettings now requires for a platform write. + +using System; +using System.IO; +using System.Text.RegularExpressions; +using Shouldly; +using Xunit; + +namespace AgentOs.Tests.Web; + +public sealed partial class SettingsFieldsPlatformWriteGuardTests +{ + [GeneratedRegex(@"]*OnClick=""\(\) => (Save|Reset)\(f\)""")] + private static partial Regex SaveOrResetButton(); + + [Fact] + public void ReadOnlyRow_RendersNoSaveOrResetHandler() + { + var source = Source(); + var guard = source.IndexOf("@if (!readOnly)", StringComparison.Ordinal); + guard.ShouldBeGreaterThanOrEqualTo(0, "Save/Reset must be inside an `@if (!readOnly)` block"); + + var buttons = SaveOrResetButton().Matches(source); + buttons.Count.ShouldBe(2); + foreach (Match m in buttons) + { + m.Index.ShouldBeGreaterThan(guard, $"{m.Value} is rendered outside the `@if (!readOnly)` block"); + m.Value.ShouldNotContain("readOnly", customMessage: "a disabled button is not a gate — don't render the handler at all"); + } + } + + [Fact] + public void PlatformWrite_ChecksThePrincipalThenEntersThePlatformScope() + { + var source = Source(); + var check = source.IndexOf("scope == SettingScope.Platform && !await IsPlatformAdminAsync()", StringComparison.Ordinal); + var enter = source.IndexOf("RowLevelSecurity.PlatformScope(\"platform-settings\")", StringComparison.Ordinal); + var write = source.IndexOf("Settings.SetAsync(", StringComparison.Ordinal); + + check.ShouldBeGreaterThanOrEqualTo(0, "the write must re-check the signed-in principal's platform-admin role"); + enter.ShouldBeGreaterThan(check, "the platform scope is entered only after the role check"); + write.ShouldBeGreaterThan(enter, "ISettings.SetAsync runs inside the scope the component entered"); + source.ShouldContain("state.User.IsPlatformAdmin()", customMessage: "the proof is the principal's role, not a parameter"); + } + + private static string Source() + => File.ReadAllText(Path.Join(RepoRoot(), "src", "AgentOs.Web", "Components", "UI", "SettingsFields.razor")); + + private static string RepoRoot() + { + for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) + { + if (File.Exists(Path.Join(dir.FullName, "AgentOs.slnx"))) { return dir.FullName; } + } + throw new InvalidOperationException("Repo root (AgentOs.slnx) not found."); + } +}