@Test public void testConcurrentImportTrustCertificate() throws Exception { X509Certificate certificate1 = generateCertificate(10, "concurrency-1", "ou", "o", "l", "st", "country"); X509Certificate certificate2 = generateCertificate(10, "concurrency-2", "ou", "o", "l", "st", "country"); KeyStoreManagerConfiguration configuration = createMockConfiguration(); KeystoreInstance trustStore = mock(KeystoreInstance.class); CountDownLatch block = new CountDownLatch(1); doAnswer(blockingAnswer(block)) .when(trustStore) .importTrustCertificate( any(Certificate.class), any(String.class), any(char[].class) ); KeyStoreManagerImpl manager = new KeyStoreManagerImpl(crypto, configuration, null, trustStore); ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1)); List<ListenableFuture<String>> futures = new ArrayList<>(); try { futures.add(service.submit(() -> { manager.importTrustCertificate(certificate1, "concurrency-1"); return "concurrency-1"; })); futures.add(service.submit(() -> { manager.importTrustCertificate(certificate2, "concurrency-2"); return "concurrency-2"; })); List<String> results = Futures.successfulAsList(futures).get(100, TimeUnit.MILLISECONDS); assertEquals(0, results.size()); } catch (TimeoutException e) { } finally { block.countDown(); service.shutdownNow(); } verify(trustStore, times(1)) .importTrustCertificate( any(Certificate.class), any(String.class), any(char[].class)); }
@Override public void importTrustCertificate(Certificate certificate, String alias) throws KeystoreException { trustedKeyStoreLock.writeLock().lock(); try { log.debug("Importing trust certificate w/alias: {}", alias); if (trustedKeyStore.getCertificate(alias) != null) { log.warn("Certificate already exists in trust-store w/alias: {}; replacing certificate", alias); trustedKeyStore.deleteEntry(alias, config.getTrustedKeyStorePassword()); } trustedKeyStore.importTrustCertificate(certificate, alias, config.getTrustedKeyStorePassword()); logTrustedCertificateAliases(trustedKeyStore); getTrustManagers(); } finally { trustedKeyStoreLock.writeLock().unlock(); } }
KeyStoreManagerImpl extends ComponentSupport implements KeyStoreManager { @Override public void importTrustCertificate(Certificate certificate, String alias) throws KeystoreException { trustedKeyStoreLock.writeLock().lock(); try { log.debug("Importing trust certificate w/alias: {}", alias); if (trustedKeyStore.getCertificate(alias) != null) { log.warn("Certificate already exists in trust-store w/alias: {}; replacing certificate", alias); trustedKeyStore.deleteEntry(alias, config.getTrustedKeyStorePassword()); } trustedKeyStore.importTrustCertificate(certificate, alias, config.getTrustedKeyStorePassword()); logTrustedCertificateAliases(trustedKeyStore); getTrustManagers(); } finally { trustedKeyStoreLock.writeLock().unlock(); } } }
KeyStoreManagerImpl extends ComponentSupport implements KeyStoreManager { @Override public void importTrustCertificate(Certificate certificate, String alias) throws KeystoreException { trustedKeyStoreLock.writeLock().lock(); try { log.debug("Importing trust certificate w/alias: {}", alias); if (trustedKeyStore.getCertificate(alias) != null) { log.warn("Certificate already exists in trust-store w/alias: {}; replacing certificate", alias); trustedKeyStore.deleteEntry(alias, config.getTrustedKeyStorePassword()); } trustedKeyStore.importTrustCertificate(certificate, alias, config.getTrustedKeyStorePassword()); logTrustedCertificateAliases(trustedKeyStore); getTrustManagers(); } finally { trustedKeyStoreLock.writeLock().unlock(); } } KeyStoreManagerImpl(final CryptoHelper crypto,
                             final KeyStoreStorageManager storageManager,
                             final KeyStoreManagerConfiguration config); @VisibleForTesting  KeyStoreManagerImpl(final CryptoHelper crypto,
                      final KeyStoreManagerConfiguration config,
                      final KeystoreInstance privateKeyStore,
                      final KeystoreInstance trustedKeyStore); }
KeyStoreManagerImpl extends ComponentSupport implements KeyStoreManager { @Override public void importTrustCertificate(Certificate certificate, String alias) throws KeystoreException { trustedKeyStoreLock.writeLock().lock(); try { log.debug("Importing trust certificate w/alias: {}", alias); if (trustedKeyStore.getCertificate(alias) != null) { log.warn("Certificate already exists in trust-store w/alias: {}; replacing certificate", alias); trustedKeyStore.deleteEntry(alias, config.getTrustedKeyStorePassword()); } trustedKeyStore.importTrustCertificate(certificate, alias, config.getTrustedKeyStorePassword()); logTrustedCertificateAliases(trustedKeyStore); getTrustManagers(); } finally { trustedKeyStoreLock.writeLock().unlock(); } } KeyStoreManagerImpl(final CryptoHelper crypto,
                             final KeyStoreStorageManager storageManager,
                             final KeyStoreManagerConfiguration config); @VisibleForTesting  KeyStoreManagerImpl(final CryptoHelper crypto,
                      final KeyStoreManagerConfiguration config,
                      final KeystoreInstance privateKeyStore,
                      final KeystoreInstance trustedKeyStore); @Override TrustManager[] getTrustManagers(); @Override KeyManager[] getKeyManagers(); @Override void importTrustCertificate(Certificate certificate, String alias); @Override void importTrustCertificate(String certificateInPEM, String alias); @Override Certificate getTrustedCertificate(String alias); @Override Collection<Certificate> getTrustedCertificates(); @Override void removeTrustCertificate(String alias); @Override void generateAndStoreKeyPair(final String commonName,
                                      final String organizationalUnit,
                                      final String organization,
                                      final String locality,
                                      final String state,
                                      final String country); @Override boolean isKeyPairInitialized(); @Override Certificate getCertificate(); @Override PrivateKey getPrivateKey(); @Override void removePrivateKey(); }
KeyStoreManagerImpl extends ComponentSupport implements KeyStoreManager { @Override public void importTrustCertificate(Certificate certificate, String alias) throws KeystoreException { trustedKeyStoreLock.writeLock().lock(); try { log.debug("Importing trust certificate w/alias: {}", alias); if (trustedKeyStore.getCertificate(alias) != null) { log.warn("Certificate already exists in trust-store w/alias: {}; replacing certificate", alias); trustedKeyStore.deleteEntry(alias, config.getTrustedKeyStorePassword()); } trustedKeyStore.importTrustCertificate(certificate, alias, config.getTrustedKeyStorePassword()); logTrustedCertificateAliases(trustedKeyStore); getTrustManagers(); } finally { trustedKeyStoreLock.writeLock().unlock(); } } KeyStoreManagerImpl(final CryptoHelper crypto,
                             final KeyStoreStorageManager storageManager,
                             final KeyStoreManagerConfiguration config); @VisibleForTesting  KeyStoreManagerImpl(final CryptoHelper crypto,
                      final KeyStoreManagerConfiguration config,
                      final KeystoreInstance privateKeyStore,
                      final KeystoreInstance trustedKeyStore); @Override TrustManager[] getTrustManagers(); @Override KeyManager[] getKeyManagers(); @Override void importTrustCertificate(Certificate certificate, String alias); @Override void importTrustCertificate(String certificateInPEM, String alias); @Override Certificate getTrustedCertificate(String alias); @Override Collection<Certificate> getTrustedCertificates(); @Override void removeTrustCertificate(String alias); @Override void generateAndStoreKeyPair(final String commonName,
                                      final String organizationalUnit,
                                      final String organization,
                                      final String locality,
                                      final String state,
                                      final String country); @Override boolean isKeyPairInitialized(); @Override Certificate getCertificate(); @Override PrivateKey getPrivateKey(); @Override void removePrivateKey();  }