8000 feat: modify config-ssh to set the host suffix by spikecurtis · Pull Request #17280 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: modify config-ssh to set the host suffix #17280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
8000 Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: modify config-ssh to set the host suffix
  • Loading branch information
spikecurtis committed Apr 8, 2025
commit 88c9a41c224926395c4f8f32c37d2aed8a24d26f
28 changes: 25 additions & 3 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
12 changes: 12 additions & 0 deletions cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
0