From 4d92497ab63d627c7f1a01082bf94f00ce85f73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20H=C3=B6lzl?= <138807728+x241c297f@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:36:52 +0200 Subject: [PATCH] [common-utils] Fix group creation if it exists MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Wolfgang Hölzl <138807728+x241c297f@users.noreply.github.com> --- src/common-utils/devcontainer-feature.json | 2 +- src/common-utils/main.sh | 12 ++++++++---- test/common-utils/group-already-exists.sh | 14 ++++++++++++++ test/common-utils/scenarios.json | 10 ++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100755 test/common-utils/group-already-exists.sh diff --git a/src/common-utils/devcontainer-feature.json b/src/common-utils/devcontainer-feature.json index 3fa6487df..82ddde046 100644 --- a/src/common-utils/devcontainer-feature.json +++ b/src/common-utils/devcontainer-feature.json @@ -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.", diff --git a/src/common-utils/main.sh b/src/common-utils/main.sh index b5fe11f57..14539dfb8 100644 --- a/src/common-utils/main.sh +++ b/src/common-utils/main.sh @@ -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 @@ -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 diff --git a/test/common-utils/group-already-exists.sh b/test/common-utils/group-already-exists.sh new file mode 100755 index 000000000..28caa7b1f --- /dev/null +++ b/test/common-utils/group-already-exists.sh @@ -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 diff --git a/test/common-utils/scenarios.json b/test/common-utils/scenarios.json index a929f534a..0688f706f 100644 --- a/test/common-utils/scenarios.json +++ b/test/common-utils/scenarios.json @@ -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": {