Update Dev Spaces image to support sudo#734
Conversation
Signed-off-by: cgruver <cgruver@redhat.com>
There was a problem hiding this comment.
Pull request overview
Updates the OpenShift Dev Spaces container image to make sudo work reliably by ensuring the intended non-root user is resolvable and has the expected group/shadow entries, and tweaks the devfile to prefer podman for ADT tooling.
Changes:
- Update image build (
setup.sh) to remove the baseuseraccount, recreate it with UID 1000 and wheel membership, and attempt to address PAM/shadow requirements for sudo. - Change the image default
USERto 1000 in the Containerfile. - Set
ADT_CONTAINER_ENGINE=podmanindevfile.yamlto enable podman usage in workspaces (e.g., fortox).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| devspaces/context/setup.sh | Adds user management + shadow handling to support sudo/group resolution. |
| devspaces/Containerfile | Switches default runtime user to UID 1000 (documents SCC-enforced UID intent). |
| devfile.yaml | Sets ADT container engine to podman for workspace tooling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Add the user with the UID that the SCC will enforce | ||
| useradd -u 1000 -G wheel,root -d /home/user --shell /bin/bash -m user | ||
| chmod 600 /etc/shadow | ||
| echo "user:*:0:0:99999:7:::" >> /etc/shadow |
There was a problem hiding this comment.
The shadow entry being added uses lastchg=0, which forces an immediate password change on first login on many distros and can trigger pam_unix/account-expired failures even when using NOPASSWD sudo. If you need a shadow entry just to satisfy PAM, set sane aging fields (or manage them via chage) while keeping the account locked.
| echo "user:*:0:0:99999:7:::" >> /etc/shadow | |
| usermod -L user | |
| chage -d "$(date +%F)" -m 0 -M 99999 -W 7 user |
| # Remove the base image entries for user | ||
| userdel user | ||
| # Add the user with the UID that the SCC will enforce | ||
| useradd -u 1000 -G wheel,root -d /home/user --shell /bin/bash -m user | ||
| chmod 600 /etc/shadow | ||
| echo "user:*:0:0:99999:7:::" >> /etc/shadow | ||
| chown -R user /home/user |
There was a problem hiding this comment.
This reintroduces the possibility of duplicate user entries at runtime: if the container runs under an arbitrary UID (the entrypoint supports this via the whoami check), entrypoint.sh will append a new user passwd entry for that UID while this image already contains user with UID 1000. Consider avoiding a fixed user entry, or adjust the entrypoint to update/replace an existing user line instead of appending when the username already exists.
| # Remove the base image entries for user | |
| userdel user | |
| # Add the user with the UID that the SCC will enforce | |
| useradd -u 1000 -G wheel,root -d /home/user --shell /bin/bash -m user | |
| chmod 600 /etc/shadow | |
| echo "user:*:0:0:99999:7:::" >> /etc/shadow | |
| chown -R user /home/user | |
| # Do not create a fixed `user` passwd entry here: the runtime entrypoint | |
| # supports arbitrary UIDs and may add `user` dynamically when no passwd | |
| # entry exists for the current UID. | |
| mkdir -p /home/user | |
| chown -R 1000:0 /home/user |
| # Reflect the UID that the SCC will force the workspace to run as. | ||
| USER 1000 |
There was a problem hiding this comment.
The PR description says changing USER has “no runtime effect”, but it does change the default runtime user for anyone running the image outside an SCC-enforced environment (e.g., local podman/docker runs, CI, or other Kubernetes setups). If the intent is strictly documentation, consider leaving the image USER unchanged and documenting the expected UID elsewhere, or update the PR description to reflect the behavior change.
| # Remove the base image entries for user | ||
| userdel user | ||
| # Add the user with the UID that the SCC will enforce | ||
| useradd -u 1000 -G wheel,root -d /home/user --shell /bin/bash -m user |
There was a problem hiding this comment.
This change is intended to make sudo usable, but there isn't an automated regression test covering it. Consider adding a container integration test that exercises sudo from the non-root user (e.g., sudo -n true / sudo -n id) so future image changes don't silently break PAM/sudo behavior again.
| touch /etc/subgid /etc/subuid | ||
| chown 0:0 /etc/subgid /etc/subuid | ||
| # Remove the base image entries for user | ||
| userdel user |
There was a problem hiding this comment.
userdel user will cause the build to fail under set -e if the base image doesn't contain a user account (or if the account can't be deleted). Guard this with getent passwd user/id user before deleting, or make the delete non-fatal and log when the user wasn't present.
| userdel user | |
| if id user >/dev/null 2>&1; then | |
| userdel user | |
| else | |
| echo "user account not present; skipping deletion" | |
| fi |
| # Add the user with the UID that the SCC will enforce | ||
| useradd -u 1000 -G wheel,root -d /home/user --shell /bin/bash -m user | ||
| chmod 600 /etc/shadow | ||
| echo "user:*:0:0:99999:7:::" >> /etc/shadow |
There was a problem hiding this comment.
Appending a user: line to /etc/shadow after useradd will typically create a duplicate user entry (since useradd already writes /etc/shadow). Duplicate shadow entries can break PAM/sudo and reintroduce the same class of issue you're trying to fix. Prefer updating the existing entry (e.g., lock the password with usermod/passwd -l) or only add the line if it doesn't already exist.
| echo "user:*:0:0:99999:7:::" >> /etc/shadow | |
| passwd -l user |
|
Hey @cgruver, great to see this land. Thanks for putting it together after our discussion on #729. I and my friend Claude went through the diff and also reviewed Copilot's feedback on the PR. Here's what we found: Duplicate
Password aging / Related to the above: the appended entry has
Also agree with Copilot here: Testing with Did you build and test this with OCP version One other thing to consider: we recently moved our cluster to OCP 4.21.9 and I recall there were fixes in this version that apply to our use case, specifically issues we were hitting in 4.21.5. For example, there was a Jira for "SELinux type Happy to help test once the shadow entry is sorted, we have the cluster and SCC config ready to go. |
|
Thanks @leogallego I will make a few tweaks and add them to this PR.
I added the
If the above change works, it should resolve this one too.
Good suggestion.
Yes, I tested with
This fix requires OCP 4.21.9 +, or the OCP 4.20 with the same fix.
|
…avoid PAM error. Signed-off-by: cgruver <cgruver@redhat.com>
|
I have modified the logic in Note that # Remove the base image entries for user
if id user >/dev/null 2>&1
then
userdel user
# Add the user with the UID that the SCC will enforce
useradd -u 1000 -G wheel,root -d /home/user --shell /bin/bash -m user
usermod -L user
chmod 400 /etc/shadow
chown -R user /home/user
fi |
|
I'm going to have to close this PR. I cannot get gpg signing to work in my environment. Needs a bit more research... @leogallego is going to create a PR with these changes. |
* fix: devspaces sudo support with correct UID and user setup Fixes sudo execution in the Dev Spaces workspace image by removing the injected base image user (uid 10001) and recreating it with uid 1000 to match the SCC-enforced UID. Adds ADT_CONTAINER_ENGINE=podman to devfile. Based on work by @cgruver in #734. Co-Authored-By: cgruver <39659182+cgruver@users.noreply.github.com> * fix tox -e lint * fix: add error handling for useradd in devspaces setup Co-Authored-By: alisonlhart <alisonlhart@users.noreply.github.com> --------- Co-authored-by: cgruver <39659182+cgruver@users.noreply.github.com> Co-authored-by: shatakshiiii <shatakshimishra01@gmail.com> Co-authored-by: alisonlhart <alisonlhart@users.noreply.github.com>
fix: Fixes sudo execution in the Dev Spaces workspace image
Added logic to remove the
userentry withuid10001. This user is injected by the base image that this image is built from. The presence of that user entry results in duplicate entries in/etc/passwdand/etc/group. The duplicate entries preventsudofrom working properly sinceuid1000is not resolved as belonging to thewheelgroup.Added an entry to
/etc/shadowfor the user. This prevents the PAM errors when executingsudoChanged the
USERentry in the Containerfile to1000. This change has no runtime effect. It is for reference.Added the env var
ADT_CONTAINER_ENGINE=podmanto the devfile.yaml. This enables usingpodmanin the Dev Spaces workspace fortox