|
| 1 | +package com.baeldung.jersey.https; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.security.KeyManagementException; |
| 11 | +import java.security.KeyStoreException; |
| 12 | +import java.security.NoSuchAlgorithmException; |
| 13 | +import java.security.UnrecoverableKeyException; |
| 14 | +import java.security.cert.CertificateException; |
| 15 | + |
| 16 | +import javax.net.ssl.SSLContext; |
| 17 | + |
| 18 | +import org.apache.hc.core5.ssl.SSLContextBuilder; |
| 19 | +import org.junit.jupiter.api.AfterEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 23 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 24 | + |
| 25 | +import jakarta.ws.rs.client.Client; |
| 26 | +import jakarta.ws.rs.client.ClientBuilder; |
| 27 | +import jakarta.ws.rs.core.Response; |
| 28 | + |
| 29 | +/** |
| 30 | + * 1. run {@code gen-keys.sh api.service} to generate server certificate/stores |
| 31 | + * 2. specify system properties with the directory of the certificates and password |
| 32 | + * 3. create api.service entries for 127.0.0.1 in /etc/hosts |
| 33 | + * 4. run this class |
| 34 | + */ |
| 35 | +class JerseyHttpsClientManualTest { |
| 36 | + |
| 37 | + static final String MOCK_HOST = "api.service"; |
| 38 | + static final String CERTS_DIR = System.getProperty("certs.dir"); |
| 39 | + static final String PASSWORD = System.getProperty("certs.password"); |
| 40 | + |
| 41 | + WireMockServer api; |
| 42 | + |
| 43 | + void setup(boolean mTls) { |
| 44 | + api = mockHttpsServer(mTls); |
| 45 | + api.stubFor(get(urlEqualTo("/test")).willReturn(aResponse().withHeader("Content-Type", "text/plain") |
| 46 | + .withStatus(200) |
| 47 | + .withBody("ok"))); |
| 48 | + api.start(); |
| 49 | + |
| 50 | + System.out.println(">> Mock server started on HTTPS port: " + api.httpsPort()); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterEach |
| 54 | + void teardown() { |
| 55 | + if (api != null) { |
| 56 | + api.stop(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void whenUsingJVMParameters_thenCorrectCertificateUsed() { |
| 62 | + setup(false); |
| 63 | + |
| 64 | + System.setProperty("javax.net.ssl.trustStore", CERTS_DIR + "/trust." + MOCK_HOST + ".p12"); |
| 65 | + System.setProperty("javax.net.ssl.trustStorePassword", PASSWORD); |
| 66 | + |
| 67 | + Response response = ClientBuilder.newClient().target(String.format("https://%s:%d/test", api.getOptions().bindAddress(), api.httpsPort())) |
| 68 | + .request() |
| 69 | + .get(); |
| 70 | + |
| 71 | + assertEquals(200, response.getStatus()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void whenUsingCustomSSLContext_thenCorrectCertificateUsed() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { |
| 76 | + setup(false); |
| 77 | + |
| 78 | + SSLContext sslContext = SSLContextBuilder.create() |
| 79 | + .loadTrustMaterial(Paths.get(CERTS_DIR + "/trust." + MOCK_HOST + ".p12"), PASSWORD.toCharArray()) |
| 80 | + .build(); |
| 81 | + |
| 82 | + Client client = ClientBuilder.newBuilder() |
| 83 | + .sslContext(sslContext) |
| 84 | + .build(); |
| 85 | + |
| 86 | + Response response = client.target(String.format("https://%s:%d/test", api.getOptions().bindAddress(), api.httpsPort())) |
| 87 | + .request() |
| 88 | + .get(); |
| 89 | + assertEquals(200, response.getStatus()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void whenUsingMutualTLS_thenCorrectCertificateUsed() throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException { |
| 94 | + setup(true); |
| 95 | + |
| 96 | + char[] password = PASSWORD.toCharArray(); |
| 97 | + |
| 98 | + SSLContext sslContext = SSLContextBuilder.create() |
| 99 | + .loadTrustMaterial(Paths.get(CERTS_DIR + "/trust." + MOCK_HOST + ".p12"), password) |
| 100 | + .loadKeyMaterial(Paths.get(CERTS_DIR + "/client." + MOCK_HOST + ".p12"), password, password) |
| 101 | + .build(); |
| 102 | + |
| 103 | + Client client = ClientBuilder.newBuilder() |
| 104 | + .sslContext(sslContext) |
| 105 | + .build(); |
| 106 | + |
| 107 | + Response response = client.target(String.format("https://%s:%d/test", api.getOptions().bindAddress(), api.httpsPort())) |
| 108 | + .request() |
| 109 | + .get(); |
| 110 | + assertEquals(200, response.getStatus()); |
| 111 | + } |
| 112 | + |
| 113 | + private static WireMockServer mockHttpsServer(boolean mTls) { |
| 114 | + WireMockConfiguration config = WireMockConfiguration.options() |
| 115 | + .bindAddress(MOCK_HOST) |
| 116 | + .dynamicPort() |
| 117 | + .dynamicHttpsPort() |
| 118 | + .keystorePath(CERTS_DIR + "/server." + MOCK_HOST + ".p12") |
| 119 | + .keystorePassword(PASSWORD) |
| 120 | + .keyManagerPassword(PASSWORD); |
| 121 | + |
| 122 | + if (mTls) { |
| 123 | + config.trustStorePath(CERTS_DIR + "/trust." + MOCK_HOST + ".p12") |
| 124 | + .trustStorePassword(PASSWORD) |
| 125 | + .needClientAuth(true); |
| 126 | + } |
| 127 | + |
| 128 | + return new WireMockServer(config); |
| 129 | + } |
| 130 | +} |
0 commit comments