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(doctor): pre-flight every port CORE publishes, not six of sixteen (#966)

Beenu Arora13226c62a3f6
diff --git a/scripts/doctor.sh b/scripts/doctor.sh
index 5e20456a6..2fb1e9eaf 100755
--- a/scripts/doctor.sh
+++ b/scripts/doctor.sh
@@ -202,9 +202,30 @@ svc_base_url() { # <service> <container-port> <fallback-url>
if [ -n "${_bu_port:-}" ]; then printf 'http://localhost:%s' "$_bu_port"; else printf '%s' "$3"; fi
}
+# Every host port the CORE stack publishes, which is the whole point: `up`
+# calls this before compose so a conflict is named here rather than arriving
+# as `Bind for 127.0.0.1:NNNN failed: port is already allocated` against
+# whichever container lost the race, half a stack later.
+#
+# It used to list six of them, and the gap was not academic. CORE gained
+# Ollama and the LiteLLM gateway, and 11434 was never added — so the one
+# conflict this product's audience is most likely to have, a locally
+# installed Ollama, was the one the pre-flight did not look for. Observed on
+# this machine: `make up` with the host's Ollama holding 11434 passed the
+# port check, and the model the stack pulled was not the model answering on
+# that port.
+#
+# `tests/test_doctor_port_coverage.py` compares this list against the ports
+# docker-compose.yml actually publishes in CORE, in both directions, so the
+# next service added to the profile cannot quietly skip the check.
+#
# host-port service container-port
for spec in "5432 postgres 5432" "6379 redis 6379" "9092 kafka 9092" \
- "8000 api 8000" "8081 ingest-worker 8080" "3000 web 3000"; do
+ "8000 api 8000" "8081 ingest-worker 8080" "9090 ingest-worker 9090" \
+ "3000 web 3000" "4000 litellm 4000" "11434 ollama 11434" \
+ "6333 qdrant 6333" "8001 agents 8084" "8002 actions 8085" \
+ "8003 fusion 8003" "8005 threatintel 8005" "8086 realtime 4000" \
+ "8088 connectors 8003"; do
# shellcheck disable=SC2086 # deliberate word splitting of a fixed 3-field spec
set -- $spec; port="$1"; owner="$2"; cport="$3"
ours="$(svc_published_port "$owner" "$cport" || true)"
diff --git a/tests/test_doctor_port_coverage.py b/tests/test_doctor_port_coverage.py
new file mode 100644
index 000000000..b66646098
--- /dev/null
+++ b/tests/test_doctor_port_coverage.py
@@ -0,0 +1,127 @@
+"""`make doctor` must pre-flight every port CORE actually publishes.
+
+`up` runs `doctor.sh --ports-only` before compose for one reason, written in
+the Makefile: compose reports a conflict as ``Bind for 127.0.0.1:5432 failed:
+port is already allocated`` against whichever container lost the race, which
+names neither the process holding the port nor what to do about it, and by
+then half the stack is running.
+
+A pre-flight that covers some of the ports still lets that happen, and it did.
+The list held six entries and CORE publishes sixteen. The missing one that
+matters most was **11434**: CORE gained a bundled Ollama, and the single most
+likely conflict for a product whose pitch is "runs a local model" is an
+operator who already has Ollama installed. Measured on a host in exactly that
+state, `make up` passed the port check and the model the stack pulled was not
+the model answering on that port.
+
+So the gate is two-directional, because a one-directional one is how the list
+went stale in the first place:
+
+* every CORE-published host port must appear in the doctor's list, and
+* every port in the doctor's list must still be published by CORE, so an
+ entry does not linger after a service is removed or remapped.
+
+`make doctor` does not read the profile, so `full`-profile ports are out of
+scope here — checking a port for a service that is not starting would fail a
+deployment over a conflict that cannot occur.
+"""
+
+from __future__ import annotations
+
+import pathlib
+import re
+
+import pytest
+import yaml
+
+REPO = pathlib.Path(__file__).resolve().parents[1]
+DOCTOR = REPO / "scripts" / "doctor.sh"
+COMPOSE = REPO / "docker-compose.yml"
+
+#: `"<host> <service> <container>"`, the three-field spec the shell loop splits.
+_SPEC = re.compile(r'"(\d+)\s+([a-z0-9-]+)\s+(\d+)"')
+
+
+def _host_port(entry: str) -> str | None:
+ """The host port of a `ports:` entry, or None when it publishes none.
+
+ Split from the right rather than matched, because the bind address is an
+ interpolation and the console's nests one inside another —
+ `${AISOC_CONSOLE_BIND_ADDR:-${AISOC_BIND_ADDR:-127.0.0.1}}:3000:3000`.
+ A leading `\\$\\{[^}]*\\}` stops at the first `}` and silently skips that
+ line, which would have excused the console from this gate entirely.
+ """
+ fields = entry.rsplit(":", 2)
+ if len(fields) == 3 and fields[1].isdigit():
+ return fields[1]
+ # `"9092:9092"` — published on every interface, no bind address.
+ if len(fields) == 2 and fields[0].isdigit() and fields[1].split("/")[0].isdigit():
+ return fields[0]
+ return None
+
+
+def _doctor_specs() -> set[tuple[str, str]]:
+ """`(host_port, service)` pairs the port pre-flight checks."""
+ text = DOCTOR.read_text(encoding="utf-8")
+ start = text.index("# host-port service container-port")
+ end = text.index("done", start)
+ return {(host, service) for host, service, _ in _SPEC.findall(text[start:end])}
+
+
+def _core_published() -> set[tuple[str, str]]:
+ """`(host_port, service)` pairs CORE publishes, from the compose file.
+
+ CORE is "has no `profiles:` key" — the same rule `docker compose up` with
+ no `--profile` applies.
+ """
+ services = (yaml.safe_load(COMPOSE.read_text(encoding="utf-8")) or {}).get("services") or {}
+ published: set[tuple[str, str]] = set()
+ for name, service in services.items():
+ if (service or {}).get("profiles"):
+ continue
+ for entry in (service or {}).get("ports") or []:
+ host = _host_port(str(entry))
+ if host:
+ published.add((host, name))
+ return published
+
+
[email protected](scope="module")
+def specs() -> set[tuple[str, str]]:
+ return _doctor_specs()
+
+
[email protected](scope="module")
+def core() -> set[tuple[str, str]]:
+ return _core_published()
+
+
+def test_the_fixtures_found_something(specs: set, core: set) -> None:
+ """Both parsers must actually parse, or the comparisons below are vacuous."""
+ assert len(specs) >= 6, f"parsed {len(specs)} specs out of doctor.sh"
+ assert len(core) >= 6, f"parsed {len(core)} published CORE ports out of docker-compose.yml"
+
+
+def test_every_core_port_is_pre_flighted(specs: set, core: set) -> None:
+ missing = sorted(core - specs, key=lambda pair: int(pair[0]))
+ assert not missing, (
+ "these CORE services publish a host port that `make up` does not check first, so a conflict "
+ f"on one arrives as a compose bind failure mid-start: {missing}"
+ )
+
+
+def test_no_pre_flighted_port_has_stopped_being_published(specs: set, core: set) -> None:
+ """The other direction: a stale entry fails a start over an impossible conflict."""
+ stale = sorted(specs - core, key=lambda pair: int(pair[0]))
+ assert not stale, f"doctor.sh checks these, but CORE no longer publishes them: {stale}"
+
+
+def test_the_bundled_model_port_is_covered(specs: set) -> None:
+ """Named explicitly because it is the conflict this audience actually hits.
+
+ An operator evaluating a self-hosted AI SOC is unusually likely to have
+ Ollama already listening on 11434. Docker Desktop does not always fail the
+ bind, so the symptom is not an error — it is the gateway talking to the
+ wrong model.
+ """
+ assert ("11434", "ollama") in specs