Files
AiSOC/services/api/tests/test_kill_switch.py
T
Beenu AroraandAgustin Torres 1f86648d99 fix(kill-switch): make the platform-wide stop engageable, and give it a product surface (#1534)
* test(kill-switch): prove the platform-wide stop cannot be engaged

Two failing suites, both for the reason the fix has to address.

tests/isolation/test_kill_switch_global_live.py drives a real Postgres as
the aisoc_app runtime role, which is the only role that can see this: 061
makes it NOSUPERUSER NOBYPASSRLS and both tables are FORCE ROW LEVEL
SECURITY, so seeding the global row as the migration role succeeds and
proves nothing. The suite fails its own run rather than skipping when the
DSN resolves to a role that can walk past a policy.

Against the current tree four assertions fail with

  new row violates row-level security policy for table "aisoc_kill_switch"

because migration 078 judges every write by WITH CHECK (tenant_id =
current_tenant_id()). The global row is tenant_id IS NULL, so an unbound
session compares NULL = NULL and a bound one NULL = '<uuid>'; both are NULL,
and a check that is not true refuses the row. The tenant-scoped assertions
and all four negative properties already pass, which is the point: the
working half must not regress.

services/api/tests/test_kill_switch.py covers the half that was not
reported. Every write in the endpoint module binds t=user.tenant_id, so no
endpoint, console control, CLI or script could set tenant_id IS NULL, while
GET /kill-switch and the agents-side kill_switch_engaged both read the
global row on every alert. Nine assertions fail: the routes answer 404.

The permission constant is read with a default so a tree without the surface
fails each assertion with its own reason instead of aborting collection.

* fix(kill-switch): make the platform-wide stop engageable, and give it a surface

The defect had two halves and only the first was reported.

The policy. Migration 078 judges every write to aisoc_kill_switch by
WITH CHECK (tenant_id = current_tenant_id()). The global row is tenant_id
IS NULL, so an unbound session compares NULL = NULL and a bound one
NULL = '<uuid>'; both are NULL and a check that is not true refuses the row.
The table's own comment says the global row is written "through a session
with no tenant set", which is the one case it rejects.

Migration 123 expresses the distinction a global row legitimately has
instead of dropping the check:

    WITH CHECK (tenant_id = current_tenant_id()
                OR (tenant_id IS NULL AND current_tenant_id() IS NULL))

Three of the five session/row combinations were already refused by 078 and
stay refused. The only behaviour that changes is that an unbound session
gains the global row — and nothing else. A tenant still cannot write the
global row, so no tenant administrator gains a switch over anybody else,
and an unbound session still cannot write into a tenant. aisoc_kill_switch
_audit carries the same check and the same fix: a switch that could be
thrown but not audited is a different defect, not a smaller one.

The surface, which is the half that was not reported. Every write in the
endpoint module bound t=user.tenant_id, so no endpoint, console control, CLI
or script could set tenant_id IS NULL, while GET /kill-switch and the
agents-side kill_switch_engaged read the global row on every alert. A reader
over a table nothing writes is not a control.

POST /kill-switch/global/{engage,release} are that surface. They take the
unbound session, which is the inverse of the tenant routes and load-bearing:
123 admits the global row only when current_tenant_id() IS NULL, so a
tenant-scoped session is exactly the one that cannot write it. The resolved
dependency is asserted rather than left to the comment, because moving these
onto TenantDBSession is the repair the tenant routes needed and would break
these.

Authorization is platform_kill_switch:write, not settings:write. A tenant
administrator holds settings:write, and this switch stops every other tenant
on the deployment. No tenant-scoped role is granted it and only the wildcard
roles hold it — the decision workload_identities:* already records for the
same reason. 123 seeds it into the catalog, because 092 has run everywhere
and a new permission reaches an existing deployment only through a new file.

Live evidence, as the NOSUPERUSER NOBYPASSRLS aisoc_app role against
postgres:16 with all 136 migrations applied:

  tests/isolation/test_kill_switch_global_live.py      11 passed (was 4F/7P)
  tests/isolation/test_kill_switch_endpoint_live.py    10 passed
  tests/isolation/test_kill_switch_closure_live.py      4 passed
  services/api tests/                                4974 passed, 164 skipped

The closure suite is the one that matters for "something reaches it": an
engaged global switch makes resolve_closure_policy refuse a 0.99 verdict
against a 0.50 bar with source="kill_switch", and releasing it resumes
closure. kill-switch-live.yml runs all three with no paths filter.

* docs(kill-switch): record the verification, the negative controls and the procedure

docs/remediation/kill-switch-reachability.md carries the executed evidence
for all three steps rather than a description of them, and a claim-matrix
row brings the table to 320 rows / 316 GATED / 4 PARTIAL / 0 NO GATE.

The negative controls, each reverting one thing and leaving the rest:

  A. policy reverted to 078, code intact
       test_kill_switch_global_live.py     4 failed, 7 passed
       test_kill_switch_endpoint_live.py   4 failed, 6 passed
     The authorization cases still passed, which is what isolates this
     control to the policy rather than to the permission.

  B. endpoint reverted, policy intact
       services/api tests/test_kill_switch.py   9 failed, 11 passed
       test_kill_switch_endpoint_live.py        8 failed,  2 passed

  C. policy reverted, closure suite
       test_kill_switch_closure_live.py    4 failed, InsufficientPrivilegeError

A and B each reproduce step 1's failure signature exactly. Restoring each
returned every suite to green.

Two things are recorded as still unreachable rather than implied fixed.
There is no console control for the kill switch in either scope; both are
API-only, which the per-tenant switch has been since 078. And verifying the
module docstring's claim that the switch is checked by "closure in the
triage worker, the actions dispatcher, playbook dispatch and the MCP write
tools" found that only the first is true: services/actions, playbook
dispatch and the MCP write surface contain no reference to the switch in any
form, so an engaged switch stops automatic closure and does not stop
dispatch. That is a second defect of the same family, found while verifying
this one, and it is named rather than fixed — the dispatcher is another
service and the shared reader is owned by an in-flight change.

* docs: re-derive the matrix counts the new row moved

`readme_gates.py` cross-checks every quoted matrix figure against the table
itself, and adding a row left two surfaces behind: ROADMAP.md at 319 rows /
315 GATED and the compliance evidence pack at 315 GATED. Both now read 320 /
316, which the gate confirms against the parsed table rather than against
each other.

This is the failure the gate exists for — a count copied into prose goes
stale in silence — and it caught it on the first CI run rather than in a
later audit, which is the argument for having it.

* chore(openapi): export the two platform kill-switch operations

`scripts/export_openapi.py --check` compares the committed spec against what
the app serves, and two new routes make it stale. Regenerated rather than
hand-edited: 72 insertions, no deletions, confined to
/api/v1/kill-switch/global/engage and /release.

No deletions is the part worth checking. The spec is generated from a venv,
so anything that drifted into that venv lands in the artefact — a pydantic
moved by an unrelated install once turned a defaulted field from a bare $ref
into allOf and read as a breaking change to a contract nothing had broken.
This export came from the api service's own locked set.

---------

Co-authored-by: Agustin Torres <agustorres@uade.edu.ar>
2026-10-27 15:07:29 -07:00

226 lines
10 KiB
Python

"""The kill switch is reachable, authorized and audited.
Parity plan 2.2. The agents-side half (that an engaged switch actually stops
closure) lives in `services/agents/tests/test_closure_policy.py`, which
drives the resolver. This covers the API surface: that the routes exist,
that writing requires a permission while reading does not, and that the
reason is mandatory.
"""
from __future__ import annotations
import inspect
import pytest
from app.api.v1.endpoints import kill_switch
from app.core.rbac_catalog import PERMISSIONS
from app.core.security import ROLE_PERMISSIONS
from app.db.database import get_db
from app.main import app
from fastapi.testclient import TestClient
#: The permission the platform-wide routes require. Read from the module so
#: the test cannot keep passing against a renamed constant, and defaulted so
#: that a tree without the surface fails each assertion with its own reason
#: rather than aborting collection and taking the rest of this file with it.
PLATFORM_KILL_SWITCH = getattr(kill_switch, "PLATFORM_KILL_SWITCH_PERMISSION", "<no platform kill-switch permission is declared>")
#: One client for the module. Resolution is by **sending a request**, not
#: by walking `app.routes`: that is not the route table and what it holds
#: changes with the FastAPI minor, which made the sibling compliance file
#: pass locally and report every path unreachable in CI.
#:
#: A 401 answers the question as well as a 200 does, because authentication
#: runs after the route has matched.
_CLIENT = TestClient(app, raise_server_exceptions=False)
def _reaches_a_route(path: str, method: str = "GET") -> bool:
return _CLIENT.request(method, path).status_code != 404
def _handler_for(path: str, method: str = "GET") -> str | None:
"""The handler that owns `path`, from the OpenAPI document."""
spec = app.openapi()
operation = spec.get("paths", {}).get(path, {}).get(method.lower())
if not operation:
return None
return operation.get("operationId", "").split("_api_v1_")[0] or None
def _route_object(path: str, method: str):
"""The router's own route, for assertions about the dependency tree.
Taken off ``kill_switch.router`` rather than ``app.routes``: what the
latter holds changes with the FastAPI minor, and a gate that enumerated it
once compared zero pairs and reported success.
"""
for route in kill_switch.router.routes:
if route.path == path and method in set(getattr(route, "methods", set())):
return route
raise AssertionError(f"no route {method} {path}")
def _required_permissions(route) -> set[str]:
"""Every permission the route's dependency tree actually resolves.
``require_permission`` returns a closure, so the permission it enforces
lives in that closure's cells rather than anywhere readable from the
route. Walking the resolved tree is what makes this an assertion about
enforcement instead of about spelling.
"""
found: set[str] = set()
def walk(dependant) -> None:
call = dependant.call
closure = getattr(call, "__closure__", None) or ()
if getattr(call, "__name__", "") == "_check":
found.update(cell.cell_contents for cell in closure if isinstance(cell.cell_contents, str))
for sub in dependant.dependencies:
walk(sub)
walk(route.dependant)
return found
class TestTheRoutesExist:
@pytest.mark.parametrize(
("path", "method", "handler"),
[
("/api/v1/kill-switch", "GET", "read_kill_switch"),
("/api/v1/kill-switch/engage", "POST", "engage"),
("/api/v1/kill-switch/release", "POST", "release"),
],
)
def test_each_route_reaches_its_handler(self, path: str, method: str, handler: str) -> None:
assert _reaches_a_route(path, method), f"{method} {path} answers 404"
assert _handler_for(path, method) == handler
class TestAuthorization:
def test_writing_requires_a_permission(self) -> None:
for name in ("engage", "release"):
source = inspect.getsource(getattr(kill_switch, name))
assert 'require_permission("settings:write")' in source, (
f"{name} does not require a permission, so any authenticated user could freeze or unfreeze the platform"
)
def test_reading_does_not(self) -> None:
"""An analyst watching the queue stop moving has to be able to find
out why without asking an administrator."""
source = inspect.getsource(kill_switch.read_kill_switch)
assert "require_permission" not in source
assert "user: AuthUser" in source, "reading must still be authenticated"
class TestTheReasonIsMandatory:
"""A switch with no reason is one nobody can safely disengage: the next
operator cannot tell a deliberate freeze from a forgotten test."""
def test_engaging_requires_a_reason(self) -> None:
field = kill_switch.EngageRequest.model_fields["reason"]
assert field.is_required()
assert any(getattr(m, "min_length", None) for m in field.metadata), "an empty-ish reason would satisfy a bare required field"
def test_releasing_requires_one_too(self) -> None:
assert kill_switch.ReleaseRequest.model_fields["reason"].is_required()
class TestItIsAudited:
def test_both_transitions_write_an_audit_row(self) -> None:
for name, action in (("engage", "'engage'"), ("release", "'release'")):
source = inspect.getsource(getattr(kill_switch, name))
assert "aisoc_kill_switch_audit" in source, f"{name} writes no audit row"
assert action in source
def test_the_log_line_cannot_be_forged_by_the_reason(self) -> None:
"""The reason is operator-supplied text that lands in a log line."""
assert "\n" not in kill_switch._sanitize("a\nb")
assert "\r" not in kill_switch._sanitize("a\rb")
assert len(kill_switch._sanitize("x" * 5000)) <= 200
class TestThePlatformSwitchHasASurface:
"""The global switch had two readers and no writer.
Every write in this module bound ``t=user.tenant_id``, so the only scope
any caller could reach was their own tenant, while ``GET /kill-switch``
and ``services/agents`` ``kill_switch_engaged`` both consult the global
row on every alert. A reader over a table nothing writes is not a
control, and this one was labelled an emergency stop.
"""
@pytest.mark.parametrize(
("path", "handler"),
[
("/api/v1/kill-switch/global/engage", "engage_global"),
("/api/v1/kill-switch/global/release", "release_global"),
],
)
def test_the_global_routes_exist(self, path: str, handler: str) -> None:
assert _reaches_a_route(path, "POST"), f"POST {path} answers 404, so the platform-wide stop has no product surface"
assert _handler_for(path, "POST") == handler
@pytest.mark.parametrize("verb", ["engage", "release"])
def test_they_require_the_platform_permission(self, verb: str) -> None:
"""Not ``settings:write``. A tenant administrator holds that, and the
global switch stops every *other* tenant on the deployment too.
Read off the **resolved** dependency tree rather than the handler's
source. A substring assertion would pass on
``Annotated[Any, require_permission(...)]`` with no ``Depends()``,
which FastAPI silently drops — that exact shape left eleven routes
reading as gated while enforcing nothing.
"""
required = _required_permissions(_route_object(f"/kill-switch/global/{verb}", "POST"))
assert PLATFORM_KILL_SWITCH in required, (
f"{verb}_global resolves {sorted(required) or 'no permission check'}, not {PLATFORM_KILL_SWITCH}, "
"so it is reachable by a principal whose authority stops at their own tenant"
)
def test_no_tenant_scoped_role_holds_it(self) -> None:
"""The permission is the authorization. If a tenant role held it,
requiring it would be theatre."""
holders = [role for role, perms in ROLE_PERMISSIONS.items() if PLATFORM_KILL_SWITCH in perms]
assert holders == [], (
f"{PLATFORM_KILL_SWITCH} is granted to {holders}, which are tenant-scoped roles. "
"Only the wildcard roles may freeze the whole deployment."
)
wildcard = [role for role, perms in ROLE_PERMISSIONS.items() if "*" in perms]
assert wildcard, "no role holds the wildcard, so nobody could ever engage the platform switch"
def test_it_is_in_the_seeded_catalog(self) -> None:
"""A permission absent from the catalog renders blank on the Roles
screen and cannot be granted to a custom role."""
assert PLATFORM_KILL_SWITCH in {name for name, _d, _c in PERMISSIONS}
@pytest.mark.parametrize("name", ["engage_global", "release_global"])
def test_they_run_on_an_unbound_session(self, name: str) -> None:
"""The inverse of the tenant routes, and load-bearing rather than an
oversight: the global row is admitted by ``WITH CHECK`` only when
``current_tenant_id()`` is NULL, so a tenant-scoped session is exactly
the one that cannot write it.
Asserted on the resolved dependency so it cannot drift into
``get_tenant_db`` the way the tenant routes once drifted out of it.
"""
route = _route_object("/kill-switch/global/" + name.split("_")[0], "POST")
db = next((d.call for d in route.dependant.dependencies if d.name == "db"), None)
assert db is get_db, f"{name} must take the unbound session; a tenant-bound one cannot write the global row"
@pytest.mark.parametrize("name", ["engage_global", "release_global"])
def test_both_transitions_are_audited_as_global(self, name: str) -> None:
source = inspect.getsource(getattr(kill_switch, name))
assert "aisoc_kill_switch_audit" in source, f"{name} writes no audit row"
assert "'global'" in source, f"{name} does not record the scope as global"
class TestReleasingAGlobalSwitch:
def test_a_tenant_cannot_release_the_platform_switch(self) -> None:
"""And is told so, rather than getting a success that changes
nothing. Reporting success over a switch that stays engaged is how
an operator concludes the stop button is broken."""
source = inspect.getsource(kill_switch.release)
assert "409" in source or "HTTP_409_CONFLICT" in source
assert "platform operator" in source