File size: 2,045 Bytes
2978bba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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()