From 5daa96eb9fa27d0f45e030c48119a0e613069288 Mon Sep 17 00:00:00 2001 From: Rudra184 Date: Sat, 14 Nov 2026 09:22:25 +0530 Subject: [PATCH] fix: use the instance SSH port for -host in brev refresh After refresh enrichment, HostSSHHostname is overridden with the instance's public IP/DNS, but HostSSHPort kept its legacy value. For VM-mode instances that value is the gateway port, which is not open on the instance, so `ssh -host` timed out. Set HostSSHPort together with the hostname override, from the instance's reported SSH port and falling back to 22 when unset. SSHHostname and SSHPort (the gateway pair) are unchanged. Fixes #531 Signed-off-by: Rudra184 --- pkg/cmd/refresh/sshaccess.go | 9 +++++++ pkg/cmd/refresh/sshaccess_test.go | 45 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/pkg/cmd/refresh/sshaccess.go b/pkg/cmd/refresh/sshaccess.go index caee70e3..a2ce1766 100644 --- a/pkg/cmd/refresh/sshaccess.go +++ b/pkg/cmd/refresh/sshaccess.go @@ -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 } @@ -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 "" diff --git a/pkg/cmd/refresh/sshaccess_test.go b/pkg/cmd/refresh/sshaccess_test.go index 9ad7e2c9..b80ba953 100644 --- a/pkg/cmd/refresh/sshaccess_test.go +++ b/pkg/cmd/refresh/sshaccess_test.go @@ -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",