Skip to content

Commit

Permalink
Add test for Mutual TLS
Browse files Browse the repository at this point in the history
Also allow the SSL URL to be injected into tests that need it.
  • Loading branch information
CSTDev committed Oct 22, 2019
1 parent ed371a1 commit e2f658b
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.hamcrest.core.Is.is;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -18,11 +17,15 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.restassured.RestAssured;
import io.vertx.ext.web.Router;

public class SslServerWithJksTest {

@TestHTTPResource(value = "/ssl", ssl = true)
URL url;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
Expand All @@ -41,8 +44,7 @@ public static void restoreRestAssured() {
}

@Test
public void testSslServerWithJKS() throws MalformedURLException {
URL url = new URL("https://localhost:8444/ssl");
public void testSslServerWithJKS() {
RestAssured.get(url).then().statusCode(200).body(is("ssl"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.hamcrest.core.Is.is;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -18,11 +17,15 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.restassured.RestAssured;
import io.vertx.ext.web.Router;

public class SslServerWithP12Test {

@TestHTTPResource(value = "/ssl", ssl = true)
URL url;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
Expand All @@ -41,8 +44,7 @@ public static void restoreRestAssured() {
}

@Test
public void testSslServerWithPkcs12() throws MalformedURLException {
URL url = new URL("https://localhost:8444/ssl");
public void testSslServerWithPkcs12() {
RestAssured.get(url).then().statusCode(200).body(is("ssl"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.hamcrest.core.Is.is;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -18,11 +17,15 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.restassured.RestAssured;
import io.vertx.ext.web.Router;

public class SslServerWithPemTest {

@TestHTTPResource(value = "/ssl", ssl = true)
URL url;

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
Expand All @@ -42,8 +45,7 @@ public static void restoreRestAssured() {
}

@Test
public void testSslServerWithPem() throws MalformedURLException {
URL url = new URL("https://localhost:8444/ssl");
public void testSslServerWithPem() {
RestAssured.get(url).then().statusCode(200).body(is("ssl"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
vertx.event-loops.size=2
vertx.event-loops.size=2
quarkus.http.ssl.certificate.key-store-file=server-keystore.jks
quarkus.http.ssl.certificate.key-store-password=password
quarkus.http.ssl.certificate.trust-store-file=server-truststore.jks
quarkus.http.ssl.certificate.trust-store-password=password
quarkus.http.ssl.client-auth=REQUIRED
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package io.quarkus.it.vertx;

import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;

import java.net.URL;

import org.junit.jupiter.api.Test;

import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;

@QuarkusTest
public class VertxProducerResourceTest {

@TestHTTPResource(ssl = true)
URL url;

@Test
public void testInjection() {
get("/").then().body(containsString("vert.x has been injected"));
Expand All @@ -20,4 +29,15 @@ public void testRouteRegistration() {
get("/my-path").then().body(containsString("OK"));
}

@Test
public void testRouteRegistrationMTLS() {
RequestSpecification spec = new RequestSpecBuilder()
.setBaseUri(String.format("%s://%s", url.getProtocol(), url.getHost()))
.setPort(url.getPort())
.setKeyStore("client-keystore.jks", "password")
.setTrustStore("client-truststore.jks", "password")
.build();
given().spec(spec).get("/my-path").then().body(containsString("OK"));
}

}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
* @return The path part of the URL
*/
String value() default "";

/**
*
* @return If the URL should use the HTTPS protocol and SSL port
*/
boolean ssl() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ public static String getUri() {
return "http://" + host + ":" + port + contextPath;
}

public static String getSslUri() {
Config config = ConfigProvider.getConfig();
String host = config.getOptionalValue("quarkus.http.host", String.class).orElse("localhost");
String port = config.getOptionalValue("quarkus.http.test-ssl-port", String.class).orElse("8444");
String contextPath = config.getOptionalValue("quarkus.servlet.context-path", String.class).orElse("");
return "https://" + host + ":" + port + contextPath;
}

public static void inject(Object testCase) {
Map<Class<?>, TestHTTPResourceProvider<?>> providers = getProviders();
Class<?> c = testCase.getClass();
Expand All @@ -34,10 +42,18 @@ public static void inject(Object testCase) {
}
String path = resource.value();
String val;
if (path.startsWith("/")) {
val = getUri() + path;
if (resource.ssl()) {
if (path.startsWith("/")) {
val = getSslUri() + path;
} else {
val = getSslUri() + "/" + path;
}
} else {
val = getUri() + "/" + path;
if (path.startsWith("/")) {
val = getUri() + path;
} else {
val = getUri() + "/" + path;
}
}
f.setAccessible(true);
try {
Expand Down

0 comments on commit e2f658b

Please sign in to comment.