Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(admin): honor DEFAULT_SUPERUSER_EMAIL/PASSWORD env vars in bootstrap (#17674)

Taranum Wasu430156154505
diff --git a/admin/server/auth.py b/admin/server/auth.py
index 2a9e5cf8f0..8d864c15b4 100644
--- a/admin/server/auth.py
+++ b/admin/server/auth.py
@@ -16,6 +16,7 @@
import logging
+import os
import uuid
from functools import wraps
from datetime import datetime
@@ -36,6 +37,63 @@ from common.connection_utils import sync_construct_response
from common import settings
+# The admin server reports DEFAULT_SUPERUSER_EMAIL via
+# `services.EnvironmentsMgr.get_all()` and the admin CLI's "list envs"
+# output, so operators reasonably expect the env value to drive the
+# bootstrap admin account's email. (DEFAULT_SUPERUSER_PASSWORD is
+# deliberately NOT exposed through the env-listing API — it would
+# leak the bootstrap credential into any audit log that records the
+# full env map.) Read once at module load so the bootstrap functions
+# below can reference the values as module-level constants, matching
+# `api/db/init_data.py:init_superuser`. See infiniflow/ragflow#16876.
+#
+# Reject empty env values explicitly: ``os.getenv(name, fallback)``
+# only uses the fallback when the variable is *unset*, so a misconfigured
+# operator setting ``DEFAULT_SUPERUSER_PASSWORD=`` would otherwise
+# persist an empty password — every first login would silently fail with
+# "credentials don't match" instead of a clear startup error. We
+# deliberately preserve the raw value (no whitespace stripping) so a
+# password like ``" s3cret "`` keeps its leading/trailing whitespace
+# and isn't silently mutated before being hashed/stored. Email/nickname
+# callers strip separately because whitespace there would be invalid.
+def _required_env(name, default):
+ value = os.getenv(name, default)
+ if not value:
+ logging.error(
+ "admin server: %s is set but empty; either unset it (the default %r will be used) or provide a real value",
+ name,
+ default,
+ )
+ raise RuntimeError(f"admin server: {name} is set but empty; either unset it (the default {default!r} will be used) or provide a real value")
+ return value
+
+
+def _required_env_stripped(name, default):
+ """Like ``_required_env`` but rejects whitespace-only values.
+
+ A raw value of ``" "`` would survive the empty-check in
+ ``_required_env`` and then collapse to ``""`` after the caller
+ ``.strip()``s it, propagating an empty nickname/email into the
+ bootstrap flow and silently failing later checks. Strip before
+ the empty check so the failure surfaces at startup with the
+ intended config-error message."""
+ raw = os.getenv(name, default)
+ stripped = raw.strip()
+ if not stripped:
+ logging.error(
+ "admin server: %s is set to whitespace-only value; either unset it (the default %r will be used) or provide a real value",
+ name,
+ default,
+ )
+ raise RuntimeError(f"admin server: {name} is set to whitespace-only value; either unset it (the default {default!r} will be used) or provide a real value")
+ return stripped
+
+
+DEFAULT_SUPERUSER_NICKNAME = _required_env_stripped("DEFAULT_SUPERUSER_NICKNAME", "admin")
+DEFAULT_SUPERUSER_EMAIL = _required_env_stripped("DEFAULT_SUPERUSER_EMAIL", "[email protected]")
+DEFAULT_SUPERUSER_PASSWORD = _required_env("DEFAULT_SUPERUSER_PASSWORD", "admin")
+
+
def setup_auth(login_manager):
@login_manager.request_loader
def load_user(web_request):
@@ -91,10 +149,10 @@ def init_default_admin():
if not users:
default_admin = {
"id": uuid.uuid1().hex,
- "password": encode_to_base64("admin"),
- "nickname": "admin",
+ "password": encode_to_base64(DEFAULT_SUPERUSER_PASSWORD),
+ "nickname": DEFAULT_SUPERUSER_NICKNAME,
"is_superuser": True,
- "email": "[email protected]",
+ "email": DEFAULT_SUPERUSER_EMAIL,
"creator": "system",
"status": "1",
}
@@ -104,16 +162,53 @@ def init_default_admin():
elif not any([u.is_active == ActiveEnum.ACTIVE.value for u in users]):
raise AdminException("No active admin. Please update 'is_active' in db manually.", 500)
else:
- default_admin_rows = [u for u in users if u.email == "[email protected]"]
+ # Filter existing superuser rows by the configured
+ # ``DEFAULT_SUPERUSER_EMAIL``. Per @Lynn-Inf's review on
+ # PR #17674, this intentionally does NOT also filter on
+ # ``is_active`` — an inactive row matching the configured
+ # email must still be treated as the configured bootstrap
+ # admin, otherwise the tenant-backfill branch above could
+ # silently create a duplicate entry for the (already-existing,
+ # just-disabled) default admin. Activity gating already lives
+ # in ``login_admin`` (``is_active == INACTIVE`` rejects the
+ # session at login time), so the bootstrap path stays
+ # read-only.
+ default_admin_rows = [u for u in users if u.email == DEFAULT_SUPERUSER_EMAIL]
if default_admin_rows:
default_admin = default_admin_rows[0].to_dict()
exist, default_admin_tenant = TenantService.get_by_id(default_admin["id"])
if not exist:
add_tenant_for_admin(default_admin, UserTenantRole.OWNER)
+ elif any(u.email != DEFAULT_SUPERUSER_EMAIL for u in users):
+ # Active superuser(s) exist but none match the configured
+ # DEFAULT_SUPERUSER_EMAIL — the operator set the env var to a
+ # value that doesn't match any existing admin row. Don't
+ # silently fall back to the original "[email protected]"
+ # identity (that would diverge the bootstrap credential
+ # from the env), and don't auto-create a new admin (the
+ # user may have intentionally kept the old admin). Surface
+ # the mismatch with explicit migration guidance.
+ existing_emails = sorted({u.email for u in users})
+ logging.error(
+ "admin server: configured DEFAULT_SUPERUSER_EMAIL (%r) "
+ "does not match any active superuser row; existing admin "
+ "emails: %r. Refusing to bootstrap to avoid diverging the "
+ "credential from the env value.",
+ DEFAULT_SUPERUSER_EMAIL,
+ existing_emails,
+ )
+ raise AdminException(
+ f"Active superuser(s) exist but none match DEFAULT_SUPERUSER_EMAIL "
+ f"({DEFAULT_SUPERUSER_EMAIL!r}). Either unset the env var to "
+ f"keep the existing admin, or run the API-side migration "
+ f"(``python -m api.ragflow_server --init-superuser``) to align "
+ f"the configured email with an existing admin row before "
+ f"restarting.",
+ 500,
+ )
def add_tenant_for_admin(user_info: dict, role: str):
-
tenant = {
"id": user_info["id"],
"name": user_info["nickname"] + "‘s Kingdom",
@@ -181,12 +276,18 @@ def check_admin(username: str, password: str):
users = UserService.query(email=username)
if not users:
logging.info(f"Username: {username} is not registered!")
+ # On first login with an unrecognised username, fall back to the
+ # env-configured default admin bootstrap so an operator who set
+ # ``[email protected]`` gets an admin at
+ # ``[email protected]`` — matching what ``services.EnvironmentsMgr.get_all``
+ # advertises and what the API-side ``init_superuser`` already
+ # does. See infiniflow/ragflow#16876.
user_info = {
"id": uuid.uuid1().hex,
- "password": encode_to_base64("admin"),
- "nickname": "admin",
+ "password": encode_to_base64(DEFAULT_SUPERUSER_PASSWORD),
+ "nickname": DEFAULT_SUPERUSER_NICKNAME,
"is_superuser": True,
- "email": "[email protected]",
+ "email": DEFAULT_SUPERUSER_EMAIL,
"creator": "system",
"status": "1",
}