fix(gateway): apply Java proxy options to Vert.x connection
Take care of Java system property-derived proxy options for connection between the vert.x gateway
and the backend service.
Additionally, this fixes the plugin download in vert.x if a proxy is used.
Closes:
MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/ConnectorFactory.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/ConnectorFactory.java
@@ -25,6 +25,7 @@ import io.apiman.gateway.engine.IConnectorFactory;
import io.apiman.gateway.engine.auth.RequiredAuthType;
import io.apiman.gateway.engine.beans.Api;
import io.apiman.gateway.engine.beans.ApiRequest;
+import io.apiman.gateway.platforms.vertx3.engine.proxy.SysPropsProxySelector;
import io.apiman.gateway.platforms.vertx3.http.HttpClientOptionsFactory;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
@@ -44,6 +45,7 @@ import java.util.concurrent.ExecutionException;
*/
public class ConnectorFactory implements IConnectorFactory {
+ private final SysPropsProxySelector sysPropsProxySelector = new SysPropsProxySelector();
private static final Set<String> SUPPRESSED_HEADERS = new HashSet<>();
static {
SUPPRESSED_HEADERS.add("Transfer-Encoding"); //$NON-NLS-1$
@@ -68,6 +70,9 @@ public class ConnectorFactory implements IConnectorFactory {
.setIdleTimeout(opts.getIdleTimeout())
.setKeepAlive(opts.isKeepAlive())
.setTryUseCompression(opts.isTryUseCompression());
+
+ sysPropsProxySelector.setProxyOptions(opts.getUri(), vxClientOptions);
+
return vertx.createHttpClient(vxClientOptions);
}
});

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/engine/VertxPluginRegistry.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/engine/VertxPluginRegistry.java
@@ -24,7 +24,6 @@ import io.apiman.gateway.engine.vertx.polling.exceptions.BadResponseCodeError;
import io.apiman.gateway.platforms.vertx3.common.config.InheritingHttpClientOptions;
import io.apiman.gateway.platforms.vertx3.common.config.InheritingHttpClientOptionsConverter;
import io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig;
-import io.apiman.gateway.platforms.vertx3.engine.proxy.HttpProxy;
import io.apiman.gateway.platforms.vertx3.engine.proxy.SysPropsProxySelector;
import io.apiman.gateway.platforms.vertx3.i18n.Messages;
@@ -51,8 +50,6 @@ import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.json.JsonObject;
-import io.vertx.core.net.ProxyOptions;
-import io.vertx.core.net.ProxyType;
/**
* A vertx implementation of the API Gateway's plugin registry. This version simply extends the default
@@ -201,32 +198,8 @@ public class VertxPluginRegistry extends DefaultPluginRegistry {
httpClientOptions.setSsl(true);
}
- // If there's a proxy that should be used, it will be resolved and set here.
- proxySelector.resolveProxy(artifactUrl).ifPresent(proxy -> {
- ProxyOptions proxyOptions = new ProxyOptions();
- proxyOptions.setHost(proxy.getHost());
- proxyOptions.setPort(proxy.getPort());
- proxyOptions.setType(translateProxyType(proxy));
-
- proxy.getCredentials().ifPresent((credentials) -> {
- proxyOptions.setUsername(credentials.getPrinciple());
- proxyOptions.setPassword(credentials.getPasswordAsString());
- });
- });
+ proxySelector.setProxyOptions(artifactUrl, httpClientOptions);
return httpClientOptions;
}
-
- private ProxyType translateProxyType(HttpProxy proxy) {
- switch(proxy.getProxyType()) {
- case HTTP:
- return ProxyType.HTTP;
- case SOCKS4:
- return ProxyType.SOCKS4;
- case SOCKS5:
- return ProxyType.SOCKS5;
- default:
- throw new IllegalStateException("Unexpected value: " + proxy.getProxyType());
- }
- }
}

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/engine/proxy/SysPropsProxySelector.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/engine/proxy/SysPropsProxySelector.java
*/
package io.apiman.gateway.platforms.vertx3.engine.proxy;
+import io.vertx.core.http.HttpClientOptions;
+import io.vertx.core.net.ProxyOptions;
+import io.vertx.core.net.ProxyType;
+
import java.net.URI;
import java.util.Optional;
import java.util.StringJoiner;
@@ -36,11 +40,33 @@ public class SysPropsProxySelector {
public SysPropsProxySelector() {
}
+ /**
+ * Set proxy options on vxClientOptions derived from System properties.
+ *
+ * @param proxyUri proxy URI
+ * @param vxClientOptions Vert.x client options to mutate
+ */
+ public void setProxyOptions(URI proxyUri, HttpClientOptions vxClientOptions) {
+ this.resolveProxy(proxyUri).ifPresent(proxy -> {
+ ProxyOptions proxyOptions = new ProxyOptions();
+ proxyOptions.setHost(proxy.getHost());
+ proxyOptions.setPort(proxy.getPort());
+ proxyOptions.setType(translateProxyType(proxy));
+
+ proxy.getCredentials().ifPresent((credentials) -> {
+ proxyOptions.setUsername(credentials.getPrinciple());
+ proxyOptions.setPassword(credentials.getPasswordAsString());
+ });
+
+ vxClientOptions.setProxyOptions(proxyOptions);
+ });
+ }
+
/**
* Resolve a proxy, if there is one.
*
* @param resourceUri the URI that will be assessed against the proxy configuration
- * @return the proxy, if there is an appropriate one configured and it is not on the non-proxy list.
+ * @return the proxy, if there is an appropriate one configured, and it is not on the non-proxy list.
*/
public Optional<HttpProxy> resolveProxy(URI resourceUri) {
String scheme = resourceUri.getScheme();
@@ -57,6 +83,19 @@ public class SysPropsProxySelector {
return Optional.empty();
}
+ private ProxyType translateProxyType(HttpProxy proxy) {
+ switch(proxy.getProxyType()) {
+ case HTTP:
+ return ProxyType.HTTP;
+ case SOCKS4:
+ return ProxyType.SOCKS4;
+ case SOCKS5:
+ return ProxyType.SOCKS5;
+ default:
+ throw new IllegalStateException("Unexpected value: " + proxy.getProxyType());
+ }
+ }
+
@Override
public String toString() {
return new StringJoiner(", ", SysPropsProxySelector.class.getSimpleName() + "[", "]")

fix(gateway): apply Java proxy options to Vert.x connection

Take care of Java system property-derived proxy options for connection between the vert.x gateway
and the backend service.

Additionally, this fixes the plugin download in vert.x if a proxy is used.

Closes: #2250

Co-Authored-By: Marc Savy <marc@rhymewithgravy.com>