Skip to content

Commit

Permalink
fix: fix to add certs to KeyStore in KeyManagerProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
equanz committed Nov 27, 2024
1 parent 387a96d commit 3492aab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import java.security.Principal;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedKeyManager;
Expand Down Expand Up @@ -88,18 +91,16 @@ private void updateKeyManager()
return;
}

X509Certificate certificate;
PrivateKey privateKey = null;
KeyStore keyStore;
try (InputStream publicCertStream = new FileInputStream(certFile.getFileName());
InputStream privateKeyStream = new FileInputStream(keyFile.getFileName())) {
final KeyStore keyStore;
try (InputStream publicCertStream = new FileInputStream(certFile.getFileName())) {
final CertificateFactory cf = CertificateFactory.getInstance("X.509");
certificate = (X509Certificate) cf.generateCertificate(publicCertStream);
final List<X509Certificate> certificateList = cf.generateCertificates(publicCertStream)
.stream().map(o -> (X509Certificate) o).collect(Collectors.toList());
keyStore = KeyStore.getInstance("JKS");
String alias = certificate.getSubjectX500Principal().getName();
privateKey = SecurityUtility.loadPrivateKeyFromPemFile(keyFile.getFileName());
final String alias = certificateList.get(0).getSubjectX500Principal().getName();
final PrivateKey privateKey = SecurityUtility.loadPrivateKeyFromPemFile(keyFile.getFileName());
keyStore.load(null);
keyStore.setKeyEntry(alias, privateKey, KEYSTORE_PASSWORD, new X509Certificate[] { certificate });
keyStore.setKeyEntry(alias, privateKey, KEYSTORE_PASSWORD, certificateList.toArray(new Certificate[0]));
} catch (IOException | KeyManagementException e) {
throw new IllegalArgumentException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.apache.pulsar.common.util;

import static org.testng.Assert.assertEquals;
import com.google.common.io.Resources;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class KeyManagerProxyTest {

@DataProvider(name = "certDataProvider")
public static Object[][] caDataProvider() {
return new Object[][]{
{"ca/multiple-ca.pem", 2},
{"ca/single-ca.pem", 1}
};
}

@Test(dataProvider = "certDataProvider")
public void testLoadCert(String path, int certCount) {
final String certFilePath = Resources.getResource(path).getPath();
// This key is not paired with certs, but this is not a problem as the key is not used in this test
final String keyFilePath = Resources.getResource("ssl/my-ca/client-key.pem").getPath();
final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();

final KeyManagerProxy keyManager = new KeyManagerProxy(certFilePath, keyFilePath, 60, scheduledExecutor);
assertEquals(keyManager.getCertificateChain("cn=test1").length, certCount);
}
}

0 comments on commit 3492aab

Please sign in to comment.