|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
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 |
7 | 42 | fi |
8 | 43 |
|
9 | 44 | # If running non-interactively at container root and /work exists; it will go there |
|
0 commit comments