lasdf1234 commented on code in PR #10971: URL: https://github.com/apache/gravitino/pull/10971#discussion_r3212752596
########## server/src/main/java/org/apache/gravitino/server/web/rest/IdpUserOperations.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.server.web.rest; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.authorization.IdpUserManager; +import org.apache.gravitino.dto.requests.CreateUserRequest; +import org.apache.gravitino.dto.requests.ResetPasswordRequest; +import org.apache.gravitino.dto.responses.IdpUserResponse; +import org.apache.gravitino.dto.responses.RemoveResponse; +import org.apache.gravitino.metrics.MetricNames; +import org.apache.gravitino.server.web.Utils; + +@Path("/idp/users") +public class IdpUserOperations { + + private final IdpUserManager userManager; Review Comment: resolved ########## server/src/test/java/org/apache/gravitino/server/TestGravitinoServer.java: ########## @@ -136,4 +172,162 @@ 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 testInitializeRestApiWithBasicAuthenticator() throws Exception { + ServerConfig serverConfig = new ServerConfig(); + serverConfig.loadFromMap(ImmutableMap.of(Configs.AUTHENTICATORS.getKey(), "basic"), t -> true); + + GravitinoServer restServer = newRestApiTestServer(serverConfig, Collections.emptySet()); + invokeInitializeRestApi(restServer); + + IdpUserManager userManager = Mockito.mock(IdpUserManager.class); + Mockito.when(userManager.getUser("user1")) + .thenReturn( + IdpUserDTO.builder() + .withName("user1") + .withGroups(Collections.emptyList()) + .withAudit(buildAudit()) + .build()); + + restServer.register(newIdpUserOperations(userManager)); + restServer.register( + new AbstractBinder() { + @Override + protected void configure() { + bind(Mockito.mock(HttpServletRequest.class)).to(HttpServletRequest.class); + } + }); + + JerseyTest jerseyTest = + new JerseyTest() { + @Override + protected Application configure() { + return restServer; + } + }; + + try { + jerseyTest.setUp(); + Response response = + 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()); + assertEquals("admin", userResponse.getUser().auditInfo().creator()); + } finally { + jerseyTest.tearDown(); + } + } + + @Test + public void testInitializeRestApiWithoutBasicAuthenticator() throws Exception { + ServerConfig serverConfig = new ServerConfig(); + serverConfig.loadFromMap( + ImmutableMap.of( + Configs.AUTHENTICATORS.getKey(), "simple", + Configs.REST_API_EXTENSION_PACKAGES.getKey(), "org.apache.gravitino.test.extension"), + t -> true); + + GravitinoServer restServer = + newRestApiTestServer(serverConfig, Set.of("org.apache.gravitino.test.lineage")); + + invokeInitializeRestApi(restServer); + + JerseyTest jerseyTest = + new JerseyTest() { + @Override + protected Application configure() { + return restServer; + } + }; + + try { + jerseyTest.setUp(); + assertIdpInterfaceNotFound(jerseyTest, "/idp/users/user1"); + assertIdpInterfaceNotFound(jerseyTest, "/idp/groups/group1"); + } finally { + jerseyTest.tearDown(); + } + } + + private GravitinoServer newRestApiTestServer( + ServerConfig serverConfig, Set<String> lineagePackages) throws IllegalAccessException { + GravitinoEnv gravitinoEnv = Mockito.mock(GravitinoEnv.class); + mockDispatchers(gravitinoEnv); + + GravitinoServer restServer = new GravitinoServer(serverConfig, gravitinoEnv); + JettyServer jettyServer = Mockito.mock(JettyServer.class); + ThreadPool threadPool = Mockito.mock(ThreadPool.class); + LineageService lineageService = Mockito.mock(LineageService.class); + + Mockito.when(jettyServer.getThreadPool()).thenReturn(threadPool); + Mockito.when(lineageService.getRESTPackages()).thenReturn(lineagePackages); + + FieldUtils.writeField(restServer, "server", jettyServer, true); + FieldUtils.writeField(restServer, "lineageService", lineageService, true); + return restServer; + } + + private void mockDispatchers(GravitinoEnv gravitinoEnv) { + Mockito.when(gravitinoEnv.metalakeDispatcher()) + .thenReturn(Mockito.mock(MetalakeDispatcher.class)); + Mockito.when(gravitinoEnv.catalogDispatcher()) + .thenReturn(Mockito.mock(CatalogDispatcher.class)); + Mockito.when(gravitinoEnv.schemaDispatcher()).thenReturn(Mockito.mock(SchemaDispatcher.class)); + Mockito.when(gravitinoEnv.tableDispatcher()).thenReturn(Mockito.mock(TableDispatcher.class)); + Mockito.when(gravitinoEnv.partitionDispatcher()) + .thenReturn(Mockito.mock(PartitionDispatcher.class)); + Mockito.when(gravitinoEnv.filesetDispatcher()) + .thenReturn(Mockito.mock(FilesetDispatcher.class)); + Mockito.when(gravitinoEnv.topicDispatcher()).thenReturn(Mockito.mock(TopicDispatcher.class)); + Mockito.when(gravitinoEnv.tagDispatcher()).thenReturn(Mockito.mock(TagDispatcher.class)); + Mockito.when(gravitinoEnv.policyDispatcher()).thenReturn(Mockito.mock(PolicyDispatcher.class)); + Mockito.when(gravitinoEnv.credentialOperationDispatcher()) + .thenReturn(Mockito.mock(CredentialOperationDispatcher.class)); + Mockito.when(gravitinoEnv.modelDispatcher()).thenReturn(Mockito.mock(ModelDispatcher.class)); + Mockito.when(gravitinoEnv.functionDispatcher()) + .thenReturn(Mockito.mock(FunctionDispatcher.class)); + Mockito.when(gravitinoEnv.jobOperationDispatcher()) + .thenReturn(Mockito.mock(JobOperationDispatcher.class)); + Mockito.when(gravitinoEnv.statisticDispatcher()) + .thenReturn(Mockito.mock(StatisticDispatcher.class)); + } Review Comment: resolved ########## server/src/main/java/org/apache/gravitino/server/GravitinoServer.java: ########## @@ -123,6 +128,10 @@ public ServerConfig serverConfig() { } private void initializeRestApi() { + boolean enableBasicAuthenticator = + serverConfig + .get(Configs.AUTHENTICATORS) + .contains(AuthenticatorType.BASIC.name().toLowerCase()); HashSet<String> restApiPackagesSet = new HashSet<>(); Review Comment: resolved -- 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]
