feat(dwctl): per-process internal credential for calls into /ai (DW-1009)

Merged
TO
12d ago
+2031-729

Part of DW-1005 (DW-1009). The playground, health probes and continuation authenticated as hidden keys by reading their plaintext secrets. They now authenticate by key id with a per-process internal credential.

What changes

Internal credential (dwctl/src/auth/internal_credential.rs): dwi-<key id>.<expiry>.<HMAC-SHA256>, signed with a 32-byte key generated at startup and held only in memory, so only the process that minted a token accepts it. 60s TTL for playground and probes.

Verifying layer on /ai: verifies a dwi- bearer, looks the key id up in onwards' live key set and passes its hash on as an onwards PresentedKey. An invalid, expired, foreign or deleted-key token is a 401 invalid_api_key, never passed through as an ordinary key. Layers that hashed the bearer (inference middleware, error enrichment, prompt cache, continuation, image normaliser, prices, rejections, /models) use the presented hash, so spend caps and attribution still apply.

onwards: PresentedKey request extension, preferred over hashing the bearer; the key must still be in the key set. Targets::key_ids maps the api_key_id label to the key hash. Adds a public field to Targets, so it rides the pending onwards 2.0.0 (#1802).

Callers: playground and continuation use the hidden key's id; probes and deployment checks use the system key's id, and ak.secret is gone from those queries. Hidden-key repository functions return the key id.

Not yet: batch, file, flex and sample-file creation still hand fusillade a stored key, via ApiKeys::legacy_dispatch_secret, until DW-1010 dispatches by key id.

Testing

  • Forged, malformed, expired, other-process, unknown-key and deleted-key tokens are rejected with no upstream call; a valid token reaches the upstream with no dwi- header; playground and test_probe work end to end over HTTP.
  • cargo nextest run -p dwctl -p onwards (3690 passed) and just lint rust pass locally.