Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ else if (clientAuthConfig.equals("requested"))
(Password) configs.get(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG));
try {
this.sslContext = createSSLContext(keystore, truststore);
log.debug("Created SSL context with keystore {} truststore {}", keystore, truststore);
} catch (Exception e) {
throw new KafkaException(e);
}
Expand Down Expand Up @@ -173,6 +174,7 @@ public void reconfigure(Map<String, ?> configs) throws KafkaException {
SecurityStore keystore = newKeystore != null ? newKeystore : this.keystore;
SecurityStore truststore = newTruststore != null ? newTruststore : this.truststore;
this.sslContext = createSSLContext(keystore, truststore);
log.info("Created new SSL context with keystore {} truststore {}", keystore, truststore);
this.keystore = keystore;
this.truststore = truststore;
} catch (Exception e) {
Expand Down Expand Up @@ -316,14 +318,15 @@ static class SecurityStore {
private final String path;
private final Password password;
private final Password keyPassword;
private Long fileLastModifiedMs;
private final Long fileLastModifiedMs;

SecurityStore(String type, String path, Password password, Password keyPassword) {
Objects.requireNonNull(type, "type must not be null");
this.type = type;
this.path = path;
this.password = password;
this.keyPassword = keyPassword;
fileLastModifiedMs = lastModifiedMs(path);
}

/**
Expand All @@ -338,10 +341,6 @@ KeyStore load() {
// If a password is not set access to the truststore is still available, but integrity checking is disabled.
char[] passwordChars = password != null ? password.value().toCharArray() : null;
ks.load(in, passwordChars);
fileLastModifiedMs = lastModifiedMs(path);

log.debug("Loaded key store with path {} modification time {}", path,
fileLastModifiedMs == null ? null : new Date(fileLastModifiedMs));
return ks;
} catch (GeneralSecurityException | IOException e) {
throw new KafkaException("Failed to load SSL keystore " + path + " of type " + type, e);
Expand All @@ -361,6 +360,13 @@ boolean modified() {
Long modifiedMs = lastModifiedMs(path);
return modifiedMs != null && !Objects.equals(modifiedMs, this.fileLastModifiedMs);
}

@Override
public String toString() {
return "SecurityStore(" +
"path=" + path +
", modificationTime=" + (fileLastModifiedMs == null ? null : new Date(fileLastModifiedMs)) + ")";
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public void testReconfiguration() throws Exception {
assertNotSame("SSL context not recreated", sslContext, sslFactory.sslContext());
sslContext = sslFactory.sslContext();

// Verify that context is recreated after validation on reconfigure() if config is not changed, but keystore file was modified
keyStoreFile.setLastModified(System.currentTimeMillis() + 15000);
sslFactory.validateReconfiguration(sslConfig);
sslFactory.reconfigure(sslConfig);
assertNotSame("SSL context not recreated", sslContext, sslFactory.sslContext());
sslContext = sslFactory.sslContext();

// Verify that the context is not recreated if modification time cannot be determined
keyStoreFile.setLastModified(System.currentTimeMillis() + 20000);
Files.delete(keyStoreFile.toPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ class DynamicBrokerReconfigurationTest extends ZooKeeperTestHarness with SaslSet
alterSslKeystore(adminClient, sslPropertiesCopy, SecureInternal)
verifyProduceConsume(producer, consumer, 10, topic2)

// Verify that keystores can be updated using same file name.
val reusableProps = sslProperties2.clone().asInstanceOf[Properties]
val reusableFile = File.createTempFile("keystore", ".jks")
reusableProps.setProperty(SSL_KEYSTORE_LOCATION_CONFIG, reusableFile.getPath)
Files.copy(new File(sslProperties1.getProperty(SSL_KEYSTORE_LOCATION_CONFIG)).toPath,
reusableFile.toPath, StandardCopyOption.REPLACE_EXISTING)
alterSslKeystore(adminClient, reusableProps, SecureExternal)
val producer3 = ProducerBuilder().trustStoreProps(sslProperties2).maxRetries(0).build()
verifyAuthenticationFailure(producer3)
// Now alter using same file name. We can't check if the update has completed by comparing config on
// the broker, so we wait for producer operation to succeed to verify that the update has been performed.
Files.copy(new File(sslProperties2.getProperty(SSL_KEYSTORE_LOCATION_CONFIG)).toPath,
reusableFile.toPath, StandardCopyOption.REPLACE_EXISTING)
reusableFile.setLastModified(System.currentTimeMillis() + 1000)
alterSslKeystore(adminClient, reusableProps, SecureExternal)
TestUtils.waitUntilTrue(() => {
try {
producer3.partitionsFor(topic).size() == numPartitions
} catch {
case _: Exception => false
}
}, "Keystore not updated")

// Verify that all messages sent with retries=0 while keystores were being altered were consumed
stopAndVerifyProduceConsume(producerThread, consumerThread)
}
Expand Down