fix(auth): decide refresh-token upserts in Go

A replicated Doorkeeper refresh-token event is now applied by Go code, not by an SQL conflict arm. The rules are the ones !947 (merged) put in the statement: a PBKDF2 hash never replaces a stored one, the first revoked_at wins, another client is refused, and a signature stored under another token id is refused. This is the follow-up sgarg asked for on !947 (merged), so the same code can serve the bulk RPC (gitlab-org/gitlab#635782 (closed)).

TokenRepository.Upsert opens a transaction and runs one event through apply, which takes the transaction's querier and does the work for a batch. One query locks the batch's replicated clients for key share, which is the client gate, and a second locks the stored rows for the batch's token ids and signatures. Clients go first because a client delete locks in that order too. decide and fold (decide.go) apply each event to its row in request order, so several events for one token id fold, and a signature stays reserved for the row that held it at lock time or earlier in the batch. New rows go through an insert that does nothing on conflict, changed rows through a plain update. A short insert count or a duplicate key from the update can then only mean a row landed in between; the call returns ABORTED and the caller resends. The batch form stays unexported here; !991 (merged) exports it with the bulk RPC and the multi-event tests. The CASE arm, the no-rows classifier and the retry on a duplicate key are gone.

One visible change, hence fix: a row that lands between the lock and the write is answered with ABORTED instead of a retry inside the call.

The integration tests wrap the querier they hand to apply to act from a second connection right after the lock query: commit a racing row, or start a client delete.

How verified: unit tests, the integration suite on Postgres and YugabyteDB, lint and the agent-docs check locally, and CI runs the same on both backends. The stack's end-to-end test in system-test-suite drove Upsert, BulkUpsert, BulkDelete and GetState against iam serve all on both backends with 3,000 tokens.

Read decide.go and its table tests first. The integration sequence table runs two of the decide cases against both backends, and two deterministic tests force the lock-to-write race and a racing client delete.

Part 4 of 8. The full change is in !959 (closed), the integration MR, kept open for the big picture.

  1. !988 (merged) feat(proto): add BulkUpsertClients and UpsertResult (merged)
  2. !989 (merged) feat(auth): add BulkUpsertClients with per-record results (merged)
  3. !990 (merged) refactor(auth): move the bulk record helpers to internal/batch (merged)
  4. !1010 (merged) fix(auth): decide refresh-token upserts in Go <- this MR
  5. !991 (merged) feat(auth): add refresh-token BulkUpsert with per-record results
  6. !992 (merged) feat(auth): add refresh-token BulkDelete
  7. !993 (merged) feat(auth): add refresh-token GetState
  8. !994 (merged) feat(auth): count bulk RPC records by outcome

Previous: !990 (merged) · Next: !991 (merged)

Edited by Shilpa Kundapur

Merge request reports

Loading
Loading