Skip to content

Commit

Permalink
Support Mutiny HTTPS with PEM certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik committed Sep 4, 2024
1 parent 571c920 commit 9380ea8
Showing 1 changed file with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import java.util.Objects;

import io.quarkus.test.security.certificate.CertificateBuilder;
import io.quarkus.test.security.certificate.PemClientCertificate;
import io.quarkus.test.services.Certificate;
import io.quarkus.test.services.URILike;
import io.quarkus.test.utils.TestExecutionProperties;
import io.restassured.RestAssured;
import io.restassured.specification.RequestSpecification;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.KeyStoreOptions;
import io.vertx.core.net.PemKeyCertOptions;
import io.vertx.core.net.PemTrustOptions;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.ext.web.client.WebClient;
Expand Down Expand Up @@ -130,30 +134,39 @@ public WebClient mutinyHttps(boolean verifyHost, String clientCertificateCn, boo
options.setDefaultHost(uri.getHost());
options.setDefaultPort(uri.getPort());

final String keystorePath;
final String truststorePath;
var certificate = certificateBuilder.certificates().get(0);

if (clientCertificateCn != null) {
var clientCert = certificate.getClientCertificateByCn(clientCertificateCn);
Objects.requireNonNull(clientCert, "Client certificate with CN %s not found".formatted(clientCertificateCn));
keystorePath = clientCert.keystorePath();
truststorePath = clientCert.truststorePath();
boolean isPemCertificate = Certificate.Format.PEM.toString().equals(certificate.format());
if (isPemCertificate && clientCertificateCn != null) {
var clientCert = (PemClientCertificate) certificate.getClientCertificateByCn(clientCertificateCn);
options.setPemKeyCertOptions(
new PemKeyCertOptions().addCertPath(clientCert.certPath()).addKeyPath(clientCert.keyPath()));
options.setPemTrustOptions(new PemTrustOptions().addCertPath(clientCert.truststorePath()));
} else {
keystorePath = certificate.keystorePath();
truststorePath = certificate.truststorePath();
}

if (keystorePath != null) {
options.setKeyCertOptions(
new KeyStoreOptions().setValue(Buffer.buffer(getFileContent(keystorePath)))
.setPassword(certificate.password()).setType(certificate.format()));
}

if (withTruststore && truststorePath != null) {
options.setTrustOptions(
new KeyStoreOptions().setValue(Buffer.buffer(getFileContent(truststorePath)))
.setPassword(certificate.password()).setType(certificate.format()));
final String keystorePath;
final String truststorePath;

if (clientCertificateCn != null) {
var clientCert = certificate.getClientCertificateByCn(clientCertificateCn);
Objects.requireNonNull(clientCert, "Client certificate with CN %s not found".formatted(clientCertificateCn));
keystorePath = clientCert.keystorePath();
truststorePath = clientCert.truststorePath();
} else {
keystorePath = certificate.keystorePath();
truststorePath = certificate.truststorePath();
}

if (keystorePath != null) {
options.setKeyCertOptions(
new KeyStoreOptions().setValue(Buffer.buffer(getFileContent(keystorePath)))
.setPassword(certificate.password()).setType(certificate.format()));
}

if (withTruststore && truststorePath != null) {
options.setTrustOptions(
new KeyStoreOptions().setValue(Buffer.buffer(getFileContent(truststorePath)))
.setPassword(certificate.password()).setType(certificate.format()));
}
}

var vertx = Vertx.vertx();
Expand Down

0 comments on commit 9380ea8

Please sign in to comment.