Skip to content

Commit 4ac0d7c

Browse files
committed
fix: static admin password
1 parent dc52d66 commit 4ac0d7c

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

app/deployer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def check_all_credentials(self):
105105
self.check_credential("SECRET_KEY", is_password=False)
106106
self.check_credential("GF_SECURITY_ADMIN_PASSWORD")
107107
self.check_credential("POSTGRES_PASSWORD")
108+
self.check_credential("NEBULA_ADMIN_PASSWORD")
108109

109110

110111
class NebulaEventHandler(PatternMatchingEventHandler):
@@ -1119,7 +1120,8 @@ def run_controller(self):
11191120
"DB_HOST": self.get_container_name("nebula-database"),
11201121
"DB_PORT": 5432,
11211122
"DB_USER": "nebula",
1122-
"DB_PASSWORD": "nebula",
1123+
"DB_PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
1124+
"NEBULA_ADMIN_PASSWORD": os.environ.get("NEBULA_ADMIN_PASSWORD")
11231125
}
11241126

11251127
volumes = ["/nebula", "/var/run/docker.sock"]

nebula/controller/controller.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from nebula.controller.database import (
2020
init_db_pool,
2121
close_db_pool,
22+
insert_default_admin,
2223
scenario_set_all_status_to_finished,
2324
scenario_set_status_to_finished,
2425
)
@@ -120,6 +121,7 @@ async def lifespan(app: FastAPI):
120121

121122
# Initialize the database connection pool
122123
await init_db_pool()
124+
await insert_default_admin()
123125

124126
yield
125127

nebula/controller/database.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ async def close_db_pool():
5353

5454
# --- User Management Functions ---
5555

56+
async def insert_default_admin():
57+
"""
58+
Inserts a default 'ADMIN' user into the database with a hashed password.
59+
The password must be provided via the ADMIN_PASSWORD environment variable.
60+
"""
61+
admin_password = os.environ.get("NEBULA_ADMIN_PASSWORD")
62+
63+
hashed_password = pwd_context.hash(admin_password)
64+
65+
query = """
66+
INSERT INTO users ("user", password, role)
67+
VALUES ($1, $2, $3)
68+
ON CONFLICT ("user") DO NOTHING;
69+
"""
70+
try:
71+
async with POOL.acquire() as conn:
72+
await conn.execute(query, "ADMIN", hashed_password, "admin")
73+
logging.info("Default admin user inserted (or already exists).")
74+
except Exception as e:
75+
logging.error(f"Failed to insert default admin user: {e}", exc_info=True)
76+
5677
async def list_users(all_info=False):
5778
"""
5879
Retrieves a list of users from the users database.

nebula/database/init-configs.sql

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,3 @@ CREATE TABLE IF NOT EXISTS notes (
6161
scenario TEXT PRIMARY KEY,
6262
scenario_notes TEXT
6363
);
64-
65-
-- 6) Insert the default 'admin' user with a hashed password
66-
-- The hash must be generated by a Python script using passlib.
67-
-- Replace the placeholder with your generated hash.
68-
INSERT INTO users ("user", password, role) VALUES ('ADMIN', '$argon2id$v=19$m=65536,t=3,p=4$OobPh8BkZeT6D5s+Rt11mQ$JjI2M3U5+4lupdr87/GrIn46ImzoQujNEyVd7IGYiXY', 'admin')
69-
ON CONFLICT ("user") DO NOTHING;

0 commit comments

Comments
 (0)