Skip to content

Add extra SSH options setting #382

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
Mar 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.coder.gateway

import com.coder.gateway.services.CoderSettingsService
import com.coder.gateway.services.CoderSettingsStateService
import com.coder.gateway.settings.CODER_SSH_CONFIG_OPTIONS
import com.coder.gateway.util.canCreateDirectory
import com.intellij.openapi.components.service
import com.intellij.openapi.options.BoundConfigurable
Expand Down Expand Up @@ -109,6 +110,13 @@ class CoderSettingsConfigurable : BoundConfigurable("Coder") {
CoderGatewayBundle.message("gateway.connector.settings.disable-autostart.comment")
)
}.layout(RowLayout.PARENT_GRID)
row(CoderGatewayBundle.message("gateway.connector.settings.ssh-config-options.title")) {
textArea().resizableColumn().align(AlignX.FILL)
.bindText(state::sshConfigOptions)
.comment(
CoderGatewayBundle.message("gateway.connector.settings.ssh-config-options.comment", CODER_SSH_CONFIG_OPTIONS)
)
}.layout(RowLayout.PARENT_GRID)
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/com/coder/gateway/cli/CoderCLIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ class CoderCLIManager(
if (settings.headerCommand.isNotBlank()) escapeSubcommand(settings.headerCommand) else null,
"ssh", "--stdio",
if (settings.disableAutostart && feats.disableAutostart) "--disable-autostart" else null)
val extraConfig = if (settings.sshConfigOptions.isNotBlank()) {
"\n" + settings.sshConfigOptions.prependIndent(" ")
} else ""
val blockContent = workspaceNames.joinToString(
System.lineSeparator(),
startBlock + System.lineSeparator(),
Expand All @@ -268,7 +271,9 @@ class CoderCLIManager(
UserKnownHostsFile /dev/null
LogLevel ERROR
SetEnv CODER_SSH_SESSION_TYPE=JetBrains
""".trimIndent().replace("\n", System.lineSeparator())
""".trimIndent()
.plus(extraConfig)
.replace("\n", System.lineSeparator())
})

if (contents == null) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/coder/gateway/settings/CoderSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.nio.file.Path
import java.nio.file.Paths

const val CODER_SSH_CONFIG_OPTIONS = "CODER_SSH_CONFIG_OPTIONS";

Check warning on line 17 in src/main/kotlin/com/coder/gateway/settings/CoderSettings.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Redundant semicolon

Redundant semicolon

open class CoderSettingsState(
// Used to download the Coder CLI which is necessary to proxy SSH
// connections. The If-None-Match header will be set to the SHA1 of the CLI
Expand Down Expand Up @@ -57,6 +59,8 @@
// around issues on macOS where it periodically wakes and Gateway
// reconnects, keeping the workspace constantly up.
open var disableAutostart: Boolean = getOS() == OS.MAC,
// Extra SSH config options.
open var sshConfigOptions: String = "",
)

/**
Expand Down Expand Up @@ -113,6 +117,12 @@
val disableAutostart: Boolean
get() = state.disableAutostart

/**
* Extra SSH config to append to each host block.
*/
val sshConfigOptions: String
get() = state.sshConfigOptions.ifBlank { env.get(CODER_SSH_CONFIG_OPTIONS) }

/**
* Where the specified deployment should put its data.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/messages/CoderGatewayBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,9 @@ gateway.connector.settings.disable-autostart.comment=Checking this box will \
cause the plugin to configure the CLI with --disable-autostart. You must go \
through the IDE selection again for the plugin to reconfigure the CLI with \
this setting.
gateway.connector.settings.ssh-config-options.title=SSH config options
gateway.connector.settings.ssh-config-options.comment=Extra SSH config options \
to use when connecting to a workspace. This text will be appended as-is to \
the SSH configuration block for each workspace. If left blank the \
environment variable {0} will be used, if set.

11 changes: 11 additions & 0 deletions src/test/fixtures/outputs/extra-config.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# --- START CODER JETBRAINS test.coder.invalid
Host coder-jetbrains--extra--test.coder.invalid
ProxyCommand /tmp/coder-gateway/test.coder.invalid/coder-linux-amd64 --global-config /tmp/coder-gateway/test.coder.invalid/config ssh --stdio extra
ConnectTimeout 0
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel ERROR
SetEnv CODER_SSH_SESSION_TYPE=JetBrains
ServerAliveInterval 5
ServerAliveCountMax 3
# --- END CODER JETBRAINS test.coder.invalid
57 changes: 35 additions & 22 deletions src/test/kotlin/com/coder/gateway/cli/CoderCLIManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.coder.gateway.cli
import com.coder.gateway.cli.ex.MissingVersionException
import com.coder.gateway.cli.ex.ResponseException
import com.coder.gateway.cli.ex.SSHConfigFormatException
import com.coder.gateway.settings.CoderSettingsState
import com.coder.gateway.settings.CODER_SSH_CONFIG_OPTIONS
import com.coder.gateway.settings.CoderSettings
import com.coder.gateway.settings.CoderSettingsState
import com.coder.gateway.settings.Environment
import com.coder.gateway.util.InvalidVersionException
import com.coder.gateway.util.OS
import com.coder.gateway.util.SemVer
Expand Down Expand Up @@ -238,34 +240,43 @@ internal class CoderCLIManagerTest {
val input: String?,
val output: String,
val remove: String,
val headerCommand: String?,
val headerCommand: String = "",
val disableAutostart: Boolean = false,
val features: Features? = null,
val features: Features = Features(),
val extraConfig: String = "",
val env: Environment = Environment(),
)

@Test
fun testConfigureSSH() {
val extraConfig = listOf(
"ServerAliveInterval 5",
"ServerAliveCountMax 3").joinToString(System.lineSeparator())
val tests = listOf(
SSHTest(listOf("foo", "bar"), null,"multiple-workspaces", "blank", null),
SSHTest(listOf("foo", "bar"), null,"multiple-workspaces", "blank", null),
SSHTest(listOf("foo-bar"), "blank", "append-blank", "blank", null),
SSHTest(listOf("foo-bar"), "blank-newlines", "append-blank-newlines", "blank", null),
SSHTest(listOf("foo-bar"), "existing-end", "replace-end", "no-blocks", null),
SSHTest(listOf("foo-bar"), "existing-end-no-newline", "replace-end-no-newline", "no-blocks", null),
SSHTest(listOf("foo-bar"), "existing-middle", "replace-middle", "no-blocks", null),
SSHTest(listOf("foo-bar"), "existing-middle-and-unrelated", "replace-middle-ignore-unrelated", "no-related-blocks", null),
SSHTest(listOf("foo-bar"), "existing-only", "replace-only", "blank", null),
SSHTest(listOf("foo-bar"), "existing-start", "replace-start", "no-blocks", null),
SSHTest(listOf("foo-bar"), "no-blocks", "append-no-blocks", "no-blocks", null),
SSHTest(listOf("foo-bar"), "no-related-blocks", "append-no-related-blocks", "no-related-blocks", null),
SSHTest(listOf("foo-bar"), "no-newline", "append-no-newline", "no-blocks", null),
SSHTest(listOf("foo", "bar"), null, "multiple-workspaces", "blank"),
SSHTest(listOf("foo", "bar"), null, "multiple-workspaces", "blank"),
SSHTest(listOf("foo-bar"), "blank", "append-blank", "blank"),
SSHTest(listOf("foo-bar"), "blank-newlines", "append-blank-newlines", "blank"),
SSHTest(listOf("foo-bar"), "existing-end", "replace-end", "no-blocks"),
SSHTest(listOf("foo-bar"), "existing-end-no-newline", "replace-end-no-newline", "no-blocks"),
SSHTest(listOf("foo-bar"), "existing-middle", "replace-middle", "no-blocks"),
SSHTest(listOf("foo-bar"), "existing-middle-and-unrelated", "replace-middle-ignore-unrelated", "no-related-blocks"),
SSHTest(listOf("foo-bar"), "existing-only", "replace-only", "blank"),
SSHTest(listOf("foo-bar"), "existing-start", "replace-start", "no-blocks"),
SSHTest(listOf("foo-bar"), "no-blocks", "append-no-blocks", "no-blocks"),
SSHTest(listOf("foo-bar"), "no-related-blocks", "append-no-related-blocks", "no-related-blocks"),
SSHTest(listOf("foo-bar"), "no-newline", "append-no-newline", "no-blocks"),
if (getOS() == OS.WINDOWS) {
SSHTest(listOf("header"), null, "header-command-windows", "blank", """"C:\Program Files\My Header Command\HeaderCommand.exe" --url="%CODER_URL%" --test="foo bar"""")
} else {
SSHTest(listOf("header"), null, "header-command", "blank", "my-header-command --url=\"\$CODER_URL\" --test=\"foo bar\" --literal='\$CODER_URL'")
},
SSHTest(listOf("foo"), null, "disable-autostart", "blank", null, true, Features(true)),
SSHTest(listOf("foo"), null, "no-disable-autostart", "blank", null, true, Features(false)),
SSHTest(listOf("foo"), null, "disable-autostart", "blank", "", true, Features(true)),
SSHTest(listOf("foo"), null, "no-disable-autostart", "blank", "", true, Features(false)),
SSHTest(listOf("extra"), null, "extra-config", "blank",
extraConfig = extraConfig),
SSHTest(listOf("extra"), null, "extra-config", "blank",
env = Environment(mapOf(CODER_SSH_CONFIG_OPTIONS to extraConfig))),
)

val newlineRe = "\r?\n".toRegex()
Expand All @@ -274,8 +285,10 @@ internal class CoderCLIManagerTest {
val settings = CoderSettings(CoderSettingsState(
disableAutostart = it.disableAutostart,
dataDirectory = tmpdir.resolve("configure-ssh").toString(),
headerCommand = it.headerCommand ?: ""),
sshConfigPath = tmpdir.resolve(it.input + "_to_" + it.output + ".conf"))
headerCommand = it.headerCommand,
sshConfigOptions = it.extraConfig),
sshConfigPath = tmpdir.resolve(it.input + "_to_" + it.output + ".conf"),
env = it.env)

val ccm = CoderCLIManager(URL("https://test.coder.invalid"), settings)

Expand All @@ -295,12 +308,12 @@ internal class CoderCLIManagerTest {
.replace("/tmp/coder-gateway/test.coder.invalid/coder-linux-amd64", escape(ccm.localBinaryPath.toString()))

// Add workspaces.
ccm.configSsh(it.workspaces.toSet(), it.features ?: Features())
ccm.configSsh(it.workspaces.toSet(), it.features)

assertEquals(expectedConf, settings.sshConfigPath.toFile().readText())

// Remove configuration.
ccm.configSsh(emptySet(), it.features ?: Features())
ccm.configSsh(emptySet(), it.features)

// Remove is the configuration we expect after removing.
assertEquals(
Expand Down
15 changes: 15 additions & 0 deletions src/test/kotlin/com/coder/gateway/settings/CoderSettingsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ internal class CoderSettingsTest {
assertEquals(Pair("http://test.gateway.coder.com$expected", "fake-token"), got)
}

@Test
fun testSSHConfigOptions() {
var settings = CoderSettings(CoderSettingsState(sshConfigOptions = "ssh config options from state"))
assertEquals("ssh config options from state", settings.sshConfigOptions)

settings = CoderSettings(CoderSettingsState(),
env = Environment(mapOf(CODER_SSH_CONFIG_OPTIONS to "ssh config options from env")))
assertEquals("ssh config options from env", settings.sshConfigOptions)

// State has precedence.
settings = CoderSettings(CoderSettingsState(sshConfigOptions = "ssh config options from state"),
env = Environment(mapOf(CODER_SSH_CONFIG_OPTIONS to "ssh config options from env")))
assertEquals("ssh config options from state", settings.sshConfigOptions)
}

@Test
fun testSettings() {
// Make sure the remaining settings are being conveyed.
Expand Down
Loading