Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from jenkinsci/handle_proxy_settings
[JENKINS-6933] Handle proxy settings
- Loading branch information
Showing
30 changed files
with
657 additions
and
21 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
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
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
24 changes: 24 additions & 0 deletions
24
src/main/java/hudson/plugins/tfs/model/ModernConnectionAdvisor.java
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,24 @@ | ||
package hudson.plugins.tfs.model; | ||
|
||
import java.util.Locale; | ||
import java.util.TimeZone; | ||
|
||
import com.microsoft.tfs.core.config.ConnectionInstanceData; | ||
import com.microsoft.tfs.core.config.DefaultConnectionAdvisor; | ||
import com.microsoft.tfs.core.config.httpclient.HTTPClientFactory; | ||
import com.microsoft.tfs.core.httpclient.ProxyHost; | ||
|
||
public class ModernConnectionAdvisor extends DefaultConnectionAdvisor { | ||
|
||
private final ProxyHost proxyHost; | ||
|
||
public ModernConnectionAdvisor(final ProxyHost proxyHost) { | ||
super(Locale.getDefault(), TimeZone.getDefault()); | ||
this.proxyHost = proxyHost; | ||
} | ||
|
||
@Override | ||
public HTTPClientFactory getHTTPClientFactory(final ConnectionInstanceData connectionInstanceData) { | ||
return new ModernHTTPClientFactory(connectionInstanceData, proxyHost); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/hudson/plugins/tfs/model/ModernHTTPClientFactory.java
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,27 @@ | ||
package hudson.plugins.tfs.model; | ||
|
||
import com.microsoft.tfs.core.config.ConnectionInstanceData; | ||
import com.microsoft.tfs.core.config.httpclient.DefaultHTTPClientFactory; | ||
import com.microsoft.tfs.core.httpclient.HostConfiguration; | ||
import com.microsoft.tfs.core.httpclient.HttpClient; | ||
import com.microsoft.tfs.core.httpclient.HttpState; | ||
import com.microsoft.tfs.core.httpclient.ProxyHost; | ||
|
||
public class ModernHTTPClientFactory extends DefaultHTTPClientFactory { | ||
|
||
private final ProxyHost proxyHost; | ||
|
||
public ModernHTTPClientFactory(final ConnectionInstanceData connectionInstanceData) { | ||
this(connectionInstanceData, null); | ||
} | ||
|
||
public ModernHTTPClientFactory(final ConnectionInstanceData connectionInstanceData, final ProxyHost proxyHost) { | ||
super(connectionInstanceData); | ||
this.proxyHost = proxyHost; | ||
} | ||
|
||
@Override | ||
public void configureClientProxy(final HttpClient httpClient, final HostConfiguration hostConfiguration,final HttpState httpState, final ConnectionInstanceData connectionInstanceData) { | ||
hostConfiguration.setProxyHost(proxyHost); | ||
} | ||
} |
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
134 changes: 134 additions & 0 deletions
134
src/main/java/hudson/plugins/tfs/model/WebProxySettings.java
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,134 @@ | ||
package hudson.plugins.tfs.model; | ||
|
||
import com.microsoft.tfs.core.httpclient.ProxyHost; | ||
import hudson.ProxyConfiguration; | ||
import hudson.util.Secret; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* A {@link Serializable} adapter between {@link ProxyConfiguration} and {@link ProxyHost}. | ||
*/ | ||
public class WebProxySettings implements Serializable { | ||
private static final long serialVersionUID = 401L; | ||
|
||
private final String hostName; | ||
private final int port; | ||
private final String proxyUser; | ||
private final Secret proxySecret; | ||
private final List<Pattern> noProxyHostPatterns; | ||
|
||
@SuppressWarnings("unused" /* Needed by Serializable interface */) | ||
private WebProxySettings() { | ||
this(null, -1, null, null, null); | ||
} | ||
|
||
/** | ||
* Convenience constructor, mostly for tests. | ||
* | ||
* @param hostName the name (or address) of the proxy server. | ||
* May be {@code null}, meaning there is no proxy server configured. | ||
* @param port the port that the proxy server is listening on | ||
* @param noProxyHostPatterns a list of {@link Pattern} representing hosts that should not be proxied. | ||
* May be {@code null}, meaning all hosts will be proxied. | ||
* @param proxyUser the name of the user with which to authenticate to the proxy server. | ||
* May be {@code null}, meaning the proxy server doesn't need authentication. | ||
* @param proxySecret the password of the user with which to authenticate to the proxy server. | ||
* May be {@code null}, meaning the proxy server doesn't need authentication. | ||
*/ | ||
public WebProxySettings(final String hostName, final int port, final List<Pattern> noProxyHostPatterns, final String proxyUser, final Secret proxySecret) { | ||
this.hostName = hostName; | ||
this.port = port; | ||
this.noProxyHostPatterns = copyNoProxyHostPatterns(noProxyHostPatterns); | ||
this.proxyUser = proxyUser; | ||
this.proxySecret = proxySecret; | ||
} | ||
|
||
/** | ||
* Initialize a {@link WebProxySettings} from a Jenkins {@link ProxyConfiguration}. | ||
* | ||
* @param proxyConfiguration the proxy settings as obtained from Jenkins. | ||
* May be {@code null}, meaning there is no proxy configured. | ||
*/ | ||
public WebProxySettings(final ProxyConfiguration proxyConfiguration) { | ||
if (proxyConfiguration != null) { | ||
this.hostName = proxyConfiguration.name; | ||
this.port = proxyConfiguration.port; | ||
this.proxyUser = proxyConfiguration.getUserName(); | ||
this.noProxyHostPatterns = copyNoProxyHostPatterns(proxyConfiguration.getNoProxyHostPatterns()); | ||
this.proxySecret = Secret.fromString(proxyConfiguration.getEncryptedPassword()); | ||
} | ||
else { | ||
this.hostName = null; | ||
this.port = -1; | ||
this.proxyUser = null; | ||
this.proxySecret = null; | ||
this.noProxyHostPatterns = copyNoProxyHostPatterns(null); | ||
} | ||
} | ||
|
||
private static ArrayList<Pattern> copyNoProxyHostPatterns(final List<Pattern> noProxyHostPatterns) { | ||
return new ArrayList<Pattern>( | ||
noProxyHostPatterns == null | ||
? Collections.<Pattern>emptyList() | ||
: noProxyHostPatterns | ||
); | ||
} | ||
|
||
/** | ||
* Initialize a {@link ProxyHost} from this {@link WebProxySettings} for the provided hostToProxy. | ||
* May return null, which either means there is no proxy server configured or it does not apply | ||
* to the provided hostToProxy. | ||
* | ||
* @param hostToProxy the name of the host for which proxying is considered. | ||
* @return an instance of {@link ProxyHost} or {@code null} if no proxy is to be used. | ||
*/ | ||
public ProxyHost toProxyHost(final String hostToProxy) { | ||
final ProxyHost proxyHost; | ||
if (this.hostName != null) { | ||
final boolean shouldProxy = shouldProxy(hostToProxy, noProxyHostPatterns); | ||
if (shouldProxy) { | ||
// TODO: The version of httpclient used by the TFS SDK does not support proxy auth | ||
proxyHost = new ProxyHost(hostName, port); | ||
} else { | ||
proxyHost = null; | ||
} | ||
} | ||
else { | ||
proxyHost = null; | ||
} | ||
return proxyHost; | ||
} | ||
|
||
static boolean shouldProxy(final String host, final List<Pattern> noProxyHostPatterns) { | ||
// inspired by https://github.com/jenkinsci/git-client-plugin/commit/2fefeae06db79d09d6604994001f8f2bd21549e1 | ||
boolean shouldProxy = true; | ||
for (final Pattern p : noProxyHostPatterns) { | ||
if (p.matcher(host).matches()) { | ||
shouldProxy = false; | ||
break; | ||
} | ||
} | ||
return shouldProxy; | ||
} | ||
|
||
public String getHostName() { | ||
return hostName; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public String getProxyUser() { | ||
return proxyUser; | ||
} | ||
|
||
public Secret getProxySecret() { | ||
return proxySecret; | ||
} | ||
} |
Oops, something went wrong.