Skip to content

Commit 453bf6e

Browse files
aledbfiQQBot
andauthored
Refactor configuration of workspace SSH key (#19059)
* Refactor configuration of workspace SSH key * Update go modules * Update CRD * only add SSH Key to workspace CR (#19130) --------- Co-authored-by: Pudong <[email protected]>
1 parent 34674c7 commit 453bf6e

File tree

6 files changed

+90
-14
lines changed

6 files changed

+90
-14
lines changed

components/ws-manager-api/go/crd/v1/workspace_types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ type WorkspaceSpec struct {
6262

6363
// the XFS quota to enforce on the workspace's /workspace folder
6464
StorageQuota int `json:"storageQuota,omitempty"`
65+
66+
SSHKey *SSHKey `json:"ssh,omitempty"`
6567
}
6668

6769
type Ownership struct {
@@ -116,6 +118,14 @@ type TimeoutSpec struct {
116118
MaximumLifetime *metav1.Duration `json:"maximumLifetime,omitempty"`
117119
}
118120

121+
// SSHKey temporal generated SSH key required to access the workspace
122+
type SSHKey struct {
123+
// +kubebuilder:validation:Required
124+
Public string `json:"publicKey"`
125+
// +kubebuilder:validation:Required
126+
Private string `json:"privateKey"`
127+
}
128+
119129
type AdmissionSpec struct {
120130
// +kubebuilder:default=Owner
121131
Level AdmissionLevel `json:"level"`

components/ws-manager-api/go/crd/v1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/ws-manager-mk2/config/crd/bases/workspace.gitpod.io_workspaces.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ spec:
157157
type: object
158158
minItems: 0
159159
type: array
160+
ssh:
161+
description: SSHKey temporal generated SSH key required to access
162+
the workspace
163+
properties:
164+
privateKey:
165+
type: string
166+
publicKey:
167+
type: string
168+
required:
169+
- privateKey
170+
- publicKey
171+
type: object
160172
sshPublicKeys:
161173
items:
162174
type: string
@@ -557,6 +569,19 @@ spec:
557569
prior to shutting the workspace down. This condition is only used
558570
for headless workspaces.
559571
type: string
572+
storage:
573+
properties:
574+
attachedDevice:
575+
type: string
576+
mountPath:
577+
type: string
578+
volumeName:
579+
type: string
580+
required:
581+
- attachedDevice
582+
- mountPath
583+
- volumeName
584+
type: object
560585
url:
561586
type: string
562587
required:

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ 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"
1213
)
1314

1415
const (
@@ -63,4 +64,6 @@ type WorkspaceInfo struct {
6364
OwnerUserId string
6465
SSHPublicKeys []string
6566
IsRunning bool
67+
68+
SSHKey *workspacev1.SSHKey
6669
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func (r *CRDWorkspaceInfoProvider) Reconcile(ctx context.Context, req ctrl.Reque
150150
StartedAt: ws.CreationTimestamp.Time,
151151
OwnerUserId: ws.Spec.Ownership.Owner,
152152
SSHPublicKeys: ws.Spec.SshPublicKeys,
153+
SSHKey: ws.Spec.SSHKey,
153154
IsRunning: ws.Status.Phase == workspacev1.WorkspacePhaseRunning,
154155
}
155156

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -299,23 +299,40 @@ func (s *Server) HandleConn(c net.Conn) {
299299
if debugWorkspace {
300300
supervisorPort = "24999"
301301
}
302-
key, userName, err := s.GetWorkspaceSSHKey(ctx, wsInfo.IPAddress, supervisorPort)
303-
if err != nil {
304-
cancel()
305-
s.TrackSSHConnection(wsInfo, "connect", ErrCreateSSHKey)
306-
ReportSSHAttemptMetrics(ErrCreateSSHKey)
307-
log.WithField("instanceId", wsInfo.InstanceID).WithError(err).Error("failed to create private pair in workspace")
308-
return
309-
}
310-
cancel()
302+
303+
var key ssh.Signer
304+
userName := "gitpod"
311305

312306
session := &Session{
313-
Conn: clientConn,
314-
WorkspaceID: workspaceId,
315-
InstanceID: wsInfo.InstanceID,
316-
OwnerUserId: wsInfo.OwnerUserId,
317-
WorkspacePrivateKey: key,
307+
Conn: clientConn,
308+
WorkspaceID: workspaceId,
309+
InstanceID: wsInfo.InstanceID,
310+
OwnerUserId: wsInfo.OwnerUserId,
318311
}
312+
313+
if wsInfo.SSHKey != nil {
314+
key, err = ssh.ParsePrivateKey([]byte(wsInfo.SSHKey.Private))
315+
if err != nil {
316+
cancel()
317+
return
318+
}
319+
320+
session.WorkspacePrivateKey = key
321+
} else {
322+
key, userName, err = s.GetWorkspaceSSHKey(ctx, wsInfo.IPAddress, supervisorPort)
323+
if err != nil {
324+
cancel()
325+
s.TrackSSHConnection(wsInfo, "connect", ErrCreateSSHKey)
326+
ReportSSHAttemptMetrics(ErrCreateSSHKey)
327+
log.WithField("instanceId", wsInfo.InstanceID).WithError(err).Error("failed to create private pair in workspace")
328+
return
329+
}
330+
331+
session.WorkspacePrivateKey = key
332+
}
333+
334+
cancel()
335+
319336
sshPort := "23001"
320337
if debugWorkspace {
321338
sshPort = "25001"

0 commit comments

Comments
 (0)