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


##########
server/src/main/java/org/apache/kafka/security/authorizer/AclEntry.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * 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.security.authorizer;
+
+import org.apache.kafka.common.acl.AccessControlEntry;
+import org.apache.kafka.common.acl.AclOperation;
+import org.apache.kafka.common.acl.AclPermissionType;
+import org.apache.kafka.common.protocol.Errors;
+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.utils.SecurityUtils;
+import org.apache.kafka.common.utils.Utils;
+import org.apache.kafka.server.util.Json;
+import org.apache.kafka.server.util.json.DecodeJson;
+import org.apache.kafka.server.util.json.JsonObject;
+import org.apache.kafka.server.util.json.JsonValue;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.apache.kafka.common.acl.AclOperation.ALTER;
+import static org.apache.kafka.common.acl.AclOperation.ALTER_CONFIGS;
+import static org.apache.kafka.common.acl.AclOperation.CLUSTER_ACTION;
+import static org.apache.kafka.common.acl.AclOperation.CREATE;
+import static org.apache.kafka.common.acl.AclOperation.CREATE_TOKENS;
+import static org.apache.kafka.common.acl.AclOperation.DELETE;
+import static org.apache.kafka.common.acl.AclOperation.DESCRIBE;
+import static org.apache.kafka.common.acl.AclOperation.DESCRIBE_CONFIGS;
+import static org.apache.kafka.common.acl.AclOperation.DESCRIBE_TOKENS;
+import static org.apache.kafka.common.acl.AclOperation.IDEMPOTENT_WRITE;
+import static org.apache.kafka.common.acl.AclOperation.READ;
+import static org.apache.kafka.common.acl.AclOperation.WRITE;
+
+public class AclEntry extends AccessControlEntry {
+    private static final DecodeJson.DecodeInteger INT = new 
DecodeJson.DecodeInteger();
+    private static final DecodeJson.DecodeString STRING = new 
DecodeJson.DecodeString();
+
+    public static final KafkaPrincipal WILDCARD_PRINCIPAL = new 
KafkaPrincipal(KafkaPrincipal.USER_TYPE, "*");
+    public static final String WILDCARD_PRINCIPAL_STRING = 
WILDCARD_PRINCIPAL.toString();
+    public static final String WILDCARD_HOST = "*";
+    public static final String WILDCARD_RESOURCE = 
ResourcePattern.WILDCARD_RESOURCE;
+    public static final String RESOURCE_SEPARATOR = ":";
+    public static final Set<ResourceType> RESOURCE_TYPES = 
Arrays.stream(ResourceType.values())
+        .filter(t -> !(t == ResourceType.UNKNOWN || t == ResourceType.ANY))
+        .collect(Collectors.toSet());
+    public static final Set<AclOperation> ACL_OPERATIONS = 
Arrays.stream(AclOperation.values())
+        .filter(t -> !(t == AclOperation.UNKNOWN || t == AclOperation.ANY))
+        .collect(Collectors.toSet());
+
+    private static final String PRINCIPAL_KEY = "principal";
+    private static final String PERMISSION_TYPE_KEY = "permissionType";
+    private static final String OPERATION_KEY = "operation";
+    private static final String HOSTS_KEY = "host";
+    public static final String VERSION_KEY = "version";
+    public static final int CURRENT_VERSION = 1;
+    private static final String ACLS_KEY = "acls";
+
+    public final AccessControlEntry ace;
+    public final KafkaPrincipal kafkaPrincipal;
+
+    public AclEntry(AccessControlEntry ace) {
+        super(ace.principal(), ace.host(), ace.operation(), 
ace.permissionType());
+        this.ace = ace;
+
+        kafkaPrincipal = ace.principal() == null
+            ? null
+            : SecurityUtils.parseKafkaPrincipal(ace.principal());
+    }
+
+    public static AclEntry apply(KafkaPrincipal principal,

Review Comment:
   Do we really need this helper? There is only one caller in production code. 
Also, testing code can create `AccessControlEntry` to instantiate `AclEntry` 



##########
core/src/main/scala/kafka/zk/ZkData.scala:
##########
@@ -757,8 +757,8 @@ case object ExtendedAclChangeStore extends ZkAclChangeStore 
{
 object ResourceZNode {
   def path(resource: ResourcePattern): String = 
ZkAclStore(resource.patternType).path(resource.resourceType, resource.name)
 
-  def encode(acls: Set[AclEntry]): Array[Byte] = 
Json.encodeAsBytes(AclEntry.toJsonCompatibleMap(acls).asJava)
-  def decode(bytes: Array[Byte], stat: Stat): VersionedAcls = 
VersionedAcls(AclEntry.fromBytes(bytes), stat.getVersion)
+  def encode(acls: Set[AclEntry]): Array[Byte] = 
Json.encodeAsBytes(AclEntry.toJsonCompatibleMap(acls.asJava))
+  def decode(bytes: Array[Byte], stat: Stat): VersionedAcls = 
VersionedAcls(immutable.Set().concat(AclEntry.fromBytes(bytes).asScala), 
stat.getVersion)

Review Comment:
   IIRC, `.toSet` can generate the immutable object, so it can be simplified to 
`AclEntry.fromBytes(bytes).asScala.toSet`



##########
clients/src/main/java/org/apache/kafka/common/utils/Utils.java:
##########
@@ -1675,6 +1675,15 @@ public static <V> Map<String, V> 
entriesWithPrefix(Map<String, V> map, String pr
         return result;
     }
 
+    /**
+     * Checks requirement. Throw {@link IllegalArgumentException} if {@code 
requirement} failed.
+     * @param requirement Requirement to check.
+     */
+    public static void require(boolean requirement) {

Review Comment:
   Can it take custom error message? Current error message is a bit meaningless.



##########
core/src/main/scala/kafka/admin/AclCommand.scala:
##########
@@ -486,7 +486,8 @@ object AclCommand extends Logging {
 
   private def validateOperation(opts: AclCommandOptions, resourceToAcls: 
Map[ResourcePatternFilter, Set[AccessControlEntry]]): Unit = {
     for ((resource, acls) <- resourceToAcls) {
-      val validOps = AclEntry.supportedOperations(resource.resourceType) + 
AclOperation.ALL
+      val validOps = 
AclEntry.supportedOperations(resource.resourceType).asScala

Review Comment:
   how about `val validOps = 
AclEntry.supportedOperations(resource.resourceType).asScala + AclOperation.ALL`



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