Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#12622] improvement(server): apply the list authorization short-circuit to every list shape (#12623)

Qi Yud54358e1bc63
diff --git a/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java b/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java
index fc7db7b615..cd6178258f 100644
--- a/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java
+++ b/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java
@@ -206,32 +206,6 @@ public class MetadataAuthzHelper {
String expression,
Entity.EntityType entityType,
NameIdentifier[] nameIdentifiers) {
- if (enableAuthorization() && nameIdentifiers.length > 0) {
- String principalName = PrincipalUtils.getCurrentPrincipal().getName();
- if (allVisibleViaParentScope(metalake, expression, entityType, nameIdentifiers)) {
- // A privilege granted at a parent scope (metalake/catalog/schema) makes every object in
- // the list visible, and no object-level deny exists, so the per-object authorization loop
- // is skipped entirely. See AuthorizationExpressionConstants.*_LIST_PARENT_SCOPE_*.
- LOG.debug(
- "List authorization short-circuit HIT for principal {}, entity type {} under metalake "
- + "{}: all {} listed object(s) are visible via a parent-scope grant; skipping the "
- + "per-object authorization loop.",
- principalName,
- entityType,
- metalake,
- nameIdentifiers.length);
- return nameIdentifiers;
- }
- LOG.debug(
- "List authorization short-circuit MISS for principal {}, entity type {} under metalake "
- + "{} ({} object(s)); falling back to the per-object authorization loop.",
- principalName,
- entityType,
- metalake,
- nameIdentifiers.length);
- }
- preloadToCache(entityType, nameIdentifiers);
- preloadOwner(entityType, nameIdentifiers);
return filterByExpression(metalake, expression, entityType, nameIdentifiers, e -> e);
}
@@ -356,6 +330,39 @@ public class MetadataAuthzHelper {
Entity.EntityType entityType,
E[] entities,
Function<E, NameIdentifier> toNameIdentifier) {
+ // Every list endpoint funnels through here, whichever shape it holds its results in, so the
+ // short-circuit and the preloads live at this one point. Keeping them in the NameIdentifier[]
+ // overload alone let the verbose catalog listing, which carries Catalog objects, run the
+ // per-object loop over every catalog in the metalake.
+ NameIdentifier[] nameIdentifiers =
+ Arrays.stream(entities).map(toNameIdentifier).toArray(NameIdentifier[]::new);
+ if (enableAuthorization() && nameIdentifiers.length > 0) {
+ String principalName = PrincipalUtils.getCurrentPrincipal().getName();
+ if (allVisibleViaParentScope(metalake, expression, entityType, nameIdentifiers)) {
+ // A privilege granted at a parent scope (metalake/catalog/schema) makes every object in
+ // the list visible, and no object-level deny exists, so the per-object authorization loop
+ // is skipped entirely. See AuthorizationExpressionConstants.*_LIST_PARENT_SCOPE_*.
+ LOG.debug(
+ "List authorization short-circuit HIT for principal {}, entity type {} under metalake "
+ + "{}: all {} listed object(s) are visible via a parent-scope grant; skipping the "
+ + "per-object authorization loop.",
+ principalName,
+ entityType,
+ metalake,
+ nameIdentifiers.length);
+ return entities;
+ }
+ LOG.debug(
+ "List authorization short-circuit MISS for principal {}, entity type {} under metalake "
+ + "{} ({} object(s)); falling back to the per-object authorization loop.",
+ principalName,
+ entityType,
+ metalake,
+ nameIdentifiers.length);
+ }
+ preloadToCache(entityType, nameIdentifiers);
+ preloadOwner(entityType, nameIdentifiers);
+
GravitinoAuthorizer authorizer =
GravitinoAuthorizerProvider.getInstance().getGravitinoAuthorizer();
AuthorizationRequestContext authorizationRequestContext = new AuthorizationRequestContext();
diff --git a/server-common/src/test/java/org/apache/gravitino/server/authorization/TestMetadataAuthzHelper.java b/server-common/src/test/java/org/apache/gravitino/server/authorization/TestMetadataAuthzHelper.java
index a5019085cb..15783d818a 100644
--- a/server-common/src/test/java/org/apache/gravitino/server/authorization/TestMetadataAuthzHelper.java
+++ b/server-common/src/test/java/org/apache/gravitino/server/authorization/TestMetadataAuthzHelper.java
@@ -23,6 +23,8 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
@@ -321,6 +323,59 @@ public class TestMetadataAuthzHelper {
}
}
+ /**
+ * The verbose catalog listing hands this helper {@code Catalog} objects rather than identifiers,
+ * so it goes through the generic overload. That overload used to skip the short-circuit, which
+ * left every catalog in the metalake on the per-object path.
+ *
+ * <p>Proving which path ran needs care. Comparing the returned elements does not work, because
+ * the metalake-scope grant satisfies the per-object expression too and every catalog comes back
+ * either way. Counting authorizer calls does not work either: the per-request cache in {@link
+ * org.apache.gravitino.authorization.AuthorizationRequestContext} collapses the repeated
+ * metalake-scope check, so the count is the same for three catalogs and for thirty.
+ *
+ * <p>What does separate them is the array itself. The short-circuit hands back the caller's own
+ * array untouched, while the per-object path collects survivors into a new one, so identity says
+ * which branch produced the result.
+ */
+ @Test
+ public void testListShortCircuitAppliesToNonIdentifierResults() {
+ makeCompletableFutureUseCurrentThread();
+ try (MockedStatic<PrincipalUtils> principalUtilsMocked = mockStatic(PrincipalUtils.class);
+ MockedStatic<GravitinoAuthorizerProvider> mockStatic =
+ mockStatic(GravitinoAuthorizerProvider.class)) {
+ principalUtilsMocked
+ .when(PrincipalUtils::getCurrentPrincipal)
+ .thenReturn(new UserPrincipal("tester"));
+ principalUtilsMocked.when(() -> PrincipalUtils.doAs(any(), any())).thenCallRealMethod();
+ GravitinoAuthorizerProvider mockedProvider = mock(GravitinoAuthorizerProvider.class);
+ mockStatic.when(GravitinoAuthorizerProvider::getInstance).thenReturn(mockedProvider);
+ GravitinoAuthorizer authorizer =
+ mockParentGrantAuthorizer(MetadataObject.Type.METALAKE, Privilege.Name.USE_CATALOG);
+ when(mockedProvider.getGravitinoAuthorizer()).thenReturn(authorizer);
+
+ // Stands in for the Catalog objects the verbose listing carries: anything that is not a
+ // NameIdentifier and needs a mapper to become one.
+ String[] catalogNames = new String[] {"c1", "c2", "c3"};
+
+ String[] filtered =
+ MetadataAuthzHelper.filterByExpression(
+ "testMetalake",
+ AuthorizationExpressionConstants.LOAD_CATALOG_AUTHORIZATION_EXPRESSION,
+ Entity.EntityType.CATALOG,
+ catalogNames,
+ name -> NameIdentifierUtil.ofCatalog("testMetalake", name));
+
+ Assertions.assertSame(
+ catalogNames,
+ filtered,
+ "The parent-scope short-circuit must return the caller's array as is; a new array means "
+ + "the per-object authorization loop ran for every catalog");
+ // Only the short-circuit asks this, and it asks once, after the parent grant is confirmed.
+ verify(authorizer, times(1)).hasDenyPolicy(any(), eq("testMetalake"), anySet(), any());
+ }
+ }
+
/**
* Builds an authorizer that grants {@code SELECT_TABLE} at the schema scope, but only for the
* schema whose simple name equals {@code grantedSchema}. Used to prove the short-circuit never