mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 20:23:39 +09:00
fix(observability-proxy): say that every @ is refused on app roots
The lookahead scan let @ start() and @ end() through and the 422 named them as allowed, but the lookback scan cannot bound any @, so every @ query was refused anyway, with a message pointing at forms that would not pass. Both scans now treat any @ as unknown and the message says so. The lookback-cap test now exercises the cap with a window just past it rather than an @ query. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
This commit is contained in:
1 parent
501f0d98f7
commit
0bb98ced05
4 files changed
+34
-18
No files matched your search
@@ -102,7 +102,7 @@ func applicationSelectors(ns, kind, name string, pods, pvcs []string) []string {
|
||||
// application's series.
|
||||
func (s *Server) applicationScope(ctx context.Context, r *http.Request, store string, req metricsRequest, ns, kind, name string) ([]string, *HTTPError) {
|
||||
if req.last.IsZero() {
|
||||
return nil, errorf(http.StatusUnprocessableEntity, "the query reads samples at a time the application scope cannot cover (an @ other than start() or end(), or an offset that is not a plain duration); use the namespace root")
|
||||
return nil, errorf(http.StatusUnprocessableEntity, "the query reads samples at a time the application scope cannot cover (an @ modifier, or an offset that is not a plain duration); use the namespace root")
|
||||
}
|
||||
lim := s.cfg.Metrics.App
|
||||
if req.start.IsZero() || req.start.Before(req.from.Add(-lim.Lookback)) {
|
||||
|
||||
@@ -131,22 +131,14 @@ func queryLookback(query string, step time.Duration) (time.Duration, bool) {
|
||||
|
||||
// queryLookahead is an upper bound on how far after its evaluation window
|
||||
// a query reads, from the same lexical scan. Only a negative offset reads
|
||||
// forward; the negative offsets are summed, which covers nesting. An @
|
||||
// modifier pins samples to any instant unless it names start() or end(),
|
||||
// which lie in the window, and WITH can apply one offset several times;
|
||||
// either makes the bound unknown.
|
||||
// forward; the negative offsets are summed, which covers nesting. Any @
|
||||
// makes the bound unknown, as it does for queryLookback, and so does WITH,
|
||||
// which can apply one offset several times.
|
||||
func queryLookahead(query string) (time.Duration, bool) {
|
||||
lower := strings.ToLower(query)
|
||||
for rest := lower; ; {
|
||||
at := strings.IndexByte(rest, '@')
|
||||
if at < 0 {
|
||||
break
|
||||
}
|
||||
rest = strings.TrimLeft(rest[at+1:], " \t\n\r")
|
||||
if !strings.HasPrefix(rest, "start()") && !strings.HasPrefix(rest, "end()") {
|
||||
return 0, false
|
||||
}
|
||||
if strings.Contains(query, "@") {
|
||||
return 0, false
|
||||
}
|
||||
lower := strings.ToLower(query)
|
||||
_, ahead, ok := scanOffsets(lower)
|
||||
if !ok || ahead > 0 && hasKeyword(lower, "with") || ahead > float64(maxMetricsDuration) {
|
||||
return 0, false
|
||||
|
||||
@@ -441,8 +441,6 @@ func TestQueryLookahead(t *testing.T) {
|
||||
{`x OFFSET - 90m`, 90 * time.Minute},
|
||||
{`rate(x[5m] offset -30m) offset -1h`, 90 * time.Minute},
|
||||
{`x offset -1h + y offset 2h`, time.Hour},
|
||||
{`x @ end()`, 0},
|
||||
{`x @ start() offset -1h`, time.Hour},
|
||||
{`node_offset_seconds - 1`, 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@@ -455,6 +453,10 @@ func TestQueryLookahead(t *testing.T) {
|
||||
}
|
||||
for _, q := range []string{
|
||||
`x @ 1700000000`,
|
||||
// The lookback scan cannot bound any @, so the application root
|
||||
// refuses every one; the lookahead does not tell them apart.
|
||||
`x @ end()`,
|
||||
`x @ start() offset -1h`,
|
||||
`x @ (end() - 1h)`,
|
||||
`x{a="a@b"}`,
|
||||
`x offset -5i`,
|
||||
|
||||
@@ -324,7 +324,9 @@ func TestServerApplicationNarrowedGrant(t *testing.T) {
|
||||
func TestServerApplicationLookbackBeyondCap(t *testing.T) {
|
||||
cfg := Config{Metrics: MetricsConfig{App: AppScopeLimits{Lookback: time.Hour, MaxPods: 100, MaxPVCs: 100}}}
|
||||
h := newPolicyHarness(t, cfg, appMetricsUpstream(nil, nil, http.StatusOK), appPolicy, nil)
|
||||
for _, query := range []string{"rate(x[7d])", "x[30d] @ end()", "rate(x[5i])"} {
|
||||
// max_over_time(x[2h]) reads just past the 1h cap; rate(x[5i]) has an
|
||||
// unknown lookback.
|
||||
for _, query := range []string{"rate(x[7d])", "max_over_time(x[2h])", "rate(x[5i])"} {
|
||||
q := url.Values{"query": {query}, "time": {unix(testNow)}}
|
||||
if got := h.status(http.MethodGet, appRoot+"db.main/metrics/api/v1/query?"+q.Encode(), "good"); got != http.StatusUnprocessableEntity {
|
||||
t.Errorf("%s answered %d, want 422", query, got)
|
||||
@@ -485,3 +487,23 @@ func TestServerApplicationLookupStoreErrors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestServerApplicationRefusesEveryAt: no @ form can be bounded, and the
|
||||
// answer says so rather than naming forms that would pass.
|
||||
func TestServerApplicationRefusesEveryAt(t *testing.T) {
|
||||
h := newPolicyHarness(t, Config{}, appMetricsUpstream(nil, nil, http.StatusOK), appPolicy, nil)
|
||||
for _, query := range []string{"up @ end()", "up @ start()", "up @ 1700000000"} {
|
||||
resp := h.do(http.MethodGet, appRoot+"db.main/metrics/api/v1/query?"+url.Values{"query": {query}}.Encode(), "good", nil, nil)
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnprocessableEntity {
|
||||
t.Errorf("%s answered %d, want 422", query, resp.StatusCode)
|
||||
}
|
||||
if strings.Contains(string(body), "start() or end()") {
|
||||
t.Errorf("%s: the refusal names @ forms as allowed: %q", query, body)
|
||||
}
|
||||
}
|
||||
if n := len(h.requests()); n != 0 {
|
||||
t.Fatalf("upstream saw %d requests for refused queries", n)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user