Copilot commented on code in PR #10971: URL: https://github.com/apache/gravitino/pull/10971#discussion_r3213117657
########## 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: The implementation class is named `IdpManager`, which forces using the fully-qualified interface name (`implements org.apache.gravitino.authorization.IdpManager`) and makes call sites/tests ambiguous between the interface and implementation. Consider renaming the plugin implementation class (e.g., `BasicIdpManager`) and importing `org.apache.gravitino.authorization.IdpManager` normally; update the ServiceLoader entry accordingly. ########## 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: `IdpUserExceptionHandler` doesn’t handle `UnsupportedOperationException`. When the IdP plugin isn’t on the runtime classpath, `IdpManagerFactory.createOrDefault()` returns `UnavailableIdpManager`, whose methods throw `UnsupportedOperationException`—currently this will be translated into a 500 internal error for `/idp/users/*`. Add an explicit `UnsupportedOperationException` branch that returns `Utils.unsupportedOperation(...)` (consistent with other handlers in this file and with `IdpGroupExceptionHandler`). ########## 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: This test case looks inconsistent with the declared authorization policy for `GET /idp/users/{user}` (`SERVICE_ADMIN || USER::SELF`). The request is made as principal `non-admin` against `/idp/users/user1`, but the test still asserts 200 OK. Either bind the request principal as `user1` to test the SELF path, or change the assertion to expect 403 to ensure authorization is actually enforced. -- 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]
