Skip to content

Commit 7f83e2f

Browse files
authored
[ws-proxy] only get username if workspace not managed by mk2 (#19180)
* [ws-proxy] only get username if workspace not managed by mk2 * remove ssh key from infoprovider * improve logs * Update components/ws-proxy/pkg/sshproxy/server.go
1 parent e21e02a commit 7f83e2f

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

components/ws-proxy/pkg/common/infoprovider.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/gitpod-io/gitpod/ws-manager/api"
1111
wsapi "github.com/gitpod-io/gitpod/ws-manager/api"
12-
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
1312
)
1413

1514
const (
@@ -65,7 +64,6 @@ type WorkspaceInfo struct {
6564
SSHPublicKeys []string
6665
IsRunning bool
6766

68-
SSHKey *workspacev1.SSHKey
69-
7067
IsEnabledSSHCA bool
68+
IsManagedByMk2 bool
7169
}

components/ws-proxy/pkg/proxy/infoprovider.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"sigs.k8s.io/controller-runtime/pkg/predicate"
1919
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2020

21+
wsk8s "github.com/gitpod-io/gitpod/common-go/kubernetes"
2122
"github.com/gitpod-io/gitpod/common-go/log"
2223
wsapi "github.com/gitpod-io/gitpod/ws-manager/api"
2324
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
@@ -137,6 +138,11 @@ func (r *CRDWorkspaceInfoProvider) Reconcile(ctx context.Context, req ctrl.Reque
137138
if ws.Spec.Admission.Level == workspacev1.AdmissionLevelEveryone {
138139
admission = wsapi.AdmissionLevel_ADMIT_EVERYONE
139140
}
141+
managedByMk2 := true
142+
if managedBy, ok := ws.Labels[wsk8s.WorkspaceManagedByLabel]; ok && managedBy != "ws-manager-mk2" {
143+
managedByMk2 = false
144+
}
145+
140146
wsinfo := &common.WorkspaceInfo{
141147
WorkspaceID: ws.Spec.Ownership.WorkspaceID,
142148
InstanceID: ws.Name,
@@ -150,9 +156,9 @@ func (r *CRDWorkspaceInfoProvider) Reconcile(ctx context.Context, req ctrl.Reque
150156
StartedAt: ws.CreationTimestamp.Time,
151157
OwnerUserId: ws.Spec.Ownership.Owner,
152158
SSHPublicKeys: ws.Spec.SshPublicKeys,
153-
SSHKey: ws.Spec.SSHKey,
154159
IsRunning: ws.Status.Phase == workspacev1.WorkspacePhaseRunning,
155160
IsEnabledSSHCA: ws.Spec.SSHGatewayCAPublicKey != "",
161+
IsManagedByMk2: managedByMk2,
156162
}
157163

158164
r.store.Update(req.Name, wsinfo)

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ func (s *Server) HandleConn(c net.Conn) {
301301
log.WithField("workspaceId", workspaceId).WithError(err).Error("failed to get workspace info")
302302
return
303303
}
304+
log := log.WithField("instanceId", wsInfo.InstanceID).WithField("isMk2", wsInfo.IsManagedByMk2)
304305
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
305306
supervisorPort := "22999"
306307
if debugWorkspace {
@@ -318,37 +319,40 @@ func (s *Server) HandleConn(c net.Conn) {
318319
OwnerUserId: wsInfo.OwnerUserId,
319320
}
320321

321-
if wsInfo.SSHKey != nil {
322-
key, err = ssh.ParsePrivateKey([]byte(wsInfo.SSHKey.Private))
323-
if err != nil {
322+
if !wsInfo.IsManagedByMk2 {
323+
if s.caKey == nil || !wsInfo.IsEnabledSSHCA {
324+
err = xerrors.Errorf("workspace not managed by mk2, but didn't have SSH CA enabled")
325+
s.TrackSSHConnection(wsInfo, "connect", ErrCreateSSHKey)
326+
ReportSSHAttemptMetrics(ErrCreateSSHKey)
327+
log.WithError(err).Error("failed to generate ssh cert")
324328
cancel()
325329
return
326330
}
327-
328-
session.WorkspacePrivateKey = key
329-
330331
// obtain the SSH username from workspacekit.
331332
workspacekitPort := "22998"
332333
userName, err = workspaceSSHUsername(ctx, wsInfo.IPAddress, workspacekitPort)
333334
if err != nil {
334-
log.WithField("instanceId", wsInfo.InstanceID).WithError(err).Warn("failed to retrieve the SSH username. Using the default.")
335+
log.WithError(err).Warn("failed to retrieve the SSH username. Using the default.")
335336
}
336-
} else if s.caKey != nil && wsInfo.IsEnabledSSHCA {
337+
}
338+
339+
if s.caKey != nil && wsInfo.IsEnabledSSHCA {
337340
key, err = s.GenerateSSHCert(ctx, userName)
338341
if err != nil {
339-
log.WithField("workspaceId", workspaceId).WithError(err).Error("failed to generate ssh cert")
342+
s.TrackSSHConnection(wsInfo, "connect", ErrCreateSSHKey)
343+
ReportSSHAttemptMetrics(ErrCreateSSHKey)
344+
log.WithError(err).Error("failed to generate ssh cert")
340345
cancel()
341346
return
342347
}
343-
344348
session.WorkspacePrivateKey = key
345349
} else {
346350
key, userName, err = s.GetWorkspaceSSHKey(ctx, wsInfo.IPAddress, supervisorPort)
347351
if err != nil {
348352
cancel()
349353
s.TrackSSHConnection(wsInfo, "connect", ErrCreateSSHKey)
350354
ReportSSHAttemptMetrics(ErrCreateSSHKey)
351-
log.WithField("instanceId", wsInfo.InstanceID).WithError(err).Error("failed to create private pair in workspace")
355+
log.WithError(err).Error("failed to create private pair in workspace")
352356
return
353357
}
354358

@@ -366,7 +370,7 @@ func (s *Server) HandleConn(c net.Conn) {
366370
if err != nil {
367371
s.TrackSSHConnection(wsInfo, "connect", ErrConnFailed)
368372
ReportSSHAttemptMetrics(ErrConnFailed)
369-
log.WithField("instanceId", wsInfo.InstanceID).WithField("workspaceIP", wsInfo.IPAddress).WithError(err).Error("dail failed")
373+
log.WithField("workspaceIP", wsInfo.IPAddress).WithError(err).Error("dial failed")
370374
return
371375
}
372376
defer conn.Close()
@@ -384,7 +388,7 @@ func (s *Server) HandleConn(c net.Conn) {
384388
if err != nil {
385389
s.TrackSSHConnection(wsInfo, "connect", ErrConnFailed)
386390
ReportSSHAttemptMetrics(ErrConnFailed)
387-
log.WithField("instanceId", wsInfo.InstanceID).WithField("workspaceIP", wsInfo.IPAddress).WithError(err).Error("connect failed")
391+
log.WithField("workspaceIP", wsInfo.IPAddress).WithError(err).Error("connect failed")
388392
return
389393
}
390394
s.Heartbeater.SendHeartbeat(wsInfo.InstanceID, false, true)

0 commit comments

Comments
 (0)