Skip to content

Commit

Permalink
Merge pull request #284 from jenkinsci/bugfix/env_vars_in_jenkins_slave
Browse files Browse the repository at this point in the history
AST-41922 fix the installer to get the envVars from the Jenkins object
  • Loading branch information
tamarleviCm authored May 1, 2024
2 parents 9c19049 + 5489be4 commit 978c438
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/main/java/com/checkmarx/jenkins/tools/CheckmarxInstaller.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.checkmarx.jenkins.tools;

import com.checkmarx.jenkins.CxLoggerAdapter;
import com.checkmarx.jenkins.exception.CheckmarxException;
import com.checkmarx.jenkins.tools.internal.DownloadService;
import hudson.EnvVars;
import hudson.Extension;
Expand All @@ -14,6 +15,7 @@
import hudson.tools.ToolInstallation;
import hudson.tools.ToolInstaller;
import hudson.tools.ToolInstallerDescriptor;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -109,7 +111,7 @@ private FilePath installCheckmarxCliAsSingleBinary(FilePath expected, Node node,
Platform platform = nodeChannel.call(new GetPlatform(node.getDisplayName()));

try {
String proxyStr = getProxy(node);
String proxyStr = getProxy();
URL checkmarxDownloadUrl = DownloadService.getDownloadUrlForCli(version, platform);

expected.mkdirs();
Expand All @@ -128,20 +130,19 @@ private FilePath installCheckmarxCliAsSingleBinary(FilePath expected, Node node,
return expected;
}

private String getProxy(Node node) {
EnvVars envVars = getEnvVars(node);
private String getProxy() {
EnvVars envVars = getEnvVars();
String httpProxyStr = envVars.get(HTTP_PROXY);
if (StringUtils.isNotEmpty(httpProxyStr)) {
log.info("Installer using proxy: " + httpProxyStr);
}
return httpProxyStr;
}

private static EnvVars getEnvVars(Node node) {
private static EnvVars getEnvVars() {
EnvironmentVariablesNodeProperty environmentVariablesNodeProperty =
((Hudson) node).getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class);
EnvVars envVars = environmentVariablesNodeProperty != null ? environmentVariablesNodeProperty.getEnvVars() : new EnvVars();
return envVars;
Jenkins.get().getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class);
return environmentVariablesNodeProperty != null ? environmentVariablesNodeProperty.getEnvVars() : new EnvVars();
}

public String getVersion() {
Expand Down Expand Up @@ -208,7 +209,7 @@ public Void call() throws IOException {
extract(downloadedFile.getAbsolutePath(), downloadedFile.getParent());
} catch (ArchiveException | CompressorException e) {
throw new IOException(format("Could not extract cli: %s", downloadedFile.getAbsolutePath()));
} catch (URISyntaxException e) {
} catch (CheckmarxException | URISyntaxException e) {
throw new RuntimeException(e);
}

Expand All @@ -224,7 +225,7 @@ public Void call() throws IOException {
return null;
}

public static void copyURLToFile(URL source, String proxyStr, File destination, int connectionTimeoutMillis, int readTimeoutMillis) throws IOException, URISyntaxException {
public static void copyURLToFile(URL source, String proxyStr, File destination, int connectionTimeoutMillis, int readTimeoutMillis) throws IOException, URISyntaxException, CheckmarxException {
OkHttpClient client = new ProxyHttpClient().getHttpClient(proxyStr, connectionTimeoutMillis,readTimeoutMillis);
Request request = new Request.Builder().url(source).build();
Response response = client.newCall(request).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

import com.checkmarx.jenkins.exception.CheckmarxException;
import okhttp3.*;
import org.apache.commons.lang.StringUtils;

Expand All @@ -16,7 +17,7 @@

public class ProxyHttpClient {

public OkHttpClient getHttpClient(String proxyString, int connectionTimeoutMillis, int readTimeoutMillis) throws URISyntaxException {
public OkHttpClient getHttpClient(String proxyString, int connectionTimeoutMillis, int readTimeoutMillis) throws URISyntaxException, CheckmarxException {
OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder()
.connectTimeout(connectionTimeoutMillis, TimeUnit.MILLISECONDS)
.readTimeout(readTimeoutMillis, TimeUnit.MILLISECONDS);
Expand All @@ -39,12 +40,14 @@ public Request authenticate(Route route, Response response) throws IOException {
} else {
return okClientBuilder.proxy(_httpProxy).build();
}
} else {
throw new CheckmarxException("Invalid proxy configuration");
}
}
return okClientBuilder.build();
}

private static boolean isValidProxy(String proxyHost, int proxyPort) {
return (!proxyHost.isEmpty()) && (proxyPort >= 10) && (proxyPort <= 65535);
return StringUtils.isNotEmpty(proxyHost) && (proxyPort >= 10) && (proxyPort <= 65535);
}
}

0 comments on commit 978c438

Please sign in to comment.