Skip to content

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

[#4699] fix(server): The error response of addUser should meet the REST specification (#4704)

roryqi79742f364f9f
diff --git a/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java b/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java
index 18576e2775..7414f4c4e8 100644
--- a/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java
+++ b/clients/client-java/src/main/java/org/apache/gravitino/client/ErrorHandlers.java
@@ -564,6 +564,9 @@ public class ErrorHandlers {
case ErrorConstants.ALREADY_EXISTS_CODE:
throw new UserAlreadyExistsException(errorMessage);
+ case ErrorConstants.UNSUPPORTED_OPERATION_CODE:
+ throw new UnsupportedOperationException(errorMessage);
+
case ErrorConstants.INTERNAL_ERROR_CODE:
throw new RuntimeException(errorMessage);
@@ -599,6 +602,9 @@ public class ErrorHandlers {
case ErrorConstants.ALREADY_EXISTS_CODE:
throw new GroupAlreadyExistsException(errorMessage);
+ case ErrorConstants.UNSUPPORTED_OPERATION_CODE:
+ throw new UnsupportedOperationException(errorMessage);
+
case ErrorConstants.INTERNAL_ERROR_CODE:
throw new RuntimeException(errorMessage);
@@ -638,6 +644,9 @@ public class ErrorHandlers {
case ErrorConstants.ALREADY_EXISTS_CODE:
throw new RoleAlreadyExistsException(errorMessage);
+ case ErrorConstants.UNSUPPORTED_OPERATION_CODE:
+ throw new UnsupportedOperationException(errorMessage);
+
case ErrorConstants.INTERNAL_ERROR_CODE:
throw new RuntimeException(errorMessage);
@@ -675,6 +684,9 @@ public class ErrorHandlers {
throw new NotFoundException(errorMessage);
}
+ case ErrorConstants.UNSUPPORTED_OPERATION_CODE:
+ throw new UnsupportedOperationException(errorMessage);
+
case ErrorConstants.INTERNAL_ERROR_CODE:
throw new RuntimeException(errorMessage);
@@ -748,6 +760,9 @@ public class ErrorHandlers {
throw new NotFoundException(errorMessage);
}
+ case ErrorConstants.UNSUPPORTED_OPERATION_CODE:
+ throw new UnsupportedOperationException(errorMessage);
+
case ErrorConstants.INTERNAL_ERROR_CODE:
throw new RuntimeException(errorMessage);
diff --git a/integration-test/src/test/java/org/apache/gravitino/integration/test/authorization/AccessControlNotAllowIT.java b/integration-test/src/test/java/org/apache/gravitino/integration/test/authorization/AccessControlNotAllowIT.java
new file mode 100644
index 0000000000..22986458ba
--- /dev/null
+++ b/integration-test/src/test/java/org/apache/gravitino/integration/test/authorization/AccessControlNotAllowIT.java
@@ -0,0 +1,153 @@
+/*
+ * 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.gravitino.integration.test.authorization;
+
+import com.google.common.collect.Lists;
+import java.util.Collections;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.MetadataObjects;
+import org.apache.gravitino.authorization.Owner;
+import org.apache.gravitino.authorization.Privileges;
+import org.apache.gravitino.authorization.SecurableObjects;
+import org.apache.gravitino.client.GravitinoMetalake;
+import org.apache.gravitino.integration.test.util.AbstractIT;
+import org.apache.gravitino.utils.RandomNameUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class AccessControlNotAllowIT extends AbstractIT {
+
+ public static String metalakeTestName = RandomNameUtils.genRandomName("test");
+
+ @Test
+ public void testNotAllowFilter() {
+ GravitinoMetalake metalake =
+ client.createMetalake(metalakeTestName, "metalake test", Collections.emptyMap());
+
+ Exception e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.addUser("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.removeUser("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.getUser("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.addGroup("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.getGroup("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.removeGroup("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ metalake.createRole(
+ "test",
+ Collections.emptyMap(),
+ Lists.newArrayList(
+ SecurableObjects.ofMetalake(
+ "test", Lists.newArrayList(Privileges.SelectTable.allow())))));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.getRole("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> metalake.deleteRole("test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> metalake.grantRolesToGroup(Lists.newArrayList("test"), "test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> metalake.grantRolesToUser(Lists.newArrayList("test"), "test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> metalake.revokeRolesFromGroup(Lists.newArrayList("test"), "test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () -> metalake.revokeRolesFromUser(Lists.newArrayList("test"), "test"));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ metalake.setOwner(
+ MetadataObjects.of(null, "test", MetadataObject.Type.METALAKE),
+ "test",
+ Owner.Type.USER));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ e =
+ Assertions.assertThrows(
+ UnsupportedOperationException.class,
+ () ->
+ metalake.getOwner(MetadataObjects.of(null, "test", MetadataObject.Type.METALAKE)));
+ Assertions.assertTrue(
+ e.getMessage().contains("You should set 'gravitino.authorization.enable'"));
+
+ client.dropMetalake(metalakeTestName);
+ }
+}
diff --git a/server/src/main/java/org/apache/gravitino/server/web/filter/AccessControlNotAllowedFilter.java b/server/src/main/java/org/apache/gravitino/server/web/filter/AccessControlNotAllowedFilter.java
index 130827371c..42c001f766 100644
--- a/server/src/main/java/org/apache/gravitino/server/web/filter/AccessControlNotAllowedFilter.java
+++ b/server/src/main/java/org/apache/gravitino/server/web/filter/AccessControlNotAllowedFilter.java
@@ -19,16 +19,13 @@
package org.apache.gravitino.server.web.filter;
-import static javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED;
-
import java.io.IOException;
-import java.util.Collections;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
-import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import org.apache.gravitino.Configs;
import org.apache.gravitino.server.authorization.NameBindings;
+import org.apache.gravitino.server.web.Utils;
/**
* AccessControlNotAllowedFilter is used for filter the requests related to access control if Apache
@@ -43,13 +40,10 @@ public class AccessControlNotAllowedFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.abortWith(
- Response.status(
- SC_METHOD_NOT_ALLOWED,
- String.format(
- "You should set '%s' to true in the server side `gravitino.conf`"
- + " to enable the authorization of the system, otherwise these interfaces can't work.",
- Configs.ENABLE_AUTHORIZATION.getKey()))
- .allow(Collections.emptySet())
- .build());
+ Utils.unsupportedOperation(
+ String.format(
+ "You should set '%s' to true in the server side `gravitino.conf`"
+ + " to enable the authorization of the system, otherwise these interfaces can't work.",
+ Configs.ENABLE_AUTHORIZATION.getKey())));
}
}