jerqi commented on code in PR #4501: URL: https://github.com/apache/gravitino/pull/4501#discussion_r1716867437
########## integration-test/src/test/java/org/apache/gravitino/integration/test/authorization/AccessControlIT.java: ########## @@ -0,0 +1,260 @@ +/* + * 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.integration.test.authorization; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import java.util.Collections; +import java.util.Map; +import org.apache.gravitino.Configs; +import org.apache.gravitino.auth.AuthConstants; +import org.apache.gravitino.authorization.Group; +import org.apache.gravitino.authorization.Privilege; +import org.apache.gravitino.authorization.Privileges; +import org.apache.gravitino.authorization.Role; +import org.apache.gravitino.authorization.SecurableObject; +import org.apache.gravitino.authorization.SecurableObjects; +import org.apache.gravitino.authorization.User; +import org.apache.gravitino.client.GravitinoMetalake; +import org.apache.gravitino.exceptions.GroupAlreadyExistsException; +import org.apache.gravitino.exceptions.NoSuchGroupException; +import org.apache.gravitino.exceptions.NoSuchRoleException; +import org.apache.gravitino.exceptions.NoSuchUserException; +import org.apache.gravitino.exceptions.UserAlreadyExistsException; +import org.apache.gravitino.integration.test.util.AbstractIT; +import org.apache.gravitino.utils.RandomNameUtils; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class AccessControlIT extends AbstractIT { + + private static String metalakeName = RandomNameUtils.genRandomName("metalake"); + private static GravitinoMetalake metalake; + + @BeforeAll + public static void startIntegrationTest() throws Exception { + Map<String, String> configs = Maps.newHashMap(); + configs.put(Configs.ENABLE_AUTHORIZATION.getKey(), String.valueOf(true)); + configs.put(Configs.SERVICE_ADMINS.getKey(), AuthConstants.ANONYMOUS_USER); + registerCustomConfigs(configs); + AbstractIT.startIntegrationTest(); + metalake = client.createMetalake(metalakeName, "metalake comment", Collections.emptyMap()); + } + + @Test + void testManageUsers() { + String username = "user1#123"; + User user = metalake.addUser(username); + Assertions.assertEquals(username, user.name()); + Assertions.assertTrue(user.roles().isEmpty()); + + // Add an existed user + Assertions.assertThrows(UserAlreadyExistsException.class, () -> metalake.addUser(username)); + + user = metalake.getUser(username); + Assertions.assertEquals(username, user.name()); + Assertions.assertTrue(user.roles().isEmpty()); + + // Get a not-existed user + Assertions.assertThrows(NoSuchUserException.class, () -> metalake.getUser("not-existed")); + + Assertions.assertTrue(metalake.removeUser(username)); + Assertions.assertFalse(metalake.removeUser(username)); + } + + @Test + void testManageGroups() { + String groupName = "group1#123"; + Group group = metalake.addGroup(groupName); + Assertions.assertEquals(group.name(), groupName); + Assertions.assertTrue(group.roles().isEmpty()); + + // Added an existed group + Assertions.assertThrows(GroupAlreadyExistsException.class, () -> metalake.addGroup(groupName)); + + group = metalake.getGroup(groupName); + Assertions.assertEquals(group.name(), groupName); + Assertions.assertTrue(group.roles().isEmpty()); + + // Get a not-existed group + Assertions.assertThrows(NoSuchGroupException.class, () -> metalake.getGroup("not-existed")); + + Assertions.assertTrue(metalake.removeGroup(groupName)); + Assertions.assertFalse(metalake.removeGroup(groupName)); + } + + @Test + void testManageRoles() { + String roleName = "role#123"; + Map<String, String> properties = Maps.newHashMap(); + properties.put("k1", "v1"); + SecurableObject metalakeObject = + SecurableObjects.ofMetalake( + metalakeName, Lists.newArrayList(Privileges.CreateCatalog.allow())); + Role role = metalake.createRole(roleName, properties, Lists.newArrayList(metalakeObject)); + + Assertions.assertEquals(roleName, role.name()); + Assertions.assertEquals(properties, role.properties()); + + // Verify the object + Assertions.assertEquals(1, role.securableObjects().size()); + SecurableObject createdObject = role.securableObjects().get(0); + Assertions.assertEquals(metalakeObject.name(), createdObject.name()); + Assertions.assertEquals(metalakeObject.type(), createdObject.type()); + + // Verify the privilege + Assertions.assertEquals(1, createdObject.privileges().size()); + Privilege createdPrivilege = createdObject.privileges().get(0); + Assertions.assertEquals(createdPrivilege.name(), Privilege.Name.CREATE_CATALOG); + Assertions.assertEquals(createdPrivilege.condition(), Privilege.Condition.ALLOW); + + // Get a role + role = metalake.getRole(roleName); + + Assertions.assertEquals(roleName, role.name()); + Assertions.assertEquals(properties, role.properties()); + + // Verify the object + Assertions.assertEquals(1, role.securableObjects().size()); + createdObject = role.securableObjects().get(0); + Assertions.assertEquals(metalakeObject.name(), createdObject.name()); + Assertions.assertEquals(metalakeObject.type(), createdObject.type()); + + // Verify the privilege + Assertions.assertEquals(1, createdObject.privileges().size()); + createdPrivilege = createdObject.privileges().get(0); + Assertions.assertEquals(createdPrivilege.name(), Privilege.Name.CREATE_CATALOG); + Assertions.assertEquals(createdPrivilege.condition(), Privilege.Condition.ALLOW); + + // Delete a role + Assertions.assertTrue(metalake.deleteRole(roleName)); + Assertions.assertFalse(metalake.deleteRole(roleName)); + } + + @Test + void testManageUserPermissions() { + String username = "user1#123"; + User user = metalake.addUser(username); + Assertions.assertTrue(user.roles().isEmpty()); + + String roleName = "role#123"; + Map<String, String> properties = Maps.newHashMap(); + properties.put("k1", "v1"); + SecurableObject metalakeObject = + SecurableObjects.ofMetalake( + metalakeName, Lists.newArrayList(Privileges.CreateCatalog.allow())); Review Comment: Added the test case. -- 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]
