-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_credentials.py
executable file
·126 lines (105 loc) · 3.61 KB
/
generate_credentials.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!env/bin/python3
# generate_credentials.py
import secrets
import sqlite3
import getpass
import argparse
import sys
import app.config
from werkzeug.security import generate_password_hash
def upsert_setting(name, value, conn):
if value is None:
return
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO settings (name, value)
VALUES (?, ?)
ON CONFLICT(name) DO UPDATE SET value=excluded.value;
""",
(name, value),
)
conn.commit()
def create_settings(conn):
create_settings_table = '''
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
value TEXT NOT NULL
);
'''
cursor = conn.cursor()
cursor.execute(create_settings_table)
conn.commit()
def generate_credentials(args):
# Use the provided or default database path
database_path = app.config.get_setting("DATABASE_PATH", "data/glimpser.db")
if args and args.db_path:
database_path = args.db_path
conn = sqlite3.connect(database_path)
if args is None or (not args.update_password and not args.update_key):
create_settings(conn)
# Handle each setting individually
if args is None or args.username:
username = app.config.get_setting("USER_NAME", "admin")
if args:
username = args.username
else:
if sys.stdin.isatty():
username = input(f"Enter the username for login [{app.config.get_setting('USER_NAME', 'admin')}]: ") or app.config.get_setting("USER_NAME", "admin")
else:
username = app.config.get_setting("USER_NAME", "admin")
upsert_setting("USER_NAME", username.strip(), conn)
if args is None or args.password or args.update_password:
password = "" # maybe populate with garbage
if args:
password = args.password
else:
if sys.stdin.isatty():
password = getpass.getpass("Enter the password for login: ")
else:
password = secrets.token_hex(16)
# your password is here. This is the only time youll be able to see it again
password_hash = generate_password_hash(password.strip())
upsert_setting("USER_PASSWORD_HASH", password_hash, conn)
if args is None or args.update_key or not args.update_password:
secret_key = app.config.get_setting("SECRET_KEY", secrets.token_hex(16))
if args and args.secret_key:
secret_key = args.secret_key
upsert_setting("SECRET_KEY", secret_key, conn)
conn.close()
print("Credentials and settings updated in the database.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate or update credentials and settings.")
parser.add_argument(
"--db-path",
type=str,
help="Path to the SQLite database file."
)
parser.add_argument(
"--username",
type=str,
help="Username for login."
)
parser.add_argument(
"--password",
type=str,
help="Password for login."
)
parser.add_argument(
"--update-password",
action="store_true",
help="Update the password only, without creating a new database or changing other settings."
)
parser.add_argument(
"--secret-key",
type=str,
help="Custom secret key. Generates a new one if not provided."
)
parser.add_argument(
"--update-key",
action="store_true",
help="Update the secret key."
)
args = parser.parse_args()
generate_credentials(args)