From 5aaae5dbd63f6409eccf5efe56dfb63d2216263b Mon Sep 17 00:00:00 2001 From: adripo Date: Thu, 16 May 2019 11:30:41 +0200 Subject: [PATCH 1/5] Added custom proxy connection for each project --- .../StashBuildTrigger.java | 7 ++++++ .../StashRepository.java | 1 + .../stash/StashApiClient.java | 23 +++++++++++++++++++ .../StashBuildTrigger/config.jelly | 18 +++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java index 18baef86..d6e8bfed 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java @@ -54,6 +54,7 @@ public class StashBuildTrigger extends Trigger> { private final String projectPath; private final String cron; private final String stashHost; + private final JSONObject proxySettings; private final String credentialsId; private final String projectCode; private final String repositoryName; @@ -81,6 +82,7 @@ public StashBuildTrigger( String projectPath, String cron, String stashHost, + JSONObject proxySettings, String credentialsId, String projectCode, String repositoryName, @@ -100,6 +102,7 @@ public StashBuildTrigger( this.projectPath = projectPath; this.cron = cron; this.stashHost = stashHost; + this.proxySettings = proxySettings; this.credentialsId = credentialsId; this.projectCode = projectCode; this.repositoryName = repositoryName; @@ -125,6 +128,10 @@ public String getStashHost() { return stashHost; } + public JSONObject getProxySettings() { + return proxySettings; + } + public String getProjectPath() { return this.projectPath; } diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java index 30f6f04c..16e90cbe 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java @@ -69,6 +69,7 @@ public void init() { client = new StashApiClient( trigger.getStashHost(), + trigger.getProxySettings(), trigger.getUsername(), trigger.getPassword(), trigger.getProjectCode(), diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java index 5bf7f856..1a95de41 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java @@ -24,6 +24,8 @@ import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; + +import net.sf.json.JSONObject; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; @@ -60,12 +62,15 @@ public class StashApiClient { private String apiBaseUrl; + private final JSONObject proxySettings; + private String project; private String repositoryName; private Credentials credentials; public StashApiClient( String stashHost, + JSONObject proxySettings, String username, String password, String project, @@ -75,6 +80,7 @@ public StashApiClient( this.project = project; this.repositoryName = repositoryName; this.apiBaseUrl = stashHost.replaceAll("/$", "") + "/rest/api/1.0/projects/"; + this.proxySettings = proxySettings; if (ignoreSsl) { Protocol easyhttps = new Protocol("https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), 443); @@ -212,6 +218,23 @@ private HttpClient getHttpClient() { // } // } // } + + String proxyHost = (proxySettings != null) ? proxySettings.getString("proxyHost") : null; + int proxyPort = (proxySettings != null) ? proxySettings.getInt("proxyPort") : null; + String proxyUser = (proxySettings != null) ? proxySettings.getString("proxyUser") : null; + String proxyPass = (proxySettings != null) ? proxySettings.getString("proxyPass") : null; + if (proxyHost != null && !"".equals(proxyHost.trim())) { + logger.info("Using proxy (host=" + proxyHost + ")"); + client.getHostConfiguration().setProxy(proxyHost, proxyPort); + if (proxyUser != null && !"".equals(proxyUser.trim())) { + logger.info("Using proxy authentication (user=" + proxyUser + ")"); + client + .getState() + .setProxyCredentials( + AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPass)); + } + } + return client; } diff --git a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly index 148beb45..07c67755 100644 --- a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly +++ b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly @@ -6,6 +6,24 @@ + + + + + + + + + + + + + + + + +
+
From 7da8e57fd53ad98037a2cbd4114f078b5f0d3f27 Mon Sep 17 00:00:00 2001 From: adripo Date: Fri, 17 May 2019 23:18:35 +0200 Subject: [PATCH 2/5] Added proxy inheritance from jenkins Various improvements --- .../StashBuildTrigger.java | 42 ++++++++++++++++- .../StashRepository.java | 2 +- .../stash/StashApiClient.java | 47 +++++++------------ .../StashBuildTrigger/config.jelly | 32 ++++++++----- 4 files changed, 78 insertions(+), 45 deletions(-) diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java index d6e8bfed..424dc2f7 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java @@ -10,6 +10,7 @@ import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; +import hudson.ProxyConfiguration; import hudson.model.AbstractProject; import hudson.model.Cause; import hudson.model.Executor; @@ -54,7 +55,7 @@ public class StashBuildTrigger extends Trigger> { private final String projectPath; private final String cron; private final String stashHost; - private final JSONObject proxySettings; + private transient JSONObject proxySettings; private final String credentialsId; private final String projectCode; private final String repositoryName; @@ -132,6 +133,45 @@ public JSONObject getProxySettings() { return proxySettings; } + public JSONObject getProxyDetails() { + return this.getProxySettings().getJSONObject("proxyChoice"); + } + + private String getProxyType() { + return this.getProxyDetails().getString("value"); + } + + public ProxyConfiguration getProxy() { + ProxyConfiguration proxy = null; + + switch (this.getProxyType()) { + case "jenkins": + proxy = Jenkins.getInstance().proxy; + break; + case "custom": + JSONObject proxyDetails = this.getProxyDetails(); + + proxy = + new ProxyConfiguration( + proxyDetails.getString("proxyHost"), + proxyDetails.getInt("proxyPort"), + proxyDetails.getString("proxyUser"), + proxyDetails.getString("proxyPass")); + } + + return proxy; + } + + /** + * Helper method for the jelly view to determine proxy type. + * + * @param type + * @return + */ + public boolean isProxyType(String type) { + return (getProxyType().equalsIgnoreCase(type)) ? true : false; + } + public String getProjectPath() { return this.projectPath; } diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java index 16e90cbe..24e631b2 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java @@ -69,7 +69,7 @@ public void init() { client = new StashApiClient( trigger.getStashHost(), - trigger.getProxySettings(), + trigger.getProxy(), trigger.getUsername(), trigger.getPassword(), trigger.getProjectCode(), diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java index 1a95de41..dc31bb01 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java @@ -1,6 +1,7 @@ package stashpullrequestbuilder.stashpullrequestbuilder.stash; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import hudson.ProxyConfiguration; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; @@ -24,8 +25,7 @@ import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; - -import net.sf.json.JSONObject; +import jenkins.model.Jenkins; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; @@ -62,7 +62,7 @@ public class StashApiClient { private String apiBaseUrl; - private final JSONObject proxySettings; + private final ProxyConfiguration proxy; private String project; private String repositoryName; @@ -70,7 +70,7 @@ public class StashApiClient { public StashApiClient( String stashHost, - JSONObject proxySettings, + ProxyConfiguration proxy, String username, String password, String project, @@ -80,7 +80,7 @@ public StashApiClient( this.project = project; this.repositoryName = repositoryName; this.apiBaseUrl = stashHost.replaceAll("/$", "") + "/rest/api/1.0/projects/"; - this.proxySettings = proxySettings; + this.proxy = proxy; if (ignoreSsl) { Protocol easyhttps = new Protocol("https", (ProtocolSocketFactory) new EasySSLProtocolSocketFactory(), 443); @@ -203,35 +203,20 @@ private HttpClient getHttpClient() { httpParams.setParameter( CoreConnectionPNames.SO_TIMEOUT, StashApiClient.HTTP_SOCKET_TIMEOUT_SECONDS * 1000); - // if (Jenkins.getInstance() != null) { - // ProxyConfiguration proxy = Jenkins.getInstance().proxy; - // if (proxy != null) { - // logger.info("Jenkins proxy: " + proxy.name + ":" + proxy.port); - // client.getHostConfiguration().setProxy(proxy.name, proxy.port); - // String username = proxy.getUserName(); - // String password = proxy.getPassword(); - // // Consider it to be passed if username specified. Sufficient? - // if (username != null && !"".equals(username.trim())) { - // logger.info("Using proxy authentication (user=" + username + ")"); - // client.getState().setProxyCredentials(AuthScope.ANY, - // new UsernamePasswordCredentials(username, password)); - // } - // } - // } - - String proxyHost = (proxySettings != null) ? proxySettings.getString("proxyHost") : null; - int proxyPort = (proxySettings != null) ? proxySettings.getInt("proxyPort") : null; - String proxyUser = (proxySettings != null) ? proxySettings.getString("proxyUser") : null; - String proxyPass = (proxySettings != null) ? proxySettings.getString("proxyPass") : null; - if (proxyHost != null && !"".equals(proxyHost.trim())) { - logger.info("Using proxy (host=" + proxyHost + ")"); - client.getHostConfiguration().setProxy(proxyHost, proxyPort); - if (proxyUser != null && !"".equals(proxyUser.trim())) { - logger.info("Using proxy authentication (user=" + proxyUser + ")"); + if (Jenkins.getInstanceOrNull() != null && proxy != null) { + logger.info("Using proxy: " + proxy.name + ":" + proxy.port); + client.getHostConfiguration().setProxy(proxy.name, proxy.port); + + String username = proxy.getUserName(); + String password = proxy.getPassword(); + + // Consider it to be passed if username specified. Sufficient? + if (username != null && !"".equals(username.trim())) { + logger.info("With proxy authentication (user=" + username + ")"); client .getState() .setProxyCredentials( - AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPass)); + AuthScope.ANY, new UsernamePasswordCredentials(username, password)); } } diff --git a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly index 07c67755..632dfbc8 100644 --- a/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly +++ b/src/main/resources/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger/config.jelly @@ -9,18 +9,26 @@ - - - - - - - - - - - - + +
+ + + + + + + + + + + + + + + + +
+
From 085b415026aac38f38492a5f9928b9432ef0f45c Mon Sep 17 00:00:00 2001 From: adripo Date: Fri, 17 May 2019 23:41:26 +0200 Subject: [PATCH 3/5] Fixed default switch case --- .../stashpullrequestbuilder/StashBuildTrigger.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java index 424dc2f7..c0b8ad4e 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java @@ -145,6 +145,7 @@ public ProxyConfiguration getProxy() { ProxyConfiguration proxy = null; switch (this.getProxyType()) { + case default: case "jenkins": proxy = Jenkins.getInstance().proxy; break; From d2e94252ca109c59f4452a43548f553aad5ba065 Mon Sep 17 00:00:00 2001 From: adripo Date: Fri, 17 May 2019 23:44:20 +0200 Subject: [PATCH 4/5] Fixed mispelled switch default case --- .../stashpullrequestbuilder/StashBuildTrigger.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java index c0b8ad4e..86963bd5 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashBuildTrigger.java @@ -145,7 +145,7 @@ public ProxyConfiguration getProxy() { ProxyConfiguration proxy = null; switch (this.getProxyType()) { - case default: + default: case "jenkins": proxy = Jenkins.getInstance().proxy; break; From 03fda63dee0f7809c1d2bfcc92d45cfb73bcc080 Mon Sep 17 00:00:00 2001 From: adripo Date: Wed, 22 May 2019 12:05:25 +0200 Subject: [PATCH 5/5] Changed logger level for proxy connection --- .../stashpullrequestbuilder/stash/StashApiClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java index dc31bb01..b7839d65 100644 --- a/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java +++ b/src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/stash/StashApiClient.java @@ -204,7 +204,7 @@ private HttpClient getHttpClient() { CoreConnectionPNames.SO_TIMEOUT, StashApiClient.HTTP_SOCKET_TIMEOUT_SECONDS * 1000); if (Jenkins.getInstanceOrNull() != null && proxy != null) { - logger.info("Using proxy: " + proxy.name + ":" + proxy.port); + logger.fine("Using proxy: " + proxy.name + ":" + proxy.port); client.getHostConfiguration().setProxy(proxy.name, proxy.port); String username = proxy.getUserName(); @@ -212,7 +212,7 @@ private HttpClient getHttpClient() { // Consider it to be passed if username specified. Sufficient? if (username != null && !"".equals(username.trim())) { - logger.info("With proxy authentication (user=" + username + ")"); + logger.fine("With proxy authentication (user=" + username + ")"); client .getState() .setProxyCredentials(