Prepare containerized dev/prod deployment (inactive)
Add a Docker image and separate Compose projects for development and production, based on the stable main behaviour without the privacy gateway. Nothing is wired into the existing systemd deploy workflow. - One non-root image (api | ui | preflight), base image pinned by digest, dependencies from a hash-locked universal lock without torch and sentence-transformers (MiniLM is not on the runtime path) - Preflight aborts on: VECTOR_DIM != 768 or non-nomic embedding profile, implicit or cross-env collection prefix, cloud keys without explicit egress acknowledgement, missing/empty or writable vault mount - Vault is a read-only NAS bind mount without host-path autocreation; healthcheck turns unhealthy when the vault sentinel disappears - Dev runs its own Qdrant; prod reuses the existing host instance - API published on loopback only; read-only rootfs, cap_drop ALL, no-new-privileges - Dev workflow kept as an inactive template outside .gitea/workflows - Docs: operations guide and migration plan with release gates, test baseline classification, R2/R3 and privacy gateway requirements Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0d61a9e191
commit
7a225ba2b6
18 files changed
+2864
No files matched your search
@@ -0,0 +1,242 @@
|
||||
"""
|
||||
FILE: tests/test_container_preflight.py
|
||||
DESCRIPTION: Prüft Container-Preflight (deploy/entrypoint.py) und statisch die
|
||||
Sicherheitszusagen der Compose-/Docker-Dateien. Läuft ohne Docker.
|
||||
"""
|
||||
import importlib.util
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEPLOY = ROOT / "deploy"
|
||||
SYSTEMD_PORTS = {8001, 8002, 8501, 8502}
|
||||
|
||||
|
||||
def _load_entrypoint():
|
||||
spec = importlib.util.spec_from_file_location("mindnet_entrypoint", DEPLOY / "entrypoint.py")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
ep = _load_entrypoint()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def env(monkeypatch):
|
||||
for key in ("MINDNET_ENV", "COLLECTION_PREFIX", "VECTOR_DIM", "MINDNET_EMBEDDING_MODEL",
|
||||
"OPENROUTER_API_KEY", "GOOGLE_API_KEY", "MINDNET_ALLOW_CLOUD_EGRESS",
|
||||
"MINDNET_VAULT_ROOT", "MINDNET_VAULT_SENTINEL", "MINDNET_VAULT_READONLY",
|
||||
"MINDNET_VAULT_REQUIRED", "MINDNET_EXPECTED_VECTOR_DIM"):
|
||||
monkeypatch.delenv(key, raising=False)
|
||||
monkeypatch.chdir(ROOT)
|
||||
return monkeypatch
|
||||
|
||||
|
||||
# --- Umgebung und Präfix ---------------------------------------------------------
|
||||
|
||||
@pytest.mark.parametrize("menv, prefix", [("", "mindnet"), ("staging", "mindnet"), ("prod", ""),
|
||||
("dev", "mindnet"), ("prod", "mindnet_dev")])
|
||||
def test_environment_rejects_ambiguous_setups(env, menv, prefix):
|
||||
env.setenv("MINDNET_ENV", menv)
|
||||
env.setenv("COLLECTION_PREFIX", prefix)
|
||||
with pytest.raises(ep.PreflightError):
|
||||
ep.check_environment()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("menv, prefix", [("dev", "mindnet_dev"), ("prod", "mindnet")])
|
||||
def test_environment_accepts_explicit_setups(env, menv, prefix):
|
||||
env.setenv("MINDNET_ENV", menv)
|
||||
env.setenv("COLLECTION_PREFIX", prefix)
|
||||
assert ep.check_environment() == menv
|
||||
|
||||
|
||||
# --- Embeddings: keine stille Rückkehr zu 384 Dimensionen -----------------------
|
||||
|
||||
@pytest.mark.parametrize("dim", [None, "384", "3072"])
|
||||
def test_embeddings_reject_wrong_or_missing_dim(env, dim):
|
||||
if dim:
|
||||
env.setenv("VECTOR_DIM", dim)
|
||||
with pytest.raises(ep.PreflightError, match="VECTOR_DIM"):
|
||||
ep.check_embeddings()
|
||||
|
||||
|
||||
def test_embeddings_accept_repository_profile(env):
|
||||
env.setenv("VECTOR_DIM", "768")
|
||||
ep.check_embeddings()
|
||||
|
||||
|
||||
def test_embeddings_reject_minilm_override(env):
|
||||
env.setenv("VECTOR_DIM", "768")
|
||||
env.setenv("MINDNET_EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
|
||||
with pytest.raises(ep.PreflightError, match="MINDNET_EMBEDDING_MODEL"):
|
||||
ep.check_embeddings()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("profile", [
|
||||
{"provider": "ollama", "model": "all-minilm", "dimensions": 384},
|
||||
{"provider": "ollama", "model": "nomic-embed-text", "dimensions": 384},
|
||||
{"provider": "ollama", "model": "nomic-embed-text"},
|
||||
])
|
||||
def test_embeddings_reject_bad_profile(env, tmp_path, profile):
|
||||
env.setenv("VECTOR_DIM", "768")
|
||||
path = tmp_path / "profiles.yaml"
|
||||
path.write_text(yaml.safe_dump({"profiles": {"embedding_expert": profile}}), encoding="utf-8")
|
||||
with pytest.raises(ep.PreflightError, match="embedding_expert"):
|
||||
ep.check_embeddings(str(path))
|
||||
|
||||
|
||||
# --- Cloud-Egress ------------------------------------------------------------------
|
||||
|
||||
def test_cloud_keys_require_explicit_ack(env):
|
||||
env.setenv("OPENROUTER_API_KEY", "sk-test")
|
||||
with pytest.raises(ep.PreflightError, match="MINDNET_ALLOW_CLOUD_EGRESS"):
|
||||
ep.check_cloud_egress()
|
||||
env.setenv("MINDNET_ALLOW_CLOUD_EGRESS", "true")
|
||||
ep.check_cloud_egress()
|
||||
|
||||
|
||||
def test_no_cloud_keys_needs_no_ack(env):
|
||||
ep.check_cloud_egress()
|
||||
|
||||
|
||||
# --- Vault / NAS -----------------------------------------------------------------------
|
||||
|
||||
def _vault(tmp_path, with_sentinel=True):
|
||||
root = tmp_path / "vault"
|
||||
root.mkdir()
|
||||
if with_sentinel:
|
||||
sentinel = root / "_system" / "dictionary" / "edge_vocabulary.md"
|
||||
sentinel.parent.mkdir(parents=True)
|
||||
sentinel.write_text("# vocab", encoding="utf-8")
|
||||
return root
|
||||
|
||||
|
||||
def test_vault_missing_directory(env, tmp_path):
|
||||
env.setenv("MINDNET_VAULT_ROOT", str(tmp_path / "nope"))
|
||||
with pytest.raises(ep.PreflightError, match="fehlt"):
|
||||
ep.check_vault()
|
||||
|
||||
|
||||
def test_vault_empty_mountpoint_is_detected(env, tmp_path):
|
||||
# Ausgefallenes NAS: Mountpunkt existiert, ist aber leer.
|
||||
env.setenv("MINDNET_VAULT_ROOT", str(_vault(tmp_path, with_sentinel=False)))
|
||||
with pytest.raises(ep.PreflightError, match="Sentinel"):
|
||||
ep.check_vault()
|
||||
|
||||
|
||||
def test_vault_must_be_read_only(env, tmp_path):
|
||||
env.setenv("MINDNET_VAULT_ROOT", str(_vault(tmp_path)))
|
||||
env.setattr(ep, "_is_read_only_mount", lambda path: False)
|
||||
with pytest.raises(ep.PreflightError, match="schreibgeschützt"):
|
||||
ep.check_vault()
|
||||
env.setattr(ep, "_is_read_only_mount", lambda path: True)
|
||||
ep.check_vault()
|
||||
|
||||
|
||||
def test_read_only_check_fails_closed_without_statvfs(env, tmp_path):
|
||||
env.delattr(ep.os, "statvfs", raising=False)
|
||||
assert ep._is_read_only_mount(tmp_path) is False
|
||||
|
||||
|
||||
def test_unknown_mode_is_rejected():
|
||||
assert ep.main(["entrypoint.py", "shell"]) == 2
|
||||
|
||||
|
||||
# --- Statische Prüfung der Deployment-Dateien ---------------------------------------
|
||||
|
||||
def _compose(name):
|
||||
return yaml.safe_load((DEPLOY / name).read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
def _host_port(mapping):
|
||||
m = re.search(r":(?:\$\{[A-Z_]+:-)?(\d+)\}?:\d+$", mapping)
|
||||
return int(m.group(1))
|
||||
|
||||
|
||||
COMPOSE = {"dev": _compose("compose.dev.yml"), "prod": _compose("compose.prod.yml")}
|
||||
APP_SERVICES = ("api", "ui")
|
||||
|
||||
|
||||
def test_compose_projects_are_separate():
|
||||
assert COMPOSE["dev"]["name"] == "mindnet-dev"
|
||||
assert COMPOSE["prod"]["name"] == "mindnet-prod"
|
||||
assert set(COMPOSE["dev"]["networks"]).isdisjoint(COMPOSE["prod"]["networks"])
|
||||
|
||||
|
||||
def test_compose_ports_do_not_collide():
|
||||
ports = {e: {_host_port(p) for s in c["services"].values() for p in s.get("ports", [])}
|
||||
for e, c in COMPOSE.items()}
|
||||
assert ports["dev"].isdisjoint(ports["prod"])
|
||||
assert not (ports["dev"] | ports["prod"]) & SYSTEMD_PORTS
|
||||
|
||||
|
||||
@pytest.mark.parametrize("envname", ["dev", "prod"])
|
||||
def test_compose_app_services_hardened(envname):
|
||||
for name in APP_SERVICES:
|
||||
svc = COMPOSE[envname]["services"][name]
|
||||
assert svc["read_only"] is True
|
||||
assert svc["cap_drop"] == ["ALL"]
|
||||
assert "no-new-privileges:true" in svc["security_opt"]
|
||||
assert svc["environment"]["VECTOR_DIM"] == "768"
|
||||
assert svc["environment"]["MINDNET_VAULT_READONLY"] == "true"
|
||||
assert svc["healthcheck"]["test"][-1] == name
|
||||
|
||||
|
||||
@pytest.mark.parametrize("envname", ["dev", "prod"])
|
||||
def test_compose_vault_is_read_only_bind_without_autocreate(envname):
|
||||
mounts = [v for v in COMPOSE[envname]["services"]["api"]["volumes"]
|
||||
if isinstance(v, dict) and v.get("target") == "/vault"]
|
||||
assert len(mounts) == 1
|
||||
assert mounts[0]["read_only"] is True
|
||||
assert mounts[0]["bind"]["create_host_path"] is False
|
||||
assert all(":/vault" not in v for v in COMPOSE[envname]["services"]["ui"]["volumes"] if isinstance(v, str))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("envname", ["dev", "prod"])
|
||||
def test_compose_api_only_on_loopback(envname):
|
||||
assert all(p.startswith("127.0.0.1:") for p in COMPOSE[envname]["services"]["api"]["ports"])
|
||||
|
||||
|
||||
def test_prod_uses_existing_qdrant_and_dev_its_own():
|
||||
assert "qdrant" not in COMPOSE["prod"]["services"]
|
||||
dev_qdrant = COMPOSE["dev"]["services"]["qdrant"]
|
||||
assert "@sha256:" in dev_qdrant["image"]
|
||||
for name in APP_SERVICES:
|
||||
dev_env = COMPOSE["dev"]["services"][name]["environment"]
|
||||
assert dev_env["QDRANT_URL"] == "http://qdrant:6333"
|
||||
assert dev_env["COLLECTION_PREFIX"].endswith("_dev")
|
||||
|
||||
|
||||
def test_dockerfile_pinned_and_non_root():
|
||||
text = (DEPLOY / "Dockerfile").read_text(encoding="utf-8")
|
||||
assert re.search(r"ARG PYTHON_IMAGE=python:3\.12-slim-bookworm@sha256:[0-9a-f]{64}", text)
|
||||
runtime = text.split("AS runtime", 1)[1].split("AS test", 1)[0]
|
||||
assert "USER mindnet:mindnet" in runtime
|
||||
assert "--require-hashes" in text
|
||||
|
||||
|
||||
def test_lock_excludes_torch_and_minilm_and_is_hashed():
|
||||
lock = (DEPLOY / "requirements.lock").read_text(encoding="utf-8")
|
||||
pins = re.findall(r"^([a-z0-9][a-z0-9._-]*)==", lock, re.M)
|
||||
assert pins and not {"torch", "sentence-transformers", "transformers"} & set(pins)
|
||||
entries = re.split(r"\n(?=[a-z0-9])", lock.strip())
|
||||
assert all("--hash=sha256:" in e for e in entries if re.match(r"[a-z0-9]", e))
|
||||
|
||||
|
||||
def test_dockerignore_is_allowlist_without_env_files():
|
||||
lines = [l.strip() for l in (ROOT / ".dockerignore").read_text(encoding="utf-8").splitlines()
|
||||
if l.strip() and not l.startswith("#")]
|
||||
assert lines[0] == "**"
|
||||
assert "config/*.env" in lines and "deploy/env/*.env" in lines
|
||||
assert not any(l.startswith("!") and ("vault" in l or ".env" in l) for l in lines)
|
||||
|
||||
|
||||
def test_prod_env_example_blocks_cloud_egress():
|
||||
values = dict(l.split("=", 1) for l in (DEPLOY / "env" / "prod.env.example").read_text(encoding="utf-8").splitlines()
|
||||
if "=" in l and not l.startswith("#"))
|
||||
assert values["OPENROUTER_API_KEY"] == "" and values["GOOGLE_API_KEY"] == ""
|
||||
assert values["MINDNET_ALLOW_CLOUD_EGRESS"] == "false"
|
||||
Reference in new issue
Block a user