You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gravitino.apache.org by "hdygxsj (via GitHub)" <gi...@apache.org> on 2025/08/05 14:06:24 UTC

[PR] [#7914] improvement(authz): Optimize the metadata array filtering code [gravitino]

hdygxsj opened a new pull request, #7941:
URL: https://github.com/apache/gravitino/pull/7941

   ### What changes were proposed in this pull request?
   
    Optimize the metadata array filtering code
   
   ### Why are the changes needed?
   
   Fix: #7914 
   
   ### Does this PR introduce _any_ user-facing change?
   
   None
   
   ### How was this patch tested?
   
   Existing IT
   


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#7914] improvement(authz): Optimize the metadata array filtering code [gravitino]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #7941:
URL: https://github.com/apache/gravitino/pull/7941#discussion_r2255616012


##########
server/src/main/java/org/apache/gravitino/server/web/rest/UserOperations.java:
##########
@@ -60,6 +57,9 @@ public class UserOperations {
 
   private static final Logger LOG = LoggerFactory.getLogger(UserOperations.class);
 
+  private static final String LOAD_USER_PRIVILEGE =
+      "METALAKE::OWNER || MATALAKE::MANAGE_USERS || USER::SELF";

Review Comment:
   There's a typo in the privilege expression: 'MATALAKE' should be 'METALAKE' to be consistent with the first occurrence in the expression.
   ```suggestion
         "METALAKE::OWNER || METALAKE::MANAGE_USERS || USER::SELF";
   ```



##########
server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataFilterHelper.java:
##########
@@ -100,6 +101,39 @@ public static NameIdentifier[] filterByExpression(
         .toArray(NameIdentifier[]::new);
   }
 
+  /**
+   * Call {@link AuthorizationExpressionEvaluator} to filter the metadata list
+   *
+   * @param metalake metalake
+   * @param expression expression
+   * @param entityType entity type
+   * @param entities entities
+   * @param toNameIdentifier convert to NameIdentifier
+   * @return Filtered Metadata Entity
+   * @param <E> Entity class
+   */
+  public static <E> E[] filterByExpression(
+      String metalake,
+      String expression,
+      Entity.EntityType entityType,
+      E[] entities,
+      Function<E, NameIdentifier> toNameIdentifier) {
+    if (!enableAuthorization()) {
+      return entities;
+    }
+    AuthorizationExpressionEvaluator authorizationExpressionEvaluator =
+        new AuthorizationExpressionEvaluator(expression);
+    return Arrays.stream(entities)
+        .filter(
+            entity -> {
+              NameIdentifier nameIdentifier = toNameIdentifier.apply(entity);
+              Map<Entity.EntityType, NameIdentifier> nameIdentifierMap =
+                  spiltMetadataNames(metalake, entityType, nameIdentifier);
+              return authorizationExpressionEvaluator.evaluate(nameIdentifierMap);
+            })
+        .toArray(size -> Arrays.copyOf(entities, size));

Review Comment:
   The array creation logic is incorrect. `Arrays.copyOf(entities, size)` creates an array with the original elements but of the specified size, which may truncate or pad with nulls. Use `(E[]) Array.newInstance(entities.getClass().getComponentType(), size)` or a similar approach to create a properly typed empty array.
   ```suggestion
           .toArray(size -> (E[]) Array.newInstance(entities.getClass().getComponentType(), size));
   ```



-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [#7914] improvement(authz): Optimize the metadata array filtering code [gravitino]

Posted by "xunliu (via GitHub)" <gi...@apache.org>.
xunliu merged PR #7941:
URL: https://github.com/apache/gravitino/pull/7941


-- 
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: commits-unsubscribe@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org