Connection logs now record how a connection was made (`connection_method`) separately from which app made it (`app_name`). Storage, the HTTP API, search filters, and the UI move to the new fields. Legacy agent reports and the deprecated `type` field keep working. - Add `connection_method` (`ssh`, `reconnecting_pty`, `workspace_app`, `port_forwarding`, `tunnel`) and optional `app_name` and `app_display_name` to `GET /api/v2/connectionlog`. Deprecate `type` and make it a plain string in the API and the Go SDK. - Add `method:` and repeatable `app:` search filters. `type:` keeps its old results but can't be combined with the new filters. - Replace the Type menu with a Method menu, show the app next to the method in each row, and warn when a filter uses `type:`. - Change the Active SSH connections preset to `method:ssh`, so it also includes SSH sessions from VS Code and JetBrains apps. - Generate `codersdk.ConnectionLogMethod` from the database enum with `typegen dbenums`. - Migration 000617 rewrites `connection_logs` under an ACCESS EXCLUSIVE lock, which takes about 50 seconds for 10 million rows. `vscode` and `jetbrains` rows become `ssh` rows with that app. Rolling back folds apps into the old types and loses per-app names. - Document the new filters and how to replace each `type:` value. Closes #27412
128 lines
3.8 KiB
Go
128 lines
3.8 KiB
Go
package agentapi
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"sync/atomic"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
|
|
"cdr.dev/slog/v3"
|
|
agentproto "github.com/coder/coder/v2/agent/proto"
|
|
"github.com/coder/coder/v2/coderd/connectionlog"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
|
"github.com/coder/coder/v2/coderd/database/sdk2db"
|
|
"github.com/coder/coder/v2/coderd/tracing"
|
|
)
|
|
|
|
type ConnLogAPI struct {
|
|
AgentID uuid.UUID
|
|
AgentName string
|
|
ConnectionLogger *atomic.Pointer[connectionlog.ConnectionLogger]
|
|
Workspace *CachedWorkspaceFields
|
|
Database database.Store
|
|
Log slog.Logger
|
|
}
|
|
|
|
func (a *ConnLogAPI) ReportConnection(ctx context.Context, req *agentproto.ReportConnectionRequest) (*emptypb.Empty, error) {
|
|
// We use the connection ID to identify which connection log event to mark
|
|
// as closed, when we receive a close action for that ID.
|
|
connectionID, err := uuid.FromBytes(req.GetConnection().GetId())
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("connection id from bytes: %w", err)
|
|
}
|
|
|
|
if connectionID == uuid.Nil {
|
|
return nil, xerrors.New("connection ID cannot be nil")
|
|
}
|
|
action, err := db2sdk.ConnectionLogStatusFromAgentProtoConnectionAction(req.GetConnection().GetAction())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
method, appName, err := sdk2db.ConnectionLogFromAgentType(req.GetConnection().GetType())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var code sql.NullInt32
|
|
if action == database.ConnectionStatusDisconnected {
|
|
code = sql.NullInt32{
|
|
Int32: req.GetConnection().GetStatusCode(),
|
|
Valid: true,
|
|
}
|
|
}
|
|
|
|
var ws database.WorkspaceIdentity
|
|
if dbws, ok := a.Workspace.AsWorkspaceIdentity(); ok {
|
|
ws = dbws
|
|
}
|
|
if ws.Equal(database.WorkspaceIdentity{}) {
|
|
workspace, err := a.Database.GetWorkspaceByAgentID(ctx, a.AgentID)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("get workspace by agent id: %w", err)
|
|
}
|
|
ws = database.WorkspaceIdentityFromWorkspace(workspace)
|
|
}
|
|
|
|
// Some older clients may incorrectly report "localhost" as the IP address.
|
|
// Related to https://github.com/coder/coder/issues/20194
|
|
logIPRaw := req.GetConnection().GetIp()
|
|
if logIPRaw == "localhost" {
|
|
logIPRaw = "127.0.0.1"
|
|
}
|
|
logIP := database.ParseIP(logIPRaw) // will return null if invalid
|
|
|
|
reason := req.GetConnection().GetReason()
|
|
clientSessionID := req.GetConnection().GetClientSessionId()
|
|
if !tracing.ValidSessionID(clientSessionID) {
|
|
clientSessionID = ""
|
|
}
|
|
connLogger := *a.ConnectionLogger.Load()
|
|
err = connLogger.Upsert(ctx, database.UpsertConnectionLogParams{
|
|
ID: uuid.New(),
|
|
Time: req.GetConnection().GetTimestamp().AsTime(),
|
|
OrganizationID: ws.OrganizationID,
|
|
WorkspaceOwnerID: ws.OwnerID,
|
|
WorkspaceID: ws.ID,
|
|
WorkspaceName: ws.Name,
|
|
AgentName: a.AgentName,
|
|
ConnectionMethod: method,
|
|
AppNameOrPort: sql.NullString{String: appName, Valid: appName != ""},
|
|
Code: code,
|
|
IP: logIP,
|
|
ConnectionID: uuid.NullUUID{
|
|
UUID: connectionID,
|
|
Valid: true,
|
|
},
|
|
ClientSessionID: sql.NullString{
|
|
String: clientSessionID,
|
|
Valid: clientSessionID != "",
|
|
},
|
|
DisconnectReason: sql.NullString{
|
|
String: reason,
|
|
Valid: reason != "",
|
|
},
|
|
// We supply the action:
|
|
// - So the DB can handle duplicate connections or disconnections properly.
|
|
// - To make it clear whether this is a connection or disconnection
|
|
// prior to it's insertion into the DB (logs)
|
|
ConnectionStatus: action,
|
|
|
|
// It's not possible to tell which user connected. Once we have
|
|
// the capability, this may be reported by the agent.
|
|
UserID: uuid.NullUUID{
|
|
Valid: false,
|
|
},
|
|
// N/A
|
|
UserAgent: sql.NullString{},
|
|
})
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("export connection log: %w", err)
|
|
}
|
|
|
|
return &emptypb.Empty{}, nil
|
|
}
|