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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
