Add key to RPC call

This commit is contained in:
JP Ungaretti 2022-05-26 21:45:20 +00:00 committed by GitHub
parent 77507cd94c
commit 76090a2ea7
3 changed files with 13 additions and 4 deletions

View file

@ -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...]

View file

@ -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 {

View file

@ -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", &params, &response); err != nil {
return 0, "", err
}