Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(ci): the third grading's residue — keys, casts and credential attribution

beenuarf85823103e29
diff --git a/scripts/check_tenant_query_predicates.py b/scripts/check_tenant_query_predicates.py
index 450f0d16d..d27b3d4c9 100644
--- a/scripts/check_tenant_query_predicates.py
+++ b/scripts/check_tenant_query_predicates.py
@@ -406,6 +406,23 @@ RATCHET: dict[str, str] = {
"the posted-keys read is scoped to one war_room_id — the room decides the tenant, and the "
"unique constraint on (war_room_id, event_key) is what makes the sweep idempotent"
),
+ "services/api/app/services/lake_query_runner.py::claim_statement::LakeQueryJob": (
+ "the claim is one conditional UPDATE by job id where status = 'queued' — a worker claiming "
+ "work enqueued by tenant-scoped routes, the same shape as approval_expiry.run_once. The "
+ "routes that create and read jobs bind the tenant; the runner executes whichever replica won, "
+ "and the policy's unbound-session arm is what admits its GUC-less session"
+ ),
+ "services/api/app/services/lake_query_runner.py::_record_progress::LakeQueryJob": (
+ "progress lands on the claimed job by id; the claim above is what decided whose job it is"
+ ),
+ "services/api/app/services/lake_query_runner.py::_finish::LakeQueryJob": (
+ "terminal state lands on the claimed job by id, guarded so a finished job is never revived; "
+ "same claim-then-act shape as _record_progress"
+ ),
+ "services/api/app/services/lake_query_runner.py::list_stuck_jobs::LakeQueryJob": (
+ "the stuck sweep asks 'which running jobs lost their runner', tenant-unbound by the same "
+ "discover-a-working-set reasoning as due_deliveries; it only moves jobs to a terminal state"
+ ),
"services/api/app/workers/war_room_sync.py::<module>::aisoc_case_war_rooms": (
"the sweep worker's 'which rooms are open' discovery, the same cross-tenant working-set "
"shape as report_scheduler.run_once; sync_war_room then handles one room at a time"
@@ -471,11 +488,14 @@ RATCHET: dict[str, str] = {
# answer link and verification sweeper, 9.4's war-room sweep, decision route
# and worker discovery — each the credential-lookup or discover-a-working-set
# shape already recorded above, verified in code before being recorded. Four
-# more were written for the lake job runner and then deleted again in the same
-# change: fixing lake_query_jobs' WITH CHECK (the unbound-session arm every
-# sibling policy carries) moved those statements onto the RLS-context credit,
-# which this gate's stale-entry check correctly refused to double-count.
-MAX_RATCHET = 50
+# more cover the lake job runner's ORM statements — keyed on the model class
+# (LakeQueryJob), the way every ORM entry here is keyed, which is the detail
+# the first attempt got wrong: keyed on the table name they matched nothing,
+# so the same four entries read as stale and the four statements as unscoped
+# in one run. The policy's unbound-session arm (fixed in the same train) is
+# what admits the runner's GUC-less session at the database; this entry is
+# what records the query-layer decision.
+MAX_RATCHET = 54
# ---------------------------------------------------------------------------
diff --git a/services/api/app/api/v1/endpoints/usage.py b/services/api/app/api/v1/endpoints/usage.py
index 21e23d235..56ff09f71 100644
--- a/services/api/app/api/v1/endpoints/usage.py
+++ b/services/api/app/api/v1/endpoints/usage.py
@@ -311,6 +311,7 @@ async def put_enforcement(
resource_id=str(current_user.tenant_id),
changes={"enforce": requested},
request=request,
+ api_key_prefix=getattr(current_user, "api_key_prefix", None),
)
await db.commit()
return EnforcementPolicy(enforce=requested)
diff --git a/services/api/app/api/v1/endpoints/verification_policies.py b/services/api/app/api/v1/endpoints/verification_policies.py
index 9fd88cacf..7b58d6447 100644
--- a/services/api/app/api/v1/endpoints/verification_policies.py
+++ b/services/api/app/api/v1/endpoints/verification_policies.py
@@ -125,6 +125,7 @@ async def enable_class(
actor_id=current_user.user_id,
actor_email=current_user.email,
changes={"alert_class": body.alert_class},
+ api_key_prefix=getattr(current_user, "api_key_prefix", None),
)
await db.commit()
return VerificationPolicyOut(alert_class=row.alert_class, enabled_by=row.enabled_by, enabled_at=row.enabled_at)
@@ -159,5 +160,6 @@ async def disable_class(
actor_id=current_user.user_id,
actor_email=current_user.email,
changes={"alert_class": folded},
+ api_key_prefix=getattr(current_user, "api_key_prefix", None),
)
await db.commit()
diff --git a/services/api/app/api/v1/endpoints/war_rooms.py b/services/api/app/api/v1/endpoints/war_rooms.py
index 68795f38d..a12070cde 100644
--- a/services/api/app/api/v1/endpoints/war_rooms.py
+++ b/services/api/app/api/v1/endpoints/war_rooms.py
@@ -283,6 +283,10 @@ async def channel_decision(
db=db,
tenant_id=row["tenant_id"],
actor_email=body.actor or None,
+ # The credential on this path is the bot's shared service token —
+ # there is no session and no API key, and None records exactly that.
+ # Which human decided is `actor_email`, as the bot reported it.
+ api_key_prefix=None,
action="war_room:decision",
resource="case",
resource_id=str(row["case_id"]),
diff --git a/tests/isolation/test_verification_pause_live.py b/tests/isolation/test_verification_pause_live.py
index 102de92e9..ef9ecf70b 100644
--- a/tests/isolation/test_verification_pause_live.py
+++ b/tests/isolation/test_verification_pause_live.py
@@ -132,7 +132,7 @@ async def wired(pool: Any, monkeypatch: pytest.MonkeyPatch) -> AsyncIterator[tup
async with pool.acquire() as conn:
# `slug` is NOT NULL since 001_init; derived from the id so two
# module-scoped runs cannot collide on the UNIQUE constraint.
- await conn.execute("INSERT INTO tenants (id, name, slug) VALUES ($1::uuid, 'live', 'live-' || left($1, 8))", tenant_id)
+ await conn.execute("INSERT INTO tenants (id, name, slug) VALUES ($1::uuid, 'live', 'live-' || left($1::text, 8))", tenant_id)
yield playbook_pause, tenant_id
@@ -270,7 +270,9 @@ async def test_the_policy_table_is_tenant_scoped_and_default_deny(pool: object)
tenant_a, tenant_b = str(uuid.uuid4()), str(uuid.uuid4())
async with pool.acquire() as conn: # type: ignore[attr-defined]
await conn.execute(
- "INSERT INTO tenants (id, name, slug) VALUES ($1::uuid, 'a', 'a-' || left($1, 8)), ($2::uuid, 'b', 'b-' || left($2, 8))",
+ "INSERT INTO tenants (id, name, slug) VALUES "
+ "($1::uuid, 'a', 'a-' || left($1::text, 8)), "
+ "($2::uuid, 'b', 'b-' || left($2::text, 8))",
tenant_a,
tenant_b,
)