Skip to content

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

[#13695] fix(authorization): Evict all name-ID mappings of a dropped metalake (#13834)

Honggeun Jie88558e1bfdc
diff --git a/core/src/main/java/org/apache/gravitino/hook/MetalakeHookDispatcher.java b/core/src/main/java/org/apache/gravitino/hook/MetalakeHookDispatcher.java
index dfdaf653d9..98e3686524 100644
--- a/core/src/main/java/org/apache/gravitino/hook/MetalakeHookDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/hook/MetalakeHookDispatcher.java
@@ -116,7 +116,11 @@ public class MetalakeHookDispatcher implements MetalakeDispatcher {
throws NonEmptyEntityException, MetalakeInUseException {
// Child catalog/schema/fileset write-through secrets are cleaned when MetalakeManager
// force-drops catalogs via CatalogManager.dropCatalog → FilesetCatalogOperations.dropSchema.
- return dispatcher.dropMetalake(ident, force);
+ boolean dropped = dispatcher.dropMetalake(ident, force);
+ // Evict even when this node dropped nothing: a concurrent drop on a peer returns false here,
+ // and the cached mappings are always reloadable.
+ AuthorizationUtils.notifyEntityNameIdMappingChange(ident, Entity.EntityType.METALAKE);
+ return dropped;
}
@Override
@@ -133,7 +137,11 @@ public class MetalakeHookDispatcher implements MetalakeDispatcher {
public boolean dropMetalake(NameIdentifier ident) {
// For metalake, we don't clear all the privileges of catalog authorization plugin.
// we just remove metalake.
- return dispatcher.dropMetalake(ident);
+ boolean dropped = dispatcher.dropMetalake(ident);
+ // Evict even when this node dropped nothing: a concurrent drop on a peer returns false here,
+ // and the cached mappings are always reloadable.
+ AuthorizationUtils.notifyEntityNameIdMappingChange(ident, Entity.EntityType.METALAKE);
+ return dropped;
}
@Override
diff --git a/core/src/test/java/org/apache/gravitino/hook/TestMetalakeHookDispatcher.java b/core/src/test/java/org/apache/gravitino/hook/TestMetalakeHookDispatcher.java
index 814967c234..474326cb0e 100644
--- a/core/src/test/java/org/apache/gravitino/hook/TestMetalakeHookDispatcher.java
+++ b/core/src/test/java/org/apache/gravitino/hook/TestMetalakeHookDispatcher.java
@@ -22,18 +22,21 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.Entity;
import org.apache.gravitino.EntityFieldLimits;
import org.apache.gravitino.GravitinoEnv;
import org.apache.gravitino.Metalake;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.UserPrincipal;
import org.apache.gravitino.authorization.AccessControlDispatcher;
+import org.apache.gravitino.authorization.GravitinoAuthorizer;
import org.apache.gravitino.authorization.OwnerDispatcher;
import org.apache.gravitino.exceptions.UserAlreadyExistsException;
import org.apache.gravitino.metalake.MetalakeDispatcher;
@@ -49,18 +52,22 @@ public class TestMetalakeHookDispatcher {
private MetalakeDispatcher mockDispatcher;
private OwnerDispatcher mockOwnerDispatcher;
private AccessControlDispatcher mockAccessControlDispatcher;
+ private GravitinoAuthorizer mockAuthorizer;
// Save the originals before each test and restore them in tearDown so we do not leak null
// state into the GravitinoEnv singleton across tests.
private OwnerDispatcher savedOwnerDispatcher;
private AccessControlDispatcher savedAccessControlDispatcher;
+ private GravitinoAuthorizer savedAuthorizer;
@BeforeEach
public void setUp() throws IllegalAccessException {
mockDispatcher = mock(MetalakeDispatcher.class);
mockOwnerDispatcher = mock(OwnerDispatcher.class);
mockAccessControlDispatcher = mock(AccessControlDispatcher.class);
+ mockAuthorizer = mock(GravitinoAuthorizer.class);
savedOwnerDispatcher = GravitinoEnv.getInstance().internalOwnerDispatcher();
savedAccessControlDispatcher = GravitinoEnv.getInstance().internalAccessControlDispatcher();
+ savedAuthorizer = GravitinoEnv.getInstance().gravitinoAuthorizer();
FieldUtils.writeField(
GravitinoEnv.getInstance(), "internalOwnerDispatcher", mockOwnerDispatcher, true);
FieldUtils.writeField(
@@ -68,6 +75,7 @@ public class TestMetalakeHookDispatcher {
"internalAccessControlDispatcher",
mockAccessControlDispatcher,
true);
+ FieldUtils.writeField(GravitinoEnv.getInstance(), "gravitinoAuthorizer", mockAuthorizer, true);
hookDispatcher = new MetalakeHookDispatcher(mockDispatcher);
}
@@ -80,6 +88,7 @@ public class TestMetalakeHookDispatcher {
"internalAccessControlDispatcher",
savedAccessControlDispatcher,
true);
+ FieldUtils.writeField(GravitinoEnv.getInstance(), "gravitinoAuthorizer", savedAuthorizer, true);
}
@Test
@@ -142,6 +151,41 @@ public class TestMetalakeHookDispatcher {
verify(mockOwnerDispatcher, never()).setOwner(any(), any(), any(), any());
}
+ @Test
+ public void testDropMetalakeInvalidatesNameIdMappingsEvenWhenNothingDropped() {
+ // A concurrent drop on a peer makes this node's drop return false; it must still evict.
+ NameIdentifier ident = NameIdentifier.of("test_metalake");
+ when(mockDispatcher.dropMetalake(ident, true)).thenReturn(false);
+ when(mockDispatcher.dropMetalake(ident)).thenReturn(false);
+
+ Assertions.assertFalse(hookDispatcher.dropMetalake(ident, true));
+ Assertions.assertFalse(hookDispatcher.dropMetalake(ident));
+
+ verify(mockAuthorizer, times(2))
+ .handleEntityNameIdMappingChange("test_metalake", ident, Entity.EntityType.METALAKE);
+ }
+
+ @Test
+ public void testDropMetalakeWithoutForceInvalidatesNameIdMappings() {
+ NameIdentifier ident = NameIdentifier.of("test_metalake");
+ when(mockDispatcher.dropMetalake(ident)).thenReturn(true);
+
+ Assertions.assertTrue(hookDispatcher.dropMetalake(ident));
+
+ verify(mockAuthorizer)
+ .handleEntityNameIdMappingChange("test_metalake", ident, Entity.EntityType.METALAKE);
+ }
+
+ @Test
+ public void testDropMetalakeFailureDoesNotInvalidateNameIdMappings() {
+ NameIdentifier ident = NameIdentifier.of("test_metalake");
+ doThrow(new RuntimeException("Drop failed")).when(mockDispatcher).dropMetalake(ident, true);
+
+ Assertions.assertThrows(RuntimeException.class, () -> hookDispatcher.dropMetalake(ident, true));
+
+ verify(mockAuthorizer, never()).handleEntityNameIdMappingChange(any(), any(), any());
+ }
+
@Test
public void testCreateMetalakeRejectsTooLongCreatorName() throws Exception {
NameIdentifier ident = NameIdentifier.of("test_metalake");
diff --git a/server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizationCacheKeys.java b/server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizationCacheKeys.java
index 81770b027a..ed2721e188 100644
--- a/server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizationCacheKeys.java
+++ b/server-common/src/main/java/org/apache/gravitino/server/authorization/jcasbin/JcasbinAuthorizationCacheKeys.java
@@ -39,7 +39,8 @@ final class JcasbinAuthorizationCacheKeys {
String[] names = metadataObject.fullName().split("\\.");
switch (metadataObject.type()) {
case METALAKE:
- parts.add(MetadataObject.Type.METALAKE.name());
+ // No type token: the metalake key must stay a strict prefix of every key in the metalake,
+ // so a metalake drop can evict them all with one prefix invalidation.
break;
case CATALOG:
parts.add(MetadataObject.Type.CATALOG.name());
diff --git a/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizationCacheKeys.java b/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizationCacheKeys.java
index d6c0d2efc0..16f70937e6 100644
--- a/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizationCacheKeys.java
+++ b/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizationCacheKeys.java
@@ -33,8 +33,7 @@ public class TestJcasbinAuthorizationCacheKeys {
MetadataObject metalake =
MetadataObjects.of(Collections.singletonList("ml1"), MetadataObject.Type.METALAKE);
Assertions.assertEquals(
- key("ml1", "METALAKE", ""),
- JcasbinAuthorizationCacheKeys.metadataIdCacheKey("ml1", metalake));
+ key("ml1", ""), JcasbinAuthorizationCacheKeys.metadataIdCacheKey("ml1", metalake));
MetadataObject catalog =
MetadataObjects.of(Collections.singletonList("cat1"), MetadataObject.Type.CATALOG);
@@ -120,6 +119,35 @@ public class TestJcasbinAuthorizationCacheKeys {
JcasbinAuthorizationCacheKeys.hasNestedMetadataObjects(MetadataObject.Type.SEMANTIC_MODEL));
}
+ @Test
+ void testMetalakeKeyIsPrefixOfEveryChildKey() {
+ String metalakeKey =
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(
+ "ml1",
+ MetadataObjects.of(Collections.singletonList("ml1"), MetadataObject.Type.METALAKE));
+ String catalogKey =
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(
+ "ml1",
+ MetadataObjects.of(Collections.singletonList("cat1"), MetadataObject.Type.CATALOG));
+ String tagKey =
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(
+ "ml1", MetadataObjects.of(Collections.singletonList("tag1"), MetadataObject.Type.TAG));
+ String policyKey =
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(
+ "ml1",
+ MetadataObjects.of(Collections.singletonList("policy1"), MetadataObject.Type.POLICY));
+ String otherMetalakeKey =
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(
+ "ml10",
+ MetadataObjects.of(Collections.singletonList("cat1"), MetadataObject.Type.CATALOG));
+
+ Assertions.assertTrue(catalogKey.startsWith(metalakeKey));
+ Assertions.assertTrue(tagKey.startsWith(metalakeKey));
+ Assertions.assertTrue(policyKey.startsWith(metalakeKey));
+ // The trailing separator keeps the metalake name boundary.
+ Assertions.assertFalse(otherMetalakeKey.startsWith(metalakeKey));
+ }
+
@Test
void testPrincipalRoleKeysAreTyped() {
Assertions.assertEquals(
diff --git a/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizer.java b/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizer.java
index 0912f4b577..67373a569a 100644
--- a/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizer.java
+++ b/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinAuthorizer.java
@@ -56,6 +56,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@@ -75,6 +76,7 @@ import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
import java.util.stream.Collectors;
+import javax.annotation.Nullable;
import org.apache.gravitino.Config;
import org.apache.gravitino.Configs;
import org.apache.gravitino.Entity;
@@ -92,7 +94,9 @@ import org.apache.gravitino.auth.AuthConstants;
import org.apache.gravitino.authorization.AccessControlDispatcher;
import org.apache.gravitino.authorization.AuthorizationRequestContext;
import org.apache.gravitino.authorization.Privilege;
+import org.apache.gravitino.authorization.Privileges;
import org.apache.gravitino.authorization.SecurableObject;
+import org.apache.gravitino.authorization.SecurableObjects;
import org.apache.gravitino.cache.GravitinoCache;
import org.apache.gravitino.catalog.CatalogManager;
import org.apache.gravitino.catalog.SemanticModelDispatcher;
@@ -117,6 +121,7 @@ import org.apache.gravitino.server.authorization.MetadataAuthzHelper;
import org.apache.gravitino.server.authorization.MetadataIdConverter;
import org.apache.gravitino.server.authorization.expression.AuthorizationExpressionConstants;
import org.apache.gravitino.server.authorization.expression.AuthorizationExpressionEvaluator;
+import org.apache.gravitino.storage.relational.EntityChangeLogNameIdentifierCodec;
import org.apache.gravitino.storage.relational.mapper.EntityChangeLogMapper;
import org.apache.gravitino.storage.relational.mapper.GroupMetaMapper;
import org.apache.gravitino.storage.relational.mapper.OwnerMetaMapper;
@@ -129,6 +134,8 @@ import org.apache.gravitino.storage.relational.po.auth.GroupUpdatedAt;
import org.apache.gravitino.storage.relational.po.auth.OwnerInfo;
import org.apache.gravitino.storage.relational.po.auth.RoleUpdatedAt;
import org.apache.gravitino.storage.relational.po.auth.UserUpdatedAt;
+import org.apache.gravitino.storage.relational.po.cache.EntityChangeRecord;
+import org.apache.gravitino.storage.relational.po.cache.OperateType;
import org.apache.gravitino.storage.relational.service.OwnerMetaService;
import org.apache.gravitino.storage.relational.utils.POConverters;
import org.apache.gravitino.storage.relational.utils.SessionUtils;
@@ -144,6 +151,7 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.MockedStatic;
@@ -4173,6 +4181,112 @@ public class TestJcasbinAuthorizer {
}
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testMetalakeDropEvictsEveryMetadataTypeAndKeepsOtherMetalake(boolean peerReplay)
+ throws Exception {
+ // Use a dedicated metalake so the shared MetadataIdConverter stubs do not leak into other
+ // tests. The other metalake name extends the dropped one to check the name boundary.
+ String metalakeName = peerReplay ? "peer_drop_ml" : "local_drop_ml";
+ String otherMetalake = metalakeName + "_other";
+ GravitinoCache<String, Long> metadataIdCache = getMetadataIdCache(jcasbinAuthorizer);
+ JcasbinAuthorizationLookups lookups =
+ new JcasbinAuthorizationLookups(metadataIdCache, getOwnerRelCache(jcasbinAuthorizer));
+ MetadataObject.Type[] types = MetadataObject.Type.values();
+ for (int i = 0; i < types.length; i++) {
+ MetadataObject object = metadataObjectOfType(metalakeName, types[i]);
+ MetadataObject otherObject = metadataObjectOfType(otherMetalake, types[i]);
+ metadataIdConverterMockedStatic
+ .when(() -> MetadataIdConverter.getID(object, metalakeName))
+ .thenReturn(Optional.of(1000L + i), Optional.of(2000L + i));
+ metadataIdConverterMockedStatic
+ .when(() -> MetadataIdConverter.getID(otherObject, otherMetalake))
+ .thenReturn(Optional.of(3000L + i));
+
+ assertEquals(Optional.of(1000L + i), resolve(lookups, object, metalakeName));
+ assertEquals(Optional.of(3000L + i), resolve(lookups, otherObject, otherMetalake));
+ assertEquals(
+ Optional.of(1000L + i),
+ metadataIdCache.getIfPresent(
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(metalakeName, object)),
+ types[i].name());
+ }
+
+ metalakeDropInvalidation(metalakeName, peerReplay).run();
+
+ for (int i = 0; i < types.length; i++) {
+ MetadataObject object = metadataObjectOfType(metalakeName, types[i]);
+ MetadataObject otherObject = metadataObjectOfType(otherMetalake, types[i]);
+ assertEquals(
+ Optional.empty(),
+ metadataIdCache.getIfPresent(
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(metalakeName, object)),
+ types[i].name());
+ assertEquals(
+ Optional.of(3000L + i),
+ metadataIdCache.getIfPresent(
+ JcasbinAuthorizationCacheKeys.metadataIdCacheKey(otherMetalake, otherObject)),
+ types[i].name());
+ // The recreated metalake resolves same-named objects to their new IDs.
+ assertEquals(Optional.of(2000L + i), resolve(lookups, object, metalakeName));
+ }
+ }
+
+ @ParameterizedTest
+ @CsvSource({"false, false", "true, false", "false, true", "true, true"})
+ public void testRecreatedMetalakeDoesNotInheritOldOwnershipOrPermissions(
+ boolean peerReplay, boolean viaGroup) throws Exception {
+ // Use a dedicated metalake so the shared mapper and MetadataIdConverter stubs do not leak into
+ // other tests.
+ String metalakeName =
+ (peerReplay ? "peer" : "local") + (viaGroup ? "_group" : "") + "_recreate_ml";
+ String roleName = "recreated_role";
+ // With viaGroup, the role is bound to the user's group instead of to the user directly.
+ Long oldGroupId = viaGroup ? 7005L : null;
+ Long newGroupId = viaGroup ? 8005L : null;
+ long oldCatalogId = 7001L;
+ long oldUserId = 7002L;
+ long oldRoleId = 7003L;
+ long newCatalogId = 8001L;
+ long newUserId = 8002L;
+ long newRoleId = 8003L;
+ long otherCatalogId = 8004L;
+ long otherOwnerId = 9999L;
+ Principal principal = setCurrentPrincipalWithGroup(viaGroup ? GROUP_NAME : null);
+ MetadataObject catalog = MetadataObjects.parse("cat", MetadataObject.Type.CATALOG);
+ MetadataObject otherCatalog = MetadataObjects.parse("other_cat", MetadataObject.Type.CATALOG);
+ metadataIdConverterMockedStatic
+ .when(() -> MetadataIdConverter.getID(catalog, metalakeName))
+ .thenReturn(Optional.of(oldCatalogId), Optional.of(newCatalogId));
+ metadataIdConverterMockedStatic
+ .when(() -> MetadataIdConverter.getID(otherCatalog, metalakeName))
+ .thenReturn(Optional.of(otherCatalogId));
+ when(ownerMetaMapper.selectOwnerByMetadataObjectIdAndType(eq(oldCatalogId), eq("CATALOG")))
+ .thenReturn(new OwnerInfo(oldUserId, "USER"));
+ when(ownerMetaMapper.selectOwnerByMetadataObjectIdAndType(eq(newCatalogId), eq("CATALOG")))
+ .thenReturn(new OwnerInfo(otherOwnerId, "USER"));
+ when(ownerMetaMapper.selectOwnerByMetadataObjectIdAndType(eq(otherCatalogId), eq("CATALOG")))
+ .thenReturn(new OwnerInfo(newUserId, "USER"));
+
+ // Old metalake: the user owns "cat" and holds a role that grants USE_CATALOG on it.
+ mockMetalakeUserWithRole(metalakeName, oldUserId, oldGroupId, oldRoleId, roleName, "cat");
+ assertTrue(authorizeUseCatalog(principal, metalakeName, catalog));
+ assertTrue(isOwner(principal, metalakeName, catalog));
+
+ // Drop the metalake, then recreate the same user, group, role and catalog names with new IDs.
+ // The new role grants USE_CATALOG on "other_cat" instead, and someone else owns "cat".
+ metalakeDropInvalidation(metalakeName, peerReplay).run();
+ mockedRoleVersions.remove(oldRoleId);
+ mockMetalakeUserWithRole(metalakeName, newUserId, newGroupId, newRoleId, roleName, "other_cat");
+
+ // The old ownership entry is retained, but the ID checks keep it from granting access.
+ assertTrue(getOwnerRelCache(jcasbinAuthorizer).getIfPresent(oldCatalogId).isPresent());
+ assertFalse(authorizeUseCatalog(principal, metalakeName, catalog));
+ assertFalse(isOwner(principal, metalakeName, catalog));
+ assertTrue(authorizeUseCatalog(principal, metalakeName, otherCatalog));
+ assertTrue(isOwner(principal, metalakeName, otherCatalog));
+ }
+
@Test
public void testSemanticModelCacheKeepsCaseSensitiveSchemasDistinct() throws Exception {
CatalogManager catalogs = mock(CatalogManager.class);
@@ -4936,4 +5050,108 @@ public class TestJcasbinAuthorizer {
field.setAccessible(true);
return (Enforcer) field.get(authorizer);
}
+
+ private Runnable metalakeDropInvalidation(String metalakeName, boolean peerReplay)
+ throws Exception {
+ if (!peerReplay) {
+ return () ->
+ jcasbinAuthorizer.handleEntityNameIdMappingChange(
+ metalakeName, NameIdentifier.of(metalakeName), Entity.EntityType.METALAKE);
+ }
+ JcasbinChangeListener listener =
+ new JcasbinChangeListener(
+ getMetadataIdCache(jcasbinAuthorizer), getOwnerRelCache(jcasbinAuthorizer), 1);
+ EntityChangeRecord metalakeDrop =
+ new EntityChangeRecord(
+ 1L,
+ metalakeName,
+ MetadataObject.Type.METALAKE.name(),
+ EntityChangeLogNameIdentifierCodec.encode(NameIdentifier.of(metalakeName)),
+ OperateType.DROP,
+ 0L);
+ return () -> listener.onEntityChange(ImmutableList.of(metalakeDrop));
+ }
+
+ private static MetadataObject metadataObjectOfType(String metalake, MetadataObject.Type type) {
+ String leaf = type.name().toLowerCase(Locale.ROOT);
+ switch (type) {
+ case METALAKE:
+ return MetadataObjects.parse(metalake, type);
+ case CATALOG:
+ return MetadataObjects.parse("catalog", type);
+ case SCHEMA:
+ return MetadataObjects.parse("catalog.schema", type);
+ case COLUMN:
+ return MetadataObjects.parse("catalog.schema.table.column", type);
+ case FILESET:
+ case TABLE:
+ case VIEW:
+ case SEMANTIC_MODEL:
+ case TOPIC:
+ case MODEL:
+ case FUNCTION:
+ return MetadataObjects.parse("catalog.schema." + leaf, type);
+ default:
+ return MetadataObjects.parse(leaf, type);
+ }
+ }
+
+ /**
+ * Mocks a metalake user, with the given ID, that holds one role granting USE_CATALOG. When {@code
+ * groupId} is set, the role is bound to the {@link #GROUP_NAME} group with that ID instead of to
+ * the user.
+ */
+ private static void mockMetalakeUserWithRole(
+ String metalake,
+ long userId,
+ @Nullable Long groupId,
+ long roleId,
+ String roleName,
+ String catalogName)
+ throws IOException {
+ RoleEntity role =
+ RoleEntity.builder()
+ .withNamespace(NamespaceUtil.ofRole(metalake))
+ .withId(roleId)
+ .withName(roleName)
+ .withAuditInfo(AuditInfo.EMPTY)
+ .withSecurableObjects(
+ ImmutableList.of(
+ SecurableObjects.ofCatalog(
+ catalogName, ImmutableList.of(Privileges.UseCatalog.allow()))))
+ .build();
+ when(entityStore.get(
+ eq(NameIdentifierUtil.ofRole(metalake, roleName)),
+ eq(Entity.EntityType.ROLE),
+ eq(RoleEntity.class)))
+ .thenReturn(role);
+ mockedRoleVersions.put(roleId, new RoleUpdatedAt(roleId, roleName, nextRoleVersion()));
+ List<RolePO> rolePOs = ImmutableList.of(buildRolePO(roleId, roleName));
+ if (groupId == null) {
+ when(roleMetaMapper.listRolesByUserId(eq(userId))).thenReturn(rolePOs);
+ } else {
+ when(roleMetaMapper.listRolesByUserId(eq(userId))).thenReturn(ImmutableList.of());
+ when(groupMetaMapper.getGroupUpdatedAt(eq(metalake), eq(GROUP_NAME)))
+ .thenReturn(new GroupUpdatedAt(groupId, groupVersionCounter.incrementAndGet()));
+ when(roleMetaMapper.listRolesByGroupId(eq(groupId))).thenReturn(rolePOs);
+ }
+ when(userMetaMapper.getUserUpdatedAt(eq(metalake), eq(USERNAME)))
+ .thenReturn(new UserUpdatedAt(userId, nextUserVersion()));
+ }
+
+ private boolean authorizeUseCatalog(
+ Principal principal, String metalake, MetadataObject catalog) {
+ return jcasbinAuthorizer.authorize(
+ principal, metalake, catalog, USE_CATALOG, new AuthorizationRequestContext());
+ }
+
+ private boolean isOwner(Principal principal, String metalake, MetadataObject metadataObject) {
+ return jcasbinAuthorizer.isOwner(
+ principal, metalake, metadataObject, new AuthorizationRequestContext());
+ }
+
+ private static Optional<Long> resolve(
+ JcasbinAuthorizationLookups lookups, MetadataObject metadataObject, String metalake) {
+ return lookups.resolveMetadataId(metadataObject, metalake, new AuthorizationRequestContext());
+ }
}
diff --git a/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinChangePoller.java b/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinChangePoller.java
index fe3207203d..3c63bd37f8 100644
--- a/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinChangePoller.java
+++ b/server-common/src/test/java/org/apache/gravitino/server/authorization/jcasbin/TestJcasbinChangePoller.java
@@ -94,8 +94,7 @@ public class TestJcasbinChangePoller {
JcasbinChangeListener.metadataObjectFromChangeLog(
"ml1", "ml1", MetadataObject.Type.METALAKE);
Assertions.assertEquals(
- key("ml1", "METALAKE", ""),
- JcasbinAuthorizationCacheKeys.metadataIdCacheKey("ml1", metalake));
+ key("ml1", ""), JcasbinAuthorizationCacheKeys.metadataIdCacheKey("ml1", metalake));
}
@Test
@@ -133,6 +132,23 @@ public class TestJcasbinChangePoller {
Assertions.assertEquals(List.of(), metadataIdCache.invalidatedKeys);
}
+ @Test
+ void testMetalakeDropInvalidatesEveryKeyOfTheMetalake() {
+ RecordingCache<String, Long> metadataIdCache = new RecordingCache<>();
+ RecordingCache<Long, Optional<OwnerInfo>> ownerRelCache = new RecordingCache<>();
+
+ JcasbinChangeListener poller = new JcasbinChangeListener(metadataIdCache, ownerRelCache, 1);
+ poller.onEntityChange(
+ List.of(
+ change(1L, MetadataObject.Type.CATALOG, "ml1.cat1"),
+ change(2L, MetadataObject.Type.METALAKE, "ml1"),
+ change(3L, MetadataObject.Type.VIEW, "ml1.cat1.sch1.view1")));
+
+ // The metalake scope prefix subsumes the catalog prefix and the view key.
+ Assertions.assertEquals(List.of(key("ml1", "")), metadataIdCache.invalidatedPrefixes);
+ Assertions.assertEquals(List.of(), metadataIdCache.invalidatedKeys);
+ }
+
@Test
void testVirtualNamespaceTypesInvalidateLookupKeys() {
RecordingCache<String, Long> metadataIdCache = new RecordingCache<>();