mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
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------------ ```
This commit is contained in:
+25
-3
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user