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