Use a programmatically-generated keystore in SniMapperTest

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-07-24 09:26:06 -04:00
1 parent 3447345d02
commit ac8d5f81bf
4 files changed
+63 -48

No files matched your search

+6
View File
@@ -492,6 +492,12 @@
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-pkitesting</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-testsuite-common</artifactId>
@@ -24,9 +24,15 @@ import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.pkitesting.CertificateBuilder;
import io.netty.pkitesting.X509Bundle;
import io.netty.util.Mapping;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -35,7 +41,10 @@ import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -47,20 +56,54 @@ class SniMapperTest {
// Configuration for precomputed keystore blob defined in sni-mapper-test-keystore.p12
private static final String FOO_DOMAIN = "foo.example.com";
private static final String BAR_DOMAIN = "bar.example.com";
private static final String KEY_STORE_PASSWORD = "password";
private static final String KEY_STORE_NAME = "sni-mapper-test-keystore.p12";
private DefaultEventLoopGroup eventLoopGroup;
private static final CertificateBuilder.Algorithm[] ALGORITHMS = new CertificateBuilder.Algorithm[] {
CertificateBuilder.Algorithm.ed25519,
CertificateBuilder.Algorithm.rsa2048
};
private static DefaultEventLoopGroup eventLoopGroup;
private static Mapping<String, SslContext> sniMapping;
private Channel serverChannel;
@BeforeAll
static void setUpBeforeAll() throws Exception {
final Instant now = Instant.now();
final KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(null);
final char[] keyStorePassword = RandomStringUtils.insecure().nextAlphanumeric(16).toCharArray();
for (final String domain : new String[] { FOO_DOMAIN, BAR_DOMAIN }) {
for (final CertificateBuilder.Algorithm algorithm : ALGORITHMS) {
final X509Bundle x509Bundle = new CertificateBuilder()
.notBefore(now)
.notAfter(now.plus(Duration.ofDays(1)))
.setIsCertificateAuthority(true)
.algorithm(algorithm)
.subject("CN=" + domain)
.addSanDnsName(domain)
.buildSelfSigned();
keyStore.setEntry(domain + "-" + algorithm,
new KeyStore.PrivateKeyEntry(x509Bundle.getKeyPair().getPrivate(), x509Bundle.getCertificatePath()),
new KeyStore.PasswordProtection(keyStorePassword));
}
}
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
keyStore.store(byteArrayOutputStream, keyStorePassword);
sniMapping = SniMapper.buildSniMapping(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()),
new String(keyStorePassword));
eventLoopGroup = new DefaultEventLoopGroup();
}
@BeforeEach
void setUp() throws Exception {
final InputStream keyStore = SniMapper.class.getResourceAsStream(KEY_STORE_NAME);
eventLoopGroup = new DefaultEventLoopGroup();
final Mapping<String, SslContext> sniMapping =
SniMapper.buildSniMapping(keyStore, KEY_STORE_PASSWORD);
final LocalAddress localAddress = new LocalAddress(SniMapper.class.getSimpleName());
serverChannel = new ServerBootstrap()
.group(eventLoopGroup)
@@ -81,13 +124,15 @@ class SniMapperTest {
if (serverChannel != null) {
serverChannel.close().sync();
}
}
@AfterAll
static void tearDownAfterAll() throws InterruptedException {
eventLoopGroup.shutdownGracefully(1, 1000, TimeUnit.MILLISECONDS).sync();
}
@Test
void unknownDomain() throws Exception {
final InputStream keyStore = SniMapper.class.getResourceAsStream(KEY_STORE_NAME);
final Mapping<String, SslContext> sniMapping = SniMapper.buildSniMapping(keyStore, KEY_STORE_PASSWORD);
assertNotNull(sniMapping.map("unknown.example.com"));
final X509Certificate defaultCertificate = connectAndGetServerCertificate("unknown.example.com", null);
@@ -1,36 +0,0 @@
#!/bin/sh
# Generates self-signed local testing certificates for SniMapperTest
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
WORK_DIR="$(mktemp -d)"
trap 'rm -rf "$WORK_DIR"' EXIT
PASSWORD="password"
DAYS=36500
SNI_KS="$SCRIPT_DIR/sni-mapper-test-keystore.p12"
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$WORK_DIR/foo-rsa.key" 2>/dev/null;
openssl req -new -x509 -key "$WORK_DIR/foo-rsa.key" -out "$WORK_DIR/foo-rsa.crt" -days "$DAYS" -subj "/CN=foo.example.com" -addext "subjectAltName=DNS:foo.example.com"
openssl genpkey -algorithm Ed25519 -out "$WORK_DIR/foo-ed25519.key" 2>/dev/null;
openssl req -new -x509 -key "$WORK_DIR/foo-ed25519.key" -out "$WORK_DIR/foo-ed25519.crt" -days "$DAYS" -subj "/CN=foo.example.com" -addext "subjectAltName=DNS:foo.example.com"
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "$WORK_DIR/bar-rsa.key" 2>/dev/null;
openssl req -new -x509 -key "$WORK_DIR/bar-rsa.key" -out "$WORK_DIR/bar-rsa.crt" -days "$DAYS" -subj "/CN=bar.example.com" -addext "subjectAltName=DNS:bar.example.com"
openssl genpkey -algorithm Ed25519 -out "$WORK_DIR/bar-ed25519.key" 2>/dev/null;
openssl req -new -x509 -key "$WORK_DIR/bar-ed25519.key" -out "$WORK_DIR/bar-ed25519.crt" -days "$DAYS" -subj "/CN=BAR.EXAMPLE.COM" -addext "subjectAltName=DNS:BAR.EXAMPLE.COM"
openssl pkcs12 -export -in "$WORK_DIR/foo-rsa.crt" -inkey "$WORK_DIR/foo-rsa.key" -out "$WORK_DIR/foo-rsa.p12" -name foo -passout pass:$PASSWORD
openssl pkcs12 -export -in "$WORK_DIR/foo-ed25519.crt" -inkey "$WORK_DIR/foo-ed25519.key" -out "$WORK_DIR/foo-ed25519.p12" -name foo-ed25519 -passout pass:$PASSWORD
openssl pkcs12 -export -in "$WORK_DIR/bar-rsa.crt" -inkey "$WORK_DIR/bar-rsa.key" -out "$WORK_DIR/bar-rsa.p12" -name bar -passout pass:$PASSWORD
openssl pkcs12 -export -in "$WORK_DIR/bar-ed25519.crt" -inkey "$WORK_DIR/bar-ed25519.key" -out "$WORK_DIR/bar-ed25519.p12" -name bar-ed25519 -passout pass:$PASSWORD
keytool -importkeystore -noprompt -srckeystore "$WORK_DIR/foo-ed25519.p12" -srcstoretype PKCS12 -srcstorepass $PASSWORD -destkeystore "$SNI_KS" -deststoretype PKCS12 -deststorepass $PASSWORD
keytool -importkeystore -noprompt -srckeystore "$WORK_DIR/foo-rsa.p12" -srcstoretype PKCS12 -srcstorepass $PASSWORD -destkeystore "$SNI_KS" -deststoretype PKCS12 -deststorepass $PASSWORD
keytool -importkeystore -noprompt -srckeystore "$WORK_DIR/bar-ed25519.p12" -srcstoretype PKCS12 -srcstorepass $PASSWORD -destkeystore "$SNI_KS" -deststoretype PKCS12 -deststorepass $PASSWORD
keytool -importkeystore -noprompt -srckeystore "$WORK_DIR/bar-rsa.p12" -srcstoretype PKCS12 -srcstorepass $PASSWORD -destkeystore "$SNI_KS" -deststoretype PKCS12 -deststorepass $PASSWORD
echo "Wrote 4 certificates for two SNIs to $SNI_KS"