feat: add /health Bearer auth (#73) and readiness probe hysteresis (#72)#141
feat: add /health Bearer auth (#73) and readiness probe hysteresis (#72)#141
Conversation
Disk and memory threshold checks now use a 5% hysteresis margin to prevent flapping when values hover near the boundary. Once a check fails, it must recover past threshold ± 5% before returning to ok.
- Extract shared extract_bearer_token helper (DRY) - Add "recovering" field to health response JSON - Reject whitespace-only bearer tokens - Add startup log when TRUSS_HEALTH_TOKEN is set - Make hysteresis margin configurable via TRUSS_HEALTH_HYSTERESIS_MARGIN - Replace bool with ThresholdDirection enum
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR adds optional Bearer token authentication to the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Router as Routing Layer
participant AuthCheck as Auth Check
participant Handler as Health Handler
participant Cache as Health Cache
participant Response
Client->>Router: GET /health
Router->>AuthCheck: Check health_token configured?
alt health_token is set
AuthCheck->>AuthCheck: Extract Bearer token from header
alt Token missing or invalid
AuthCheck->>Response: 401 Unauthorized + WWW-Authenticate
Response->>Client: 401 with problem+json
else Token valid
AuthCheck->>Handler: Proceed to health check
Handler->>Cache: Check disk/memory with hysteresis
Cache->>Cache: Apply threshold + margin logic
Cache->>Handler: Return (status, recovering)
Handler->>Response: 200 OK with health JSON
Response->>Client: 200 with status
end
else health_token not set
Router->>Handler: Proceed to health check
Handler->>Cache: Check disk/memory with hysteresis
Cache->>Handler: Return (status, recovering)
Handler->>Response: 200 OK with health JSON
Response->>Client: 200 with status
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
TRUSS_HEALTH_TOKENenv var for Bearer authentication on/health. Sub-paths/health/liveand/health/readyremain unauthenticated for load balancer probes.TRUSS_HEALTH_HYSTERESIS_MARGIN(default 5%, range 0.01–0.50).extract_bearer_tokenhelper (DRY), expose"recovering"state in health JSON, reject whitespace-only tokens, add startup log, replaceboolwithThresholdDirectionenum.Closes #73, #72
Changes
src/adapters/server/auth.rsextract_bearer_tokenhelpersrc/adapters/server/config.rshealth_tokenandhysteresis_marginfields,parse_env_f64_rangedsrc/adapters/server/handler.rsThresholdDirectionenum,check_with_hysteresisreturns(ok, recovering), configurable marginsrc/adapters/server/routing.rs/healthauth (before body read)src/adapters/server/mod.rsHealthCache::new()doc/openapi.yaml/healthdocs/configuration.mdTRUSS_HEALTH_TOKENandTRUSS_HEALTH_HYSTERESIS_MARGINtests/server_transform_basic.rsTest plan
cargo fmt --all -- --checkpassescargo clippy --all-targets --all-features -- -D warningspassescargo test— all 941+ tests pass/health/liveand/health/readyunaffectedHigherIsWorse(memory) andLowerIsWorse(disk) directionsSummary by CodeRabbit
New Features
/healthendpoint viaTRUSS_HEALTH_TOKENconfigurationTRUSS_HEALTH_HYSTERESIS_MARGINconfigurationDocumentation