From e6697ea4f176d1b059a40bab9bb87b31340d4e08 Mon Sep 17 00:00:00 2001 From: ZverGuy Date: Wed, 4 Nov 2026 06:11:18 +0700 Subject: [PATCH] test(observability-proxy): cover malformed pod lookups and the lookup window An empty name in a lookup answer would turn the pod branch into a pattern that matches every series without a pod label, widening the application scope to the namespace; nothing pinned the guard against it. Malformed answers must only drop their branch, and the lookup must start at the query's own lookback, or at the configured limit when that lookback is unknown. Assisted-by: LLM Signed-off-by: ZverGuy --- internal/observabilityproxy/appkinds.go | 6 ++- internal/observabilityproxy/appscope.go | 6 +-- .../observabilityproxy/server_app_test.go | 39 +++++++++++++++++-- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/internal/observabilityproxy/appkinds.go b/internal/observabilityproxy/appkinds.go index 5e545f390..d58368a13 100644 --- a/internal/observabilityproxy/appkinds.go +++ b/internal/observabilityproxy/appkinds.go @@ -79,7 +79,11 @@ func (k *ApplicationKinds) Kind(plural string) (string, error) { } var kinds []string for _, obj := range objs { - kind, _, _ := unstructured.NestedString(obj.(*unstructured.Unstructured).Object, "spec", "application", "kind") + u, ok := obj.(*unstructured.Unstructured) + if !ok { + continue + } + kind, _, _ := unstructured.NestedString(u.Object, "spec", "application", "kind") if kind != "" && !slices.Contains(kinds, kind) { kinds = append(kinds, kind) } diff --git a/internal/observabilityproxy/appscope.go b/internal/observabilityproxy/appscope.go index b969cc502..3b192d056 100644 --- a/internal/observabilityproxy/appscope.go +++ b/internal/observabilityproxy/appscope.go @@ -121,15 +121,15 @@ func (s *Server) applicationScope(ctx context.Context, r *http.Request, store st return applicationSelectors(ns, kind, name, lists[0], lists[1]), nil } -// lookupNames asks for at most max+1 values, enough to tell a list over +// lookupNames asks for at most limit+1 values, enough to tell a list over // the cap without reading all of it. -func (s *Server) lookupNames(ctx context.Context, r *http.Request, store, label, match, ns string, start, end time.Time, max int) ([]string, error) { +func (s *Server) lookupNames(ctx context.Context, r *http.Request, store, label, match, ns string, start, end time.Time, limit int) ([]string, error) { params := url.Values{} params.Set("match[]", match) params.Set("extra_filters[]", namespaceSelector(ns)) params.Set("start", formatTime(start)) params.Set("end", formatTime(end)) - params.Set("limit", strconv.Itoa(max+1)) + params.Set("limit", strconv.Itoa(limit+1)) resp, herr := s.post(ctx, r, s.cfg.Metrics.UpstreamURL(store)+"/api/v1/label/"+label+"/values", params, "metrics store", nil) if herr != nil { return nil, herr diff --git a/internal/observabilityproxy/server_app_test.go b/internal/observabilityproxy/server_app_test.go index 14807637e..25d8f2a7f 100644 --- a/internal/observabilityproxy/server_app_test.go +++ b/internal/observabilityproxy/server_app_test.go @@ -173,9 +173,9 @@ func TestServerApplicationMetricsScope(t *testing.T) { if got := r.form["extra_filters[]"]; !slices.Equal(got, []string{wantMetricsScope}) { t.Errorf("%s extra_filters[] = %q", r.path, got) } - start, herr := parseMetricsTime("start", r.form.Get("start")) - if herr != nil || start.After(testNow.Add(-time.Hour)) || start.Before(testNow.Add(-25*time.Hour)) { - t.Errorf("%s start = %s, want within the lookback before the window", r.path, r.form.Get("start")) + // The query's own lookback is known and shorter than the 24h cap. + if want := formatTime(buildMetrics(t, "api/v1/query_range", hostile).start); r.form.Get("start") != want { + t.Errorf("%s start = %s, want the query's earliest read %s", r.path, r.form.Get("start"), want) } if r.form.Get("end") != formatTime(testNow) || r.form.Get("limit") != "101" { t.Errorf("%s end/limit = %s/%s", r.path, r.form.Get("end"), r.form.Get("limit")) @@ -204,6 +204,7 @@ func TestServerApplicationMetricsBranches(t *testing.T) { name string pods, pvcs []string podStatus int + podBody string cfg Config path string wantCode int @@ -218,13 +219,29 @@ func TestServerApplicationMetricsBranches(t *testing.T) { path: "api/v1/query?query=up", wantCode: http.StatusUnprocessableEntity}, {name: "labels get the branches as match[]", pods: []string{"p"}, path: "api/v1/labels", wantCode: http.StatusOK, want: []string{wantIdentityBranch, podBranch}}, {name: "series needs match[]", path: "api/v1/series", wantCode: http.StatusBadRequest}, + // An empty name would make pod=~"^(|p)$", which matches every + // series without a pod label. + {name: "empty pod name dropped", pods: []string{"", "p"}, path: "api/v1/query?query=up", wantCode: http.StatusOK, want: []string{wantIdentityBranch, podBranch}}, + {name: "only empty pod names", pods: []string{""}, path: "api/v1/query?query=up", wantCode: http.StatusOK, want: []string{wantIdentityBranch}}, + {name: "lookup status error", podBody: `{"status":"error","data":["p"]}`, path: "api/v1/query?query=up", wantCode: http.StatusOK, want: []string{wantIdentityBranch}}, + {name: "truncated lookup answer", podBody: `{"status":"success","data":["p"`, path: "api/v1/query?query=up", wantCode: http.StatusOK, want: []string{wantIdentityBranch}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.podStatus == 0 { tt.podStatus = http.StatusOK } - h := newPolicyHarness(t, tt.cfg, appMetricsUpstream(tt.pods, tt.pvcs, tt.podStatus), appPolicy, nil) + upstream := appMetricsUpstream(tt.pods, tt.pvcs, tt.podStatus) + if tt.podBody != "" { + upstream = func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/v1/label/pod/values" { + _, _ = io.WriteString(w, tt.podBody) + return + } + appMetricsUpstream(nil, nil, http.StatusOK)(w, r) + } + } + h := newPolicyHarness(t, tt.cfg, upstream, appPolicy, nil) if got := h.status(http.MethodGet, appRoot+"db.main/metrics/"+tt.path, "good"); got != tt.wantCode { t.Fatalf("status = %d, want %d", got, tt.wantCode) } @@ -282,3 +299,17 @@ func TestServerApplicationNarrowedGrant(t *testing.T) { } } } + +// An expression whose lookback cannot be bounded looks pods up from the +// configured lookback before the window. +func TestServerApplicationLookupUnknownLookback(t *testing.T) { + h := newPolicyHarness(t, Config{}, appMetricsUpstream(nil, nil, http.StatusOK), appPolicy, nil) + q := url.Values{"query": {"up @ end()"}, "time": {unix(testNow)}} + if got := h.status(http.MethodGet, appRoot+"db.main/metrics/api/v1/query?"+q.Encode(), "good"); got != http.StatusOK { + t.Fatalf("status = %d", got) + } + r := h.requests()[0] + if want := formatTime(testNow.Add(-24 * time.Hour)); r.path != "/api/v1/label/pod/values" || r.form.Get("start") != want { + t.Fatalf("lookup %s start = %s, want %s", r.path, r.form.Get("start"), want) + } +}