Skip to content

fix(jetbrains): avoid connection loops #12232

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 1 commit into from
Sep 2, 2022
Merged
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 @@ -12,6 +12,7 @@ import com.intellij.ide.BrowserUtil
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.ui.Messages
import com.intellij.remote.RemoteCredentialsHolder
import com.intellij.ssh.AskAboutHostKey
import com.intellij.ssh.OpenSshLikeHostKeyVerifier
Expand All @@ -30,8 +31,10 @@ import com.jetbrains.gateway.api.*
import com.jetbrains.gateway.ssh.ClientOverSshTunnelConnector
import com.jetbrains.gateway.ssh.SshHostTunnelConnector
import com.jetbrains.gateway.thinClientLink.ThinClientHandle
import com.jetbrains.rd.util.ConcurrentHashMap
import com.jetbrains.rd.util.URI
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rd.util.lifetime.LifetimeDefinition
import io.gitpod.gitpodprotocol.api.entities.WorkspaceInstance
import io.gitpod.jetbrains.icons.GitpodIcons
import kotlinx.coroutines.*
Expand All @@ -49,7 +52,7 @@ import kotlin.coroutines.coroutineContext

@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
class GitpodConnectionProvider : GatewayConnectionProvider {

private val activeConnections = ConcurrentHashMap<String, LifetimeDefinition>()
private val gitpod = service<GitpodConnectionService>()

private val httpClient = HttpClient.newBuilder()
Expand All @@ -74,8 +77,34 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
parameters["workspaceId"]!!,
parameters["backendPort"]
)

val connectionKeyId = "${connectParams.gitpodHost}-${connectParams.workspaceId}-${connectParams.backendPort}"

var found = true
val connectionLifetime = activeConnections.computeIfAbsent(connectionKeyId) {
found = false
Lifetime.Eternal.createNested()
}

if (found) {
val message =
"You are trying to connect to a workspace that has a client already open. Check for opened JetBrains clients on your machine"
val title = "${connectParams.workspaceId} (${connectParams.gitpodHost})"
val okButton = Messages.getOkButton()
val options = arrayOf(okButton)
val defaultIndex = 0
val icon = Messages.getWarningIcon()
Messages.showDialog(message, title, options, defaultIndex, icon)

val errMessage = "A connection to the same workspace already exists: $connectionKeyId"
throw IllegalStateException(errMessage)
} else {
connectionLifetime.onTermination {
activeConnections.remove(connectionKeyId)
}
}

val client = gitpod.obtainClient(connectParams.gitpodHost)
val connectionLifetime = Lifetime.Eternal.createNested()
val updates = client.listenToWorkspace(connectionLifetime, connectParams.workspaceId)
val workspace = client.syncWorkspace(connectParams.workspaceId).workspace

Expand Down Expand Up @@ -216,7 +245,8 @@ class GitpodConnectionProvider : GatewayConnectionProvider {
thinClientJob = launch {
try {
val updatedIdeUrl = URL(update.ideUrl);
val sshHostUrl = URL(update.ideUrl.replace(update.workspaceId, "${update.workspaceId}.ssh"));
val sshHostUrl =
URL(update.ideUrl.replace(update.workspaceId, "${update.workspaceId}.ssh"));
val hostKeys = resolveHostKeys(updatedIdeUrl, connectParams)
if (hostKeys.isNullOrEmpty()) {
setErrorMessage("${connectParams.gitpodHost} installation does not allow SSH access, public keys cannot be found")
Expand Down