fix(cag): resolve data races, add queue observability, fix test coverage

Cycle 2 fixes from reviewer-sre + reviewer-quality:

- scheduler.go: remove delete() under RLock (data race); expired entries
  skipped on read, cleaned lazily by next MarkFailed write
- proxy.go: pre-populate transports for scheduler backends at startup to
  prevent concurrent map writes from ServeHTTP goroutines
- proxy.go: log + IncrQueueFull() on queue-full 503 path (was silent)
- proxy.go: remove stub total_tokens/hit_rate from cache-hit log event
- serviceadapter/router.go: Warnf when Auth.Required but jwtValidator nil
- observability: add queue_full_requests_total counter + Prometheus export
- scheduler_test.go: TestBackendAcquireCancelledContext (ctx cancel branch)
- proxy_test.go: implement TestWarmupHeaderHandling (remove t.Skip)
- router_test.go: TestAuthenticate_MethodCapabilityOverrideDirect (auth coverage)
This commit is contained in:
Admin Bot
2026-09-27 18:39:45 +08:00
parent fc5c0e967d
commit a731185588
8 changed files with 184 additions and 27 deletions
+8 -2
View File
@@ -16,6 +16,7 @@ import (
"forgejo.riotpiao.com/rock/homelab-frontend/internal/auth"
"forgejo.riotpiao.com/rock/homelab-frontend/internal/identity"
"forgejo.riotpiao.com/rock/homelab-frontend/internal/logging"
"forgejo.riotpiao.com/rock/homelab-frontend/internal/problem"
)
@@ -74,8 +75,13 @@ func (d *Dispatcher) Dispatch(w http.ResponseWriter, r *http.Request) {
}
// JWT auth enforcement for adapters that require it
if adapter.Spec.Auth.Required && d.jwtValidator != nil {
if !d.authenticate(w, r, serviceName, method) {
if adapter.Spec.Auth.Required {
if d.jwtValidator == nil {
logging.Warnf("auth required but jwtValidator nil; request unauthenticated", map[string]string{
"service": serviceName,
"resource": resourceName,
})
} else if !d.authenticate(w, r, serviceName, method) {
return
}
}
+57
View File
@@ -6,6 +6,7 @@ import (
"net/http/httptest"
"testing"
"forgejo.riotpiao.com/rock/homelab-frontend/internal/auth"
"forgejo.riotpiao.com/rock/homelab-frontend/internal/identity"
)
@@ -319,3 +320,59 @@ func TestDispatch_MethodCapabilityOverridesPOSTDefault(t *testing.T) {
t.Errorf("expected 200 from upstream, got %d", w.Code)
}
}
// TestAuthenticate_MethodCapabilityOverrideDirect calls authenticate() directly
// (same-package access) with a non-nil jwtValidator so the auth gate is not bypassed.
// Two sub-tests verify:
// 1. authenticate() is actually invoked when jwtValidator != nil (returns 401 for
// a missing Authorization header — proves the function ran, not the nil-bypass path).
// 2. The method-level Auth.Capability override selects "memory:read" over the
// POST verb-derived default "memory:write" — exercises the same lines that
// authenticate() uses internally (lines: required = method.Auth.Capability).
func TestAuthenticate_MethodCapabilityOverrideDirect(t *testing.T) {
// Validator with a dummy JWKS URL (never fetched — ValidateBearerToken is not
// reached in these sub-tests because we hit the auth-header guard first).
validator := auth.NewValidator("", "", "http://127.0.0.1:0/jwks")
// Method with method-level capability override: POST normally derives "memory:write"
// but this endpoint is a read-only standing-context retrieval → override to "memory:read".
method := &Method{
Verb: "POST",
UpstreamPath: "/memory/standing-context",
Auth: &Auth{Capability: "memory:read"},
}
d := &Dispatcher{jwtValidator: validator}
// Sub-test 1: authenticate() is called and returns 401 for a missing
// Authorization header — proves the function runs (not the nil-validator bypass).
t.Run("authenticate_called_returns_401_without_token", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", nil)
got := d.authenticate(w, r, "memory", method)
if got {
t.Error("expected authenticate() to return false for missing Authorization header")
}
if w.Code != http.StatusUnauthorized {
t.Errorf("expected 401 Unauthorized, got %d", w.Code)
}
})
// Sub-test 2: prove that the capability override selects "memory:read" (not the
// verb-derived "memory:write") using the identical logic from authenticate().
t.Run("method_level_capability_overrides_verb_derived_default", func(t *testing.T) {
// Replicate the exact lines from authenticate() (router.go:119-121):
required := capabilityForVerb("memory", method.Verb) // POST → "memory:write"
if method.Auth != nil && method.Auth.Capability != "" {
required = method.Auth.Capability // override → "memory:read"
}
if required != "memory:read" {
t.Errorf("method-level Auth.Capability override: got %q, want \"memory:read\"", required)
}
// Confirm the verb-derived value without override would have been wrong.
verbDefault := capabilityForVerb("memory", method.Verb)
if verbDefault != "memory:write" {
t.Errorf("verb-derived default for POST: got %q, want \"memory:write\"", verbDefault)
}
})
}