fix(api): cap the API-key verdict cache at 10,000 entries
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 10m7s
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 10m7s
Callers choose the keys, so cached invalid verdicts must not grow without end: at the cap expired verdicts are dropped first, and if none are, the new verdict is answered but not kept. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
This commit is contained in:
1 parent
ae8761c15c
commit
2254e1c8ff
1 file changed
+46
-1
@@ -25,6 +25,10 @@ use compliance_core::AgentConfig;
|
||||
/// How long an entitlement answer is trusted before it is re-fetched.
|
||||
pub const ENTITLEMENT_TTL: Duration = Duration::from_secs(60);
|
||||
|
||||
/// The most API-key verdicts held at once. Callers choose the keys, so an
|
||||
/// unbounded cache of invalid ones would grow without end.
|
||||
const API_KEY_CACHE_MAX: usize = 10_000;
|
||||
|
||||
/// How long an API-key verdict is reused. A revoked key therefore keeps
|
||||
/// working for up to this long; past it, an unreachable registry means `503`.
|
||||
pub const API_KEY_TTL: Duration = Duration::from_secs(60);
|
||||
@@ -509,9 +513,23 @@ impl RegistryClient {
|
||||
}
|
||||
}
|
||||
let verdict = self.fetch_api_key_verdict(key).await?;
|
||||
self.cache_api_key_verdict(digest, &verdict);
|
||||
Ok(verdict)
|
||||
}
|
||||
|
||||
/// Keep `verdict` under `digest`. At [`API_KEY_CACHE_MAX`] the expired
|
||||
/// verdicts go first; if none have, this one is not kept (the request
|
||||
/// still gets it).
|
||||
fn cache_api_key_verdict(&self, digest: String, verdict: &ApiKeyVerify) {
|
||||
if self.api_keys.len() >= API_KEY_CACHE_MAX {
|
||||
self.api_keys
|
||||
.retain(|_, (at, _)| at.elapsed() < API_KEY_TTL);
|
||||
if self.api_keys.len() >= API_KEY_CACHE_MAX {
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.api_keys
|
||||
.insert(digest, (Instant::now(), verdict.clone()));
|
||||
Ok(verdict)
|
||||
}
|
||||
|
||||
async fn fetch_api_key_verdict(&self, key: &str) -> Result<ApiKeyVerify, String> {
|
||||
@@ -735,6 +753,33 @@ mod tests {
|
||||
assert!(!err.contains("bp_good"), "{err}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_full_cache_drops_expired_verdicts_and_never_grows_past_its_cap() {
|
||||
let (client, _, _) = verify_registry().await;
|
||||
let verdict = client.verify_api_key("bp_good").await.unwrap();
|
||||
let aged = Instant::now()
|
||||
.checked_sub(API_KEY_TTL + Duration::from_secs(1))
|
||||
.unwrap();
|
||||
for i in 0..API_KEY_CACHE_MAX {
|
||||
client
|
||||
.api_keys
|
||||
.insert(format!("old-{i}"), (aged, verdict.clone()));
|
||||
}
|
||||
client.verify_api_key("bp_new").await.unwrap();
|
||||
assert!(client.api_keys.len() < 10, "{}", client.api_keys.len());
|
||||
assert!(client.api_keys.contains_key(&key_digest("bp_new")));
|
||||
|
||||
// Full of fresh verdicts: the new one is answered but not kept.
|
||||
for i in 0..API_KEY_CACHE_MAX {
|
||||
client
|
||||
.api_keys
|
||||
.insert(format!("new-{i}"), (Instant::now(), verdict.clone()));
|
||||
}
|
||||
let before = client.api_keys.len();
|
||||
assert!(client.verify_api_key("bp_newer").await.unwrap().valid);
|
||||
assert_eq!(client.api_keys.len(), before);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn the_cache_holds_a_digest_never_the_key() {
|
||||
let (client, _, _) = verify_registry().await;
|
||||
|
||||
Reference in new issue
Block a user