Parkerhiphop commented on code in PR #22907: URL: https://github.com/apache/kafka/pull/22907#discussion_r3915799807
########## clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/security/SaslClientsWithInvalidCredentialsTest.java: ########## @@ -0,0 +1,290 @@ +/* + * 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 org.apache.kafka.clients.security; + +import org.apache.kafka.clients.CommonClientConfigs; +import org.apache.kafka.clients.admin.Admin; +import org.apache.kafka.clients.admin.NewTopic; +import org.apache.kafka.clients.admin.ScramCredentialInfo; +import org.apache.kafka.clients.admin.TopicDescription; +import org.apache.kafka.clients.admin.UserScramCredentialUpsertion; +import org.apache.kafka.clients.consumer.Consumer; +import org.apache.kafka.clients.consumer.ConsumerConfig; +import org.apache.kafka.clients.consumer.GroupProtocol; +import org.apache.kafka.clients.producer.Producer; +import org.apache.kafka.clients.producer.ProducerConfig; +import org.apache.kafka.clients.producer.ProducerRecord; +import org.apache.kafka.common.KafkaException; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.config.SaslConfigs; +import org.apache.kafka.common.config.internals.BrokerSecurityConfigs; +import org.apache.kafka.common.errors.SaslAuthenticationException; +import org.apache.kafka.common.security.auth.SecurityProtocol; +import org.apache.kafka.common.security.scram.ScramLoginModule; +import org.apache.kafka.common.test.ClusterInstance; +import org.apache.kafka.common.test.api.ClusterConfigProperty; +import org.apache.kafka.common.test.api.ClusterTest; +import org.apache.kafka.common.test.api.ClusterTestDefaults; +import org.apache.kafka.common.test.api.Type; +import org.apache.kafka.coordinator.group.GroupCoordinatorConfig; +import org.apache.kafka.coordinator.transaction.TransactionLogConfig; +import org.apache.kafka.metadata.authorizer.StandardAuthorizer; +import org.apache.kafka.test.TestUtils; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.function.Executable; + +import java.time.Duration; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@ClusterTestDefaults( + types = {Type.KRAFT}, + brokers = 1, + serverProperties = { + @ClusterConfigProperty(key = BrokerSecurityConfigs.SASL_ENABLED_MECHANISMS_CONFIG, value = SaslClientsWithInvalidCredentialsTest.ENABLED_MECHANISMS), + @ClusterConfigProperty(key = BrokerSecurityConfigs.SASL_MECHANISM_INTER_BROKER_PROTOCOL_CONFIG, value = "PLAIN"), + @ClusterConfigProperty(key = GroupCoordinatorConfig.OFFSETS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "1"), + @ClusterConfigProperty(key = TransactionLogConfig.TRANSACTIONS_TOPIC_REPLICATION_FACTOR_CONFIG, value = "1"), + @ClusterConfigProperty(key = TransactionLogConfig.TRANSACTIONS_TOPIC_MIN_ISR_CONFIG, value = "1"), + @ClusterConfigProperty(key = StandardAuthorizer.ALLOW_EVERYONE_IF_NO_ACL_IS_FOUND_CONFIG, value = "true") + } +) +public class SaslClientsWithInvalidCredentialsTest { + public static final String SASL_MECHANISM = "SCRAM-SHA-256"; + public static final String ENABLED_MECHANISMS = "PLAIN," + SASL_MECHANISM; + + private static final String CLIENT_USER = "scram-user"; + private static final String CLIENT_PASSWORD = "scram-user-secret"; + private static final String TOPIC = "topic"; + private static final int NUM_PARTITIONS = 1; + private static final TopicPartition TP = new TopicPartition(TOPIC, 0); + + private final ClusterInstance cluster; + + public SaslClientsWithInvalidCredentialsTest(ClusterInstance cluster) { + this.cluster = cluster; + } + + @BeforeEach + public void setUp() throws Exception { + try (var admin = cluster.admin()) { + admin.createTopics(List.of(new NewTopic(TOPIC, NUM_PARTITIONS, (short) 1))).all().get(); + } + cluster.waitTopicCreation(TOPIC, NUM_PARTITIONS); + } + + @ClusterTest(brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT) + public void testIdempotentProducerWithAuthenticationFailure() throws InterruptedException { + try (Producer<byte[], byte[]> producer = cluster.producer(clientConfig(CLIENT_USER, Map.of( + ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true" + )))) { + verifyAuthenticationException(() -> sendOneRecord(producer, 10000)); + verifyAuthenticationException(() -> producer.partitionsFor(TOPIC)); + createScramCredential(CLIENT_USER); + } + + try (Producer<byte[], byte[]> producer = cluster.producer(clientConfig(CLIENT_USER, Map.of( + ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true" + )))) { + TestUtils.retryOnExceptionWithTimeout(() -> sendOneRecord(producer, 15000)); + } + } + + @ClusterTest(brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT) + public void testNonIdempotentProducerWithAuthenticationFailure() throws InterruptedException { + try (Producer<byte[], byte[]> producer = cluster.producer(clientConfig(CLIENT_USER, Map.of( + ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "false" + )))) { + verifyAuthenticationException(() -> sendOneRecord(producer, 10000)); + verifyAuthenticationException(() -> producer.partitionsFor(TOPIC)); + createScramCredential(CLIENT_USER); + TestUtils.retryOnExceptionWithTimeout(() -> sendOneRecord(producer, 15000)); + } + } + + @ClusterTest(brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT) + public void testTransactionalProducerWithAuthenticationFailure() { + try (Producer<byte[], byte[]> producer = cluster.producer(clientConfig(CLIENT_USER, Map.of( + ProducerConfig.TRANSACTIONAL_ID_CONFIG, "txclient-1", + ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true" + )))) { + verifyAuthenticationException(producer::initTransactions); + + createScramCredential(CLIENT_USER); + assertThrows(KafkaException.class, producer::initTransactions); + } + } + + @ClusterTest(brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT) + public void testConsumerWithAuthenticationFailure() throws InterruptedException { + for (GroupProtocol groupProtocol : cluster.supportedGroupProtocols()) { + String user = CLIENT_USER + "-" + groupProtocol.name().toLowerCase(Locale.ROOT); + try (Consumer<byte[], byte[]> consumer = createConsumer(user, Map.of( + ConsumerConfig.GROUP_PROTOCOL_CONFIG, groupProtocol.name().toLowerCase(Locale.ROOT) + ))) { + consumer.subscribe(List.of(TOPIC)); + verifyConsumerWithAuthenticationFailure(consumer, user, true); + } + } + } + + @ClusterTest(brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT) + public void testManualAssignmentConsumerWithAuthenticationFailure() throws InterruptedException { + for (GroupProtocol groupProtocol : cluster.supportedGroupProtocols()) { + String user = CLIENT_USER + "-" + groupProtocol.name().toLowerCase(Locale.ROOT); + try (Consumer<byte[], byte[]> consumer = createConsumer(user, Map.of( + ConsumerConfig.GROUP_PROTOCOL_CONFIG, groupProtocol.name().toLowerCase(Locale.ROOT) + ))) { + consumer.assign(List.of(TP)); + verifyConsumerWithAuthenticationFailure(consumer, user, false); + } + } + } + + @ClusterTest(brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT) + public void testManualAssignmentConsumerWithAutoCommitDisabledWithAuthenticationFailure() throws InterruptedException { + for (GroupProtocol groupProtocol : cluster.supportedGroupProtocols()) { + String user = CLIENT_USER + "-" + groupProtocol.name().toLowerCase(Locale.ROOT); + try (Consumer<byte[], byte[]> consumer = createConsumer(user, Map.of( + ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false", + ConsumerConfig.GROUP_PROTOCOL_CONFIG, groupProtocol.name().toLowerCase(Locale.ROOT) + ))) { + consumer.assign(List.of(TP)); + consumer.seek(TP, 0); + verifyConsumerWithAuthenticationFailure(consumer, user, false); + } + } + } + + private void verifyConsumerWithAuthenticationFailure( + Consumer<byte[], byte[]> consumer, + String user, + boolean usePollForInitialFailure + ) throws InterruptedException { + long startMs = System.currentTimeMillis(); + if (usePollForInitialFailure) { Review Comment: I agree that the flag was misleading. I removed it and now call `verifyAuthenticationException` directly in each test case. The subscription test uses `poll()`, while the manual-assignment tests use `partitionsFor()`, because `poll()` does not reliably trigger the initial authentication failure in manual-assignment mode. The common recovery logic remains in `verifyConsumerRecovery()`. ########## core/src/test/scala/integration/kafka/api/AbstractSaslTest.scala: ########## @@ -1,22 +0,0 @@ -/** - * 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.api - -/** - * Abstract Sasl test to be used both in scala and java tests of SASL. - * Separate class required to overcome issues related to usage of scala trait in java code. - * @see SaslClientsWithInvalidCredentialsTest - */ -abstract class AbstractSaslTest extends IntegrationTestHarness with SaslSetup { Review Comment: Updated it! -- 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]
