roryqi commented on code in PR #11234: URL: https://github.com/apache/gravitino/pull/11234#discussion_r3304527621
########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/web/rest/IdpAuthorizationFilter.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.web.rest; + +import java.io.IOException; +import java.util.List; +import java.util.function.Supplier; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.ext.Provider; +import org.apache.gravitino.Configs; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.authorization.GravitinoAuthorizer; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.web.IdpManagement; +import org.apache.gravitino.idp.web.IdpRestUtils; + +/** + * Enforces built-in IdP management API access rules without server interception: + * + * <ul> + * <li>Available only when {@code basic} is configured in {@link Configs#AUTHENTICATORS} + * <li>Caller must be a service admin via {@link GravitinoAuthorizer#isServiceAdmin()} + * </ul> + * + * <p>Scoped to resources annotated with {@link IdpManagement} via Jersey name binding. + */ +@Provider +@IdpManagement +public class IdpAuthorizationFilter implements ContainerRequestFilter { + + /** Authenticator name that enables built-in IdP management APIs. */ + public static final String BASIC_AUTHENTICATOR = "basic"; + + /** Error message when the caller is not a service admin. */ + public static final String SERVICE_ADMIN_REQUIRED_MESSAGE = + "Only service admins can manage built-in IdP users and groups."; + + /** Error message when the {@code basic} authenticator is not enabled. */ + public static final String BASIC_AUTHENTICATOR_REQUIRED_MESSAGE = + "Built-in IdP management APIs are available only when the basic authenticator is enabled."; + + private final Supplier<List<String>> authenticatorsSupplier; + private final Supplier<GravitinoAuthorizer> authorizerSupplier; + + /** Creates a filter backed by the running Gravitino server configuration and authorizer. */ + public IdpAuthorizationFilter() { + this( + () -> GravitinoEnv.getInstance().config().get(Configs.AUTHENTICATORS), + () -> GravitinoEnv.getInstance().gravitinoAuthorizer()); + } + + IdpAuthorizationFilter( + Supplier<List<String>> authenticatorsSupplier, + Supplier<GravitinoAuthorizer> authorizerSupplier) { + this.authenticatorsSupplier = authenticatorsSupplier; + this.authorizerSupplier = authorizerSupplier; + } + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + if (!basicAuthenticatorEnabled(authenticatorsSupplier.get())) { Review Comment: Why do we return ForbiddenException here? This REST is optional. If we don't enable this plugin, the resource is unavaible. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/web/rest/IdpAuthorizationFilter.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.web.rest; + +import java.io.IOException; +import java.util.List; +import java.util.function.Supplier; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.ext.Provider; +import org.apache.gravitino.Configs; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.authorization.GravitinoAuthorizer; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.web.IdpManagement; +import org.apache.gravitino.idp.web.IdpRestUtils; + +/** + * Enforces built-in IdP management API access rules without server interception: + * + * <ul> + * <li>Available only when {@code basic} is configured in {@link Configs#AUTHENTICATORS} + * <li>Caller must be a service admin via {@link GravitinoAuthorizer#isServiceAdmin()} + * </ul> + * + * <p>Scoped to resources annotated with {@link IdpManagement} via Jersey name binding. + */ +@Provider +@IdpManagement +public class IdpAuthorizationFilter implements ContainerRequestFilter { + + /** Authenticator name that enables built-in IdP management APIs. */ + public static final String BASIC_AUTHENTICATOR = "basic"; + + /** Error message when the caller is not a service admin. */ + public static final String SERVICE_ADMIN_REQUIRED_MESSAGE = + "Only service admins can manage built-in IdP users and groups."; + + /** Error message when the {@code basic} authenticator is not enabled. */ + public static final String BASIC_AUTHENTICATOR_REQUIRED_MESSAGE = + "Built-in IdP management APIs are available only when the basic authenticator is enabled."; + + private final Supplier<List<String>> authenticatorsSupplier; + private final Supplier<GravitinoAuthorizer> authorizerSupplier; + + /** Creates a filter backed by the running Gravitino server configuration and authorizer. */ + public IdpAuthorizationFilter() { + this( + () -> GravitinoEnv.getInstance().config().get(Configs.AUTHENTICATORS), + () -> GravitinoEnv.getInstance().gravitinoAuthorizer()); + } + + IdpAuthorizationFilter( + Supplier<List<String>> authenticatorsSupplier, + Supplier<GravitinoAuthorizer> authorizerSupplier) { + this.authenticatorsSupplier = authenticatorsSupplier; + this.authorizerSupplier = authorizerSupplier; + } + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + if (!basicAuthenticatorEnabled(authenticatorsSupplier.get())) { + requestContext.abortWith( + IdpRestUtils.notFound( + BASIC_AUTHENTICATOR_REQUIRED_MESSAGE, + new NotFoundException(BASIC_AUTHENTICATOR_REQUIRED_MESSAGE))); + return; + } + + GravitinoAuthorizer authorizer = authorizerSupplier.get(); + if (authorizer == null || !authorizer.isServiceAdmin()) { Review Comment: Maybe you don't need to reuse authorizer class. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/web/rest/IdpAuthorizationFilter.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.web.rest; + +import java.io.IOException; +import java.util.List; +import java.util.function.Supplier; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.ext.Provider; +import org.apache.gravitino.Configs; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.authorization.GravitinoAuthorizer; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.web.IdpManagement; +import org.apache.gravitino.idp.web.IdpRestUtils; + +/** + * Enforces built-in IdP management API access rules without server interception: + * + * <ul> + * <li>Available only when {@code basic} is configured in {@link Configs#AUTHENTICATORS} + * <li>Caller must be a service admin via {@link GravitinoAuthorizer#isServiceAdmin()} + * </ul> + * + * <p>Scoped to resources annotated with {@link IdpManagement} via Jersey name binding. + */ +@Provider +@IdpManagement +public class IdpAuthorizationFilter implements ContainerRequestFilter { + + /** Authenticator name that enables built-in IdP management APIs. */ + public static final String BASIC_AUTHENTICATOR = "basic"; + + /** Error message when the caller is not a service admin. */ + public static final String SERVICE_ADMIN_REQUIRED_MESSAGE = + "Only service admins can manage built-in IdP users and groups."; + + /** Error message when the {@code basic} authenticator is not enabled. */ + public static final String BASIC_AUTHENTICATOR_REQUIRED_MESSAGE = + "Built-in IdP management APIs are available only when the basic authenticator is enabled."; + + private final Supplier<List<String>> authenticatorsSupplier; + private final Supplier<GravitinoAuthorizer> authorizerSupplier; + + /** Creates a filter backed by the running Gravitino server configuration and authorizer. */ + public IdpAuthorizationFilter() { + this( + () -> GravitinoEnv.getInstance().config().get(Configs.AUTHENTICATORS), + () -> GravitinoEnv.getInstance().gravitinoAuthorizer()); + } + + IdpAuthorizationFilter( + Supplier<List<String>> authenticatorsSupplier, + Supplier<GravitinoAuthorizer> authorizerSupplier) { + this.authenticatorsSupplier = authenticatorsSupplier; + this.authorizerSupplier = authorizerSupplier; + } + + @Override + public void filter(ContainerRequestContext requestContext) throws IOException { + if (!basicAuthenticatorEnabled(authenticatorsSupplier.get())) { + requestContext.abortWith( + IdpRestUtils.notFound( + BASIC_AUTHENTICATOR_REQUIRED_MESSAGE, + new NotFoundException(BASIC_AUTHENTICATOR_REQUIRED_MESSAGE))); + return; + } + + GravitinoAuthorizer authorizer = authorizerSupplier.get(); + if (authorizer == null || !authorizer.isServiceAdmin()) { Review Comment: What's the filter order? Will your filter be applied after authenticator filter? ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/dto/util/IdpDTOConverters.java: ########## @@ -0,0 +1,50 @@ +/* + * 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.dto.util; + +import org.apache.gravitino.idp.dto.IdpGroupDTO; +import org.apache.gravitino.idp.dto.IdpUserDTO; +import org.apache.gravitino.idp.model.IdpGroup; +import org.apache.gravitino.idp.model.IdpUser; + +/** Utility class for converting between built-in IdP DTOs and domain objects. */ +public class IdpDTOConverters { + + private IdpDTOConverters() {} + + /** + * Converts a built-in IdP user to a user DTO. + * + * @param user The built-in IdP user. + * @return The user DTO. + */ + public static IdpUserDTO toDTO(IdpUser user) { Review Comment: Optional: You can put this method to IdpUser. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/web/rest/IdpGroupOperations.java: ########## @@ -0,0 +1,163 @@ +/* + * 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.web.rest; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import java.util.Arrays; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +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.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.dto.responses.RemoveResponse; +import org.apache.gravitino.idp.IdpUserGroupManager; +import org.apache.gravitino.idp.dto.requests.AddGroupRequest; +import org.apache.gravitino.idp.dto.requests.GroupMembershipChangeRequest; +import org.apache.gravitino.idp.dto.responses.IdpGroupResponse; +import org.apache.gravitino.idp.dto.util.IdpDTOConverters; +import org.apache.gravitino.idp.web.IdpManagement; +import org.apache.gravitino.idp.web.IdpOperationType; +import org.apache.gravitino.idp.web.IdpRestUtils; +import org.apache.gravitino.metrics.MetricNames; + +/** REST resource for built-in IdP group management exposed by the {@code idp-basic} plugin. */ +@IdpManagement +@Path("/idp/groups") +public class IdpGroupOperations { + + private final IdpUserGroupManager userGroupManager; + + @Context private HttpServletRequest httpRequest; + + public IdpGroupOperations() { + this( Review Comment: It's not good to create a two UserGroupManager. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/web/rest/IdpGroupOperations.java: ########## @@ -0,0 +1,163 @@ +/* + * 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.web.rest; + +import com.codahale.metrics.annotation.ResponseMetered; +import com.codahale.metrics.annotation.Timed; +import java.util.Arrays; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +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.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.dto.responses.RemoveResponse; +import org.apache.gravitino.idp.IdpUserGroupManager; +import org.apache.gravitino.idp.dto.requests.AddGroupRequest; +import org.apache.gravitino.idp.dto.requests.GroupMembershipChangeRequest; +import org.apache.gravitino.idp.dto.responses.IdpGroupResponse; +import org.apache.gravitino.idp.dto.util.IdpDTOConverters; +import org.apache.gravitino.idp.web.IdpManagement; +import org.apache.gravitino.idp.web.IdpOperationType; +import org.apache.gravitino.idp.web.IdpRestUtils; +import org.apache.gravitino.metrics.MetricNames; + +/** REST resource for built-in IdP group management exposed by the {@code idp-basic} plugin. */ +@IdpManagement +@Path("/idp/groups") +public class IdpGroupOperations { + + private final IdpUserGroupManager userGroupManager; + + @Context private HttpServletRequest httpRequest; + + public IdpGroupOperations() { + this( + new IdpUserGroupManager( + GravitinoEnv.getInstance().config(), GravitinoEnv.getInstance().idGenerator())); + } + + IdpGroupOperations(IdpUserGroupManager userGroupManager) { + this.userGroupManager = userGroupManager; + } + + @GET + @Path("{group}") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "get-idp-group." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "get-idp-group", absolute = true) + public Response getGroup(@PathParam("group") String group) { + return IdpRestUtils.doAs( + httpRequest, + () -> + IdpRestUtils.ok( + new IdpGroupResponse(IdpDTOConverters.toDTO(userGroupManager.getGroup(group)))), + "group", + IdpOperationType.GET, + group); + } + + @POST + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "add-idp-group." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "add-idp-group", absolute = true) + public Response addGroup(AddGroupRequest request) { + return IdpRestUtils.doAs( + httpRequest, + () -> { + request.validate(); + return IdpRestUtils.ok( + new IdpGroupResponse( + IdpDTOConverters.toDTO(userGroupManager.addGroup(request.getGroup())))); + }, + "group", + IdpOperationType.ADD, + request.getGroup()); + } + + @DELETE + @Path("{group}") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "remove-idp-group." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "remove-idp-group", absolute = true) + public Response removeGroup( + @PathParam("group") String group, @DefaultValue("false") @QueryParam("force") boolean force) { + return IdpRestUtils.doAs( + httpRequest, + () -> IdpRestUtils.ok(new RemoveResponse(userGroupManager.removeGroup(group, force))), + "group", + IdpOperationType.REMOVE, + group); + } + + @PUT + @Path("{group}/add") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "add-idp-group-users." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "add-idp-group-users", absolute = true) + public Response addUsersToGroup( + @PathParam("group") String group, GroupMembershipChangeRequest request) { + return IdpRestUtils.doAs( + httpRequest, + () -> { + request.validate(); + String[] usersToAdd = request.getUsersToAdd(); + return IdpRestUtils.ok( + new IdpGroupResponse( + IdpDTOConverters.toDTO( + userGroupManager.changeGroupMembership( + group, usersToAdd == null ? null : Arrays.asList(usersToAdd), null)))); + }, + "group", + IdpOperationType.ADD, + group); + } + + @PUT + @Path("{group}/remove") + @Produces("application/vnd.gravitino.v1+json") + @Timed(name = "remove-idp-group-users." + MetricNames.HTTP_PROCESS_DURATION, absolute = true) + @ResponseMetered(name = "remove-idp-group-users", absolute = true) + public Response removeUsersFromGroup( + @PathParam("group") String group, GroupMembershipChangeRequest request) { + return IdpRestUtils.doAs( + httpRequest, + () -> { + request.validate(); + String[] usersToRemove = request.getUsersToRemove(); + return IdpRestUtils.ok( + new IdpGroupResponse( + IdpDTOConverters.toDTO( + userGroupManager.changeGroupMembership( + group, + null, + usersToRemove == null ? null : Arrays.asList(usersToRemove))))); Review Comment: Should we validate in the request? ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/web/rest/IdpUserOperations.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.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.GravitinoEnv; +import org.apache.gravitino.dto.responses.RemoveResponse; +import org.apache.gravitino.idp.IdpUserGroupManager; +import org.apache.gravitino.idp.dto.requests.AddUserRequest; +import org.apache.gravitino.idp.dto.requests.ChangePasswordRequest; +import org.apache.gravitino.idp.dto.responses.IdpUserResponse; +import org.apache.gravitino.idp.dto.util.IdpDTOConverters; +import org.apache.gravitino.idp.web.IdpManagement; +import org.apache.gravitino.idp.web.IdpOperationType; +import org.apache.gravitino.idp.web.IdpRestUtils; +import org.apache.gravitino.metrics.MetricNames; + +/** REST resource for built-in IdP user management exposed by the {@code idp-basic} plugin. */ +@IdpManagement +@Path("/idp/users") +public class IdpUserOperations { + + private final IdpUserGroupManager userGroupManager; + + @Context private HttpServletRequest httpRequest; + + public IdpUserOperations() { + this( + new IdpUserGroupManager( Review Comment: You shouldn't new IdpUserGroupManager here. You should initialize it in the plugin class. -- 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]
