Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

chore: Use webrtc for coder sh (#408) #435

Merged
merged 3 commits into from
Sep 10, 2021
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: 8 additions & 1 deletion internal/cmd/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
Expand Down
22 changes: 13 additions & 9 deletions internal/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"net/url"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand Down
13 changes: 0 additions & 13 deletions internal/cmd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package cmd
import (
"testing"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"

"cdr.dev/coder-cli/coder-sdk"
)

Expand All @@ -16,17 +13,7 @@ func Test_users(t *testing.T) {
res := execute(t, nil, "users", "ls", "--output=json")
res.success(t)
res.stdoutUnmarshals(t, &users)
assertAdmin(t, users)

res = execute(t, nil, "users", "ls", "--output=human")
res.success(t)
}

func assertAdmin(t *testing.T, users []coder.User) {
for _, u := range users {
if u.Username == "admin" {
return
}
}
slogtest.Fatal(t, "did not find admin user", slog.F("users", users))
}