Skip to content

feat(agent): add connection reporting for SSH and reconnecing PTY #16652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: put connection reports behind experimental flag
  • Loading branch information
mafredri committed Feb 24, 2025
commit a77ceacf65df8f1c8d9e872a51d75518c34474e2
11 changes: 11 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ type Options struct {
BlockFileTransfer bool
Execer agentexec.Execer
ContainerLister agentcontainers.Lister

ExperimentalConnectionReports bool
}

type Client interface {
Expand Down Expand Up @@ -189,6 +191,8 @@ func New(options Options) Agent {
metrics: newAgentMetrics(prometheusRegistry),
execer: options.Execer,
lister: options.ContainerLister,

experimentalConnectionReports: options.ExperimentalConnectionReports,
}
// Initially, we have a closed channel, reflecting the fact that we are not initially connected.
// Each time we connect we replace the channel (while holding the closeMutex) with a new one
Expand Down Expand Up @@ -263,6 +267,8 @@ type agent struct {
metrics *agentMetrics
execer agentexec.Execer
lister agentcontainers.Lister

experimentalConnectionReports bool
}

func (a *agent) TailnetConn() *tailnet.Conn {
Expand Down Expand Up @@ -770,6 +776,11 @@ func (a *agent) reportConnectionsLoop(ctx context.Context, aAPI proto.DRPCAgentC
}

func (a *agent) reportConnection(id uuid.UUID, connectionType proto.Connection_Type, ip string) (disconnected func(code int, reason string)) {
// If the experiment hasn't been enabled, we don't report connections.
if !a.experimentalConnectionReports {
return func(int, string) {} // Noop.
}

// Remove the port from the IP.
if portIndex := strings.LastIndex(ip, ":"); portIndex != -1 {
ip = ip[:portIndex]
Expand Down
23 changes: 18 additions & 5 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ func TestAgent_Stats_Magic(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
//nolint:dogsled
conn, agentClient, stats, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
conn, agentClient, stats, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.ExperimentalConnectionReports = true
})
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)
defer sshClient.Close()
Expand Down Expand Up @@ -227,7 +229,9 @@ func TestAgent_Stats_Magic(t *testing.T) {
remotePort := sc.Text()

//nolint:dogsled
conn, agentClient, stats, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
conn, agentClient, stats, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.ExperimentalConnectionReports = true
})
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)

Expand Down Expand Up @@ -922,7 +926,9 @@ func TestAgent_SFTP(t *testing.T) {
home = "/" + strings.ReplaceAll(home, "\\", "/")
}
//nolint:dogsled
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.ExperimentalConnectionReports = true
})
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)
defer sshClient.Close()
Expand Down Expand Up @@ -958,7 +964,9 @@ func TestAgent_SCP(t *testing.T) {
defer cancel()

//nolint:dogsled
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.ExperimentalConnectionReports = true
})
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)
defer sshClient.Close()
Expand Down Expand Up @@ -1001,6 +1009,7 @@ func TestAgent_FileTransferBlocked(t *testing.T) {
//nolint:dogsled
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.BlockFileTransfer = true
o.ExperimentalConnectionReports = true
})
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)
Expand All @@ -1021,6 +1030,7 @@ func TestAgent_FileTransferBlocked(t *testing.T) {
//nolint:dogsled
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.BlockFileTransfer = true
o.ExperimentalConnectionReports = true
})
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)
Expand Down Expand Up @@ -1049,6 +1059,7 @@ func TestAgent_FileTransferBlocked(t *testing.T) {
//nolint:dogsled
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.BlockFileTransfer = true
o.ExperimentalConnectionReports = true
})
sshClient, err := conn.SSHClient(ctx)
require.NoError(t, err)
Expand Down Expand Up @@ -1679,7 +1690,9 @@ func TestAgent_ReconnectingPTY(t *testing.T) {
defer cancel()

//nolint:dogsled
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
conn, agentClient, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0, func(_ *agenttest.Client, o *agent.Options) {
o.ExperimentalConnectionReports = true
})
id := uuid.New()

// Test that the connection is reported. This must be tested in the
Expand Down
16 changes: 16 additions & 0 deletions cli/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
agentHeaderCommand string
agentHeader []string
devcontainersEnabled bool

experimentalConnectionReports bool
)
cmd := &serpent.Command{
Use: "agent",
Expand Down Expand Up @@ -325,6 +327,10 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
containerLister = agentcontainers.NewDocker(execer)
}

if experimentalConnectionReports {
logger.Info(ctx, "experimental connection reports enabled")
}

agnt := agent.New(agent.Options{
Client: client,
Logger: logger,
Expand All @@ -351,6 +357,8 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
BlockFileTransfer: blockFileTransfer,
Execer: execer,
ContainerLister: containerLister,

ExperimentalConnectionReports: experimentalConnectionReports,
})

promHandler := agent.PrometheusMetricsHandler(prometheusRegistry, logger)
Expand Down Expand Up @@ -480,6 +488,14 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
Description: "Allow the agent to automatically detect running devcontainers.",
Value: serpent.BoolOf(&devcontainersEnabled),
},
{
Flag: "experimental-connection-reports-enable",
Hidden: true,
Default: "false",
Env: "CODER_AGENT_EXPERIMENTAL_CONNECTION_REPORTS_ENABLE",
Description: "Enable experimental connection reports.",
Value: serpent.BoolOf(&experimentalConnectionReports),
},
Comment on lines +492 to +499
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be something that gets set as a deployment-wide experiment and is automatically set in the agent manifest?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this is an (temporary) experiment is simply to not mess up the audit log UI until it is updated. I'm not aware that this needs to be placed behind an experiment deployment wide.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an alternative suggestion!

}

return cmd
Expand Down
Loading