Skip to content

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

[#13360] fix(server): Avoid exposing missing metalakes in authorization errors (#13361)

Qi Yue8ddebc0e33f
diff --git a/core/src/main/java/org/apache/gravitino/authorization/AuthorizationUtils.java b/core/src/main/java/org/apache/gravitino/authorization/AuthorizationUtils.java
index 42eb56ba0b..a275a2ef3f 100644
--- a/core/src/main/java/org/apache/gravitino/authorization/AuthorizationUtils.java
+++ b/core/src/main/java/org/apache/gravitino/authorization/AuthorizationUtils.java
@@ -128,12 +128,23 @@ public class AuthorizationUtils {
String metalake, String user, AuthorizationRequestContext requestContext) {
GravitinoAuthorizer authorizer = GravitinoEnv.getInstance().gravitinoAuthorizer();
if (authorizer != null && !authorizer.isMetalakeUser(metalake, requestContext)) {
- throw new ForbiddenException(
- "Current user %s doesn't exist in the metalake %s, you should add the user to the metalake first",
- user, metalake);
+ throw new ForbiddenException("%s", metalakeMembershipFailureMessage(metalake, user));
}
}
+ /**
+ * Returns a membership error that does not disclose whether the metalake exists.
+ *
+ * @param metalake The metalake name.
+ * @param user The current user name.
+ * @return A neutral membership error message.
+ */
+ public static String metalakeMembershipFailureMessage(String metalake, String user) {
+ return String.format(
+ "Current user %s is not a member of metalake %s, or the metalake does not exist",
+ user, metalake);
+ }
+
public static NameIdentifier ofRole(String metalake, String role) {
return NameIdentifier.of(
metalake, Entity.SYSTEM_CATALOG_RESERVED_NAME, Entity.ROLE_SCHEMA_NAME, role);
diff --git a/core/src/test/java/org/apache/gravitino/authorization/TestAuthorizationUtils.java b/core/src/test/java/org/apache/gravitino/authorization/TestAuthorizationUtils.java
index 0049a36ec2..ddaf6ae97f 100644
--- a/core/src/test/java/org/apache/gravitino/authorization/TestAuthorizationUtils.java
+++ b/core/src/test/java/org/apache/gravitino/authorization/TestAuthorizationUtils.java
@@ -40,6 +40,7 @@ import org.apache.gravitino.catalog.SchemaDispatcher;
import org.apache.gravitino.catalog.TableDispatcher;
import org.apache.gravitino.connector.BaseCatalog;
import org.apache.gravitino.connector.authorization.AuthorizationPlugin;
+import org.apache.gravitino.exceptions.ForbiddenException;
import org.apache.gravitino.exceptions.IllegalNameIdentifierException;
import org.apache.gravitino.exceptions.IllegalNamespaceException;
import org.apache.gravitino.meta.AuditInfo;
@@ -56,6 +57,24 @@ class TestAuthorizationUtils {
String metalake = "metalake";
+ @Test
+ void testCheckCurrentUserUsesNeutralMembershipMessage() {
+ try (MockedStatic<GravitinoEnv> envMock = Mockito.mockStatic(GravitinoEnv.class)) {
+ GravitinoEnv env = Mockito.mock(GravitinoEnv.class);
+ GravitinoAuthorizer authorizer = Mockito.mock(GravitinoAuthorizer.class);
+ envMock.when(GravitinoEnv::getInstance).thenReturn(env);
+ Mockito.when(env.gravitinoAuthorizer()).thenReturn(authorizer);
+
+ ForbiddenException exception =
+ Assertions.assertThrows(
+ ForbiddenException.class,
+ () -> AuthorizationUtils.checkCurrentUser(metalake, "tester"));
+ Assertions.assertEquals(
+ "Current user tester is not a member of metalake metalake, or the metalake does not exist",
+ exception.getMessage());
+ }
+ }
+
@Test
void testCreateNameIdentifier() {
NameIdentifier user = AuthorizationUtils.ofUser(metalake, "user");
diff --git a/server/src/main/java/org/apache/gravitino/server/web/filter/GravitinoInterceptionService.java b/server/src/main/java/org/apache/gravitino/server/web/filter/GravitinoInterceptionService.java
index 1fd90c407c..f4307bf90f 100644
--- a/server/src/main/java/org/apache/gravitino/server/web/filter/GravitinoInterceptionService.java
+++ b/server/src/main/java/org/apache/gravitino/server/web/filter/GravitinoInterceptionService.java
@@ -182,13 +182,7 @@ public class GravitinoInterceptionService implements InterceptionService {
authorizationMetalake = Optional.of(metalakeIdent.name());
Optional<Response> validationFailure =
validateCurrentUserAndActiveRoles(
- metalakeIdent,
- authorizationRequestContext,
- expressionAnnotation,
- metadataContext,
- method,
- expression,
- false);
+ metalakeIdent, authorizationRequestContext, method, expression);
if (validationFailure.isPresent()) {
return validationFailure.get();
}
@@ -234,11 +228,8 @@ public class GravitinoInterceptionService implements InterceptionService {
validateCurrentUserAndActiveRoles(
NameIdentifier.of(dynamicMetalake.get()),
authorizationRequestContext,
- expressionAnnotation,
- metadataContext,
method,
- expression,
- true);
+ expression);
if (validationFailure.isPresent()) {
return validationFailure.get();
}
@@ -298,28 +289,19 @@ public class GravitinoInterceptionService implements InterceptionService {
private Optional<Response> validateCurrentUserAndActiveRoles(
NameIdentifier metalakeIdent,
AuthorizationRequestContext authorizationRequestContext,
- AuthorizationExpression expressionAnnotation,
- Map<Entity.EntityType, NameIdentifier> metadataContext,
Method method,
- String expression,
- boolean dynamicMetalake) {
+ String expression) {
String currentUser = PrincipalUtils.getCurrentUserName();
try {
AuthorizationUtils.checkCurrentUser(
metalakeIdent.name(), currentUser, authorizationRequestContext);
} catch (NoSuchMetalakeException e) {
+ // A custom authorizer may report a missing metalake directly; JCasbin reports it as
+ // non-membership instead.
LOG.warn("Metalake {} does not exist when validating user {}", metalakeIdent, currentUser);
- if (dynamicMetalake) {
- return Optional.of(
- Utils.illegalArguments(
- String.format(
- "job.namespace must identify an existing metalake: %s", metalakeIdent.name()),
- e));
- }
- // Not a real authz denial — metalake is absent, not forbidden. Skip event dispatch;
- // HttpAuditFilter will emit a generic HttpRequestFailureEvent for this 403.
- return Optional.of(
- buildNoAuthResponse(expressionAnnotation, metadataContext, method, expression));
+ // A missing metalake is not an authorization denial, so skip the denial event. Return the
+ // same client-visible response as a failed membership check to avoid exposing existence.
+ return Optional.of(metalakeMembershipFailure(currentUser, metalakeIdent.name()));
} catch (ForbiddenException ex) {
LOG.warn(
"User validation failed - User: {}, Metalake: {}, Reason: {}",
@@ -328,7 +310,7 @@ public class GravitinoInterceptionService implements InterceptionService {
ex.getMessage(),
ex);
dispatchAuthzDenialEvent(currentUser, metalakeIdent, method.getName(), expression);
- return Optional.of(Utils.forbidden(ex.getMessage(), ex));
+ return Optional.of(metalakeMembershipFailure(currentUser, metalakeIdent.name()));
} catch (Exception ex) {
LOG.error(
"Unexpected error during user validation - User: {}, Metalake: {}",
@@ -363,6 +345,11 @@ public class GravitinoInterceptionService implements InterceptionService {
return Optional.empty();
}
+ private Response metalakeMembershipFailure(String user, String metalake) {
+ return Utils.forbidden(
+ AuthorizationUtils.metalakeMembershipFailureMessage(metalake, user), null);
+ }
+
private Response buildNoAuthResponse(
AuthorizationExpression expressionAnnotation,
Map<Entity.EntityType, NameIdentifier> metadataContext,
diff --git a/server/src/test/java/org/apache/gravitino/server/web/filter/TestGravitinoInterceptionService.java b/server/src/test/java/org/apache/gravitino/server/web/filter/TestGravitinoInterceptionService.java
index 0044367ab1..0e316ef41d 100644
--- a/server/src/test/java/org/apache/gravitino/server/web/filter/TestGravitinoInterceptionService.java
+++ b/server/src/test/java/org/apache/gravitino/server/web/filter/TestGravitinoInterceptionService.java
@@ -376,7 +376,7 @@ public class TestGravitinoInterceptionService {
.thenAnswer(invocation -> null);
GravitinoInterceptionService service = new GravitinoInterceptionService();
- Method testMethod = TestOperations.class.getMethods()[0];
+ Method testMethod = TestOperations.class.getMethod("testMethod", String.class);
MethodInterceptor interceptor = service.getMethodInterceptors(testMethod).get(0);
when(methodInvocation.getMethod()).thenReturn(testMethod);
when(methodInvocation.getArguments()).thenReturn(new Object[] {"testMetalake"});
@@ -541,52 +541,43 @@ public class TestGravitinoInterceptionService {
}
@Test
- public void testMetalakeNotExist() throws Throwable {
+ public void testMissingAndInaccessibleMetalakeHaveSameResponse() throws Throwable {
try (MockedStatic<PrincipalUtils> principalUtilsMocked = mockStatic(PrincipalUtils.class);
- MockedStatic<GravitinoAuthorizerProvider> authorizerMocked =
- mockStatic(GravitinoAuthorizerProvider.class);
MockedStatic<AuthorizationUtils> authorizationUtilsMocked =
mockStatic(AuthorizationUtils.class)) {
-
- principalUtilsMocked
- .when(PrincipalUtils::getCurrentPrincipal)
- .thenReturn(new UserPrincipal("tester"));
principalUtilsMocked.when(PrincipalUtils::getCurrentUserName).thenReturn("tester");
-
- MethodInvocation methodInvocation = mock(MethodInvocation.class);
- GravitinoAuthorizerProvider mockedProvider = mock(GravitinoAuthorizerProvider.class);
- authorizerMocked.when(GravitinoAuthorizerProvider::getInstance).thenReturn(mockedProvider);
- when(mockedProvider.getGravitinoAuthorizer()).thenReturn(new MockGravitinoAuthorizer());
-
- // Mock AuthorizationUtils.checkCurrentUser to throw NoSuchMetalakeException
authorizationUtilsMocked
.when(
() ->
AuthorizationUtils.checkCurrentUser(
ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any()))
- .thenThrow(new NoSuchMetalakeException("Metalake nonExistentMetalake does not exist"));
+ .thenThrow(new NoSuchMetalakeException("Metalake target does not exist"))
+ .thenThrow(new ForbiddenException("User is not a member of target"));
+ authorizationUtilsMocked
+ .when(() -> AuthorizationUtils.metalakeMembershipFailureMessage("target", "tester"))
+ .thenCallRealMethod();
- GravitinoInterceptionService gravitinoInterceptionService =
- new GravitinoInterceptionService();
- Class<TestOperations> testOperationsClass = TestOperations.class;
- Method[] methods = testOperationsClass.getMethods();
- Method testMethod = methods[0];
- List<MethodInterceptor> methodInterceptors =
- gravitinoInterceptionService.getMethodInterceptors(testMethod);
- MethodInterceptor methodInterceptor = methodInterceptors.get(0);
+ Method method = TestOperations.class.getMethods()[0];
+ MethodInvocation invocation = mock(MethodInvocation.class);
+ when(invocation.getMethod()).thenReturn(method);
+ when(invocation.getArguments()).thenReturn(new Object[] {"target"});
+ MethodInterceptor interceptor =
+ new GravitinoInterceptionService().getMethodInterceptors(method).get(0);
- // Test with non-existent metalake
- when(methodInvocation.getMethod()).thenReturn(testMethod);
- when(methodInvocation.getArguments()).thenReturn(new Object[] {"nonExistentMetalake"});
- Response response = (Response) methodInterceptor.invoke(methodInvocation);
+ Response missingResponse = (Response) interceptor.invoke(invocation);
+ Response inaccessibleResponse = (Response) interceptor.invoke(invocation);
- // Verify that a 403 Forbidden response is returned
- assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.getStatus());
- ErrorResponse errorResponse = (ErrorResponse) response.getEntity();
+ assertEquals(Response.Status.FORBIDDEN.getStatusCode(), missingResponse.getStatus());
+ assertEquals(missingResponse.getStatus(), inaccessibleResponse.getStatus());
+ ErrorResponse missingError = (ErrorResponse) missingResponse.getEntity();
+ ErrorResponse inaccessibleError = (ErrorResponse) inaccessibleResponse.getEntity();
+ assertEquals(missingError.getCode(), inaccessibleError.getCode());
+ assertEquals(missingError.getType(), inaccessibleError.getType());
assertEquals(
- "User 'tester' is not authorized to perform operation 'testMethod' on "
- + "metadata 'nonExistentMetalake'",
- errorResponse.getMessage());
+ "Current user tester is not a member of metalake target, or the metalake does not exist",
+ missingError.getMessage());
+ assertEquals(missingError.getMessage(), inaccessibleError.getMessage());
+ verify(invocation, never()).proceed();
}
}
diff --git a/server/src/test/java/org/apache/gravitino/server/web/filter/authorization/TestLineageAuthorizationExecutor.java b/server/src/test/java/org/apache/gravitino/server/web/filter/authorization/TestLineageAuthorizationExecutor.java
index 7f7a778229..ed74444575 100644
--- a/server/src/test/java/org/apache/gravitino/server/web/filter/authorization/TestLineageAuthorizationExecutor.java
+++ b/server/src/test/java/org/apache/gravitino/server/web/filter/authorization/TestLineageAuthorizationExecutor.java
@@ -113,7 +113,7 @@ class TestLineageAuthorizationExecutor {
}
@Test
- void testInterceptorRejectsUserOutsideDynamicMetalake() throws Throwable {
+ void testMissingAndInaccessibleDynamicMetalakeHaveSameResponse() throws Throwable {
MethodInvocation invocation = invocation(event());
try (MockedStatic<PrincipalUtils> principalUtils = mockStatic(PrincipalUtils.class);
@@ -126,38 +126,24 @@ class TestLineageAuthorizationExecutor {
() ->
AuthorizationUtils.checkCurrentUser(
eq(METALAKE), eq("tester"), any(AuthorizationRequestContext.class)))
+ .thenThrow(new NoSuchMetalakeException("Metalake does not exist"))
.thenThrow(new ForbiddenException("User tester is not a member"));
-
- Response response = (Response) lineageInterceptor().invoke(invocation);
-
- Assertions.assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.getStatus());
- verify(invocation, never()).proceed();
- }
- }
-
- @Test
- void testRejectNonexistentDynamicMetalakeAsBadRequest() throws Throwable {
- MethodInvocation invocation = invocation(event());
-
- try (MockedStatic<PrincipalUtils> principalUtils = mockStatic(PrincipalUtils.class);
- MockedStatic<AuthorizationUtils> authorizationUtils =
- mockStatic(AuthorizationUtils.class)) {
- principalUtils.when(PrincipalUtils::getCurrentPrincipal).thenReturn(principal());
- principalUtils.when(PrincipalUtils::getCurrentUserName).thenReturn("tester");
authorizationUtils
- .when(
- () ->
- AuthorizationUtils.checkCurrentUser(
- eq(METALAKE), eq("tester"), any(AuthorizationRequestContext.class)))
- .thenThrow(new NoSuchMetalakeException("Metalake does not exist"));
-
- Response response = (Response) lineageInterceptor().invoke(invocation);
-
- Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus());
- ErrorResponse errorResponse = (ErrorResponse) response.getEntity();
- Assertions.assertTrue(
- errorResponse.getMessage().contains("job.namespace"),
- "The response should identify the invalid field");
+ .when(() -> AuthorizationUtils.metalakeMembershipFailureMessage("metalake", "tester"))
+ .thenCallRealMethod();
+
+ MethodInterceptor interceptor = lineageInterceptor();
+ Response missingResponse = (Response) interceptor.invoke(invocation);
+ Response inaccessibleResponse = (Response) interceptor.invoke(invocation);
+
+ Assertions.assertEquals(
+ Response.Status.FORBIDDEN.getStatusCode(), missingResponse.getStatus());
+ Assertions.assertEquals(missingResponse.getStatus(), inaccessibleResponse.getStatus());
+ ErrorResponse missingError = (ErrorResponse) missingResponse.getEntity();
+ ErrorResponse inaccessibleError = (ErrorResponse) inaccessibleResponse.getEntity();
+ Assertions.assertEquals(missingError.getCode(), inaccessibleError.getCode());
+ Assertions.assertEquals(missingError.getType(), inaccessibleError.getType());
+ Assertions.assertEquals(missingError.getMessage(), inaccessibleError.getMessage());
verify(invocation, never()).proceed();
}
}