Skip to content

Commit

Permalink
Merge pull request #27925 from Sgitario/rr_ssl
Browse files Browse the repository at this point in the history
Resteasy Rest Client: Fix truststore password issue with Vert.x
  • Loading branch information
geoand authored Sep 14, 2022
2 parents a5d62c2 + b653c64 commit 7a932c6
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public RestClientBuilderImpl trustStore(KeyStore trustStore) {
return this;
}

public RestClientBuilderImpl trustStore(KeyStore trustStore, String trustStorePassword) {
clientBuilder.trustStore(trustStore, trustStorePassword.toCharArray());
return this;
}

@Override
public RestClientBuilderImpl keyStore(KeyStore keyStore, String keystorePassword) {
clientBuilder.keyStore(keyStore, keystorePassword);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void configureShared(RestClientBuilder builder) {
}
}

private void configureSsl(RestClientBuilder builder) {
private void configureSsl(RestClientBuilderImpl builder) {

Optional<String> maybeTrustStore = oneOf(clientConfigByClassName().trustStore, clientConfigByConfigKey().trustStore,
configRoot.trustStore);
Expand Down Expand Up @@ -249,7 +249,7 @@ private void registerKeyStore(String keyStorePath, RestClientBuilder builder) {
}
}

private void registerTrustStore(String trustStorePath, RestClientBuilder builder) {
private void registerTrustStore(String trustStorePath, RestClientBuilderImpl builder) {
Optional<String> maybeTrustStorePassword = oneOf(clientConfigByClassName().trustStorePassword,
clientConfigByConfigKey().trustStorePassword, configRoot.trustStorePassword);
Optional<String> maybeTrustStoreType = oneOf(clientConfigByClassName().trustStoreType,
Expand All @@ -269,7 +269,7 @@ private void registerTrustStore(String trustStorePath, RestClientBuilder builder
e);
}

builder.trustStore(trustStore);
builder.trustStore(trustStore, password);
} catch (KeyStoreException e) {
throw new IllegalArgumentException("Failed to initialize trust store from " + trustStorePath, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testClientSpecificConfigs() {
Mockito.verify(restClientBuilderMock).register(MyResponseFilter1.class);
Mockito.verify(restClientBuilderMock).queryParamStyle(QueryParamStyle.COMMA_SEPARATED);

Mockito.verify(restClientBuilderMock).trustStore(Mockito.any());
Mockito.verify(restClientBuilderMock).trustStore(Mockito.any(), Mockito.anyString());
Mockito.verify(restClientBuilderMock).keyStore(Mockito.any(), Mockito.anyString());
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public void testGlobalConfigs() {
Mockito.verify(restClientBuilderMock).register(MyResponseFilter2.class);
Mockito.verify(restClientBuilderMock).queryParamStyle(QueryParamStyle.MULTI_PAIRS);

Mockito.verify(restClientBuilderMock).trustStore(Mockito.any());
Mockito.verify(restClientBuilderMock).trustStore(Mockito.any(), Mockito.anyString());
Mockito.verify(restClientBuilderMock).keyStore(Mockito.any(), Mockito.anyString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ClientBuilderImpl extends ClientBuilder {
private char[] keystorePassword;
private SSLContext sslContext;
private KeyStore trustStore;
private char[] trustStorePassword;

private String proxyHost;
private int proxyPort;
Expand Down Expand Up @@ -88,7 +89,12 @@ public ClientBuilder keyStore(KeyStore keyStore, char[] password) {

@Override
public ClientBuilder trustStore(KeyStore trustStore) {
return trustStore(trustStore, null);
}

public ClientBuilder trustStore(KeyStore trustStore, char[] password) {
this.trustStore = trustStore;
this.trustStorePassword = password;
return this;
}

Expand Down Expand Up @@ -164,7 +170,7 @@ public ClientBuilder clientLogger(ClientLogger clientLogger) {
@Override
public ClientImpl build() {
Buffer keyStore = asBuffer(this.keyStore, keystorePassword);
Buffer trustStore = asBuffer(this.trustStore, EMPTY_CHAR_ARARAY);
Buffer trustStore = asBuffer(this.trustStore, this.trustStorePassword);

HttpClientOptions options = Optional.ofNullable(configuration.getFromContext(HttpClientOptions.class))
.orElseGet(HttpClientOptions::new);
Expand All @@ -185,7 +191,7 @@ public ClientImpl build() {
if (trustStore != null) {
JksOptions jks = new JksOptions();
jks.setValue(trustStore);
jks.setPassword("");
jks.setPassword(trustStorePassword == null ? "" : new String(trustStorePassword));
options.setTrustStoreOptions(jks);
}
}
Expand Down
30 changes: 30 additions & 0 deletions integration-tests/rest-client-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<artifactId>quarkus-integration-test-rest-client-reactive</artifactId>
<name>Quarkus - Integration Tests - REST Client Reactive</name>

<properties>
<self-signed.trust-store>${project.build.directory}/self-signed.p12</self-signed.trust-store>
<self-signed.trust-store-password>changeit</self-signed.trust-store-password>
</properties>

<!--todo add ssl tests-->

<dependencies>
Expand Down Expand Up @@ -165,6 +170,31 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>uk.co.automatictester</groupId>
<artifactId>truststore-maven-plugin</artifactId>
<version>${truststore-maven-plugin.version}</version>
<executions>
<execution>
<id>self-signed-truststore</id>
<phase>generate-test-resources</phase>
<goals>
<goal>generate-truststore</goal>
</goals>
<configuration>
<truststoreFormat>PKCS12</truststoreFormat>
<truststoreFile>${self-signed.trust-store}</truststoreFile>
<truststorePassword>${self-signed.trust-store-password}</truststorePassword>
<servers>
<server>self-signed.badssl.com:443</server>
</servers>
<trustAllCertificates>true</trustAllCertificates>
<includeCertificates>LEAF</includeCertificates>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.quarkus.it.rest.client.main.MyResponseExceptionMapper.MyException;
import io.quarkus.it.rest.client.main.selfsigned.ExternalSelfSignedClient;
import io.smallrye.mutiny.Uni;
import io.vertx.core.Future;
import io.vertx.core.json.Json;
Expand All @@ -44,6 +45,9 @@ public class ClientCallingResource {
@RestClient
FaultToleranceOnInterfaceClient faultToleranceOnInterfaceClient;

@RestClient
ExternalSelfSignedClient externalSelfSignedClient;

@Inject
InMemorySpanExporter inMemorySpanExporter;

Expand Down Expand Up @@ -165,6 +169,9 @@ void init(@Observes Router router) {
});

router.get("/with%20space").handler(rc -> rc.response().setStatusCode(200).end());

router.get("/self-signed").blockingHandler(
rc -> rc.response().setStatusCode(200).end(String.valueOf(externalSelfSignedClient.invoke().getStatus())));
}

private Future<Void> success(RoutingContext rc, String body) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.it.rest.client.main.selfsigned;

import javax.ws.rs.GET;
import javax.ws.rs.core.Response;

import org.eclipse.microprofile.faulttolerance.Retry;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient(baseUri = "https://self-signed.badssl.com/", configKey = "self-signed")
public interface ExternalSelfSignedClient {

@GET
@Retry(delay = 1000)
Response invoke();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
w-exception-mapper/mp-rest/url=${test.url}
w-fault-tolerance/mp-rest/url=${test.url}
io.quarkus.it.rest.client.main.ParamClient/mp-rest/url=${test.url}
io.quarkus.it.rest.client.multipart.MultipartClient/mp-rest/url=${test.url}
io.quarkus.it.rest.client.multipart.MultipartClient/mp-rest/url=${test.url}
# HTTPS
quarkus.rest-client.self-signed.trust-store=${self-signed.trust-store}
quarkus.rest-client.self-signed.trust-store-password=${self-signed.trust-store-password}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.it.rest.client.selfsigned;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class ExternalSelfSignedTestCase {

@Test
public void should_accept_self_signed_certs() {
when()
.get("/self-signed")
.then()
.statusCode(200)
.body(is("200"));
}
}

0 comments on commit 7a932c6

Please sign in to comment.