Skip to content

Commit d8676bc

Browse files
committed
feat(cli): use non-root user for package build in docker
1 parent d5a41a8 commit d8676bc

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

packages/debmagic/src/debmagic/cli/_build_driver/driver_docker.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import uuid
23
from pathlib import Path
34
from typing import Self, Sequence
@@ -16,12 +17,25 @@
1617

1718
BUILD_DIR_IN_CONTAINER = Path("/debmagic")
1819

20+
DOCKER_USER = "user"
21+
1922
DOCKERFILE_TEMPLATE = f"""
2023
FROM {{base_image}}
2124
22-
RUN apt-get update && apt-get -y install dpkg-dev python3
25+
ARG USERNAME={DOCKER_USER}
26+
ARG USER_UID=1000
27+
ARG USER_GID=$USER_UID
28+
29+
RUN apt-get update && apt-get install -y sudo dpkg-dev python3
30+
31+
RUN groupadd --gid $USER_GID $USERNAME \
32+
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
33+
&& echo $USERNAME ALL=\\(root\\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
34+
&& chmod 0440 /etc/sudoers.d/$USERNAME
2335
2436
RUN mkdir -p {BUILD_DIR_IN_CONTAINER}
37+
RUN chown $USERNAME:$USERNAME {BUILD_DIR_IN_CONTAINER}
38+
USER $USERNAME
2539
ENTRYPOINT ["sleep", "infinity"]
2640
"""
2741

@@ -61,10 +75,18 @@ def create(cls, config: BuildConfig, driver_config: DockerDriverConfig) -> Self:
6175
dockerfile_path.write_text(formatted_dockerfile)
6276

6377
docker_image_name = f"debmagic-{config.build_identifier}"
78+
79+
additional_args = []
80+
if not os.getuid() == 0:
81+
# to reduce potential permission problems with missing user remappings on some systems
82+
# we simply create the build user inside the docker container with the same uid / gid as our host user
83+
additional_args.extend(["--build-arg", f"USER_UID={os.getuid()}", "--build-arg", f"USER_GID={os.getgid()}"])
84+
6485
ret = run_cmd(
6586
[
6687
"docker",
6788
"build",
89+
*additional_args,
6890
"--tag",
6991
docker_image_name,
7092
"-f",
@@ -109,15 +131,16 @@ def get_build_metadata(self) -> DriverSpecificBuildMetadata:
109131
return meta.model_dump()
110132

111133
def run_command(self, cmd: Sequence[str | Path], cwd: Path | None = None, requires_root: bool = False):
112-
del requires_root # we assume to always be root in the container
134+
conditional_args: list[str | Path] = []
113135

114136
if cwd:
115137
cwd = self._translate_path_in_container(cwd)
116-
cwd_args: list[str | Path] = ["--workdir", cwd]
117-
else:
118-
cwd_args = []
138+
conditional_args.extend(["--workdir", cwd])
139+
140+
if requires_root:
141+
conditional_args.extend(["--user", "root"])
119142

120-
ret = run_cmd(["docker", "exec", *cwd_args, self._container_name, *cmd], dry_run=self._dry_run)
143+
ret = run_cmd(["docker", "exec", *conditional_args, self._container_name, *cmd], dry_run=self._dry_run)
121144
if ret.returncode != 0:
122145
raise BuildError("Error building package")
123146

0 commit comments

Comments
 (0)