package proxy import ( "context" "encoding/json" "fmt" "io" "net" "net/http" "net/http/httptest" "net/url" "strconv" "sync/atomic" "testing" "time" "go.iscode.ca/mats/internal/query" "go.iscode.ca/mats/internal/types" ) type testListenerProvider struct{} func (testListenerProvider) Listen(ctx context.Context, network, addr string) (net.Listener, error) { var lc net.ListenConfig return lc.Listen(ctx, network, addr) } func TestSecurityGate_Filtering(t *testing.T) { gate := NewSecurityGate([]string{"node-demo", "weather"}, []string{"load.short", "temp"}, 7*24*time.Hour) now := time.Now().UnixMilli() // 1. Valid specific query q := url.Values{ "host": []string{"node-demo"}, "service": []string{"load.short"}, "from": []string{fmt.Sprintf("%d", now-3600*1000)}, "to": []string{fmt.Sprintf("%d", now)}, } sanitized, _, err := gate.SanitizeQuery(q, now) if err != nil { t.Fatalf("expected allowed query, got: %v", err) } if sanitized.Get("host") != "node-demo" { t.Errorf("expected host node-demo, got %s", sanitized.Get("host")) } if sanitized.Get("service") != "load.short" { t.Errorf("expected service load.short, got %s", sanitized.Get("service")) } // 1b. Anchored query as generated by web UI (^node-demo$, ^(node-demo)$, ^load.short$) qAnchored := url.Values{ "host": []string{"^node-demo$"}, "service": []string{"^load.short$"}, } sanitizedAnchored, _, errAnchored := gate.SanitizeQuery(qAnchored, now) if errAnchored != nil { t.Fatalf("expected anchored query to be allowed, got error: %v", errAnchored) } if sanitizedAnchored.Get("host") != "^node-demo$" { t.Errorf("expected preserved host ^node-demo$, got %s", sanitizedAnchored.Get("host")) } if sanitizedAnchored.Get("service") != "^load.short$" { t.Errorf("expected preserved service ^load.short$, got %s", sanitizedAnchored.Get("service")) } // 2. Wildcard query rewriting qWild := url.Values{ "host": []string{".*"}, "service": []string{".*"}, } sanitizedWild, _, errWild := gate.SanitizeQuery(qWild, now) if errWild != nil { t.Fatalf("expected wildcard to be rewritten, got error: %v", errWild) } if sanitizedWild.Get("host") != "^(node-demo|weather)$" { t.Errorf("expected rewritten host regex, got %s", sanitizedWild.Get("host")) } if sanitizedWild.Get("service") != "^(load\\.short|temp)$" { t.Errorf("expected rewritten service regex, got %s", sanitizedWild.Get("service")) } // 3. Forbidden host query qForbidden := url.Values{ "host": []string{"internal-nas"}, "service": []string{"load.short"}, } if _, _, err := gate.SanitizeQuery(qForbidden, now); err == nil { t.Errorf("expected error for unapproved host, got nil") } // 4. Forbidden service query qForbiddenSvc := url.Values{ "host": []string{"node-demo"}, "service": []string{"private.vault"}, } if _, _, err := gate.SanitizeQuery(qForbiddenSvc, now); err == nil { t.Errorf("expected error for unapproved service, got nil") } // 4b. Substring and partial regex service query on hierarchical services hGate := NewSecurityGate([]string{"VS5-CA-MFA0436A", "io"}, []string{"dyson/humidity", "switchbot/humidity"}, 7*24*time.Hour) qSubstr := url.Values{ "host": []string{".*"}, "service": []string{"humidity"}, } sanitizedSubstr, _, errSubstr := hGate.SanitizeQuery(qSubstr, now) if errSubstr != nil { t.Fatalf("expected substring query 'humidity' to match allowed services, got error: %v", errSubstr) } if sanitizedSubstr.Get("service") != "^(dyson/humidity|switchbot/humidity)$" { t.Errorf("expected rewritten service regex ^(dyson/humidity|switchbot/humidity)$, got %s", sanitizedSubstr.Get("service")) } // Query for single hierarchical prefix "dyson" qDyson := url.Values{ "host": []string{"VS5-CA-MFA0436A"}, "service": []string{"dyson"}, } sanitizedDyson, _, errDyson := hGate.SanitizeQuery(qDyson, now) if errDyson != nil { t.Fatalf("expected query 'dyson' to succeed, got error: %v", errDyson) } if sanitizedDyson.Get("service") != "^(dyson/humidity)$" { t.Errorf("expected rewritten service regex ^(dyson/humidity)$, got %s", sanitizedDyson.Get("service")) } // Query for unapproved service "co2" qCo2 := url.Values{ "host": []string{"io"}, "service": []string{"co2"}, } if _, _, err := hGate.SanitizeQuery(qCo2, now); err == nil { t.Errorf("expected error for unapproved service 'co2', got nil") } // Suffix allow rule match: rule "humidity" matches "switchbot/humidity" sGate := NewSecurityGate([]string{"io"}, []string{"humidity"}, 7*24*time.Hour) if !sGate.IsServiceAllowed("switchbot/humidity") { t.Errorf("expected allow-rule 'humidity' to match service 'switchbot/humidity'") } // 4c. Regex allow rules: ".*/humidity" rGate := NewSecurityGate([]string{"io", "VS5-CA-.*"}, []string{".*/humidity"}, 7*24*time.Hour) if !rGate.IsServiceAllowed("dyson/humidity") { t.Errorf("expected regex allow-rule '.*/humidity' to match 'dyson/humidity'") } if !rGate.IsServiceAllowed("switchbot/humidity") { t.Errorf("expected regex allow-rule '.*/humidity' to match 'switchbot/humidity'") } if rGate.IsServiceAllowed("switchbot/co2") { t.Errorf("expected regex allow-rule '.*/humidity' to NOT match 'switchbot/co2'") } if !rGate.IsHostAllowed("VS5-CA-MFA0436A") { t.Errorf("expected regex host allow-rule 'VS5-CA-.*' to match 'VS5-CA-MFA0436A'") } // Wildcard query with regex allow rules qRegexWild := url.Values{ "host": []string{".*"}, "service": []string{".*"}, } sanitizedRegexWild, _, errRegexWild := rGate.SanitizeQuery(qRegexWild, now) if errRegexWild != nil { t.Fatalf("expected wildcard query with regex allow-rules to succeed: %v", errRegexWild) } if sanitizedRegexWild.Get("service") != "^((?:.*/humidity))$" { t.Errorf("expected rewritten service regex ^((?:.*/humidity))$, got %s", sanitizedRegexWild.Get("service")) } if sanitizedRegexWild.Get("host") != "^((?:VS5-CA-.*)|io)$" { t.Errorf("expected rewritten host regex ^((?:VS5-CA-.*)|io)$, got %s", sanitizedRegexWild.Get("host")) } // Substring / user query 'humidity' matching regex rule '.*/humidity' qHumidity := url.Values{ "host": []string{"io"}, "service": []string{"humidity"}, } sanitizedHumidity, _, errHumidity := rGate.SanitizeQuery(qHumidity, now) if errHumidity != nil { t.Fatalf("expected query 'humidity' to match regex allow-rule '.*/humidity': %v", errHumidity) } if sanitizedHumidity.Get("service") != "^(?:.*/humidity)$" { t.Errorf("expected service ^(?:.*/humidity)$, got %s", sanitizedHumidity.Get("service")) } // Concrete query for 'dyson/humidity' qDysonHum := url.Values{ "host": []string{"io"}, "service": []string{"dyson/humidity"}, } sanitizedDysonHum, _, errDysonHum := rGate.SanitizeQuery(qDysonHum, now) if errDysonHum != nil { t.Fatalf("expected query 'dyson/humidity' to succeed: %v", errDysonHum) } if sanitizedDysonHum.Get("service") != "dyson/humidity" { t.Errorf("expected preserved service 'dyson/humidity', got %s", sanitizedDysonHum.Get("service")) } // Anchored query '^switchbot/humidity$' qAnchoredHum := url.Values{ "host": []string{"io"}, "service": []string{"^switchbot/humidity$"}, } sanitizedAnchoredHum, _, errAnchoredHum := rGate.SanitizeQuery(qAnchoredHum, now) if errAnchoredHum != nil { t.Fatalf("expected query '^switchbot/humidity$' to succeed: %v", errAnchoredHum) } if sanitizedAnchoredHum.Get("service") != "^switchbot/humidity$" { t.Errorf("expected preserved service '^switchbot/humidity$', got %s", sanitizedAnchoredHum.Get("service")) } // Forbidden query for unapproved service 'co2' qBadSvc := url.Values{ "host": []string{"io"}, "service": []string{"co2"}, } if _, _, err := rGate.SanitizeQuery(qBadSvc, now); err == nil { t.Errorf("expected error for unapproved query 'co2', got nil") } // Scrubbing verification with regex rules rawJSON := `[ {"host":"io","service":"switchbot/humidity","points":[[100,50.0]]}, {"host":"io","service":"switchbot/co2","points":[[100,800.0]]}, {"host":"VS5-CA-MFA0436A","service":"dyson/humidity","points":[[100,55.0]]}, {"host":"unapproved-host","service":"dyson/humidity","points":[[100,55.0]]} ]` scrubbedBytes, errScrub := rGate.ScrubSeriesResults([]byte(rawJSON)) if errScrub != nil { t.Fatalf("unexpected scrubbing error: %v", errScrub) } var scrubbedResults []query.SeriesResult if err := json.Unmarshal(scrubbedBytes, &scrubbedResults); err != nil { t.Fatalf("failed unmarshaling scrubbed results: %v", err) } if len(scrubbedResults) != 2 { t.Fatalf("expected exactly 2 scrubbed results, got %d: %+v", len(scrubbedResults), scrubbedResults) } if scrubbedResults[0].Host != "io" || scrubbedResults[0].Service != "switchbot/humidity" { t.Errorf("unexpected first scrubbed result: %+v", scrubbedResults[0]) } if scrubbedResults[1].Host != "VS5-CA-MFA0436A" || scrubbedResults[1].Service != "dyson/humidity" { t.Errorf("unexpected second scrubbed result: %+v", scrubbedResults[1]) } // 5. Window clamping (request 30 days when max window is 7 days) qClamped := url.Values{ "host": []string{"node-demo"}, "service": []string{"temp"}, "from": []string{fmt.Sprintf("%d", now-30*24*3600*1000)}, "to": []string{fmt.Sprintf("%d", now)}, } sanitizedClamped, _, err := gate.SanitizeQuery(qClamped, now) if err != nil { t.Fatalf("unexpected error on clamped query: %v", err) } fromVal, _ := url.QueryUnescape(sanitizedClamped.Get("from")) toVal, _ := url.QueryUnescape(sanitizedClamped.Get("to")) f, err := strconv.ParseInt(fromVal, 10, 64) if err != nil { t.Fatalf("failed parsing fromVal: %v", err) } toInt, err := strconv.ParseInt(toVal, 10, 64) if err != nil { t.Fatalf("failed parsing toVal: %v", err) } diffDays := (toInt - f) / (24 * 3600 * 1000) if diffDays > 8 { // allow slight delta for modulus quantization t.Errorf("expected window to be clamped near 7 days, got %d days", diffDays) } } func TestSecurityGate_ScrubSeriesResults(t *testing.T) { gate := NewSecurityGate([]string{"demo"}, []string{"ping"}, time.Hour) input := []query.SeriesResult{ { MetricID: 1, Host: "demo", Service: "ping", Timestamps: []int64{100, 200}, Values: []float64{1.1, 2.2}, }, { MetricID: 2, Host: "private-device", Service: "secret", Timestamps: []int64{100, 200}, Values: []float64{99.9, 99.9}, }, } data, _ := json.Marshal(input) scrubbed, err := gate.ScrubSeriesResults(data) if err != nil { t.Fatalf("scrub failed: %v", err) } var output []query.SeriesResult if err := json.Unmarshal(scrubbed, &output); err != nil { t.Fatalf("unmarshal scrubbed failed: %v", err) } if len(output) != 1 { t.Fatalf("expected 1 output series, got %d", len(output)) } if output[0].Host != "demo" || output[0].Service != "ping" { t.Errorf("unexpected scrubbed output: %+v", output[0]) } } func TestServer_EndToEnd(t *testing.T) { var upstreamCalls atomic.Uint64 // Mock upstream mats engine upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { upstreamCalls.Add(1) if r.URL.Path == "/api/v1/query" { res := []query.SeriesResult{ { MetricID: types.MetricID(10), Host: "demo-node", Service: "co2", Timestamps: []int64{1000, 2000}, Values: []float64{450.0, 455.0}, }, } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(res) return } if r.URL.Path == "/api/v1/render" { w.Header().Set("Content-Type", "image/svg+xml") _, _ = w.Write([]byte("")) return } http.NotFound(w, r) })) defer upstream.Close() cfg := DefaultConfig() cfg.BackendURL = upstream.URL cfg.ListenAddr = "127.0.0.1:0" cfg.AllowedHosts = []string{"demo-node"} cfg.AllowedServices = []string{"co2"} cfg.EnableSnapshot = false proxyServer, err := NewServer(cfg) if err != nil { t.Fatalf("NewServer failed: %v", err) } lp := testListenerProvider{} if err := proxyServer.Start(lp); err != nil { t.Fatalf("Start failed: %v", err) } defer func() { _ = proxyServer.Stop(context.Background()) }() baseURL := fmt.Sprintf("http://%s", proxyServer.Addr().String()) client := &http.Client{Timeout: 5 * time.Second} // 1. Initial query: should hit upstream and cache result resp1, err := client.Get(baseURL + "/api/v1/query?host=demo-node&service=co2") if err != nil { t.Fatalf("query failed: %v", err) } body1, _ := io.ReadAll(resp1.Body) _ = resp1.Body.Close() if resp1.StatusCode != http.StatusOK { t.Fatalf("expected 200 OK, got %d: %s", resp1.StatusCode, string(body1)) } if resp1.Header.Get("X-Cache") != "" && resp1.Header.Get("X-Cache") != "MISS" { t.Errorf("unexpected X-Cache header on first request: %s", resp1.Header.Get("X-Cache")) } if upstreamCalls.Load() != 1 { t.Errorf("expected 1 upstream call, got %d", upstreamCalls.Load()) } etag := resp1.Header.Get("ETag") if etag == "" { t.Errorf("expected ETag header in response") } // 2. Second query with identical parameters: should be served from cache! resp2, err := client.Get(baseURL + "/api/v1/query?host=demo-node&service=co2") if err != nil { t.Fatalf("second query failed: %v", err) } _ = resp2.Body.Close() if resp2.StatusCode != http.StatusOK { t.Errorf("expected 200 OK on cached request, got %d", resp2.StatusCode) } // Upstream call count must remain 1 if upstreamCalls.Load() != 1 { t.Errorf("expected upstream call count to stay 1, got %d", upstreamCalls.Load()) } // 3. Conditional GET with If-None-Match: should return 304 Not Modified req3, _ := http.NewRequest(http.MethodGet, baseURL+"/api/v1/query?host=demo-node&service=co2", nil) req3.Header.Set("If-None-Match", etag) resp3, err := client.Do(req3) if err != nil { t.Fatalf("conditional GET failed: %v", err) } _ = resp3.Body.Close() if resp3.StatusCode != http.StatusNotModified { t.Errorf("expected 304 Not Modified, got %d", resp3.StatusCode) } // 4. Query for unapproved host: should return 403 Forbidden without calling upstream resp4, err := client.Get(baseURL + "/api/v1/query?host=secret-laptop&service=co2") if err != nil { t.Fatalf("forbidden query failed: %v", err) } _ = resp4.Body.Close() if resp4.StatusCode != http.StatusForbidden { t.Errorf("expected 403 Forbidden, got %d", resp4.StatusCode) } if upstreamCalls.Load() != 1 { t.Errorf("expected upstream calls to stay 1 after 403, got %d", upstreamCalls.Load()) } // 5. Query stats endpoint respStats, err := client.Get(baseURL + "/api/v1/stats") if err != nil { t.Fatalf("stats query failed: %v", err) } var stats map[string]any _ = json.NewDecoder(respStats.Body).Decode(&stats) _ = respStats.Body.Close() if stats["cache_hits"].(float64) < 1 { t.Errorf("expected at least 1 cache hit, got %v", stats["cache_hits"]) } if stats["blocked_queries"].(float64) != 1 { t.Errorf("expected 1 blocked query, got %v", stats["blocked_queries"]) } } func TestServer_RateLimiting(t *testing.T) { upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte("[]")) })) defer upstream.Close() cfg := DefaultConfig() cfg.BackendURL = upstream.URL cfg.ListenAddr = "127.0.0.1:0" cfg.RateLimitRPS = 1 cfg.RateLimitBurst = 2 cfg.EnableSnapshot = false proxyServer, err := NewServer(cfg) if err != nil { t.Fatalf("NewServer failed: %v", err) } lp := testListenerProvider{} if err := proxyServer.Start(lp); err != nil { t.Fatalf("Start failed: %v", err) } defer func() { _ = proxyServer.Stop(context.Background()) }() baseURL := fmt.Sprintf("http://%s", proxyServer.Addr().String()) client := &http.Client{Timeout: 5 * time.Second} // First two requests should consume burst capacity for i := range 2 { resp, err := client.Get(baseURL + "/api/v1/query?host=demo&service=load") if err != nil { t.Fatalf("req %d failed: %v", i, err) } _ = resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Errorf("expected req %d to succeed, got %d", i, resp.StatusCode) } } // Third request immediately after should be rate limited (429) resp3, err := client.Get(baseURL + "/api/v1/query?host=demo&service=load") if err != nil { t.Fatalf("req 3 failed: %v", err) } _ = resp3.Body.Close() if resp3.StatusCode != http.StatusTooManyRequests { t.Errorf("expected 429 Too Many Requests, got %d", resp3.StatusCode) } }