Skip to content

Commit

Permalink
Merge pull request #1280 from michalvavrik/feature/support-mutiny-htt…
Browse files Browse the repository at this point in the history
…ps-with-pem

Support Mutiny HTTPS with PEM certificates
  • Loading branch information
michalvavrik authored Sep 4, 2024
2 parents 4f41e55 + 9380ea8 commit be1e2bd
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 be1e2bd

Please sign in to comment.