Skip to content

fix(admin): honor DEFAULT_SUPERUSER_EMAIL/PASSWORD env vars in bootstrap (#16876) - #17063

Closed
Taranum01 wants to merge 1 commit into
infiniflow:mainfrom
Taranum01:fix/16876-admin-default-superuser-env
Closed

Taranum01 wants to merge 1 commit into
infiniflow:mainfrom
Taranum01:fix/16876-admin-default-superuser-env

Conversation

@Taranum01

Copy link
Copy Markdown
Contributor

Summary

Fixes #16876 — admin server advertises
DEFAULT_SUPERUSER_EMAIL and DEFAULT_SUPERUSER_PASSWORD through
services.EnvironmentsMgr.get_all() (and the admin CLI's "list
envs"), so operators reasonably expect the values they set in the
environment to drive the bootstrap admin account. The legacy
implementation hardcoded "[email protected]" / "admin" in
init_default_admin() and check_admin() instead — the env var read
as effective while the behaviour stayed fixed.

Fix

Wire the env vars through, matching the existing convention from
api/db/init_data.py:init_superuser (which has read these vars since
it was introduced):

  • Read DEFAULT_SUPERUSER_NICKNAME, DEFAULT_SUPERUSER_EMAIL, and
    DEFAULT_SUPERUSER_PASSWORD at module load, defaulting to the
    legacy hardcoded pair when unset so existing deployments keep
    working byte-for-byte.
  • init_default_admin() uses the env values for the bootstrap row
    and the default_admin_rows filter (which was also hardcoded).
  • check_admin() uses the env values for the on-the-fly "create on
    first login" branch.

Defaults are preserved so existing deployments are not disrupted:
DEFAULT_SUPERUSER_EMAIL defaults to "[email protected]" and
DEFAULT_SUPERUSER_PASSWORD defaults to "admin", exactly matching
the pre-#16876 behaviour.

Tests

New test/unit_test/admin/test_auth_env.py pins the env-var wiring
both at the source level (no hardcoded "[email protected]" /
"admin" inside the function bodies) and at the behavioural level
(a self-contained replica verifies the env values reach the
bootstrap row). 7 tests, all pass.

The behavioural replica exists because admin/server/auth.py
transitively imports the full api db layer (peewee, quart_auth,
xxhash, ...), which is heavy and orthogonal to the env-var wiring
under test. The replica mirrors the production code line-for-line so
a future refactor that breaks the wiring will still fail the textual
assertions above, even before running the behavioural check.

Files

 admin/server/auth.py                              | 25 ++++++++++++++++-----
 test/unit_test/admin/test_auth_env.py             | 242 ++++++++++++++++++++++++++
 2 files changed, 259 insertions(+), 7 deletions(-)

Refs #16876

Made with Cursor

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. 🌈 python Pull requests that update Python code 🐞 bug Something isn't working, pull request that fix bug. 🧪 test Pull requests that update test cases. labels Jul 17, 2026
@coderabbitai

coderabbitai Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 701b9bcc-73af-4350-8af0-7a687de86680

📥 Commits

Reviewing files that changed from the base of the PR and between 39c4d45 and 3140dd6.

📒 Files selected for processing (4)
  • admin/server/auth.py
  • test/unit_test/admin/conftest.py
  • test/unit_test/admin/test_auth_env.py
  • test/unit_test/admin/test_isolation_check.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/unit_test/admin/conftest.py

📝 Walkthrough

Walkthrough

Admin bootstrap credentials are configurable through environment variables with legacy defaults. Initialization and first-login authentication use the configured values, while tests cover credential loading, bootstrap behavior, and module isolation.

Changes

Admin bootstrap configuration

Layer / File(s) Summary
Environment-driven bootstrap wiring
admin/server/auth.py
Adds environment-backed superuser constants and uses them for account creation, configured-account lookup, and first-login bootstrap.
Isolated auth test harness
test/unit_test/admin/conftest.py
Stubs transitive imports and reloads the real authentication module with fresh mocked services for environment-sensitive tests.
Bootstrap behavior coverage
test/unit_test/admin/test_auth_env.py
Validates configured and default credentials, password encoding, admin initialization states, email passthrough, and existing-user handling.
Module isolation validation
test/unit_test/admin/test_isolation_check.py
Checks that authentication stubs and reloaded modules do not leak through sys.modules.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant AuthModule
  participant UserService
  participant TenantService
  AuthModule->>AuthModule: Read DEFAULT_SUPERUSER_* environment variables
  AuthModule->>UserService: Create or find configured superuser
  AuthModule->>TenantService: Backfill tenant for an existing active admin
Loading

Poem

I’m a rabbit with credentials tucked neat,
Env-vars now guide each bootstrap seat.
Old defaults remain in their familiar place,
Tests hop along to verify the case.
Nibble, deploy, and login with grace!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: honoring DEFAULT_SUPERUSER_EMAIL/PASSWORD env vars in admin bootstrap.
Description check ✅ Passed The PR description includes a clear summary of the problem, the fix, and testing notes, matching the template's required summary section.
Linked Issues check ✅ Passed The code now reads the env vars in both bootstrap paths and preserves legacy defaults, satisfying the objective in #16876.
Out of Scope Changes check ✅ Passed The added tests and fixtures directly support the admin bootstrap env-var change and do not introduce unrelated scope.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@admin/server/auth.py`:
- Around line 39-48: Remove compatibility-focused wording from comments and
documentation at admin/server/auth.py lines 39-48, 122-128, and 205-214, and
test/unit_test/admin/test_auth_env.py lines 16-42, 82-86, 105-111, 122-127, and
195-196. Keep only concise descriptions of the current environment-driven
bootstrap behavior and test intent; remove references to legacy implementations,
hardcoded values, prior behavior, preserved defaults, existing deployments, and
backward compatibility.

In `@test/unit_test/admin/test_auth_env.py`:
- Around line 138-185: Replace the source-regex checks and behavioral replica
helpers such as _read_default_superuser_constants and
_init_default_admin_replica with tests that import and execute the production
init_default_admin and check_admin functions from admin/server/auth.py. Patch
heavy dependencies including UserService and TenantService with unittest.mock so
the tests remain isolated without duplicating implementation logic, and assert
the real functions honor the configured default-superuser environment
credentials.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 243bf938-9914-4a92-977c-78de9bc86ea2

📥 Commits

Reviewing files that changed from the base of the PR and between 1c0432a and a574c76.

📒 Files selected for processing (2)
  • admin/server/auth.py
  • test/unit_test/admin/test_auth_env.py

Comment thread admin/server/auth.py Resolved
Comment thread test/unit_test/admin/test_auth_env.py Resolved
@Taranum01
Taranum01 force-pushed the fix/16876-admin-default-superuser-env branch from a574c76 to 275a8e5 Compare July 17, 2026 21:17

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/unit_test/admin/conftest.py`:
- Around line 79-84: Update _install_stub_modules and _ensure_module so stub
modules are created and registered through the fixture’s
monkeypatch.setitem(sys.modules, ...) per test, rather than mutating or
retaining real modules during conftest import. Ensure the fixture restores or
removes the cached admin.server.auth module during teardown and resets shared
mock state, preserving test isolation and preventing order-dependent behavior.

In `@test/unit_test/admin/test_auth_env.py`:
- Around line 186-210: Update
test_check_admin_creates_admin_from_env_on_first_login to pass the configured
password "s3cret!@#" as the second check_admin argument instead of the email,
and assert that query_user was called with the expected credentials so
password-propagation regressions are detected.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a8a11b47-3f61-483e-9f37-41fdb3994d7f

📥 Commits

Reviewing files that changed from the base of the PR and between a574c76 and 275a8e5.

📒 Files selected for processing (3)
  • admin/server/auth.py
  • test/unit_test/admin/conftest.py
  • test/unit_test/admin/test_auth_env.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • admin/server/auth.py

Comment thread test/unit_test/admin/conftest.py Resolved
Comment thread test/unit_test/admin/test_auth_env.py Resolved
@Taranum01
Taranum01 force-pushed the fix/16876-admin-default-superuser-env branch from 275a8e5 to 7b091de Compare July 17, 2026 21:58

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/unit_test/admin/conftest.py`:
- Around line 230-234: The auth-module fixture in
test/unit_test/admin/conftest.py lines 230-234 must register the imported module
via monkeypatch.setitem instead of direct sys.modules assignment. In
test/unit_test/admin/test_isolation_check.py lines 44-57, explicitly undo that
monkeypatch and assert that sys.modules["admin.server.auth"] is restored to its
original module identity.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3baf2fba-7c3b-4273-9649-d8240e24b361

📥 Commits

Reviewing files that changed from the base of the PR and between 275a8e5 and 7b091de.

📒 Files selected for processing (4)
  • admin/server/auth.py
  • test/unit_test/admin/conftest.py
  • test/unit_test/admin/test_auth_env.py
  • test/unit_test/admin/test_isolation_check.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • admin/server/auth.py

Comment thread test/unit_test/admin/conftest.py Resolved
@Taranum01
Taranum01 force-pushed the fix/16876-admin-default-superuser-env branch from 7b091de to 39c4d45 Compare July 17, 2026 23:46

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/unit_test/admin/conftest.py`:
- Around line 201-208: Replace the constant-name setattr calls in the fixture
setup with direct attribute assignments on api, api_common, api_utils, api_db,
and api_db_services, preserving the existing assigned modules and behavior.
Apply the same change to the additional occurrence and run the narrowest
relevant Ruff validation.

In `@test/unit_test/admin/test_isolation_check.py`:
- Around line 81-85: Update the module-skip condition in the isolation-check
logic to continue only when the module is currently absent and was also absent
during collection. If it existed at collection time but is now missing, allow
execution to reach the _identity_restored check so the removal is detected.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3f5d91a4-3bbb-4317-83f3-40f2d63d857c

📥 Commits

Reviewing files that changed from the base of the PR and between 7b091de and 39c4d45.

📒 Files selected for processing (4)
  • admin/server/auth.py
  • test/unit_test/admin/conftest.py
  • test/unit_test/admin/test_auth_env.py
  • test/unit_test/admin/test_isolation_check.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • admin/server/auth.py

Comment thread test/unit_test/admin/conftest.py Resolved
…rap (infiniflow#16876)

The admin server advertised `DEFAULT_SUPERUSER_EMAIL` and
`DEFAULT_SUPERUSER_PASSWORD` through `services.EnvironmentsMgr.get_all()`
(and the admin CLI's "list envs"), so operators reasonably expected the
values they set in the environment to drive the bootstrap admin
account. The legacy implementation hardcoded `"[email protected]"` /
`"admin"` in `init_default_admin()` and `check_admin()` instead — the
env var read as effective while the behaviour stayed fixed.

This wires the env vars through, matching the existing convention
from `api/db/init_data.py:init_superuser` (which has read these vars
since it was introduced):

- Read `DEFAULT_SUPERUSER_NICKNAME`, `DEFAULT_SUPERUSER_EMAIL`, and
  `DEFAULT_SUPERUSER_PASSWORD` at module load, defaulting to the
  legacy hardcoded pair when unset so existing deployments keep
  working byte-for-byte.
- `init_default_admin()` uses the env values for the bootstrap row
  and the `default_admin_rows` filter (which was also hardcoded).
- `check_admin()` uses the env values for the on-the-fly "create on
  first login" branch.

Tests:

- New `test/unit_test/admin/conftest.py` installs lightweight
  `sys.modules` stubs for the api db layer so the real
  `admin.server.auth` module can be loaded in-process without
  pulling in the full database stack. The stubs are wired into
  `monkeypatch.setitem` (including the freshly imported
  `admin.server.auth`) so each test starts from the same
  `sys.modules` state it had at collection time. Stub cross-links
  between parent and child modules use direct attribute assignment
  rather than `setattr` with constant names to keep Ruff B010
  quiet.
- New `test/unit_test/admin/test_auth_env.py` exercises the real
  `init_default_admin` / `check_admin` production functions
  end-to-end against the real source, with `UserService`,
  `TenantService`, and `UserTenantService` patched to `MagicMock`
  per-test. 12 tests cover: default values when env is unset,
  env-driven credentials when set, password encoding via
  `encode_to_base64`, the inactive-admin guard, the existing-admin
  tenant-backfill path, and the no-bootstrap path when the user
  already exists.
- New `test/unit_test/admin/test_isolation_check.py` snapshots
  `sys.modules` at collection time and asserts each stubbed module
  has been restored to its prior identity (and, for any name that
  was present at collection time, is still present) so a future
  regression that leaks or deletes a stub is caught immediately.

All 14 tests pass; `ruff check` and `ruff format --check` are clean.

Refs infiniflow#16876

Signed-off-by: Taranum Wasu <[email protected]>
Co-authored-by: Cursor <[email protected]>
@Taranum01
Taranum01 force-pushed the fix/16876-admin-default-superuser-env branch from 39c4d45 to 3140dd6 Compare July 17, 2026 23:59
@Taranum01

Copy link
Copy Markdown
Contributor Author

Closing stale bot feedback — All review rounds addressed across multiple amended commits on this branch — production-code tests (no replica), per-test stub isolation, identity-based isolation check, monkeypatch registration for admin.server.auth restoration. CodeRabbit confirmed each round.

@Taranum01

Copy link
Copy Markdown
Contributor Author

Closing as superseded. Branch is too far behind main to rebase cleanly: the PR's test scaffolding (reload_admin_auth fixture with sys.modules stubbing) conflicts with current test/unit_test/admin/conftest.py (basic sys.path manipulation). Will open a fresh PR against main with just the admin/server/auth.py core change and re-add the tests on the new fixture shape.

@Taranum01 Taranum01 closed this Jul 31, 2026
Taranum01 pushed a commit to Taranum01/ragflow that referenced this pull request Jul 31, 2026
Three follow-ups from the review on infiniflow#17674:

1. **Comment correctness** (`services.EnvironmentsMgr.get_all` returns
   only `DEFAULT_SUPERUSER_EMAIL`, not the password — the previous
   comment implied both were listed, which would mislead an operator
   reading the env-listing output and expect the password to appear
   there too).

2. **Reject blank/whitespace env values** via a `_required_env` helper
   before defining the three module-level constants. Without this,
   `DEFAULT_SUPERUSER_PASSWORD=` would silently persist an empty
   password (os.getenv uses fallback only when *unset*, not when
   empty), making every first login fail with the misleading
   'credentials do not match' error rather than a startup-time
   configuration error.

3. **Admin email mismatch in `init_default_admin`** — when active
   superusers exist but none match `DEFAULT_SUPERUSER_EMAIL`, raise
   an explicit `AdminException` with migration guidance (run the
   API-side `--init-superuser` to align) instead of silently skipping
   the tenant backfill. The previous behaviour diverged the
   bootstrap credential from the env value without telling anyone.

Item 4 (check_admin auth-bypass) does not apply to this PR — the
function referenced in the review was in the closed predecessor
infiniflow#17063, which was superseded by this PR that only touches
`init_default_admin`.
Taranum01 pushed a commit to Taranum01/ragflow that referenced this pull request Aug 23, 2026
Supersedes infiniflow#17063 (closed as superseded due to unrebasable test
fixture drift). This is a fresh port against current `main` of the
core change only; test scaffolding omitted because the previous PR's
reload_admin_auth fixture conflicts with the conftest.py shape that
has since evolved on main.

## Fix

The admin server's bootstrap (`init_default_admin`) and first-login
fallback (`check_admin`) hardcoded [email protected] / admin / admin.
The admin CLI's "list envs" output and the EnvironmentsMgr API both
advertise DEFAULT_SUPERUSER_EMAIL/PASSWORD/NICKNAME as configurable,
but the bootstrap ignored them — operators setting custom env values
saw the docs claim support while the code still created the default
admin. Matches what `api/db/init_data.py:init_superuser` already does
on the API side.

## Change

- Three module-level constants read from env at import time:
  - DEFAULT_SUPERUSER_NICKNAME (default "admin")
  - DEFAULT_SUPERUSER_EMAIL (default "[email protected]")
  - DEFAULT_SUPERUSER_PASSWORD (default "admin")
- `init_default_admin`: superuser row uses constants.
- `init_default_admin`: existing-row lookup uses DEFAULT_SUPERUSER_EMAIL.
- `check_admin`: first-login fallback uses constants.

Refs infiniflow#16876
Taranum01 pushed a commit to Taranum01/ragflow that referenced this pull request Aug 23, 2026
Three follow-ups from the review on infiniflow#17674:

1. **Comment correctness** (`services.EnvironmentsMgr.get_all` returns
   only `DEFAULT_SUPERUSER_EMAIL`, not the password — the previous
   comment implied both were listed, which would mislead an operator
   reading the env-listing output and expect the password to appear
   there too).

2. **Reject blank/whitespace env values** via a `_required_env` helper
   before defining the three module-level constants. Without this,
   `DEFAULT_SUPERUSER_PASSWORD=` would silently persist an empty
   password (os.getenv uses fallback only when *unset*, not when
   empty), making every first login fail with the misleading
   'credentials do not match' error rather than a startup-time
   configuration error.

3. **Admin email mismatch in `init_default_admin`** — when active
   superusers exist but none match `DEFAULT_SUPERUSER_EMAIL`, raise
   an explicit `AdminException` with migration guidance (run the
   API-side `--init-superuser` to align) instead of silently skipping
   the tenant backfill. The previous behaviour diverged the
   bootstrap credential from the env value without telling anyone.

Item 4 (check_admin auth-bypass) does not apply to this PR — the
function referenced in the review was in the closed predecessor
infiniflow#17063, which was superseded by this PR that only touches
`init_default_admin`.
Taranum01 pushed a commit to Taranum01/ragflow that referenced this pull request Aug 23, 2026
Supersedes infiniflow#17063 (closed as superseded due to unrebasable test
fixture drift). This is a fresh port against current `main` of the
core change only; test scaffolding omitted because the previous PR's
reload_admin_auth fixture conflicts with the conftest.py shape that
has since evolved on main.

## Fix

The admin server's bootstrap (`init_default_admin`) and first-login
fallback (`check_admin`) hardcoded [email protected] / admin / admin.
The admin CLI's "list envs" output and the EnvironmentsMgr API both
advertise DEFAULT_SUPERUSER_EMAIL/PASSWORD/NICKNAME as configurable,
but the bootstrap ignored them — operators setting custom env values
saw the docs claim support while the code still created the default
admin. Matches what `api/db/init_data.py:init_superuser` already does
on the API side.

## Change

- Three module-level constants read from env at import time:
  - DEFAULT_SUPERUSER_NICKNAME (default "admin")
  - DEFAULT_SUPERUSER_EMAIL (default "[email protected]")
  - DEFAULT_SUPERUSER_PASSWORD (default "admin")
- `init_default_admin`: superuser row uses constants.
- `init_default_admin`: existing-row lookup uses DEFAULT_SUPERUSER_EMAIL.
- `check_admin`: first-login fallback uses constants.

Refs infiniflow#16876
Taranum01 pushed a commit to Taranum01/ragflow that referenced this pull request Aug 23, 2026
Three follow-ups from the review on infiniflow#17674:

1. **Comment correctness** (`services.EnvironmentsMgr.get_all` returns
   only `DEFAULT_SUPERUSER_EMAIL`, not the password — the previous
   comment implied both were listed, which would mislead an operator
   reading the env-listing output and expect the password to appear
   there too).

2. **Reject blank/whitespace env values** via a `_required_env` helper
   before defining the three module-level constants. Without this,
   `DEFAULT_SUPERUSER_PASSWORD=` would silently persist an empty
   password (os.getenv uses fallback only when *unset*, not when
   empty), making every first login fail with the misleading
   'credentials do not match' error rather than a startup-time
   configuration error.

3. **Admin email mismatch in `init_default_admin`** — when active
   superusers exist but none match `DEFAULT_SUPERUSER_EMAIL`, raise
   an explicit `AdminException` with migration guidance (run the
   API-side `--init-superuser` to align) instead of silently skipping
   the tenant backfill. The previous behaviour diverged the
   bootstrap credential from the env value without telling anyone.

Item 4 (check_admin auth-bypass) does not apply to this PR — the
function referenced in the review was in the closed predecessor
infiniflow#17063, which was superseded by this PR that only touches
`init_default_admin`.
Taranum01 pushed a commit to Taranum01/ragflow that referenced this pull request Aug 23, 2026
Supersedes infiniflow#17063 (closed as superseded due to unrebasable test
fixture drift). This is a fresh port against current `main` of the
core change only; test scaffolding omitted because the previous PR's
reload_admin_auth fixture conflicts with the conftest.py shape that
has since evolved on main.

## Fix

The admin server's bootstrap (`init_default_admin`) and first-login
fallback (`check_admin`) hardcoded [email protected] / admin / admin.
The admin CLI's "list envs" output and the EnvironmentsMgr API both
advertise DEFAULT_SUPERUSER_EMAIL/PASSWORD/NICKNAME as configurable,
but the bootstrap ignored them — operators setting custom env values
saw the docs claim support while the code still created the default
admin. Matches what `api/db/init_data.py:init_superuser` already does
on the API side.

## Change

- Three module-level constants read from env at import time:
  - DEFAULT_SUPERUSER_NICKNAME (default "admin")
  - DEFAULT_SUPERUSER_EMAIL (default "[email protected]")
  - DEFAULT_SUPERUSER_PASSWORD (default "admin")
- `init_default_admin`: superuser row uses constants.
- `init_default_admin`: existing-row lookup uses DEFAULT_SUPERUSER_EMAIL.
- `check_admin`: first-login fallback uses constants.

Refs infiniflow#16876
Taranum01 pushed a commit to Taranum01/ragflow that referenced this pull request Aug 23, 2026
Three follow-ups from the review on infiniflow#17674:

1. **Comment correctness** (`services.EnvironmentsMgr.get_all` returns
   only `DEFAULT_SUPERUSER_EMAIL`, not the password — the previous
   comment implied both were listed, which would mislead an operator
   reading the env-listing output and expect the password to appear
   there too).

2. **Reject blank/whitespace env values** via a `_required_env` helper
   before defining the three module-level constants. Without this,
   `DEFAULT_SUPERUSER_PASSWORD=` would silently persist an empty
   password (os.getenv uses fallback only when *unset*, not when
   empty), making every first login fail with the misleading
   'credentials do not match' error rather than a startup-time
   configuration error.

3. **Admin email mismatch in `init_default_admin`** — when active
   superusers exist but none match `DEFAULT_SUPERUSER_EMAIL`, raise
   an explicit `AdminException` with migration guidance (run the
   API-side `--init-superuser` to align) instead of silently skipping
   the tenant backfill. The previous behaviour diverged the
   bootstrap credential from the env value without telling anyone.

Item 4 (check_admin auth-bypass) does not apply to this PR — the
function referenced in the review was in the closed predecessor
infiniflow#17063, which was superseded by this PR that only touches
`init_default_admin`.
JinHai-CN added a commit that referenced this pull request Aug 25, 2026
…rap (#17674)

Supersedes #17063 (closed as superseded due to unrebasable test-fixture
drift).

## Summary

Fix admin bootstrap to respect DEFAULT_SUPERUSER_EMAIL/PASSWORD/NICKNAME env vars. Previously hardcoded defaults ignored custom values. Now reads env at import time and keys superuser lookup off configured email. Core fix only; tests excluded due to fixture conflicts.

Co-authored-by: Taranum01 <[email protected]>
Co-authored-by: Taranum01 <[email protected]>
Co-authored-by: Jin Hai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. 🌈 python Pull requests that update Python code size:M This PR changes 30-99 lines, ignoring generated files. 🧪 test Pull requests that update test cases.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin server surfaces DEFAULT_SUPERUSER_EMAIL via 'list envs' but init_default_admin() hardcodes the account

1 participant