Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

20.0 KB
Raw Blame

Why

Databasus creates its first administrator at startup with the literal string admin in the email column (backend/internal/features/users/repositories/user_repository.go:77), and backend/internal/features/users/services/user_services.go:468 refuses to change it. The bootstrap is sound in what it protects: a fresh install hands the owner an administrator account without requiring SMTP, an internet connection or a delivered email, which is what a self-hosted backup tool deployed behind NAT needs.

The cost is that the most privileged account on the instance has no reachable address. Password reset by email (user_services.go:545) silently does nothing for it, because no mail server can deliver to admin, and neither can any future feature that has to mail the account holder. Meanwhile the only place the product ever mentions that the login is admin is a sign-in error string (user_services.go:159), shown when the instance holds exactly one user. It tells an anonymous visitor that the account exists, and it stops helping the moment a second user is invited.

The seeded account buys nothing that registration does not. Claiming an instance today means filling in a password on a screen whose email field is disabled and reads admin (frontend/src/features/users/ui/AdminPasswordComponent.tsx:92-93), after which the client signs in with that literal address. Letting the first person who registers become the administrator asks for the same thing plus an address the owner already has, and no mail needs to leave the instance for it. The placeholder address then never exists on a new install, and the reachability problem is gone at the source rather than patched afterwards.

Existing instances still hold that account, so the change has to serve both: give the seeded account a stored identity that survives an address change, let its owner move it to a real address, and stop creating it for anyone new.

Governing docs

This change answers to AGENTS.md at the repo root, backend/AGENTS.md, frontend/AGENTS.md, website/AGENTS.md and assets/readme/AGENTS.md. It touches no verification agent code, so agent/verification/AGENTS.md does not apply.

What Changes

  • Stop seeding an account at startup. CreateInitialAdmin and its call from backend/cmd/main.go:109 are deleted, together with the repository function that writes the row (user_repository.go:65-86). A fresh instance starts with an empty users table.
  • Make the first account on an empty instance the administrator. Whether it arrives through the registration form or through a first Google or GitHub sign-in, the account created while the instance holds no users gets the ADMIN role and is recorded as the bootstrap administrator. It is granted regardless of the open-registration setting, which nobody has been able to configure yet at that point.
  • BREAKING for the sign-up contract: validate the registration address as an email address. SignUpRequestDTO.Email binds required alone (backend/internal/features/users/dto/dto.go:12), while the invitation and the profile update both bind email (:47, :42), so registration accepts foo today. The first account on an instance is now its administrator, and an administrator whose address is not an address is the problem this change exists to remove.
  • Record in the audit log that the instance has been claimed. The account that takes the ADMIN role and the flag gets an entry of its own, beside the registration entry SignUp already writes (user_services.go:130). It is the one line in the log that explains every privilege that account exercises afterwards, and without it the grant is invisible.
  • Identify the bootstrap administrator by a stored flag instead of by its email text. A migration adds users.is_root_admin, and a partial unique index makes it impossible for two accounts to carry it, which is also what settles a race between two people registering on the same empty instance: one insert wins, the loser falls back to ordinary registration.
  • Keep that flag out of every response. GET /users/me and the user list both build UserProfileResponseDTO instead of serializing the model (user_controller.go:301, management_controller.go:91-101), so no response shape changes when the column lands. The field still carries json:"-", the way HashedPassword does (models/user.go:14), so a handler that ever hands the model straight to a client cannot leak it by accident. Nothing on the client needs to know which account is the bootstrap administrator; the backend answers that question itself.
  • BREAKING removal of the claim path. GET /users/admin/has-password and POST /users/admin/set-password are deleted, along with IsRootAdminHasPassword (user_services.go:299), SetRootAdminPassword (:312), the two DTOs that exist only for them (SetAdminPasswordRequestDTO and IsAdminHasPasswordResponseDTO, dto.go:30-36), the screen that drives them (AdminPasswordComponent.tsx) and the client functions and types only that screen used (userApi.isAdminHasPassword, userApi.setAdminPassword, IsAdminHasPasswordResponse and SetAdminPasswordRequest). The authentication screen decides what to offer from whether the instance holds any account, through GET /users/is-any-user-exist - the endpoint frontend/src/entity/users/api/userApi.ts:62-74 already calls and the backend never implemented. No compatibility shim is kept.
  • Upgrade an existing instance in the migration, in one of three ways depending on what the instance holds:
    • Claimed: the admin row keeps its password and its sessions, and receives the flag, so the owner signs in exactly as before and can move to a real address afterwards. If that row is missing because somebody renamed it by hand, the flag goes to the oldest ADMIN-role account, which exists on any instance that still holds one: no code path deletes a user, and an ADMIN account can only be demoted by the root administrator, which cannot act on itself.
    • Never claimed and alone on the instance: the seeded row has no password and no linked identity, nobody has ever signed in with it, and it owns nothing. It is deleted, which leaves an empty instance whose first registration becomes the administrator. Every foreign key into users cascades or nulls, and none of them can hold a reference to an account nobody could sign in as.
    • Never claimed, but other accounts exist: POST /users/signup answers whether or not the claim screen offers registration, and open registration is on by default, so an instance can hold ordinary members while its seeded account was never claimed. Deleting the row there would take away the only ADMIN-role account and leave members behind with no administrator and no way to appoint one - the first-account rule cannot help, because the instance is not empty. The row is kept and flagged instead. The owner gives it a password with the existing reset command and then moves it to a real address, which is the same recovery an owner who forgot the password already uses.
  • Move every remaining decision that reads the address text onto the flag. Three sites survive the deletions above, and missing one turns an address change into a loss of function:
    • management_service.go:74 - only the root administrator may deactivate an ADMIN-role account.
    • management_service.go:108 - only the root administrator may reactivate one.
    • management_service.go:154 - only the root administrator may grant or revoke the ADMIN role.
  • Let the root administrator change their email like any other user, from the profile screen. The refusal at user_services.go:468 is deleted, and the frontend stops treating the address admin as read-only: ProfileComponent.tsx:234 disables the field, :236-238 explains why, and :138 and :157 skip validating and sending the value. All four go, and the now-unused dictionary key users.profile.adminEmailReadOnly is removed from all six locale files. The new address is still validated as an email address, so an upgraded account cannot move from admin to another value no mail server can reach.
  • Reset the administrator's password by email. No new code: SendResetPasswordCode (user_services.go:545) works for any account with a deliverable address, which every new administrator now has from the first minute and an upgraded one has as soon as the owner changes the address. The specs record it as a behavior this change is responsible for.
  • Replace the claim screen with the registration form on an empty instance, and say what the account is. AuthPageComponent.tsx:21-41 asks whether the administrator has a password and renders AdminPasswordComponent when it does not; it now asks whether any account exists and opens on registration, with copy explaining that this first account administers the instance. An instance that already holds an account keeps exactly the screen it opens on today - the same four modes, starting on registration - without that copy, so nothing changes for anyone but the first visitor. The dictionary keys users.adminPassword.* and app.auth.adminPasswordCheckFailed go, and the new copy lands in all six locale files.
  • Refuse sign-in to an account with no password instead of crashing on it. SignIn compares against *user.HashedPassword without checking that a hash exists (user_services.go:181), and hashed_password is nullable so that accounts can exist without one (migrations/20251031143019_add_many_users_and_settings.sql:35-36). Submitting the password form for an account created through Google or GitHub (user_services.go:963-974) therefore panics and answers 500 today.
  • Rework the test support, which this change touches in five places:
    • Replace the tests that pin the behavior being deleted. Test_CheckAdminHasPassword_WhenAdminHasNoPassword_ReturnsFalse, the three Test_SetAdminPassword_* tests (user_controller_test.go:278-374) and Test_UpdateUserInfo_WhenAdminTriesToChangeEmail_ReturnsBadRequest (:761) cover the claim path and the address refusal, so they are replaced by tests of what takes their place. Test_AdminLifecycleE2E_CompletesSuccessfully (e2e_test.go:21-119) belongs to the same group and is the one that matters most: it walks a fresh instance from the claim call to a signed-in administrator, so it is rewritten to walk it from the first registration instead of deleted.
    • Rebuild both bootstrap-account test helpers on the flag. RecreateInitialAdmin (users/testing/user_utils.go:67-79) renames the current admin row out of the way and calls CreateInitialAdmin again. With no seeding function left, it creates the account itself the way CreateTestUser (:19-48) does, retiring any account that currently holds the flag first - selected by the flag, because the rename it uses today selects by the address and returns without error when nothing matches (user_repository.go:147-157), which would leave the next test with no bootstrap account after any test that changes the address. RecreateInitAdminAndGetAccess (:50-65) reads the same account back with GetUserByEmail(ctx, "admin") and hands it straight to GenerateAccessToken, so it moves onto the flag too; left alone it would pass a nil user and panic in the nine tests that call it.
    • Close the coverage gaps around the paths this change rewrites. Three branches next to the edits have no test today, so a regression in them would land silently: password registration refused because external registration is disabled (only the GitHub path has such a test, user_controller_test.go:1007), and the two sign-in refusals that stand immediately above the password comparison this change guards - an account still in the invited state and a deactivated one (user_services.go:168-179).
    • Let a test decide whether the instance holds any account. The behavior this change introduces is keyed on an empty users table, and no test can assume one: cleanup_test_db recreates the per-slot databases once per run (backend/cmd/cleanup_test_db/main.go:64-97), each slot is then reused by whichever package claims it next, around twenty packages outside features/users create users through users_testing.CreateTestUser, and nothing anywhere empties the table. users/testing gains a helper that empties it, and the tests that assert an ordinary member account create one first instead of relying on what an earlier test left behind.
    • Assert the audit entries this account produces rather than trusting them. The users feature writes audit entries through a stub that discards them in tests (controllers/e2e_test.go:255-258), so no test has ever checked one. A recording writer replaces that stub, which lets the address change assert the entry user_services.go:486-506 already writes.
  • BREAKING for the sign-in experience: delete the hint "user with this email does not exist, seems you need to sign in as \"admin\"" (user_services.go:159). A visitor who mistypes the address now gets the same response as on any other instance. The replacement for a genuinely forgetful owner is the console command and the documentation below, not an error message served to anonymous requests.
  • Add a console command that prints the administrators of the instance, so an owner who has forgotten which address to use can recover it from the host: docker exec -it databasus ./main --list-admins prints the email, name, creation date and active state of every user with the ADMIN role, marking which one is the bootstrap administrator. The active state is there because an owner reading the list needs to know why an address does not let them in. It prints no password material of any kind. The listing itself lives under backend/internal/, not in backend/cmd/, because make test and CI both run go test ./internal/... only (backend/Makefile:16, .github/workflows/ci-release.yml:345) - code under cmd/ is linted but never tested. main.go keeps the flag and the call.
  • Document how an instance is set up now, and both recovery paths, where a locked-out owner will look: the root README.md password section and its five translations, the /password page on the website and its five translations, and one new FAQ entry on the website home page. Correct the access-management page, which tells the reader the system holds a user literally called admin.

The console command reaches the same trust level as the existing --new-password reset: both require a shell inside the container, and whoever has that already controls the instance and its database.

It also inherits the same defect. Console commands started with docker exec cannot currently reach the metadata database of a container that generates its password at startup, which is what restore-console-command-database-access fixes. Until that change lands, --list-admins fails with an authentication error in the default deployment, and so does the recovery path this change documents. This change therefore lands after it.

Out of scope

  • The second authentication factor itself. It lands in add-email-two-factor-auth, which depends on this change.
  • Who may reach an unconfigured instance first. An instance that is published before its owner registers hands the administrator account to whoever arrives first, exactly as the claim screen does today. The window is the same one, and the answer is the same: do not expose the instance before setting it up.
  • Verifying that the administrator's address receives mail. The address is an identifier and a delivery target; nothing here sends a confirmation message to it.
  • A guard against the bootstrap account losing its ADMIN role or its active status. None is needed: demoting or deactivating an ADMIN-role account requires being the root administrator (management_service.go:74,108,154), and that account may act on neither its own role (:142) nor its own status (:63), so nobody can strip it. No code path deletes a user.
  • Deleting users. The migration removes the seeded row of an instance nobody ever claimed and that holds nothing else, which is a one-time cleanup of an account that was never usable and never had company, not a general ability to delete accounts.
  • Linking a Google or GitHub identity to an account by matching email addresses (user_services.go:891-900). Every administrator now holds a real address, which brings the account within reach of that existing behavior; the behavior itself is unchanged. See design.md - Risks.
  • Hiding from an anonymous caller whether an instance has been set up. GET /users/is-any-user-exist answers that question without authentication, exactly as GET /users/admin/has-password does today; the authentication screen cannot decide what to render without it. The disclosure is unchanged, not new.
  • The user-enumeration shape of the remaining sign-in errors. "user with this email does not exist" and "password is incorrect" still tell an anonymous caller which of the two failed. Deleting the admin hint removes one leak but does not address the pair, and merging them changes behavior for every sign-in, which belongs in its own change.
  • Any rename of the ADMIN role.

Capabilities

New Capabilities

  • root-admin-account: how an instance gets its first administrator, how it keeps recognizing that account once its email address changes, which powers it holds over other administrators, and how an owner who has forgotten the address recovers it from the console.

Modified Capabilities

None. No existing spec under openspec/specs/ describes authentication or user accounts.

Impact

  • Database: a migration adding users.is_root_admin with a partial unique index, backfilling it on the claimed seeded account, and deleting the seeded account of an instance that was never claimed and holds no other account.
  • Backend: the sign-up and OAuth account-creation paths, the sign-in password check, the email-update path, the three root-administrator privilege checks in UserManagementService, two deleted routes and one added, an administrator listing under backend/internal/features/users/, and a new command-line flag parsed in backend/cmd/main.go alongside --test-storage, --new-password and --email.
  • API: GET /users/admin/has-password and POST /users/admin/set-password are removed, GET /users/is-any-user-exist is added, and the generated documentation changes with them. No existing response shape changes: the bootstrap-administrator flag is stored but not serialized.
  • Test support: both bootstrap-account helpers in users/testing/user_utils.go create the account directly and find it by flag; users/testing gains a helper that empties the users table, so a test can put the instance in the empty state the new rule is keyed on; and it gains an audit-log writer that records entries instead of discarding them, installed in the four test routers that build the users feature (e2e_test.go:177,196,214,232).
  • Frontend: the authentication screen opens on registration for an empty instance and loses AdminPasswordComponent; the profile screen stops locking the email field; the six locale files lose the claim-screen keys and users.profile.adminEmailReadOnly and gain the copy that explains the first account. The sign-in form keeps accepting admin as a login value (SignInComponent.tsx:50), because an upgraded instance that has not changed the address still uses it.
  • Docs: root README.md and assets/readme/README.{ru,es,pt,zh,fr}.md; website/app/(en)/password/page.tsx plus website/app/[lang]/password/content/{ru,es,pt,zh,fr}.tsx; website/app/(en)/access-management/page.tsx plus its five translated contents; the FAQ on website/app/(en)/page.tsx, in both its visible list and its structured-data block, plus the five translated home-page contents.
  • Depends on: restore-console-command-database-access, which restores the metadata-database access every console command needs. --list-admins and the recovery documentation added here do not work in the default deployment without it.
  • Downstream: add-email-two-factor-auth consumes the stored flag and the requirement that an administrator holds a real address.