visit2rahul commented on code in PR #4422: URL: https://github.com/apache/polaris/pull/4422#discussion_r3253305830
########## runtime/service/src/test/java/org/apache/polaris/service/admin/BaseLocationValidationTest.java: ########## @@ -0,0 +1,136 @@ +/* + * 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.assertThatCode; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.List; +import org.apache.iceberg.exceptions.BadRequestException; +import org.apache.polaris.core.entity.CatalogEntity; +import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link PolarisAdminService#validateBaseLocationAgainstStorageConfig}, the validation + * invoked by {@code updateCatalog} to reject catalog updates whose new default-base-location is not + * within any allowed location in the existing storage configuration. + */ +public class BaseLocationValidationTest { + + private static final String CATALOG = "myCatalog"; + + private static CatalogEntity catalogWithAllowedLocations(List<String> allowedLocations) { + PolarisStorageConfigurationInfo storageConfig = mock(PolarisStorageConfigurationInfo.class); + when(storageConfig.getAllowedLocations()).thenReturn(allowedLocations); + CatalogEntity catalogEntity = mock(CatalogEntity.class); + when(catalogEntity.getStorageConfigurationInfo()).thenReturn(storageConfig); + return catalogEntity; + } + + @Test + public void testChildLocationIsAccepted() { + CatalogEntity catalog = catalogWithAllowedLocations(List.of("s3://bucket/")); + assertThatCode( + () -> + PolarisAdminService.validateBaseLocationAgainstStorageConfig( + catalog, "s3://bucket/path/to/data", CATALOG)) + .doesNotThrowAnyException(); + } + + @Test + public void testExactMatchIsAccepted() { + CatalogEntity catalog = catalogWithAllowedLocations(List.of("s3://bucket/")); + assertThatCode( + () -> + PolarisAdminService.validateBaseLocationAgainstStorageConfig( + catalog, "s3://bucket/", CATALOG)) + .doesNotThrowAnyException(); + } + + @Test + public void testLocationOutsideAllowedRejected() { + CatalogEntity catalog = catalogWithAllowedLocations(List.of("s3://bucket/")); + assertThatThrownBy( + () -> + PolarisAdminService.validateBaseLocationAgainstStorageConfig( + catalog, "s3://other-bucket/data", CATALOG)) + .isInstanceOf(BadRequestException.class) + .hasMessageContaining(CATALOG) + .hasMessageContaining("s3://other-bucket/data") + .hasMessageContaining("s3://bucket/"); + } + + @Test + public void testAcceptedUnderAnyOfMultipleAllowedLocations() { + CatalogEntity catalog = + catalogWithAllowedLocations(List.of("s3://bucket-a/", "s3://bucket-b/warehouse/")); + assertThatCode( + () -> + PolarisAdminService.validateBaseLocationAgainstStorageConfig( + catalog, "s3://bucket-b/warehouse/data", CATALOG)) + .doesNotThrowAnyException(); + } + + @Test + public void testDifferentSchemeRejected() { + CatalogEntity catalog = catalogWithAllowedLocations(List.of("s3://bucket/")); + assertThatThrownBy( + () -> + PolarisAdminService.validateBaseLocationAgainstStorageConfig( + catalog, "gs://bucket/data", CATALOG)) + .isInstanceOf(BadRequestException.class); + } + + @Test + public void testNullStorageConfigSkipsValidation() { + CatalogEntity catalog = mock(CatalogEntity.class); + when(catalog.getStorageConfigurationInfo()).thenReturn(null); + // No storage config -> no allowed-location constraint -> validation must be a no-op. Review Comment: @dimas-b thank you for your thorough review. Based on my analysis - the null-storage-config "no-op" semantics match the established pattern in Polaris's catalog-level location handling: - `validateUpdateCatalogDiffOrThrow` (`PolarisAdminService.java:841-846`) early-returns when either current or new storage config is null - `getCatalogLocations` (`PolarisAdminService.java:571`) skips null storage config without exception - `DefaultAccessDelegationModeResolver.isCredentialVendingAvailable` returns `true` on null storage config - `CatalogEntity.Builder.setStorageConfigurationInfo` (`CatalogEntity.java:269+`) wraps its body in `if (storageConfigModel != null)`, confirming a catalog without storage config is a legal state at creation time Since `updateCatalog` calls `validateBaseLocationAgainstStorageConfig(currentCatalogEntity, ...)` and the current entity may legitimately have null storage config (per the creation path), the test exercises a real reachable scenario. The early-return preserves the same "no storage config = no allowed-location constraint" semantics used by the sibling validation methods. The single place where null storage config does throw is `IcebergCatalog.java:1107` for namespace creation — but that's a different invariant (no namespaces under a config-less catalog), not catalog-level location validation. Happy to add a brief code comment in the test referencing `validateUpdateCatalogDiffOrThrow` if it'd help future readers — WDYT? -- 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]
