3636import java .security .cert .X509Certificate ;
3737import java .time .Duration ;
3838import java .util .Arrays ;
39+ import java .util .Optional ;
3940import java .util .concurrent .Executors ;
4041import java .util .concurrent .ScheduledExecutorService ;
4142import java .util .concurrent .TimeUnit ;
@@ -68,12 +69,12 @@ public class ReloadingKeyManagerFactory extends KeyManagerFactory implements Aut
6869 *
6970 * @param keystorePath the keystore file to reload
7071 * @param keystorePassword the keystore password
71- * @param reloadInterval the duration between reload attempts. Set to {@link
72- * java.time.Duration#ZERO} to disable scheduled reloading.
72+ * @param reloadInterval the duration between reload attempts. Set to {@link Optional#empty()} to
73+ * disable scheduled reloading.
7374 * @return
7475 */
75- public static ReloadingKeyManagerFactory create (
76- Path keystorePath , String keystorePassword , Duration reloadInterval )
76+ static ReloadingKeyManagerFactory create (
77+ Path keystorePath , String keystorePassword , Optional < Duration > reloadInterval )
7778 throws UnrecoverableKeyException , KeyStoreException , NoSuchAlgorithmException ,
7879 CertificateException , IOException {
7980 KeyManagerFactory kmf = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
@@ -103,14 +104,24 @@ private ReloadingKeyManagerFactory(Spi spi, Provider provider, String algorithm)
103104 this .spi = spi ;
104105 }
105106
106- private void start (Path keystorePath , String keystorePassword , Duration reloadInterval ) {
107+ private void start (
108+ Path keystorePath , String keystorePassword , Optional <Duration > reloadInterval ) {
107109 this .keystorePath = keystorePath ;
108110 this .keystorePassword = keystorePassword ;
109111
110112 // Ensure that reload is called once synchronously, to make sure the file exists etc.
111113 reload ();
112114
113- if (!reloadInterval .isZero ()) {
115+ if (!reloadInterval .isPresent () || reloadInterval .get ().isZero ()) {
116+ final String msg =
117+ "KeyStore reloading is disabled. If your Cassandra cluster requires client certificates, "
118+ + "client application restarts are infrequent, and client certificates have short lifetimes, then your client "
119+ + "may fail to re-establish connections to Cassandra hosts. To enable KeyStore reloading, see "
120+ + "`advanced.ssl-engine-factory.keystore-reload-interval` in reference.conf." ;
121+ logger .info (msg );
122+ } else {
123+ logger .info ("KeyStore reloading is enabled with interval {}" , reloadInterval .get ());
124+
114125 this .executor =
115126 Executors .newScheduledThreadPool (
116127 1 ,
@@ -122,8 +133,8 @@ private void start(Path keystorePath, String keystorePassword, Duration reloadIn
122133 });
123134 this .executor .scheduleWithFixedDelay (
124135 this ::reload ,
125- reloadInterval .toMillis (),
126- reloadInterval .toMillis (),
136+ reloadInterval .get (). toMillis (),
137+ reloadInterval .get (). toMillis (),
127138 TimeUnit .MILLISECONDS );
128139 }
129140 }
@@ -135,7 +146,7 @@ void reload() {
135146 } catch (Exception e ) {
136147 String msg =
137148 "Failed to reload KeyStore. If this continues to happen, your client may use stale identity"
138- + "certificates and fail to re-establish connections to Cassandra hosts." ;
149+ + " certificates and fail to re-establish connections to Cassandra hosts." ;
139150 logger .warn (msg , e );
140151 }
141152 }
0 commit comments