Merge pull request #698 from Argelbargel/fix-697

Make plugin respect no-proxy-hosts when a proxy is used
This commit is contained in:
Owen Mehegan 2018-04-04 16:32:51 +10:00 committed by GitHub
commit f95e65964b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 17 deletions

View File

@ -53,7 +53,7 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.net.Proxy.NO_PROXY;
import static java.net.Proxy.Type.HTTP;
@Restricted(NoExternalUse.class)
@ -90,20 +90,23 @@ public class ResteasyGitLabClientBuilder extends GitLabClientBuilder {
private GitLabClient buildClient(String url, String apiToken, ProxyConfiguration httpProxyConfig, boolean ignoreCertificateErrors, int connectionTimeout, int readTimeout) {
ResteasyClientBuilder builder = new ResteasyClientBuilder();
if (ignoreCertificateErrors) {
builder.hostnameVerification(ResteasyClientBuilder.HostnameVerificationPolicy.ANY);
builder.disableTrustManager();
}
Proxy proxy = httpProxyConfig != null ? httpProxyConfig.createProxy(getHost(url)) : NO_PROXY;
if (proxy != NO_PROXY) {
if (httpProxyConfig != null) {
Proxy proxy = httpProxyConfig.createProxy(getHost(url));
if (proxy.type() == HTTP) {
InetSocketAddress address = (InetSocketAddress) proxy.address();
builder.defaultProxy(address.getHostName().replaceFirst("^.*://", ""),
builder.defaultProxy(address.getHostString().replaceFirst("^.*://", ""),
address.getPort(),
address.getHostName().startsWith("https") ? "https" : "http",
httpProxyConfig.getUserName(),
httpProxyConfig.getPassword());
}
}
GitLabApiProxy apiProxy = builder
.connectionPoolSize(60)
@ -120,6 +123,7 @@ public class ResteasyGitLabClientBuilder extends GitLabClientBuilder {
.proxyBuilder(apiProxyClass)
.classloader(apiProxyClass.getClassLoader())
.build();
return new ResteasyGitLabClient(url, apiProxy, mergeRequestIdProvider);
}
@ -139,7 +143,7 @@ public class ResteasyGitLabClientBuilder extends GitLabClientBuilder {
this.gitlabApiToken = gitlabApiToken;
}
public void filter(ClientRequestContext requestContext) throws IOException {
public void filter(ClientRequestContext requestContext) {
requestContext.getHeaders().putSingle(PRIVATE_TOKEN, gitlabApiToken);
}
}
@ -147,7 +151,7 @@ public class ResteasyGitLabClientBuilder extends GitLabClientBuilder {
@Priority(Priorities.USER)
private static class LoggingFilter implements ClientRequestFilter, ClientResponseFilter {
@Override
public void filter(ClientRequestContext context) throws IOException {
public void filter(ClientRequestContext context) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Call GitLab:\nHTTP method: {0}\nURL: {1}\nRequest headers: [\n{2}\n]",
LoggerUtil.toArray(context.getMethod(), context.getUri(), toFilteredString(context.getHeaders())));
@ -155,7 +159,7 @@ public class ResteasyGitLabClientBuilder extends GitLabClientBuilder {
}
@Override
public void filter(ClientRequestContext request, ClientResponseContext response) throws IOException {
public void filter(ClientRequestContext request, ClientResponseContext response) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(Level.FINEST, "Got response from GitLab:\nURL: {0}\nStatus: {1} {2}\nResponse headers: [\n{3}\n]\nResponse body: {4}",
LoggerUtil.toArray(request.getUri(), response.getStatus(), response.getStatusInfo(), toString(response.getHeaders()),
@ -220,7 +224,7 @@ public class ResteasyGitLabClientBuilder extends GitLabClientBuilder {
private static class RemoveAcceptEncodingFilter implements ClientRequestFilter {
RemoveAcceptEncodingFilter() {}
@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
public void filter(ClientRequestContext clientRequestContext) {
clientRequestContext.getHeaders().remove("Accept-Encoding");
}
}

View File

@ -9,6 +9,7 @@ import com.cloudbees.plugins.credentials.domains.Domain;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.gitlab.api.GitLabClient;
import com.dabsquared.gitlabjenkins.gitlab.api.impl.V3GitLabClientBuilder;
import hudson.ProxyConfiguration;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.security.GlobalMatrixAuthorizationStrategy;
@ -39,8 +40,11 @@ import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import static com.dabsquared.gitlabjenkins.connection.Messages.connection_error;
import static com.dabsquared.gitlabjenkins.connection.Messages.connection_success;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@ -77,18 +81,33 @@ public class GitLabConnectionConfigTest {
@Test
public void doCheckConnection_success() {
String expected = Messages.connection_success();
String expected = connection_success();
assertThat(doCheckConnection("v3", Response.Status.OK), is(expected));
assertThat(doCheckConnection("v4", Response.Status.OK), is(expected));
}
@Test
public void doCheckConnection_forbidden() throws IOException {
String expected = Messages.connection_error("HTTP 403 Forbidden");
public void doCheckConnection_forbidden() {
String expected = connection_error("HTTP 403 Forbidden");
assertThat(doCheckConnection("v3", Response.Status.FORBIDDEN), is(expected));
assertThat(doCheckConnection("v4", Response.Status.FORBIDDEN), is(expected));
}
@Test
public void doCheckConnection_proxy() {
jenkins.getInstance().proxy = new ProxyConfiguration("0.0.0.0", 80);
GitLabConnectionConfig connectionConfig = jenkins.get(GitLabConnectionConfig.class);
FormValidation result = connectionConfig.doTestConnection(gitLabUrl, API_TOKEN_ID, "v3", false, 10, 10);
assertThat(result.getMessage(), containsString("Connection refused"));
}
@Test
public void doCheckConnection_noProxy() {
jenkins.getInstance().proxy = new ProxyConfiguration("0.0.0.0", 80, "", "", "localhost");
assertThat(doCheckConnection("v3", Response.Status.OK), is(connection_success()));
}
private String doCheckConnection(String clientBuilderId, Response.Status status) {
HttpRequest request = request().withPath("/gitlab/api/" + clientBuilderId + "/.*").withHeader("PRIVATE-TOKEN", API_TOKEN);
mockServerClient.when(request).respond(response().withStatusCode(status.getStatusCode()));
@ -101,7 +120,7 @@ public class GitLabConnectionConfigTest {
@Test
public void authenticationEnabled_anonymous_forbidden() throws IOException, URISyntaxException {
public void authenticationEnabled_anonymous_forbidden() throws IOException {
Boolean defaultValue = jenkins.get(GitLabConnectionConfig.class).isUseAuthenticatedEndpoint();
assertTrue(defaultValue);
jenkins.getInstance().setAuthorizationStrategy(new GlobalMatrixAuthorizationStrategy());