chia7712 commented on code in PR #17671:
URL: https://github.com/apache/kafka/pull/17671#discussion_r1899068385


##########
test-common/src/main/java/org/apache/kafka/common/test/JaasModule.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.common.test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class JaasModule {

Review Comment:
   Is this duplicate to core JaasModule?



##########
test-common/src/main/java/org/apache/kafka/common/test/JaasTestUtils.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.common.test;
+
+import org.apache.kafka.common.security.JaasUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import javax.security.auth.login.Configuration;
+
+public class JaasTestUtils {

Review Comment:
   It is already in xxx.test package, maybe we can call it JaasUtils?



##########
test-common/src/main/java/org/apache/kafka/common/test/JaasTestUtils.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.common.test;
+
+import org.apache.kafka.common.security.JaasUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import javax.security.auth.login.Configuration;
+
+public class JaasTestUtils {
+    public static class JaasSection {
+        private final String contextName;
+        private final List<JaasModule> modules;
+
+        public JaasSection(String contextName, List<JaasModule> modules) {
+            this.contextName = contextName;
+            this.modules = modules;
+        }
+
+        public List<JaasModule> getModules() {
+            return modules;
+        }
+
+        public String getContextName() {
+            return contextName;
+        }
+
+        @Override
+        public String toString() {
+            return String.format("%s {%n  %s%n};%n",
+                    contextName,
+                    
modules.stream().map(Object::toString).collect(Collectors.joining("\n  ")));
+        }
+    }
+
+    public static final String KAFKA_SERVER_CONTEXT_NAME = "KafkaServer";
+
+    public static final String KAFKA_PLAIN_USER1 = "plain-user1";
+    public static final String KAFKA_PLAIN_USER1_PASSWORD = 
"plain-user1-secret";
+    public static final String KAFKA_PLAIN_ADMIN = "plain-admin";
+    public static final String KAFKA_PLAIN_ADMIN_PASSWORD = 
"plain-admin-secret";
+
+    public static File writeJaasContextsToFile(List<JaasSection> jaasSections) 
throws IOException {

Review Comment:
   Do we really need this object? How about using map? It is more simple 



##########
test-common/src/main/java/org/apache/kafka/common/test/JaasModule.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.common.test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class JaasModule {
+
+    public static JaasModule plainLoginModule(String username, String 
password, boolean debug, Map<String, String> validUsers) {
+        String name = 
"org.apache.kafka.common.security.plain.PlainLoginModule";
+
+        Map<String, String> entries = new HashMap<>();
+        entries.put("username", username);
+        entries.put("password", password);
+        validUsers.forEach((user, pass) -> entries.put("user_" + user, pass));
+
+        return new JaasModule(
+            name,
+            debug,
+            entries
+        );
+    }
+
+    private final String name;
+
+    private final boolean debug;
+
+    private final Map<String, String> entries;
+
+    private JaasModule(String name, boolean debug, Map<String, String> 
entries) {

Review Comment:
   Could you please try to use record class?



##########
test-common/src/main/java/org/apache/kafka/common/test/KafkaClusterTestKit.java:
##########
@@ -602,6 +630,9 @@ public void close() throws Exception {
             waitForAllFutures(futureEntries);
             futureEntries.clear();
             Utils.delete(baseDirectory);
+            if (jaasFile.isPresent()) {

Review Comment:
   Could you please use ifPresent instead?



##########
test-common/test-common-api/src/test/java/org/apache/kafka/common/test/api/ClusterTestExtensionsTest.java:
##########
@@ -331,4 +338,42 @@ public void testControllerListenerName(ClusterInstance 
cluster) throws Execution
             assertEquals(1, 
admin.describeMetadataQuorum().quorumInfo().get().nodes().size());
         }
     }
+
+    @ClusterTest(types = {Type.KRAFT, Type.CO_KRAFT},
+        brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT,
+        controllerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT
+    )
+    public void testSaslPlaintext(ClusterInstance clusterInstance) {
+        Assertions.assertEquals(SecurityProtocol.SASL_PLAINTEXT, 
clusterInstance.config().brokerSecurityProtocol());
+
+        Map<String, Object> configs = new HashMap<>();
+        configs.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, 
SecurityProtocol.SASL_PLAINTEXT.name);
+        configs.put(SaslConfigs.SASL_MECHANISM, "PLAIN");
+
+        // client with admin credentials
+        configs.put(SaslConfigs.SASL_JAAS_CONFIG,
+            
String.format("org.apache.kafka.common.security.plain.PlainLoginModule required 
username=\"%s\" password=\"%s\";",
+                JaasTestUtils.KAFKA_PLAIN_ADMIN, 
JaasTestUtils.KAFKA_PLAIN_ADMIN_PASSWORD));
+        try (Admin admin = clusterInstance.admin(configs)) {
+            Assertions.assertDoesNotThrow(() -> 
admin.describeAcls(AclBindingFilter.ANY).values().get());
+        }
+
+        // client with non-admin credentials
+        configs.put(SaslConfigs.SASL_JAAS_CONFIG,
+            
String.format("org.apache.kafka.common.security.plain.PlainLoginModule required 
username=\"%s\" password=\"%s\";",
+                JaasTestUtils.KAFKA_PLAIN_USER1, 
JaasTestUtils.KAFKA_PLAIN_USER1_PASSWORD));
+        try (Admin admin = clusterInstance.admin(configs)) {

Review Comment:
   Those helper should return authorized client object, right? Otherwise, 
developers have to configure them manually



-- 
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-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to