Add admin user creation on first run
This commit is contained in:
+4
-2
@@ -48,9 +48,11 @@ EXPOSE 80 4000 3306
|
|||||||
|
|
||||||
ENV DB_HOST=localhost
|
ENV DB_HOST=localhost
|
||||||
ENV DB_PORT=3306
|
ENV DB_PORT=3306
|
||||||
ENV DB_USER=root
|
ENV DB_USER=hospital
|
||||||
ENV DB_PASSWORD=
|
ENV DB_PASSWORD=hospital123
|
||||||
ENV DB_NAME=hospital
|
ENV DB_NAME=hospital
|
||||||
|
ENV ADMIN_DNI=00000000
|
||||||
|
ENV ADMIN_PASSWORD=Admin123!
|
||||||
|
|
||||||
ENTRYPOINT ["/docker-entrypoint.sh"]
|
ENTRYPOINT ["/docker-entrypoint.sh"]
|
||||||
CMD ["nginx.sh", "node", "server/api-mariadb.js"]
|
CMD ["nginx.sh", "node", "server/api-mariadb.js"]
|
||||||
@@ -12,4 +12,6 @@ services:
|
|||||||
- DB_USER=hospital
|
- DB_USER=hospital
|
||||||
- DB_PASSWORD=hospital123
|
- DB_PASSWORD=hospital123
|
||||||
- DB_NAME=hospital
|
- DB_NAME=hospital
|
||||||
|
- ADMIN_DNI=00000000
|
||||||
|
- ADMIN_PASSWORD=Admin123!
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
@@ -51,5 +51,55 @@ if [ "$1" = "nginx.sh" ]; then
|
|||||||
exec "$@"
|
exec "$@"
|
||||||
fi
|
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
|
# Run the command passed to the container
|
||||||
exec "$@"
|
exec "$@"
|
||||||
Reference in New Issue
Block a user