mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-04 23:52:20 -08:00
derp/derpserver: report TLS PQ metrics for DERP and WebSocket connections
Track accepted and currently active DERP connections in Server.ExpVar by
transport ("http" vs "websocket"), post-quantum status ("true", "false",
or "none" when r.TLS is nil), and negotiated TLS key exchange group:
- derp_tls_accepts{transport,pq,group}
- derp_current_tls_connections{transport,pq,group}
Both derpserver.Handler and derpserver.AddWebSocketSupport inspect r.TLS
before handing off to Server.Accept (without retaining *http.Request on
hijacked goroutines) and decrement the active gauge when Accept returns.
Updates tailscale/corp#52720
Change-Id: I32bdb549c1218654df1f86e91b0217c5a169c566
Signed-off-by: James Tucker <[email protected]>
This commit is contained in:
1 parent
aff6d18f39
commit
b08899f18c
5 files changed
+199
-3
No files matched your search
@@ -115,7 +115,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
|
||||
tailscale.com/net/stun from tailscale.com/net/stunserver
|
||||
tailscale.com/net/stunserver from tailscale.com/cmd/derper
|
||||
L tailscale.com/net/tcpinfo from tailscale.com/derp/derpserver
|
||||
tailscale.com/net/tlsdial from tailscale.com/derp/derphttp
|
||||
tailscale.com/net/tlsdial from tailscale.com/derp/derphttp+
|
||||
tailscale.com/net/tlsdial/blockblame from tailscale.com/net/tlsdial
|
||||
tailscale.com/net/traffic from tailscale.com/net/routecheck
|
||||
tailscale.com/net/tsaddr from tailscale.com/ipn+
|
||||
|
||||
@@ -48,6 +48,7 @@ import (
|
||||
"tailscale.com/disco"
|
||||
"tailscale.com/envknob"
|
||||
"tailscale.com/metrics"
|
||||
"tailscale.com/net/tlsdial"
|
||||
"tailscale.com/syncs"
|
||||
"tailscale.com/tailcfg"
|
||||
"tailscale.com/tstime"
|
||||
@@ -172,6 +173,8 @@ type Server struct {
|
||||
gotPing expvar.Int // number of ping frames from client
|
||||
sentPong expvar.Int // number of pong frames enqueued to client
|
||||
accepts expvar.Int
|
||||
tlsAccepts *metrics.MultiLabelMap[tlsConnLabels]
|
||||
curTLSConns *metrics.MultiLabelMap[tlsConnLabels]
|
||||
curClients expvar.Int
|
||||
curClientsNotIdeal expvar.Int
|
||||
curHomeClients expvar.Int // ones with preferred
|
||||
@@ -416,6 +419,31 @@ var bytesDropped = metrics.NewMultiLabelMap[dropReasonKindLabels](
|
||||
"DERP bytes dropped by reason and by kind",
|
||||
)
|
||||
|
||||
type tlsConnLabels struct {
|
||||
Transport string // "http" or "websocket"
|
||||
PQ string // "true", "false", or "none" (non-TLS)
|
||||
Group string // tlsdial.CurveName(CurveID), or "none" (non-TLS)
|
||||
}
|
||||
|
||||
func tlsConnLabelsFor(transport string, cs *tls.ConnectionState) tlsConnLabels {
|
||||
if cs == nil {
|
||||
return tlsConnLabels{
|
||||
Transport: transport,
|
||||
PQ: "none",
|
||||
Group: "none",
|
||||
}
|
||||
}
|
||||
pq := "false"
|
||||
if tlsdial.IsPQCurve(cs.CurveID) {
|
||||
pq = "true"
|
||||
}
|
||||
return tlsConnLabels{
|
||||
Transport: transport,
|
||||
PQ: pq,
|
||||
Group: tlsdial.CurveName(cs.CurveID),
|
||||
}
|
||||
}
|
||||
|
||||
// New returns a new DERP server. It doesn't listen on its own.
|
||||
// Connections are given to it via Server.Accept.
|
||||
func New(privateKey key.NodePrivate, logf logger.Logf) *Server {
|
||||
@@ -429,6 +457,8 @@ func New(privateKey key.NodePrivate, logf logger.Logf) *Server {
|
||||
logf: logf,
|
||||
limitedLogf: logger.RateLimitedFn(logf, 30*time.Second, 5, 100),
|
||||
packetsRecvByKind: metrics.LabelMap{Label: "kind"},
|
||||
tlsAccepts: &metrics.MultiLabelMap[tlsConnLabels]{Type: "counter", Help: "DERP connections accepted by transport, post-quantum status, and TLS key exchange group"},
|
||||
curTLSConns: &metrics.MultiLabelMap[tlsConnLabels]{Type: "gauge", Help: "Current active DERP connections by transport, post-quantum status, and TLS key exchange group"},
|
||||
clientsMesh: map[key.NodePublic]PacketForwarder{},
|
||||
netConns: map[derp.Conn]chan struct{}{},
|
||||
memSys0: ms.Sys,
|
||||
@@ -3172,6 +3202,7 @@ func (s *Server) ExpVar(rateLimitEnabled bool) expvar.Var {
|
||||
m.Set("gauge_watchers", s.expVarFunc(func() any { return len(s.watchers) }))
|
||||
m.Set("gauge_current_file_descriptors", expvar.Func(func() any { return metrics.CurrentFDs() }))
|
||||
m.Set("gauge_current_connections", &s.curClients)
|
||||
m.Set("gauge_current_tls_connections", s.curTLSConns)
|
||||
m.Set("gauge_current_home_connections", &s.curHomeClients)
|
||||
m.Set("gauge_current_notideal_connections", &s.curClientsNotIdeal)
|
||||
m.Set("gauge_clients_total", s.expVarFunc(func() any { return len(s.clientsMesh) }))
|
||||
@@ -3181,6 +3212,7 @@ func (s *Server) ExpVar(rateLimitEnabled bool) expvar.Var {
|
||||
m.Set("gauge_current_dup_client_conns", &s.dupClientConns)
|
||||
m.Set("counter_total_dup_client_conns", &s.dupClientConnTotal)
|
||||
m.Set("accepts", &s.accepts)
|
||||
m.Set("counter_tls_accepts", s.tlsAccepts)
|
||||
m.Set("bytes_received", &s.bytesRecv)
|
||||
m.Set("bytes_sent", &s.bytesSent)
|
||||
m.Set("counter_packets_received_kind", &s.packetsRecvByKind)
|
||||
|
||||
@@ -105,7 +105,10 @@ func Handler(s *Server) http.Handler {
|
||||
// bufio buffers, this Request and its headers) can be garbage
|
||||
// collected instead of being pinned for the lifetime of the
|
||||
// connection.
|
||||
go serveHijackedConn(ctx, s, netConn, derpBRW)
|
||||
labels := tlsConnLabelsFor("http", r.TLS)
|
||||
s.tlsAccepts.Add(labels, 1)
|
||||
s.curTLSConns.Add(labels, 1)
|
||||
go serveHijackedConn(ctx, s, netConn, derpBRW, labels)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -114,7 +117,8 @@ func Handler(s *Server) http.Handler {
|
||||
// runs as its own goroutine, no longer under net/http's per-connection
|
||||
// panic recovery, so it recovers panics itself to keep one bad
|
||||
// connection from taking down the whole process.
|
||||
func serveHijackedConn(ctx context.Context, s *Server, netConn net.Conn, brw *bufio.ReadWriter) {
|
||||
func serveHijackedConn(ctx context.Context, s *Server, netConn net.Conn, brw *bufio.ReadWriter, labels tlsConnLabels) {
|
||||
defer s.curTLSConns.Add(labels, -1)
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
log.Printf("derp: panic serving %v: %v\n%s", netConn.RemoteAddr(), e, debug.Stack())
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"expvar"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -18,12 +19,15 @@ import (
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"tailscale.com/derp"
|
||||
"tailscale.com/derp/derphttp"
|
||||
"tailscale.com/net/memnet"
|
||||
"tailscale.com/net/netmon"
|
||||
"tailscale.com/net/wsconn"
|
||||
"tailscale.com/tstest"
|
||||
"tailscale.com/tstest/tlstest"
|
||||
"tailscale.com/tsweb/varz"
|
||||
"tailscale.com/types/key"
|
||||
)
|
||||
|
||||
@@ -194,3 +198,155 @@ func TestKeepAlive(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestHandlerTLSMetrics(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
websocket bool
|
||||
useTLS bool
|
||||
serverCurves []tls.CurveID
|
||||
wantLabels tlsConnLabels
|
||||
}{
|
||||
{
|
||||
name: "https_pq",
|
||||
useTLS: true,
|
||||
wantLabels: tlsConnLabels{Transport: "http", PQ: "true", Group: "X25519MLKEM768"},
|
||||
},
|
||||
{
|
||||
name: "https_classic",
|
||||
useTLS: true,
|
||||
serverCurves: []tls.CurveID{tls.X25519},
|
||||
wantLabels: tlsConnLabels{Transport: "http", PQ: "false", Group: "X25519"},
|
||||
},
|
||||
{
|
||||
name: "http",
|
||||
useTLS: false,
|
||||
wantLabels: tlsConnLabels{Transport: "http", PQ: "none", Group: "none"},
|
||||
},
|
||||
{
|
||||
name: "wss_pq",
|
||||
websocket: true,
|
||||
useTLS: true,
|
||||
wantLabels: tlsConnLabels{Transport: "websocket", PQ: "true", Group: "X25519MLKEM768"},
|
||||
},
|
||||
{
|
||||
name: "wss_classic",
|
||||
websocket: true,
|
||||
useTLS: true,
|
||||
serverCurves: []tls.CurveID{tls.X25519},
|
||||
wantLabels: tlsConnLabels{Transport: "websocket", PQ: "false", Group: "X25519"},
|
||||
},
|
||||
{
|
||||
name: "ws_http",
|
||||
websocket: true,
|
||||
useTLS: false,
|
||||
wantLabels: tlsConnLabels{Transport: "websocket", PQ: "none", Group: "none"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := New(key.NewNode(), t.Logf)
|
||||
defer s.Close()
|
||||
|
||||
hs := httptest.NewUnstartedServer(AddWebSocketSupport(s, Handler(s)))
|
||||
if tt.useTLS {
|
||||
if tt.serverCurves != nil {
|
||||
hs.TLS = &tls.Config{CurvePreferences: tt.serverCurves}
|
||||
}
|
||||
hs.StartTLS()
|
||||
} else {
|
||||
hs.Start()
|
||||
}
|
||||
defer hs.Close()
|
||||
|
||||
var closeClient func()
|
||||
if tt.websocket {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
hc := hs.Client()
|
||||
if tt.useTLS && tt.wantLabels.PQ == "true" {
|
||||
hc.Transport.(*http.Transport).TLSClientConfig.CurvePreferences = []tls.CurveID{tls.X25519MLKEM768}
|
||||
}
|
||||
wc, _, err := websocket.Dial(ctx, hs.URL+"/derp", &websocket.DialOptions{
|
||||
HTTPClient: hc,
|
||||
Subprotocols: []string{"derp"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("websocket.Dial: %v", err)
|
||||
}
|
||||
nc := wsconn.NetConn(context.Background(), wc, websocket.MessageBinary, hs.URL)
|
||||
brw := bufio.NewReadWriter(bufio.NewReader(nc), bufio.NewWriter(nc))
|
||||
dc, err := derp.NewClient(key.NewNode(), nc, brw, t.Logf)
|
||||
if err != nil {
|
||||
nc.Close()
|
||||
t.Fatalf("derp.NewClient: %v", err)
|
||||
}
|
||||
if m, err := dc.Recv(); err != nil {
|
||||
nc.Close()
|
||||
t.Fatalf("Recv: %v", err)
|
||||
} else if _, ok := m.(derp.ServerInfoMessage); !ok {
|
||||
nc.Close()
|
||||
t.Fatalf("first message = %T; want ServerInfoMessage", m)
|
||||
}
|
||||
closeClient = func() { nc.Close() }
|
||||
} else {
|
||||
c, err := derphttp.NewClient(key.NewNode(), hs.URL+"/derp", t.Logf, netmon.NewStatic())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if tt.useTLS {
|
||||
roots := x509.NewCertPool()
|
||||
roots.AddCert(hs.Certificate())
|
||||
tlsCfg := &tls.Config{RootCAs: roots}
|
||||
if tt.wantLabels.PQ == "true" {
|
||||
tlsCfg.CurvePreferences = []tls.CurveID{tls.X25519MLKEM768}
|
||||
}
|
||||
c.TLSConfig = tlsCfg
|
||||
}
|
||||
if err := c.Connect(context.Background()); err != nil {
|
||||
c.Close()
|
||||
t.Fatalf("Connect: %v", err)
|
||||
}
|
||||
if m, err := c.Recv(); err != nil {
|
||||
c.Close()
|
||||
t.Fatalf("Recv: %v", err)
|
||||
} else if _, ok := m.(derp.ServerInfoMessage); !ok {
|
||||
c.Close()
|
||||
t.Fatalf("first message = %T; want ServerInfoMessage", m)
|
||||
}
|
||||
closeClient = func() { c.Close() }
|
||||
}
|
||||
|
||||
gotAccepts := s.tlsAccepts.Get(tt.wantLabels)
|
||||
if gotAccepts == nil || gotAccepts.(*expvar.Int).Value() != 1 {
|
||||
t.Fatalf("tlsAccepts[%+v] = %v; want 1", tt.wantLabels, gotAccepts)
|
||||
}
|
||||
gotCur := s.curTLSConns.Get(tt.wantLabels)
|
||||
if gotCur == nil || gotCur.(*expvar.Int).Value() != 1 {
|
||||
t.Fatalf("curTLSConns[%+v] = %v; want 1", tt.wantLabels, gotCur)
|
||||
}
|
||||
|
||||
var prom bytes.Buffer
|
||||
varz.WritePrometheusExpvar(&prom, expvar.KeyValue{Key: "derp", Value: s.ExpVar(false)})
|
||||
wantAcceptLine := `derp_tls_accepts{transport="` + tt.wantLabels.Transport + `",pq="` + tt.wantLabels.PQ + `",group="` + tt.wantLabels.Group + `"} 1`
|
||||
wantCurLine := `derp_current_tls_connections{transport="` + tt.wantLabels.Transport + `",pq="` + tt.wantLabels.PQ + `",group="` + tt.wantLabels.Group + `"} 1`
|
||||
if !strings.Contains(prom.String(), wantAcceptLine) {
|
||||
t.Errorf("prometheus output missing %q:\n%s", wantAcceptLine, prom.String())
|
||||
}
|
||||
if !strings.Contains(prom.String(), wantCurLine) {
|
||||
t.Errorf("prometheus output missing %q:\n%s", wantCurLine, prom.String())
|
||||
}
|
||||
|
||||
closeClient()
|
||||
if err := tstest.WaitFor(5*time.Second, func() error {
|
||||
if v := s.curTLSConns.Get(tt.wantLabels).(*expvar.Int).Value(); v != 0 {
|
||||
return io.ErrShortBuffer
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Errorf("curTLSConns[%+v] = %v after close; want 0", tt.wantLabels, s.curTLSConns.Get(tt.wantLabels))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,10 @@ func AddWebSocketSupport(s *Server, base http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
counterWebSocketAccepts.Add(1)
|
||||
labels := tlsConnLabelsFor("websocket", r.TLS)
|
||||
s.tlsAccepts.Add(labels, 1)
|
||||
s.curTLSConns.Add(labels, 1)
|
||||
defer s.curTLSConns.Add(labels, -1)
|
||||
wc := wsconn.NetConn(r.Context(), c, websocket.MessageBinary, r.RemoteAddr)
|
||||
brw := bufio.NewReadWriter(bufio.NewReader(wc), bufio.NewWriter(wc))
|
||||
s.Accept(r.Context(), wc, brw, r.RemoteAddr)
|
||||
|
||||
Reference in new issue
Block a user