Skip to content

Commit 3df1a08

Browse files
committed
feat: Add forward agent flag to coder ssh
1 parent 54b74f7 commit 3df1a08

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

cli/ssh.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/mattn/go-isatty"
1616
"github.com/spf13/cobra"
1717
gossh "golang.org/x/crypto/ssh"
18+
gosshagent "golang.org/x/crypto/ssh/agent"
1819
"golang.org/x/term"
1920
"golang.org/x/xerrors"
2021

@@ -32,6 +33,7 @@ func ssh() *cobra.Command {
3233
var (
3334
stdio bool
3435
shuffle bool
36+
forwardAgent bool
3537
wsPollInterval time.Duration
3638
)
3739
cmd := &cobra.Command{
@@ -108,6 +110,17 @@ func ssh() *cobra.Command {
108110
return err
109111
}
110112

113+
if forwardAgent && os.Getenv("SSH_AUTH_SOCK") != "" {
114+
err = gosshagent.ForwardToRemote(sshClient, os.Getenv("SSH_AUTH_SOCK"))
115+
if err != nil {
116+
return xerrors.Errorf("forward agent failed: %w", err)
117+
}
118+
err = gosshagent.RequestAgentForwarding(sshSession)
119+
if err != nil {
120+
return xerrors.Errorf("request agent forwarding failed: %w", err)
121+
}
122+
}
123+
111124
stdoutFile, valid := cmd.OutOrStdout().(*os.File)
112125
if valid && isatty.IsTerminal(stdoutFile.Fd()) {
113126
state, err := term.MakeRaw(int(os.Stdin.Fd()))
@@ -156,8 +169,9 @@ func ssh() *cobra.Command {
156169
}
157170
cliflag.BoolVarP(cmd.Flags(), &stdio, "stdio", "", "CODER_SSH_STDIO", false, "Specifies whether to emit SSH output over stdin/stdout.")
158171
cliflag.BoolVarP(cmd.Flags(), &shuffle, "shuffle", "", "CODER_SSH_SHUFFLE", false, "Specifies whether to choose a random workspace")
159-
cliflag.DurationVarP(cmd.Flags(), &wsPollInterval, "workspace-poll-interval", "", "CODER_WORKSPACE_POLL_INTERVAL", workspacePollInterval, "Specifies how often to poll for workspace automated shutdown.")
160172
_ = cmd.Flags().MarkHidden("shuffle")
173+
cliflag.BoolVarP(cmd.Flags(), &forwardAgent, "forward-agent", "", "CODER_SSH_FORWARD_AGENT", false, "Specifies whether to forward the SSH agent specified in $SSH_AUTH_SOCK")
174+
cliflag.DurationVarP(cmd.Flags(), &wsPollInterval, "workspace-poll-interval", "", "CODER_WORKSPACE_POLL_INTERVAL", workspacePollInterval, "Specifies how often to poll for workspace automated shutdown.")
161175

162176
return cmd
163177
}

0 commit comments

Comments
 (0)