Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixed

- potential race condition that could cause crashes when settings are modified concurrently

## 0.7.0 - 2025-09-27

### Changed
Expand Down
18 changes: 8 additions & 10 deletions src/main/kotlin/com/coder/toolbox/cli/CoderCLIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -354,24 +354,22 @@ class CoderCLIManager(
// always use the correct URL.
"--url",
escape(deploymentURL.toString()),
if (!context.settingsStore.headerCommand.isNullOrBlank()) "--header-command" else null,
if (!context.settingsStore.headerCommand.isNullOrBlank()) escapeSubcommand(context.settingsStore.headerCommand!!) else null,
context.settingsStore.headerCommand?.takeIf { it.isNotBlank() }?.let { "--header-command" },
context.settingsStore.headerCommand?.takeIf { it.isNotBlank() }?.let { escapeSubcommand(it) },
"ssh",
"--stdio",
if (context.settingsStore.disableAutostart && feats.disableAutostart) "--disable-autostart" else null,
"--network-info-dir ${escape(context.settingsStore.networkInfoDir)}"
)
val proxyArgs = baseArgs + listOfNotNull(
if (!context.settingsStore.sshLogDirectory.isNullOrBlank()) "--log-dir" else null,
if (!context.settingsStore.sshLogDirectory.isNullOrBlank()) escape(context.settingsStore.sshLogDirectory!!) else null,
context.settingsStore.sshLogDirectory?.takeIf { it.isNotBlank() }?.let { "--log-dir" },
context.settingsStore.sshLogDirectory?.takeIf { it.isNotBlank() }?.let { escape(it) },
if (feats.reportWorkspaceUsage) "--usage-app=jetbrains" else null,
)
val extraConfig =
if (!context.settingsStore.sshConfigOptions.isNullOrBlank()) {
"\n" + context.settingsStore.sshConfigOptions!!.prependIndent(" ")
} else {
""
}
val extraConfig = context.settingsStore.sshConfigOptions
?.takeIf { it.isNotBlank() }
?.let { "\n" + it.prependIndent(" ") }
?: ""
val options = """
ConnectTimeout 0
StrictHostKeyChecking no
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/com/coder/toolbox/sdk/CoderHttpClientBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ object CoderHttpClientBuilder {
val trustManagers = coderTrustManagers(settings.tls.caPath)
var builder = OkHttpClient.Builder()

if (context.proxySettings.getProxy() != null) {
context.logger.info("proxy: ${context.proxySettings.getProxy()}")
builder.proxy(context.proxySettings.getProxy())
} else if (context.proxySettings.getProxySelector() != null) {
context.logger.info("proxy selector: ${context.proxySettings.getProxySelector()}")
builder.proxySelector(context.proxySettings.getProxySelector()!!)
context.proxySettings.getProxy()?.let { proxy ->
context.logger.info("proxy: $proxy")
builder.proxy(proxy)
} ?: context.proxySettings.getProxySelector()?.let { proxySelector ->
context.logger.info("proxy selector: $proxySelector")
builder.proxySelector(proxySelector)
}

// Note: This handles only HTTP/HTTPS proxy authentication.
Expand Down
68 changes: 37 additions & 31 deletions src/main/kotlin/com/coder/toolbox/sdk/CoderRestClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import com.coder.toolbox.sdk.v2.models.CreateWorkspaceBuildRequest
import com.coder.toolbox.sdk.v2.models.Template
import com.coder.toolbox.sdk.v2.models.User
import com.coder.toolbox.sdk.v2.models.Workspace
import com.coder.toolbox.sdk.v2.models.WorkspaceAgent
import com.coder.toolbox.sdk.v2.models.WorkspaceBuild
import com.coder.toolbox.sdk.v2.models.WorkspaceBuildReason
import com.coder.toolbox.sdk.v2.models.WorkspaceResource
import com.coder.toolbox.sdk.v2.models.WorkspaceStatus
import com.coder.toolbox.sdk.v2.models.WorkspaceTransition
import com.squareup.moshi.Moshi
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -114,7 +112,9 @@ open class CoderRestClient(
)
}

return userResponse.body()!!
return requireNotNull(userResponse.body()) {
"Successful response returned null body or user"
}
}

/**
Expand All @@ -132,41 +132,29 @@ open class CoderRestClient(
)
}

return workspacesResponse.body()!!.workspaces
return requireNotNull(workspacesResponse.body()?.workspaces) {
"Successful response returned null body or workspaces"
}
}

/**
* Retrieves a workspace with the provided id.
* @throws [APIResponseException].
*/
suspend fun workspace(workspaceID: UUID): Workspace {
val workspacesResponse = retroRestClient.workspace(workspaceID)
if (!workspacesResponse.isSuccessful) {
val workspaceResponse = retroRestClient.workspace(workspaceID)
if (!workspaceResponse.isSuccessful) {
throw APIResponseException(
"retrieve workspace",
url,
workspacesResponse.code(),
workspacesResponse.parseErrorBody(moshi)
workspaceResponse.code(),
workspaceResponse.parseErrorBody(moshi)
)
}

return workspacesResponse.body()!!
}

/**
* Maps the available workspaces to the associated agents.
*/
suspend fun workspacesByAgents(): Set<Pair<Workspace, WorkspaceAgent>> {
// It is possible for there to be resources with duplicate names so we
// need to use a set.
return workspaces().flatMap { ws ->
when (ws.latestBuild.status) {
WorkspaceStatus.RUNNING -> ws.latestBuild.resources
else -> resources(ws)
}.filter { it.agents != null }.flatMap { it.agents!! }.map {
ws to it
}
}.toSet()
return requireNotNull(workspaceResponse.body()) {
"Successful response returned null body or workspace"
}
}

/**
Expand All @@ -187,7 +175,10 @@ open class CoderRestClient(
resourcesResponse.parseErrorBody(moshi)
)
}
return resourcesResponse.body()!!

return requireNotNull(resourcesResponse.body()) {
"Successful response returned null body or workspace resources"
}
}

suspend fun buildInfo(): BuildInfo {
Expand All @@ -200,7 +191,10 @@ open class CoderRestClient(
buildInfoResponse.parseErrorBody(moshi)
)
}
return buildInfoResponse.body()!!

return requireNotNull(buildInfoResponse.body()) {
"Successful response returned null body or build info"
}
}

/**
Expand All @@ -216,7 +210,10 @@ open class CoderRestClient(
templateResponse.parseErrorBody(moshi)
)
}
return templateResponse.body()!!

return requireNotNull(templateResponse.body()) {
"Successful response returned null body or template"
}
}

/**
Expand All @@ -238,7 +235,10 @@ open class CoderRestClient(
buildResponse.parseErrorBody(moshi)
)
}
return buildResponse.body()!!

return requireNotNull(buildResponse.body()) {
"Successful response returned null body or workspace build"
}
}

/**
Expand All @@ -254,7 +254,10 @@ open class CoderRestClient(
buildResponse.parseErrorBody(moshi)
)
}
return buildResponse.body()!!

return requireNotNull(buildResponse.body()) {
"Successful response returned null body or workspace build"
}
}

/**
Expand Down Expand Up @@ -296,7 +299,10 @@ open class CoderRestClient(
buildResponse.parseErrorBody(moshi)
)
}
return buildResponse.body()!!

return requireNotNull(buildResponse.body()) {
"Successful response returned null body or workspace build"
}
}

fun close() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ open class CoderProtocolHandler(
parameters: Map<String, String?>,
workspace: Workspace,
): WorkspaceAgent? {
val agents = workspace.latestBuild.resources.filter { it.agents != null }.flatMap { it.agents!! }
val agents = workspace.latestBuild.resources
.mapNotNull { it.agents }
.flatten()

if (agents.isEmpty()) {
context.logAndShowError(CAN_T_HANDLE_URI_TITLE, "The workspace \"${workspace.name}\" has no agents")
return null
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/com/coder/toolbox/views/ConnectStep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ class ConnectStep(
return
}

statusField.textState.update { context.i18n.pnotr("Connecting to ${CoderCliSetupContext.url!!.host}...") }
statusField.textState.update { context.i18n.pnotr("Connecting to ${CoderCliSetupContext.url?.host ?: "unknown host"}...") }
connect()
}

/**
* Try connecting to Coder with the provided URL and token.
*/
private fun connect() {
if (!CoderCliSetupContext.hasUrl()) {
val url = CoderCliSetupContext.url
if (url == null) {
errorField.textState.update { context.i18n.ptrl("URL is required") }
return
}
Expand All @@ -74,15 +75,15 @@ class ConnectStep(
return
}
// Capture the host name early for error reporting
val hostName = CoderCliSetupContext.url!!.host
val hostName = url.host

signInJob?.cancel()
signInJob = context.cs.launch(CoroutineName("Http and CLI Setup")) {
try {
context.logger.info("Setting up the HTTP client...")
val client = CoderRestClient(
context,
CoderCliSetupContext.url!!,
url,
if (context.settingsStore.requireTokenAuth) CoderCliSetupContext.token else null,
PluginManager.pluginInfo.version,
)
Expand Down
Loading