Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit3af8385

Browse files
authored
feat: Enable arbitrary SSH options passed via config-ssh (#410)
* feat: Add option to disable SSH connection cache* Refactor to enable arbitrary options* Generate docs* Add comment for duplicated values
1 parentedc273f commit3af8385

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

‎docs/coder_config-ssh.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ coder config-ssh [flags]
1515
```
1616
--filepath string override the default path of your ssh config file (default "~/.ssh/config")
1717
-h, --help help for config-ssh
18+
-o, --option strings additional options injected in the ssh config (ex. disable caching with "-o ControlPath=none")
1819
--remove remove the auto-generated Coder ssh config
1920
```
2021

‎internal/cmd/configssh.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@ const sshEndToken = "# ------------END-CODER-ENTERPRISE------------"
3333

3434
funcconfigSSHCmd()*cobra.Command {
3535
var (
36-
configpathstring
37-
remove=false
36+
configpathstring
37+
remove=false
38+
additionalOptions []string
3839
)
3940

4041
cmd:=&cobra.Command{
4142
Use:"config-ssh",
4243
Short:"Configure SSH to access Coder workspaces",
4344
Long:"Inject the proper OpenSSH configuration into your local SSH config file.",
44-
RunE:configSSH(&configpath,&remove),
45+
RunE:configSSH(&configpath,&remove,&additionalOptions),
4546
}
4647
cmd.Flags().StringVar(&configpath,"filepath",filepath.Join("~",".ssh","config"),"override the default path of your ssh config file")
48+
cmd.Flags().StringSliceVarP(&additionalOptions,"option","o", []string{},"additional options injected in the ssh config (ex. disable caching with\"-o ControlPath=none\")")
4749
cmd.Flags().BoolVar(&remove,"remove",false,"remove the auto-generated Coder ssh config")
4850

4951
returncmd
5052
}
5153

52-
funcconfigSSH(configpath*string,remove*bool)func(cmd*cobra.Command,_ []string)error {
54+
funcconfigSSH(configpath*string,remove*bool,additionalOptions*[]string)func(cmd*cobra.Command,_ []string)error {
5355
returnfunc(cmd*cobra.Command,_ []string)error {
5456
ctx:=cmd.Context()
5557
usr,err:=user.Current()
@@ -118,7 +120,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
118120
returnxerrors.Errorf("Failed to get executable path: %w",err)
119121
}
120122

121-
newConfig:=makeNewConfigs(binPath,workspacesWithProviders,privateKeyFilepath)
123+
newConfig:=makeNewConfigs(binPath,workspacesWithProviders,privateKeyFilepath,*additionalOptions)
122124

123125
err=os.MkdirAll(filepath.Dir(*configpath),os.ModePerm)
124126
iferr!=nil {
@@ -226,7 +228,7 @@ func writeSSHKey(ctx context.Context, client coder.Client, privateKeyPath string
226228
returnioutil.WriteFile(privateKeyPath, []byte(key.PrivateKey),0600)
227229
}
228230

229-
funcmakeNewConfigs(binPathstring,workspaces []coderutil.WorkspaceWithWorkspaceProvider,privateKeyFilepathstring)string {
231+
funcmakeNewConfigs(binPathstring,workspaces []coderutil.WorkspaceWithWorkspaceProvider,privateKeyFilepathstring,additionalOptions []string)string {
230232
newConfig:=fmt.Sprintf("\n%s\n%s\n\n",sshStartToken,sshStartMessage)
231233

232234
sort.Slice(workspaces,func(i,jint)bool {returnworkspaces[i].Workspace.Name<workspaces[j].Workspace.Name })
@@ -240,32 +242,41 @@ func makeNewConfigs(binPath string, workspaces []coderutil.WorkspaceWithWorkspac
240242
continue
241243
}
242244

243-
newConfig+=makeSSHConfig(binPath,workspace.Workspace.Name,privateKeyFilepath)
245+
newConfig+=makeSSHConfig(binPath,workspace.Workspace.Name,privateKeyFilepath,additionalOptions)
244246
}
245247
newConfig+=fmt.Sprintf("\n%s\n",sshEndToken)
246248

247249
returnnewConfig
248250
}
249251

250-
funcmakeSSHConfig(binPath,workspaceName,privateKeyFilepathstring)string {
251-
entry:=fmt.Sprintf(
252-
`Host coder.%s
253-
HostName coder.%s
254-
ProxyCommand "%s" tunnel %s 12213 stdio
255-
StrictHostKeyChecking no
256-
ConnectTimeout=0
257-
IdentitiesOnly yes
258-
IdentityFile="%s"
259-
`,workspaceName,workspaceName,binPath,workspaceName,privateKeyFilepath)
252+
funcmakeSSHConfig(binPath,workspaceName,privateKeyFilepathstring,additionalOptions []string)string {
253+
// Custom user options come first to maximizessh customization.
254+
options:= []string{}
255+
iflen(additionalOptions)>0 {
256+
options= []string{
257+
"# Custom options. Duplicated values will always prefer the first!",
258+
}
259+
options=append(options,additionalOptions...)
260+
options=append(options,"# End custom options.")
261+
}
262+
options=append(options,
263+
fmt.Sprintf("HostName coder.%s",workspaceName),
264+
fmt.Sprintf("ProxyCommand %q tunnel %s 12213 stdio",binPath,workspaceName),
265+
"StrictHostKeyChecking no",
266+
"ConnectTimeout=0",
267+
"IdentitiesOnly yes",
268+
fmt.Sprintf("IdentityFile=%q",privateKeyFilepath),
269+
)
260270

261271
ifruntime.GOOS=="linux"||runtime.GOOS=="darwin" {
262-
entry+=` ControlMaster auto
263-
ControlPath ~/.ssh/.connection-%r@%h:%p
264-
ControlPersist 600
265-
`
272+
options=append(options,
273+
"ControlMaster auto",
274+
"ControlPath ~/.ssh/.connection-%r@%h:%p",
275+
"ControlPersist 600",
276+
)
266277
}
267278

268-
returnentry
279+
returnfmt.Sprintf("Host coder.%s\n\t%s\n\n",workspaceName,strings.Join(options,"\n\t"))
269280
}
270281

271282
funcwriteStr(filename,datastring)error {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp