Skip to content

Commit 6f74c72

Browse files
authored
[FAST_BUILD] No sudo when run with rootless triplet (#2132)
* No sudo when run with rootless triplet - rootless triplet: -e NB_USER=root -e NB_UID=0 -e NB_GID=0 * Add tests for rootless triplet * Update tests for rootless triplet * Fix tests for rootless triplet
1 parent e6b5e74 commit 6f74c72

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

images/docker-stacks-foundation/start.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,14 @@ if [ "$(id -u)" == 0 ]; then
155155
unset_explicit_env_vars
156156

157157
_log "Running as ${NB_USER}:" "${cmd[@]}"
158-
exec sudo --preserve-env --set-home --user "${NB_USER}" \
159-
LD_LIBRARY_PATH="${LD_LIBRARY_PATH}" \
160-
PATH="${PATH}" \
161-
PYTHONPATH="${PYTHONPATH:-}" \
162-
"${cmd[@]}"
158+
if [ "${NB_USER}" = "root" ] && [ "${NB_UID}" = "$(id -u "${NB_USER}")" ] && [ "${NB_GID}" = "$(id -g "${NB_USER}")" ]; then
159+
HOME="/home/root" exec "${cmd[@]}"
160+
else
161+
exec sudo --preserve-env --set-home --user "${NB_USER}" \
162+
LD_LIBRARY_PATH="${LD_LIBRARY_PATH}" \
163+
PATH="${PATH}" \
164+
PYTHONPATH="${PYTHONPATH:-}" \
165+
"${cmd[@]}"
163166
# Notes on how we ensure that the environment that this container is started
164167
# with is preserved (except vars listed in JUPYTER_ENV_VARS_TO_UNSET) when
165168
# we transition from running as root to running as NB_USER.
@@ -187,6 +190,7 @@ if [ "$(id -u)" == 0 ]; then
187190
# above in /etc/sudoers.d/path. Thus PATH is irrelevant to how the above
188191
# sudo command resolves the path of `${cmd[@]}`. The PATH will be relevant
189192
# for resolving paths of any subprocesses spawned by `${cmd[@]}`.
193+
fi
190194

191195
# The container didn't start as the root user, so we will have to act as the
192196
# user we started as.

tests/docker-stacks-foundation/test_user_options.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,42 @@ def test_startsh_multiple_exec(container: TrackedContainer) -> None:
305305
"WARNING: start.sh is the default ENTRYPOINT, do not include it in CMD"
306306
in warnings[0]
307307
)
308+
309+
310+
def test_rootless_triplet_change(container: TrackedContainer) -> None:
311+
"""Container should change the username (`NB_USER`), the UID and the GID of the default user."""
312+
logs = container.run_and_wait(
313+
timeout=10,
314+
tty=True,
315+
user="root",
316+
environment=["NB_USER=root", "NB_UID=0", "NB_GID=0"],
317+
command=["id"],
318+
)
319+
assert "uid=0(root)" in logs
320+
assert "gid=0(root)" in logs
321+
assert "groups=0(root)" in logs
322+
323+
324+
def test_rootless_triplet_home(container: TrackedContainer) -> None:
325+
"""Container should change the home directory for triplet NB_USER=root, NB_UID=0, NB_GID=0."""
326+
logs = container.run_and_wait(
327+
timeout=10,
328+
tty=True,
329+
user="root",
330+
environment=["NB_USER=root", "NB_UID=0", "NB_GID=0"],
331+
command=["bash", "-c", "echo HOME=${HOME} && getent passwd root"],
332+
)
333+
assert "HOME=/home/root" in logs
334+
assert "root:x:0:0:root:/home/root:/bin/bash" in logs
335+
336+
337+
def test_rootless_triplet_sudo(container: TrackedContainer) -> None:
338+
"""Container should not be started with sudo for triplet NB_USER=root, NB_UID=0, NB_GID=0."""
339+
logs = container.run_and_wait(
340+
timeout=10,
341+
tty=True,
342+
user="root",
343+
environment=["NB_USER=root", "NB_UID=0", "NB_GID=0"],
344+
command=["env"],
345+
)
346+
assert "SUDO" not in logs

0 commit comments

Comments
 (0)