m1a2st commented on code in PR #22907:
URL: https://github.com/apache/kafka/pull/22907#discussion_r3803772534


##########
clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/security/SaslClientsWithInvalidCredentialsTest.java:
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.KafkaProducer;
+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 java.time.Duration;
+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 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 (Admin 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 
Exception {
+        testProducerWithAuthenticationFailure(true);
+    }
+
+    @ClusterTest(brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT)
+    public void testNonIdempotentProducerWithAuthenticationFailure() throws 
Exception {
+        testProducerWithAuthenticationFailure(false);
+    }
+
+    private void testProducerWithAuthenticationFailure(boolean 
isIdempotenceEnabled) throws Exception {
+        KafkaProducer<byte[], byte[]> producer = createProducer(Map.of(
+            ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, 
Boolean.toString(isIdempotenceEnabled)
+        ));
+
+        try {
+            KafkaProducer<byte[], byte[]> producerWithInvalidCredentials = 
producer;

Review Comment:
   I think @chia7712 means that we should use a try-with-resources block to 
close it automatically.



-- 
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]

Reply via email to