-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: environment for language server
extracted the CLI environment logic to a third helper file to be used also by language server.
- Loading branch information
1 parent
2a1d4f7
commit f7140dd
Showing
3 changed files
with
66 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters