From 5761e8339cf32ea9ebfed5222a46fcf6c026a748 Mon Sep 17 00:00:00 2001 From: Sergio Nicolas Lavaise Date: Fri, 24 Apr 2026 13:27:02 -0300 Subject: [PATCH] Add admin user creation on first run --- Dockerfile | 6 ++++-- docker-compose.yml | 2 ++ docker-entrypoint.sh | 50 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index a6fec4d..2b390d3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -48,9 +48,11 @@ EXPOSE 80 4000 3306 ENV DB_HOST=localhost ENV DB_PORT=3306 -ENV DB_USER=root -ENV DB_PASSWORD= +ENV DB_USER=hospital +ENV DB_PASSWORD=hospital123 ENV DB_NAME=hospital +ENV ADMIN_DNI=00000000 +ENV ADMIN_PASSWORD=Admin123! ENTRYPOINT ["/docker-entrypoint.sh"] CMD ["nginx.sh", "node", "server/api-mariadb.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 155b1ed..00c17dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,4 +12,6 @@ services: - DB_USER=hospital - DB_PASSWORD=hospital123 - DB_NAME=hospital + - ADMIN_DNI=00000000 + - ADMIN_PASSWORD=Admin123! restart: unless-stopped \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index a585a89..eb4f693 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -51,5 +51,55 @@ if [ "$1" = "nginx.sh" ]; then exec "$@" fi +# Create initial admin user if not exists +ADMIN_DNI="${ADMIN_DNI:-00000000}" +ADMIN_PASSWORD="${ADMIN_PASSWORD:-Admin123!}" + +echo "Checking for admin user..." +TABLE_EXISTS=$(mysql -h localhost -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -N -e "SHOW TABLES LIKE 'usuarios';" 2>/dev/null || echo "") + +if [ -n "$TABLE_EXISTS" ]; then + USER_COUNT=$(mysql -h localhost -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -N -e "SELECT COUNT(*) FROM usuarios WHERE dni='$ADMIN_DNI';" 2>/dev/null || echo "0") + + if [ "$USER_COUNT" = "0" ]; then + echo "Creating initial admin user..." + + # Generate UUID for user + ADMIN_ID=$(cat /proc/sys/kernel/random/uuid) + TODAY=$(date +%Y-%m-%d) + + # Use bcrypt hash - default password for bcrypt is $2a$10$ (10 rounds) + # Hash for "Admin123!" - this is a pre-computed hash, admin should change it + BCRYPT_HASH='$2a$10$8kOhKfxFzN9YxJxGfJ5EwONz1JQJNKJqk1JqNKJqNKJqNKJqNKJqu' + + # Insert with a temp hash that won't work - we'll display the credentials to set manually + mysql -h localhost -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -e " + INSERT INTO usuarios (id, apellido, nombre, dni, fechaNacimiento, email, rol, passwordHash, fechaCreacion) + VALUES ('$ADMIN_ID', 'Administrador', 'Sistema', '$ADMIN_DNI', '$TODAY', 'admin@hospital.local', 'admin', '', '$TODAY'); + " + + echo "==========================================" + echo "ADMIN CREDENTIALS (change password on first login):" + echo "DNI: $ADMIN_DNI" + echo "Password: $ADMIN_PASSWORD" + echo "==========================================" + + # Now update with correct bcrypt hash + node -e " + const bcrypt = require('bcryptjs'); + const hash = bcrypt.hashSync('$ADMIN_PASSWORD', 10); + console.log(hash); + " > /tmp/hash.txt 2>/dev/null || true + + HASH=$(cat /tmp/hash.txt 2>/dev/null || echo "") + if [ -n "$HASH" ]; then + mysql -h localhost -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" -e "UPDATE usuarios SET passwordHash='$HASH' WHERE dni='$ADMIN_DNI';" + echo "Admin user created with password hash." + fi + else + echo "Admin user already exists, skipping creation." + fi +fi + # Run the command passed to the container exec "$@" \ No newline at end of file