fix(manager-core): F-P-002 move Argon2id verify off the Tokio async worker

verify_password (Argon2id, OWASP defaults m=19456 KiB, t=2) is CPU-bound
at tens-to-hundreds of ms and was invoked synchronously on the Tokio
worker. Worse, verify_api_key Argon2-verifies every candidate sharing
the 8-char prefix — a hot user with N keys serialized every admin
request behind N×Argon2.

Wrap each call site in tokio::task::spawn_blocking:
- auth_middleware::verify_api_key (per request carrying a Bearer key)
- auth_api::login (admin login)
- users_service::login (data-plane app-user login, both real-hash and
  TIMING_FLAT_DUMMY_HASH branches)

Cold-cache login is now ~2× current latency due to one spawn_blocking
hop, but the worker no longer parks on Argon2 so steady-state under
load is dramatically better. The LRU cache for the hot-path (token →
principal) is finding F-P-009 — separate commit.

AUDIT.md anchor: F-P-002.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-06-07 20:10:54 +02:00
parent 5303419eec
commit 1cd8791bff
3 changed files with 35 additions and 6 deletions

View File

@@ -203,9 +203,20 @@ async fn verify_api_key(state: &AuthState, rest: &str) -> Result<Option<Principa
}
};
let matched: Option<ApiKeyVerification> = candidates
.into_iter()
.find(|c| verify_password(&c.hash, rest));
// F-P-002: Argon2id verify is CPU-bound (~tens of ms each at OWASP
// defaults). Move it off the Tokio async worker so a hot user with
// N prefix-colliding keys doesn't park the worker for N×Argon2.
let rest_owned = rest.to_string();
let matched: Option<ApiKeyVerification> = tokio::task::spawn_blocking(move || {
candidates
.into_iter()
.find(|c| verify_password(&c.hash, &rest_owned))
})
.await
.map_err(|e| {
tracing::error!(error = ?e, "verify_api_key spawn_blocking join failed");
InternalError
})?;
let Some(matched) = matched else {
return Ok(None);
};