mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
Adds an opt-in, in-memory aggregator of recent connection-rejection events (TSMP rejects received from peers, outbound TSMP rejects we emit on ACL-blocked inbound flows, and pendopen timeouts) keyed by (direction, proto, peer-address, reason). The aggregated data is exposed over a new debug-rejects LocalAPI endpoint and a GET /debug/rejects c2n endpoint, intended for future GUI/CLI consumption when diagnosing why a connection failed. Architecture: - net/connreject holds the data types and a per-LocalBackend Aggregator (LRU-bounded, default 256 entries on desktop / 32 on mobile, per direction). - feature/connreject is a self-registering ipnext.Extension that owns one Aggregator per LocalBackend, installs note callbacks on the tundev and engine, subscribes to OnSelfChange to flip the runtime gate, and serves the LocalAPI/c2n endpoints. - wgengine.Engine and *tstun.Wrapper each gain a SetConnRejectNote setter; data-plane sites use a single atomic.Pointer load + nil check, so the cost when no consumer is installed is one MOV. Gating: - Compile-time: ts_omit_connreject build tag (standard feature/buildfeatures + condregister plumbing). Trims ~41 KB. - Runtime: nodecap.ConnReject node attribute, off by default at the control plane. May be removed once the feature is enabled by default. Updates CapabilityVersion to 146 (clients understand nodecap.ConnReject and can serve GET /debug/rejects). Adds Proto/Src/Dst accessors on flowtrack.Tuple (used by pendopen to construct events without exposing the tuple's internals to the aggregator). Updates #1094 Updates #14802 Change-Id: I83e8f24a7e66fa2d158d128bd25fbe851134941b Signed-off-by: James Tucker <[email protected]>
155 lines
4.0 KiB
Go
155 lines
4.0 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package connreject
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"tailscale.com/net/connreject"
|
|
"tailscale.com/tailcfg"
|
|
"tailscale.com/tailcfg/nodecap"
|
|
"tailscale.com/types/ipproto"
|
|
"tailscale.com/types/logger"
|
|
)
|
|
|
|
// newTestExtension returns a fresh extension backed by an isolated
|
|
// Aggregator (no SafeBackend wiring), suitable for per-test use in
|
|
// parallel.
|
|
func newTestExtension() *extension {
|
|
return &extension{
|
|
logf: logger.Discard,
|
|
agg: connreject.NewAggregator(8),
|
|
}
|
|
}
|
|
|
|
func sampleOutgoingEvent() connreject.Event {
|
|
return connreject.Event{
|
|
Direction: connreject.Outgoing,
|
|
Proto: ipproto.TCP,
|
|
Src: netip.MustParseAddrPort("100.0.0.1:1001"),
|
|
Dst: netip.MustParseAddrPort("100.0.0.2:443"),
|
|
Reason: connreject.ReasonACL,
|
|
Source: connreject.SourceTSMPRecv,
|
|
}
|
|
}
|
|
|
|
func sampleIncomingEvent() connreject.Event {
|
|
return connreject.Event{
|
|
Direction: connreject.Incoming,
|
|
Proto: ipproto.TCP,
|
|
Src: netip.MustParseAddrPort("100.0.0.2:5555"),
|
|
Dst: netip.MustParseAddrPort("100.0.0.1:22"),
|
|
Reason: connreject.ReasonACL,
|
|
Source: connreject.SourceTSMPSent,
|
|
}
|
|
}
|
|
|
|
// TestOnEventDeliversToAggregator verifies that events reaching the
|
|
// extension's callback are forwarded to the aggregator, which then
|
|
// partitions by direction into its own buffers.
|
|
func TestOnEventDeliversToAggregator(t *testing.T) {
|
|
t.Parallel()
|
|
e := newTestExtension()
|
|
e.agg.SetEnabled(true)
|
|
|
|
e.note(sampleOutgoingEvent())
|
|
e.note(sampleIncomingEvent())
|
|
|
|
if got := len(e.agg.Outgoing()); got != 1 {
|
|
t.Errorf("outgoing len = %d, want 1", got)
|
|
}
|
|
if got := len(e.agg.Incoming()); got != 1 {
|
|
t.Errorf("incoming len = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func TestOnEventDroppedWhenDisabled(t *testing.T) {
|
|
t.Parallel()
|
|
e := newTestExtension()
|
|
|
|
e.note(sampleOutgoingEvent())
|
|
e.note(sampleIncomingEvent())
|
|
|
|
if got := len(e.agg.Outgoing()); got != 0 {
|
|
t.Errorf("outgoing len = %d, want 0 when disabled", got)
|
|
}
|
|
if got := len(e.agg.Incoming()); got != 0 {
|
|
t.Errorf("incoming len = %d, want 0 when disabled", got)
|
|
}
|
|
}
|
|
|
|
func TestExtensionOnSelfChange(t *testing.T) {
|
|
t.Parallel()
|
|
e := newTestExtension()
|
|
|
|
// NodeView without the cap: stays off.
|
|
noCap := (&tailcfg.Node{}).View()
|
|
e.onSelfChange(noCap)
|
|
if e.agg.Enabled() {
|
|
t.Error("enabled = true after self-change with no cap, want false")
|
|
}
|
|
|
|
// NodeView with the cap: turns on.
|
|
withCap := (&tailcfg.Node{
|
|
CapMap: tailcfg.NodeCapMap{
|
|
nodecap.ConnReject: nil,
|
|
},
|
|
}).View()
|
|
e.onSelfChange(withCap)
|
|
if !e.agg.Enabled() {
|
|
t.Error("enabled = false after self-change with cap, want true")
|
|
}
|
|
|
|
// Flipping back off works.
|
|
e.onSelfChange(noCap)
|
|
if e.agg.Enabled() {
|
|
t.Error("enabled = true after flip back, want false")
|
|
}
|
|
}
|
|
|
|
func TestExtensionShutdownDisables(t *testing.T) {
|
|
t.Parallel()
|
|
e := newTestExtension()
|
|
e.agg.SetEnabled(true)
|
|
|
|
if err := e.Shutdown(); err != nil {
|
|
t.Fatalf("Shutdown error: %v", err)
|
|
}
|
|
if e.agg.Enabled() {
|
|
t.Error("enabled still true after Shutdown")
|
|
}
|
|
}
|
|
|
|
// TestBuildResponse covers the JSON-payload shape served by both the
|
|
// LocalAPI and c2n handlers. The handlers themselves are pure routing
|
|
// boilerplate (method check, extension lookup, JSON marshal) and are
|
|
// not exercised here; verifying buildResponse plus the
|
|
// FindMatchingExtension contract on LocalBackend is sufficient.
|
|
func TestBuildResponse(t *testing.T) {
|
|
t.Parallel()
|
|
e := newTestExtension()
|
|
e.agg.SetEnabled(true)
|
|
e.note(sampleOutgoingEvent())
|
|
e.note(sampleIncomingEvent())
|
|
|
|
resp := buildResponse(e.agg)
|
|
if !resp.Enabled {
|
|
t.Error("Enabled = false, want true")
|
|
}
|
|
if got := len(resp.Outgoing); got != 1 {
|
|
t.Errorf("len(Outgoing) = %d, want 1", got)
|
|
}
|
|
if got := len(resp.Incoming); got != 1 {
|
|
t.Errorf("len(Incoming) = %d, want 1", got)
|
|
}
|
|
|
|
// Disabled aggregator yields empty slices and Enabled=false.
|
|
e.agg.SetEnabled(false)
|
|
resp = buildResponse(e.agg)
|
|
if resp.Enabled {
|
|
t.Error("Enabled = true after SetEnabled(false), want false")
|
|
}
|
|
}
|