Copilot commented on code in PR #8880:
URL: https://github.com/apache/hbase/pull/8880#discussion_r4564625855


##########
hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/PermissionStorage.java:
##########
@@ -442,279 +463,203 @@ static Map<byte[], ListMultimap<String, 
UserPermission>> loadAll(Configuration c
     Scan scan = new Scan();
     scan.addFamily(ACL_LIST_FAMILY);
 
-    ResultScanner scanner = null;
     // TODO: Pass in a Connection rather than create one each time.
-    try (Connection connection = ConnectionFactory.createConnection(conf)) {
-      try (Table table = connection.getTable(ACL_TABLE_NAME)) {
-        scanner = table.getScanner(scan);
-        try {
-          for (Result row : scanner) {
-            ListMultimap<String, UserPermission> resultPerms =
-              parsePermissions(row.getRow(), row, null, null, null, false);
-            allPerms.put(row.getRow(), resultPerms);
-          }
-        } finally {
-          if (scanner != null) {
-            scanner.close();
-          }
+    try (Table table = conn.getTable(ACL_TABLE_NAME);
+      ResultScanner scanner = table.getScanner(scan)) {
+      while (true) {
+        Result result = scanner.next();
+        if (result == null) {
+          break;
         }
+        byte[] entryName = result.getRow();
+        ListMultimap<String, UserPermission> perms = 
ArrayListMultimap.create();
+        result.getFamilyMap(ACL_LIST_FAMILY).forEach((qual, actionCodes) -> {
+          UserPermission userPerm = parseUserPermission(entryName, qual, 
actionCodes);
+          perms.put(userPerm.getUser(), userPerm);

Review Comment:
   `parseUserPermission(...)` can return null when `actionCodes` is null/empty 
(see method body), but this loop unconditionally dereferences `userPerm`. This 
will NPE if the ACL table contains empty values or partially-written cells. Add 
a null guard before `perms.put(...)` (and ideally skip/log malformed cells).



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/PermissionUtils.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.hadoop.hbase.security.access;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.mutable.MutableObject;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.yetus.audience.InterfaceAudience;
+
[email protected]
+public final class PermissionUtils {
+
+  private static Set<String> getUserGroups(MutableObject<Set<String>> 
userGroupsHolder,
+    String username) {
+    Set<String> userGroups = userGroupsHolder.get();
+    if (userGroups != null) {
+      return userGroups;
+    }
+    userGroups = new HashSet<String>(AccessChecker.getUserGroups(username));
+    userGroupsHolder.setValue(userGroups);
+    return userGroups;
+  }
+
+  /**
+   * Filter user permissions based on username. Just delegate call to
+   * {@link #filter(List, byte[], byte[], String)} with null {@code cf} and 
{@code cq}.
+   */
+  public static List<UserPermission> filter(List<UserPermission> userPerms, 
String username) {
+    return filter(userPerms, null, null, username);
+  }
+
+  /**
+   * Filter user permissions based on column family, column qualifier, and 
username.
+   * <p>
+   * Notice that, if {@code cf} or {@code cq} is not null, we will assume that 
the permission is a
+   * table permission.
+   * <p>
+   * When there is group permission, i.e, the user field for the {@link 
UserPermission} is start
+   * with {@literal @}, then we will check whether the given {@code username} 
belongs to this group.
+   */
+  public static List<UserPermission> filter(List<UserPermission> userPerms, 
byte[] cf, byte[] cq,
+    String username) {
+    if (cf == null && cq == null && username == null) {
+      // nothing to filter
+      return userPerms;
+    }
+    Stream<UserPermission> stream = userPerms.stream();
+    if (cf != null) {
+      stream =
+        stream.filter(up -> Bytes.equals(((TablePermission) 
up.getPermission()).getFamily(), cf));
+    }
+    if (cq != null) {
+      stream = stream
+        .filter(up -> Bytes.equals(((TablePermission) 
up.getPermission()).getQualifier(), cq));
+    }
+    if (username != null) {
+      MutableObject<Set<String>> userGroupsHolder = new MutableObject<>();
+      stream = stream.filter(up -> {
+        String u = up.getUser();
+        if (u.charAt(0) == '@') {
+          // this is a group, check whether the given user belongs to this 
group
+          return getUserGroups(userGroupsHolder, 
username).contains(u.substring(1));
+        }
+        return u.equals(username);
+      });
+    }

Review Comment:
   The new filtering logic breaks the prior semantics for filtering *group 
principals* (i.e., when the requested `username` itself starts with '@'). 
Previously, querying for a group entry matched only the exact group principal; 
now it attempts to look up groups for the literal name (e.g., '@admins') and 
will likely filter out the group entry entirely. Update the predicate so that 
when `username` starts with '@', it matches `u.equals(username)` (and does not 
perform membership checks). Also, `u.charAt(0)` will throw for an empty 
principal string; using `u.startsWith(\"@\")` (or checking length) avoids that 
failure mode.



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/PermissionStorage.java:
##########
@@ -123,69 +120,132 @@ public final class PermissionStorage {
   private PermissionStorage() {
   }
 
+  private static byte[] toTableEntryName(TableName tableName) {
+    return tableName.getName();
+  }
+
+  private static String getNamespaceFromEntryName(byte[] entryName) {
+    return Bytes.toString(entryName, 1);
+  }
+
   /**
-   * Stores a new user permission grant in the access control lists table.
-   * @param conf     the configuration
-   * @param userPerm the details of the permission to be granted
-   * @param t        acl table instance. It is closed upon method return.
-   * @throws IOException in the case of an error accessing the metadata table
+   * Return the username for this table entry, and add family/qualifer to the 
given {@code builder}
+   * if available.

Review Comment:
   Correct typo in Javadoc: 'qualifer' should be 'qualifier'.



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java:
##########
@@ -280,11 +279,11 @@ private void updateACL(RegionCoprocessorEnvironment e, 
final Map<byte[], List<Ce
     // to and fro conversion overhead. get req is converted to PB req
     // and results are converted to PB results 1st and then to POJOs
     // again. We could have avoided such at least in ACL table context..
-    try (Table t = 
e.getConnection().getTable(PermissionStorage.ACL_TABLE_NAME)) {
+    try {
       for (byte[] entry : entries) {
         currentEntry = entry;
         ListMultimap<String, UserPermission> perms =
-          PermissionStorage.getPermissions(conf, entry, t, null, null, null, 
false);
+          PermissionStorage.getPermissions(e.getConnection(), entry);
         byte[] serialized = PermissionStorage.writePermissionsAsBytes(perms, 
conf);
         zkPermissionWatcher.writeToZookeeper(entry, serialized);
       }

Review Comment:
   `PermissionStorage.getPermissions(Connection, entry)` opens and closes the 
ACL `Table` internally for each call. In this loop, that becomes one table 
open/close per ACL entry, whereas the previous implementation reused a single 
`Table` instance for the whole sync. Consider reintroducing an overload that 
accepts a `Table` (or move the `Table` acquisition back into this method) so 
the update can reuse the same `Table` across iterations.



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/PermissionStorage.java:
##########
@@ -442,279 +463,203 @@ static Map<byte[], ListMultimap<String, 
UserPermission>> loadAll(Configuration c
     Scan scan = new Scan();
     scan.addFamily(ACL_LIST_FAMILY);
 
-    ResultScanner scanner = null;
     // TODO: Pass in a Connection rather than create one each time.
-    try (Connection connection = ConnectionFactory.createConnection(conf)) {
-      try (Table table = connection.getTable(ACL_TABLE_NAME)) {
-        scanner = table.getScanner(scan);
-        try {
-          for (Result row : scanner) {
-            ListMultimap<String, UserPermission> resultPerms =
-              parsePermissions(row.getRow(), row, null, null, null, false);
-            allPerms.put(row.getRow(), resultPerms);
-          }
-        } finally {
-          if (scanner != null) {
-            scanner.close();
-          }
+    try (Table table = conn.getTable(ACL_TABLE_NAME);
+      ResultScanner scanner = table.getScanner(scan)) {
+      while (true) {
+        Result result = scanner.next();
+        if (result == null) {
+          break;
         }
+        byte[] entryName = result.getRow();
+        ListMultimap<String, UserPermission> perms = 
ArrayListMultimap.create();
+        result.getFamilyMap(ACL_LIST_FAMILY).forEach((qual, actionCodes) -> {
+          UserPermission userPerm = parseUserPermission(entryName, qual, 
actionCodes);
+          perms.put(userPerm.getUser(), userPerm);
+        });
+        allPerms.put(entryName, perms);
       }
     }
 
     return allPerms;
   }
 
-  public static ListMultimap<String, UserPermission> 
getTablePermissions(Configuration conf,
-    TableName tableName) throws IOException {
-    return getTablePermissions(conf, tableName, null);
-  }
-
-  public static ListMultimap<String, UserPermission> 
getTablePermissions(Configuration conf,
-    TableName tableName, Table t) throws IOException {
-    return getPermissions(conf, tableName != null ? tableName.getName() : 
null, t, null, null, null,
-      false);
-  }
-
-  public static ListMultimap<String, UserPermission> 
getNamespacePermissions(Configuration conf,
-    String namespace) throws IOException {
-    return getPermissions(conf, Bytes.toBytes(toNamespaceEntry(namespace)), 
null, null, null, null,
-      false);
-  }
-
-  public static ListMultimap<String, UserPermission> 
getGlobalPermissions(Configuration conf)
-    throws IOException {
-    return getPermissions(conf, null, null, null, null, null, false);
-  }
-
   /**
-   * Reads user permission assignments stored in the <code>l:</code> column 
family of the first
-   * table row in <code>_acl_</code>.
-   * <p>
-   * See {@link PermissionStorage class documentation} for the key structure 
used for storage.
-   * </p>
+   * Get all the permission entries for this table.
    */
-  static ListMultimap<String, UserPermission> getPermissions(Configuration 
conf, byte[] entryName,
-    Table t, byte[] cf, byte[] cq, String user, boolean hasFilterUser) throws 
IOException {
-    if (entryName == null) {
-      entryName = ACL_GLOBAL_NAME;
-    }
-    // for normal user tables, we just read the table row from _acl_
-    ListMultimap<String, UserPermission> perms = ArrayListMultimap.create();
-    Get get = new Get(entryName);
-    get.addFamily(ACL_LIST_FAMILY);
-    Result row = null;
-    if (t == null) {
-      try (Connection connection = ConnectionFactory.createConnection(conf)) {
-        try (Table table = connection.getTable(ACL_TABLE_NAME)) {
-          row = table.get(get);
-        }
+  public static List<UserPermission> getTablePermissions(Connection conn, 
TableName tableName)
+    throws IOException {
+    byte[] entryName = toTableEntryName(tableName);
+    Get get = new Get(entryName).addFamily(ACL_LIST_FAMILY);
+    try (Table aclTable = conn.getTable(ACL_TABLE_NAME)) {
+      Result result = aclTable.get(get);
+      if (result.isEmpty()) {
+        return Collections.emptyList();
       }
-    } else {
-      row = t.get(get);
-    }
-    if (!row.isEmpty()) {
-      perms = parsePermissions(entryName, row, cf, cq, user, hasFilterUser);
-    } else {
-      LOG.info("No permissions found in " + ACL_TABLE_NAME + " for acl entry "
-        + Bytes.toString(entryName));
+      List<UserPermission> userPerms = new ArrayList<>();
+      result.getFamilyMap(ACL_LIST_FAMILY).forEach((qual, actionCodes) -> {
+        Permission.Builder builder = 
Permission.newBuilder(tableName).withActionCodes(actionCodes);
+        String user = parseTableEntry(builder, qual);
+        userPerms.add(new UserPermission(user, builder.build()));
+      });
+      return userPerms;
     }
-
-    return perms;
   }
 
-  /**
-   * Returns the currently granted permissions for a given table as the 
specified user plus
-   * associated permissions.
-   */
-  public static List<UserPermission> getUserTablePermissions(Configuration 
conf,
-    TableName tableName, byte[] cf, byte[] cq, String userName, boolean 
hasFilterUser)
+  public static List<UserPermission> getNamespacePermissions(Connection conn, 
String namespace)
     throws IOException {
-    return getUserPermissions(conf, tableName == null ? null : 
tableName.getName(), cf, cq,
-      userName, hasFilterUser);
+    byte[] entryName = toNamespaceEntry(namespace);
+    Get get = new Get(entryName).addFamily(ACL_LIST_FAMILY);
+    try (Table aclTable = conn.getTable(ACL_TABLE_NAME)) {
+      Result result = aclTable.get(get);
+      if (result.isEmpty()) {
+        return Collections.emptyList();
+      }
+      List<UserPermission> userPerms = new ArrayList<>();
+      result.getFamilyMap(ACL_LIST_FAMILY).forEach((qual, actionCodes) -> {
+        Permission perm = 
Permission.newBuilder(namespace).withActionCodes(actionCodes).build();
+        String user = Bytes.toString(qual);
+        userPerms.add(new UserPermission(user, perm));
+      });
+      return userPerms;
+    }
+  }
+
+  public static List<UserPermission> getGlobalPermissions(Connection conn) 
throws IOException {
+    Get get = new Get(ACL_GLOBAL_NAME).addFamily(ACL_LIST_FAMILY);
+    try (Table aclTable = conn.getTable(ACL_TABLE_NAME)) {
+      Result result = aclTable.get(get);
+      if (result.isEmpty()) {
+        return Collections.emptyList();
+      }
+      List<UserPermission> userPerms = new ArrayList<>();
+      result.getFamilyMap(ACL_LIST_FAMILY).forEach((qual, actionCodes) -> {
+        Permission perm = 
Permission.newBuilder().withActionCodes(actionCodes).build();
+        String user = Bytes.toString(qual);
+        userPerms.add(new UserPermission(user, perm));
+      });
+      return userPerms;
+    }
   }
 
-  /**
-   * Returns the currently granted permissions for a given namespace as the 
specified user plus
-   * associated permissions.
-   */
-  public static List<UserPermission> getUserNamespacePermissions(Configuration 
conf,
-    String namespace, String user, boolean hasFilterUser) throws IOException {
-    return getUserPermissions(conf, 
Bytes.toBytes(toNamespaceEntry(namespace)), null, null, user,
-      hasFilterUser);
+  public static boolean isGlobalEntry(byte[] entryName) {
+    return Bytes.equals(entryName, ACL_GLOBAL_NAME);
   }
 
-  /**
-   * Returns the currently granted permissions for a given table/namespace 
with associated
-   * permissions based on the specified column family, column qualifier and 
user name.
-   * @param conf          the configuration
-   * @param entryName     Table name or the namespace
-   * @param cf            Column family
-   * @param cq            Column qualifier
-   * @param user          User name to be filtered from permission as requested
-   * @param hasFilterUser true if filter user is provided, otherwise false.
-   * @return List of UserPermissions
-   * @throws IOException on failure
-   */
-  public static List<UserPermission> getUserPermissions(Configuration conf, 
byte[] entryName,
-    byte[] cf, byte[] cq, String user, boolean hasFilterUser) throws 
IOException {
-    ListMultimap<String, UserPermission> allPerms =
-      getPermissions(conf, entryName, null, cf, cq, user, hasFilterUser);
-    List<UserPermission> perms = new ArrayList<>();
-    for (Map.Entry<String, UserPermission> entry : allPerms.entries()) {
-      perms.add(entry.getValue());
-    }
-    return perms;
+  public static boolean isNamespaceEntry(String entryName) {
+    return isNamespaceEntry(Bytes.toBytes(entryName));
   }
 
-  /**
-   * Parse and filter permission based on the specified column family, column 
qualifier and user
-   * name.
-   */
-  private static ListMultimap<String, UserPermission> parsePermissions(byte[] 
entryName,
-    Result result, byte[] cf, byte[] cq, String user, boolean hasFilterUser) {
-    ListMultimap<String, UserPermission> perms = ArrayListMultimap.create();
-    if (result != null && result.size() > 0) {
-      for (Cell kv : result.rawCells()) {
-        Pair<String, Permission> permissionsOfUserOnTable =
-          parsePermissionRecord(entryName, kv, cf, cq, hasFilterUser, user);
-
-        if (permissionsOfUserOnTable != null) {
-          String username = permissionsOfUserOnTable.getFirst();
-          Permission permission = permissionsOfUserOnTable.getSecond();
-          perms.put(username, new UserPermission(username, permission));
-        }
-      }
-    }
-    return perms;
+  public static boolean isNamespaceEntry(byte[] entryName) {
+    return entryName != null && entryName.length != 0 && entryName[0] == 
NAMESPACE_PREFIX;
   }
 
-  private static Pair<String, Permission> parsePermissionRecord(byte[] 
entryName, Cell kv,
-    byte[] cf, byte[] cq, boolean filterPerms, String filterUser) {
-    // return X given a set of permissions encoded in the permissionRecord kv.
-    byte[] family = CellUtil.cloneFamily(kv);
-    if (!Bytes.equals(family, ACL_LIST_FAMILY)) {
-      return null;
-    }
-
-    byte[] key = CellUtil.cloneQualifier(kv);
-    byte[] value = CellUtil.cloneValue(kv);
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("Read acl: entry[" + Bytes.toStringBinary(entryName) + "], kv 
["
-        + Bytes.toStringBinary(key) + ": " + Bytes.toStringBinary(value) + 
"]");
-    }
-
-    // check for a column family appended to the key
-    // TODO: avoid the string conversion to make this more efficient
-    String username = Bytes.toString(key);
-
-    // Retrieve group list for the filterUser if cell key is a group.
-    // Group list is not required when filterUser itself a group
-    List<String> filterUserGroups = null;
-    if (filterPerms) {
-      if (
-        username.charAt(0) == '@' && !StringUtils.isEmpty(filterUser) && 
filterUser.charAt(0) != '@'
-      ) {
-        filterUserGroups = AccessChecker.getUserGroups(filterUser);
-      }
-    }
+  public static boolean isTableEntry(byte[] entryName) {
+    return !isNamespaceEntry(entryName) && !isGlobalEntry(entryName) && 
entryName != null;
+  }
 
-    // Handle namespace entry
-    if (isNamespaceEntry(entryName)) {
-      // Filter the permissions cell record if client query
-      if (filterPerms && !validateFilterUser(username, filterUser, 
filterUserGroups)) {
-        return null;
-      }
+  public static byte[] toNamespaceEntry(String namespace) {
+    return Bytes.toBytes(NAMESPACE_PREFIX + namespace);
+  }
 
-      return new Pair<>(username, Permission
-        
.newBuilder(Bytes.toString(fromNamespaceEntry(entryName))).withActionCodes(value).build());
+  public static String fromNamespaceEntry(String namespace) {
+    if (namespace.charAt(0) != NAMESPACE_PREFIX) {
+      throw new IllegalArgumentException("Argument is not a valid namespace 
entry");
     }
+    return namespace.substring(1);
+  }
 
-    // Handle global entry
-    if (isGlobalEntry(entryName)) {
-      // Filter the permissions cell record if client query
-      if (filterPerms && !validateFilterUser(username, filterUser, 
filterUserGroups)) {
-        return null;
-      }
+  public static byte[] toNamespaceEntry(byte[] namespace) {
+    byte[] ret = new byte[namespace.length + 1];
+    ret[0] = NAMESPACE_PREFIX;
+    System.arraycopy(namespace, 0, ret, 1, namespace.length);
+    return ret;
+  }
 
-      return new Pair<>(username, 
Permission.newBuilder().withActionCodes(value).build());
+  public static byte[] fromNamespaceEntry(byte[] namespace) {
+    if (namespace[0] != NAMESPACE_PREFIX) {
+      throw new IllegalArgumentException(
+        "Argument is not a valid namespace entry: " + 
Bytes.toString(namespace));
     }
+    return Arrays.copyOfRange(namespace, 1, namespace.length);
+  }
 
-    // Handle table entry
-    int idx = username.indexOf(ACL_KEY_DELIMITER);
-    byte[] permFamily = null;
-    byte[] permQualifier = null;
-    if (idx > 0 && idx < username.length() - 1) {
-      String remainder = username.substring(idx + 1);
-      username = username.substring(0, idx);
-      idx = remainder.indexOf(ACL_KEY_DELIMITER);
-      if (idx > 0 && idx < remainder.length() - 1) {
-        permFamily = Bytes.toBytes(remainder.substring(0, idx));
-        permQualifier = Bytes.toBytes(remainder.substring(idx + 1));
-      } else {
-        permFamily = Bytes.toBytes(remainder);
-      }
+  public static List<Permission> getCellPermissionsForUser(User user, 
ExtendedCell cell)
+    throws IOException {
+    // Save an object allocation where we can
+    if (cell.getTagsLength() == 0) {
+      return null;
     }
-
-    // Filter the permissions cell record if client query
-    if (filterPerms) {
-      // ACL table contain 3 types of cell key entries; hbase:Acl, namespace 
and table. So to filter
-      // the permission cell records additional validations are required at 
CF, CQ and username.
-      // Here we can proceed based on client input whether it contain 
filterUser.
-      // Validate the filterUser when specified
-      if (filterUser != null && !validateFilterUser(username, filterUser, 
filterUserGroups)) {
-        return null;
-      }
-      if (!validateCFAndCQ(permFamily, cf, permQualifier, cq)) {
-        return null;
+    List<Permission> results = Lists.newArrayList();
+    Iterator<Tag> tagsIterator = PrivateCellUtil.tagsIterator(cell);
+    while (tagsIterator.hasNext()) {
+      Tag tag = tagsIterator.next();
+      if (tag.getType() == ACL_TAG_TYPE) {
+        // Deserialize the table permissions from the KV
+        // TODO: This can be improved. Don't build UsersAndPermissions just to 
unpack it again,
+        // use the builder
+        AccessControlProtos.UsersAndPermissions.Builder builder =
+          AccessControlProtos.UsersAndPermissions.newBuilder();
+        if (tag.hasArray()) {
+          ProtobufUtil.mergeFrom(builder, tag.getValueArray(), 
tag.getValueOffset(),
+            tag.getValueLength());
+        } else {
+          ProtobufUtil.mergeFrom(builder, Tag.cloneValue(tag));
+        }
+        ListMultimap<String, Permission> kvPerms =
+          AccessControlUtil.toUsersAndPermissions(builder.build());
+        // Are there permissions for this user?
+        List<Permission> userPerms = kvPerms.get(user.getShortName());
+        if (userPerms != null) {
+          results.addAll(userPerms);
+        }
+        // Are there permissions for any of the groups this user belongs to?
+        String[] groupNames = user.getGroupNames();
+        if (groupNames != null) {
+          for (String group : groupNames) {
+            List<Permission> groupPerms = 
kvPerms.get(AuthUtil.toGroupEntry(group));
+            if (results != null) {
+              results.addAll(groupPerms);
+            }
+          }
+        }
       }
     }
-
-    return new Pair<>(username, 
Permission.newBuilder(TableName.valueOf(entryName))
-      
.withFamily(permFamily).withQualifier(permQualifier).withActionCodes(value).build());
+    return results;
   }
 
-  /*
-   * Validate the cell key with the client filterUser if specified in the 
query input. 1. If cell
-   * key (username) is not a group then check whether client filterUser is 
equal to username 2. If
-   * cell key (username) is a group then check whether client filterUser 
belongs to the cell key
-   * group (username) 3. In case when both filterUser and username are group 
names then cell will be
-   * filtered if not equal.
+  // ====================
+  // Below are methods for sync permission between acl table and zk
+  // ====================
+
+  /**
+   * Reads user permission assignments stored in the <code>l:</code> column 
family of the first
+   * table row in <code>_acl_</code>.
+   * <p>
+   * See {@link PermissionStorage class documentation} for the key structure 
used for storage.
+   * </p>
    */
-  private static boolean validateFilterUser(String username, String filterUser,
-    List<String> filterUserGroups) {
-    if (filterUserGroups == null) {
-      // Validate user name or group names whether equal
-      if (filterUser.equals(username)) {
-        return true;
-      }
+  static ListMultimap<String, UserPermission> getPermissions(Connection conn, 
byte[] entryName)
+    throws IOException {
+    // for normal user tables, we just read the table row from _acl_
+    ListMultimap<String, UserPermission> perms = ArrayListMultimap.create();
+    Get get = new Get(entryName);
+    get.addFamily(ACL_LIST_FAMILY);
+    Result result;
+    try (Table aclTable = conn.getTable(ACL_TABLE_NAME)) {
+      result = aclTable.get(get);
+    }
+    if (!result.isEmpty()) {
+      result.getFamilyMap(ACL_LIST_FAMILY).forEach((qual, actionCodes) -> {
+        UserPermission userPerms = parseUserPermission(entryName, qual, 
actionCodes);

Review Comment:
   Same null-dereference issue as in `loadAll(Connection)`: 
`parseUserPermission(...)` may return null for empty action codes, but the code 
unconditionally calls `userPerms.getUser()`. Add a null check (and consider 
skipping malformed entries) to avoid runtime failures during ACL-to-ZK sync and 
other internal reads.



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/PermissionStorage.java:
##########
@@ -123,69 +120,132 @@ public final class PermissionStorage {
   private PermissionStorage() {
   }
 
+  private static byte[] toTableEntryName(TableName tableName) {
+    return tableName.getName();
+  }
+
+  private static String getNamespaceFromEntryName(byte[] entryName) {
+    return Bytes.toString(entryName, 1);
+  }
+
   /**
-   * Stores a new user permission grant in the access control lists table.
-   * @param conf     the configuration
-   * @param userPerm the details of the permission to be granted
-   * @param t        acl table instance. It is closed upon method return.
-   * @throws IOException in the case of an error accessing the metadata table
+   * Return the username for this table entry, and add family/qualifer to the 
given {@code builder}
+   * if available.
    */
-  public static void addUserPermission(Configuration conf, UserPermission 
userPerm, Table t,
-    boolean mergeExistingPermissions) throws IOException {
-    Permission permission = userPerm.getPermission();
-    Permission.Action[] actions = permission.getActions();
-    byte[] rowKey = userPermissionRowKey(permission);
-    Put p = new Put(rowKey);
-    byte[] key = userPermissionKey(userPerm);
-
-    if ((actions == null) || (actions.length == 0)) {
-      String msg = "No actions associated with user '" + userPerm.getUser() + 
"'";
-      LOG.warn(msg);
-      throw new IOException(msg);
-    }
-
-    Set<Permission.Action> actionSet = new TreeSet<Permission.Action>();
-    if (mergeExistingPermissions) {
-      List<UserPermission> perms = getUserPermissions(conf, rowKey, null, 
null, null, false);
-      UserPermission currentPerm = null;
-      for (UserPermission perm : perms) {
-        if (userPerm.equalsExceptActions(perm)) {
-          currentPerm = perm;
-          break;
-        }
-      }
+  private static String parseTableEntry(Permission.Builder builder, byte[] 
qual) {
+    byte delimiter = (byte) ACL_KEY_DELIMITER;
+    int idx = ArrayUtils.indexOf(qual, delimiter);
+    // no family/qualifier
+    if (idx < 0) {
+      return Bytes.toString(qual);
+    }
+    int idx2 = ArrayUtils.indexOf(qual, delimiter, idx + 1);
+    if (idx2 < 0) {
+      // no qualifer
+      builder.withFamily(Arrays.copyOfRange(qual, idx + 1, qual.length));
+    } else {
+      builder.withFamily(Arrays.copyOfRange(qual, idx + 1, idx2))
+        .withQualifier(Arrays.copyOfRange(qual, idx2 + 1, qual.length));
+    }
+    return Bytes.toString(qual, 0, idx);
+  }
 
-      if (currentPerm != null && currentPerm.getPermission().getActions() != 
null) {
-        
actionSet.addAll(Arrays.asList(currentPerm.getPermission().getActions()));
-      }
+  private static UserPermission parseUserPermission(byte[] entryName, byte[] 
qual,
+    byte[] actionCodes) {
+    if (actionCodes == null || actionCodes.length == 0) {
+      return null;
     }
+    String user;
+    Permission.Builder builder;
+    if (isGlobalEntry(entryName)) {
+      builder = Permission.newBuilder();
+      user = Bytes.toString(qual);
+    } else if (isNamespaceEntry(entryName)) {
+      builder = Permission.newBuilder(getNamespaceFromEntryName(entryName));
+      user = Bytes.toString(qual);
+    } else {
+      // table entry
+      builder = Permission.newBuilder(TableName.valueOf(entryName));
+      user = parseTableEntry(builder, qual);
+    }
+    return new UserPermission(user, 
builder.withActionCodes(actionCodes).build());
+  }
 
-    // merge current action with new action.
-    actionSet.addAll(Arrays.asList(actions));
+  // mainly used in grant/revoke, where we need to get the exact permission 
record for a given user
+  private static UserPermission getUserPermission(Table aclTable, 
UserPermission userPerm)
+    throws IOException {
+    byte[] entryName = userPermissionRowKey(userPerm.getPermission());
+    Get get = new Get(entryName);
+    byte[] qual = userPermissionKey(userPerm);
+    get.addColumn(ACL_LIST_FAMILY, qual);
+    Result result = aclTable.get(get);
+    if (result.isEmpty()) {
+      return null;
+    }
+    byte[] actionCodes = result.getValue(ACL_LIST_FAMILY, qual);
+    return parseUserPermission(entryName, qual, actionCodes);
+  }
 
-    // serialize to byte array.
-    byte[] value = new byte[actionSet.size()];
-    int index = 0;
-    for (Permission.Action action : actionSet) {
-      value[index++] = action.code();
+  private static byte[] getActionCodes(Permission perm) {
+    Permission.Action[] actions = perm.getActions();
+    byte[] actionCodes = new byte[actions.length];
+    for (int i = 0; i < actionCodes.length; i++) {
+      actionCodes[i] = actions[i].code();
     }
-    
p.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(p.getRow())
-      
.setFamily(ACL_LIST_FAMILY).setQualifier(key).setTimestamp(p.getTimestamp()).setType(Type.Put)
-      .setValue(value).build());
+    return actionCodes;
+  }
+
+  private static void put(Table aclTable, UserPermission userPerm) throws 
IOException {
+    Permission perm = userPerm.getPermission();
+    Put put = new Put(userPermissionRowKey(perm));
+    byte[] qual = userPermissionKey(userPerm);
+    byte[] value = getActionCodes(perm);
+    put.addColumn(ACL_LIST_FAMILY, qual, value);
     if (LOG.isDebugEnabled()) {
-      LOG.debug("Writing permission with rowKey " + Bytes.toString(rowKey) + " 
"
-        + Bytes.toString(key) + ": " + Bytes.toStringBinary(value));
+      LOG.debug("Writing permission with rowKey " + 
Bytes.toString(put.getRow()) + " "
+        + Bytes.toString(qual) + ": " + Bytes.toStringBinary(value));
     }
-    try {
-      t.put(p);
-    } finally {
-      t.close();
+    aclTable.put(put);
+  }
+
+  private static void delete(UserPermission userPerm, Table aclTable) throws 
IOException {
+    Delete d = new Delete(userPermissionRowKey(userPerm.getPermission()));
+    byte[] qual = userPermissionKey(userPerm);
+    d.addColumns(ACL_LIST_FAMILY, qual);
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Removing permission with rowKey " + 
Bytes.toString(d.getRow()) + " "
+        + Bytes.toString(qual));
     }
+    aclTable.delete(d);
   }
 
-  static void addUserPermission(Configuration conf, UserPermission userPerm, 
Table t)
-    throws IOException {
-    addUserPermission(conf, userPerm, t, false);
+  /**
+   * Stores a new user permission grant in the access control lists table.
+   * @param conn     connection to hbase cluster
+   * @param userPerm the details of the permission to be granted
+   * @param aclTable acl table instance. It is closed upon method return.
+   * @throws IOException in the case of an error accessing the metadata table
+   */
+  public static void addUserPermission(Connection conn, UserPermission 
userPerm,
+    boolean mergeExistingPermissions) throws IOException {
+    try (Table aclTable = conn.getTable(ACL_TABLE_NAME)) {
+      UserPermission toAdd;
+      if (mergeExistingPermissions) {
+        UserPermission oldUserPerm = getUserPermission(aclTable, userPerm);
+        if (oldUserPerm == null || 
ArrayUtils.isEmpty(oldUserPerm.getPermission().getActions())) {
+          toAdd = userPerm;
+        } else {
+          Set<Permission.Action> actionSet = new TreeSet<>();
+          
actionSet.addAll(Arrays.asList(oldUserPerm.getPermission().getActions()));
+          
actionSet.addAll(Arrays.asList(userPerm.getPermission().getActions()));
+          
oldUserPerm.getPermission().setActions(actionSet.toArray(Permission.Action[]::new));
+          toAdd = oldUserPerm;
+        }
+      } else {
+        toAdd = userPerm;
+      }
+      put(aclTable, toAdd);
+    }
   }

Review Comment:
   The refactored addUserPermission no longer validates that 
`userPerm.getPermission().getActions()` is non-null and non-empty (the old 
implementation explicitly rejected empty actions). As written, 
`userPerm.getPermission().getActions()` can cause an immediate NPE (e.g., in 
merge mode at line 240, or later in `put()`/`getActionCodes()`), and an empty 
action array would write an invalid/empty record that downstream parsing treats 
as absent. Restore the explicit validation (throwing `IOException` with a clear 
message) before attempting to merge/write.



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