-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_key.py
More file actions
37 lines (33 loc) · 1.06 KB
/
create_key.py
File metadata and controls
37 lines (33 loc) · 1.06 KB
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
import secrets
import sys
from core.database import SessionLocal
from models.models import APIKey, ModelsModel
from inferences.models import Inference
def create_manual_key(is_admin: bool = False):
db = SessionLocal()
new_key_string = ""
try:
print(f"INFO: Creating new manual key (is_admin={is_admin})...")
new_key_string = secrets.token_hex(32)
db_key = APIKey(
key=new_key_string,
is_active=True,
is_admin=is_admin,
description=f"Manual Key (admin={is_admin})"
)
db.add(db_key)
db.commit()
db.refresh(db_key)
print(f"INFO: Commit successful. Key ID: {db_key.id}, is_admin: {db_key.is_admin}")
print("\n--- API Key ---")
print(new_key_string)
print("---------------\n")
except Exception as e:
print(f"ERROR in create_manual_key: {e}")
db.rollback()
raise
finally:
db.close()
if __name__ == "__main__":
is_admin_flag = "--admin" in sys.argv
create_manual_key(is_admin=is_admin_flag)