Skip to content

Commit

Permalink
Revert proxy system properties from connectors
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Jun 24, 2022
1 parent ea85d10 commit f738329
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;

import org.glassfish.jersey.ExternalProperties;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.client.internal.HttpUrlConnector;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.util.PropertiesHelper;
Expand Down Expand Up @@ -281,19 +279,21 @@ class ApacheConnector implements Connector {
clientBuilder.setRetryHandler((HttpRequestRetryHandler) retryHandler);
}

URI proxyUri = HttpUrlConnector.getProxyUri(config);
final Object proxyUri;
proxyUri = config.getProperty(ClientProperties.PROXY_URI);
if (proxyUri != null) {
// TODO No proxy hosts?
HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme());
String userName = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
final URI u = getProxyUri(proxyUri);
final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme());
final String userName;
userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class);
if (userName != null) {
String password = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
final String password;
password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class);

if (password != null) {
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUri.getHost(), proxyUri.getPort()),
new AuthScope(u.getHost(), u.getPort()),
new UsernamePasswordCredentials(userName, password)
);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@
import org.apache.hc.core5.util.TextUtils;
import org.apache.hc.core5.util.Timeout;
import org.apache.hc.core5.util.VersionInfo;
import org.glassfish.jersey.ExternalProperties;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.glassfish.jersey.client.internal.HttpUrlConnector;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.util.PropertiesHelper;
Expand Down Expand Up @@ -282,20 +280,21 @@ class Apache5Connector implements Connector {
clientBuilder.setRetryStrategy((HttpRequestRetryStrategy) retryHandler);
}

URI proxyUri = HttpUrlConnector.getProxyUri(config);
final Object proxyUri;
proxyUri = config.getProperty(ClientProperties.PROXY_URI);
if (proxyUri != null) {
// TODO No proxy hosts?
HttpHost proxy = new HttpHost(proxyUri.getScheme(), proxyUri.getHost(), proxyUri.getPort());
String userName = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
final URI u = getProxyUri(proxyUri);
final HttpHost proxy = new HttpHost(u.getScheme(), u.getHost(), u.getPort());
final String userName;
userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class);
if (userName != null) {
String password = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
final String password;
password = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_PASSWORD, String.class);

if (password != null) {
final CredentialsStore credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUri.getHost(), proxyUri.getPort()),
new AuthScope(u.getHost(), u.getPort()),
new UsernamePasswordCredentials(userName, password.toCharArray())
);
clientBuilder.setDefaultCredentialsProvider(credsProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,37 @@
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.net.ssl.SSLContext;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.MultivaluedMap;

import javax.net.ssl.SSLContext;

import org.eclipse.jetty.client.util.BasicAuthentication;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.client.util.FutureResponseListener;
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.util.collection.ByteBufferInputStream;
import org.glassfish.jersey.internal.util.collection.NonBlockingInputStream;
import org.glassfish.jersey.message.internal.HeaderUtils;
import org.glassfish.jersey.message.internal.OutboundMessageContext;
import org.glassfish.jersey.message.internal.Statuses;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpProxy;
import org.eclipse.jetty.client.ProxyConfiguration;
Expand All @@ -53,29 +71,13 @@
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BasicAuthentication;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.client.util.FutureResponseListener;
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.util.HttpCookieStore;
import org.eclipse.jetty.util.Jetty;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.glassfish.jersey.ExternalProperties;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.client.internal.HttpUrlConnector;
import org.glassfish.jersey.client.spi.AsyncConnectorCallback;
import org.glassfish.jersey.client.spi.Connector;
import org.glassfish.jersey.internal.util.collection.ByteBufferInputStream;
import org.glassfish.jersey.internal.util.collection.NonBlockingInputStream;
import org.glassfish.jersey.message.internal.HeaderUtils;
import org.glassfish.jersey.message.internal.OutboundMessageContext;
import org.glassfish.jersey.message.internal.Statuses;

/**
* A {@link Connector} that utilizes the Jetty HTTP Client to send and receive
Expand Down Expand Up @@ -185,18 +187,17 @@ class JettyConnector implements Connector {
auth.addAuthentication((BasicAuthentication) basicAuthProvider);
}

URI proxyUri = HttpUrlConnector.getProxyUri(config);
final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI);
if (proxyUri != null) {
// TODO No proxy hosts?
final URI u = getProxyUri(proxyUri);
final ProxyConfiguration proxyConfig = client.getProxyConfiguration();
proxyConfig.getProxies().add(new HttpProxy(proxyUri.getHost(), proxyUri.getPort()));
String proxyUsername = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
proxyConfig.getProxies().add(new HttpProxy(u.getHost(), u.getPort()));

final Object proxyUsername = config.getProperties().get(ClientProperties.PROXY_USERNAME);
if (proxyUsername != null) {
String proxyPassword = ClientProperties.getValue(config.getProperties(),
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
auth.addAuthentication(new BasicAuthentication(proxyUri, "<<ANY_REALM>>",
proxyUsername, proxyPassword));
final Object proxyPassword = config.getProperties().get(ClientProperties.PROXY_PASSWORD);
auth.addAuthentication(new BasicAuthentication(u, "<<ANY_REALM>>",
String.valueOf(proxyUsername), String.valueOf(proxyPassword)));
}
}

Expand All @@ -222,6 +223,17 @@ class JettyConnector implements Connector {
this.cookieStore = client.getCookieStore();
}

@SuppressWarnings("ChainOfInstanceofChecks")
private static URI getProxyUri(final Object proxy) {
if (proxy instanceof URI) {
return (URI) proxy;
} else if (proxy instanceof String) {
return URI.create((String) proxy);
} else {
throw new ProcessingException(LocalizationMessages.WRONG_PROXY_URI_TYPE(ClientProperties.PROXY_URI));
}
}

/**
* Get the {@link HttpClient}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,6 @@ public static <T> T getValue(final Map<String, ?> properties, final String key,
* @since 2.37
*/
public static String getValue(Map<String, ?> properties, String key, String systemKey) {
String value = PropertiesHelper.getValue(properties, key, String.class, null);
if (value == null) {
value = System.getProperty(systemKey);
}
return value;
return PropertiesHelper.getValue(properties, key, System.getProperty(systemKey), String.class, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,64 +331,25 @@ private static URI getProxyUriValue(Object proxy) {
}
}

/**
* Builds an URI from {@link ClientProperties#PROXY_URI}.
* In case it is not set, it will build it from System properties http.proxyHost and http.proxyPort.
* Otherwise returns null.
*
* @param config the configuration containing properties
* @return the URI or null
*/
public static URI getProxyUri(Configuration config) {
URI uri = null;
Object proxyUri = config.getProperty(ClientProperties.PROXY_URI);
if (proxyUri == null) {
String proxyHost = System.getProperty(ExternalProperties.HTTP_PROXY_HOST);
String proxyPort = System.getProperty(ExternalProperties.HTTP_PROXY_PORT);
if (proxyHost != null && proxyPort != null) {
if (proxyHost.startsWith("http://")) {
uri = URI.create(proxyHost + ":" + proxyPort);
} else {
uri = URI.create("http://" + proxyHost + ":" + proxyPort);
}
}
} else {
uri = getProxyUriValue(proxyUri);
}
return uri;
}

private ClientResponse _apply(final ClientRequest request) throws IOException {
final HttpURLConnection uc;
Proxy proxy = null;
boolean skipProxy = false;
// Evaluate HTTP_NON_PROXY_HOSTS if HTTP_PROXY_HOST is also set
if (System.getProperty(ExternalProperties.HTTP_PROXY_HOST) != null
&& System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS) != null) {
String[] nonProxyHosts = System.getProperty(ExternalProperties.HTTP_NON_PROXY_HOSTS)
.trim().split("\\|");
String currentHost = request.getUri().getHost();
for (String nonProxyHost : nonProxyHosts) {
if (Pattern.matches(nonProxyHost, currentHost)) {
skipProxy = true;
break;
}
}
}
if (!skipProxy) {
URI proxyUri = getProxyUri(request.getConfiguration());
if (proxyUri != null) {
String username = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
String password = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
if (username != null && password != null) {
StringBuilder auth = new StringBuilder().append(username).append(":").append(password);
String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes());
request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded));
Object proxyUri = request.getConfiguration().getProperties().get(ClientProperties.PROXY_URI);
if (proxyUri != null) {
URI uri = getProxyUriValue(proxyUri);
String username = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_USERNAME, ExternalProperties.HTTP_PROXY_USER);
String password = ClientProperties.getValue(request.getConfiguration().getProperties(),
ClientProperties.PROXY_PASSWORD, ExternalProperties.HTTP_PROXY_PASSWORD);
if (username != null) {
StringBuilder auth = new StringBuilder().append(username).append(":");
if (password != null) {
auth.append(password);
}
proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyUri.getHost(), proxyUri.getPort()));
String encoded = "Basic " + Base64.getEncoder().encodeToString(auth.toString().getBytes());
request.getHeaders().put("Proxy-Authorization", Arrays.asList(encoded));
}
proxy = new Proxy(Type.HTTP, new InetSocketAddress(uri.getHost(), uri.getPort()));
}
uc = this.connectionFactory.getConnection(request.getUri().toURL(), proxy);
uc.setDoInput(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.apache5.connector.Apache5ConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
import org.glassfish.jersey.client.spi.ConnectorProvider;
import org.glassfish.jersey.jetty.connector.JettyConnectorProvider;
import org.glassfish.jersey.netty.connector.NettyConnectorProvider;
Expand All @@ -46,7 +44,6 @@
import javax.ws.rs.core.Response;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -60,17 +57,14 @@
@RunWith(Parameterized.class)
public class ProxySelectorTest {
private static final String NO_PASS = "no-pass";
private static final String PROXY_USERNAME = "proxy-user";
private static final String PROXY_PASSWORD = "proxy-password";

@Parameterized.Parameters(name = "{index}: {0}")
public static List<Object[]> testData() {
return Arrays.asList(new Object[][]{
{ApacheConnectorProvider.class},
{Apache5ConnectorProvider.class},
{JettyConnectorProvider.class},
// {ApacheConnectorProvider.class},
// {Apache5ConnectorProvider.class},
// {JettyConnectorProvider.class},
{NettyConnectorProvider.class},
{HttpUrlConnectorProvider.class},
});
}

Expand Down Expand Up @@ -101,23 +95,6 @@ public void testGet407() {
}
}

@Test
public void testGetSuccess() {
// It seems Netty is not supporting user/pass in System properties
if (connectorProvider.getClass() != NettyConnectorProvider.class) {
try {
System.setProperty("http.proxyUser", PROXY_USERNAME);
System.setProperty("http.proxyPassword", PROXY_PASSWORD);
Response response = target("proxyTest").request().get();
response.bufferEntity();
assertEquals(response.readEntity(String.class), 200, response.getStatus());
} finally {
System.clearProperty("http.proxyUser");
System.clearProperty("http.proxyPassword");
}
}
}

private static Server server;
@BeforeClass
public static void startFakeProxy() {
Expand Down Expand Up @@ -171,29 +148,6 @@ public void handle(String target,
HttpServletResponse response) {
if (request.getHeader(NO_PASS) != null) {
response.setStatus(Integer.parseInt(request.getHeader(NO_PASS)));
} else if (request.getHeader("Proxy-Authorization") != null) {
String proxyAuthorization = request.getHeader("Proxy-Authorization");
String decoded = new String(Base64.getDecoder().decode(proxyAuthorization.substring(6).getBytes()));
final String[] split = decoded.split(":");
final String username = split[0];
final String password = split[1];

if (!username.equals(PROXY_USERNAME)) {
response.setStatus(400);
System.out.println("Found unexpected username: " + username);
}

if (!password.equals(PROXY_PASSWORD)) {
response.setStatus(400);
System.out.println("Found unexpected password: " + username);
}

if (response.getStatus() != 400) {
response.setStatus(200);
if ("CONNECT".equalsIgnoreCase(baseRequest.getMethod())) { // NETTY way of doing proxy
httpConnect.add(baseRequest.getHttpChannel());
}
}
} else {
response.setStatus(407);
response.addHeader("Proxy-Authenticate", "Basic");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down

0 comments on commit f738329

Please sign in to comment.