mirror of
https://github.com/StarFleetCPTN/GoMFT.git
synced 2026-09-08 15:41:20 +02:00
refactor: Enhance user and group management in entrypoint script
- Added checks for existing groups and users before creation to prevent conflicts and improve error handling. - Updated user and group creation logic to use existing identifiers when available, ensuring smoother execution. - Improved ownership setting for application directories and .env file to accommodate potential changes in group names. - Enhanced verification of UID/GID changes with clearer warning messages for better user feedback.
This commit is contained in:
+48
-11
@@ -26,6 +26,14 @@ if [ -n "${PUID}" ] && [ -n "${PGID}" ]; then
|
|||||||
deluser ${USERNAME} > /dev/null 2>&1 || true
|
deluser ${USERNAME} > /dev/null 2>&1 || true
|
||||||
delgroup ${USERNAME} > /dev/null 2>&1 || true
|
delgroup ${USERNAME} > /dev/null 2>&1 || true
|
||||||
|
|
||||||
|
# Check if a group with the target GID already exists
|
||||||
|
EXISTING_GROUP=$(getent group ${PGID} | cut -d: -f1 || echo "")
|
||||||
|
|
||||||
|
if [ -n "${EXISTING_GROUP}" ]; then
|
||||||
|
echo "Group with GID ${PGID} already exists as '${EXISTING_GROUP}', will use this group"
|
||||||
|
# Set USERNAME_GROUP to the existing group name
|
||||||
|
USERNAME_GROUP="${EXISTING_GROUP}"
|
||||||
|
else
|
||||||
# Add group with the specified GID
|
# Add group with the specified GID
|
||||||
echo "Adding group ${USERNAME} with GID ${PGID}"
|
echo "Adding group ${USERNAME} with GID ${PGID}"
|
||||||
if ! addgroup -g ${PGID} ${USERNAME}; then
|
if ! addgroup -g ${PGID} ${USERNAME}; then
|
||||||
@@ -33,22 +41,24 @@ if [ -n "${PUID}" ] && [ -n "${PGID}" ]; then
|
|||||||
# Exiting because user creation will likely fail
|
# Exiting because user creation will likely fail
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
USERNAME_GROUP="${USERNAME}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Add user with the specified UID and GID
|
# Add user with the specified UID and GID
|
||||||
# Use -G for primary group with adduser in BusyBox
|
# Use -G for primary group with adduser in BusyBox
|
||||||
# Use -h /app for home directory (consistent with expectations)
|
# Use -h /app for home directory (consistent with expectations)
|
||||||
# Use -s /bin/sh for shell
|
# Use -s /bin/sh for shell
|
||||||
# Use -D for no password (system user)
|
# Use -D for no password (system user)
|
||||||
echo "Adding user ${USERNAME} with UID ${PUID}"
|
echo "Adding user ${USERNAME} with UID ${PUID} and group ${USERNAME_GROUP}"
|
||||||
if ! adduser -u ${PUID} -G ${USERNAME} -h /app -s /bin/sh -D ${USERNAME}; then
|
if ! adduser -u ${PUID} -G ${USERNAME_GROUP} -h /app -s /bin/sh -D ${USERNAME}; then
|
||||||
echo "⚠️ Failed to add user ${USERNAME} with UID ${PUID} and group ${USERNAME}."
|
echo "⚠️ Failed to add user ${USERNAME} with UID ${PUID} and group ${USERNAME_GROUP}."
|
||||||
# Exiting because the application cannot run as the correct user
|
# Exiting because the application cannot run as the correct user
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Verify the change
|
# Verify the change
|
||||||
FINAL_UID=$(id -u ${USERNAME} 2>/dev/null || echo "error")
|
FINAL_UID=$(id -u ${USERNAME} 2>/dev/null || echo "error")
|
||||||
FINAL_GID=$(getent group ${USERNAME} | cut -d: -f3 2>/dev/null || echo "error")
|
FINAL_GID=$(id -g ${USERNAME} 2>/dev/null || echo "error")
|
||||||
|
|
||||||
if [ "${FINAL_UID}" = "${PUID}" ] && [ "${FINAL_GID}" = "${PGID}" ]; then
|
if [ "${FINAL_UID}" = "${PUID}" ] && [ "${FINAL_GID}" = "${PGID}" ]; then
|
||||||
echo "✅ Successfully updated UID/GID to ${PUID}:${PGID}"
|
echo "✅ Successfully updated UID/GID to ${PUID}:${PGID}"
|
||||||
@@ -79,13 +89,40 @@ if [ -n "${PUID}" ] && [ -n "${PGID}" ]; then
|
|||||||
groupdel ${USERNAME} 2>/dev/null || true
|
groupdel ${USERNAME} 2>/dev/null || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Recreate group and user in the correct order
|
# Check if a group with the target GID already exists
|
||||||
|
EXISTING_GROUP=$(getent group ${PGID} | cut -d: -f1 || echo "")
|
||||||
|
|
||||||
|
if [ -n "${EXISTING_GROUP}" ]; then
|
||||||
|
echo "Group with GID ${PGID} already exists as '${EXISTING_GROUP}', will use this group"
|
||||||
|
# Set USERNAME_GROUP to the existing group name
|
||||||
|
USERNAME_GROUP="${EXISTING_GROUP}"
|
||||||
|
else
|
||||||
|
# Recreate group with the specified GID
|
||||||
echo "Creating group ${USERNAME} with GID ${PGID}"
|
echo "Creating group ${USERNAME} with GID ${PGID}"
|
||||||
groupadd -g ${PGID} ${USERNAME} 2>/dev/null || groupadd ${USERNAME} 2>/dev/null || true
|
groupadd -g ${PGID} ${USERNAME} 2>/dev/null || groupadd ${USERNAME} 2>/dev/null || true
|
||||||
|
USERNAME_GROUP="${USERNAME}"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Creating user ${USERNAME} with UID ${PUID}"
|
# Check if a user with the target UID already exists
|
||||||
useradd -u ${PUID} -g ${USERNAME} -s /bin/sh ${USERNAME} 2>/dev/null ||
|
EXISTING_USER=$(getent passwd ${PUID} | cut -d: -f1 || echo "")
|
||||||
useradd -g ${USERNAME} -s /bin/sh ${USERNAME} 2>/dev/null || true
|
|
||||||
|
if [ -n "${EXISTING_USER}" ] && [ "${EXISTING_USER}" != "${USERNAME}" ]; then
|
||||||
|
echo "⚠️ Warning: User with UID ${PUID} already exists as '${EXISTING_USER}'. Using a different username may cause issues."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Creating user ${USERNAME} with UID ${PUID} and group ${USERNAME_GROUP}"
|
||||||
|
useradd -u ${PUID} -g ${USERNAME_GROUP} -s /bin/sh ${USERNAME} 2>/dev/null ||
|
||||||
|
useradd -g ${USERNAME_GROUP} -s /bin/sh ${USERNAME} 2>/dev/null || true
|
||||||
|
|
||||||
|
# Verify the change
|
||||||
|
FINAL_UID=$(id -u ${USERNAME} 2>/dev/null || echo "error")
|
||||||
|
FINAL_GID=$(id -g ${USERNAME} 2>/dev/null || echo "error")
|
||||||
|
|
||||||
|
if [ "${FINAL_UID}" = "${PUID}" ] && [ "${FINAL_GID}" = "${PGID}" ]; then
|
||||||
|
echo "✅ Successfully updated UID/GID to ${PUID}:${PGID}"
|
||||||
|
else
|
||||||
|
echo "⚠️ Warning: Verification failed. Target: ${PUID}:${PGID}, Actual: ${FINAL_UID}:${FINAL_GID}"
|
||||||
|
fi
|
||||||
} || {
|
} || {
|
||||||
echo "⚠️ Warning: Failed to update UID/GID, continuing with built-in user"
|
echo "⚠️ Warning: Failed to update UID/GID, continuing with built-in user"
|
||||||
}
|
}
|
||||||
@@ -93,17 +130,17 @@ if [ -n "${PUID}" ] && [ -n "${PGID}" ]; then
|
|||||||
|
|
||||||
# Fix ownership of app directories
|
# Fix ownership of app directories
|
||||||
echo "Setting ownership of app directories"
|
echo "Setting ownership of app directories"
|
||||||
chown -R ${USERNAME}:${USERNAME} /app/data /app/backups || echo "⚠️ Warning: Failed to change ownership"
|
chown -R ${USERNAME}:${USERNAME_GROUP:-${USERNAME}} /app/data /app/backups || echo "⚠️ Warning: Failed to change ownership"
|
||||||
|
|
||||||
# Ensure .env file exists and has correct permissions
|
# Ensure .env file exists and has correct permissions
|
||||||
if [ -f /app/.env ]; then
|
if [ -f /app/.env ]; then
|
||||||
echo "Found .env file, setting permissions..."
|
echo "Found .env file, setting permissions..."
|
||||||
chown ${USERNAME}:${USERNAME} /app/.env || echo "⚠️ Warning: Failed to change .env ownership"
|
chown ${USERNAME}:${USERNAME_GROUP:-${USERNAME}} /app/.env || echo "⚠️ Warning: Failed to change .env ownership"
|
||||||
chmod 644 /app/.env || echo "⚠️ Warning: Failed to change .env permissions"
|
chmod 644 /app/.env || echo "⚠️ Warning: Failed to change .env permissions"
|
||||||
else
|
else
|
||||||
echo "No .env file found, creating empty one..."
|
echo "No .env file found, creating empty one..."
|
||||||
touch /app/.env
|
touch /app/.env
|
||||||
chown ${USERNAME}:${USERNAME} /app/.env || echo "⚠️ Warning: Failed to change .env ownership"
|
chown ${USERNAME}:${USERNAME_GROUP:-${USERNAME}} /app/.env || echo "⚠️ Warning: Failed to change .env ownership"
|
||||||
chmod 644 /app/.env || echo "⚠️ Warning: Failed to change .env permissions"
|
chmod 644 /app/.env || echo "⚠️ Warning: Failed to change .env permissions"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user