Skip to content

Commit

Permalink
Issue #5062 - KeyStoreScannerTest should use manual scanning to avoid…
Browse files Browse the repository at this point in the history
… timing issues

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Jul 21, 2020
1 parent 65de149 commit 38a9487
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public void fileRemoved(String filename)
reload();
}

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

_scanner.scan();
_scanner.scan();
}

@ManagedOperation(value = "Reload the SSL Keystore", impact = "ACTION")
public void reload()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.Calendar;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
Expand Down Expand Up @@ -57,10 +56,10 @@
@ExtendWith(WorkDirExtension.class)
public class KeyStoreScannerTest
{
private static final int scanInterval = 1;
public WorkDir testdir;
private Server server;
private Path keystoreDir;
private KeyStoreScanner keystoreScanner;

@BeforeEach
public void before()
Expand Down Expand Up @@ -99,8 +98,8 @@ public void start(Configuration configuration) throws Exception
server.addConnector(connector);

// Configure Keystore Reload.
KeyStoreScanner keystoreScanner = new KeyStoreScanner(sslContextFactory);
keystoreScanner.setScanInterval(scanInterval);
keystoreScanner = new KeyStoreScanner(sslContextFactory);
keystoreScanner.setScanInterval(0);
server.addBean(keystoreScanner);

server.start();
Expand All @@ -123,7 +122,7 @@ public void testKeystoreHotReload() throws Exception

// Switch to use newKeystore which has a later expiry date.
useKeystore("newKeystore");
Thread.sleep(Duration.ofSeconds(scanInterval * 2).toMillis());
keystoreScanner.scan();

// The scanner should have detected the updated keystore, expiry should be renewed.
X509Certificate cert2 = getCertificateFromServer();
Expand All @@ -143,11 +142,11 @@ public void testReloadWithBadKeystore() throws Exception
try (StacklessLogging ignored = new StacklessLogging(KeyStoreScanner.class))
{
useKeystore("badKeystore");
Thread.sleep(Duration.ofSeconds(scanInterval * 2).toMillis());
keystoreScanner.scan();
}

// The good keystore is removed, now the bad keystore now causes an exception.
assertThrows(Throwable.class, () -> getCertificateFromServer());
assertThrows(Throwable.class, this::getCertificateFromServer);
}

@Test
Expand All @@ -163,15 +162,15 @@ public void testKeystoreRemoval() throws Exception
try (StacklessLogging ignored = new StacklessLogging(KeyStoreScanner.class))
{
useKeystore(null);
Thread.sleep(Duration.ofSeconds(scanInterval * 2).toMillis());
keystoreScanner.scan();
}

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

// Switch to use keystore2 which has a later expiry date.
useKeystore("newKeystore");
Thread.sleep(Duration.ofSeconds(scanInterval * 2).toMillis());
keystoreScanner.scan();
X509Certificate cert2 = getCertificateFromServer();
assertThat(getExpiryYear(cert2), is(2020));
}
Expand All @@ -195,7 +194,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"));
Thread.sleep(Duration.ofSeconds(scanInterval * 2).toMillis());
keystoreScanner.scan();

// The scanner should have detected the updated keystore, expiry should be renewed.
X509Certificate cert2 = getCertificateFromServer();
Expand All @@ -220,7 +219,7 @@ public void testReloadChangingTargetOfSymbolicLink() throws Exception

// Change the target file of the symlink to the newKeystore which has a later expiry date.
useKeystore("newKeystore");
Thread.sleep(Duration.ofSeconds(scanInterval * 2).toMillis());
keystoreScanner.scan();

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

0 comments on commit 38a9487

Please sign in to comment.