lasdf1234 commented on code in PR #10971:
URL: https://github.com/apache/gravitino/pull/10971#discussion_r3213126222
##########
server/src/main/java/org/apache/gravitino/server/web/rest/ExceptionHandlers.java:
##########
@@ -576,6 +584,83 @@ public Response handle(OperationType op, String group,
String metalake, Exceptio
}
}
+ private static class IdpUserExceptionHandler extends BaseExceptionHandler {
+
+ private static final ExceptionHandler INSTANCE = new
IdpUserExceptionHandler();
+
+ private static String getUserErrorMsg(String user, String operation,
String reason) {
+ return String.format(
+ "Failed to operate built-in IdP user %s operation [%s], reason [%s]",
+ user, operation, reason);
+ }
+
+ @Override
+ public Response handle(OperationType op, String user, String ignored,
Exception e) {
+ String formatted = StringUtil.isBlank(user) ? "" : " [" + user + "]";
+ String errorMsg = getUserErrorMsg(formatted, op.name(), getErrorMsg(e));
+ LOG.warn(errorMsg, e);
+
+ if (e instanceof IllegalArgumentException) {
+ return Utils.illegalArguments(errorMsg, e);
+
+ } else if (e instanceof NotFoundException) {
+ return Utils.notFound(errorMsg, e);
+
+ } else if (e instanceof UserAlreadyExistsException) {
+ return Utils.alreadyExists(errorMsg, e);
+
+ } else if (e instanceof NotInUseException) {
+ return Utils.notInUse(errorMsg, e);
+
+ } else if (e instanceof ForbiddenException) {
+ return Utils.forbidden(errorMsg, e);
+
+ } else {
+ return Utils.internalError(errorMsg, e);
Review Comment:
Fixed in 897c54041. `IdpUserExceptionHandler` now maps
`UnsupportedOperationException` to `Utils.unsupportedOperation(...)`, so
`/idp/users/*` no longer returns 500 when the IdP plugin is unavailable.
##########
server/src/test/java/org/apache/gravitino/server/TestGravitinoServer.java:
##########
@@ -136,4 +176,174 @@ public void testMainShutdownHookShouldInvokeServerStop()
throws IOException {
hookBlock.contains("server.gracefulStop()"),
"Shutdown hook should invoke server.gracefulStop() so app-level
cleanup runs on SIGTERM");
}
+
+ @Test
+ public void testInitializeRestApiExposesIdpInterfaces() throws Exception {
+ ServerConfig serverConfig = new ServerConfig();
+ serverConfig.loadFromMap(
+ ImmutableMap.of(
+ Configs.AUTHENTICATORS.getKey(), "oauth",
+ Configs.ENABLE_AUTHORIZATION.getKey(), "true",
+ Configs.SERVICE_ADMINS.getKey(), "admin",
+ Configs.REST_API_EXTENSION_PACKAGES.getKey(),
"org.apache.gravitino.test.extension"),
+ t -> true);
+
+ IdpManager idpManager = Mockito.mock(IdpManager.class);
+ Mockito.when(idpManager.getUser("user1"))
+ .thenReturn(
+
IdpUserDTO.builder().withName("user1").withGroups(Collections.emptyList()).build());
+
+ try (IdpUserServerTestContext adminContext =
+ newIdpUserServerTestContext(serverConfig, idpManager, "admin")) {
+ Response response =
+ adminContext
+ .jerseyTest()
+ .target("/idp/users/user1")
+ .request("application/vnd.gravitino.v1+json")
+ .get();
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+
+ IdpUserResponse userResponse =
response.readEntity(IdpUserResponse.class);
+ assertEquals("user1", userResponse.getUser().name());
+ }
+
+ try (IdpUserServerTestContext nonAdminContext =
+ newIdpUserServerTestContext(serverConfig, idpManager, "non-admin")) {
+ Response response =
+ nonAdminContext
+ .jerseyTest()
+ .target("/idp/users/user1")
+ .request("application/vnd.gravitino.v1+json")
+ .get();
+ assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Review Comment:
Fixed in 897c54041. The test now binds principal `user1` for `GET
/idp/users/user1`, so it exercises the `USER::SELF` authorization path instead
of a mismatched non-admin case.
##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/authorization/IdpManager.java:
##########
@@ -0,0 +1,301 @@
+/*
+ * 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.idp.basic.authorization;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.GravitinoEnv;
+import org.apache.gravitino.dto.IdpGroupDTO;
+import org.apache.gravitino.dto.IdpUserDTO;
+import org.apache.gravitino.exceptions.GroupAlreadyExistsException;
+import org.apache.gravitino.exceptions.NoSuchGroupException;
+import org.apache.gravitino.exceptions.NoSuchUserException;
+import org.apache.gravitino.exceptions.UserAlreadyExistsException;
+import org.apache.gravitino.idp.basic.password.PasswordHasher;
+import org.apache.gravitino.idp.basic.password.PasswordHasherFactory;
+import org.apache.gravitino.storage.IdGenerator;
+import org.apache.gravitino.storage.relational.po.IdpGroupPO;
+import org.apache.gravitino.storage.relational.po.IdpGroupUserRelPO;
+import org.apache.gravitino.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.storage.relational.service.IdpGroupMetaService;
+import org.apache.gravitino.storage.relational.service.IdpUserMetaService;
+
+/**
+ * Built-in IdP manager implementation loaded from the {@code idp-basic}
plugin.
+ *
+ * <p>This implementation manages both built-in IdP users and groups.
+ */
+public class IdpManager implements
org.apache.gravitino.authorization.IdpManager {
Review Comment:
Fixed in 897c54041. The plugin implementation has been renamed to
`BasicIdpManager`, the interface is now imported normally, and the
ServiceLoader entry plus related tests were updated accordingly.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]