Skip to content

feat: Add support for --identity-agent in coder ssh #1954

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 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func ssh() *cobra.Command {
stdio bool
shuffle bool
forwardAgent bool
identityAgent string
wsPollInterval time.Duration
)
cmd := &cobra.Command{
Expand Down Expand Up @@ -110,8 +111,11 @@ func ssh() *cobra.Command {
return err
}

if forwardAgent && os.Getenv("SSH_AUTH_SOCK") != "" {
err = gosshagent.ForwardToRemote(sshClient, os.Getenv("SSH_AUTH_SOCK"))
if identityAgent == "" {
identityAgent = os.Getenv("SSH_AUTH_SOCK")
}
if forwardAgent && identityAgent != "" {
err = gosshagent.ForwardToRemote(sshClient, identityAgent)
if err != nil {
return xerrors.Errorf("forward agent failed: %w", err)
}
Expand Down Expand Up @@ -171,6 +175,7 @@ func ssh() *cobra.Command {
cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace")
_ = cmd.Flags().MarkHidden("shuffle")
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")
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")
cliflag.DurationVarP(cmd.Flags(), &wsPollInterval, "workspace-poll-interval", "", "CODER_WORKSPACE_POLL_INTERVAL", workspacePollInterval, "Specifies how often to poll for workspace automated shutdown.")

return cmd
Expand Down
5 changes: 3 additions & 2 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ func TestSSH(t *testing.T) {

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

t.Parallel()

client, workspace, agentToken := setupWorkspaceForSSH(t)

_, _ = tGoContext(t, func(ctx context.Context) {
Expand Down Expand Up @@ -198,11 +199,11 @@ func TestSSH(t *testing.T) {
}
})

t.Setenv("SSH_AUTH_SOCK", agentSock)
cmd, root := clitest.New(t,
"ssh",
workspace.Name,
"--forward-agent",
"--identity-agent", agentSock, // Overrides $SSH_AUTH_SOCK.
)
clitest.SetupConfig(t, client, root)
pty := ptytest.New(t)
Expand Down