diff --git a/ipn/ipnlocal/loadmetrics_test.go b/ipn/ipnlocal/loadmetrics_test.go new file mode 100644 index 000000000..158632d77 --- /dev/null +++ b/ipn/ipnlocal/loadmetrics_test.go @@ -0,0 +1,106 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package ipnlocal + +import ( + "runtime" + "strings" + "testing" + + "tailscale.com/ipn" + "tailscale.com/types/opt" +) + +func TestCheckCollectLoadMetrics(t *testing.T) { + tests := []struct { + name string + prefs *ipn.Prefs + wantErr string // substring; "" means the prefs must be accepted + }{ + { + name: "unset is always fine", + prefs: &ipn.Prefs{}, + }, + { + name: "explicitly disabled is always fine", + prefs: &ipn.Prefs{CollectLoadMetrics: opt.NewBool(false)}, + }, + { + name: "disabled on an App Connector is fine", + prefs: &ipn.Prefs{ + CollectLoadMetrics: opt.NewBool(false), + AppConnector: ipn.AppConnectorPrefs{Advertise: true}, + }, + }, + { + name: "enabled on an App Connector is rejected", + prefs: &ipn.Prefs{ + CollectLoadMetrics: opt.NewBool(true), + AppConnector: ipn.AppConnectorPrefs{Advertise: true}, + }, + wantErr: "not supported on App Connectors", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := checkCollectLoadMetrics(tt.prefs) + if tt.wantErr == "" { + if err != nil { + t.Fatalf("checkCollectLoadMetrics() = %v; want nil", err) + } + return + } + if err == nil { + t.Fatalf("checkCollectLoadMetrics() = nil; want error containing %q", tt.wantErr) + } + if !strings.Contains(err.Error(), tt.wantErr) { + t.Errorf("checkCollectLoadMetrics() = %v; want error containing %q", err, tt.wantErr) + } + }) + } +} + +// TestCheckCollectLoadMetricsPlatform covers the platform gate: the counting +// hook is only wired up on Linux, so accepting the pref elsewhere would leave +// it silently inert. +func TestCheckCollectLoadMetricsPlatform(t *testing.T) { + p := &ipn.Prefs{CollectLoadMetrics: opt.NewBool(true)} + err := checkCollectLoadMetrics(p) + if runtime.GOOS == "linux" { + if err != nil { + t.Fatalf("checkCollectLoadMetrics() on linux = %v; want nil", err) + } + return + } + if err == nil { + t.Fatalf("checkCollectLoadMetrics() on %s = nil; want an error", runtime.GOOS) + } + if !strings.Contains(err.Error(), "Linux") { + t.Errorf("checkCollectLoadMetrics() on %s = %v; want the error to mention Linux", runtime.GOOS, err) + } +} + +// TestCheckPrefsRejectsLoadMetricsOnAppConnector verifies the check is actually +// reached through checkPrefsLocked, which is the shared gate for tailscale set, +// tailscale up, and config-file loads. +func TestCheckPrefsRejectsLoadMetricsOnAppConnector(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skipf("pref is Linux-only; running on %s", runtime.GOOS) + } + b := newTestLocalBackend(t) + + p := &ipn.Prefs{ + ControlURL: ipn.DefaultControlURL, + CollectLoadMetrics: opt.NewBool(true), + AppConnector: ipn.AppConnectorPrefs{Advertise: true}, + } + err := b.CheckPrefs(p) + if err == nil { + t.Fatal("CheckPrefs accepted load metrics on an App Connector; want an error") + } + if !strings.Contains(err.Error(), "not supported on App Connectors") { + t.Errorf("CheckPrefs error = %v; want it to mention App Connectors", err) + } +} diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 5170b9ee7..7f08662fc 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -4971,6 +4971,9 @@ func (b *LocalBackend) checkPrefsLocked(p *ipn.Prefs) error { if err := checkAdvertiseRoutes(p); err != nil { errs = append(errs, err) } + if err := checkCollectLoadMetrics(p); err != nil { + errs = append(errs, err) + } return errors.Join(errs...) } @@ -5080,6 +5083,26 @@ func checkAdvertiseRoutes(p *ipn.Prefs) error { return errors.Join(errs...) } +// checkCollectLoadMetrics validates the CollectLoadMetrics pref. +// +// Per-route load metrics are rejected on App Connectors: those advertise a +// route per resolved DNS address, so the number of series would grow with +// end-user browsing rather than with admin configuration. They are also +// rejected off Linux, where nothing on the packet path honors the pref, so +// accepting it would leave it silently inert. +func checkCollectLoadMetrics(p *ipn.Prefs) error { + if !p.CollectLoadMetrics.EqualBool(true) { + return nil + } + if buildfeatures.HasAppConnectors && p.AppConnector.Advertise { + return errors.New("load metrics are not supported on App Connectors: this node advertises routes dynamically per resolved DNS address, which would produce unbounded metric cardinality. Support for App Connectors is planned; for now use network flow logs for per-destination visibility on this node") + } + if runtime.GOOS != "linux" { + return fmt.Errorf("load metrics are only supported on Linux, not %s", runtime.GOOS) + } + return nil +} + // SetUseExitNodeEnabled turns on or off the most recently selected exit node. // // On success, it returns the resulting prefs (or current prefs, in the case of no change).