From 76090a2ea79dbcb3e9e92943bb556fffaadf0918 Mon Sep 17 00:00:00 2001 From: JP Ungaretti Date: Thu, 26 May 2022 21:45:20 +0000 Subject: [PATCH] Add key to RPC call --- internal/codespaces/ssh.go | 1 + pkg/cmd/codespace/ssh.go | 10 ++++++++-- pkg/liveshare/session.go | 6 ++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/codespaces/ssh.go b/internal/codespaces/ssh.go index 0c81c4f32..50dabd620 100644 --- a/internal/codespaces/ssh.go +++ b/internal/codespaces/ssh.go @@ -56,6 +56,7 @@ func NewRemoteCommand(ctx context.Context, tunnelPort int, destination string, s // newSSHCommand populates an exec.Cmd to run a command (or if blank, // an interactive shell) over ssh. func newSSHCommand(ctx context.Context, port int, dst string, cmdArgs []string) (*exec.Cmd, []string, error) { + // TODO: Read SSH key from ~/.ssh/codespace and pass as argument with -i connArgs := []string{"-p", strconv.Itoa(port), "-o", "NoHostAuthenticationForLocalhost=yes"} // The ssh command syntax is: ssh [flags] user@host command [args...] diff --git a/pkg/cmd/codespace/ssh.go b/pkg/cmd/codespace/ssh.go index 10ea30473..f44418a68 100644 --- a/pkg/cmd/codespace/ssh.go +++ b/pkg/cmd/codespace/ssh.go @@ -115,6 +115,11 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e ctx, cancel := context.WithCancel(ctx) defer cancel() + // Make sure we have SSH keys available + // If not, generate them to ~/.ssh/codespace + // If that doesn't work, fail before connecting + userPublicKey := "hello" + codespace, err := getOrChooseCodespace(ctx, a.apiClient, opts.codespace) if err != nil { return err @@ -127,7 +132,7 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e defer safeClose(session, &err) a.StartProgressIndicatorWithLabel("Fetching SSH Details") - remoteSSHServerPort, sshUser, err := session.StartSSHServer(ctx) + remoteSSHServerPort, sshUser, err := session.StartSSHServer(ctx, userPublicKey) a.StopProgressIndicator() if err != nil { return fmt.Errorf("error getting ssh server details: %w", err) @@ -232,7 +237,8 @@ func (a *App) printOpenSSHConfig(ctx context.Context, opts sshOptions) (err erro } else { defer safeClose(session, &err) - _, result.user, err = session.StartSSHServer(ctx) + // Pass empty key here because it doesn't actually connect to SSH + _, result.user, err = session.StartSSHServer(ctx, "") if err != nil { result.err = fmt.Errorf("error getting ssh server details: %w", err) } else { diff --git a/pkg/liveshare/session.go b/pkg/liveshare/session.go index e2648c1c8..a53564888 100644 --- a/pkg/liveshare/session.go +++ b/pkg/liveshare/session.go @@ -38,7 +38,7 @@ func (s *Session) registerRequestHandler(requestType string, h handler) func() { // StartsSSHServer starts an SSH server in the container, installing sshd if necessary, // and returns the port on which it listens and the user name clients should provide. -func (s *Session) StartSSHServer(ctx context.Context) (int, string, error) { +func (s *Session) StartSSHServer(ctx context.Context, userPublicKey string) (int, string, error) { var response struct { Result bool `json:"result"` ServerPort string `json:"serverPort"` @@ -46,7 +46,9 @@ func (s *Session) StartSSHServer(ctx context.Context) (int, string, error) { Message string `json:"message"` } - if err := s.rpc.do(ctx, "ISshServerHostService.startRemoteServer", []string{}, &response); err != nil { + // Add param with key here, update corresponding on C# side + params := []string{userPublicKey} + if err := s.rpc.do(ctx, "ISshServerHostService.startRemoteServer", ¶ms, &response); err != nil { return 0, "", err }