Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRIO (Testfix included): CustomKeyAndCertificateFactorySupplier added #1030

Merged
merged 8 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
import org.slf4j.event.Level;

import java.lang.reflect.Constructor;
import java.util.function.Function;

/**
* @author jamesdbloom
*/
public class KeyAndCertificateFactoryFactory {

private static Function<MockServerLogger, KeyAndCertificateFactory> customKeyAndCertificateFactorySupplier = null;

private static final ClassLoader CLASS_LOADER = KeyAndCertificateFactoryFactory.class.getClassLoader();

@SuppressWarnings("unchecked")
public static KeyAndCertificateFactory createKeyAndCertificateFactory(MockServerLogger mockServerLogger) {
if (ConfigurationProperties.useBouncyCastleForKeyAndCertificateGeneration()) {
if (customKeyAndCertificateFactorySupplier != null) {
return customKeyAndCertificateFactorySupplier.apply(mockServerLogger);
} else if (ConfigurationProperties.useBouncyCastleForKeyAndCertificateGeneration()) {
Class<?> bouncyCastleProvider = null;
Class<?> bouncyCastleX509Holder = null;
try {
Expand Down Expand Up @@ -70,4 +75,12 @@ public static KeyAndCertificateFactory createKeyAndCertificateFactory(MockServer
}
}

public static Function<MockServerLogger, KeyAndCertificateFactory> getCustomKeyAndCertificateFactorySupplier() {
return customKeyAndCertificateFactorySupplier;
}

public static void setCustomKeyAndCertificateFactorySupplier(
Function<MockServerLogger, KeyAndCertificateFactory> customKeyAndCertificateFactorySupplier) {
KeyAndCertificateFactoryFactory.customKeyAndCertificateFactorySupplier = customKeyAndCertificateFactorySupplier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.function.Function;
import javax.net.ssl.SSLException;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.mockserver.socket.tls.KeyAndCertificateFactoryFactory.createKeyAndCertificateFactory;
Expand All @@ -32,6 +34,15 @@
*/
public class NettySslContextFactory {

public static Function<SslContextBuilder, SslContext> clientSslContextBuilderFunction =
sslContextBuilder -> {
try {
return sslContextBuilder.build();
} catch (SSLException e) {
throw new RuntimeException(e);
}
};

private final MockServerLogger mockServerLogger;
private final KeyAndCertificateFactory keyAndCertificateFactory;
private SslContext clientSslContext = null;
Expand Down Expand Up @@ -72,7 +83,8 @@ public synchronized SslContext createClientSslContext(boolean forwardProxyClient
} else {
sslContextBuilder.trustManager(trustCertificateChain());
}
clientSslContext = sslContextBuilder.build();
clientSslContext = clientSslContextBuilderFunction
.apply(sslContextBuilder);
ConfigurationProperties.rebuildTLSContext(false);
} catch (Throwable throwable) {
throw new RuntimeException("Exception creating SSL context for client", throwable);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.mockserver.socket.tls;

import static org.junit.Assert.assertTrue;

import java.math.BigInteger;
import org.junit.AfterClass;
import org.junit.Test;
import org.mockserver.socket.tls.bouncycastle.BCKeyAndCertificateFactory;

public class CustomKeyAndCertificateFactorySupplierTest {

@Test
public void setSupplier_shouldUseSupplier() {
KeyAndCertificateFactory factoryInstance = new BCKeyAndCertificateFactory(null);
KeyAndCertificateFactoryFactory.setCustomKeyAndCertificateFactorySupplier(logger -> factoryInstance);

assertTrue("Should give exact instance",
KeyAndCertificateFactoryFactory.createKeyAndCertificateFactory(null)
== factoryInstance);
}

@AfterClass
public static void resetSupplier() {
KeyAndCertificateFactoryFactory.setCustomKeyAndCertificateFactorySupplier(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ rm -rf leaf-cert-and-key.json


# download certificates from domain
openssl s_client -host stackoverflow.com -port 443 -prexit -showcerts > stackoverflow-chain.pem
openssl s_client -host stackoverflow.com -port 443 -prexit -showcerts > stackoverflow-chain.pem

# finally update the chains