Skip to content

fix(authserver): one admin client API at /admin/oauth2/clients; locked updates and token revocation; client create on every database (#344) - #359

Merged
dfcoffin merged 4 commits into
mainfrom
fix/344-one-admin-client-api
Nov 30, 2026
Merged

dfcoffin merged 4 commits into
mainfrom
fix/344-one-admin-client-api

Conversation

@dfcoffin

@dfcoffin dfcoffin commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

#344 part 3a, per the owner's design of 2026-11-30 (decision). Part 3b follows with the shared registration validator (D2), the #343 docs and #355.

Closes #348
Closes #349
Closes #351
Closes #353

Refs #344 (stays open for part 3b)

One admin client API at /admin/oauth2/clients

Endpoint Change
GET /admin/oauth2/clients the paged, filtered list (was /api/v1/oauth2/clients); replaces OAuthAdminController's list, which answered with mock clients on failure
POST /admin/oauth2/clients create; now works on every database (#351, below)
GET /admin/oauth2/clients/{clientId} detail
PUT /admin/oauth2/clients/{clientId} update; #348: locks the client row and applies the change to the client as read after the lock, in one transaction; a client deleted meanwhile is 404, not re-inserted by save
PUT /admin/oauth2/clients/{clientId}/status status (#345)
DELETE /admin/oauth2/clients/{clientId} delete, moved from OAuthAdminController; the response is now {"status","client_id","authorizations_deleted"}
DELETE /admin/oauth2/clients/{clientId}/tokens #349: really revokes. The client row is locked, its authorizations are deleted, the count is audited (client_tokens_revoked) and returned, and an unknown client is 404
  • /api/v1/oauth2/clients is removed outright. Nothing called it and nothing is deployed. It is also gone from ADMIN_API_PATHS and ADMIN_BEARER_API_PATHS, since /admin/** covers the new path.
  • D1: GET /admin/oauth2/tokens and GET /admin/oauth2/authorizations returned hard-coded sample data, so they are removed. DELETE /admin/oauth2/tokens/{tokenId} stays and no longer echoes the token value.
  • Review date (D5): if no operator tool or admin portal uses this API by 2027-06-30, deprecate and retire it. Recorded in the controller's Javadoc and the API doc.

#351: create failed on every database

The issue described PostgreSQL's enum columns and H2's missing columns. While moving the code I found that the create's espi_application_info INSERT also named uuid, client_description, scope and grant_types, which only PostgreSQL has. So a create failed on MySQL and H2 too, after the client had already been saved. No test caught it because the controller test mocks the database.

  • New transaction: ClientAdminService.create saves the client, its application info and its ESPI columns in one transaction, so a failed write keeps nothing.
  • Columns every database has: the INSERT now writes only those. The request's client_description goes to third_party_application_description (varchar(500) on MySQL and H2), so its limit is now 500 characters instead of 1000. scope and grant_types duplicated the client's own.
  • PostgreSQL enum columns: EnumParameters casts the text to the enum type on PostgreSQL. It generalises AS: suspending or revoking a client has no effect: client_status is never checked at the token, authorization or introspection endpoints #345's statusParameterFor and covers client_status, security_level, security_classification and certification_status, in create, update, the status change and the list's securityLevel filter.
  • Migrations: H2 V13 adds the columns MySQL and PostgreSQL got in their V2. PostgreSQL V13 gives espi_application_info.uuid a default (gen_random_uuid()), since only PostgreSQL has that NOT NULL column.
  • The list's securityLevel filter takes only standard, high or maximum, as the status filter does. Anything else, including a blank value, is 400. A test checks the list against the requests' @Pattern.

The SQL for these writes moved from the controller to RegisteredClientAdminDao.

#353

max-failures-before-lock and default-lock-duration-minutes are removed from OAuth2ClientManagementConfig, the docker profile and the docs, and failure_count is no longer reported. There is no failure lockout; if one is wanted, it is a new issue (D3).

Tests

  • End to end on MySQL and PostgreSQL (ClientAdminScenarios, run by both ClientSuspension*IntegrationTests):

    • create through the API, with the ESPI columns read back, including PostgreSQL's enum columns;
    • suspend: the token is refused; reactivate;
    • update the security level, and the list filtered by it;
    • revoke the tokens: 2 authorizations deleted, both tokens introspect inactive, the revocation is audited, and the client still gets new tokens;
    • delete: 404 afterwards, and the application info is gone.

    Also on both databases: an update, status change or token revocation of a deleted client is 404 and brings nothing back.

  • H2 (ClientAdminH2Test, the real H2 migrations):

    • a create stores the ESPI data;
    • a create whose ESPI write fails keeps nothing, not even the client;
    • an update writes only the given columns, and an unknown client is not re-inserted;
    • a token revocation deletes the authorizations and keeps the client.
  • Controller (OAuth2ClientManagementControllerTest, now with the real service and DAO over a mocked database):

    • a client deleted after the lookup is 404 for an update;
    • the update applies to the client read after the lock, keeping another administrator's change;
    • token revocation and delete: the response, the audit, the 404s, and no exception text in a 500;
    • the securityLevel filter: the 400s, PostgreSQL's cast, and the list matching the @Pattern.
  • Unit: EnumParametersTest, the DAO's SQL per database, and OAuthAdminControllerTest reduced to token revocation (the D1 lists are gone).

  • Security: AdminEndpointAuthorizationIntegrationTest covers the new paths (customer 403, anonymous 401 or login, ADMIN and DC_ADMIN allowed, password-only admin sent to the passkey step) and method security on both controllers.

  • Full AS suite: 654 tests, 0 failures (WSL, Docker).

Review findings

Round 1, /code-review high (the whole PR):

# Finding Where it went
1 Real bug in this PR. On MySQL's REPEATABLE READ, the update's first plain read fixed the snapshot, so the re-read after FOR UPDATE returned the client as it was before another administrator's committed change. The update then overwrote that change: the lost update #348 was meant to prevent Fixed in 3403180: the lock (SELECT id … WHERE client_id = ? FOR UPDATE) is now the transaction's first statement, for the update and the token revocation. A MySQL test holds the lock in another transaction and commits a change while the update waits; it fails with the old order and passes now
2 openapi.yaml gave client_description maxLength: 1000 after this PR lowered it to 500, and lacks the two DELETE operations Fixed: 500. The DELETE operations come with the #343 docs in part 3b
3 Other docs still named /api/v1/oauth2/clients Fixed: DEPLOYMENT_GUIDE, QUICK_START, API_DOCUMENTATION, INTEGRATION_GUIDE, ESPI_COMPLIANCE_GUIDE, docs/README and the AS README (which also listed the removed token and authorization lists). The rest of their content stays with #343 in part 3b
4 Pre-existing: a rename left espi_application_info.client_name unchanged Fixed: renamed in the same transaction
5 Pre-existing: a client update (redirect URIs, scopes, ...) was not audited Fixed: client_updated, under the signed-in administrator, with the names of the fields set
6 Pre-existing: DELETE /admin/oauth2/tokens/{tokenId} puts the access token in the URL, where access logs keep it Filed as #360, for part 3b

Round 1 found a real bug in this PR, so round 2 goes up a level to xhigh, scoped to 3403180.

Round 2, /code-review xhigh (scoped to 3403180):

# Finding Where it went
1 Real bug in the round-1 fix. On MySQL, the update and the token revocation locked by client_id (the index record, then the row), but the delete locked the row by id first and then needed the index record. A delete and an update of the same client could therefore deadlock, and one of them would answer 500 Fixed in 08069d0: deleteById(id) became deleteByClientId(clientId), which locks by client_id first, so every writer takes the locks in one order. A MySQL test shows that an update waiting on a delete in progress finds the client gone (404), with no deadlock
2 A name already out of sync is not repaired by an update that keeps the name Fixed: every update writes the client's name to espi_application_info
3 deleteAuthorizations' Javadoc linked the removed lockById Fixed
4 Nothing guaranteed the lock is the transaction's first statement if a caller opened a transaction Fixed: update and revokeTokens are REQUIRES_NEW
5 The client_updated audit row is written after the commit, so two updates can be audited out of order Answered on the PR: an audit insert inside the transaction would let a failed audit roll back the update on PostgreSQL (an error aborts the transaction even when caught). It is written after the commit, as for delete and status changes, and each row has its timestamp
6 The Javadoc claimed the two names never differ, but the seeded default clients' names are reset at startup in oauth2_registered_client only Fixed: the Javadoc says so

Round 2 also found a real bug in this PR. Under CLAUDE.md, going up from xhigh means ultra, and the owner chose to stay at xhigh for round 3, scoped to 08069d0. If round 3 still finds a real bug, the automated reviews stop and the owner reviews the design.

Full AS suite after the round-2 fixes: 661 tests, 0 failures.

Round 3, /code-review xhigh (scoped to 08069d0). No real bug. The reviewer confirmed that update, token revocation and delete now take the client's locks in one order, so none of them can deadlock with another.

# Finding Where it went
1 The MySQL delete/update test claimed to prove "no deadlock", which it cannot reproduce (the delete finishes before the update waits) Fixed in 6dabe62: renamed for what it checks (an update waiting on a delete finds the client gone). The lock order is checked by RegisteredClientAdminDaoTest
2 REQUIRES_NEW would wait on itself under a caller already holding the client's lock in its own transaction Answered on the PR: there is no such caller, the controller is not transactional, and REQUIRES_NEW guarantees the lock is the transaction's first statement
3 Two Javadoc links still named the removed deleteById Fixed
4 The controller's delete tests had unused stubs and did not check which client_id the delete locks Fixed: the lock is stubbed for the path's client_id only, and verified

Round 3 found no real bug, so round 4 steps down to high, scoped to 6dabe62 (tests and Javadoc only).

Full AS suite after the round-3 fixes: 661 tests, 0 failures.

Round 4, /code-review high (scoped to 6dabe62): no findings. No further src/ change, so the review cycle is complete.

Not in this PR

🤖 Generated with Claude Code

…es and token revocation lock the client; client create works on every database (#344)

#344 part 3a (owner design, 2026-11-30):
- OAuth2ClientManagementController moves from /api/v1/oauth2/clients to
  /admin/oauth2/clients and takes OAuthAdminController's client delete and
  token revocation. The token and authorization lists that answered with
  hard-coded sample data are removed (D1); DELETE /admin/oauth2/tokens/{id}
  stays and no longer echoes the token.
- #348: an update locks the client row and applies the change to the client
  read after the lock, in one transaction (ClientAdminService), so a client
  deleted meanwhile is 404 instead of re-inserted by save.
- #349: DELETE /admin/oauth2/clients/{clientId}/tokens deletes the client's
  authorizations under the same lock, audits the count and answers 404 for
  an unknown client, instead of reporting a revocation it never made.
- #351: create wrote espi_application_info columns only PostgreSQL has, and
  enum columns as text, so it failed on every database after saving the
  client. It now writes columns all three have, in one transaction with the
  client; PostgreSQL enum parameters are cast (EnumParameters, generalising
  #345's statusParameterFor); H2 V13 adds the columns; PostgreSQL V13 gives
  espi_application_info.uuid a default. The list's securityLevel filter takes
  only standard/high/maximum.
- #353: max-failures-before-lock and default-lock-duration-minutes are
  removed, and failure_count is no longer reported.
- Create -> suspend -> reactivate -> update -> revoke tokens -> delete is
  tested end to end on MySQL and PostgreSQL, and the writes on H2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…renames and updates are recorded (#344 review)

Round 1 of the PR #359 review:
- #1: on MySQL's REPEATABLE READ the update's first plain read fixed the
  snapshot, so the re-read after FOR UPDATE returned the client as it was
  before another administrator's committed change, which the update then
  overwrote. The lock (SELECT id ... WHERE client_id = ? FOR UPDATE) is now
  the transaction's first statement, for the update and the token
  revocation. A MySQL test holds the lock in another transaction, commits a
  change while the update waits, and checks the change is kept; it fails
  with the old order.
- #4: a rename is recorded in espi_application_info.client_name too.
- #5: an update is audited (client_updated) under the signed-in
  administrator, with the names of the fields it set.
- #2, #3: openapi.yaml's client_description maxLength is 500; the docs that
  named /api/v1/oauth2/clients, or the removed token and authorization
  lists, name the new paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o the two cannot deadlock (#344 review)

Round 2 of the PR #359 review:
- #1: round 1 made the update and token revocation lock by client_id
  (MySQL: the client_id index record, then the row), while the delete
  still locked the row by id and then needed the index record, so a delete
  and an update of the same client could deadlock. deleteById(id) becomes
  deleteByClientId(clientId), which locks by client_id first; every writer
  now takes the locks in one order. A MySQL test shows an update waiting
  on a delete in progress finds the client gone.
- #2: every update writes the client's name to espi_application_info, so
  a name that differed before is repaired too.
- #3: the deleteAuthorizations Javadoc links lockByClientId.
- #4: update and revokeTokens run in a transaction of their own
  (REQUIRES_NEW), so the lock is always its first statement.
- #6: the application-info name Javadoc says the seeded default clients'
  names are reset at startup in oauth2_registered_client only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dfcoffin

Copy link
Copy Markdown
Contributor Author

Round 2 (/code-review xhigh, scoped to 3403180), finding #5: answered. The client_updated audit row is written after the update's transaction commits, so two updates of the same client can, in theory, be audited in a different order from the one they were applied in.

This stays as it is, on purpose. An audit insert inside the transaction would let a failed audit write roll back the update on PostgreSQL, where any error aborts the transaction even when it is caught (AuditLogDao.record catches its own failures). The admin API audits delete, status changes and token revocation the same way, after the change. Each audit row has its own timestamp, and a reorder needs two updates of the same client finishing within the same moment.

🤖 Generated with Claude Code

…t_id; Javadoc links name deleteByClientId (#344 review)

Round 3 of the PR #359 review:
- #1: the MySQL delete/update test is named for what it checks (an update
  waiting on a delete finds the client gone), not a deadlock it cannot
  reproduce; the lock order is checked by RegisteredClientAdminDaoTest.
- #3: two Javadoc links still named the removed deleteById.
- #4: the controller's delete tests lock only the path's client_id and
  verify the lock, and lose their unused stubs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dfcoffin

Copy link
Copy Markdown
Contributor Author

Round 3 (/code-review xhigh, scoped to 08069d0), finding #2: answered. update and revokeTokens are REQUIRES_NEW (round 2, #4), so a caller that already held the client's row lock in its own transaction and then called them would wait on itself.

This stays as it is. No caller runs them inside a transaction: the controller is not transactional. Holding a client's lock and then calling the service that takes that lock would be a misuse in any design. REQUIRES_NEW is what guarantees the lock is the first statement of the transaction, which the round-1 MySQL fix depends on.

🤖 Generated with Claude Code

@dfcoffin
dfcoffin merged commit 388fc82 into main Nov 30, 2026
13 checks passed
@dfcoffin
dfcoffin deleted the fix/344-one-admin-client-api branch November 30, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment