Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/cmd/refresh/sshaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func resolveWorkspaceSSH(
if providerHostname := providerSSHHostname(environment.GetInstance(), port.GetHostname()); providerHostname != "" {
workspace.HostSSHHostname = providerHostname
workspace.HostSSHProxyHostname = ""
// The legacy HostSSHPort may be the gateway port, which is not open on the instance itself.
workspace.HostSSHPort = providerSSHPort(environment.GetInstance())
}
return workspace, nil
}
Expand All @@ -181,6 +183,13 @@ func findNetworkPort(networkInfo *devplanev1.EnvironmentNetworkInfo, portID stri
return nil
}

func providerSSHPort(instance *devplanev1.Instance) int {
if port := instance.GetSshPort(); port != 0 {
return int(port)
}
return 22
}

func providerSSHHostname(instance *devplanev1.Instance, workloadHostname string) string {
if instance == nil {
return ""
Expand Down
45 changes: 45 additions & 0 deletions pkg/cmd/refresh/sshaccess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,51 @@ func TestEnrichWorkspacesWithSSHAccess_UsesCurrentUsersPort(t *testing.T) {
}
}

func TestEnrichWorkspacesWithSSHAccess_HostPortIsNeverTheGatewayPort(t *testing.T) {
tests := []struct {
name string
instancePort int32
want int
}{
{name: "unset instance port defaults to 22", instancePort: 0, want: 22},
{name: "uses the instance's own port", instancePort: 2222, want: 2222},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
workspace := entity.Workspace{
ID: "env-1",
Status: entity.Running,
SSHPort: 23126,
HostSSHPort: 23126, // legacy value: the gateway port
}
client := &stubEnvironmentSSHClient{
environment: &devplanev1.Environment{
Instance: &devplanev1.Instance{PublicIp: "203.0.113.10", SshPort: tt.instancePort},
SshAccess: []*devplanev1.SSHAccess{
{UserId: "user-1", LinuxUser: "ubuntu", PortId: "ssh-port"},
},
},
networkInfo: &devplanev1.EnvironmentNetworkInfo{
Ports: []*devplanev1.Port{
{PortId: "ssh-port", Hostname: strPtr("gateway.example.com"), PortNumber: 23126},
},
},
}

got := enrichWorkspacesWithSSHAccess(context.Background(), client, "user-1", []entity.Workspace{workspace})
if got[0].HostSSHHostname != "203.0.113.10" {
t.Fatalf("HostSSHHostname = %q, want the instance IP", got[0].HostSSHHostname)
}
if got[0].HostSSHPort != tt.want {
t.Errorf("HostSSHPort = %d, want %d", got[0].HostSSHPort, tt.want)
}
if got[0].SSHPort != 23126 {
t.Errorf("SSHPort = %d, want gateway port 23126", got[0].SSHPort)
}
})
}
}

func TestEnrichWorkspacesWithSSHAccess_FallsBackOnError(t *testing.T) {
workspace := entity.Workspace{
ID: "env-1",
Expand Down