Skip to content

Commit

Permalink
[common-utils] Fix group creation if it exists
Browse files Browse the repository at this point in the history
Signed-off-by: Wolfgang Hölzl <[email protected]>
  • Loading branch information
x241c297f committed Oct 25, 2024
1 parent 2951f04 commit 4d92497
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/common-utils/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "common-utils",
"version": "2.5.2",
"version": "2.5.3",
"name": "Common Utilities",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/common-utils",
"description": "Installs a set of common command line utilities, Oh My Zsh!, and sets up a non-root user.",
Expand Down
12 changes: 8 additions & 4 deletions src/common-utils/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ elif [ "${USERNAME}" = "none" ]; then
USER_GID=0
fi
# Create or update a non-root user to match UID/GID.
group_name="${USERNAME}"
if [ "${USER_GID}" != "automatic" ] && getent group "${USER_GID}" > /dev/null 2>&1; then
group_name=$(getent group "${USER_GID}" | cut -d: -f1)
else
group_name="${USERNAME}"
fi
if id -u ${USERNAME} > /dev/null 2>&1; then
# User exists, update if needed
if [ "${USER_GID}" != "automatic" ] && [ "$USER_GID" != "$(id -g $USERNAME)" ]; then
Expand All @@ -421,13 +425,13 @@ else
# Create user
if [ "${USER_GID}" = "automatic" ]; then
groupadd $USERNAME
else
elif ! getent group "${USER_GID}" > /dev/null 2>&1; then
groupadd --gid $USER_GID $USERNAME
fi
if [ "${USER_UID}" = "automatic" ]; then
useradd -s /bin/bash --gid $USERNAME -m $USERNAME
useradd -s /bin/bash --gid "${group_name}" -m "${USERNAME}"
else
useradd -s /bin/bash --uid $USER_UID --gid $USERNAME -m $USERNAME
useradd -s /bin/bash --uid "${USER_UID}" --gid "${group_name}" -m "${USERNAME}"
fi
fi

Expand Down
14 changes: 14 additions & 0 deletions test/common-utils/group-already-exists.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
. /etc/os-release
check "non-root user" test "$(whoami)" = "devcontainer"
check "group name is adm" test "$(id -gn)" = "adm"

# Report result
reportResults
10 changes: 10 additions & 0 deletions test/common-utils/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@
"common-utils": {}
}
},
"group-already-exists": {
"image": "alpine",
"remoteUser": "devcontainer",
"features": {
"common-utils": {
"userUid": "1000",
"userGid": "4"
}
}
},
"already-run": {
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"features": {
Expand Down

0 comments on commit 4d92497

Please sign in to comment.