mimaison commented on code in PR #22307:
URL: https://github.com/apache/kafka/pull/22307#discussion_r3272288090
##########
core/src/main/scala/kafka/server/KafkaApis.scala:
##########
@@ -1541,8 +1547,8 @@ class KafkaApis(val requestChannel: RequestChannel,
val nonExistingTopicResponses = mutable.Map[TopicPartition,
DeleteRecordsPartitionResult]()
val authorizedForDeleteTopicOffsets = mutable.Map[TopicPartition, Long]()
- val topics = deleteRecordsRequest.data.topics.asScala
- val authorizedTopics = authHelper.filterByAuthorized(request.context,
DELETE, TOPIC, topics)(_.name)
+ val topics = deleteRecordsRequest.data.topics.asScala
Review Comment:
Nit: The alignment is off
##########
core/src/main/scala/kafka/server/KafkaApis.scala:
##########
@@ -2640,11 +2651,11 @@ class KafkaApis(val requestChannel: RequestChannel,
} else {
if (consumerGroupHeartbeatRequest.data.subscribedTopicNames != null &&
!consumerGroupHeartbeatRequest.data.subscribedTopicNames.isEmpty) {
- // Check the authorization if the subscribed topic names are provided.
- // Clients are not allowed to see topics that are not authorized for
Describe.
- val subscribedTopicSet =
consumerGroupHeartbeatRequest.data.subscribedTopicNames.asScala.toSet
- val authorizedTopics = authHelper.filterByAuthorized(request.context,
DESCRIBE, TOPIC,
- subscribedTopicSet)(identity)
+ // Check the authorization if the subscribed topic names are provided.
+ // Clients are not allowed to see topics that are not authorized for
Describe.
+ val subscribedTopicSet =
consumerGroupHeartbeatRequest.data.subscribedTopicNames.asScala.toSet
+ val authorizedTopics = authHelper.filterByAuthorized(request.context,
DESCRIBE, TOPIC,
+ subscribedTopicSet.asJava, (t: String) => t)
Review Comment:
This whole block is misaligned
##########
core/src/main/scala/kafka/server/KafkaApis.scala:
##########
@@ -3085,11 +3096,11 @@ class KafkaApis(val requestChannel: RequestChannel,
} else {
if (shareGroupHeartbeatRequest.data.subscribedTopicNames != null &&
!shareGroupHeartbeatRequest.data.subscribedTopicNames.isEmpty) {
- // Check the authorization if the subscribed topic names are provided.
- // Clients are not allowed to see topics that are not authorized for
Describe.
- val subscribedTopicSet =
shareGroupHeartbeatRequest.data.subscribedTopicNames.asScala.toSet
- val authorizedTopics = authHelper.filterByAuthorized(request.context,
DESCRIBE, TOPIC,
- subscribedTopicSet)(identity)
+ // Check the authorization if the subscribed topic names are provided.
+ // Clients are not allowed to see topics that are not authorized for
Describe.
+ val subscribedTopicSet =
shareGroupHeartbeatRequest.data.subscribedTopicNames.asScala.toSet
+ val authorizedTopics = authHelper.filterByAuthorized(request.context,
DESCRIBE, TOPIC,
+ subscribedTopicSet.asJava, (t: String) => t)
Review Comment:
Ditto
##########
server/src/test/java/org/apache/kafka/server/AuthHelperTest.java:
##########
@@ -0,0 +1,270 @@
+/*
+ * 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.server;
+
+import org.apache.kafka.clients.admin.EndpointType;
+import org.apache.kafka.common.acl.AclOperation;
+import org.apache.kafka.common.internals.Plugin;
+import org.apache.kafka.common.message.DescribeClusterRequestData;
+import org.apache.kafka.common.message.DescribeClusterResponseData;
+import
org.apache.kafka.common.message.DescribeClusterResponseData.DescribeClusterBroker;
+import
org.apache.kafka.common.message.DescribeClusterResponseData.DescribeClusterBrokerCollection;
+import org.apache.kafka.common.network.ClientInformation;
+import org.apache.kafka.common.network.ListenerName;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.DescribeClusterRequest;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.requests.RequestHeader;
+import org.apache.kafka.common.resource.PatternType;
+import org.apache.kafka.common.resource.ResourcePattern;
+import org.apache.kafka.common.resource.ResourceType;
+import org.apache.kafka.common.security.auth.KafkaPrincipal;
+import org.apache.kafka.common.security.auth.SecurityProtocol;
+import org.apache.kafka.network.Request;
+import org.apache.kafka.server.authorizer.Action;
+import org.apache.kafka.server.authorizer.AuthorizationResult;
+import org.apache.kafka.server.authorizer.Authorizer;
+
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentMatchers;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class AuthHelperTest {
+
+ private static Request
newMockDescribeClusterRequest(DescribeClusterRequestData data, int
requestVersion)
+ throws UnknownHostException {
+ RequestContext requestContext = new RequestContext(
+ new RequestHeader(ApiKeys.DESCRIBE_CLUSTER, (short)
requestVersion, "", 0),
+ "",
+ InetAddress.getLocalHost(),
+ KafkaPrincipal.ANONYMOUS,
+ new ListenerName("PLAINTEXT"),
+ SecurityProtocol.PLAINTEXT,
+ ClientInformation.EMPTY,
+ false);
+ Request request = mock(Request.class);
+ when(request.body(DescribeClusterRequest.class)).thenReturn(
+ new DescribeClusterRequest(data, (short) requestVersion));
+ when(request.context()).thenReturn(requestContext);
+ when(request.header()).thenReturn(requestContext.header);
+ return request;
+ }
+
+ @Test
+ public void testAuthorize() throws UnknownHostException {
+ Authorizer authorizer = mock(Authorizer.class);
+ Plugin<Authorizer> authorizerPlugin = Plugin.wrapInstance(authorizer,
null, "authorizer.class.name");
Review Comment:
It seems all tests have these 2 lines, maybe we could move them to a
`setup()` method?
##########
server/src/main/java/org/apache/kafka/server/AuthHelper.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.server;
+
+import org.apache.kafka.clients.admin.EndpointType;
+import org.apache.kafka.common.acl.AclOperation;
+import org.apache.kafka.common.errors.ClusterAuthorizationException;
+import org.apache.kafka.common.internals.Plugin;
+import org.apache.kafka.common.message.DescribeClusterResponseData;
+import
org.apache.kafka.common.message.DescribeClusterResponseData.DescribeClusterBrokerCollection;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.DescribeClusterRequest;
+import org.apache.kafka.common.requests.RequestContext;
+import org.apache.kafka.common.resource.PatternType;
+import org.apache.kafka.common.resource.Resource;
+import org.apache.kafka.common.resource.ResourcePattern;
+import org.apache.kafka.common.resource.ResourceType;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.network.Request;
+import org.apache.kafka.security.authorizer.AclEntry;
+import org.apache.kafka.server.authorizer.Action;
+import org.apache.kafka.server.authorizer.AuthorizationResult;
+import org.apache.kafka.server.authorizer.Authorizer;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+public class AuthHelper {
+
+ private final Optional<Plugin<Authorizer>> authorizer;
+
+ public AuthHelper(Optional<Plugin<Authorizer>> authorizer) {
+ this.authorizer = authorizer;
+ }
+
+ public boolean authorize(
+ RequestContext requestContext,
+ AclOperation operation,
+ ResourceType resourceType,
+ String resourceName,
+ boolean logIfAllowed,
+ boolean logIfDenied,
+ int refCount
+ ) {
+ if (authorizer.isEmpty()) {
+ return true;
+ }
+ ResourcePattern resource = new ResourcePattern(resourceType,
resourceName, PatternType.LITERAL);
+ List<Action> actions = List.of(
+ new Action(operation, resource, refCount, logIfAllowed,
logIfDenied)
+ );
+ return authorizer.get().get().authorize(requestContext,
actions).get(0) == AuthorizationResult.ALLOWED;
+ }
+
+ public boolean authorize(
+ RequestContext requestContext,
+ AclOperation operation,
+ ResourceType resourceType,
+ String resourceName
+ ) {
+ return authorize(requestContext, operation, resourceType,
resourceName, true, true, 1);
+ }
+
+ public void authorizeClusterOperation(Request request, AclOperation
operation) {
+ if (!authorize(request.context(), operation, ResourceType.CLUSTER,
Resource.CLUSTER_NAME)) {
+ throw new ClusterAuthorizationException("Request " + request + "
needs " + operation + " permission.");
+ }
+ }
+
+ public int authorizedOperations(Request request, Resource resource) {
+ List<AclOperation> supportedOps = new
ArrayList<>(AclEntry.supportedOperations(resource.resourceType()));
+ Set<AclOperation> authorizedOps;
+ if (authorizer.isPresent()) {
+ ResourcePattern resourcePattern = new
ResourcePattern(resource.resourceType(), resource.name(), PatternType.LITERAL);
+ List<Action> actions = supportedOps.stream()
+ .map(op -> new Action(op, resourcePattern, 1, false, false))
+ .toList();
+ List<AuthorizationResult> results =
authorizer.get().get().authorize(request.context(), actions);
+ authorizedOps = new HashSet<>();
+ for (int i = 0; i < results.size(); i++) {
+ if (results.get(i) == AuthorizationResult.ALLOWED) {
+ authorizedOps.add(supportedOps.get(i));
+ }
+ }
+ } else {
+ authorizedOps = new HashSet<>(supportedOps);
+ }
+ Set<Byte> opCodes = authorizedOps.stream()
+ .map(AclOperation::code)
+ .collect(Collectors.toSet());
+ return Utils.to32BitField(opCodes);
+ }
+
+ public boolean authorizeByResourceType(
+ RequestContext requestContext,
+ AclOperation operation,
+ ResourceType resourceType
+ ) {
+ return authorizer.map(authorizerPlugin ->
authorizerPlugin.get().authorizeByResourceType(requestContext, operation,
resourceType) == AuthorizationResult.ALLOWED).orElse(true);
+ }
+
+ public <T> Set<String> filterByAuthorized(
+ RequestContext requestContext,
+ AclOperation operation,
+ ResourceType resourceType,
+ Iterable<T> resources,
+ boolean logIfAllowed,
+ boolean logIfDenied,
+ Function<T, String> resourceName
+ ) {
+ if (authorizer.isEmpty()) {
+ Set<String> result = new HashSet<>();
+ for (T resource : resources) {
+ result.add(resourceName.apply(resource));
+ }
+ return result;
+ }
+ // Count occurrences of each resource name
+ Map<String, Integer> resourceNameToCount = new HashMap<>();
+ for (T resource : resources) {
+ String name = resourceName.apply(resource);
+ resourceNameToCount.merge(name, 1, Integer::sum);
+ }
+ if (resourceNameToCount.isEmpty()) {
+ return Set.of();
+ }
+
+ List<String> names = new ArrayList<>(resourceNameToCount.keySet());
+ List<Action> actions = names.stream()
+ .map(name -> new Action(
+ operation,
+ new ResourcePattern(resourceType, name, PatternType.LITERAL),
+ resourceNameToCount.get(name),
+ logIfAllowed,
+ logIfDenied
+ ))
+ .toList();
+ List<AuthorizationResult> results =
authorizer.get().get().authorize(requestContext, actions);
+ Set<String> authorized = new HashSet<>();
+ for (int i = 0; i < results.size(); i++) {
+ if (results.get(i) == AuthorizationResult.ALLOWED) {
+ authorized.add(names.get(i));
+ }
+ }
+ return authorized;
+ }
+
+ public <T> Set<String> filterByAuthorized(
+ RequestContext requestContext,
+ AclOperation operation,
+ ResourceType resourceType,
+ Iterable<T> resources,
+ Function<T, String> resourceName
+ ) {
+ return filterByAuthorized(requestContext, operation, resourceType,
resources, true, true, resourceName);
+ }
+
+ public <T> Map.Entry<List<T>, List<T>> partitionSeqByAuthorized(
Review Comment:
We should probably rename the method since we are not using `Seq`
Collections anymore. Same below
--
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]