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.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
fix(connectors): heal array-shaped connector_config at checkpoint write (#1536)
alexmateescuba9774d5cfab
diff --git a/services/connectors/app/db/connector_repo.py b/services/connectors/app/db/connector_repo.pyindex 9dc661495..c7bc27931 100644--- a/services/connectors/app/db/connector_repo.py+++ b/services/connectors/app/db/connector_repo.py@@ -32,6 +32,7 @@ from sqlalchemy import (Table,Text,bindparam,+ case,func,literal_column,select,@@ -401,7 +402,38 @@ async def record_checkpoint(patch = bindparam("checkpoint_patch", {"checkpoint": checkpoint}, JSONB, unique=True)# A literal rather than another `cast(...)` bind, for the same reason.empty = literal_column("'{}'::jsonb")- merged = func.coalesce(connectors_table.c.connector_config, empty).op("||")(patch)+ config = connectors_table.c.connector_config+ # Heal the array a previous release left behind, at write time.+ #+ # ``_recover_connector_config`` recovers the array shape on *read*, but it+ # never lands in the column: every ``||`` merge below would see the raw+ # array and append the patch as one more element instead of setting the+ # ``checkpoint`` key, so a connector upgraded past the fix kept its row+ # corrupt forever — and the first API that validates the column strictly+ # (``ConnectorResponse.connector_config: dict``) answers 500 for the whole+ # connectors list, not just this row.+ #+ # The fold mirrors the read-side recovery: object elements merge left to+ # right (later wins), double-encoded string elements are decoded and+ # merged too, anything else is dropped. A hand-mangled string that starts+ # with ``{`` but does not parse still raises — inside the scheduler's+ # best-effort caller, which is the same failure shape as before: a failed+ # checkpoint write costs a re-scan, never corruption.+ heal_array = literal_column(+ "(SELECT coalesce(jsonb_object_agg(kv.key, kv.value ORDER BY el.ord), '{}'::jsonb) "+ "FROM jsonb_array_elements(connectors.connector_config) WITH ORDINALITY AS el(v, ord), "+ "LATERAL jsonb_each(CASE "+ "WHEN jsonb_typeof(el.v) = 'object' THEN el.v "+ "WHEN jsonb_typeof(el.v) = 'string' AND left(btrim(el.v #>> '{}'), 1) = '{' "+ "THEN btrim(el.v #>> '{}')::jsonb "+ "ELSE '{}'::jsonb END) AS kv(key, value))"+ )+ base = case(+ (config.is_(None), empty),+ (func.jsonb_typeof(config) == "array", heal_array),+ else_=config,+ )+ merged = base.op("||")(patch)stmt = update(connectors_table).where(connectors_table.c.id == connector_id).values(connector_config=merged, updated_at=now)await connection.execute(stmt)diff --git a/tests/isolation/test_checkpoint_array_heal_live.py b/tests/isolation/test_checkpoint_array_heal_live.pynew file mode 100644index 000000000..2faeab7e0--- /dev/null+++ b/tests/isolation/test_checkpoint_array_heal_live.py@@ -0,0 +1,155 @@+"""A checkpoint write heals the array a previous release left in the column.++The array shape was produced by the double-encoded ``record_checkpoint`` bind+(and kept alive by every later ``||`` merge, which appends to an array rather+than setting a key). ``_recover_connector_config`` recovers it on *read*, but+the column itself never healed: an upgraded deployment stayed corrupt in+Postgres, each poll appended one more element, and any consumer that+validates ``connector_config`` strictly (``ConnectorResponse``) answered 500+for the whole connectors list.++These tests run ``record_checkpoint`` against real Postgres and assert on the+raw column — a fake engine that records the dict it was handed cannot observe+either the append or the heal.+"""++from __future__ import annotations++import json+import os+import uuid+from typing import Any++import pytest+import pytest_asyncio++pytestmark = [+ pytest.mark.asyncio(loop_scope="module"),+ pytest.mark.skipif(+ not os.environ.get("ISOLATION_CONNECTOR_DSN", "").strip(),+ reason="ISOLATION_CONNECTOR_DSN is not set; this suite needs live infrastructure",+ ),+]+++def _dsn() -> str:+ value = os.environ.get("ISOLATION_CONNECTOR_DSN", "").strip()+ if not value:+ pytest.skip("ISOLATION_CONNECTOR_DSN is not set; this suite needs a live Postgres")+ return value+++@pytest_asyncio.fixture(scope="module")+async def engine(): # noqa: ANN201+ from sqlalchemy.ext.asyncio import create_async_engine++ created = create_async_engine(_dsn(), future=True)+ yield created+ await created.dispose()+++@pytest_asyncio.fixture()+async def connector_row(engine): # noqa: ANN001, ANN201+ """A connector row, with teardown. ``config`` seeds connector_config raw."""+ import sqlalchemy++ tenant_id = uuid.uuid4()+ connector_id = uuid.uuid4()+ async with engine.begin() as conn:+ await conn.execute(+ sqlalchemy.text("INSERT INTO tenants (id, name, slug) VALUES (:i, :n, :s) ON CONFLICT DO NOTHING"),+ {"i": tenant_id, "n": f"h-{tenant_id.hex[:8]}", "s": f"h-{tenant_id.hex[:8]}"},+ )+ yield {"tenant_id": tenant_id, "connector_id": connector_id}+ async with engine.begin() as conn:+ await conn.execute(+ sqlalchemy.text("DELETE FROM connectors WHERE tenant_id = :t"),+ {"t": tenant_id},+ )+ await conn.execute(+ sqlalchemy.text("DELETE FROM tenants WHERE id = :i"),+ {"i": tenant_id},+ )+++async def _seed_config(engine, row: dict[str, Any], config: Any) -> None: # noqa: ANN001+ import sqlalchemy++ async with engine.begin() as conn:+ await conn.execute(+ sqlalchemy.text(+ "INSERT INTO connectors "+ "(id, tenant_id, connector_type, name, is_enabled, auth_config, connector_config) "+ "VALUES (:i, :t, 'splunk', 'heal-me', true, :a, :c)"+ ),+ {+ "i": row["connector_id"],+ "t": row["tenant_id"],+ "a": json.dumps({"token": "***"}),+ "c": json.dumps(config),+ },+ )+++async def _raw_config(engine, row: dict[str, Any]) -> Any: # noqa: ANN001+ import sqlalchemy++ async with engine.begin() as conn:+ result = await conn.execute(+ sqlalchemy.text("SELECT connector_config::text FROM connectors WHERE id = :i"),+ {"i": row["connector_id"]},+ )+ return json.loads(result.scalar_one())+++async def test_checkpoint_heals_legacy_array_column(engine, connector_row) -> None: # noqa: ANN001+ """The exact shape the old binder produced is rewritten to an object."""+ from app.db.connector_repo import record_checkpoint++ legacy = [+ {"poll_interval_seconds": 120, "filter_rules": ["a"]},+ json.dumps({"checkpoint": {"time": "2026-10-27T17:45:00Z", "id": "old"}}),+ {"checkpoint": {"time": "2026-10-27T17:58:00Z", "id": "newer"}},+ ]+ await _seed_config(engine, connector_row, legacy)++ new_checkpoint = {"time": "2026-10-27T20:00:00Z", "id": "healed"}+ async with engine.begin() as conn:+ await record_checkpoint(conn, connector_row["connector_id"], new_checkpoint)++ config = await _raw_config(engine, connector_row)+ assert isinstance(config, dict), f"column must be an object after the write, got {type(config)}"+ # Everything from every object element survives the fold, and the fresh+ # checkpoint wins over the recovered ones.+ assert config["poll_interval_seconds"] == 120+ assert config["filter_rules"] == ["a"]+ assert config["checkpoint"] == new_checkpoint+++async def test_checkpoint_on_clean_object_is_unchanged_shape(engine, connector_row) -> None: # noqa: ANN001+ """The heal must not disturb the healthy path: object in, object out."""+ from app.db.connector_repo import record_checkpoint++ await _seed_config(engine, connector_row, {"poll_interval_seconds": 300})++ checkpoint = {"seen": ["m1", "m2"]}+ async with engine.begin() as conn:+ await record_checkpoint(conn, connector_row["connector_id"], checkpoint)++ config = await _raw_config(engine, connector_row)+ assert config == {"poll_interval_seconds": 300, "checkpoint": checkpoint}+++async def test_heal_is_stable_across_repeated_polls(engine, connector_row) -> None: # noqa: ANN001+ """Two polls after a heal must not grow the column — the appending stops."""+ from app.db.connector_repo import record_checkpoint++ await _seed_config(engine, connector_row, {"poll_interval_seconds": 60})+ for cursor in ("c1", "c2"):+ async with engine.begin() as conn:+ await record_checkpoint(conn, connector_row["connector_id"], {"id": cursor})++ config = await _raw_config(engine, connector_row)+ assert isinstance(config, dict)+ assert config["checkpoint"] == {"id": "c2"}+ assert sorted(config.keys()) == ["checkpoint", "poll_interval_seconds"]