Skip to content

Commit 0d13954

Browse files
committed
feat: Add support for --identity-agent in coder ssh
Fixes #1887
1 parent 6be8a37 commit 0d13954

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

cli/ssh.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func ssh() *cobra.Command {
3434
stdio bool
3535
shuffle bool
3636
forwardAgent bool
37+
identityAgent string
3738
wsPollInterval time.Duration
3839
)
3940
cmd := &cobra.Command{
@@ -110,8 +111,11 @@ func ssh() *cobra.Command {
110111
return err
111112
}
112113

113-
if forwardAgent && os.Getenv("SSH_AUTH_SOCK") != "" {
114-
err = gosshagent.ForwardToRemote(sshClient, os.Getenv("SSH_AUTH_SOCK"))
114+
if identityAgent == "" {
115+
identityAgent = os.Getenv("SSH_AUTH_SOCK")
116+
}
117+
if forwardAgent && identityAgent != "" {
118+
err = gosshagent.ForwardToRemote(sshClient, identityAgent)
115119
if err != nil {
116120
return xerrors.Errorf("forward agent failed: %w", err)
117121
}
@@ -171,6 +175,7 @@ func ssh() *cobra.Command {
171175
cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace")
172176
_ = cmd.Flags().MarkHidden("shuffle")
173177
cliflag.BoolVarP(cmd.Flags(), &forwardAgent, "forward-agent", "A", "CODER_SSH_FORWARD_AGENT", false, "Specifies whether to forward the SSH agent specified in $SSH_AUTH_SOCK")
178+
cliflag.StringVarP(cmd.Flags(), &identityAgent, "identity-agent", "", "CODER_SSH_IDENTITY_AGENT", "", "Specifies which identity agent to use (overrides $SSH_AUTH_SOCK), forward agent must also be enabled")
174179
cliflag.DurationVarP(cmd.Flags(), &wsPollInterval, "workspace-poll-interval", "", "CODER_WORKSPACE_POLL_INTERVAL", workspacePollInterval, "Specifies how often to poll for workspace automated shutdown.")
175180

176181
return cmd

cli/ssh_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,13 @@ func TestSSH(t *testing.T) {
146146

147147
<-cmdDone
148148
})
149-
//nolint:paralleltest // Disabled due to use of t.Setenv.
150149
t.Run("ForwardAgent", func(t *testing.T) {
151150
if runtime.GOOS == "windows" {
152151
t.Skip("Test not supported on windows")
153152
}
154153

154+
t.Parallel()
155+
155156
client, workspace, agentToken := setupWorkspaceForSSH(t)
156157

157158
_, _ = tGoContext(t, func(ctx context.Context) {
@@ -198,11 +199,11 @@ func TestSSH(t *testing.T) {
198199
}
199200
})
200201

201-
t.Setenv("SSH_AUTH_SOCK", agentSock)
202202
cmd, root := clitest.New(t,
203203
"ssh",
204204
workspace.Name,
205205
"--forward-agent",
206+
"--identity-agent", agentSock, // Overrides $SSH_AUTH_SOCK.
206207
)
207208
clitest.SetupConfig(t, client, root)
208209
pty := ptytest.New(t)

0 commit comments

Comments
 (0)