From c0dd08f644ec9eba2d5d955802903a66b298e5c1 Mon Sep 17 00:00:00 2001 From: Ben Sejas Date: Tue, 14 Mar 2023 13:15:32 +1100 Subject: [PATCH 1/2] feat(agent): Add SSH max timeout option for coder agent --- agent/agent.go | 10 +++++++--- cli/agent.go | 13 ++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 29c56a9e43f38..9c0b80b2df73b 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -78,6 +78,7 @@ type Options struct { EnvironmentVariables map[string]string Logger slog.Logger AgentPorts map[int]string + SshMaxTimeout time.Duration } type Client interface { @@ -126,6 +127,7 @@ func New(options Options) io.Closer { lifecycleReported: make(chan codersdk.WorkspaceAgentLifecycle, 1), ignorePorts: options.AgentPorts, connStatsChan: make(chan *agentsdk.Stats, 1), + sshMaxTimeout: options.SshMaxTimeout, } a.init(ctx) return a @@ -153,9 +155,10 @@ type agent struct { envVars map[string]string // metadata is atomic because values can change after reconnection. - metadata atomic.Value - sessionToken atomic.Pointer[string] - sshServer *ssh.Server + metadata atomic.Value + sessionToken atomic.Pointer[string] + sshServer *ssh.Server + sshMaxTimeout time.Duration lifecycleUpdate chan struct{} lifecycleReported chan codersdk.WorkspaceAgentLifecycle @@ -780,6 +783,7 @@ func (a *agent) init(ctx context.Context) { _ = session.Exit(1) }, }, + MaxTimeout: a.sshMaxTimeout, } go a.runLoop(ctx) diff --git a/cli/agent.go b/cli/agent.go index 35db05863c9ee..f78d59d5eb1fc 100644 --- a/cli/agent.go +++ b/cli/agent.go @@ -31,10 +31,11 @@ import ( func workspaceAgent() *cobra.Command { var ( - auth string - logDir string - pprofAddress string - noReap bool + auth string + logDir string + pprofAddress string + noReap bool + sshMaxTimeout time.Duration ) cmd := &cobra.Command{ Use: "agent", @@ -208,7 +209,8 @@ func workspaceAgent() *cobra.Command { EnvironmentVariables: map[string]string{ "GIT_ASKPASS": executablePath, }, - AgentPorts: agentPorts, + AgentPorts: agentPorts, + SshMaxTimeout: sshMaxTimeout, }) <-ctx.Done() return closer.Close() @@ -219,6 +221,7 @@ func workspaceAgent() *cobra.Command { cliflag.StringVarP(cmd.Flags(), &logDir, "log-dir", "", "CODER_AGENT_LOG_DIR", os.TempDir(), "Specify the location for the agent log files") cliflag.StringVarP(cmd.Flags(), &pprofAddress, "pprof-address", "", "CODER_AGENT_PPROF_ADDRESS", "127.0.0.1:6060", "The address to serve pprof.") cliflag.BoolVarP(cmd.Flags(), &noReap, "no-reap", "", "", false, "Do not start a process reaper.") + cliflag.DurationVarP(cmd.Flags(), &sshMaxTimeout, "ssh-max-timeout", "", "CODER_AGENT_SSH_MAX_TIMEOUT", time.Duration(0), "Specify the max timeout for a SSH connection") return cmd } From 3723d0b0a85fc258553c2667587a4ae15ad16634 Mon Sep 17 00:00:00 2001 From: Ben Sejas Date: Wed, 15 Mar 2023 13:29:15 +1100 Subject: [PATCH 2/2] Fix lint and update test golden snapshot --- agent/agent.go | 4 ++-- cli/agent.go | 2 +- cli/testdata/coder_agent_--help.golden | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 9c0b80b2df73b..59485e330c325 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -78,7 +78,7 @@ type Options struct { EnvironmentVariables map[string]string Logger slog.Logger AgentPorts map[int]string - SshMaxTimeout time.Duration + SSHMaxTimeout time.Duration } type Client interface { @@ -127,7 +127,7 @@ func New(options Options) io.Closer { lifecycleReported: make(chan codersdk.WorkspaceAgentLifecycle, 1), ignorePorts: options.AgentPorts, connStatsChan: make(chan *agentsdk.Stats, 1), - sshMaxTimeout: options.SshMaxTimeout, + sshMaxTimeout: options.SSHMaxTimeout, } a.init(ctx) return a diff --git a/cli/agent.go b/cli/agent.go index f78d59d5eb1fc..c81fdb0b57bc1 100644 --- a/cli/agent.go +++ b/cli/agent.go @@ -210,7 +210,7 @@ func workspaceAgent() *cobra.Command { "GIT_ASKPASS": executablePath, }, AgentPorts: agentPorts, - SshMaxTimeout: sshMaxTimeout, + SSHMaxTimeout: sshMaxTimeout, }) <-ctx.Done() return closer.Close() diff --git a/cli/testdata/coder_agent_--help.golden b/cli/testdata/coder_agent_--help.golden index 2577cd660ed80..80ce3938d34aa 100644 --- a/cli/testdata/coder_agent_--help.golden +++ b/cli/testdata/coder_agent_--help.golden @@ -2,14 +2,16 @@ Usage: coder agent [flags] Flags: - --auth string Specify the authentication type to use for the agent. - Consumes $CODER_AGENT_AUTH (default "token") - -h, --help help for agent - --log-dir string Specify the location for the agent log files. - Consumes $CODER_AGENT_LOG_DIR (default "/tmp") - --no-reap Do not start a process reaper. - --pprof-address string The address to serve pprof. - Consumes $CODER_AGENT_PPROF_ADDRESS (default "127.0.0.1:6060") + --auth string Specify the authentication type to use for the agent. + Consumes $CODER_AGENT_AUTH (default "token") + -h, --help help for agent + --log-dir string Specify the location for the agent log files. + Consumes $CODER_AGENT_LOG_DIR (default "/tmp") + --no-reap Do not start a process reaper. + --pprof-address string The address to serve pprof. + Consumes $CODER_AGENT_PPROF_ADDRESS (default "127.0.0.1:6060") + --ssh-max-timeout duration Specify the max timeout for a SSH connection. + Consumes $CODER_AGENT_SSH_MAX_TIMEOUT Global Flags: --global-config coder Path to the global coder config directory.