Skip to content

Commit

Permalink
Improved KeyStoreScanner.scan().
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
  • Loading branch information
sbordet committed Dec 2, 2020
1 parent fe0e076 commit 9c882ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.io.File;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

Expand Down Expand Up @@ -119,23 +119,18 @@ public void fileRemoved(String filename)
}

@ManagedOperation(value = "Scan for changes in the SSL Keystore", impact = "ACTION")
public boolean scan()
public boolean scan(long waitMillis)
{
if (LOG.isDebugEnabled())
LOG.debug("scanning");

CompletableFuture<Boolean> cf = new CompletableFuture<>();
try
{
CountDownLatch complete = new CountDownLatch(2);
Callback callback = Callback.from(complete::countDown, t ->
{
LOG.warn("Scan fail", t);
complete.countDown();
});

_scanner.scan(callback);
_scanner.scan(callback);
return complete.await(10, TimeUnit.SECONDS);
// Perform 2 scans to be sure that the scan is stable.
_scanner.scan(Callback.from(() ->
_scanner.scan(Callback.from(() -> cf.complete(true), cf::completeExceptionally)), cf::completeExceptionally));
return cf.get(waitMillis, TimeUnit.MILLISECONDS);
}
catch (Exception e)
{
Expand All @@ -152,7 +147,8 @@ public void reload()
try
{
sslContextFactory.reload(scf ->
{});
{
});
}
catch (Throwable t)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void testKeystoreHotReload() throws Exception

// Switch to use newKeystore which has a later expiry date.
useKeystore("newKeystore");
assertTrue(keystoreScanner.scan());
assertTrue(keystoreScanner.scan(5000));

// The scanner should have detected the updated keystore, expiry should be renewed.
X509Certificate cert2 = getCertificateFromServer();
Expand All @@ -146,7 +146,7 @@ public void testReloadWithBadKeystore() throws Exception
try (StacklessLogging ignored = new StacklessLogging(KeyStoreScanner.class))
{
useKeystore("badKeystore");
keystoreScanner.scan();
keystoreScanner.scan(5000);
}

// The good keystore is removed, now the bad keystore now causes an exception.
Expand All @@ -167,15 +167,15 @@ public void testKeystoreRemoval() throws Exception
{
Path keystorePath = keystoreDir.resolve("keystore");
assertTrue(Files.deleteIfExists(keystorePath));
keystoreScanner.scan();
keystoreScanner.scan(5000);
}

// The good keystore is removed, having no keystore causes an exception.
assertThrows(Throwable.class, this::getCertificateFromServer);

// Switch to use keystore2 which has a later expiry date.
useKeystore("newKeystore");
keystoreScanner.scan();
keystoreScanner.scan(5000);
X509Certificate cert2 = getCertificateFromServer();
assertThat(getExpiryYear(cert2), is(2020));
}
Expand All @@ -200,7 +200,7 @@ public void testReloadChangingSymbolicLink() throws Exception
// Change the symlink to point to the newKeystore file location which has a later expiry date.
Files.delete(keystorePath);
Files.createSymbolicLink(keystorePath, useKeystore("newKeystore"));
keystoreScanner.scan();
keystoreScanner.scan(5000);

// The scanner should have detected the updated keystore, expiry should be renewed.
X509Certificate cert2 = getCertificateFromServer();
Expand Down Expand Up @@ -232,7 +232,7 @@ public void testReloadChangingTargetOfSymbolicLink() throws Exception
// Change the target file of the symlink to the newKeystore which has a later expiry date.
Files.copy(newKeystoreSrc, target, StandardCopyOption.REPLACE_EXISTING);
System.err.println("### Triggering scan");
keystoreScanner.scan();
keystoreScanner.scan(5000);

// The scanner should have detected the updated keystore, expiry should be renewed.
X509Certificate cert2 = getCertificateFromServer();
Expand Down

0 comments on commit 9c882ee

Please sign in to comment.