Skip to content

Commit

Permalink
fix: remove misleading options in vertx client factory
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitgravitee committed Jun 10, 2024
1 parent f627ddd commit 915cd5a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
* @author Yann TAVERNIER (yann.tavernier at graviteesource.com)
Expand Down Expand Up @@ -58,13 +56,6 @@ public class VertxHttpClientOptions implements Serializable {
private boolean pipelining = DEFAULT_PIPELINING;
private int maxConcurrentConnections = DEFAULT_MAX_CONCURRENT_CONNECTIONS;
private boolean useCompression = DEFAULT_USE_COMPRESSION;
private boolean propagateClientAcceptEncoding = DEFAULT_PROPAGATE_CLIENT_ACCEPT_ENCODING;
private boolean followRedirects = DEFAULT_FOLLOW_REDIRECTS;
private boolean clearTextUpgrade = DEFAULT_CLEAR_TEXT_UPGRADE;
private VertxHttpProtocolVersion version = DEFAULT_PROTOCOL_VERSION;

public boolean isPropagateClientAcceptEncoding() {
// Propagate Accept-Encoding can only be made if useCompression is disabled.
return !useCompression && propagateClientAcceptEncoding;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.gravitee.node.vertx.client.http;

import static io.gravitee.node.vertx.client.http.VertxHttpClientFactory.HTTP_SSL_OPENSSL_CONFIGURATION;
import static io.gravitee.node.vertx.client.http.VertxHttpProtocolVersion.HTTP_2;
import static io.gravitee.node.vertx.client.http.VertxHttpProxyType.HTTP;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.gravitee.node.api.configuration.Configuration;
import io.gravitee.node.vertx.client.ssl.SslOptions;
import io.gravitee.node.vertx.client.ssl.jks.JKSKeyStore;
Expand Down Expand Up @@ -37,14 +38,34 @@ class VertxHttpClientFactoryTest {

protected static final String PASSWORD = "gravitee";
private final Vertx vertx = Vertx.vertx();
private final ObjectMapper mapper = new ObjectMapper();

@Mock
private Configuration nodeConfiguration;

private VertxHttpClientFactory.VertxHttpClientFactoryBuilder builder() throws Exception {
final VertxHttpClientOptions httpOptions = mapper.readValue(HTTP_CONFIG, VertxHttpClientOptions.class);
final VertxHttpProxyOptions proxyOptions = mapper.readValue(PROXY_CONFIG, VertxHttpProxyOptions.class);
private VertxHttpClientFactory.VertxHttpClientFactoryBuilder builder() {
final VertxHttpClientOptions httpOptions = VertxHttpClientOptions
.builder()
.keepAlive(true)
.readTimeout(10000)
.idleTimeout(60000)
.keepAliveTimeout(30000)
.connectTimeout(5000)
.useCompression(false)
.maxConcurrentConnections(100)
.version(HTTP_2)
.pipelining(false)
.clearTextUpgrade(true)
.build();
final VertxHttpProxyOptions proxyOptions = VertxHttpProxyOptions
.builder()
.enabled(true)
.useSystemProxy(false)
.host("localhost")
.port(8080)
.username("user")
.password("pwd")
.type(HTTP)
.build();

when(nodeConfiguration.getProperty(HTTP_SSL_OPENSSL_CONFIGURATION, Boolean.class, false)).thenReturn(false);

Expand All @@ -60,10 +81,17 @@ private VertxHttpClientFactory.VertxHttpClientFactoryBuilder builder() throws Ex
}

@Test
void should_build_client_with_system_proxy() throws Exception {
final VertxHttpProxyOptions proxyOptions = mapper.readValue(PROXY_CONFIG, VertxHttpProxyOptions.class);
proxyOptions.setUseSystemProxy(true);

void should_build_client_with_system_proxy() {
final VertxHttpProxyOptions proxyOptions = VertxHttpProxyOptions
.builder()
.enabled(true)
.useSystemProxy(true)
.host("localhost")
.port(8080)
.username("user")
.password("pwd")
.type(HTTP)
.build();
final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().proxyOptions(proxyOptions);
final HttpClient httpClient = VertxHttpClientFactoryBuilder.build().createHttpClient();

Expand All @@ -74,18 +102,19 @@ void should_build_client_with_system_proxy() throws Exception {
class PEM {

@Test
void should_throw_illegal_argument_exception_with_PEM_trustStore_missing_path_or_content() throws Exception {
void should_throw_illegal_argument_exception_with_PEM_trustStore_missing_path_or_content() {
final SslOptions sslOptions = new SslOptions();
final PEMTrustStore trustStore = new PEMTrustStore();
sslOptions.setTrustStore(trustStore);

final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);
final VertxHttpClientFactory.VertxHttpClientFactoryBuilder vertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);

assertThrows(IllegalArgumentException.class, () -> VertxHttpClientFactoryBuilder.build().createHttpClient());
VertxHttpClientFactory factory = vertxHttpClientFactoryBuilder.build();
assertThrows(IllegalArgumentException.class, () -> factory.createHttpClient());
}

@Test
void should_build_client_with_PEM_trustStore_path() throws Exception {
void should_build_client_with_PEM_trustStore_path() {
final String location = getSslFilePath("truststore.pem");

final SslOptions sslOptions = new SslOptions();
Expand Down Expand Up @@ -117,32 +146,34 @@ void should_build_client_with_PEM_trustStore_content() throws Exception {
}

@Test
void should_throw_illegal_argument_exception_with_PEM_cert_missing_path_or_content() throws Exception {
void should_throw_illegal_argument_exception_with_PEM_cert_missing_path_or_content() {
final SslOptions sslOptions = new SslOptions();
final PEMKeyStore keystore = new PEMKeyStore();
sslOptions.setKeyStore(keystore);

final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);
final VertxHttpClientFactory.VertxHttpClientFactoryBuilder vertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);

assertThrows(IllegalArgumentException.class, () -> VertxHttpClientFactoryBuilder.build().createHttpClient());
VertxHttpClientFactory factory = vertxHttpClientFactoryBuilder.build();
assertThrows(IllegalArgumentException.class, () -> factory.createHttpClient());
}

@Test
void should_throw_illegal_argument_exception_with_pem_key_missing_path_or_content() throws Exception {
void should_throw_illegal_argument_exception_with_pem_key_missing_path_or_content() {
final String certLocation = getSslFilePath("client.cer");

final SslOptions sslOptions = new SslOptions();
final PEMKeyStore keyStore = new PEMKeyStore();
sslOptions.setKeyStore(keyStore);
keyStore.setCertPath(certLocation);

final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);
final VertxHttpClientFactory.VertxHttpClientFactoryBuilder vertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);

assertThrows(IllegalArgumentException.class, () -> VertxHttpClientFactoryBuilder.build().createHttpClient());
VertxHttpClientFactory factory = vertxHttpClientFactoryBuilder.build();
assertThrows(IllegalArgumentException.class, () -> factory.createHttpClient());
}

@Test
void should_build_client_with_pem_key_store_path() throws Exception {
void should_build_client_with_pem_key_store_path() {
final String keyLocation = getSslFilePath("client.key");
final String certLocation = getSslFilePath("client.cer");

Expand Down Expand Up @@ -182,18 +213,19 @@ void should_build_client_with_pem_keystore_content() throws Exception {
class PKCS12 {

@Test
void should_throw_illegal_argument_exception_with_pkcs12_trust_store_missing_path_or_content() throws Exception {
void should_throw_illegal_argument_exception_with_pkcs12_trust_store_missing_path_or_content() {
final SslOptions sslOptions = new SslOptions();
final PKCS12TrustStore trustStore = new PKCS12TrustStore();
sslOptions.setTrustStore(trustStore);

final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);
final VertxHttpClientFactory.VertxHttpClientFactoryBuilder vertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);

assertThrows(IllegalArgumentException.class, () -> VertxHttpClientFactoryBuilder.build().createHttpClient());
VertxHttpClientFactory factory = vertxHttpClientFactoryBuilder.build();
assertThrows(IllegalArgumentException.class, () -> factory.createHttpClient());
}

@Test
void should_build_client_with_pkcs12_trust_store_path() throws Exception {
void should_build_client_with_pkcs12_trust_store_path() {
final String location = getSslFilePath("truststore.p12");

final SslOptions sslOptions = new SslOptions();
Expand Down Expand Up @@ -225,18 +257,19 @@ void should_build_client_with_pkcs12_trust_store_content() throws Exception {
}

@Test
void should_throw_illegal_argument_exception_with_pkcs12_key_store_missing_path_or_content() throws Exception {
void should_throw_illegal_argument_exception_with_pkcs12_key_store_missing_path_or_content() {
final SslOptions sslOptions = new SslOptions();
final PKCS12KeyStore keyStore = new PKCS12KeyStore();
sslOptions.setKeyStore(keyStore);

final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);
final VertxHttpClientFactory.VertxHttpClientFactoryBuilder vertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);

assertThrows(IllegalArgumentException.class, () -> VertxHttpClientFactoryBuilder.build().createHttpClient());
VertxHttpClientFactory factory = vertxHttpClientFactoryBuilder.build();
assertThrows(IllegalArgumentException.class, () -> factory.createHttpClient());
}

@Test
void should_build_client_with_pkcs12_key_store_path() throws Exception {
void should_build_client_with_pkcs12_key_store_path() {
final String location = getSslFilePath("client.p12");

final SslOptions sslOptions = new SslOptions();
Expand Down Expand Up @@ -274,18 +307,19 @@ void should_build_client_with_pkcs12_keystore_content() throws Exception {
class JKS {

@Test
void should_throw_illegal_argument_exception_with_jks_trust_store_missing_path_or_content() throws Exception {
void should_throw_illegal_argument_exception_with_jks_trust_store_missing_path_or_content() {
final SslOptions sslOptions = new SslOptions();
final JKSTrustStore trustStore = new JKSTrustStore();
sslOptions.setTrustStore(trustStore);

final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);

assertThrows(IllegalArgumentException.class, () -> VertxHttpClientFactoryBuilder.build().createHttpClient());
VertxHttpClientFactory factory = VertxHttpClientFactoryBuilder.build();
assertThrows(IllegalArgumentException.class, () -> factory.createHttpClient());
}

@Test
void should_build_client_with_jks_trust_store_path() throws Exception {
void should_build_client_with_jks_trust_store_path() {
final String location = getSslFilePath("truststore.jks");

final SslOptions sslOptions = new SslOptions();
Expand Down Expand Up @@ -317,18 +351,19 @@ void should_build_client_with_jks_trust_store_content() throws Exception {
}

@Test
void should_throw_illegal_argument_exception_with_jks_key_store_missing_path_or_content() throws Exception {
void should_throw_illegal_argument_exception_with_jks_key_store_missing_path_or_content() {
final SslOptions sslOptions = new SslOptions();
final JKSKeyStore keyStore = new JKSKeyStore();
sslOptions.setKeyStore(keyStore);

final VertxHttpClientFactory.VertxHttpClientFactoryBuilder VertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);
final VertxHttpClientFactory.VertxHttpClientFactoryBuilder vertxHttpClientFactoryBuilder = builder().sslOptions(sslOptions);

assertThrows(IllegalArgumentException.class, () -> VertxHttpClientFactoryBuilder.build().createHttpClient());
VertxHttpClientFactory factory = vertxHttpClientFactoryBuilder.build();
assertThrows(IllegalArgumentException.class, () -> factory.createHttpClient());
}

@Test
void should_build_client_with_jks_key_store_path() throws Exception {
void should_build_client_with_jks_key_store_path() {
final String location = getSslFilePath("client.jks");

final SslOptions sslOptions = new SslOptions();
Expand Down Expand Up @@ -390,33 +425,4 @@ private static String getContentAsBase64(String file) throws IOException {
)
);
}

private static final String HTTP_CONFIG =
"""
{
"keepAlive": true,
"followRedirects": false,
"readTimeout": 10000,
"idleTimeout": 60000,
"keepAliveTimeout": 30000,
"connectTimeout": 5000,
"propagateClientAcceptEncoding": true,
"useCompression": false,
"maxConcurrentConnections": 100,
"version": "HTTP_2",
"pipelining": false,
"clearTextUpgrade": true
}""";

private static final String PROXY_CONFIG =
"""
{
"enabled": true,
"useSystemProxy": false,
"host": "localhost",
"port": 8080,
"username": "user",
"password": "pwd",
"type": "HTTP"
}""";
}

0 comments on commit 915cd5a

Please sign in to comment.