fix: run manifest containers as root and drop privileges in entrypoint
The compose file was forcing the container to run as uid 1001, so the entrypoint could not create /workspace even with sudo configured. - Remove Dockerfile USER directive so containers start as root - Make compile_compose use user: 0:0 when the manifest declares a user - Make the entrypoint drop to the container user via after setup, preserving environment variables and command arguments - Update unit tests to assert root startup and privilege drop Quality gates: - pytest tests/unit: 210 passed - ruff: clean on changed files - mypy: clean on changed files
This commit is contained in:
@@ -237,6 +237,47 @@ def test_compile_dockerfile_uses_user_npm_prefix() -> None:
|
||||
assert "ENV PATH=/home/custom/.npm-global/bin:$PATH" in dockerfile
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_compile_dockerfile_starts_as_root_and_drops_privileges() -> None:
|
||||
"""The Dockerfile must not set USER so the entrypoint starts as root.
|
||||
|
||||
The entrypoint itself drops privileges to the container user before
|
||||
exec-ing the real command.
|
||||
"""
|
||||
manifest = {
|
||||
"base_image": "ubuntu:24.04",
|
||||
"interface_type": "terminal",
|
||||
"user": {"name": "dev", "uid": 1000, "gid": 1000},
|
||||
}
|
||||
dockerfile = compile_dockerfile(manifest)
|
||||
entrypoint = compile_entrypoint(manifest)
|
||||
|
||||
assert "USER dev" not in dockerfile
|
||||
assert 'exec su -s /bin/bash -c "exec \\"$@\\"" dev -- "$@"' in entrypoint
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_compile_compose_runs_as_root() -> None:
|
||||
"""The compose service must start as root so the entrypoint can fix /workspace."""
|
||||
manifest = {
|
||||
"base_image": "ubuntu:24.04",
|
||||
"interface_type": "terminal",
|
||||
"user": {"name": "dev", "uid": 1000, "gid": 1000},
|
||||
}
|
||||
variables = {
|
||||
"IMAGE_TAG": "test:latest",
|
||||
"INSTANCE_NAME": "test-instance",
|
||||
"REPO_PATH": "/host/repos/my-app",
|
||||
"WORKSPACE_NAME": "my-app",
|
||||
"TOOL_PORT": 0,
|
||||
"EXTRA_ENV": {},
|
||||
"EXTRA_VOLUMES": [],
|
||||
}
|
||||
compose = compile_compose(manifest, variables)
|
||||
|
||||
assert "user: 0:0" in compose
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestCompileEntrypoint:
|
||||
"""Tests for the generated permission-fixing entrypoint."""
|
||||
|
||||
Reference in New Issue
Block a user