Skip to content

Commit a3b2925

Browse files
committed
Improve UID/GID remapping in entrywrapper.sh with error handling for existing users and groups
1 parent 72e9bb4 commit a3b2925

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

dockerfiles/entrywrapper.sh

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# 0) Only try chown if we're root (bind-mounts may be root-owned)
5-
if [[ "$(id -u)" -eq 0 ]] && [[ ! -O /home/mambauser/Code ]]; then
6-
chown -R mambauser:mambauser /home/mambauser/Code || true
4+
# 0) If root, optionally remap mambauser UID/GID for bind mounts, then drop privileges.
5+
if [[ "$(id -u)" -eq 0 ]]; then
6+
if [[ -n "${PGID:-}" ]]; then
7+
existing_group=""
8+
if getent group "$PGID" >/dev/null; then
9+
existing_group="$(getent group "$PGID" | cut -d: -f1)"
10+
fi
11+
if [[ -n "$existing_group" && "$existing_group" != "mambauser" ]]; then
12+
echo "Error: requested PGID '$PGID' is already in use by group '$existing_group', cannot remap mambauser group." >&2
13+
exit 1
14+
fi
15+
groupmod -g "$PGID" mambauser
16+
fi
17+
if [[ -n "${PUID:-}" ]]; then
18+
current_uid="$(id -u mambauser)"
19+
if [[ "$PUID" != "$current_uid" ]]; then
20+
if getent passwd "$PUID" >/dev/null; then
21+
echo "Error: Cannot remap mambauser to UID '$PUID' because it is already in use by another user." >&2
22+
exit 1
23+
fi
24+
usermod -u "$PUID" mambauser
25+
fi
26+
fi
27+
28+
if [[ -d /home/mambauser/Code ]]; then
29+
if ! chown -R mambauser:mambauser /home/mambauser/Code; then
30+
echo "warning: failed to change ownership of /home/mambauser/Code to mambauser:mambauser (read-only mount or permission issue?)" >&2
31+
fi
32+
fi
33+
if [[ -d /work ]]; then
34+
if ! chown -R mambauser:mambauser /work; then
35+
echo "warning: failed to change ownership of /work to mambauser:mambauser (read-only mount or permission issue?)" >&2
36+
fi
37+
fi
38+
39+
if [[ "${ENTRYWRAPPER_AS_USER:-0}" != "1" ]]; then
40+
exec runuser -u mambauser -- env ENTRYWRAPPER_AS_USER=1 /usr/local/bin/entrywrapper.sh "$@"
41+
fi
742
fi
843

944
# If running non-interactively at container root and /work exists; it will go there

0 commit comments

Comments
 (0)