-
Notifications
You must be signed in to change notification settings - Fork 901
feat(cli): support header and header-command in config-ssh #10413
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
42a7512
feat(configssh): add any provided header command to the SSH config also
JoshVee 9a9ff38
chore: escape command also
JoshVee 57355fb
Merge branch 'main' into joshvee-ssh-header-command
JoshVee 1220039
chore: fix up the variable name
JoshVee a785392
chore: more test cases
JoshVee c5e0ec1
chore: fix tests
JoshVee 7fc1849
Merge branch 'coder:main' into joshvee-ssh-header-command
JoshVee ed5e82b
Merge branch 'main' into joshvee-ssh-header-command
mafredri 1c585c4
remove echoResponses
mafredri bcdb129
add support for serializing header and header-command
mafredri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"github.com/cli/safeexec" | ||
"github.com/pkg/diff" | ||
"github.com/pkg/diff/write" | ||
"golang.org/x/exp/constraints" | ||
"golang.org/x/exp/slices" | ||
"golang.org/x/sync/errgroup" | ||
"golang.org/x/xerrors" | ||
|
@@ -51,6 +52,8 @@ type sshConfigOptions struct { | |
userHostPrefix string | ||
sshOptions []string | ||
disableAutostart bool | ||
header []string | ||
headerCommand string | ||
} | ||
|
||
// addOptions expects options in the form of "option=value" or "option value". | ||
|
@@ -100,15 +103,25 @@ func (o *sshConfigOptions) addOption(option string) error { | |
} | ||
|
||
func (o sshConfigOptions) equal(other sshConfigOptions) bool { | ||
// Compare without side-effects or regard to order. | ||
opt1 := slices.Clone(o.sshOptions) | ||
sort.Strings(opt1) | ||
opt2 := slices.Clone(other.sshOptions) | ||
sort.Strings(opt2) | ||
if !slices.Equal(opt1, opt2) { | ||
if !slicesSortedEqual(o.sshOptions, other.sshOptions) { | ||
return false | ||
} | ||
return o.waitEnum == other.waitEnum && o.userHostPrefix == other.userHostPrefix && o.disableAutostart == other.disableAutostart | ||
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 | ||
} | ||
|
||
// slicesSortedEqual compares two slices without side-effects or regard to order. | ||
func slicesSortedEqual[S ~[]E, E constraints.Ordered](a, b S) bool { | ||
if len(a) != len(b) { | ||
return false | ||
} | ||
a = slices.Clone(a) | ||
slices.Sort(a) | ||
b = slices.Clone(b) | ||
slices.Sort(b) | ||
return slices.Equal(a, b) | ||
} | ||
|
||
func (o sshConfigOptions) asList() (list []string) { | ||
|
@@ -124,6 +137,13 @@ func (o sshConfigOptions) asList() (list []string) { | |
for _, opt := range o.sshOptions { | ||
list = append(list, fmt.Sprintf("ssh-option: %s", opt)) | ||
} | ||
for _, h := range o.header { | ||
list = append(list, fmt.Sprintf("header: %s", h)) | ||
} | ||
if o.headerCommand != "" { | ||
list = append(list, fmt.Sprintf("header-command: %s", o.headerCommand)) | ||
} | ||
|
||
return list | ||
} | ||
|
||
|
@@ -230,6 +250,8 @@ func (r *RootCmd) configSSH() *clibase.Cmd { | |
// specifies skip-proxy-command, then wait cannot be applied. | ||
return xerrors.Errorf("cannot specify both --skip-proxy-command and --wait") | ||
} | ||
sshConfigOpts.header = r.header | ||
sshConfigOpts.headerCommand = r.headerCommand | ||
|
||
recvWorkspaceConfigs := sshPrepareWorkspaceConfigs(inv.Context(), client) | ||
|
||
|
@@ -393,6 +415,14 @@ func (r *RootCmd) configSSH() *clibase.Cmd { | |
} | ||
|
||
if !skipProxyCommand { | ||
rootFlags := fmt.Sprintf("--global-config %s", escapedGlobalConfig) | ||
for _, h := range sshConfigOpts.header { | ||
rootFlags += fmt.Sprintf(" --header %q", h) | ||
} | ||
if sshConfigOpts.headerCommand != "" { | ||
rootFlags += fmt.Sprintf(" --header-command %q", sshConfigOpts.headerCommand) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review: We shouldn't need to do special escaping here like we do for the coder binary, that's because we're simply passing a string argument to coder. |
||
} | ||
|
||
flags := "" | ||
if sshConfigOpts.waitEnum != "auto" { | ||
flags += " --wait=" + sshConfigOpts.waitEnum | ||
|
@@ -401,8 +431,8 @@ func (r *RootCmd) configSSH() *clibase.Cmd { | |
flags += " --disable-autostart=true" | ||
} | ||
defaultOptions = append(defaultOptions, fmt.Sprintf( | ||
"ProxyCommand %s --global-config %s ssh --stdio%s %s", | ||
escapedCoderBinary, escapedGlobalConfig, flags, workspaceHostname, | ||
"ProxyCommand %s %s ssh --stdio%s %s", | ||
escapedCoderBinary, rootFlags, flags, workspaceHostname, | ||
)) | ||
} | ||
|
||
|
@@ -623,6 +653,12 @@ func sshConfigWriteSectionHeader(w io.Writer, addNewline bool, o sshConfigOption | |
for _, opt := range o.sshOptions { | ||
_, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "ssh-option", opt) | ||
} | ||
for _, h := range o.header { | ||
_, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "header", h) | ||
} | ||
if o.headerCommand != "" { | ||
_, _ = fmt.Fprintf(&ow, "# :%s=%s\n", "header-command", o.headerCommand) | ||
} | ||
if ow.Len() > 0 { | ||
_, _ = fmt.Fprint(w, sshConfigOptionsHeader) | ||
_, _ = fmt.Fprint(w, ow.String()) | ||
|
@@ -654,6 +690,10 @@ func sshConfigParseLastOptions(r io.Reader) (o sshConfigOptions) { | |
o.sshOptions = append(o.sshOptions, parts[1]) | ||
case "disable-autostart": | ||
o.disableAutostart, _ = strconv.ParseBool(parts[1]) | ||
case "header": | ||
o.header = append(o.header, parts[1]) | ||
case "header-command": | ||
o.headerCommand = parts[1] | ||
default: | ||
// Unknown option, ignore. | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: curious, can we replace
slices.Clone
with a simple copy to omit cloning and just operate on same objects?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually just a simple copy, so I think it's fine.