From 3df771fbc4de876d234a40ab79224d8ea4e20ca7 Mon Sep 17 00:00:00 2001 From: videlanicolas Date: Wed, 12 May 2021 01:26:38 +1000 Subject: [PATCH] Check the logged user instead of $USER (#3330) * Check the logged user instead of $USER Given that `sudo usermod --login "$DOCKER_USER" coder` and `sudo groupmod -n "$DOCKER_USER" coder` modify the container's disk it'll persist across restarts, but environment variables will be reset to whatever state they had at the end of `Dockerfile`. In this case, `$USER` is set to `coder`, so this branch will always be true. By checking with the output of `whoami`, which gets it's information from `/etc/passwd`, we make sure to get the real logged user and not the one defined by $USER. We also move `USER="$DOCKER_USER"` out of the branch, since we always want this to happen at entry-point. If we don't do this assignment, $USER will contain `coder` upon restart. * Update entrypoint.sh Check `$DOCKER_USER` was defined before copying it to `$USER`. --- ci/release-image/entrypoint.sh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ci/release-image/entrypoint.sh b/ci/release-image/entrypoint.sh index 1c8c8bff..6eddae46 100755 --- a/ci/release-image/entrypoint.sh +++ b/ci/release-image/entrypoint.sh @@ -5,16 +5,17 @@ set -eu # Otherwise the current container UID may not exist in the passwd database. eval "$(fixuid -q)" -if [ "${DOCKER_USER-}" ] && [ "$DOCKER_USER" != "$USER" ]; then - echo "$DOCKER_USER ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers.d/nopasswd >/dev/null - # Unfortunately we cannot change $HOME as we cannot move any bind mounts - # nor can we bind mount $HOME into a new home as that requires a privileged container. - sudo usermod --login "$DOCKER_USER" coder - sudo groupmod -n "$DOCKER_USER" coder - +if [ "${DOCKER_USER-}" ]; then USER="$DOCKER_USER" + if [ "$DOCKER_USER" != "$(whoami)" ]; then + echo "$DOCKER_USER ALL=(ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers.d/nopasswd >/dev/null + # Unfortunately we cannot change $HOME as we cannot move any bind mounts + # nor can we bind mount $HOME into a new home as that requires a privileged container. + sudo usermod --login "$DOCKER_USER" coder + sudo groupmod -n "$DOCKER_USER" coder - sudo sed -i "/coder/d" /etc/sudoers.d/nopasswd + sudo sed -i "/coder/d" /etc/sudoers.d/nopasswd + fi fi dumb-init /usr/bin/code-server "$@"