diff --git a/internal/cmd/configssh.go b/internal/cmd/configssh.go index 8818c9e2..9740a102 100644 --- a/internal/cmd/configssh.go +++ b/internal/cmd/configssh.go @@ -261,7 +261,7 @@ func makeSSHConfig(binPath, workspaceName, privateKeyFilepath string, additional } options = append(options, fmt.Sprintf("HostName coder.%s", workspaceName), - fmt.Sprintf("ProxyCommand %q tunnel %s 12213 stdio", binPath, workspaceName), + fmt.Sprintf("ProxyCommand %s", proxyCommand(binPath, workspaceName, true)), "StrictHostKeyChecking no", "ConnectTimeout=0", "IdentitiesOnly yes", @@ -279,6 +279,13 @@ func makeSSHConfig(binPath, workspaceName, privateKeyFilepath string, additional return fmt.Sprintf("Host coder.%s\n\t%s\n\n", workspaceName, strings.Join(options, "\n\t")) } +func proxyCommand(binPath, workspaceName string, quoted bool) string { + if quoted { + binPath = fmt.Sprintf("%q", binPath) + } + return fmt.Sprintf(`%s tunnel %s 12213 stdio`, binPath, workspaceName) +} + func writeStr(filename, data string) error { return ioutil.WriteFile(filename, []byte(data), 0777) } diff --git a/internal/cmd/ssh.go b/internal/cmd/ssh.go index cec588a6..983ec3f4 100644 --- a/internal/cmd/ssh.go +++ b/internal/cmd/ssh.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "net/url" "os" "os/exec" "os/user" @@ -41,10 +40,7 @@ func shell(cmd *cobra.Command, args []string) error { if err != nil { return err } - me, err := client.Me(ctx) - if err != nil { - return err - } + workspace, err := findWorkspace(ctx, client, args[0], coder.Me) if err != nil { return err @@ -60,9 +56,9 @@ func shell(cmd *cobra.Command, args []string) error { if err != nil { return err } - u, err := url.Parse(wp.EnvproxyAccessURL) - if err != nil { - return err + + if !wp.SSHEnabled { + return clog.Error("SSH is disabled on this Workspace") } usr, err := user.Current() @@ -75,13 +71,21 @@ func shell(cmd *cobra.Command, args []string) error { if err != nil { return err } + + binPath, err := binPath() + if err != nil { + return xerrors.Errorf("Failed to get executable path: %w", err) + } + ssh := exec.CommandContext(ctx, "ssh", "-i"+privateKeyFilepath, - fmt.Sprintf("%s-%s@%s", me.Username, workspace.Name, u.Hostname()), + "-o"+fmt.Sprintf("ProxyCommand=%s", proxyCommand(binPath, workspace.Name, false)), + workspace.Name, ) if len(args) > 1 { ssh.Args = append(ssh.Args, args[1:]...) } + ssh.Stderr = os.Stderr ssh.Stdout = os.Stdout ssh.Stdin = os.Stdin