-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathset_admin_passwd.py
executable file
·49 lines (40 loc) · 1.61 KB
/
set_admin_passwd.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
#!/usr/bin/env -S BACKEND_ENV=config.env python3
from data_adapters.sql.adapter import SQLAdapter
from data_adapters.sql.create_tables import Users
from utils.password_hashing import hash_password
from utils.settings import settings
import json
import getpass
import subprocess
import utils.regex as regex
import re
from sqlmodel import select
users : dict[str, dict]= {"dmart":{}, "alibaba": {}}
while True:
password = getpass.getpass("Enter the admin/testuser password then hit enter: ")
if re.match(regex.PASSWORD, password):
break
else:
print("Password didn't match the rules: >= 8 chars that are Alphanumeric mix cap/small with _#@%*!?$^- ")
print("Generating and storing the password for admin and testuser")
hashed = hash_password(password)
if settings.active_data_db == "file":
for key in users.keys():
file_name = settings.spaces_folder / f"management/users/.dm/{key}/meta.user.json"
with open(file_name, 'r') as read_file:
data = json.load(read_file)
data["password"] = hashed
with open(file_name, 'w') as write_file:
write_file.write(json.dumps(data))
else:
with SQLAdapter().get_session() as session:
for key in users.keys():
statement = select(Users).where(Users.shortname == key)
user = session.exec(statement).one()
user.password=hashed
user.is_active=True
session.add(user)
session.commit()
with open("./login_creds.sh", 'w') as creds:
subprocess.run( [ "sed", f"s/xxxx/{password}/g", "login_creds.sh.sample" ], stdout=creds)
print("Done")