Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ endif
site/src/api/chatModelOptionsGenerated.json \
site/src/api/rbacresourcesGenerated.ts \
site/src/api/countriesGenerated.ts \
site/src/api/connectionTypesGenerated.ts \
site/src/theme/icons.json \
examples/examples.gen.json \
docs/manifest.json \
Expand Down Expand Up @@ -590,6 +591,7 @@ SITE_GEN_FILES := \
site/src/api/typesGenerated.ts \
site/src/api/rbacresourcesGenerated.ts \
site/src/api/countriesGenerated.ts \
site/src/api/connectionTypesGenerated.ts \
site/src/api/chatModelOptionsGenerated.json \
site/src/theme/icons.json

Expand Down Expand Up @@ -1127,6 +1129,7 @@ gen/mark-fresh:
codersdk/apikey_scopes_gen.go \
site/src/api/rbacresourcesGenerated.ts \
site/src/api/countriesGenerated.ts \
site/src/api/connectionTypesGenerated.ts \
site/src/api/chatModelOptionsGenerated.json \
docs/admin/integrations/prometheus.md \
docs/reference/cli/index.md \
Expand Down Expand Up @@ -1336,6 +1339,9 @@ site/src/api/rbacresourcesGenerated.ts: site/node_modules/.installed scripts/typ
site/src/api/countriesGenerated.ts: site/node_modules/.installed scripts/typegen/countries.tstmpl scripts/typegen/main.go codersdk/countries.go | _gen _gen/bin/typegen
$(call atomic_write,_gen/bin/typegen countries,./scripts/biome_format.sh)

site/src/api/connectionTypesGenerated.ts: site/node_modules/.installed scripts/typegen/connectiontypes.tstmpl scripts/typegen/main.go codersdk/connectionlog.go codersdk/appname.go | _gen _gen/bin/typegen
$(call atomic_write,_gen/bin/typegen connectiontypes,./scripts/biome_format.sh)

site/src/api/chatModelOptionsGenerated.json: scripts/modeloptionsgen/main.go codersdk/chats.go | _gen _gen/bin/modeloptionsgen
$(call atomic_write,_gen/bin/modeloptionsgen | tail -n +2,./scripts/biome_format.sh)

Expand Down
38 changes: 21 additions & 17 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,22 +412,9 @@ func (a *agent) init() {
BlockReversePortForwarding: a.blockReversePortForwarding,
BlockLocalPortForwarding: a.blockLocalPortForwarding,
ReportConnection: func(id uuid.UUID, report agentssh.ConnectionReport) func(code int, reason string) {
var connectionType proto.Connection_Type
// Connection_Type is a fixed enum, stored as a database enum in
// the connection log, so it can only hold a family.
switch codersdk.AppNameFamily(report.AppName) {
case codersdk.AppFamilySSH:
connectionType = proto.Connection_SSH
case codersdk.AppFamilyVSCode:
connectionType = proto.Connection_VSCODE
case codersdk.AppFamilyJetBrains:
connectionType = proto.Connection_JETBRAINS
default:
connectionType = proto.Connection_TYPE_UNSPECIFIED
}

return a.reportConnection(id, connectionReport{
connectionType: connectionType,
connectionType: sshConnectionType(report.AppName),
appName: report.AppName,
ip: report.IP,
clientSessionID: report.ClientSessionID,
})
Expand Down Expand Up @@ -513,6 +500,7 @@ func (a *agent) init() {
func(id uuid.UUID, ip string) func(code int, reason string) {
return a.reportConnection(id, connectionReport{
connectionType: proto.Connection_RECONNECTING_PTY,
appName: string(codersdk.AppFamilyReconnectingPTY),
ip: ip,
})
},
Expand Down Expand Up @@ -1043,10 +1031,24 @@ const (

type connectionReport struct {
connectionType proto.Connection_Type
appName string
ip string
clientSessionID string
}

// sshConnectionType maps appName onto the frozen enum for coderd without
// app_name.
func sshConnectionType(appName string) proto.Connection_Type {
switch codersdk.AppNameFamily(appName) {
case codersdk.AppFamilyVSCode:
return proto.Connection_VSCODE
case codersdk.AppFamilyJetBrains:
return proto.Connection_JETBRAINS
default:
return proto.Connection_SSH
}
}

func (a *agent) reportConnection(id uuid.UUID, report connectionReport) (disconnected func(code int, reason string)) {
// A blank IP can unfortunately happen if the connection is broken in a data race before we get to introspect it. We
// still report it, and the recipient can handle a blank IP.
Expand Down Expand Up @@ -1075,7 +1077,7 @@ func (a *agent) reportConnection(id uuid.UUID, report connectionReport) (disconn
a.logger.Warn(a.hardCtx, "connection report buffer limit reached, dropping connect",
slog.F("limit", reportConnectionBufferLimit),
slog.F("connection_id", id),
slog.F("connection_type", report.connectionType),
slog.F("app_name", report.appName),
slog.F("ip", ip),
slog.F("client_session_id", report.clientSessionID),
)
Expand All @@ -1085,6 +1087,7 @@ func (a *agent) reportConnection(id uuid.UUID, report connectionReport) (disconn
Id: id[:],
Action: proto.Connection_CONNECT,
Type: report.connectionType,
AppName: report.appName,
Timestamp: timestamppb.New(time.Now()),
Ip: ip,
StatusCode: 0,
Expand All @@ -1105,7 +1108,7 @@ func (a *agent) reportConnection(id uuid.UUID, report connectionReport) (disconn
a.logger.Warn(a.hardCtx, "connection report buffer limit reached, dropping disconnect",
slog.F("limit", reportConnectionBufferLimit),
slog.F("connection_id", id),
slog.F("connection_type", report.connectionType),
slog.F("app_name", report.appName),
slog.F("ip", ip),
slog.F("client_session_id", report.clientSessionID),
)
Expand All @@ -1117,6 +1120,7 @@ func (a *agent) reportConnection(id uuid.UUID, report connectionReport) (disconn
Id: id[:],
Action: proto.Connection_DISCONNECT,
Type: report.connectionType,
AppName: report.appName,
Timestamp: timestamppb.New(time.Now()),
Ip: ip,
StatusCode: int32(code), //nolint:gosec
Expand Down
26 changes: 25 additions & 1 deletion agent/agent_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func TestReportConnection(t *testing.T) {
clientSessionID: "0123456789abcdef0123456789abcdef",
},
},
{
name: "AppName",
report: connectionReport{
connectionType: proto.Connection_VSCODE,
appName: "cursor",
},
},
}

for _, tc := range tests {
Expand All @@ -71,6 +78,7 @@ func TestReportConnection(t *testing.T) {
require.Equal(t, connID[:], req0.GetConnection().GetId())
require.Equal(t, proto.Connection_CONNECT, req0.GetConnection().GetAction())
require.Equal(t, tc.report.clientSessionID, req0.GetConnection().GetClientSessionId())
require.Equal(t, tc.report.appName, req0.GetConnection().GetAppName())

disconnected(0, "because")
require.Len(t, uut.reportConnections, 2)
Expand All @@ -80,7 +88,8 @@ func TestReportConnection(t *testing.T) {
require.Equal(t, connID[:], req1.GetConnection().GetId())
require.Equal(t, proto.Connection_DISCONNECT, req1.GetConnection().GetAction())
require.Equal(t, "because", req1.GetConnection().GetReason())
require.Equal(t, tc.report.clientSessionID, req0.GetConnection().GetClientSessionId())
require.Equal(t, tc.report.clientSessionID, req1.GetConnection().GetClientSessionId())
require.Equal(t, tc.report.appName, req1.GetConnection().GetAppName())
}
}

Expand Down Expand Up @@ -166,3 +175,18 @@ func TestClassifyCoordinatorRPCExit(t *testing.T) {
})
}
}

// Every app must map to a value that coderd without app_name accepts.
func TestSSHConnectionType(t *testing.T) {
t.Parallel()

for appName, want := range map[string]proto.Connection_Type{
"ssh": proto.Connection_SSH,
"zed": proto.Connection_SSH,
"cursor": proto.Connection_VSCODE,
"jetbrains": proto.Connection_JETBRAINS,
"an_unregistered_ide": proto.Connection_SSH,
} {
require.Equal(t, want, sshConnectionType(appName), appName)
}
}
4 changes: 4 additions & 0 deletions agent/agentssh/agentssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ func extractAppName(env []string) (appName, rawAppName string, filteredEnv []str
// Normalize, don't classify: unknown names flow through.
appName = codersdk.NormalizeAppName(rawAppName)
}
// Only the reconnecting PTY server is the web terminal.
if appName == string(codersdk.AppFamilyReconnectingPTY) {
appName = string(codersdk.AppFamilyUnknown)
}

return appName, rawAppName, slices.DeleteFunc(env, func(kv string) bool {
return strings.HasPrefix(kv, prefix)
Expand Down
1 change: 1 addition & 0 deletions agent/agentssh/appname_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestExtractAppName(t *testing.T) {
{"VSCode", envWith("vscode"), "", "vscode"},
{"JetBrainsLegacyCasing", envWith("JetBrains"), "", "jetbrains"},
{"UnknownTypeNormalized", envWith("Cursor-Nightly"), "", "cursor_nightly"},
{"WebTerminalReserved", envWith("Reconnecting-PTY"), "", "unknown"},
{"LastInstanceWins", append(envWith("vscode"), AppNameEnvironmentVariable+"=cursor"), "", "cursor"},
// A variable that merely starts with the same characters is a
// different variable, not an app name.
Expand Down
Loading
Loading