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.

4.9 KB
Raw Blame
"""Phase 5 — the fusion consumer dead-letters poison messages (never drops).

Before Phase 5 the consumer logged a warning and dropped a malformed message —
silent data loss. These tests drive ``FusionWorker._process_message`` directly
(no Kafka) with an in-memory DLQ and prove: a valid event is processed and the
DLQ stays empty; every poison shape is captured in the DLQ with a reason; and a
DLQ that itself fails does not crash the consumer.
"""

from __future__ import annotations

from types import SimpleNamespace
from unittest.mock import AsyncMock

import pytest
from app.core.config import settings
from app.models.alert import FusionDecision
from app.services.dlq import InMemoryDLQ
from app.workers.consumer import FusionWorker, PermanentProcessingError

from tests.kafka_doubles import TwoStageProducer

pytestmark = pytest.mark.asyncio

TENANT = "00000000-0000-0000-0000-000000000001"
RAW_EVENTS = settings.kafka_topic_raw_events
ALERTS_RAW = settings.kafka_topic_alerts_raw


def _valid_raw_event() -> dict:
    return {
        "tenant_id": TENANT,
        "ocsf_event": {
            "class_uid": 2001,
            "category_uid": 2,
            "severity_id": 5,
            "message": "Credential dumping",
            "metadata": {"uid": "ev-1", "product": {"name": "Falcon", "vendor_name": "CrowdStrike"}},
        },
    }


def _worker_with_dlq() -> tuple[FusionWorker, InMemoryDLQ]:
    dlq = InMemoryDLQ()
    engine = SimpleNamespace(
        process=AsyncMock(
            return_value=SimpleNamespace(
                id="fused-1",
                fusion_decision=FusionDecision.NEW_ALERT,
                model_dump=lambda mode="json": {"id": "fused-1"},
            )
        )
    )
    worker = FusionWorker(engine=engine, sink=None, dlq=dlq)
    # Not an AsyncMock: its `send` cannot distinguish an enqueue from a
    # delivery, so a publish that never leaves the process passes. See
    # tests.kafka_doubles.TwoStageProducer.
    worker._producer = TwoStageProducer()  # noqa: SLF001 — test drives the internals
    return worker, dlq


async def test_valid_event_is_processed_and_not_dead_lettered():
    worker, dlq = _worker_with_dlq()
    await worker._process_message(_valid_raw_event(), topic=RAW_EVENTS)  # noqa: SLF001
    assert dlq.records == []
    worker._engine.process.assert_awaited_once()  # noqa: SLF001
    assert len(worker._producer.enqueued) == 1  # noqa: SLF001
    assert worker._producer.futures[0].done(), "published without waiting for the broker"  # noqa: SLF001
    await worker._producer.drain()  # noqa: SLF001


async def test_missing_ocsf_event_is_dead_lettered_not_dropped():
    worker, dlq = _worker_with_dlq()
    await worker._process_message({"tenant_id": TENANT}, topic=RAW_EVENTS)  # noqa: SLF001
    assert len(dlq.records) == 1
    assert "ocsf_event" in dlq.records[0].reason
    worker._engine.process.assert_not_awaited()  # noqa: SLF001


async def test_unknown_schema_version_is_dead_lettered():
    worker, dlq = _worker_with_dlq()
    poison = _valid_raw_event()
    poison["schema_version"] = "v99"
    await worker._process_message(poison, topic=RAW_EVENTS)  # noqa: SLF001
    assert len(dlq.records) == 1
    assert "unknown schema_version" in dlq.records[0].reason


async def test_non_uuid_tenant_is_dead_lettered_with_lineage():
    worker, dlq = _worker_with_dlq()
    poison = _valid_raw_event()
    poison["tenant_id"] = "not-a-uuid"
    poison["ocsf_event"].pop("tenant_uid", None)
    await worker._process_message(poison, topic=RAW_EVENTS)  # noqa: SLF001
    assert len(dlq.records) == 1
    assert dlq.records[0].source_event_id == "ev-1"


async def test_invalid_raw_alert_is_dead_lettered_not_silently_dropped():
    worker, dlq = _worker_with_dlq()
    # Missing every required RawAlert field — deep Pydantic validation fails.
    await worker._process_message({"tenant_id": TENANT, "nonsense": True}, topic=ALERTS_RAW)  # noqa: SLF001
    assert len(dlq.records) == 1
    assert "RawAlert validation failed" in dlq.records[0].reason
    worker._engine.process.assert_not_awaited()  # noqa: SLF001


async def test_failed_dead_letter_is_not_reported_as_handled():
    """A poison message whose capture failed must not let the offset commit.

    The old contract returned cleanly when the DLQ raised. The consume loop
    read that as success and committed the offset, so the poison message
    existed only in a log line. The capture failure now propagates as a
    permanent processing failure; `_handle_message` holds the message
    uncommitted until a DLQ accepts it (or shutdown).
    """

    class ExplodingDLQ:
        async def record(self, dead_letter):  # noqa: ANN001, ARG002
            raise RuntimeError("dlq sink down")

    engine = SimpleNamespace(process=AsyncMock())
    worker = FusionWorker(engine=engine, sink=None, dlq=ExplodingDLQ())
    worker._producer = AsyncMock()  # noqa: SLF001
    with pytest.raises(PermanentProcessingError):
        await worker._process_message({"tenant_id": TENANT}, topic=RAW_EVENTS)  # noqa: SLF001