poojanilangekar commented on code in PR #2170: URL: https://github.com/apache/polaris/pull/2170#discussion_r2229118532
########## polaris-core/src/test/java/org/apache/polaris/core/persistence/PolarisResolvedPathWrapperTest.java: ########## @@ -0,0 +1,145 @@ +/* + * 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.polaris.core.persistence; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntityType; +import org.junit.jupiter.api.Test; + +public class PolarisResolvedPathWrapperTest { + + @Test + void testIsFullyResolvedNamespace_NullResolvedPath() { + PolarisResolvedPathWrapper wrapper = new PolarisResolvedPathWrapper(null); + + boolean result = wrapper.isFullyResolvedNamespace("test-catalog", Namespace.of("ns1")); + + assertThat(result).isFalse(); + } + + @Test + void testIsFullyResolvedNamespace_InsufficientPathLength() { + String catalogName = "test-catalog"; + Namespace namespace = Namespace.of("ns1", "ns2"); + + PolarisEntity catalogEntity = createEntity(catalogName, PolarisEntityType.CATALOG); + List<ResolvedPolarisEntity> shortPath = List.of(createResolvedEntity(catalogEntity)); + PolarisResolvedPathWrapper wrapper = new PolarisResolvedPathWrapper(shortPath); + + assertFalse(wrapper.isFullyResolvedNamespace(catalogName, namespace)); + } + + @Test + void testIsFullyResolvedNamespace_WrongCatalogName() { + String catalogName = "test-catalog"; + String wrongCatalogName = "wrong-catalog"; + Namespace namespace = Namespace.of("ns1"); + + PolarisEntity wrongCatalogEntity = createEntity(wrongCatalogName, PolarisEntityType.CATALOG); + PolarisEntity namespaceEntity = createEntity("ns1", PolarisEntityType.NAMESPACE); + List<ResolvedPolarisEntity> path = + List.of(createResolvedEntity(wrongCatalogEntity), createResolvedEntity(namespaceEntity)); + PolarisResolvedPathWrapper wrapper = new PolarisResolvedPathWrapper(path); + + assertFalse(wrapper.isFullyResolvedNamespace(catalogName, namespace)); + } + + @Test + void testIsFullyResolvedNamespace_WrongNamespaceNames() { + String catalogName = "test-catalog"; + Namespace namespace = Namespace.of("ns1", "ns2"); + + PolarisEntity catalogEntity = createEntity(catalogName, PolarisEntityType.CATALOG); + PolarisEntity namespace1Entity = createEntity("ns1", PolarisEntityType.NAMESPACE); + PolarisEntity namespace2WrongEntity = createEntity("wrong-ns2", PolarisEntityType.NAMESPACE); + List<ResolvedPolarisEntity> path = + List.of( + createResolvedEntity(catalogEntity), + createResolvedEntity(namespace1Entity), + createResolvedEntity(namespace2WrongEntity)); + PolarisResolvedPathWrapper wrapper = new PolarisResolvedPathWrapper(path); + + assertFalse(wrapper.isFullyResolvedNamespace(catalogName, namespace)); + } + + @Test + void testIsFullyResolvedNamespace_CorrectPath() { + String catalogName = "test-catalog"; + Namespace namespace = Namespace.of("ns1", "ns2"); + + PolarisEntity catalogEntity = createEntity(catalogName, PolarisEntityType.CATALOG); + PolarisEntity namespace1Entity = createEntity("ns1", PolarisEntityType.NAMESPACE); + PolarisEntity namespace2Entity = createEntity("ns2", PolarisEntityType.NAMESPACE); + List<ResolvedPolarisEntity> path = + List.of( + createResolvedEntity(catalogEntity), + createResolvedEntity(namespace1Entity), + createResolvedEntity(namespace2Entity)); + PolarisResolvedPathWrapper wrapper = new PolarisResolvedPathWrapper(path); + + assertTrue(wrapper.isFullyResolvedNamespace(catalogName, namespace)); + } + + @Test + void testIsFullyResolvedNamespace_WrongEntityType() { + String catalogName = "test-catalog"; + Namespace namespace = Namespace.of("ns1"); + + PolarisEntity catalogEntity = createEntity(catalogName, PolarisEntityType.CATALOG); + PolarisEntity wrongTypeEntity = createEntity("ns1", PolarisEntityType.TABLE_LIKE); Review Comment: Done. ########## service/common/src/test/java/org/apache/polaris/service/admin/PolarisAdminServiceTest.java: ########## @@ -0,0 +1,282 @@ +/* + * 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.polaris.service.admin; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import jakarta.ws.rs.core.SecurityContext; +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.PolarisDiagnostics; +import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal; +import org.apache.polaris.core.auth.PolarisAuthorizer; +import org.apache.polaris.core.context.CallContext; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntityType; +import org.apache.polaris.core.entity.PolarisPrivilege; +import org.apache.polaris.core.persistence.PolarisEntityManager; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.dao.entity.PrivilegeResult; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifest; +import org.apache.polaris.core.persistence.resolver.ResolverStatus; +import org.apache.polaris.core.secrets.UserSecretsManager; +import org.apache.polaris.service.config.ReservedProperties; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class PolarisAdminServiceTest { + + @Mock private CallContext callContext; + @Mock private PolarisCallContext polarisCallContext; + @Mock private PolarisDiagnostics polarisDiagnostics; + @Mock private PolarisEntityManager entityManager; + @Mock private PolarisMetaStoreManager metaStoreManager; + @Mock private UserSecretsManager userSecretsManager; + @Mock private SecurityContext securityContext; + @Mock private PolarisAuthorizer authorizer; + @Mock private ReservedProperties reservedProperties; + @Mock private AuthenticatedPolarisPrincipal authenticatedPrincipal; + @Mock private PolarisResolutionManifest resolutionManifest; + @Mock private PolarisResolvedPathWrapper resolvedPathWrapper; + + private PolarisAdminService adminService; + + @BeforeEach + void setUp() throws Exception { + MockitoAnnotations.openMocks(this); + when(securityContext.getUserPrincipal()).thenReturn(authenticatedPrincipal); + when(callContext.getPolarisCallContext()).thenReturn(polarisCallContext); + when(polarisCallContext.getDiagServices()).thenReturn(polarisDiagnostics); + + adminService = + new PolarisAdminService( + callContext, + entityManager, + metaStoreManager, + userSecretsManager, + securityContext, + authorizer, + reservedProperties); + } + + @Test + void testGrantPrivilegeOnNamespaceToRole() throws Exception { + String catalogName = "test-catalog"; + String catalogRoleName = "test-role"; + Namespace namespace = Namespace.of("existing-ns"); + PolarisPrivilege privilege = PolarisPrivilege.NAMESPACE_FULL_METADATA; + + setupSuccessfulNamespaceResolution(catalogName, catalogRoleName, namespace); + + PrivilegeResult successResult = mock(PrivilegeResult.class); + when(successResult.isSuccess()).thenReturn(true); + when(metaStoreManager.grantPrivilegeOnSecurableToRole(any(), any(), any(), any(), any())) + .thenReturn(successResult); + + boolean result = + adminService.grantPrivilegeOnNamespaceToRole( + catalogName, catalogRoleName, namespace, privilege); + + assertThat(result).isTrue(); + } + + @Test + void testGrantPrivilegeOnNamespaceToRole_ThrowsNamespaceNotFoundException() { + String catalogName = "test-catalog"; + String catalogRoleName = "test-role"; + Namespace namespace = Namespace.of("non-existent-ns"); + PolarisPrivilege privilege = PolarisPrivilege.NAMESPACE_FULL_METADATA; + + when(entityManager.prepareResolutionManifest(any(), any(), eq(catalogName))) + .thenReturn(resolutionManifest); + when(resolutionManifest.resolveAll()).thenReturn(createSuccessfulResolverStatus()); + + PolarisResolvedPathWrapper catalogRoleWrapper = mock(PolarisResolvedPathWrapper.class); + PolarisEntity catalogRoleEntity = createEntity(catalogRoleName, PolarisEntityType.CATALOG_ROLE); + when(catalogRoleWrapper.getRawLeafEntity()).thenReturn(catalogRoleEntity); + when(resolutionManifest.getResolvedPath(eq(catalogRoleName))).thenReturn(catalogRoleWrapper); + + when(resolutionManifest.getResolvedPath(eq(namespace))).thenReturn(null); + + assertThatThrownBy( + () -> + adminService.grantPrivilegeOnNamespaceToRole( + catalogName, catalogRoleName, namespace, privilege)) + .isInstanceOf(NotFoundException.class) + .hasMessageContaining("Namespace " + namespace + " not found"); + } + + @Test + void testGrantPrivilegeOnNamespaceToRole_IncompleteNamespaceThrowsNamespaceNotFoundException() + throws Exception { + String catalogName = "test-catalog"; + String catalogRoleName = "test-role"; + Namespace namespace = Namespace.of("incomplete-ns"); Review Comment: Done. ########## polaris-core/src/test/java/org/apache/polaris/core/persistence/PolarisResolvedPathWrapperTest.java: ########## @@ -0,0 +1,145 @@ +/* + * 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.polaris.core.persistence; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import org.apache.iceberg.catalog.Namespace; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntityType; +import org.junit.jupiter.api.Test; + +public class PolarisResolvedPathWrapperTest { + + @Test + void testIsFullyResolvedNamespace_NullResolvedPath() { + PolarisResolvedPathWrapper wrapper = new PolarisResolvedPathWrapper(null); + + boolean result = wrapper.isFullyResolvedNamespace("test-catalog", Namespace.of("ns1")); + + assertThat(result).isFalse(); Review Comment: Done. ########## polaris-core/src/main/java/org/apache/polaris/core/persistence/PolarisResolvedPathWrapper.java: ########## @@ -77,6 +80,38 @@ public List<PolarisEntity> getRawParentPath() { .collect(Collectors.toList()); } + /** + * Checks if a namespace is fully resolved. + * + * @param catalogName the name of the catalog + * @param namespace the namespace we're trying to resolve + * @return true if the namespace is considered fully resolved for the given catalog type + */ + public boolean isFullyResolvedNamespace( + @Nonnull String catalogName, @Nonnull Namespace namespace) { + if (resolvedPath == null) { + return false; + } + + List<PolarisEntity> fullPath = getRawFullPath(); + int expectedPathLength = 1 + namespace.levels().length; + if (fullPath.size() != expectedPathLength) { + return false; + } + + if (!fullPath.get(0).getName().equals(catalogName)) { + return false; + } + + for (int i = 0; i < namespace.levels().length; i++) { + if (!fullPath.get(i + 1).getName().equals(namespace.levels()[i]) Review Comment: Done. -- 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: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org