diff --git a/core/services.py b/core/services.py index 815b5d6..110f805 100644 --- a/core/services.py +++ b/core/services.py @@ -4,6 +4,7 @@ The command runner must be supplied by the caller. This module does not execute subprocesses directly. """ +import json import os import stat import tempfile @@ -37,6 +38,7 @@ class ServiceLifecycle: stop_timeout=90, *, site_secrets=None, + topology_runner=None, ): require( @@ -86,6 +88,8 @@ class ServiceLifecycle: self._queue = docker["queue_service"] self._scheduler = docker["scheduler_service"] + self._backend_network = docker["backend_network"] + self._topology_runner = topology_runner self._runner = runner self._stop_timeout = stop_timeout @@ -114,6 +118,18 @@ class ServiceLifecycle: f"Service operation failed: {operation}", ) + def _require_project_backend(self): + require(callable(self._topology_runner), "Trusted Compose topology runner required") + command = (*self._prefix, "config", "--format", "json") + try: + model = json.loads(self._topology_runner(command, timeout=30)) + backend = model["networks"][self._backend_network] + except Exception: + raise ServiceError("Unable to verify Compose backend network") from None + require(isinstance(backend, dict), "Invalid Compose backend network") + require(backend.get("external", False) is False, + "External Compose backend networks are prohibited") + def quiesce(self): self._execute( @@ -130,6 +146,7 @@ class ServiceLifecycle: def restart_php(self): + self._require_project_backend() self._execute( "restart PHP", ( @@ -181,6 +198,7 @@ class ServiceLifecycle: def resume(self): + self._require_project_backend() self._execute( "resume workers", ( @@ -203,8 +221,9 @@ class ContainedServiceLifecycle(ServiceLifecycle): """ def __init__(self, config, runner, *, gate_directory, ingress_sealed, - worker_running, stop_timeout=90, site_secrets=None): - super().__init__(config, runner, stop_timeout, site_secrets=site_secrets) + worker_running, stop_timeout=90, site_secrets=None, topology_runner=None): + super().__init__(config, runner, stop_timeout, site_secrets=site_secrets, + topology_runner=topology_runner) require(callable(ingress_sealed) and callable(worker_running), "Containment probes are required") self._gate_directory = Path(gate_directory) diff --git a/docs/NGINX-CONTAINMENT.md b/docs/NGINX-CONTAINMENT.md index 0dfb0bd..02c260f 100644 --- a/docs/NGINX-CONTAINMENT.md +++ b/docs/NGINX-CONTAINMENT.md @@ -34,3 +34,11 @@ result record the activation and health outcome independently of reopening. These controls are available to the trusted runtime composition. The operational production CLI remains disabled. + +Pass a trusted `topology_runner(command, *, timeout)` that returns stdout +from a successful Compose command as JSON bytes or text and raises on failure. +The runtime resolves the exact project and Compose files with +`config --format json` before starting workers or restarting PHP. Missing, +unverifiable, or external configured backend networks deny those operations. +Keep Compose inputs under trusted control across validation and execution. +Stopping workers remains available if topology validation fails. diff --git a/tests/unit/test_contained_services.py b/tests/unit/test_contained_services.py index 8358231..a245b3c 100644 --- a/tests/unit/test_contained_services.py +++ b/tests/unit/test_contained_services.py @@ -24,6 +24,9 @@ class ContainedServiceTests(unittest.TestCase): self.config, self.run_command, gate_directory=self.directory, ingress_sealed=lambda: self.gate.is_file() and self.probe_ok, worker_running=lambda _name: self.running, + topology_runner=lambda _command, **_options: json.dumps({ + "networks": {self.config["docker"]["backend_network"]: {"internal": True}}, + }), ) def run_command(self, args, *, timeout): diff --git a/tests/unit/test_services.py b/tests/unit/test_services.py index e8baf2b..e292451 100644 --- a/tests/unit/test_services.py +++ b/tests/unit/test_services.py @@ -19,10 +19,14 @@ class ServiceTests(unittest.TestCase): ) self.runner = Mock(return_value=0) + self.topology = Mock(return_value=json.dumps({ + "networks": {self.config["docker"]["backend_network"]: {"internal": True}}, + })) self.manager = ServiceLifecycle( self.config, self.runner, + topology_runner=self.topology, ) self.prefix = [ @@ -74,6 +78,26 @@ class ServiceTests(unittest.TestCase): ], ) + def test_external_backend_blocks_start_and_restart(self): + self.topology.return_value = json.dumps({ + "networks": {self.config["docker"]["backend_network"]: {"external": True}}, + }) + for operation in (self.manager.resume, self.manager.restart_php): + with self.assertRaisesRegex(ServiceError, "External Compose backend"): + operation() + self.runner.assert_not_called() + self.topology.assert_called_with( + (*self.prefix, "config", "--format", "json"), timeout=30, + ) + + def test_unverifiable_backend_fails_closed(self): + for model in ("invalid json", '{"networks": {}}', '{"networks": null}'): + with self.subTest(model=model): + self.topology.return_value = model + with self.assertRaisesRegex(ServiceError, "Unable to verify"): + self.manager.resume() + self.runner.assert_not_called() + def test_validate_nginx(self): self.manager.validate_nginx() @@ -148,6 +172,7 @@ class ServiceTests(unittest.TestCase): manager = ServiceLifecycle( self.config, self.runner, + topology_runner=self.topology, ) manager.restart_php() @@ -164,6 +189,7 @@ class ServiceTests(unittest.TestCase): manager = ServiceLifecycle( self.config, self.runner, + topology_runner=self.topology, ) manager.validate_nginx() @@ -181,6 +207,7 @@ class ServiceTests(unittest.TestCase): manager = ServiceLifecycle( self.config, self.runner, + topology_runner=self.topology, ) manager.quiesce()