-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced io.quarkus.restclient.runtime.NoopHostnameVerifier
This should be used in the rest-client when hostname verification needs to be disabled for a specific client endpoint Also tests were improved to use what's provided in the rest-client extension
- Loading branch information
Showing
13 changed files
with
135 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...rest-client/runtime/src/main/java/io/quarkus/restclient/runtime/NoopHostnameVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.quarkus.restclient.runtime; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLSession; | ||
|
||
/** | ||
* The {@link NoopHostnameVerifier} essentially turns hostname verification off. | ||
*/ | ||
public class NoopHostnameVerifier implements HostnameVerifier { | ||
|
||
@Override | ||
public boolean verify(String hostname, SSLSession session) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public final String toString() { | ||
return "NO_OP"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...t-client/src/main/java/io/quarkus/it/rest/client/selfsigned/ExternalSelfSignedClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkus.it.rest.client.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 | ||
Response invoke(); | ||
} |
32 changes: 8 additions & 24 deletions
32
...client/src/main/java/io/quarkus/it/rest/client/selfsigned/ExternalSelfSignedResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,29 @@ | ||
package io.quarkus.it.rest.client.selfsigned; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
|
||
/** | ||
* This has nothing to do with rest-client, but we add it here in order to avoid creating | ||
* a new integration test that would slow down our CI | ||
*/ | ||
@Path("/self-signed") | ||
public class ExternalSelfSignedResource { | ||
|
||
@Inject | ||
@RestClient | ||
ExternalSelfSignedClient client; | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String perform() throws IOException { | ||
try { | ||
return doGetCipher(); | ||
} catch (IOException e) { | ||
|
||
// if it fails it might be because the remote service is down, so sleep and try again | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException ignored) { | ||
|
||
} | ||
|
||
return doGetCipher(); | ||
} | ||
} | ||
|
||
private String doGetCipher() throws IOException { | ||
// this URL provides an always on example of an HTTPS URL utilizing self-signed certificate | ||
URL url = new URL("https://self-signed.badssl.com/"); | ||
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); | ||
con.setRequestMethod("GET"); | ||
con.getResponseCode(); | ||
return con.getCipherSuite(); | ||
return String.valueOf(client.invoke().getStatus()); | ||
} | ||
} |
10 changes: 7 additions & 3 deletions
10
...-tests/rest-client/src/main/java/io/quarkus/it/rest/client/wronghost/WrongHostClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
package io.quarkus.it.rest.client.wronghost; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("/") | ||
import org.eclipse.microprofile.faulttolerance.Retry; | ||
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; | ||
|
||
@RegisterRestClient(baseUri = "https://wrong.host.badssl.com/", configKey = "wrong-host") | ||
public interface WrongHostClient { | ||
|
||
@GET | ||
@Produces(MediaType.TEXT_PLAIN) | ||
String root(); | ||
@Retry | ||
Response invoke(); | ||
} |
26 changes: 9 additions & 17 deletions
26
...ests/rest-client/src/main/java/io/quarkus/it/rest/client/wronghost/WrongHostResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
package io.quarkus.it.rest.client.wronghost; | ||
|
||
import java.io.FileInputStream; | ||
import java.net.URL; | ||
import java.security.KeyStore; | ||
import java.io.IOException; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import org.apache.http.conn.ssl.NoopHostnameVerifier; | ||
import org.eclipse.microprofile.rest.client.RestClientBuilder; | ||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
|
||
@Path("/wrong-host") | ||
public class WrongHostResource { | ||
|
||
@Inject | ||
@RestClient | ||
WrongHostClient client; | ||
|
||
@GET | ||
@Path("/rest-client") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String restClient() throws Exception { | ||
KeyStore ks = KeyStore.getInstance("JKS"); | ||
|
||
// the system props are set in pom.xml and made available for native tests via RestClientTestResource | ||
ks.load(new FileInputStream(System.getProperty("rest-client.trustStore")), | ||
System.getProperty("rest-client.trustStorePassword").toCharArray()); | ||
|
||
return RestClientBuilder.newBuilder().baseUrl(new URL("https://wrong.host.badssl.com/")).trustStore(ks) | ||
.hostnameVerifier(NoopHostnameVerifier.INSTANCE) | ||
.build(WrongHostClient.class) | ||
.root(); | ||
public String perform() throws IOException { | ||
return String.valueOf(client.invoke().getStatus()); | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
...ion-tests/rest-client/src/test/java/io/quarkus/it/rest/client/RestClientTestResource.java
This file was deleted.
Oops, something went wrong.
8 changes: 3 additions & 5 deletions
8
...client/src/test/java/io/quarkus/it/rest/client/selfsigned/ExternalSelfSignedTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
package io.quarkus.it.rest.client.selfsigned; | ||
|
||
import static org.hamcrest.Matchers.empty; | ||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTest | ||
public class ExternalSelfSignedTestCase { | ||
|
||
@Test | ||
public void included() { | ||
RestAssured.when() | ||
when() | ||
.get("/self-signed") | ||
.then() | ||
.statusCode(200) | ||
.body(is(not(empty()))); | ||
.body(is("200")); | ||
} | ||
} |
13 changes: 5 additions & 8 deletions
13
...t-client/src/test/java/io/quarkus/it/rest/client/wronghost/ExternalWrongHostTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
package io.quarkus.it.rest.client.wronghost; | ||
|
||
import static org.hamcrest.Matchers.empty; | ||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.it.rest.client.RestClientTestResource; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(RestClientTestResource.class) | ||
@QuarkusTestResource(ExternalWrongHostTestResource.class) | ||
public class ExternalWrongHostTestCase { | ||
|
||
@Test | ||
public void restClient() { | ||
RestAssured.when() | ||
.get("/wrong-host/rest-client") | ||
when() | ||
.get("/wrong-host") | ||
.then() | ||
.statusCode(200) | ||
.body(is(not(empty()))); | ||
.body(is("200")); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ient/src/test/java/io/quarkus/it/rest/client/wronghost/ExternalWrongHostTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.quarkus.it.rest.client.wronghost; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
||
/** | ||
* The only point of this class is to propagate the properties when running the native tests | ||
*/ | ||
public class ExternalWrongHostTestResource implements QuarkusTestResourceLifecycleManager { | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
Map<String, String> result = new HashMap<>(); | ||
result.put("wrong-host/mp-rest/trustStore", System.getProperty("rest-client.trustStore")); | ||
result.put("wrong-host/mp-rest/trustStorePassword", System.getProperty("rest-client.trustStorePassword")); | ||
result.put("wrong-host/mp-rest/hostnameVerifier", "io.quarkus.restclient.runtime.NoopHostnameVerifier"); | ||
return result; | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
|
||
} | ||
} |