## Summary Implements DEVEX-664: the coder CLI commands that open a workspace connection now generate a client session ID and attach it to their requests, logs, and existing network telemetry, per the connection-log RFC. Commands opt in explicitly with a `client_session_id` annotation. The opted-in set is `coder ssh`, `coder start`, `coder stop`, and `coder update`. Long-running daemon commands (`coder server`, `agent`, `provisionerd`, and so on) deliberately stay out, so they never carry a meaningless session ID in their logs, request baggage, or telemetry. - **Generate / resolve**: a 16-byte lowercase-hex session ID is generated per opted-in invocation. When `CODER_TRACE_SESSION_ID` is set (so Toolbox and the VS Code plugin can correlate the CLI they spawn), that value is used verbatim; a warning is logged if it is not the canonical 32-char lowercase hex form, since coderd/agent middleware drop non-canonical values. - **Requests**: the ID is attached to every request as `client_session_id` W3C baggage via a transport wrapper, independent of whether tracing is enabled. The already-merged coderd tracing middleware (#27671) reads it. - **Coordinate websocket**: the ID is also set explicitly as baggage on the tailnet coordinate handshake in `workspacesdk.DialAgent`, so coderd's `/api/v2/workspaceagents/{id}/coordinate` endpoint logs it (it does not rely on the CLI transport for that dial). - **Logs**: the ID is added to the command's log context. - **Existing telemetry**: the ID is added to tailnet `TelemetryEvent` (`client_session_id`, new field 20; proto minor version bumped) and plumbed from the CLI through `workspacesdk.DialAgent`. Scope is the coder CLI client only (RFC glossary + requirement 5), and within the CLI only the opted-in workspace-connecting commands above. This PR is independent of #28039 (the agent middleware that consumes the baggage); it depends only on `tracing.SessionIDBaggageKey`, which is already on `main` via #27671. ## Out of scope RFC requirements 7-16 (connection-state change logging, `connection_logs` columns, log buffering, routing/disconnect-reason logs, coordination-protocol plumbing, `agentssh`/`reconnectingpty` command logging, support-bundle log capture) and requirements 4.2/4.3 (VS Code telemetry, Prometheus stats) are separate tickets. IDE plugins, Toolbox, and the daemon commands are also out of scope. ## Testing - `go test ./coderd/tracing/... ./cli/... ./tailnet/... ./codersdk/workspacesdk/...` (unit tests for the generator, the env/generate resolver, per-command opt-in, the baggage transport wrapper, the coordinate-dial baggage helper, and the telemetry field). - `go build ./...`, `go vet`, and `golangci-lint` pass on the touched packages. - `make tailnet/proto/tailnet.pb.go` regenerated the proto; the proto minor version was bumped and `postStartup` now connects at the current agent API version so `TestWorkspaceAgent_Startup/OK` tracks it. > Note: the pre-commit `enterprise/cli/.gen-golden` step could not run in this environment (no Postgres: `open postgres connection: EOF`). `cli/testdata/.gen-golden` passed; this change adds no serpent flags, so there is no CLI help/golden drift. <details> <summary>Implementation plan</summary> ### Scope mapping (RFC -> DEVEX-664) In scope (the CLI workspace-connecting subset of RFC phase 2): - Req 1: generate a 16-byte session ID as 32-char lowercase hex. - Req 2: add the session ID to every log the command makes for the session. - Req 3: attach the session ID to every API request via `client_session_id` baggage, including the coordinate websocket handshake. (The query-param fallback is browser-only; not applicable to the CLI.) - Req 4.1: attach the session ID to CLI network telemetry. - Req 5 / 5.3: support `CODER_TRACE_SESSION_ID`; when set, use it instead of generating. ### Resolved decisions - Scope: opt-in per command via a `client_session_id` annotation; opted-in set is `coder ssh`, `coder start`, `coder stop`, `coder update`. Daemon commands are excluded. - `CODER_TRACE_SESSION_ID`: when set, use the value verbatim; warn if it is not canonical 32-char lowercase hex. When unset, generate a fresh ID. - Existing telemetry = CLI network telemetry (tailnet `TelemetryEvent`); requires a new proto field + plumbing and a proto minor version bump. - Coordinate endpoint: set the session ID as baggage on the coordinate handshake explicitly in `DialAgent` (header, not query param) so it is logged server-side without relying on the CLI transport. - Shared helper: add `tracing.NewSessionID()` and export the validator as `tracing.ValidSessionID` for a single source of truth. ### Implementation 1. `coderd/tracing`: add `NewSessionID()` (16 random bytes, hex-encoded); export `ValidSessionID`. 2. `cli`: resolve the ID from `CODER_TRACE_SESSION_ID` or generate; attach `client_session_id` baggage to every request via `wrapTransportWithSessionIDHeader`, gated on the `annotationClientSessionID` opt-in; add it to the command log context. 3. `tailnet` + `codersdk/workspacesdk`: add `client_session_id` to `TelemetryEvent`, an `Options.ClientSessionID`/`Conn.clientSessionID` set in `newTelemetryEvent`, a `DialAgentOptions.ClientSessionID` plumbed from the opted-in commands, and the coordinate-handshake baggage in `DialAgent`. </details> --- _This PR was created by Coder Agents on behalf of @aqandrew._
246 lines
6.0 KiB
Go
246 lines
6.0 KiB
Go
package tailnet
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"tailscale.com/tailcfg"
|
|
"tailscale.com/types/netmap"
|
|
|
|
"github.com/coder/coder/v2/tailnet/proto"
|
|
)
|
|
|
|
func TestTelemetryStore(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("CreateEvent", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
remotePrefix := TailscaleServicePrefix.RandomPrefix()
|
|
remoteIP := remotePrefix.Addr()
|
|
application := "test"
|
|
|
|
nm := &netmap.NetworkMap{
|
|
SelfNode: &tailcfg.Node{
|
|
ID: 0,
|
|
DERP: "127.3.3.40:999",
|
|
},
|
|
Peers: []*tailcfg.Node{
|
|
{
|
|
ID: 1,
|
|
Addresses: []netip.Prefix{
|
|
TailscaleServicePrefix.RandomPrefix(),
|
|
TailscaleServicePrefix.RandomPrefix(),
|
|
},
|
|
},
|
|
{
|
|
ID: 2,
|
|
Addresses: []netip.Prefix{
|
|
remotePrefix,
|
|
TailscaleServicePrefix.RandomPrefix(),
|
|
TailscaleServicePrefix.RandomPrefix(),
|
|
},
|
|
},
|
|
},
|
|
DERPMap: &tailcfg.DERPMap{
|
|
HomeParams: &tailcfg.DERPHomeParams{
|
|
RegionScore: map[int]float64{
|
|
999: 1.0,
|
|
},
|
|
},
|
|
Regions: map[int]*tailcfg.DERPRegion{
|
|
999: {
|
|
RegionID: 999,
|
|
RegionCode: "zzz",
|
|
RegionName: "Cool Region",
|
|
EmbeddedRelay: true,
|
|
Avoid: false,
|
|
},
|
|
},
|
|
OmitDefaultRegions: false,
|
|
},
|
|
}
|
|
|
|
telemetry, err := newTelemetryStore()
|
|
require.NoError(t, err)
|
|
telemetry.markConnected(&remoteIP, application)
|
|
telemetry.updateNetworkMap(nm)
|
|
e := telemetry.newEvent()
|
|
// DERPMapToProto already tested
|
|
require.Equal(t, DERPMapToProto(nm.DERPMap), e.DerpMap)
|
|
// #nosec G115 - Safe conversion in test code as node IDs are within uint64 range
|
|
require.Equal(t, uint64(nm.Peers[1].ID), e.NodeIdRemote)
|
|
// #nosec G115 - Safe conversion in test code as node IDs are within uint64 range
|
|
require.Equal(t, uint64(nm.SelfNode.ID), e.NodeIdSelf)
|
|
require.Equal(t, application, e.Application)
|
|
require.Equal(t, nm.SelfNode.DERP, fmt.Sprintf("127.3.3.40:%d", e.HomeDerp))
|
|
})
|
|
|
|
t.Run("CleanIPs", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
ipv4 string
|
|
ipv6 string
|
|
expectedVersion int32
|
|
expectedClass proto.IPFields_IPClass
|
|
}{
|
|
{
|
|
name: "Public",
|
|
ipv4: "142.250.71.78",
|
|
ipv6: "2404:6800:4006:812::200e",
|
|
expectedClass: proto.IPFields_PUBLIC,
|
|
},
|
|
{
|
|
name: "Private",
|
|
ipv4: "192.168.0.1",
|
|
ipv6: "fd12:3456:789a:1::1",
|
|
expectedClass: proto.IPFields_PRIVATE,
|
|
},
|
|
{
|
|
name: "LinkLocal",
|
|
ipv4: "169.254.1.1",
|
|
ipv6: "fe80::1",
|
|
expectedClass: proto.IPFields_LINK_LOCAL,
|
|
},
|
|
{
|
|
name: "Loopback",
|
|
ipv4: "127.0.0.1",
|
|
ipv6: "::1",
|
|
expectedClass: proto.IPFields_LOOPBACK,
|
|
},
|
|
{
|
|
name: "IPv4Mapped",
|
|
ipv4: "1.2.3.4",
|
|
ipv6: "::ffff:1.2.3.4",
|
|
expectedClass: proto.IPFields_PUBLIC,
|
|
},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
telemetry, err := newTelemetryStore()
|
|
require.NoError(t, err)
|
|
|
|
telemetry.setNetInfo(&tailcfg.NetInfo{
|
|
GlobalV4: c.ipv4,
|
|
GlobalV6: c.ipv6,
|
|
})
|
|
|
|
event := telemetry.newEvent()
|
|
|
|
v4hash := telemetry.hashAddrorHostname(c.ipv4)
|
|
require.Equal(t, &proto.Netcheck_NetcheckIP{
|
|
Hash: v4hash,
|
|
Fields: &proto.IPFields{
|
|
Version: 4,
|
|
Class: c.expectedClass,
|
|
},
|
|
}, event.LatestNetcheck.GlobalV4)
|
|
|
|
v6hash := telemetry.hashAddrorHostname(c.ipv6)
|
|
require.Equal(t, &proto.Netcheck_NetcheckIP{
|
|
Hash: v6hash,
|
|
Fields: &proto.IPFields{
|
|
Version: 6,
|
|
Class: c.expectedClass,
|
|
},
|
|
}, event.LatestNetcheck.GlobalV6)
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("DerpMapClean", func(t *testing.T) {
|
|
t.Parallel()
|
|
telemetry, err := newTelemetryStore()
|
|
require.NoError(t, err)
|
|
|
|
derpMap := &tailcfg.DERPMap{
|
|
Regions: make(map[int]*tailcfg.DERPRegion),
|
|
}
|
|
derpMap.Regions[998] = &tailcfg.DERPRegion{
|
|
RegionID: 998,
|
|
EmbeddedRelay: true,
|
|
RegionCode: "zzz",
|
|
RegionName: "Cool Region",
|
|
Avoid: true,
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
{
|
|
Name: "zzz1",
|
|
RegionID: 998,
|
|
HostName: "coolderp.com",
|
|
CertName: "coolderpcert",
|
|
IPv4: "1.2.3.4",
|
|
IPv6: "2001:db8::1",
|
|
STUNTestIP: "5.6.7.8",
|
|
},
|
|
},
|
|
}
|
|
derpMap.Regions[999] = &tailcfg.DERPRegion{
|
|
RegionID: 999,
|
|
EmbeddedRelay: true,
|
|
RegionCode: "zzo",
|
|
RegionName: "Other Cool Region",
|
|
Avoid: true,
|
|
Nodes: []*tailcfg.DERPNode{
|
|
{
|
|
Name: "zzo1",
|
|
HostName: "coolderp.com",
|
|
CertName: "coolderpcert",
|
|
IPv4: "1.2.3.4",
|
|
IPv6: "2001:db8::1",
|
|
STUNTestIP: "5.6.7.8",
|
|
},
|
|
},
|
|
}
|
|
telemetry.updateDerpMap(derpMap)
|
|
|
|
event := telemetry.newEvent()
|
|
require.Len(t, event.DerpMap.Regions[999].Nodes, 1)
|
|
node := event.DerpMap.Regions[999].Nodes[0]
|
|
require.NotContains(t, node.HostName, "coolderp.com")
|
|
require.NotContains(t, node.Ipv4, "1.2.3.4")
|
|
require.NotContains(t, node.Ipv6, "2001:db8::1")
|
|
require.NotContains(t, node.StunTestIp, "5.6.7.8")
|
|
otherNode := event.DerpMap.Regions[998].Nodes[0]
|
|
require.Equal(t, otherNode.HostName, node.HostName)
|
|
require.Equal(t, otherNode.Ipv4, node.Ipv4)
|
|
require.Equal(t, otherNode.Ipv6, node.Ipv6)
|
|
require.Equal(t, otherNode.StunTestIp, node.StunTestIp)
|
|
})
|
|
}
|
|
|
|
func TestConn_newTelemetryEvent_ClientSessionID(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
store, err := newTelemetryStore()
|
|
require.NoError(t, err)
|
|
|
|
t.Run("Set", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const sessionID = "0123456789abcdef0123456789abcdef"
|
|
c := &Conn{
|
|
telemetryStore: store,
|
|
clientType: proto.TelemetryEvent_CLI,
|
|
clientSessionID: sessionID,
|
|
}
|
|
require.Equal(t, sessionID, c.newTelemetryEvent().ClientSessionId)
|
|
})
|
|
|
|
t.Run("Empty", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
c := &Conn{
|
|
telemetryStore: store,
|
|
clientType: proto.TelemetryEvent_CLI,
|
|
}
|
|
require.Empty(t, c.newTelemetryEvent().ClientSessionId)
|
|
})
|
|
}
|