m1a2st commented on code in PR #19527: URL: https://github.com/apache/kafka/pull/19527#discussion_r2444890179
########## clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/security/LoginTest.java: ########## @@ -0,0 +1,267 @@ +/* + * 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.admin.Admin; +import org.apache.kafka.common.Endpoint; +import org.apache.kafka.common.Metric; +import org.apache.kafka.common.MetricName; +import org.apache.kafka.common.acl.AclBinding; +import org.apache.kafka.common.acl.AclBindingFilter; +import org.apache.kafka.common.metrics.Gauge; +import org.apache.kafka.common.metrics.KafkaMetric; +import org.apache.kafka.common.metrics.Monitorable; +import org.apache.kafka.common.metrics.PluginMetrics; +import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler; +import org.apache.kafka.common.security.auth.Login; +import org.apache.kafka.common.security.auth.SecurityProtocol; +import org.apache.kafka.common.security.authenticator.AbstractLogin; +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.Type; +import org.apache.kafka.server.authorizer.AclCreateResult; +import org.apache.kafka.server.authorizer.AclDeleteResult; +import org.apache.kafka.server.authorizer.Action; +import org.apache.kafka.server.authorizer.AuthorizableRequestContext; +import org.apache.kafka.server.authorizer.AuthorizationResult; +import org.apache.kafka.server.authorizer.Authorizer; +import org.apache.kafka.server.authorizer.AuthorizerServerInfo; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import javax.security.auth.Subject; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; + +import static org.apache.kafka.clients.CommonClientConfigs.CLIENT_ID_CONFIG; +import static org.apache.kafka.clients.CommonClientConfigs.SECURITY_PROTOCOL_CONFIG; +import static org.apache.kafka.common.config.SaslConfigs.SASL_JAAS_CONFIG; +import static org.apache.kafka.common.config.SaslConfigs.SASL_LOGIN_CLASS; +import static org.apache.kafka.common.config.SaslConfigs.SASL_MECHANISM; +import static org.apache.kafka.common.config.internals.BrokerSecurityConfigs.SASL_ENABLED_MECHANISMS_CONFIG; +import static org.apache.kafka.common.config.internals.BrokerSecurityConfigs.SASL_MECHANISM_INTER_BROKER_PROTOCOL_CONFIG; +import static org.apache.kafka.server.config.ServerConfigs.AUTHORIZER_CLASS_NAME_CONFIG; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class LoginTest { + + private static final String USERNAME = "userA"; + private static final String PASSWORD = "pwd"; + private static final String CLIENT_ID = "test-login-client"; + private static final String LISTENER_PREFIX = "listener.name.controller."; + private static final String EXTERNAL_PREFIX = "listener.name.external."; + private static final String MECHANISMS = "PLAIN"; + private static final String MECHANISMS_PREFIX = "plain."; + private static final String SASL_JAAS = "org.apache.kafka.common.security.plain.PlainLoginModule required " + + "user_" + USERNAME + "=\"" + PASSWORD + "\";"; + + @ClusterTest( + types = {Type.CO_KRAFT}, + controllerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT, + brokerSecurityProtocol = SecurityProtocol.SASL_PLAINTEXT, + serverProperties = { + @ClusterConfigProperty(key = LISTENER_PREFIX + MECHANISMS_PREFIX + SASL_LOGIN_CLASS, value = "org.apache.kafka.clients.security.LoginTest$CustomerLogin"), + @ClusterConfigProperty(key = EXTERNAL_PREFIX + MECHANISMS_PREFIX + SASL_LOGIN_CLASS, value = "org.apache.kafka.clients.security.LoginTest$CustomerLogin"), + @ClusterConfigProperty(key = SASL_ENABLED_MECHANISMS_CONFIG, value = MECHANISMS), + @ClusterConfigProperty(key = SASL_MECHANISM_INTER_BROKER_PROTOCOL_CONFIG, value = MECHANISMS), + @ClusterConfigProperty(key = LISTENER_PREFIX + MECHANISMS_PREFIX + SASL_JAAS_CONFIG, value = SASL_JAAS), + @ClusterConfigProperty(key = AUTHORIZER_CLASS_NAME_CONFIG, value = "org.apache.kafka.clients.security.LoginTest$CustomerAuthorizer"), + } + ) + public void testCustomLoginWithKafkaCluster(ClusterInstance cluster) { + try (Admin admin = cluster.admin(saslConfig())) { + int found = 0; + for (Metric metric : admin.metrics().values()) { + found += assertMetricName( + metric.metricName(), + expectedTags(Map.of("client-id", CLIENT_ID)) + ); + } + assertEquals(1, found, "Expected to find 1 metric"); + + Map<MetricName, KafkaMetric> allMetrics = Stream.of( + cluster.controllers().get(0).metrics().metrics(), + cluster.brokers().get(0).metrics().metrics() + ).collect(HashMap::new, Map::putAll, Map::putAll); + assertMetrics( + allMetrics, + expectedTags(Map.of( + "mechanism", MECHANISMS, + "listener", "EXTERNAL", + "networkProcessor", "0" Review Comment: Only one network processor registers the `Login` plugin, so we only need to check processor 0.” -- 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]
