Repository navigation
fix(coderd): let only one request create the first user (#32417) - #32540
Conversation
Prevent multiple Owner accounts from being created during concurrent first-user setup requests. Refs https://linear.app/codercom/issue/CORE-853 Use a transaction-scoped PostgreSQL advisory lock shared by password setup and GitHub/OIDC first sign-ins. Recheck the user count under the lock to ensure only one account receives the Owner role, while preserving existing responses for subsequent requests. - Add a stable, backport-safe lock ID and shared first-user setup helper. - Apply locking only when no users exist, leaving normal sign-ins unaffected. - Use `READ COMMITTED` isolation to ensure accurate counts regardless of database defaults. - Keep validation and trial licence requests outside the lock. - Preserve existing API responses, status codes, and telemetry behavior. **Testing** - Add concurrent password, OAuth, and mixed-path tests verifying exactly one Owner. - Cover lock contention, cancellation, isolation levels, telemetry, and lock bypass. - Verify tests fail when locking is removed. - Pass targeted race tests, 124 related tests, lint, and `intxcheck`. **Known limitation:** A burst of concurrent OAuth sign-ins on an empty deployment could exhaust the database connection pool while waiting for the lock. This remains unverified and is outside the scope of this fix. **Note:** Stacked on #32324; will be rebased onto `main` after it merges. (cherry picked from commit cf68914)
|
/coder-agents-review Please limit the review to P0, P1, and P2 issues only. |
|
Chat: Review posted | View chat Review history
deep-review v0.13.0 | Round 1 | Last posted: Round 1, 8 findings (1 P3, 4 P4, 3 Nit), COMMENT. Review Finding inventoryFindings
Contested and acknowledgedNone. Drop and downgrade decisions
Round logRound 1Full panel (round 1). Netero: no findings. Law not run (effective +579). Panel: ging-go, takumi, knuckle, kurapika, ryosuke (specialists); pariston, mafuuu (core); bisky, komugi, gon, leorio, hisoka (pool); zoro (wildcard). Size 12 + wildcard; security-sensitive first-user creation path. Backport diff verified identical to upstream cf68914. 1 P3, 4 P4, 3 Nit, 3 Note (body), 5 out of scope, 3 dropped. Reviewed against dbab9ed..2656317. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This backport serializes first user creation in postFirstUser and oauthLogin with a transaction-scoped advisory lock and a user count taken under it at read committed. Findings: 1 P3, 4 P4, 3 Nit. The +/- lines are identical to upstream cf68914, so every change made for these findings also needs to go to main.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/users.go:302: whenRefreshEntitlementsfails after the first user is created, the handler returns without writing a response, so the client gets an empty 200 although the owner exists.enterprise/coderd/scim/users.go:160: SCIM can create a member while the user count is 0 without takingLockIDFirstUserSetup; after that/users/firstreturns 409 and the deployment has no owner.coderd/users.go:2187:CreateUserenqueues account-created notifications through a pool connection while the caller's transaction is open, so every transactional caller needs two connections and the notification is sent before the user row commits.enterprise/trialer/trialer.go:121:InsertLicenseruns outside the first-user transaction, so a first-user request that is later refused keeps its trial license.coderd/users.go:316: this unchanged comment contains an em dash, whichmake lint/emdashrejects.
Notes:
TestFirstUserSetup_PasswordAndOAuthdiscards every response status and asserts only that one owner exists, so it passes when some requests return 500.- The cherry-picked commit body ends with "Stacked on #32324; will be rebased onto
mainafter it merges.", which is false onrelease/2.39. A squash merge replaces it with the PR description; delete it before a merge commit or rebase merge. - Two concurrent OAuth callbacks for the same new email at user count 0 create one owner; the second callback returns 403 (signups disabled) or fails on the duplicate email (signups enabled), and a retry signs in as the owner.
coderd/users.go:224
P4 [CRF-2] A POST /api/v2/users/first request with Trial: true that loses the race stores a trial license and then gets 409. (Mafuuu, Ryosuke, Pariston)
TrialGenerator runs here, before the lock, and in enterprise trialer.Generate calls the licensor and then InsertLicense (enterprise/trialer/trialer.go:121) outside this transaction. The deployment ends up with one owner and an extra trial license registered to the contact details of the refused request. Keeping the licensor call outside the lock is correct, as TrialRequestedBeforeLock requires. If the extra license is acceptable, state that in the comment on TrialRequestedBeforeLock; otherwise this needs a human decision on the trialer change listed in the review body.
🤖
🤖 This review was automatically generated with Coder Agents.
Backport of #32417
Original PR: #32417 — fix(coderd): let only one request create the first user
Merge commit: cf68914
Requested by: @BobbyHo
The automatic cherry-pick had conflicts because #32324 was not on this branch yet. With #32324 backported, the cherry-pick applies cleanly and the change is the same as in #32417.