Skip to content

Commit 1e90684

Browse files
committed
fix: ssh connection when wildcard config is enabled on Coder < 2.19.x
- Coder versions like 2.18.x and before that don't support the wildcard config. In this case, enabling this setting is ignored when generating the ssh config. - however the hostname passed to the Toolbox ssh connection was not taking into account that some versions don't support wildcard config. - this corner case is now covered by encapsulating the hostname resolving logic and taking into account the cli version.
1 parent 0cece98 commit 1e90684

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

src/main/kotlin/com/coder/toolbox/CoderRemoteEnvironment.kt

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.coder.toolbox
22

33
import com.coder.toolbox.browser.BrowserUtil
4+
import com.coder.toolbox.cli.CoderCLIManager
45
import com.coder.toolbox.models.WorkspaceAndAgentStatus
56
import com.coder.toolbox.sdk.CoderRestClient
67
import com.coder.toolbox.sdk.ex.APIResponseException
@@ -35,6 +36,7 @@ import kotlin.time.Duration.Companion.seconds
3536
class CoderRemoteEnvironment(
3637
private val context: CoderToolboxContext,
3738
private val client: CoderRestClient,
39+
private val cli: CoderCLIManager,
3840
private var workspace: Workspace,
3941
private var agent: WorkspaceAgent,
4042
) : RemoteProviderEnvironment("${workspace.name}.${agent.name}"), BeforeConnectionHook, AfterDisconnectHook {
@@ -155,6 +157,7 @@ class CoderRemoteEnvironment(
155157
fun getContentsView(): EnvironmentContentsView = EnvironmentView(
156158
context.settingsStore.readOnly(),
157159
client.url,
160+
cli,
158161
workspace,
159162
agent
160163
)

src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class CoderRemoteProvider(
9191
it.name
9292
}?.map { agent ->
9393
// If we have an environment already, update that.
94-
val env = CoderRemoteEnvironment(context, client, ws, agent)
94+
val env = CoderRemoteEnvironment(context, client, cli, ws, agent)
9595
lastEnvironments.firstOrNull { it == env }?.let {
9696
it.update(ws, agent)
9797
it

src/main/kotlin/com/coder/toolbox/cli/CoderCLIManager.kt

+14-12
Original file line numberDiff line numberDiff line change
@@ -511,25 +511,27 @@ class CoderCLIManager(
511511
}
512512
}
513513

514+
fun getHostname(url: URL, ws: Workspace, agent: WorkspaceAgent): String {
515+
return if (settings.isSshWildcardConfigEnabled && features.wildcardSsh) {
516+
"${getHostnamePrefix(url)}--${ws.ownerName}--${ws.name}.${agent.name}"
517+
} else {
518+
"coder-jetbrains-toolbox--${ws.ownerName}--${ws.name}.${agent.name}--${url.safeHost()}"
519+
}
520+
}
521+
522+
fun getBackgroundHostname(url: URL, ws: Workspace, agent: WorkspaceAgent): String {
523+
return "${getHostname(url, ws, agent)}--bg"
524+
}
525+
514526
companion object {
515527
private val tokenRegex = "--token [^ ]+".toRegex()
516528

517529
private fun getHostnamePrefix(url: URL): String = "coder-jetbrains-toolbox-${url.safeHost()}"
518530

519531
private fun getBackgroundHostnamePrefix(url: URL): String = "coder-jetbrains-toolbox-${url.safeHost()}-bg"
520532

521-
fun getWildcardHostname(url: URL, ws: Workspace, agent: WorkspaceAgent): String =
522-
"${getHostnamePrefix(url)}--${ws.ownerName}--${ws.name}.${agent.name}"
523-
524-
fun getHostname(url: URL, ws: Workspace, agent: WorkspaceAgent): String {
525-
return "coder-jetbrains-toolbox--${ws.ownerName}--${ws.name}.${agent.name}--${url.safeHost()}"
526-
}
527-
528-
fun getBackgroundHostname(url: URL, ws: Workspace, agent: WorkspaceAgent): String {
529-
return "${getHostname(url, ws, agent)}--bg"
530-
}
531-
532-
private fun getWsByOwner(ws: Workspace, agent: WorkspaceAgent): String = "${ws.ownerName}/${ws.name}.${agent.name}"
533+
private fun getWsByOwner(ws: Workspace, agent: WorkspaceAgent): String =
534+
"${ws.ownerName}/${ws.name}.${agent.name}"
533535

534536
private fun Pair<Workspace, WorkspaceAgent>.workspace() = this.first
535537

src/main/kotlin/com/coder/toolbox/views/EnvironmentView.kt

+3-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ import java.net.URL
1919
class EnvironmentView(
2020
private val settings: ReadOnlyCoderSettings,
2121
private val url: URL,
22+
private val cli: CoderCLIManager,
2223
private val workspace: Workspace,
2324
private val agent: WorkspaceAgent,
2425
) : SshEnvironmentContentsView {
2526
override suspend fun getConnectionInfo(): SshConnectionInfo = object : SshConnectionInfo {
2627
/**
2728
* The host name generated by the cli manager for this workspace.
2829
*/
29-
override val host: String = resolveHost()
30+
override val host: String = cli.getHostname(url, workspace, agent)
3031

3132
/**
3233
* The port is ignored by the Coder proxy command.
@@ -37,11 +38,5 @@ class EnvironmentView(
3738
* The username is ignored by the Coder proxy command.
3839
*/
3940
override val userName: String? = null
40-
4141
}
42-
43-
private fun resolveHost(): String =
44-
if (settings.isSshWildcardConfigEnabled)
45-
CoderCLIManager.getWildcardHostname(url, workspace, agent)
46-
else CoderCLIManager.getHostname(url, workspace, agent)
47-
}
42+
}

0 commit comments

Comments
 (0)