Skip to content

Commit cebbfc7

Browse files
committed
Get SSH username from workspacekit
1 parent c25da39 commit cebbfc7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

components/ws-proxy/pkg/sshproxy/server.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ package sshproxy
77
import (
88
"context"
99
"crypto/subtle"
10+
"fmt"
11+
"io"
1012
"net"
13+
"net/http"
1114
"regexp"
1215
"strings"
1316
"time"
@@ -318,6 +321,13 @@ func (s *Server) HandleConn(c net.Conn) {
318321
}
319322

320323
session.WorkspacePrivateKey = key
324+
325+
// obtain the SSH username from workspacekit.
326+
workspacekitPort := "22998"
327+
userName, err = workspaceSSHUsername(ctx, wsInfo.IPAddress, workspacekitPort)
328+
if err != nil {
329+
log.WithField("instanceId", wsInfo.InstanceID).WithError(err).Warn("failed to retrieve the SSH username. Using the default.")
330+
}
321331
} else {
322332
key, userName, err = s.GetWorkspaceSSHKey(ctx, wsInfo.IPAddress, supervisorPort)
323333
if err != nil {
@@ -494,3 +504,23 @@ func (s *Server) Serve(l net.Listener) error {
494504
go s.HandleConn(conn)
495505
}
496506
}
507+
508+
func workspaceSSHUsername(ctx context.Context, workspaceIP string, workspacekitPort string) (string, error) {
509+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%v:%v/ssh/username", workspaceIP, workspacekitPort), nil)
510+
if err != nil {
511+
return "", err
512+
}
513+
514+
resp, err := http.DefaultClient.Do(req)
515+
if err != nil {
516+
return "", err
517+
}
518+
defer resp.Body.Close()
519+
520+
username, err := io.ReadAll(resp.Body)
521+
if err != nil {
522+
return "", err
523+
}
524+
525+
return string(username), nil
526+
}

0 commit comments

Comments
 (0)