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.

3.2 KB
Raw Blame
"""`make up` must print the port the console was actually published on.

Found in a clean-room install. `scripts/resolve_port_conflicts.py` moves
a published port when the host already holds it, and on that machine 3000
was taken: the console served happily on 15000 and `make up` printed
`http://localhost:3000`. A first-run URL that refuses the connection is a
first impression of a product that does not start, from a stack that is
entirely healthy.

The expression is also fragile in a way worth pinning. Two separate
attempts produced `*** unterminated call to function 'shell'` rather than
a wrong answer: a sed capture group's ``\\(`` ``\\)`` unbalances the
parentheses ``make`` counts, and a literal ``#`` anywhere in the command
starts a ``make`` comment that swallows the closing paren. A test that
only checked the happy value would not have caught either, because the
Makefile would not parse at all.
"""

from __future__ import annotations

import shutil
import subprocess
import sys
from pathlib import Path

import pytest

REPO = Path(__file__).resolve().parents[2]
MAKEFILE = REPO / "Makefile"

OVERLAY = """\
# Generated by scripts/resolve_port_conflicts.py — do not edit by hand.

services:
  # web: 3000 was in use, published on 15000 instead
  web:
    ports: !override
      - "15000:3000"
"""


def _console_url(workdir: Path) -> str:
    """Ask the real Makefile what it would print."""
    (workdir / "extra.mk").write_text("_show:\n\t@echo $(console_url)\n", encoding="utf-8")
    result = subprocess.run(  # noqa: S603
        [shutil.which("make") or "make", "--no-print-directory", "-s", "-f", "Makefile", "-f", "extra.mk", "_show"],
        cwd=workdir,
        capture_output=True,
        text=True,
        check=False,
    )
    if result.returncode != 0:
        pytest.fail(
            f"the Makefile did not parse: {result.stderr.strip()}\n"
            "Both previous attempts at this expression failed this way rather than "
            "producing a wrong URL."
        )
    return result.stdout.strip().splitlines()[-1] if result.stdout.strip() else ""


@pytest.fixture
def workdir(tmp_path: Path) -> Path:
    shutil.copy(MAKEFILE, tmp_path / "Makefile")
    return tmp_path


@pytest.mark.skipif(shutil.which("make") is None, reason="make is not installed")
class TestTheConsoleUrl:
    def test_defaults_to_3000_when_nothing_was_remapped(self, workdir: Path) -> None:
        assert _console_url(workdir) == "http://localhost:3000"

    def test_follows_the_port_the_overlay_published(self, workdir: Path) -> None:
        """The defect: a remapped console printed as 3000."""
        (workdir / "docker-compose.ports.yml").write_text(OVERLAY, encoding="utf-8")
        assert _console_url(workdir) == "http://localhost:15000"

    def test_an_explicit_console_url_still_wins(self, workdir: Path) -> None:
        """A deployment behind a reverse proxy sets this, and neither the
        overlay nor the default should override it."""
        (workdir / "docker-compose.ports.yml").write_text(OVERLAY, encoding="utf-8")
        (workdir / ".env").write_text("AISOC_CONSOLE_URL=https://soc.example.com\n", encoding="utf-8")
        assert _console_url(workdir) == "https://soc.example.com"


if __name__ == "__main__":
    sys.exit(pytest.main([__file__, "-v"]))