Files
coder/coderd/database/sdk2db/sdk2db.go
Ehab Younes f573ec3533 feat: report agent connection methods and app identities (#30467)
Agents now report how a connection was made and which app made it, so
the connection log records the method and the client app separately.

- Bump the Agent API to v2.13. `Connection` gains `connection_method`
  and `app_name`, and `type` is deprecated.
- SSH and reconnecting PTY handlers set the method themselves, so a
  client-supplied app name can't change it.
- SSH sessions report the app from `CODER_SSH_SESSION_TYPE`. Sessions
  without one count as SSH but report no app.
- Disconnect reports repeat the connect report's method, app, and
  client session ID.
- Coderd logs the reported method and normalized app name. For agents
  older than v2.13, it maps `type` as before.
- Coderd rejects unknown methods instead of logging them as SSH.

Closes CLIENT-1037
2026-10-14 13:54:17 +03:00

59 lines
2.3 KiB
Go

// Package sdk2db provides common conversion routines from codersdk types to database types
package sdk2db
import (
"golang.org/x/xerrors"
agentproto "github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/util/slice"
"github.com/coder/coder/v2/codersdk"
)
func ProvisionerDaemonStatus(status codersdk.ProvisionerDaemonStatus) database.ProvisionerDaemonStatus {
return database.ProvisionerDaemonStatus(status)
}
func ProvisionerDaemonStatuses(params []codersdk.ProvisionerDaemonStatus) []database.ProvisionerDaemonStatus {
return slice.List(params, ProvisionerDaemonStatus)
}
// ConnectionLogFromAgentConnection returns the method and app name to log for
// an agent connection report.
func ConnectionLogFromAgentConnection(conn *agentproto.Connection) (database.ConnectionLogMethod, string, error) {
appName := conn.GetAppName()
if appName != "" {
appName = codersdk.NormalizeAppName(appName)
}
switch method := conn.GetConnectionMethod(); method {
case agentproto.Connection_METHOD_UNSPECIFIED:
return connectionLogFromAgentType(conn.GetType())
case agentproto.Connection_METHOD_SSH:
return database.ConnectionLogMethodSSH, appName, nil
case agentproto.Connection_METHOD_RECONNECTING_PTY:
return database.ConnectionLogMethodReconnectingPTY, appName, nil
case agentproto.Connection_METHOD_PORT_FORWARDING:
// app_name_or_port holds a forward's destination, not a client app.
return database.ConnectionLogMethodPortForwarding, "", nil
default:
return "", "", xerrors.Errorf("unsupported agent connection method %d", method)
}
}
// connectionLogFromAgentType maps the type sent by agents before API v2.13.
// SSH handlers reported unfamiliar apps as unspecified.
func connectionLogFromAgentType(typ agentproto.Connection_Type) (database.ConnectionLogMethod, string, error) {
switch typ {
case agentproto.Connection_SSH, agentproto.Connection_TYPE_UNSPECIFIED:
return database.ConnectionLogMethodSSH, "", nil
case agentproto.Connection_JETBRAINS:
return database.ConnectionLogMethodSSH, "jetbrains", nil
case agentproto.Connection_VSCODE:
return database.ConnectionLogMethodSSH, "vscode", nil
case agentproto.Connection_RECONNECTING_PTY:
return database.ConnectionLogMethodReconnectingPTY, "", nil
default:
return "", "", xerrors.Errorf("unsupported agent connection type %d", typ)
}
}