MorphGuard / scripts /reset_pg_password.py
juanquy's picture
Initial clean commit of modular MorphGuard
2978bba
Raw
History Blame Contribute Delete
2.05 kB
import os
import sys
import psycopg2
from werkzeug.security import generate_password_hash
# Add parent directory to path to allow importing config if needed, though we use defaults matching auth.py
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Database configuration (matching utils/auth.py defaults)
DB_HOST = os.environ.get('MORPHGUARD_DB_HOST', 'localhost')
DB_PORT = int(os.environ.get('MORPHGUARD_DB_PORT', 5432))
DB_NAME = os.environ.get('MORPHGUARD_DB_NAME', 'morphguard')
DB_USER = os.environ.get('MORPHGUARD_DB_USER', 'morphguard')
DB_PASS = os.environ.get('MORPHGUARD_DB_PASS', 'morphguard')
NEW_PASSWORD = "morphguard_admin"
def reset_password():
print(f"Connecting to database: {DB_NAME} on {DB_HOST} with user {DB_USER}")
try:
conn = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME,
user=DB_USER,
password=DB_PASS
)
cur = conn.cursor()
# Check if admin exists
cur.execute("SELECT id FROM users WHERE username = 'admin';")
row = cur.fetchone()
if not row:
print("User 'admin' does not exist. Creating it.")
pwd_hash = generate_password_hash(NEW_PASSWORD, method='scrypt')
cur.execute(
"INSERT INTO users (username, password_hash, role, active) VALUES (%s, %s, %s, %s)",
('admin', pwd_hash, 'admin', True)
)
else:
print("User 'admin' found. Updating password.")
pwd_hash = generate_password_hash(NEW_PASSWORD, method='scrypt')
cur.execute(
"UPDATE users SET password_hash = %s WHERE username = %s",
(pwd_hash, 'admin')
)
conn.commit()
print(f"✅ Successfully set password for 'admin' to: {NEW_PASSWORD}")
conn.close()
except Exception as e:
print(f"❌ Error: {e}")
if __name__ == "__main__":
reset_password()