diff --git a/launch/core/platforms/windows.py b/launch/core/platforms/windows.py index 28c146e..f2ddc6a 100644 --- a/launch/core/platforms/windows.py +++ b/launch/core/platforms/windows.py @@ -25,6 +25,32 @@ from docker.models.containers import Container +# Passing a host Docker daemon into an evaluated task container is privileged. +# Keep it opt-in and let callers select the source pipe explicitly. +WINDOWS_CONTAINER_DOCKER_PIPE_SOURCE_ENV = "REPOLAUNCH_WINDOWS_CONTAINER_DOCKER_PIPE_SOURCE" +WINDOWS_DOCKER_PIPE_TARGET = r"\\.\pipe\docker_engine" + + +def build_windows_container_docker_mounts() -> list[Any]: + """Return an explicit host-Docker npipe mount for Windows task containers. + + Some Windows projects invoke Docker from inside the task container and use + Docker's conventional ``\\\\.\\pipe\\docker_engine`` endpoint. Native engines + may expose a differently named host pipe. The evaluator must not expose + the host daemon by default; callers can opt in by setting + ``REPOLAUNCH_WINDOWS_CONTAINER_DOCKER_PIPE_SOURCE`` to the host npipe path. + An empty value explicitly disables the mount. + """ + source = os.environ.get(WINDOWS_CONTAINER_DOCKER_PIPE_SOURCE_ENV, "").strip() + if not source: + return [] + return [docker.types.Mount( + source=source, + target=WINDOWS_DOCKER_PIPE_TARGET, + type="npipe", + )] + + class WindowsRuntime(LinuxRuntime): def __init__( @@ -148,6 +174,7 @@ def _start_container( run_kwargs = { "cpu_count": CPU_CORES, # cpu_quota is Linux-only "mem_limit": MEM_LIMIT, + "mounts": build_windows_container_docker_mounts(), } container = client.containers.run( diff --git a/tests/windows_container_docker_pipe_test.py b/tests/windows_container_docker_pipe_test.py new file mode 100644 index 0000000..6bca8ab --- /dev/null +++ b/tests/windows_container_docker_pipe_test.py @@ -0,0 +1,49 @@ +from __future__ import annotations + +import os +import sys +import unittest +from pathlib import Path +from unittest import mock + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from launch.core.platforms import windows + + +class WindowsContainerDockerPipeTest(unittest.TestCase): + def test_mount_is_disabled_without_an_explicit_source(self) -> None: + with mock.patch.dict( + os.environ, + {windows.WINDOWS_CONTAINER_DOCKER_PIPE_SOURCE_ENV: ""}, + clear=False, + ): + self.assertEqual(windows.build_windows_container_docker_mounts(), []) + + def test_explicit_source_is_mounted_at_the_conventional_guest_pipe(self) -> None: + observed: list[dict[str, str]] = [] + + def fake_mount(**kwargs: str) -> dict[str, str]: + observed.append(kwargs) + return kwargs + + with mock.patch.dict( + os.environ, + { + windows.WINDOWS_CONTAINER_DOCKER_PIPE_SOURCE_ENV: + r"\\.\pipe\docker_engine_windows" + }, + clear=False, + ), mock.patch.object(windows.docker.types, "Mount", side_effect=fake_mount): + mounts = windows.build_windows_container_docker_mounts() + + self.assertEqual(mounts, observed) + self.assertEqual(observed, [{ + "source": r"\\.\pipe\docker_engine_windows", + "target": r"\\.\pipe\docker_engine", + "type": "npipe", + }]) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file