FrankYang0529 commented on code in PR #22307:
URL: https://github.com/apache/kafka/pull/22307#discussion_r3273376527


##########
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:
   Thanks for the review. I rename the function as partitionByAuthorized and 
also refactor the output as PartitionResult class. It's more clear than using 
map key/value as authorized/unauthorized.



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