fix(gateway-vertx): Remove TLS config caching
TLS config caching was causing issues when non-SSL/SSL connections
were at the same endpoint (first one would pin it to one or other).
Could reintroduce later if the additional effort to do this properly
is worth it.
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
@@ -88,7 +88,8 @@ public class ConnectorFactory implements IConnectorFactory {
.setHasDataPolicy(hasDataPolicy)
.setRequiredAuthType(authType)
.setTlsOptions(tlsOptions)
- .setUri(parseApiEndpoint(api));
+ .setUri(parseApiEndpoint(api))
+ .setSsl(api.getEndpoint().toLowerCase().startsWith("https")); //$NON-NLS-1$
// Get from cache
HttpClient client = clientFromCache(httpOptions);
return new HttpConnector(vertx, client, request, api, httpOptions, resultHandler);

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/HttpConnector.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/HttpConnector.java
@@ -40,6 +40,7 @@ import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
+import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
@@ -91,7 +92,6 @@ class HttpConnector implements IApiConnectionResponse, IApiConnection {
private String apiHost;
private String destination;
private int apiPort;
- private boolean isHttps;
private BasicAuthOptions basicOptions;
private HttpClient client;
@@ -125,7 +125,6 @@ class HttpConnector implements IApiConnectionResponse, IApiConnection {
this.apiEndpoint = options.getUri();
this.options = options;
- isHttps = apiEndpoint.getScheme().equals("https");
apiHost = apiEndpoint.getHost();
apiPort = getPort();
apiPath = apiEndpoint.getPath().isEmpty() || apiEndpoint.getPath().equals("/") ? "" : apiEndpoint.getPath();
@@ -139,18 +138,18 @@ class HttpConnector implements IApiConnectionResponse, IApiConnection {
if (apiEndpoint.getPort() != -1)
return apiEndpoint.getPort();
- return isHttps ? 443 : 80;
+ return options.isSsl() ? 443 : 80;
}
private void verifyConnection() {
switch (options.getRequiredAuthType()) {
case BASIC:
basicOptions = new BasicAuthOptions(api.getEndpointProperties());
- if (!isHttps && basicOptions.isRequireSSL())
+ if (!options.isSsl() && basicOptions.isRequireSSL())
throw new ConnectorException("Endpoint security requested (BASIC auth) but endpoint is not secure (SSL).");
break;
case MTLS:
- if (!isHttps)
+ if (!options.isSsl())
throw new ConnectorException("Mutual TLS specified, but endpoint is not HTTPS.");
break;
case DEFAULT:
@@ -160,8 +159,8 @@ class HttpConnector implements IApiConnectionResponse, IApiConnection {
private void doConnection() {
String endpoint = ApimanPathUtils.join(apiPath, destination + queryParams(apiRequest.getQueryParams()));
- logger.debug("Connecting to {0} | port: {1} verb: {2} path: {3}", apiHost, apiPort,
- HttpMethod.valueOf(apiRequest.getType()), endpoint);
+ logger.debug("Connecting to {0} | ssl?: {1} port: {2} verb: {3} path: {4}",
+ apiHost, options.isSsl(), apiPort, HttpMethod.valueOf(apiRequest.getType()), endpoint);
clientRequest = client.request(HttpMethod.valueOf(apiRequest.getType()),
apiPort,
@@ -314,6 +313,7 @@ class HttpConnector implements IApiConnectionResponse, IApiConnection {
private class ExceptionHandler implements Handler<Throwable> {
@Override
public void handle(Throwable error) {
+ // TODO better exception distinguishing.
resultHandler.handle(AsyncResultImpl
.<IApiConnectionResponse> create(error));
}

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/HttpConnectorOptions.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/connector/HttpConnectorOptions.java
@@ -36,6 +36,7 @@ public class HttpConnectorOptions {
private boolean tryUseCompression = HttpClientOptions.DEFAULT_TRY_USE_COMPRESSION;
private TLSOptions tlsOptions;
private URI endpoint;
+ private boolean isSsl;
/**
* @return the requiredAuthType
@@ -140,6 +141,15 @@ public class HttpConnectorOptions {
return this;
}
+ public HttpConnectorOptions setSsl(boolean isSsl) {
+ this.isSsl = isSsl;
+ return this;
+ }
+
+ public boolean isSsl() {
+ return isSsl;
+ }
+
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@@ -152,6 +162,7 @@ public class HttpConnectorOptions {
result = prime * result + (keepAlive ? 1231 : 1237);
result = prime * result + ((tlsOptions == null) ? 0 : tlsOptions.hashCode());
result = prime * result + (tryUseCompression ? 1231 : 1237);
+ result = prime * result + (isSsl ? 0 : 1);
return result;
}
/* (non-Javadoc)
@@ -181,4 +192,5 @@ public class HttpConnectorOptions {
return false;
return true;
}
+
}

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/http/HttpClientOptionsFactory.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/http/HttpClientOptionsFactory.java
@@ -25,9 +25,7 @@ import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import javax.net.ssl.SSLContext;
@@ -38,19 +36,9 @@ import javax.net.ssl.SSLContext;
*/
public class HttpClientOptionsFactory {
private static final String[] EMPTY = new String[]{};
- private static Map<TLSOptions, HttpClientOptions> configCache = new HashMap<>();
private static Logger log = LoggerFactory.getLogger(HttpClientOptionsFactory.class);
public static HttpClientOptions parseTlsOptions(TLSOptions tlsOptions, URI apiEndpoint) {
- if (configCache.containsKey(tlsOptions))
- return configCache.get(tlsOptions);
-
- HttpClientOptions clientOptions = doParse(tlsOptions, apiEndpoint);
- configCache.put(tlsOptions, clientOptions);
- return clientOptions;
- }
-
- private static HttpClientOptions doParse(TLSOptions tlsOptions, URI apiEndpoint) {
HttpClientOptions clientOptions = new HttpClientOptions();
if (apiEndpoint.getScheme().equals("http")) { //$NON-NLS-1$
@@ -60,7 +48,7 @@ public class HttpClientOptionsFactory {
}
clientOptions.setTrustAll(tlsOptions.isTrustSelfSigned() || tlsOptions.isDevMode())
- .setVerifyHost(!tlsOptions.isAllowAnyHost() || tlsOptions.isDevMode());
+ .setVerifyHost(!(tlsOptions.isAllowAnyHost() || tlsOptions.isDevMode()));
if (tlsOptions.getTrustStore() != null) {
clientOptions.setTrustStoreOptions(

fix(gateway-vertx): Remove TLS config caching

TLS config caching was causing issues when non-SSL/SSL connections
were at the same endpoint (first one would pin it to one or other).

Could reintroduce later if the additional effort to do this properly
is worth it.