From d31f783c13eb6841e2fd24f67bb39d86fcaff4ca Mon Sep 17 00:00:00 2001 From: paul Date: Wed, 23 Jul 2025 15:28:17 +0200 Subject: [PATCH] feat: Add admin password management tools and debug logging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add setAdminPassword.js utility for setting password inside container - Add set-admin-password.sh for easy Docker exec password setting - Add reset-to-default-password.sh for quick testing (sets to admin123) - Add debug logging to password verification for troubleshooting - Improve error handling when password hash is missing These tools make it much easier to manage the admin password without dealing with bcrypt hash generation and .env file formatting issues. Usage: - Set custom password: ./scripts/set-admin-password.sh - Reset to default: ./scripts/reset-to-default-password.sh šŸ¤– Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- backend/src/services/auth.service.js | 19 +++++- backend/src/utils/setAdminPassword.js | 84 +++++++++++++++++++++++++++ scripts/reset-to-default-password.sh | 56 ++++++++++++++++++ scripts/set-admin-password.sh | 38 ++++++++++++ 4 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 backend/src/utils/setAdminPassword.js create mode 100755 scripts/reset-to-default-password.sh create mode 100755 scripts/set-admin-password.sh diff --git a/backend/src/services/auth.service.js b/backend/src/services/auth.service.js index 07043e2..c3a8880 100644 --- a/backend/src/services/auth.service.js +++ b/backend/src/services/auth.service.js @@ -26,7 +26,24 @@ class AuthService { async verifyPassword(password) { try { - const isValid = await bcrypt.compare(password, config.auth.adminPasswordHash); + const hash = config.auth.adminPasswordHash; + + // Debug logging in development + if (config.app.env === 'development') { + logger.debug(`Password verification debug:`, { + hashExists: !!hash, + hashLength: hash ? hash.length : 0, + hashPrefix: hash ? hash.substring(0, 7) : 'none', + passwordLength: password.length + }); + } + + if (!hash) { + logger.error('No admin password hash configured'); + return false; + } + + const isValid = await bcrypt.compare(password, hash); return isValid; } catch (error) { logger.error('Password verification error:', error); diff --git a/backend/src/utils/setAdminPassword.js b/backend/src/utils/setAdminPassword.js new file mode 100644 index 0000000..563802b --- /dev/null +++ b/backend/src/utils/setAdminPassword.js @@ -0,0 +1,84 @@ +#!/usr/bin/env node + +const bcrypt = require('bcrypt'); +const fs = require('fs').promises; +const path = require('path'); +const readline = require('readline'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +async function updateEnvFile(hash) { + const envPath = path.join(__dirname, '../../../.env'); + + try { + // Read the current .env file + let envContent = await fs.readFile(envPath, 'utf8'); + + // Replace or add the ADMIN_PASSWORD_HASH + if (envContent.includes('ADMIN_PASSWORD_HASH=')) { + envContent = envContent.replace(/ADMIN_PASSWORD_HASH=.*$/m, `ADMIN_PASSWORD_HASH=${hash}`); + } else { + envContent += `\nADMIN_PASSWORD_HASH=${hash}\n`; + } + + // Write back to .env + await fs.writeFile(envPath, envContent); + console.log('āœ… Password hash updated in .env file'); + + } catch (error) { + console.error('āš ļø Could not update .env file:', error.message); + console.log('\nPlease manually update your .env file with:'); + console.log(`ADMIN_PASSWORD_HASH=${hash}`); + } +} + +async function setAdminPassword() { + console.log('================================='); + console.log('MinIO WebUI - Set Admin Password'); + console.log('=================================\n'); + + const password = await new Promise((resolve) => { + rl.question('Enter new admin password (min 8 chars): ', (answer) => { + resolve(answer); + }); + }); + + if (!password || password.length < 8) { + console.error('\nāŒ Error: Password must be at least 8 characters long.'); + process.exit(1); + } + + console.log('\nGenerating password hash...'); + + try { + const hash = await bcrypt.hash(password, 12); + + console.log('\n================================='); + console.log('Password Hash Generated:'); + console.log('=================================\n'); + console.log(`Hash: ${hash}\n`); + + // Try to update the .env file + await updateEnvFile(hash); + + console.log('\n================================='); + console.log('āœ… Admin password has been set!'); + console.log('================================='); + console.log('\nYou can now login with:'); + console.log('Username: admin'); + console.log('Password: [the password you just set]'); + console.log('\nNote: You may need to restart the backend for changes to take effect.'); + + } catch (error) { + console.error('āŒ Error generating hash:', error); + process.exit(1); + } + + rl.close(); +} + +// Run the script +setAdminPassword(); \ No newline at end of file diff --git a/scripts/reset-to-default-password.sh b/scripts/reset-to-default-password.sh new file mode 100755 index 0000000..b85baec --- /dev/null +++ b/scripts/reset-to-default-password.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +set -e + +echo "======================================" +echo "MinIO WebUI - Reset to Default Password" +echo "======================================" +echo "" + +# Default password hash for "admin123" +DEFAULT_HASH='$2b$12$LQv7c3SijBJLhSHPDOLkDe7YfPzFaXhJNXLKqFsFC37GA9xc1wSNi' + +# Check if .env exists +if [ ! -f .env ]; then + echo "āŒ Error: .env file not found" + exit 1 +fi + +# Update the .env file +if grep -q "ADMIN_PASSWORD_HASH=" .env; then + # Replace existing hash + if [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + sed -i '' "s/ADMIN_PASSWORD_HASH=.*/ADMIN_PASSWORD_HASH=$DEFAULT_HASH/" .env + else + # Linux + sed -i "s/ADMIN_PASSWORD_HASH=.*/ADMIN_PASSWORD_HASH=$DEFAULT_HASH/" .env + fi +else + # Add hash if not present + echo "ADMIN_PASSWORD_HASH=$DEFAULT_HASH" >> .env +fi + +echo "āœ… Password reset to default!" +echo "" +echo "Default credentials:" +echo " Username: admin" +echo " Password: admin123" +echo "" + +# Restart backend if running +if docker ps | grep -q minio-webui-backend; then + if docker ps | grep -q minio-webui-backend-dev; then + CONTAINER="minio-webui-backend-dev" + else + CONTAINER="minio-webui-backend" + fi + + echo "Restarting backend container..." + docker restart $CONTAINER + echo "āœ… Backend restarted." +fi + +echo "" +echo "You can now login with: admin / admin123" +echo "" \ No newline at end of file diff --git a/scripts/set-admin-password.sh b/scripts/set-admin-password.sh new file mode 100755 index 0000000..a378498 --- /dev/null +++ b/scripts/set-admin-password.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +set -e + +echo "======================================" +echo "MinIO WebUI - Set Admin Password" +echo "======================================" +echo "" + +# Check if docker-compose is running +if ! docker ps | grep -q minio-webui-backend; then + echo "āŒ Error: Backend container is not running." + echo "" + echo "Please start the containers first:" + echo " docker compose -f docker-compose.dev.yml up -d" + exit 1 +fi + +# Determine which container name to use +if docker ps | grep -q minio-webui-backend-dev; then + CONTAINER="minio-webui-backend-dev" +else + CONTAINER="minio-webui-backend" +fi + +echo "Using container: $CONTAINER" +echo "" + +# Run the password setter inside the container +docker exec -it $CONTAINER node src/utils/setAdminPassword.js + +echo "" +echo "Restarting backend container..." +docker restart $CONTAINER + +echo "" +echo "āœ… Backend restarted. You can now login with your new password." +echo "" \ No newline at end of file