Skip to content

Commit

Permalink
fix: environment for language server
Browse files Browse the repository at this point in the history
extracted the CLI environment logic to a third helper file to be used also by language server.
  • Loading branch information
bastiandoetsch committed Jan 9, 2024
1 parent 2a1d4f7 commit f7140dd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 54 deletions.
57 changes: 3 additions & 54 deletions src/main/kotlin/io/snyk/plugin/cli/ConsoleCommandRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,11 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Key
import com.intellij.util.net.HttpConfigurable
import io.snyk.plugin.controlExternalProcessWithProgressIndicator
import io.snyk.plugin.getWaitForResultsTimeout
import io.snyk.plugin.pluginSettings
import io.snyk.plugin.ui.SnykBalloonNotificationHelper
import snyk.common.getEndpointUrl
import snyk.common.isFedramp
import snyk.common.isOauth
import snyk.common.EnvironmentHelper
import snyk.errorHandler.SentryErrorReporter
import snyk.pluginInfo
import java.net.URI
import java.net.URLEncoder
import java.nio.charset.Charset

open class ConsoleCommandRunner {
Expand Down Expand Up @@ -90,54 +83,10 @@ open class ConsoleCommandRunner {
* Setup environment variables for CLI.
*/
fun setupCliEnvironmentVariables(commandLine: GeneralCommandLine, apiToken: String) {
val endpoint = getEndpointUrl()

val oauthEnabledEnvVar = "INTERNAL_SNYK_OAUTH_ENABLED"
val oauthEnvVar = "INTERNAL_OAUTH_TOKEN_STORAGE"
val snykTokenEnvVar = "SNYK_TOKEN"

val endpointURI = URI(endpoint)
val oauthEnabled = endpointURI.isOauth()
if (oauthEnabled) {
commandLine.environment[oauthEnabledEnvVar] = "1"
commandLine.environment.remove(snykTokenEnvVar)
} else {
commandLine.environment.remove(oauthEnvVar)
commandLine.environment.remove(oauthEnabledEnvVar)
}

if (apiToken.isNotEmpty()) {
if (oauthEnabled) {
commandLine.environment[oauthEnvVar] = apiToken
} else {
commandLine.environment[snykTokenEnvVar] = apiToken
}
}

commandLine.environment["SNYK_API"] = endpoint

if (!pluginSettings().usageAnalyticsEnabled || endpointURI.isFedramp()) {
commandLine.environment["SNYK_CFG_DISABLE_ANALYTICS"] = "1"
}

commandLine.environment["SNYK_INTEGRATION_NAME"] = pluginInfo.integrationName
commandLine.environment["SNYK_INTEGRATION_VERSION"] = pluginInfo.integrationVersion
commandLine.environment["SNYK_INTEGRATION_ENVIRONMENT"] = pluginInfo.integrationEnvironment
commandLine.environment["SNYK_INTEGRATION_ENVIRONMENT_VERSION"] = pluginInfo.integrationEnvironmentVersion
val proxySettings = HttpConfigurable.getInstance()
val proxyHost = proxySettings.PROXY_HOST
if (proxySettings != null && proxySettings.USE_HTTP_PROXY && proxyHost.isNotEmpty()) {
val authentication = if (proxySettings.PROXY_AUTHENTICATION) {
val auth = proxySettings.getPromptedAuthentication(proxyHost, "Snyk: Please enter your proxy password")
if (auth == null) "" else auth.userName.urlEncode() + ":" + String(auth.password).urlEncode() + "@"
} else ""
commandLine.environment["http_proxy"] = "http://$authentication$proxyHost:${proxySettings.PROXY_PORT}"
commandLine.environment["https_proxy"] = "http://$authentication$proxyHost:${proxySettings.PROXY_PORT}"
}
val environment = commandLine.environment
EnvironmentHelper.updateEnvironment(environment, apiToken)
}

private fun String.urlEncode() = URLEncoder.encode(this, "UTF-8")

companion object {
const val PROCESS_CANCELLED_BY_USER = "PROCESS_CANCELLED_BY_USER"
}
Expand Down
61 changes: 61 additions & 0 deletions src/main/kotlin/snyk/common/EnvironmentHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package snyk.common

import com.intellij.util.net.HttpConfigurable
import io.snyk.plugin.pluginSettings
import snyk.pluginInfo
import java.net.URI
import java.net.URLEncoder

object EnvironmentHelper {
fun updateEnvironment(
environment: MutableMap<String, String>,
apiToken: String
) {
val endpoint = getEndpointUrl()

val oauthEnabledEnvVar = "INTERNAL_SNYK_OAUTH_ENABLED"
val oauthEnvVar = "INTERNAL_OAUTH_TOKEN_STORAGE"
val snykTokenEnvVar = "SNYK_TOKEN"

val endpointURI = URI(endpoint)
val oauthEnabled = endpointURI.isOauth()
if (oauthEnabled) {
environment[oauthEnabledEnvVar] = "1"
environment.remove(snykTokenEnvVar)
} else {
environment.remove(oauthEnvVar)
environment.remove(oauthEnabledEnvVar)
}

if (apiToken.isNotEmpty()) {
if (oauthEnabled) {
environment[oauthEnvVar] = apiToken
} else {
environment[snykTokenEnvVar] = apiToken
}
}

environment["SNYK_API"] = endpoint

if (!pluginSettings().usageAnalyticsEnabled || endpointURI.isFedramp()) {
environment["SNYK_CFG_DISABLE_ANALYTICS"] = "1"
}

environment["SNYK_INTEGRATION_NAME"] = pluginInfo.integrationName
environment["SNYK_INTEGRATION_VERSION"] = pluginInfo.integrationVersion
environment["SNYK_INTEGRATION_ENVIRONMENT"] = pluginInfo.integrationEnvironment
environment["SNYK_INTEGRATION_ENVIRONMENT_VERSION"] = pluginInfo.integrationEnvironmentVersion
val proxySettings = HttpConfigurable.getInstance()
val proxyHost = proxySettings.PROXY_HOST
if (proxySettings != null && proxySettings.USE_HTTP_PROXY && proxyHost.isNotEmpty()) {
val authentication = if (proxySettings.PROXY_AUTHENTICATION) {
val auth = proxySettings.getPromptedAuthentication(proxyHost, "Snyk: Please enter your proxy password")
if (auth == null) "" else auth.userName.urlEncode() + ":" + String(auth.password).urlEncode() + "@"
} else ""

Check warning

Code scanning / detekt

Detects multiline if-else statements without braces Warning

Missing { ... }
environment["http_proxy"] = "http://$authentication$proxyHost:${proxySettings.PROXY_PORT}"
environment["https_proxy"] = "http://$authentication$proxyHost:${proxySettings.PROXY_PORT}"
}
}

private fun String.urlEncode() = URLEncoder.encode(this, "UTF-8")
}
2 changes: 2 additions & 0 deletions src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.eclipse.lsp4j.jsonrpc.Launcher
import org.eclipse.lsp4j.launch.LSPLauncher
import org.eclipse.lsp4j.services.LanguageClient
import org.eclipse.lsp4j.services.LanguageServer
import snyk.common.EnvironmentHelper
import snyk.common.getEndpointUrl
import snyk.common.lsp.commands.ScanDoneEvent
import snyk.pluginInfo
Expand Down Expand Up @@ -46,6 +47,7 @@ class LanguageServerWrapper(private val lsPath: String = getCliFile().absolutePa
val cmd = listOf(lsPath, "language-server", "-l", logLevel)

val processBuilder = ProcessBuilder(cmd)
pluginSettings().token?.let { EnvironmentHelper.updateEnvironment(processBuilder.environment(), it) }

process = processBuilder.start()
launcher = LSPLauncher.createClientLauncher(languageClient, process.inputStream, process.outputStream)
Expand Down

0 comments on commit f7140dd

Please sign in to comment.