Reject external Compose backend networks before service startup
ABSDEV Framework CI / framework-ci (push) Successful in 24s
ABSDEV Framework CI / framework-ci (push) Successful in 24s
This commit is contained in:
+21
-2
@@ -4,6 +4,7 @@ The command runner must be supplied by the caller.
|
|||||||
This module does not execute subprocesses directly.
|
This module does not execute subprocesses directly.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import stat
|
import stat
|
||||||
import tempfile
|
import tempfile
|
||||||
@@ -37,6 +38,7 @@ class ServiceLifecycle:
|
|||||||
stop_timeout=90,
|
stop_timeout=90,
|
||||||
*,
|
*,
|
||||||
site_secrets=None,
|
site_secrets=None,
|
||||||
|
topology_runner=None,
|
||||||
):
|
):
|
||||||
|
|
||||||
require(
|
require(
|
||||||
@@ -86,6 +88,8 @@ class ServiceLifecycle:
|
|||||||
self._queue = docker["queue_service"]
|
self._queue = docker["queue_service"]
|
||||||
self._scheduler = docker["scheduler_service"]
|
self._scheduler = docker["scheduler_service"]
|
||||||
|
|
||||||
|
self._backend_network = docker["backend_network"]
|
||||||
|
self._topology_runner = topology_runner
|
||||||
self._runner = runner
|
self._runner = runner
|
||||||
self._stop_timeout = stop_timeout
|
self._stop_timeout = stop_timeout
|
||||||
|
|
||||||
@@ -114,6 +118,18 @@ class ServiceLifecycle:
|
|||||||
f"Service operation failed: {operation}",
|
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):
|
def quiesce(self):
|
||||||
|
|
||||||
self._execute(
|
self._execute(
|
||||||
@@ -130,6 +146,7 @@ class ServiceLifecycle:
|
|||||||
|
|
||||||
def restart_php(self):
|
def restart_php(self):
|
||||||
|
|
||||||
|
self._require_project_backend()
|
||||||
self._execute(
|
self._execute(
|
||||||
"restart PHP",
|
"restart PHP",
|
||||||
(
|
(
|
||||||
@@ -181,6 +198,7 @@ class ServiceLifecycle:
|
|||||||
|
|
||||||
def resume(self):
|
def resume(self):
|
||||||
|
|
||||||
|
self._require_project_backend()
|
||||||
self._execute(
|
self._execute(
|
||||||
"resume workers",
|
"resume workers",
|
||||||
(
|
(
|
||||||
@@ -203,8 +221,9 @@ class ContainedServiceLifecycle(ServiceLifecycle):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, config, runner, *, gate_directory, ingress_sealed,
|
def __init__(self, config, runner, *, gate_directory, ingress_sealed,
|
||||||
worker_running, stop_timeout=90, site_secrets=None):
|
worker_running, stop_timeout=90, site_secrets=None, topology_runner=None):
|
||||||
super().__init__(config, runner, stop_timeout, site_secrets=site_secrets)
|
super().__init__(config, runner, stop_timeout, site_secrets=site_secrets,
|
||||||
|
topology_runner=topology_runner)
|
||||||
require(callable(ingress_sealed) and callable(worker_running),
|
require(callable(ingress_sealed) and callable(worker_running),
|
||||||
"Containment probes are required")
|
"Containment probes are required")
|
||||||
self._gate_directory = Path(gate_directory)
|
self._gate_directory = Path(gate_directory)
|
||||||
|
|||||||
@@ -34,3 +34,11 @@ result record the activation and health outcome independently of reopening.
|
|||||||
|
|
||||||
These controls are available to the trusted runtime composition. The
|
These controls are available to the trusted runtime composition. The
|
||||||
operational production CLI remains disabled.
|
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.
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ class ContainedServiceTests(unittest.TestCase):
|
|||||||
self.config, self.run_command, gate_directory=self.directory,
|
self.config, self.run_command, gate_directory=self.directory,
|
||||||
ingress_sealed=lambda: self.gate.is_file() and self.probe_ok,
|
ingress_sealed=lambda: self.gate.is_file() and self.probe_ok,
|
||||||
worker_running=lambda _name: self.running,
|
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):
|
def run_command(self, args, *, timeout):
|
||||||
|
|||||||
@@ -19,10 +19,14 @@ class ServiceTests(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.runner = Mock(return_value=0)
|
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.manager = ServiceLifecycle(
|
||||||
self.config,
|
self.config,
|
||||||
self.runner,
|
self.runner,
|
||||||
|
topology_runner=self.topology,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.prefix = [
|
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):
|
def test_validate_nginx(self):
|
||||||
|
|
||||||
self.manager.validate_nginx()
|
self.manager.validate_nginx()
|
||||||
@@ -148,6 +172,7 @@ class ServiceTests(unittest.TestCase):
|
|||||||
manager = ServiceLifecycle(
|
manager = ServiceLifecycle(
|
||||||
self.config,
|
self.config,
|
||||||
self.runner,
|
self.runner,
|
||||||
|
topology_runner=self.topology,
|
||||||
)
|
)
|
||||||
|
|
||||||
manager.restart_php()
|
manager.restart_php()
|
||||||
@@ -164,6 +189,7 @@ class ServiceTests(unittest.TestCase):
|
|||||||
manager = ServiceLifecycle(
|
manager = ServiceLifecycle(
|
||||||
self.config,
|
self.config,
|
||||||
self.runner,
|
self.runner,
|
||||||
|
topology_runner=self.topology,
|
||||||
)
|
)
|
||||||
|
|
||||||
manager.validate_nginx()
|
manager.validate_nginx()
|
||||||
@@ -181,6 +207,7 @@ class ServiceTests(unittest.TestCase):
|
|||||||
manager = ServiceLifecycle(
|
manager = ServiceLifecycle(
|
||||||
self.config,
|
self.config,
|
||||||
self.runner,
|
self.runner,
|
||||||
|
topology_runner=self.topology,
|
||||||
)
|
)
|
||||||
|
|
||||||
manager.quiesce()
|
manager.quiesce()
|
||||||
|
|||||||
Reference in New Issue
Block a user