You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "sidb98 (via GitHub)" <gi...@apache.org> on 2026/08/26 22:16:47 UTC
[PR] KAFKA-20025: Reload KRaft Raft controller-quorum SSL keystore on dynamic update [kafka]
sidb98 opened a new pull request, #23288:
URL: https://github.com/apache/kafka/pull/23288
## Summary
In KRaft, `KafkaRaftManager` builds its controller-quorum network client with an
`SslChannelBuilder` that is never registered as a `Reconfigurable` on the config that
processes dynamic updates. A renewed controller-listener certificate therefore never
reaches the running Raft channel: it keeps the cert loaded at startup and, on the next
reconnect (e.g. controller failover), can present a stale/expired cert, breaking the
broker↔controller connection and stalling metadata propagation. (KAFKA-20025.)
`SharedServer` creates three `KafkaConfig` objects (`sharedServerConfig`, `brokerConfig`,
`controllerConfig`). `RaftManager` is constructed with `sharedServerConfig`, whose
`dynamicConfig` is initialized but never receives cert-rotation updates (those go to
`brokerConfig`/`controllerConfig`), so self-registering on RaftManager's own config would
be a silent no-op.
## Changes
- `KafkaRaftManager`: expose the channel's `Reconfigurable` (`reconfigurableChannelBuilder`)
and capture it when the channel builder is an SSL builder.
- `BrokerServer` / `ControllerServer`: register it on their own config
(`config.dynamicConfig.addReconfigurable`) alongside the other reconfigurables, keeping
`RaftManager` on `sharedServerConfig` while routing the reconfigurable to the config that
actually receives cert-rotation updates.
## Testing
**Unit** : `RaftManagerTest.testRaftManagerExposesSslChannelBuilderAsReconfigurable`: a
RaftManager built with an SSL controller listener exposes its `SslChannelBuilder` via
`reconfigurableChannelBuilder`.
**Integration** : `RaftManagerSslReconfigIntegrationTest`: brings up an SSL
KRaft cluster, rotates the broker's `listener.name.controller.ssl.keystore.*` from cert A
to cert B via `incrementalAlterConfigs`, and asserts the certificate serial loaded in the
running Raft channel's `SslFactory` (read by reflection) flips A → B. Reverting the
registration makes it fail with "Raft channel SslFactory never loaded the rotated keystore
(cert B)". Deterministic: it inspects the loaded keystore directly, not cert expiry or
handshake timing. Cert A and B share a CA (controller trusts both; cluster stays healthy)
and a subject DN.
## Alternate integration test considered but rejected
`Kraft cluster with short-lived cert A → rotate to B → force re-handshake → assert no SSL
error`
This is flaky by construction: invalidating the old cert relies on expiry (A and B share
a CA), and JSSE's PKIX validation reads the real system clock with no injectable clock,
giving a bring-up race and an expiry race that generous margins only slow down, never
remove. The implemented test asserts the fix's effect deterministically instead.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20025: Reload KRaft Raft controller-quorum SSL keystore on dynamic update [kafka]
Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov commented on code in PR #23288:
URL: https://github.com/apache/kafka/pull/23288#discussion_r3903096539
##########
core/src/test/scala/integration/kafka/server/RaftManagerSslReconfigIntegrationTest.scala:
##########
@@ -0,0 +1,232 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package kafka.server
+
+import java.math.BigInteger
+import java.security.KeyStore
+import java.security.KeyPair
+import java.security.cert.X509Certificate
+import java.util.Properties
+import kafka.integration.KafkaServerTestHarness
+import kafka.utils.TestUtils
+import org.apache.kafka.clients.admin.{Admin, AdminClientConfig}
+import org.apache.kafka.common.config.SslConfigs
+import org.apache.kafka.common.config.types.Password
+import org.apache.kafka.common.network.SslChannelBuilder
+import org.apache.kafka.common.security.auth.{SecurityProtocol, SslEngineFactory}
+import org.apache.kafka.test.TestSslUtils
+import org.junit.jupiter.api.Assertions._
+import org.junit.jupiter.api.{AfterEach, BeforeEach, Tag, Test, TestInfo, Timeout}
+
+import scala.util.Try
+
+class RaftManagerSslReconfigIntegrationTest extends KafkaServerTestHarness {
+
+ private val storePassword = new Password("changeit")
+ private val storePasswordValue = "changeit"
+
+ private var adminClient: Admin = _
+ private var storedTestInfo: TestInfo = _
+
+ private var trustStorePath: String = _
+ private var controllerKeystorePath: String = _
+ private var brokerKeystorePath: String = _
+ private var certAKeystorePath: String = _
+ private var certBKeystorePath: String = _
+
+ private var certASerial: BigInteger = _
+ private var certBSerial: BigInteger = _
+
+ private val brokerId = 0
+
+ override protected def securityProtocol: SecurityProtocol = SecurityProtocol.SSL
+ override protected val controllerListenerSecurityProtocol: SecurityProtocol = SecurityProtocol.SSL
+
+ override protected def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
+ val props = new Properties()
+ props.put("listener.name.controller.ssl.keystore.location", controllerKeystorePath)
+ props.put("listener.name.controller.ssl.keystore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.key.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.truststore.location", trustStorePath)
+ props.put("listener.name.controller.ssl.truststore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.client.auth", "required")
+ Seq(props)
+ }
+
+ override def generateConfigs: Seq[KafkaConfig] = {
+ val props = TestUtils.createBrokerConfig(brokerId, enableControlledShutdown = false)
+ props.put("node.id", brokerId.toString)
+ props.put("process.roles", "broker")
+
+ props.put("listeners", "SSL://localhost:0")
+ props.put("advertised.listeners", "SSL://localhost:0")
+ props.put("listener.security.protocol.map", "SSL:SSL,CONTROLLER:SSL")
+ props.put("inter.broker.listener.name", "SSL")
+ props.put("ssl.keystore.location", brokerKeystorePath)
+ props.put("ssl.keystore.password", storePasswordValue)
+ props.put("ssl.key.password", storePasswordValue)
+ props.put("ssl.truststore.location", trustStorePath)
+ props.put("ssl.truststore.password", storePasswordValue)
+ props.put("ssl.endpoint.identification.algorithm", "")
+ props.put("ssl.client.auth", "none")
+
+ props.put("listener.name.controller.ssl.keystore.location", certAKeystorePath)
+ props.put("listener.name.controller.ssl.keystore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.key.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.truststore.location", trustStorePath)
+ props.put("listener.name.controller.ssl.truststore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.endpoint.identification.algorithm", "")
+
+ Seq(KafkaConfig.fromProps(props))
+ }
+
+ private def generateCertificates(): Unit = {
+ val algorithm = "SHA256withRSA"
+
+ val caKeyPair: KeyPair = TestSslUtils.generateKeyPair("RSA")
+ val caCert: X509Certificate = TestSslUtils.generateSignedCertificate(
+ "CN=TestCA", caKeyPair, 0, 3650, null, null, algorithm, true, false, false)
+
+ val controllerKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val controllerCert = TestSslUtils.generateSignedCertificate(
+ "CN=localhost", controllerKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, true, false,
+ Array[String]("localhost"))
+
+ val brokerKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val brokerCert = TestSslUtils.generateSignedCertificate(
+ "CN=localhost", brokerKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, true, false,
+ Array[String]("localhost"))
+
+ val certAKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val certACert = TestSslUtils.generateSignedCertificate(
+ "CN=brokerClient", certAKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, false, true)
+ certASerial = certACert.getSerialNumber
+
+ val certBKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val certBCert = TestSslUtils.generateSignedCertificate(
+ "CN=brokerClient", certBKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, false, true)
+ certBSerial = certBCert.getSerialNumber
+
+ assertNotEquals(certASerial, certBSerial, "Cert A and B must have distinct serials for the test to be meaningful")
+
+ trustStorePath = TestUtils.tempFile("raft-ssl-reconfig-truststore", ".jks").getPath
+ controllerKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-controller-ks", ".jks").getPath
+ brokerKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-broker-ks", ".jks").getPath
+ certAKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-clientA-ks", ".jks").getPath
+ certBKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-clientB-ks", ".jks").getPath
+
+ TestSslUtils.createKeyStore(controllerKeystorePath, storePassword, storePassword, "controller",
+ controllerKeyPair.getPrivate, controllerCert)
+ TestSslUtils.createKeyStore(brokerKeystorePath, storePassword, storePassword, "broker",
+ brokerKeyPair.getPrivate, brokerCert)
+ TestSslUtils.createKeyStore(certAKeystorePath, storePassword, storePassword, "clienta",
+ certAKeyPair.getPrivate, certACert)
+ TestSslUtils.createKeyStore(certBKeystorePath, storePassword, storePassword, "clientb",
+ certBKeyPair.getPrivate, certBCert)
+
+ // Truststore holds only the CA, so the controller trusts both client certs and the cluster stays healthy
+ // across the rotation.
+ val trustCerts = new java.util.HashMap[String, X509Certificate]()
+ trustCerts.put("ca", caCert)
+ TestSslUtils.createTrustStore(trustStorePath, storePassword, trustCerts)
+ }
+
+ private def createAdminClient(): Admin = {
+ val adminProps = new Properties()
+ adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers())
+ adminProps.put("security.protocol", SecurityProtocol.SSL.name)
+ adminProps.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, trustStorePath)
+ adminProps.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, storePasswordValue)
+ adminProps.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "")
+ Admin.create(adminProps)
+ }
+
+ private def readField(target: AnyRef, name: String): AnyRef = {
Review Comment:
I am not the greatest fan of using reflection here because this test would depend on an internal call chain and will break the moment we decide to change the call chain even if we don't change the tested behaviour.
An alternative I can propose is to create a new SslEngineFactory which extends the DefaultSslEngineFactory and has static methods which allow you to inspect the serial of a certificate and then pass the new EngineFactory in via `listener.name.controller.ssl.engine.factory.class`.
As a separate note, we are trying to get rid of Scala in the project. Therefore, please write the integration test in Java and also use the KafkaClusterTestKit to create the test rather than the KafkaServerTestHarness.
Lastly, there are extraneous ERROR messages in the test logs, which, while benevolent in this case, do sometimes throw me off because then one has to check whether they are a genuine error (affecting the particular thing under test or not). If you could tweak the right knobs in order to make them disappear that would also be appreciated!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20025: Reload KRaft Raft controller-quorum SSL keystore on dynamic update [kafka]
Posted by "sidb98 (via GitHub)" <gi...@apache.org>.
sidb98 commented on code in PR #23288:
URL: https://github.com/apache/kafka/pull/23288#discussion_r3907535926
##########
core/src/test/scala/integration/kafka/server/RaftManagerSslReconfigIntegrationTest.scala:
##########
@@ -0,0 +1,232 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package kafka.server
+
+import java.math.BigInteger
+import java.security.KeyStore
+import java.security.KeyPair
+import java.security.cert.X509Certificate
+import java.util.Properties
+import kafka.integration.KafkaServerTestHarness
+import kafka.utils.TestUtils
+import org.apache.kafka.clients.admin.{Admin, AdminClientConfig}
+import org.apache.kafka.common.config.SslConfigs
+import org.apache.kafka.common.config.types.Password
+import org.apache.kafka.common.network.SslChannelBuilder
+import org.apache.kafka.common.security.auth.{SecurityProtocol, SslEngineFactory}
+import org.apache.kafka.test.TestSslUtils
+import org.junit.jupiter.api.Assertions._
+import org.junit.jupiter.api.{AfterEach, BeforeEach, Tag, Test, TestInfo, Timeout}
+
+import scala.util.Try
+
+class RaftManagerSslReconfigIntegrationTest extends KafkaServerTestHarness {
+
+ private val storePassword = new Password("changeit")
+ private val storePasswordValue = "changeit"
+
+ private var adminClient: Admin = _
+ private var storedTestInfo: TestInfo = _
+
+ private var trustStorePath: String = _
+ private var controllerKeystorePath: String = _
+ private var brokerKeystorePath: String = _
+ private var certAKeystorePath: String = _
+ private var certBKeystorePath: String = _
+
+ private var certASerial: BigInteger = _
+ private var certBSerial: BigInteger = _
+
+ private val brokerId = 0
+
+ override protected def securityProtocol: SecurityProtocol = SecurityProtocol.SSL
+ override protected val controllerListenerSecurityProtocol: SecurityProtocol = SecurityProtocol.SSL
+
+ override protected def kraftControllerConfigs(testInfo: TestInfo): Seq[Properties] = {
+ val props = new Properties()
+ props.put("listener.name.controller.ssl.keystore.location", controllerKeystorePath)
+ props.put("listener.name.controller.ssl.keystore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.key.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.truststore.location", trustStorePath)
+ props.put("listener.name.controller.ssl.truststore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.client.auth", "required")
+ Seq(props)
+ }
+
+ override def generateConfigs: Seq[KafkaConfig] = {
+ val props = TestUtils.createBrokerConfig(brokerId, enableControlledShutdown = false)
+ props.put("node.id", brokerId.toString)
+ props.put("process.roles", "broker")
+
+ props.put("listeners", "SSL://localhost:0")
+ props.put("advertised.listeners", "SSL://localhost:0")
+ props.put("listener.security.protocol.map", "SSL:SSL,CONTROLLER:SSL")
+ props.put("inter.broker.listener.name", "SSL")
+ props.put("ssl.keystore.location", brokerKeystorePath)
+ props.put("ssl.keystore.password", storePasswordValue)
+ props.put("ssl.key.password", storePasswordValue)
+ props.put("ssl.truststore.location", trustStorePath)
+ props.put("ssl.truststore.password", storePasswordValue)
+ props.put("ssl.endpoint.identification.algorithm", "")
+ props.put("ssl.client.auth", "none")
+
+ props.put("listener.name.controller.ssl.keystore.location", certAKeystorePath)
+ props.put("listener.name.controller.ssl.keystore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.key.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.truststore.location", trustStorePath)
+ props.put("listener.name.controller.ssl.truststore.password", storePasswordValue)
+ props.put("listener.name.controller.ssl.endpoint.identification.algorithm", "")
+
+ Seq(KafkaConfig.fromProps(props))
+ }
+
+ private def generateCertificates(): Unit = {
+ val algorithm = "SHA256withRSA"
+
+ val caKeyPair: KeyPair = TestSslUtils.generateKeyPair("RSA")
+ val caCert: X509Certificate = TestSslUtils.generateSignedCertificate(
+ "CN=TestCA", caKeyPair, 0, 3650, null, null, algorithm, true, false, false)
+
+ val controllerKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val controllerCert = TestSslUtils.generateSignedCertificate(
+ "CN=localhost", controllerKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, true, false,
+ Array[String]("localhost"))
+
+ val brokerKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val brokerCert = TestSslUtils.generateSignedCertificate(
+ "CN=localhost", brokerKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, true, false,
+ Array[String]("localhost"))
+
+ val certAKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val certACert = TestSslUtils.generateSignedCertificate(
+ "CN=brokerClient", certAKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, false, true)
+ certASerial = certACert.getSerialNumber
+
+ val certBKeyPair = TestSslUtils.generateKeyPair("RSA")
+ val certBCert = TestSslUtils.generateSignedCertificate(
+ "CN=brokerClient", certBKeyPair, 0, 3650, "CN=TestCA", caKeyPair, algorithm, false, false, true)
+ certBSerial = certBCert.getSerialNumber
+
+ assertNotEquals(certASerial, certBSerial, "Cert A and B must have distinct serials for the test to be meaningful")
+
+ trustStorePath = TestUtils.tempFile("raft-ssl-reconfig-truststore", ".jks").getPath
+ controllerKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-controller-ks", ".jks").getPath
+ brokerKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-broker-ks", ".jks").getPath
+ certAKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-clientA-ks", ".jks").getPath
+ certBKeystorePath = TestUtils.tempFile("raft-ssl-reconfig-clientB-ks", ".jks").getPath
+
+ TestSslUtils.createKeyStore(controllerKeystorePath, storePassword, storePassword, "controller",
+ controllerKeyPair.getPrivate, controllerCert)
+ TestSslUtils.createKeyStore(brokerKeystorePath, storePassword, storePassword, "broker",
+ brokerKeyPair.getPrivate, brokerCert)
+ TestSslUtils.createKeyStore(certAKeystorePath, storePassword, storePassword, "clienta",
+ certAKeyPair.getPrivate, certACert)
+ TestSslUtils.createKeyStore(certBKeystorePath, storePassword, storePassword, "clientb",
+ certBKeyPair.getPrivate, certBCert)
+
+ // Truststore holds only the CA, so the controller trusts both client certs and the cluster stays healthy
+ // across the rotation.
+ val trustCerts = new java.util.HashMap[String, X509Certificate]()
+ trustCerts.put("ca", caCert)
+ TestSslUtils.createTrustStore(trustStorePath, storePassword, trustCerts)
+ }
+
+ private def createAdminClient(): Admin = {
+ val adminProps = new Properties()
+ adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers())
+ adminProps.put("security.protocol", SecurityProtocol.SSL.name)
+ adminProps.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, trustStorePath)
+ adminProps.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, storePasswordValue)
+ adminProps.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "")
+ Admin.create(adminProps)
+ }
+
+ private def readField(target: AnyRef, name: String): AnyRef = {
Review Comment:
On dropping the reflection : an injected `SslEngineFactory` can't make this assertion, because
`ssl.engine.factory.class` is scoped per **listener** while the bug is in one **channel** among
several that share the controller listener. `NodeToControllerChannelManagerImpl` builds with
`controllerInfo.listenerName()` and already calls `config.addReconfigurable(...)` itself (line 103),
and there are instances at `BrokerServer.scala:255/329/442`, `DefaultAlterPartitionManager.java:80`
and `ControllerServer.scala:419`. It is literally the same listener — `KafkaRaftManager.scala:209`
and `RaftControllerNodeProvider.java:44` both do
`new ListenerName(config.controllerListenerNames.get(0))`.
So all of them get the injected class, all of them reload on the keystore alter with or without this
fix, and nothing on an instance says which channel it belongs to (`SslFactory` is constructed as
`new SslFactory(connectionMode, null, isInterBrokerListener)` — no channel identity is passed down,
and each `configure()` receives an identical map). "A controller-listener factory loaded cert B" is
therefore true even when the fix is reverted — I tried it, and the untargeted version passes on a
tree with the bug restored.
The reflection isn't reading the certificate insteadevery hop returns an object reference, walking the ownership chain from a named anchor
(`cluster.brokers().get(BROKER_ID).raftManager()`) down to the builder created at
`KafkaRaftManager.scala:212`. That anchoring is what makes the assertion specific to the Raft
channel, and it's the part injection can't reproduce.
I have updated the integration to be in java and also fixed the extraneous error.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] KAFKA-20025: Reload KRaft Raft controller-quorum SSL keystore on dynamic update [kafka]
Posted by "clolov (via GitHub)" <gi...@apache.org>.
clolov merged PR #23288:
URL: https://github.com/apache/kafka/pull/23288
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org