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..638e38a3fee1b 100644 --- a/cli/configssh_test.go +++ b/cli/configssh_test.go @@ -611,6 +611,33 @@ 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{ + 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