From 0f9a4cffd285df5f9bd5535f7d75db64c491d552 Mon Sep 17 00:00:00 2001 From: StarFleetCPTN Date: Sun, 6 Apr 2025 23:35:48 -0700 Subject: [PATCH] fix: Improve UID/GID management in entrypoint script - Enhanced the logic for updating user and group IDs by deleting and recreating the user/group if the current IDs do not match the specified values. - Added verification steps to ensure the UID/GID changes were successful, with appropriate error handling and logging for failures. - Streamlined the process for handling Alpine Linux user management, improving clarity and maintainability of the script. https://github.com/StarFleetCPTN/GoMFT/issues/68 --- entrypoint.sh | 52 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 4305494..bf2f734 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -16,15 +16,49 @@ if [ -n "${PUID}" ] && [ -n "${PGID}" ]; then echo "Detected Alpine Linux, using busybox usermod/groupmod..." # Update group ID first - if [ "$(getent group ${USERNAME} | cut -d: -f3)" != "${PGID}" ]; then - echo "Updating GID to ${PGID}..." - groupmod -g ${PGID} ${USERNAME} || echo "⚠️ Failed to change GID" - fi - - # Update user ID - if [ "$(id -u ${USERNAME})" != "${PUID}" ]; then - echo "Updating UID to ${PUID}..." - usermod -u ${PUID} ${USERNAME} || echo "⚠️ Failed to change UID" + CURRENT_GID=$(getent group ${USERNAME} | cut -d: -f3) + CURRENT_UID=$(id -u ${USERNAME}) + + if [ "${CURRENT_GID}" != "${PGID}" ] || [ "${CURRENT_UID}" != "${PUID}" ]; then + echo "Attempting to update UID/GID to ${PUID}:${PGID} using delete/recreate..." + + # Delete existing user and group, ignoring errors + deluser ${USERNAME} > /dev/null 2>&1 || true + delgroup ${USERNAME} > /dev/null 2>&1 || true + + # Add group with the specified GID + echo "Adding group ${USERNAME} with GID ${PGID}" + if ! addgroup -g ${PGID} ${USERNAME}; then + echo "⚠️ Failed to add group ${USERNAME} with GID ${PGID}." + # Exiting because user creation will likely fail + exit 1 + fi + + # Add user with the specified UID and GID + # Use -G for primary group with adduser in BusyBox + # Use -h /app for home directory (consistent with expectations) + # Use -s /bin/sh for shell + # Use -D for no password (system user) + echo "Adding user ${USERNAME} with UID ${PUID}" + if ! adduser -u ${PUID} -G ${USERNAME} -h /app -s /bin/sh -D ${USERNAME}; then + echo "⚠️ Failed to add user ${USERNAME} with UID ${PUID} and group ${USERNAME}." + # Exiting because the application cannot run as the correct user + exit 1 + fi + + # Verify the change + FINAL_UID=$(id -u ${USERNAME} 2>/dev/null || echo "error") + FINAL_GID=$(getent group ${USERNAME} | cut -d: -f3 2>/dev/null || echo "error") + + if [ "${FINAL_UID}" = "${PUID}" ] && [ "${FINAL_GID}" = "${PGID}" ]; then + echo "✅ Successfully updated UID/GID to ${PUID}:${PGID}" + else + echo "⚠️ Verification failed after update. Target: ${PUID}:${PGID}, Actual: ${FINAL_UID}:${FINAL_GID}" + # Exiting because the UID/GID is not correct + exit 1 + fi + else + echo "UID/GID ${PUID}:${PGID} already set." fi else echo "Non-Alpine system, using standard user management..."