Skip to content

Commit ea2e475

Browse files
aratnoabsurdfarce
authored andcommitted
Address PR feedback: reload-interval to use Optional internally and null in config, rather than using sentinel Duration.ZERO
1 parent c7719ae commit ea2e475

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

Diff for: core/src/main/java/com/datastax/oss/driver/internal/core/ssl/DefaultSslEngineFactory.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.security.SecureRandom;
3434
import java.time.Duration;
3535
import java.util.List;
36+
import java.util.Optional;
3637
import javax.net.ssl.SSLContext;
3738
import javax.net.ssl.SSLEngine;
3839
import javax.net.ssl.SSLParameters;
@@ -153,14 +154,11 @@ protected SSLContext buildContext(DriverExecutionProfile config) throws Exceptio
153154
private ReloadingKeyManagerFactory buildReloadingKeyManagerFactory(DriverExecutionProfile config)
154155
throws Exception {
155156
Path keystorePath = Paths.get(config.getString(DefaultDriverOption.SSL_KEYSTORE_PATH));
156-
String password =
157-
config.isDefined(DefaultDriverOption.SSL_KEYSTORE_PASSWORD)
158-
? config.getString(DefaultDriverOption.SSL_KEYSTORE_PASSWORD)
159-
: null;
160-
Duration reloadInterval =
161-
config.isDefined(DefaultDriverOption.SSL_KEYSTORE_RELOAD_INTERVAL)
162-
? config.getDuration(DefaultDriverOption.SSL_KEYSTORE_RELOAD_INTERVAL)
163-
: Duration.ZERO;
157+
String password = config.getString(DefaultDriverOption.SSL_KEYSTORE_PASSWORD, null);
158+
Optional<Duration> reloadInterval =
159+
Optional.ofNullable(
160+
config.getDuration(DefaultDriverOption.SSL_KEYSTORE_RELOAD_INTERVAL, null));
161+
164162
return ReloadingKeyManagerFactory.create(keystorePath, password, reloadInterval);
165163
}
166164

Diff for: core/src/main/java/com/datastax/oss/driver/internal/core/ssl/ReloadingKeyManagerFactory.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.security.cert.X509Certificate;
3737
import java.time.Duration;
3838
import java.util.Arrays;
39+
import java.util.Optional;
3940
import java.util.concurrent.Executors;
4041
import java.util.concurrent.ScheduledExecutorService;
4142
import 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
}

Diff for: core/src/test/java/com/datastax/oss/driver/internal/core/ssl/ReloadingKeyManagerFactoryTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.security.NoSuchAlgorithmException;
3535
import java.security.SecureRandom;
3636
import java.security.cert.X509Certificate;
37-
import java.time.Duration;
3837
import java.util.Optional;
3938
import java.util.concurrent.BlockingQueue;
4039
import java.util.concurrent.LinkedBlockingQueue;
@@ -86,7 +85,6 @@ public class ReloadingKeyManagerFactoryTest {
8685

8786
static final Path CLIENT_TRUSTSTORE_PATH = CERT_BASE.resolve("client.truststore");
8887
static final String CERTSTORE_PASSWORD = "changeit";
89-
static final Duration NO_SCHEDULED_RELOAD = Duration.ofMillis(0);
9088

9189
private static TrustManagerFactory buildTrustManagerFactory() {
9290
TrustManagerFactory tmf;
@@ -186,7 +184,7 @@ public void client_certificates_should_reload() throws Exception {
186184

187185
final ReloadingKeyManagerFactory kmf =
188186
ReloadingKeyManagerFactory.create(
189-
TMP_CLIENT_KEYSTORE_PATH, CERTSTORE_PASSWORD, NO_SCHEDULED_RELOAD);
187+
TMP_CLIENT_KEYSTORE_PATH, CERTSTORE_PASSWORD, Optional.empty());
190188
// Need a tmf that tells the server to send its certs
191189
final TrustManagerFactory tmf = buildTrustManagerFactory();
192190

0 commit comments

Comments
 (0)