@@ -179,8 +179,9 @@ class CoderCLIManager @JvmOverloads constructor(
179
179
/* *
180
180
* Configure SSH to use this binary.
181
181
*/
182
- fun configSsh (workspaces : List <WorkspaceAgentModel >) {
183
- writeSSHConfig(modifySSHConfig(readSSHConfig(), workspaces))
182
+ @JvmOverloads
183
+ fun configSsh (workspaces : List <WorkspaceAgentModel >, headerCommand : String? = null) {
184
+ writeSSHConfig(modifySSHConfig(readSSHConfig(), workspaces, headerCommand))
184
185
}
185
186
186
187
/* *
@@ -194,16 +195,35 @@ class CoderCLIManager @JvmOverloads constructor(
194
195
}
195
196
}
196
197
198
+ /* *
199
+ * Escape a command argument by wrapping it in double quotes and escaping
200
+ * any double quotes in the argument. For example, echo "test" becomes
201
+ * "echo \"test\"".
202
+ */
203
+ private fun escape (s : String ): String {
204
+ return " \" " + s.replace(" \" " , " \\\" " ) + " \" "
205
+ }
206
+
197
207
/* *
198
208
* Given an existing SSH config modify it to add or remove the config for
199
209
* this deployment and return the modified config or null if it does not
200
210
* need to be modified.
201
211
*/
202
- private fun modifySSHConfig (contents : String? , workspaces : List <WorkspaceAgentModel >): String? {
212
+ private fun modifySSHConfig (
213
+ contents : String? ,
214
+ workspaces : List <WorkspaceAgentModel >,
215
+ headerCommand : String? ,
216
+ ): String? {
203
217
val host = getSafeHost(deploymentURL)
204
218
val startBlock = " # --- START CODER JETBRAINS $host "
205
219
val endBlock = " # --- END CODER JETBRAINS $host "
206
220
val isRemoving = workspaces.isEmpty()
221
+ val proxyArgs = listOfNotNull(
222
+ escape(localBinaryPath.toString()),
223
+ " --global-config" , escape(coderConfigPath.toString()),
224
+ if (! headerCommand.isNullOrBlank()) " --header-command" else null ,
225
+ if (! headerCommand.isNullOrBlank()) escape(headerCommand) else null ,
226
+ " ssh" , " --stdio" )
207
227
val blockContent = workspaces.joinToString(
208
228
System .lineSeparator(),
209
229
startBlock + System .lineSeparator(),
@@ -212,7 +232,7 @@ class CoderCLIManager @JvmOverloads constructor(
212
232
"""
213
233
Host ${getHostName(deploymentURL, it)}
214
234
HostName coder.${it.name}
215
- ProxyCommand " $localBinaryPath " --global-config " $coderConfigPath " ssh --stdio ${it.name}
235
+ ProxyCommand ${proxyArgs.joinToString( " " )} ${it.name}
216
236
ConnectTimeout 0
217
237
StrictHostKeyChecking no
218
238
UserKnownHostsFile /dev/null
0 commit comments