feat: run foreground as current user, support arbitrary UIDs - #136
feat: run foreground as current user, support arbitrary UIDs#136dotconfig404 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the container-focused foreground startup script to better support container runtimes that run with arbitrary UIDs (often without a corresponding username). It removes the prior UID/username matching and sudo/su branching, and instead only drops privileges when running as root and a service USER is set.
Changes:
- Replace
pushd/popdwith a simplecdinto${INSTALL_DIR}before launching. - When started as root and
USERis non-empty (andrunuserexists), drop privileges viarunuser. - Otherwise, run the service as the current runtime user/UID (supporting arbitrary UID containers).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| sudo -H -u "${USER}" $COMMAND | ||
| else | ||
| su "${USER}" -s /bin/bash -c "$COMMAND" | ||
| cd "${INSTALL_DIR}" |
There was a problem hiding this comment.
Not a new behaviour, previously pushd also assumed INSTALL_DIR to exist, but the error happened silently (&> /dev/null). Since there is no set -e here it'd continue either way, using cd or not, except that right now it's visible due to no redirection of stderr.
Also, the reason I swapped to cd was that popd was unreachable anyways, exec replaces the shell with the JVM. popd/pushd does not make sense here. Going to edit my PR body to mention this.
Pull Request (PR) description
Currently the
foregroundscript is used to switch to a service user when executed as root (among other things). It also lets you keep the user that you executed it as in this check (added in this PR).That check resolves
id -u ${USER}to check if it matches the executing UID, but when running containers with arbitrary UIDs the runtime user usually does not have a username. Currently the server container is using a sed command to clear the USER variable, asid -uwithout an argument resolves in the current user's ID. This PR would remove the need for the USER variable when no user switch is required (or possible).If running as root and USER is set, switch to user. If not, just run. Before it would also effectively check if
USERmatches the executing user's name (by comparing the UID that belongs to the username). This and the su/sudo branches do not make sense anymore if the script is intended as container only, which it is.Behaviour changes: a non-root UID that doesn't match USER now starts the service as that UID instead of failing in the sudo/su branches, and root with an empty USER now runs as root instead of failing with
runuser: user does not exist.PS: the
runuser: user does not existerror can be replicated using:runuser: user does not exist or the user entry does not contain all the required fieldsEDIT: The reason I swapped to cd was that popd was unreachable anyways, exec replaces the shell with the JVM.
popd/pushddoes not make sense here.