Copilot commented on code in PR #10971: URL: https://github.com/apache/gravitino/pull/10971#discussion_r3213133102
########## core/src/main/java/org/apache/gravitino/IdpManagerFactory.java: ########## @@ -0,0 +1,142 @@ +/* + * 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; + +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; +import org.apache.gravitino.authorization.IdpManager; +import org.apache.gravitino.dto.IdpGroupDTO; +import org.apache.gravitino.dto.IdpUserDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is responsible for creating instances of IdpManager implementations. IdpManager + * implementations are used to manage built-in IdP users and groups within the Apache Gravitino + * framework. + */ +public class IdpManagerFactory { + + private static final Logger LOG = LoggerFactory.getLogger(IdpManagerFactory.class); + + private static final String IDP_MANAGER_UNAVAILABLE_MESSAGE = + "Built-in IdP management is unavailable because no IdpManager plugin implementation was found" + + " on the runtime classpath."; + + // Private constructor to prevent instantiation of this factory class. + private IdpManagerFactory() {} + + /** + * Creates an instance of IdpManager from the runtime classpath. + * + * @return An instance of IdpManager. + */ + public static IdpManager createIdpManager() { + return loadService(IdpManager.class); + } + + /** + * Creates an instance of IdpManager from the runtime classpath or returns an unavailable + * placeholder when no implementation is present. + * + * @return An instance of IdpManager. + */ + public static IdpManager createIdpManagerOrDefault() { + try { + return createIdpManager(); + } catch (IllegalStateException e) { + if (e.getMessage() != null + && e.getMessage() + .contains("No " + IdpManager.class.getSimpleName() + " implementation")) { + LOG.warn("No IdpManager implementation found on the runtime classpath."); + return new UnavailableIdpManager(); + } Review Comment: `createIdpManagerOrDefault()` detects the “no provider found” case by substring-matching the `IllegalStateException` message. This is brittle (message changes will break the fallback and may prevent server startup). Prefer making `loadService(...)` return an `Optional`/nullable result for the empty-provider case (or throw a dedicated exception type) so the defaulting logic doesn’t depend on exception text. ########## 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 selfContext = + newIdpUserServerTestContext(serverConfig, idpManager, "user1")) { + Response response = + selfContext + .jerseyTest() + .target("/idp/users/user1") + .request("application/vnd.gravitino.v1+json") + .get(); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + } Review Comment: `testInitializeRestApiExposesIdpInterfaces` only covers the allowed cases (service admin and USER::SELF) for `GET /idp/users/{user}`. Since the PR’s key behavior is operations-layer authorization, please add a negative test (e.g., principal `user2` requesting `/idp/users/user1`) asserting `403 FORBIDDEN` to ensure the interceptor actually enforces `SERVICE_ADMIN || USER::SELF`. -- 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]
