Repository navigation
fix(coderd): let only one request create the first user - #32417
Conversation
develop.sh, the compose dev environment and the deploy-ephemeral skill created every admin and member user with the same public password. Each now generates a random password per user on first use, stores it in a file only the owner can read, prints it, and reuses it on later runs. The old default is refused as an override. A user that already exists keeps its password. develop.sh warns when it did not set that password, and stops with advice when it has no way to sign in.
A compose environment created before passwords were generated has the member user but no member password file. setup-users exited 1 there, and the site service never started because it waits for setup-users. It now warns that it did not set the member's password and exits 0. It still fails when the member has to be created and no password is available.
…vironment setup-users warned when the member existed without a password file. setup-init only said the admin password was not set by the environment. The admin is the Owner, so it now gets the same warning. Both scripts call one function in password.sh.
An instance created before passwords were generated kept the old public default for [email protected] and [email protected]. develop.sh and the compose setup now give such a user a generated password. The old default is sent as the current password, so the server keeps a password that was changed by hand. Changing the admin password revokes its tokens, so develop.sh signs in again and the compose setup recreates the bootstrap token.
…that keep the database When --password signed in and no admin password was stored, develop.sh forgot it, so the next run without the flag stopped once the saved session expired. It is now saved to the admin password file, which is never replaced when it already exists. The unknown-password error now lists the two ways out that keep the database before --db-reset.
A server published through a try.coder.app tunnel let the first visitor become Owner. The server now prints a setup token at startup when it opened a tunnel and no user exists. The first-user endpoint refuses a request without it, and an OAuth sign-in cannot create the first user until setup is complete.
The slim binary must not import coderd/rbac, and the setup token code reaches it through dbauthz. It is only used by the server command, which is already left out of slim builds. Also write the nolint directive without a leading space.
A short --setup-token stopped the server even when an access URL was set and the token was ignored. The warning for an ignored token now names coder server create-admin-user.
The setup page hint and the install page said a matching email is enough to sign in with GitHub later. The account must also be switched under Account > Security. The quickstart and the GCP guide now say where to find the setup token.
A stored password did not show that the server still accepted it. develop and compose now replace the old default whether or not a password file exists, and store an override that signs in when the stored password differs. develop signs in as the admin with the old default instead of using the saved session, which can belong to another user. A refused stored password has its own error in develop, and the compose error names the source, the HTTP status and the recovery.
…expired The bootstrap token expires after the default token lifetime of 7 days. An environment created before passwords were generated then kept the old default password, because setup-init had no way to sign in. It now signs in with the old default to replace it. An accepted CODER_DEV_ADMIN_PASSWORD is also stored on the first run, so a later start without the variable can still create a new token.
Concurrent requests during first-user setup could each read a user count of zero and each create a user with the owner role. Both the password path and the OAuth path now take an advisory lock, count the users again while holding it, and create the user in the same transaction. A request that waited gets the existing 409, or continues as an ordinary sign-up on the OAuth path. The lock ID is derived from a string so that it has the same value on every release branch.
|
/coder-agents-review Please limit the review to P0, P1, and P2 issues only. |
|
Chat: Review in progress (10/10 reviewers complete) | View chat Review history
deep-review v0.13.0 | Round 2 | Last posted: Round 2, 1 findings (1 P2), APPROVE. Review Finding inventoryFindings
Contested and acknowledgedRound logRound 1Netero first pass: 3 P4, 1 Note. No P0, panel proceeds. Author comment requests P0 to P2 findings only. Full panel (round 1). Multi-domain, security-sensitive (first Owner creation, unauthenticated endpoint, Postgres locking). Specialists: ging-go, kurapika, knuckle, takumi, ryosuke, killua. Core: pariston, mafuuu. Pool: bisky (tests), hisoka, gon, leorio (once-per-PR floor). Wildcard: razor (shuf from distilled set). 13 reviewers; target 9 to 12 exceeded by one because Gon and Leorio were placed in round 1 per the floor. Cross-check:
Round 2Churn guard PROCEED: CRF-5 addressed in 5116516. Netero (advisory): no findings, CRF-5 fix verified by breaking it on purpose. Fix round (not a restructure: no files added, renamed, or moved; no exported signature change; effective additions grew by 88). Panel: razor, pariston, gon, leorio (raised CRF-5, code touched), ging-go, bisky, knuckle (changed Go, test, isolation), wildcard kite (shuf from distilled set). Gon and Leorio also required because comments changed. Cross-check: CRF-5 verified fixed by Razor, Pariston, Gon, Leorio, Bisky, Knuckle, Kite. Each reverted a call site and saw ServerDefaultRepeatableRead fail. Gon's four comment findings downgraded from P2 to Nit. Keep-argument: comment length is a pattern across the PR. Downgrade reason: the comments are accurate and the code works, which is the Nit definition. Kite's side-effect note (every OAuth login transaction now runs at read committed) has no consequence; the rest of that transaction makes only database calls. No P0 to P2 findings open. Reviewed against c6cd321..5116516. About deep-reviewCRF = Coder Review Finding (P0-P4, Nit, Note)
|
There was a problem hiding this comment.
This PR makes postFirstUser and oauthLogin take a transaction-scoped advisory lock and count the users again before creating the first user. One P2 finding. As you asked, findings below P2 are not posted.
Out of scope (needs a ticket or explicit acceptance by a human):
coderd/users.go:2199:CreateUserenqueues the account-created notification throughapi.NotificationsEnqueuer, which needs a second pool connection while the caller's transaction is still open. With OAuth sign-ups enabled, a number of concurrent sign-ups equal to the pool size leaves every request holding one connection and waiting for another, so they all hang until clients disconnect. This also happens at the base commit, and on deployments that already have an Owner. Making lock waiters roll back and retry did not remove it. The PR description's "Known limit" says only the new lock on an empty deployment causes it.coderd/users.go:2187: the same notifications are sent before the caller's transaction commits, so anoauthLoginthat later rolls back (for example, when org or group sync fails) tells admins about an account that does not exist.coderd/userauth.go:1878:oauthLoginreports first-user telemetry inside the transaction, before org, group, and role sync. If one of those fails, telemetry has reported a first user that does not exist.
🤖 This review was automatically generated with Coder Agents.
The count taken under the first-user setup lock must see a user that another request committed during the wait. That holds at read committed. Both transactions passed no options, so they ran at the Postgres server's default level. Where an operator has set default_transaction_isolation to repeatable read, the count came from a snapshot taken before the wait and a second owner was created. Both transactions now ask for read committed.
|
/coder-agents-review Please limit the review to P0, P1, and P2 issues only. |
There was a problem hiding this comment.
Round 2 reviewed the read committed fix in 511651665d. The unpinned isolation level is fixed: both lock transactions now use firstUserSetupTxOptions(), and TestFirstUserSetup_Lock/ServerDefaultRepeatableRead fails on each path when that path's options are reverted. No new P0 to P2 findings. The out-of-scope items from round 1 are unchanged and still need a ticket or explicit acceptance by a human.
🤖 This review was automatically generated with Coder Agents.
…yhigh-prevent-concurrent-first-user
…tyhigh-prevent-concurrent-first-user # Conflicts: # .agents/skills/deploy-ephemeral/SKILL.md # coderd/userauth.go # scripts/develop/main.go # scripts/develop/password.go # scripts/develop/password_test.go
TL;DR
The first account on a new deployment gets the Owner role. If two requests to create that account arrive at the same moment, both can see that no user exists yet, and both become Owners. This PR makes the two ways of creating the first account, the setup form and a first GitHub or OIDC sign-in, take turns. Only one request creates the Owner. Any other request that arrived at the same time gets the answer it would get today after setup is finished.
Refs https://linear.app/codercom/issue/CORE-853
Note
This PR is stacked on #32324 and targets that branch, so the diff shows only this change. It will be rebased onto
mainafter #32324 merges.How to navigate this PR
coderd/database/lock.go: the new lock ID. Check that it is declared outside the numbered list.coderd/firstusersetup.go(new):lockFirstUserSetuptakes the lock and returns the user count while the lock is held. This is the most important function. Both paths call it.coderd/users.go:postFirstUser. The call toCreateUsermoves into a transaction that calls the helper first. Most of the diff here is indentation. Check that the checks above it, and the trial request, stay outside the transaction.coderd/userauth.go:oauthLogin. One inserted block. Check its condition: the lock is taken only when the first count is zero and the person has no account.coderd/firstusersetup_test.go(new): the tests.oauthLogin, which every GitHub and OIDC sign-in passes. It does nothing when a user already exists.Implementation
coderd/database/lock.go: addsLockIDFirstUserSetup, derived from a string withGenLockID.coderd/firstusersetup.go: addslockFirstUserSetup, which takes a Postgres advisory lock for the length of the transaction and then counts the users, anderrFirstUserExists.coderd/users.go:postFirstUsercreates the user in a transaction. It takes the lock, counts again, and returns the existing 409 when the count is no longer zero.coderd/userauth.go:oauthLogintakes the lock when the first count is zero and the person is new, and replaces its user count with the count taken under the lock. The lines below it that decide whether a sign-up is allowed and whether to give the Owner role already read that variable.Choices a reviewer might question
firstUserSetupTxOptions(), so that a Postgres server whose default is repeatable read or serializable gets the same result.CreateUserhashes inside its transaction. Moving it would change a function that other callers use.Known limit
CreateUser, so it is left out of this PR.Expected Output
Testing
All in
coderd/firstusersetup_test.go:TestFirstUserSetup_ConcurrentPassword: ten first-user requests at once. Checks one 201, nine 409s, one user, one Owner and one telemetry report.TestFirstUserSetup_ConcurrentOAuth: ten GitHub sign-ins by different users at once, with sign-ups disabled and enabled. Checks the statuses, the user count and that there is one Owner.TestFirstUserSetup_PasswordAndOAuth: five first-user requests and five GitHub sign-ins at once. Checks that there is one Owner.TestFirstUserSetup_Lock: the test holds the lock itself.RequestWaitsForLock: a first-user request waits, and returns 201 after the lock is released. The test readspg_locksto see the request waiting, so it does not depend on timing.ClientGivesUp: the client cancels while waiting. The server stops waiting and no user is created.TrialRequestedBeforeLock: the trial generator is called while the lock is still held by the test.ServerDefaultRepeatableRead: the test database defaults to repeatable read. A user is committed while a request waits for the lock. The request is refused, on the password path and on the GitHub path.NotTakenWhenUsersExist: with a user present, a first-user request and a GitHub sign-up both complete while the test holds the lock.oauthLogin. The fake OIDC provider incoderd/coderdtest/oidctest/idp.gowrites its shared OAuth2 config on every login, which the race detector reports when two logins run at once.coderd/users.goandcoderd/userauth.goremoved, the three concurrent tests fail, and ten requests create ten Owners. With the trial request moved inside the lock, or the lock taken on every sign-up, the matching sub-test fails. With the transactions left at the server default,ServerDefaultRepeatableReadfails.go test ./coderd/ -run 'TestFirstUserSetup' -count=3 -race -parallel=4go test ./coderd/ -parallel=3forTestFirstUser,TestFirstUser_OnboardingTelemetry,TestUserOIDC,TestUserOAuth2Github,TestUserLogin,TestPostLogin,TestPostUsersand the other OIDC tests: 124 passed.golangci-lint run ./coderd/ ./coderd/database/andgo run ./scripts/intxcheck ./coderd/.make pre-commit,make genand the full test suite.