Batch A5: #21 races, #22 renewal supersede, #24 CRL lifecycle, #25 sync durability #53

Merged
amiryahaya merged 2 commits from fix/a5-correctness-durability into main 2026-11-15 11:15:26 +01:00
Owner

Batch A5: closes #21, #22, #24, #25.

Changes

  • #21 races: new MdidWs.Lock (:global.trans). Enrollment and standalone renewal hold a per-IC lock from the "active certificate" check to the save, and revocation takes the same lock and re-reads the record. The IC is normalised (dashes stripped) so dashed and plain forms share one lock. The login rate limiter locks per identifier; create_client/3 locks per client_id ({:error, :client_exists}); duplicate client rows fail authentication closed (:ambiguous_client) instead of raising.
  • #22 renewal: the replacement is issued first, then the old certificate is retired. A local source is revoked (reason superseded) and the CRL republished; a remote-only source is revoked at Trustgate after issuance. If that fails, the replacement record is deleted, so the subscriber keeps the old certificate.
  • #24 CRL: publishing is serialized per issuer and written temp-file-then-rename. nextUpdate (72h, :crl_validity_hours) and a strictly increasing CRLNumber are set. /revoke and /revoke-by-ic accept an optional RFC 5280 reason (400 for unknown values). A failed publish after a committed revocation no longer 500s; MdidWs.CrlRepublisher retries after 60s and refreshes every 6h (disabled in test config).
  • #25 durability: certificate, client, token and signing-key stores use sync_transaction; the issuer (CA key) store uses sync_dirty. The login-attempt store stays async.

Behaviour changes / deploy notes

  • After a renewal the previous local certificate is revoked (superseded) and appears in the CRL; /status follows the newest certificate. Two existing tests asserting the old behaviour were rewritten.
  • Stored ic_number is now digits-only for dashed CSR serials.
  • A same-IC request waits while another holds the IC lock (up to the SOAP timeouts, ~30s per call).
  • create_client for an existing id now returns {:error, :client_exists}.

Tests and coverage

mix test: 275/277 pass; the 2 failures (TrustgateLifecycleTest) also fail on main. Race tests were verified to fail with the lock removed.
Coverage of changed modules: CrlRepublisher 86%, CrlPublisher 82%, ClientCredentials 82%, CertManagementController 83%, RateLimiter 92%, Renewal 79.5%, Enrollment 76% (untested lines are pre-existing paths). Project total 73.4% (no project minimum stated).

Review

Pass 1 (complexity): clean (removed an unreachable :aborted branch from the lock). Pass 2: no critical issues; fixed the IC-format lock bypass, revoke/renew race, republisher crash-loop and stale-timer chain, rollback logging. Not fixed: lock held across SOAP calls (bounded by the 30s HTTP timeout), stray *.tmp-* files after a hard crash.

🤖 Generated with Claude Code

Batch A5: closes #21, #22, #24, #25. ## Changes - **#21 races:** new `MdidWs.Lock` (`:global.trans`). Enrollment and standalone renewal hold a per-IC lock from the "active certificate" check to the save, and revocation takes the same lock and re-reads the record. The IC is normalised (dashes stripped) so dashed and plain forms share one lock. The login rate limiter locks per identifier; `create_client/3` locks per `client_id` (`{:error, :client_exists}`); duplicate client rows fail authentication closed (`:ambiguous_client`) instead of raising. - **#22 renewal:** the replacement is issued first, then the old certificate is retired. A local source is revoked (reason `superseded`) and the CRL republished; a remote-only source is revoked at Trustgate after issuance. If that fails, the replacement record is deleted, so the subscriber keeps the old certificate. - **#24 CRL:** publishing is serialized per issuer and written temp-file-then-rename. `nextUpdate` (72h, `:crl_validity_hours`) and a strictly increasing CRLNumber are set. `/revoke` and `/revoke-by-ic` accept an optional RFC 5280 `reason` (400 for unknown values). A failed publish after a committed revocation no longer 500s; `MdidWs.CrlRepublisher` retries after 60s and refreshes every 6h (disabled in test config). - **#25 durability:** certificate, client, token and signing-key stores use `sync_transaction`; the issuer (CA key) store uses `sync_dirty`. The login-attempt store stays async. ## Behaviour changes / deploy notes - After a renewal the previous local certificate is revoked (`superseded`) and appears in the CRL; `/status` follows the newest certificate. Two existing tests asserting the old behaviour were rewritten. - Stored `ic_number` is now digits-only for dashed CSR serials. - A same-IC request waits while another holds the IC lock (up to the SOAP timeouts, ~30s per call). - `create_client` for an existing id now returns `{:error, :client_exists}`. ## Tests and coverage `mix test`: 275/277 pass; the 2 failures (`TrustgateLifecycleTest`) also fail on main. Race tests were verified to fail with the lock removed. Coverage of changed modules: CrlRepublisher 86%, CrlPublisher 82%, ClientCredentials 82%, CertManagementController 83%, RateLimiter 92%, Renewal 79.5%, Enrollment 76% (untested lines are pre-existing paths). Project total 73.4% (no project minimum stated). ## Review Pass 1 (complexity): clean (removed an unreachable `:aborted` branch from the lock). Pass 2: no critical issues; fixed the IC-format lock bypass, revoke/renew race, republisher crash-loop and stale-timer chain, rollback logging. Not fixed: lock held across SOAP calls (bounded by the 30s HTTP timeout), stray `*.tmp-*` files after a hard crash. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
#21: per-IC lock for enrollment/renewal, locked rate-limiter counting, unique client_id create, ambiguous-client fail-closed.
#22: renewal issues first, then revokes the old certificate (local: superseded + CRL; remote: after issue, rollback on failure).
#24: atomic serialized CRL publish, nextUpdate + CRLNumber, revocation reasons, republisher with retry.
#25: sync_transaction/sync_dirty for issuance, revocation, client, token and CA-key writes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fix: A5 review — normalise IC for lock key, lock revocation, harden CRL republisher
All checks were successful
Lint / lint (pull_request) Successful in 2m38s
Security / Dependency advisories (mix hex.audit) (pull_request) Successful in 22s
Security / Secret scan (gitleaks) (pull_request) Successful in 17s
Unit tests / test (pull_request) Successful in 3m34s
b635a499f6
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.