From 88c9a41c224926395c4f8f32c37d2aed8a24d26f Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Mon, 7 Apr 2025 13:11:33 +0000 Subject: [PATCH 1/3] feat: modify config-ssh to set the host suffix --- cli/configssh.go | 28 +++++++++++++++++++++++++--- cli/configssh_test.go | 12 ++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cli/configssh.go b/cli/configssh.go index 67fbd19ef3f69..6a0f41c2a2fbc 100644 --- a/cli/configssh.go +++ b/cli/configssh.go @@ -356,9 +356,15 @@ func (r *RootCmd) configSSH() *serpent.Command { if sshConfigOpts.disableAutostart { flags += " --disable-autostart=true" } + if coderdConfig.HostnamePrefix != "" { + flags += " --ssh-host-prefix " + coderdConfig.HostnamePrefix + } + if coderdConfig.HostnameSuffix != "" { + flags += " --hostname-suffix " + coderdConfig.HostnameSuffix + } defaultOptions = append(defaultOptions, fmt.Sprintf( - "ProxyCommand %s %s ssh --stdio%s --ssh-host-prefix %s %%h", - escapedCoderBinary, rootFlags, flags, coderdConfig.HostnamePrefix, + "ProxyCommand %s %s ssh --stdio%s %%h", + escapedCoderBinary, rootFlags, flags, )) } @@ -391,7 +397,7 @@ func (r *RootCmd) configSSH() *serpent.Command { } hostBlock := []string{ - "Host " + coderdConfig.HostnamePrefix + "*", + sshConfigHostLinePatterns(coderdConfig), } // Prefix with '\t' for _, v := range configOptions.sshOptions { @@ -837,3 +843,19 @@ func diffBytes(name string, b1, b2 []byte, color bool) ([]byte, error) { } return b, nil } + +func sshConfigHostLinePatterns(config codersdk.SSHConfigResponse) string { + builder := strings.Builder{} + // by inspection, WriteString always returns nil error + _, _ = builder.WriteString("Host") + if config.HostnamePrefix != "" { + _, _ = builder.WriteString(" ") + _, _ = builder.WriteString(config.HostnamePrefix) + _, _ = builder.WriteString("*") + } + if config.HostnameSuffix != "" { + _, _ = builder.WriteString(" *.") + _, _ = builder.WriteString(config.HostnameSuffix) + } + return builder.String() +} diff --git a/cli/configssh_test.go b/cli/configssh_test.go index 84399ddc67949..97e40146a7a33 100644 --- a/cli/configssh_test.go +++ b/cli/configssh_test.go @@ -611,6 +611,18 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { regexMatch: "RemoteForward 2222 192.168.11.1:2222.*\n.*RemoteForward 2223 192.168.11.1:2223", }, }, + { + name: "Hostname Suffix", + args: []string{ + "--yes", + "--hostname-suffix", "testy", + }, + wantErr: false, + hasAgent: true, + wantConfig: wantConfig{ + regexMatch: `ProxyCommand .* ssh .* --hostname-suffix testy %h`, + }, + }, } for _, tt := range tests { tt := tt From 35b4021c9c6250a2986918db2944db3c765bf971 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Mon, 7 Apr 2025 17:18:15 +0000 Subject: [PATCH 2/3] additional testing --- cli/configssh_test.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cli/configssh_test.go b/cli/configssh_test.go index 97e40146a7a33..80a4ba11e2894 100644 --- a/cli/configssh_test.go +++ b/cli/configssh_test.go @@ -509,7 +509,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { }, }, { - name: "Start/End out of order", + name: "Start/End out of order", matches: []match{ // {match: "Continue?", write: "yes"}, }, @@ -524,7 +524,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { wantErr: true, }, { - name: "Multiple sections", + name: "Multiple sections", matches: []match{ // {match: "Continue?", write: "yes"}, }, @@ -620,9 +620,24 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { wantErr: false, hasAgent: true, wantConfig: wantConfig{ + ssh: []string{"Host coder.* *.testy"}, regexMatch: `ProxyCommand .* ssh .* --hostname-suffix testy %h`, }, }, + { + name: "Hostname Prefix and Suffix", + args: []string{ + "--yes", + "--ssh-host-prefix", "presto.", + "--hostname-suffix", "testy", + }, + wantErr: false, + hasAgent: true, + wantConfig: wantConfig{ + ssh: []string{"Host presto.* *.testy"}, + regexMatch: `ProxyCommand .* ssh .* --ssh-host-prefix presto\. --hostname-suffix testy %h`, + }, + }, } for _, tt := range tests { tt := tt From b78738cd163e2be8ea8eb143dcf2887f4589df21 Mon Sep 17 00:00:00 2001 From: Spike Curtis Date: Tue, 8 Apr 2025 07:37:49 +0000 Subject: [PATCH 3/3] appease linter --- cli/configssh_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/configssh_test.go b/cli/configssh_test.go index 80a4ba11e2894..638e38a3fee1b 100644 --- a/cli/configssh_test.go +++ b/cli/configssh_test.go @@ -509,7 +509,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { }, }, { - name: "Start/End out of order", + name: "Start/End out of order", matches: []match{ // {match: "Continue?", write: "yes"}, }, @@ -524,7 +524,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) { wantErr: true, }, { - name: "Multiple sections", + name: "Multiple sections", matches: []match{ // {match: "Continue?", write: "yes"}, },