Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2972fe6

Browse files
committedMay 9, 2023
Allow configuring CLI directory separately from data
This is so admins can download the CLI to some restricted location (like ProgramFiles). This will break if they upload the wrong version and the plugin tries to download a new one since it will not have permissions to do so.
1 parent 59dac3d commit 2972fe6

File tree

6 files changed

+42
-17
lines changed

6 files changed

+42
-17
lines changed
 

‎src/main/kotlin/com/coder/gateway/CoderSettingsConfigurable.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,29 @@ class CoderSettingsConfigurable : BoundConfigurable("Coder") {
2929
)
3030
)
3131
}
32-
row(CoderGatewayBundle.message("gateway.connector.settings.binary-destination.title")) {
32+
row(CoderGatewayBundle.message("gateway.connector.settings.data-directory.title")) {
3333
textField().resizableColumn().align(AlignX.FILL)
34-
.bindText(state::binaryDestination)
35-
.validationOnApply(validateBinaryDestination())
36-
.validationOnInput(validateBinaryDestination())
34+
.bindText(state::dataDirectory)
35+
.validationOnApply(validateDataDirectory())
36+
.validationOnInput(validateDataDirectory())
3737
.comment(
3838
CoderGatewayBundle.message(
39-
"gateway.connector.settings.binary-destination.comment",
39+
"gateway.connector.settings.data-directory.comment",
4040
CoderCLIManager.getDataDir(),
4141
)
4242
)
4343
}
44+
// The binary destination is not validated because it could be a
45+
// read-only path that is pre-downloaded by admins.
46+
row(CoderGatewayBundle.message("gateway.connector.settings.binary-destination.title")) {
47+
textField().resizableColumn().align(AlignX.FILL)
48+
.bindText(state::binaryDestination)
49+
.comment(CoderGatewayBundle.message("gateway.connector.settings.binary-destination.comment"))
50+
}
4451
}
4552
}
4653

47-
private fun validateBinaryDestination(): ValidationInfoBuilder.(JBTextField) -> ValidationInfo? = {
54+
private fun validateDataDirectory(): ValidationInfoBuilder.(JBTextField) -> ValidationInfo? = {
4855
if (it.text.isNotBlank() && !Path.of(it.text).canCreateDirectory()) {
4956
error("Cannot create this directory")
5057
} else {

‎src/main/kotlin/com/coder/gateway/sdk/CoderCLIManager.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import javax.xml.bind.annotation.adapters.HexBinaryAdapter
2525
*/
2626
class CoderCLIManager @JvmOverloads constructor(
2727
private val deploymentURL: URL,
28-
destinationDir: Path,
28+
dataDir: Path,
29+
cliDir: Path? = null,
2930
remoteBinaryURLOverride: String? = null,
3031
private val sshConfigPath: Path = Path.of(System.getProperty("user.home")).resolve(".ssh/config"),
3132
) {
@@ -51,8 +52,8 @@ class CoderCLIManager @JvmOverloads constructor(
5152
}
5253
val host = getSafeHost(deploymentURL)
5354
val subdir = if (deploymentURL.port > 0) "${host}-${deploymentURL.port}" else host
54-
localBinaryPath = destinationDir.resolve(subdir).resolve(binaryName).toAbsolutePath()
55-
coderConfigPath = destinationDir.resolve(subdir).resolve("config").toAbsolutePath()
55+
localBinaryPath = (cliDir ?: dataDir).resolve(subdir).resolve(binaryName).toAbsolutePath()
56+
coderConfigPath = dataDir.resolve(subdir).resolve("config").toAbsolutePath()
5657
}
5758

5859
/**

‎src/main/kotlin/com/coder/gateway/services/CoderSettingsState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.intellij.util.xmlb.XmlSerializerUtil
1515
class CoderSettingsState : PersistentStateComponent<CoderSettingsState> {
1616
var binarySource: String = ""
1717
var binaryDestination: String = ""
18+
var dataDirectory: String = ""
1819
override fun getState(): CoderSettingsState {
1920
return this
2021
}

‎src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,9 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
456456
) {
457457
val cliManager = CoderCLIManager(
458458
deploymentURL,
459-
if (settings.binaryDestination.isNotBlank()) Path.of(settings.binaryDestination)
459+
if (settings.dataDirectory.isNotBlank()) Path.of(settings.dataDirectory)
460460
else CoderCLIManager.getDataDir(),
461+
if (settings.binaryDestination.isNotBlank()) Path.of(settings.binaryDestination) else null,
461462
settings.binarySource,
462463
)
463464
try {
@@ -701,8 +702,10 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
701702
logger.info("Configuring Coder CLI...")
702703
val cliManager = CoderCLIManager(
703704
wizardModel.coderURL.toURL(),
704-
if (settings.binaryDestination.isNotBlank()) Path.of(settings.binaryDestination)
705+
if (settings.dataDirectory.isNotBlank()) Path.of(settings.dataDirectory)
705706
else CoderCLIManager.getDataDir(),
707+
if (settings.binaryDestination.isNotBlank()) Path.of(settings.binaryDestination)
708+
else null,
706709
settings.binarySource,
707710
)
708711
cliManager.configSsh(tableOfWorkspaces.items)

‎src/main/resources/messages/CoderGatewayBundle.properties

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ gateway.connector.settings.binary-source.comment=Used to download the Coder \
5555
URLs will be used as-is; otherwise this value will be resolved against the \
5656
deployment domain. \
5757
Defaults to {0}.
58-
gateway.connector.settings.binary-destination.title=Data directory:
59-
gateway.connector.settings.binary-destination.comment=Directories are created \
60-
here that store the CLI and credentials for each domain to which the plugin \
58+
gateway.connector.settings.data-directory.title=Data directory:
59+
gateway.connector.settings.data-directory.comment=Directories are created \
60+
here that store the credentials for each domain to which the plugin \
6161
connects. \
6262
Defaults to {0}.
63+
gateway.connector.settings.binary-destination.title=CLI directory:
64+
gateway.connector.settings.binary-destination.comment=Directories are created \
65+
here that store the CLI for each domain to which the plugin connects. \
66+
Defaults to the data directory.
6367
gateway.connector.no-details="The error did not provide any further details"

‎src/test/groovy/CoderCLIManagerTest.groovy

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class CoderCLIManagerTest extends Specification {
242242
def "overrides binary URL"() {
243243
given:
244244
def (srv, url) = mockServer()
245-
def ccm = new CoderCLIManager(new URL(url), tmpdir, override.replace("{{url}}", url))
245+
def ccm = new CoderCLIManager(new URL(url), tmpdir, null, override.replace("{{url}}", url))
246246

247247
when:
248248
def downloaded = ccm.downloadCLI()
@@ -362,7 +362,7 @@ class CoderCLIManagerTest extends Specification {
362362
def "configures an SSH file"() {
363363
given:
364364
def sshConfigPath = tmpdir.resolve(input + "_to_" + output + ".conf")
365-
def ccm = new CoderCLIManager(new URL("https://test.coder.invalid"), tmpdir, null, sshConfigPath)
365+
def ccm = new CoderCLIManager(new URL("https://test.coder.invalid"), tmpdir, null, null, sshConfigPath)
366366
if (input != null) {
367367
Files.createDirectories(sshConfigPath.getParent())
368368
def originalConf = Path.of("src/test/fixtures/inputs").resolve(input + ".conf").toFile().text
@@ -407,7 +407,7 @@ class CoderCLIManagerTest extends Specification {
407407
def "fails if config is malformed"() {
408408
given:
409409
def sshConfigPath = tmpdir.resolve("configured" + input + ".conf")
410-
def ccm = new CoderCLIManager(new URL("https://test.coder.invalid"), tmpdir, null, sshConfigPath)
410+
def ccm = new CoderCLIManager(new URL("https://test.coder.invalid"), tmpdir, null, null, sshConfigPath)
411411
Files.createDirectories(sshConfigPath.getParent())
412412
Files.copy(
413413
Path.of("src/test/fixtures/inputs").resolve(input + ".conf"),
@@ -429,4 +429,13 @@ class CoderCLIManagerTest extends Specification {
429429
"malformed-start-after-end",
430430
]
431431
}
432+
433+
def "separately configures cli path from data dir"() {
434+
given:
435+
def dir = tmpdir.resolve("different-dir")
436+
def ccm = new CoderCLIManager(new URL("https://test.coder.invalid"), tmpdir, dir)
437+
438+
expect:
439+
ccm.localBinaryPath.getParent() == dir.resolve("test.coder.invalid")
440+
}
432441
}

0 commit comments

Comments
 (0)
Please sign in to comment.