Skip to content

Commit 9eeb506

Browse files
authored
feat: modify config-ssh to set the host suffix (#17280)
Wires up `config-ssh` command to use a hostname suffix if configured. part of: #16828 e.g. `coder config-ssh --hostname-suffix spiketest` gives: ``` # ------------START-CODER----------- # This section is managed by coder. DO NOT EDIT. # # You should not hand-edit this section unless you are removing it, all # changes will be lost when running "coder config-ssh". # # Last config-ssh options: # :hostname-suffix=spiketest # Host coder.* *.spiketest ConnectTimeout=0 StrictHostKeyChecking=no UserKnownHostsFile=/dev/null LogLevel ERROR ProxyCommand /home/coder/repos/coder/build/coder_config_ssh --global-config /home/coder/.config/coderv2 ssh --stdio --ssh-host-prefix coder. --hostname-suffix spiketest %h # ------------END-CODER------------ ```
1 parent 114ba45 commit 9eeb506

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

cli/configssh.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,15 @@ func (r *RootCmd) configSSH() *serpent.Command {
356356
if sshConfigOpts.disableAutostart {
357357
flags += " --disable-autostart=true"
358358
}
359+
if coderdConfig.HostnamePrefix != "" {
360+
flags += " --ssh-host-prefix " + coderdConfig.HostnamePrefix
361+
}
362+
if coderdConfig.HostnameSuffix != "" {
363+
flags += " --hostname-suffix " + coderdConfig.HostnameSuffix
364+
}
359365
defaultOptions = append(defaultOptions, fmt.Sprintf(
360-
"ProxyCommand %s %s ssh --stdio%s --ssh-host-prefix %s %%h",
361-
escapedCoderBinary, rootFlags, flags, coderdConfig.HostnamePrefix,
366+
"ProxyCommand %s %s ssh --stdio%s %%h",
367+
escapedCoderBinary, rootFlags, flags,
362368
))
363369
}
364370

@@ -391,7 +397,7 @@ func (r *RootCmd) configSSH() *serpent.Command {
391397
}
392398

393399
hostBlock := []string{
394-
"Host " + coderdConfig.HostnamePrefix + "*",
400+
sshConfigHostLinePatterns(coderdConfig),
395401
}
396402
// Prefix with '\t'
397403
for _, v := range configOptions.sshOptions {
@@ -837,3 +843,19 @@ func diffBytes(name string, b1, b2 []byte, color bool) ([]byte, error) {
837843
}
838844
return b, nil
839845
}
846+
847+
func sshConfigHostLinePatterns(config codersdk.SSHConfigResponse) string {
848+
builder := strings.Builder{}
849+
// by inspection, WriteString always returns nil error
850+
_, _ = builder.WriteString("Host")
851+
if config.HostnamePrefix != "" {
852+
_, _ = builder.WriteString(" ")
853+
_, _ = builder.WriteString(config.HostnamePrefix)
854+
_, _ = builder.WriteString("*")
855+
}
856+
if config.HostnameSuffix != "" {
857+
_, _ = builder.WriteString(" *.")
858+
_, _ = builder.WriteString(config.HostnameSuffix)
859+
}
860+
return builder.String()
861+
}

cli/configssh_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,33 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
611611
regexMatch: "RemoteForward 2222 192.168.11.1:2222.*\n.*RemoteForward 2223 192.168.11.1:2223",
612612
},
613613
},
614+
{
615+
name: "Hostname Suffix",
616+
args: []string{
617+
"--yes",
618+
"--hostname-suffix", "testy",
619+
},
620+
wantErr: false,
621+
hasAgent: true,
622+
wantConfig: wantConfig{
623+
ssh: []string{"Host coder.* *.testy"},
624+
regexMatch: `ProxyCommand .* ssh .* --hostname-suffix testy %h`,
625+
},
626+
},
627+
{
628+
name: "Hostname Prefix and Suffix",
629+
args: []string{
630+
"--yes",
631+
"--ssh-host-prefix", "presto.",
632+
"--hostname-suffix", "testy",
633+
},
634+
wantErr: false,
635+
hasAgent: true,
636+
wantConfig: wantConfig{
637+
ssh: []string{"Host presto.* *.testy"},
638+
regexMatch: `ProxyCommand .* ssh .* --ssh-host-prefix presto\. --hostname-suffix testy %h`,
639+
},
640+
},
614641
}
615642
for _, tt := range tests {
616643
tt := tt

0 commit comments

Comments
 (0)