Skip to content

feat: add hostname-suffix option to config-ssh #17270

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 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ const (
// sshConfigOptions represents options that can be stored and read
// from the coder config in ~/.ssh/coder.
type sshConfigOptions struct {
waitEnum string
waitEnum string
// Deprecated: moving away from prefix to hostnameSuffix
userHostPrefix string
hostnameSuffix string
sshOptions []string
disableAutostart bool
header []string
Expand Down Expand Up @@ -97,7 +99,11 @@ func (o sshConfigOptions) equal(other sshConfigOptions) bool {
if !slicesSortedEqual(o.header, other.header) {
return false
}
return o.waitEnum == other.waitEnum && o.userHostPrefix == other.userHostPrefix && o.disableAutostart == other.disableAutostart && o.headerCommand == other.headerCommand
return o.waitEnum == other.waitEnum &&
o.userHostPrefix == other.userHostPrefix &&
o.disableAutostart == other.disableAutostart &&
o.headerCommand == other.headerCommand &&
o.hostnameSuffix == other.hostnameSuffix
}

// slicesSortedEqual compares two slices without side-effects or regard to order.
Expand All @@ -119,6 +125,9 @@ func (o sshConfigOptions) asList() (list []string) {
if o.userHostPrefix != "" {
list = append(list, fmt.Sprintf("ssh-host-prefix: %s", o.userHostPrefix))
}
if o.hostnameSuffix != "" {
list = append(list, fmt.Sprintf("hostname-suffix: %s", o.hostnameSuffix))
}
if o.disableAutostart {
list = append(list, fmt.Sprintf("disable-autostart: %v", o.disableAutostart))
}
Expand Down Expand Up @@ -314,6 +323,10 @@ func (r *RootCmd) configSSH() *serpent.Command {
// Override with user flag.
coderdConfig.HostnamePrefix = sshConfigOpts.userHostPrefix
}
if sshConfigOpts.hostnameSuffix != "" {
// Override with user flag.
coderdConfig.HostnameSuffix = sshConfigOpts.hostnameSuffix
}

// Write agent configuration.
defaultOptions := []string{
Expand Down Expand Up @@ -518,6 +531,12 @@ func (r *RootCmd) configSSH() *serpent.Command {
Description: "Override the default host prefix.",
Value: serpent.StringOf(&sshConfigOpts.userHostPrefix),
},
{
Flag: "hostname-suffix",
Env: "CODER_CONFIGSSH_HOSTNAME_SUFFIX",
Description: "Override the default hostname suffix.",
Value: serpent.StringOf(&sshConfigOpts.hostnameSuffix),
},
{
Flag: "wait",
Env: "CODER_CONFIGSSH_WAIT", // Not to be mixed with CODER_SSH_WAIT.
Expand Down Expand Up @@ -568,6 +587,9 @@ func sshConfigWriteSectionHeader(w io.Writer, addNewline bool, o sshConfigOption
if o.userHostPrefix != "" {
_, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "ssh-host-prefix", o.userHostPrefix)
}
if o.hostnameSuffix != "" {
_, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "hostname-suffix", o.hostnameSuffix)
}
if o.disableAutostart {
_, _ = fmt.Fprintf(&ow, "# :%s=%v\n", "disable-autostart", o.disableAutostart)
}
Expand Down Expand Up @@ -607,6 +629,8 @@ func sshConfigParseLastOptions(r io.Reader) (o sshConfigOptions) {
o.waitEnum = parts[1]
case "ssh-host-prefix":
o.userHostPrefix = parts[1]
case "hostname-suffix":
o.hostnameSuffix = parts[1]
case "ssh-option":
o.sshOptions = append(o.sshOptions, parts[1])
case "disable-autostart":
Expand Down
2 changes: 2 additions & 0 deletions cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
"# Last config-ssh options:",
"# :wait=yes",
"# :ssh-host-prefix=coder-test.",
"# :hostname-suffix=coder-suffix",
"# :header=X-Test-Header=foo",
"# :header=X-Test-Header2=bar",
"# :header-command=printf h1=v1 h2=\"v2\" h3='v3'",
Expand All @@ -447,6 +448,7 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
"--yes",
"--wait=yes",
"--ssh-host-prefix", "coder-test.",
"--hostname-suffix", "coder-suffix",
"--header", "X-Test-Header=foo",
"--header", "X-Test-Header2=bar",
"--header-command", "printf h1=v1 h2=\"v2\" h3='v3'",
Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_config-ssh_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ OPTIONS:
unix-like shell. This flag forces the use of unix file paths (the
forward slash '/').

--hostname-suffix string, $CODER_CONFIGSSH_HOSTNAME_SUFFIX
Override the default hostname suffix.

--ssh-config-file string, $CODER_SSH_CONFIG_FILE (default: ~/.ssh/config)
Specifies the path to an SSH config.

Expand Down
9 changes: 9 additions & 0 deletions docs/reference/cli/config-ssh.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading