diff --git a/changelog.d/purge-task-session-tags.md b/changelog.d/purge-task-session-tags.md new file mode 100644 index 00000000000..0fd265c6b03 --- /dev/null +++ b/changelog.d/purge-task-session-tags.md @@ -0,0 +1,12 @@ +### Fixes + +- The cleanup tasks scheduled by a drop with purge now vend STS credentials with the dropped table's + or view's catalog, namespace and table in the session tags and role session name + (`SESSION_TAGS_IN_SUBSCOPED_CREDENTIAL`, `SESSION_NAME_FIELDS_IN_SUBSCOPED_CREDENTIAL`). + Previously the task's name was used as the catalog and table, and the namespace was missing. + +### Upgrade notes + +- Cleanup tasks scheduled by a drop with purge before upgrading don't carry the catalog name, so + their STS credentials use `unknown` as the catalog session tag and leave the catalog out of the + role session name. diff --git a/polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisTaskConstants.java b/polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisTaskConstants.java index 9c0a8bd7c1d..76233ed5d27 100644 --- a/polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisTaskConstants.java +++ b/polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisTaskConstants.java @@ -39,4 +39,10 @@ public class PolarisTaskConstants { * {@link #STORAGE_LOCATION} when present. */ public static final String STORAGE_LOCATIONS = "storageLocations"; + + /** + * Name of the catalog the dropped table or view belonged to, used for the credential vending + * context (e.g. STS session tags) of purge tasks. + */ + public static final String CATALOG_NAME = "catalogName"; } diff --git a/runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/LocalIcebergCatalog.java b/runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/LocalIcebergCatalog.java index 5deb04c25a6..82c89b55c70 100644 --- a/runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/LocalIcebergCatalog.java +++ b/runtime/service/src/main/java/org/apache/polaris/service/catalog/iceberg/LocalIcebergCatalog.java @@ -3193,12 +3193,20 @@ private void updateTableLike( } } + Map cleanupProperties = storageProperties; + if (purge) { + // Cleanup tasks vend storage credentials outside of a catalog request, so they need the + // catalog name for the credential vending context (e.g. STS session tags). + cleanupProperties = new HashMap<>(storageProperties); + cleanupProperties.put(PolarisTaskConstants.CATALOG_NAME, catalogName); + } + return getMetaStoreManager() .dropEntityIfExists( getCurrentPolarisContext(), PolarisEntity.toCoreList(catalogPath), leafEntity, - storageProperties, + cleanupProperties, purge); } diff --git a/runtime/service/src/main/java/org/apache/polaris/service/catalog/io/StorageAccessConfigProvider.java b/runtime/service/src/main/java/org/apache/polaris/service/catalog/io/StorageAccessConfigProvider.java index 6b57b042480..cc357b8ce51 100644 --- a/runtime/service/src/main/java/org/apache/polaris/service/catalog/io/StorageAccessConfigProvider.java +++ b/runtime/service/src/main/java/org/apache/polaris/service/catalog/io/StorageAccessConfigProvider.java @@ -27,6 +27,7 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import org.apache.iceberg.catalog.Namespace; import org.apache.iceberg.catalog.TableIdentifier; import org.apache.polaris.core.StructuredLogKeys; import org.apache.polaris.core.auth.PolarisPrincipal; @@ -36,6 +37,7 @@ import org.apache.polaris.core.entity.PolarisEntity; import org.apache.polaris.core.entity.PolarisEntityType; import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.rest.NamespaceUtils; import org.apache.polaris.core.storage.CredentialVendingContext; import org.apache.polaris.core.storage.LocationGrant; import org.apache.polaris.core.storage.PolarisStorageActions; @@ -49,7 +51,9 @@ /** * Request-scoped entry point for vending scoped storage credentials. Resolves the storage * integration for the given entity path via {@link PolarisStorageIntegrationProvider}, builds a - * {@link CredentialVendingContext} from request-scoped state, and delegates to the integration. + * {@link CredentialVendingContext} from the resolved entity path and request-scoped state (or, for + * background tasks, from the catalog name and table identifier the task carries), and delegates to + * the integration. */ @RequestScoped public class StorageAccessConfigProvider { @@ -79,18 +83,75 @@ public StorageAccessConfig getStorageAccessConfig( @NonNull Set storageActions, @NonNull Optional refreshCredentialsEndpoint, @NonNull PolarisResolvedPathWrapper resolvedPath) { + List resolvedEntityPath = resolvedPath.getRawFullPath(); + return getStorageAccessConfig( + tableIdentifier, + resolvedEntityPath, + tableLocations, + storageActions, + refreshCredentialsEndpoint, + buildCredentialVendingContext(resolvedEntityPath)); + } + + /** + * Vends storage credentials for a background task acting on {@code tableIdentifier}. The + * credential vending context is built from {@code catalogName} and {@code tableIdentifier}; + * {@code resolvedTaskPath} is only used to look up the storage integration. + */ + public StorageAccessConfig getStorageAccessConfigForTask( + @NonNull Optional catalogName, + @NonNull TableIdentifier tableIdentifier, + @NonNull Set tableLocations, + @NonNull Set storageActions, + @NonNull PolarisResolvedPathWrapper resolvedTaskPath) { + return getStorageAccessConfig( + tableIdentifier, + resolvedTaskPath.getRawFullPath(), + tableLocations, + storageActions, + Optional.empty(), + buildCredentialVendingContext( + catalogName, tableIdentifier.namespace(), Optional.of(tableIdentifier.name()))); + } + + private StorageAccessConfig getStorageAccessConfig( + @NonNull TableIdentifier tableIdentifier, + @NonNull List resolvedEntityPath, + @NonNull Set tableLocations, + @NonNull Set storageActions, + @NonNull Optional refreshCredentialsEndpoint, + @NonNull CredentialVendingContext credentialVendingContext) { LOGGER .atDebug() .addKeyValue(StructuredLogKeys.TABLE_IDENTIFIER, tableIdentifier) .addKeyValue(StructuredLogKeys.TABLE_LOCATION, tableLocations) .log("Fetching client credentials for table"); - StorageAccessConfig accessConfig = - getStorageAccessConfig( - resolvedPath.getRawFullPath(), - tableLocations, - storageActions, - refreshCredentialsEndpoint); + boolean skipCredentialSubscopingIndirection = + callContext + .getRealmConfig() + .getConfig(FeatureConfiguration.SKIP_CREDENTIAL_SUBSCOPING_INDIRECTION); + PolarisStorageIntegration integration = + skipCredentialSubscopingIndirection + ? null + : storageIntegrationProvider.getStorageIntegration(resolvedEntityPath); + + StorageAccessConfig accessConfig; + if (integration == null) { + accessConfig = StorageAccessConfig.builder().supportsCredentialVending(false).build(); + } else { + // Non-delegated loadTable still calls in here to fetch storage extra-properties + // (endpoint/region/path-style) and passes no actions; treat that as a READ grant so + // the integration emits a well-formed inline policy rather than calling STS empty-handed. + Set effectiveActions = + storageActions.isEmpty() ? Set.of(PolarisStorageActions.READ) : storageActions; + + accessConfig = + integration.getStorageAccessConfig( + List.of(new LocationGrant(tableLocations, effectiveActions)), + refreshCredentialsEndpoint, + credentialVendingContext); + } LOGGER .atDebug() @@ -104,43 +165,40 @@ public StorageAccessConfig getStorageAccessConfig( return accessConfig; } - private StorageAccessConfig getStorageAccessConfig( - @NonNull List resolvedEntityPath, - @NonNull Set locations, - @NonNull Set storageActions, - @NonNull Optional refreshCredentialsEndpoint) { - - boolean skipCredentialSubscopingIndirection = - callContext - .getRealmConfig() - .getConfig(FeatureConfiguration.SKIP_CREDENTIAL_SUBSCOPING_INDIRECTION); - if (skipCredentialSubscopingIndirection) { - return StorageAccessConfig.builder().supportsCredentialVending(false).build(); - } + private CredentialVendingContext buildCredentialVendingContext( + List resolvedEntityPath) { + Optional catalogName = Optional.empty(); + Namespace namespace = Namespace.empty(); + Optional tableName = Optional.empty(); - PolarisStorageIntegration integration = - storageIntegrationProvider.getStorageIntegration(resolvedEntityPath); - if (integration == null) { - return StorageAccessConfig.builder().supportsCredentialVending(false).build(); - } + if (!resolvedEntityPath.isEmpty()) { + // First entity is the catalog; paths not rooted at a catalog leave it unset + PolarisEntity root = resolvedEntityPath.get(0); + if (root.getType() == PolarisEntityType.CATALOG) { + catalogName = Optional.of(root.getName()); + } - CredentialVendingContext credentialVendingContext = - buildCredentialVendingContext(resolvedEntityPath); + // Last entity is the table/view + PolarisEntity leaf = resolvedEntityPath.get(resolvedEntityPath.size() - 1); + if (leaf.getType() == PolarisEntityType.TABLE_LIKE) { + tableName = Optional.of(leaf.getName()); + } - // Non-delegated loadTable still calls in here to fetch storage extra-properties - // (endpoint/region/path-style) and passes no actions; treat that as a READ grant so - // the integration emits a well-formed inline policy rather than calling STS empty-handed. - Set effectiveActions = - storageActions.isEmpty() ? Set.of(PolarisStorageActions.READ) : storageActions; + // Namespace entities are between catalog and leaf + if (resolvedEntityPath.size() > 2) { + namespace = + Namespace.of( + resolvedEntityPath.subList(1, resolvedEntityPath.size() - 1).stream() + .map(PolarisEntity::getName) + .toArray(String[]::new)); + } + } - return integration.getStorageAccessConfig( - List.of(new LocationGrant(locations, effectiveActions)), - refreshCredentialsEndpoint, - credentialVendingContext); + return buildCredentialVendingContext(catalogName, namespace, tableName); } private CredentialVendingContext buildCredentialVendingContext( - List resolvedEntityPath) { + Optional catalogName, Namespace namespace, Optional tableName) { CredentialVendingContext.Builder builder = CredentialVendingContext.builder(); List sessionTagFields = @@ -149,27 +207,15 @@ private CredentialVendingContext buildCredentialVendingContext( .getConfig(FeatureConfiguration.SESSION_TAGS_IN_SUBSCOPED_CREDENTIAL); builder.realm(Optional.of(realmContext.getRealmIdentifier())); - - if (!resolvedEntityPath.isEmpty()) { - // First entity is the catalog - builder.catalogName(Optional.of(resolvedEntityPath.get(0).getName())); - - // Last entity is the table/view - PolarisEntity leaf = resolvedEntityPath.get(resolvedEntityPath.size() - 1); - if (leaf.getType() == PolarisEntityType.TABLE_LIKE - || leaf.getType() == PolarisEntityType.TASK) { - builder.tableName(Optional.of(leaf.getName())); - } - - // Namespace entities are between catalog and leaf - if (resolvedEntityPath.size() > 2) { - String namespace = - resolvedEntityPath.subList(1, resolvedEntityPath.size() - 1).stream() - .map(PolarisEntity::getName) - .collect(Collectors.joining(".")); - builder.namespace(Optional.of(namespace)); - } - } + builder.catalogName(catalogName); + // The single place where a namespace becomes the dot-separated string documented on + // CredentialVendingContext#namespace, so request and task paths stay consistent. Note that a + // level containing a dot is indistinguishable from nested levels in this value. + builder.namespace( + namespace.isEmpty() + ? Optional.empty() + : Optional.of(NamespaceUtils.joinNamespace(namespace, "."))); + builder.tableName(tableName); builder.principalName(Optional.of(polarisPrincipal.getName())); diff --git a/runtime/service/src/main/java/org/apache/polaris/service/task/TaskFileIOSupplier.java b/runtime/service/src/main/java/org/apache/polaris/service/task/TaskFileIOSupplier.java index b1f3c3b09f1..5f08f159bed 100644 --- a/runtime/service/src/main/java/org/apache/polaris/service/task/TaskFileIOSupplier.java +++ b/runtime/service/src/main/java/org/apache/polaris/service/task/TaskFileIOSupplier.java @@ -28,6 +28,7 @@ import org.apache.iceberg.CatalogProperties; import org.apache.iceberg.catalog.TableIdentifier; import org.apache.iceberg.io.FileIO; +import org.apache.polaris.core.entity.PolarisTaskConstants; import org.apache.polaris.core.entity.TaskEntity; import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; import org.apache.polaris.core.persistence.ResolvedPolarisEntity; @@ -58,9 +59,11 @@ public FileIO apply(TaskEntity task, TableIdentifier identifier) { new ResolvedPolarisEntity(task, List.of(), List.of()); PolarisResolvedPathWrapper resolvedPath = new PolarisResolvedPathWrapper(List.of(resolvedTaskEntity)); + Optional catalogName = + Optional.ofNullable(properties.get(PolarisTaskConstants.CATALOG_NAME)); StorageAccessConfig storageAccessConfig = - accessConfigProvider.getStorageAccessConfig( - identifier, locations, storageActions, Optional.empty(), resolvedPath); + accessConfigProvider.getStorageAccessConfigForTask( + catalogName, identifier, locations, storageActions, resolvedPath); String ioImpl = properties.getOrDefault( diff --git a/runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/AbstractLocalIcebergCatalogTest.java b/runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/AbstractLocalIcebergCatalogTest.java index bbecb834bbd..fc78f41b1d2 100644 --- a/runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/AbstractLocalIcebergCatalogTest.java +++ b/runtime/service/src/test/java/org/apache/polaris/service/catalog/iceberg/AbstractLocalIcebergCatalogTest.java @@ -133,6 +133,7 @@ import org.apache.polaris.core.entity.PolarisEntityCore; import org.apache.polaris.core.entity.PolarisEntitySubType; import org.apache.polaris.core.entity.PolarisEntityType; +import org.apache.polaris.core.entity.PolarisTaskConstants; import org.apache.polaris.core.entity.TaskEntity; import org.apache.polaris.core.entity.table.IcebergTableLikeEntity; import org.apache.polaris.core.exceptions.CommitConflictException; @@ -2075,6 +2076,8 @@ public void testDropTableWithPurge() { .getEntities(); Assertions.assertThat(tasks).hasSize(1); TaskEntity taskEntity = TaskEntity.of(tasks.get(0)); + Assertions.assertThat(taskEntity.getInternalPropertiesAsMap()) + .containsEntry(PolarisTaskConstants.CATALOG_NAME, CATALOG_NAME); var integration = storageIntegrationProvider.getStorageIntegration(List.of(taskEntity)); Map credentials = integration @@ -2123,6 +2126,8 @@ public void testDropViewWithPurge() { .getEntities() .getFirst()); Map properties = taskEntity.getInternalPropertiesAsMap(); + Assertions.assertThat(properties) + .containsEntry(PolarisTaskConstants.CATALOG_NAME, CATALOG_NAME); properties.put(CatalogProperties.FILE_IO_IMPL, "org.apache.iceberg.inmemory.InMemoryFileIO"); taskEntity = TaskEntity.of( diff --git a/runtime/service/src/test/java/org/apache/polaris/service/catalog/io/StorageAccessConfigProviderTest.java b/runtime/service/src/test/java/org/apache/polaris/service/catalog/io/StorageAccessConfigProviderTest.java new file mode 100644 index 00000000000..e3aaa4cc744 --- /dev/null +++ b/runtime/service/src/test/java/org/apache/polaris/service/catalog/io/StorageAccessConfigProviderTest.java @@ -0,0 +1,162 @@ +/* + * 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.catalog.io; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Stream; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.polaris.core.auth.PolarisPrincipal; +import org.apache.polaris.core.collection.AttributeMap; +import org.apache.polaris.core.config.RealmConfigImpl; +import org.apache.polaris.core.context.CallContext; +import org.apache.polaris.core.context.RealmContext; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntityType; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.ResolvedPolarisEntity; +import org.apache.polaris.core.storage.CredentialVendingContext; +import org.apache.polaris.core.storage.PolarisStorageActions; +import org.apache.polaris.core.storage.PolarisStorageIntegration; +import org.apache.polaris.core.storage.PolarisStorageIntegrationProvider; +import org.apache.polaris.core.storage.StorageAccessConfig; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.ArgumentCaptor; + +class StorageAccessConfigProviderTest { + + private static final TableIdentifier TABLE = + TableIdentifier.of(Namespace.of("ns1", "ns2"), "tbl"); + private static final Set LOCATIONS = Set.of("s3://bucket/path"); + private static final Set ACTIONS = Set.of(PolarisStorageActions.ALL); + + private PolarisStorageIntegration integration; + private PolarisStorageIntegrationProvider integrationProvider; + private StorageAccessConfigProvider provider; + + @BeforeEach + void setUp() { + RealmContext realmContext = () -> "test-realm"; + CallContext callContext = mock(CallContext.class); + when(callContext.getRealmConfig()) + .thenReturn(new RealmConfigImpl((rc, name) -> null, realmContext)); + + integration = mock(PolarisStorageIntegration.class); + when(integration.getStorageAccessConfig(any(), any(), any())) + .thenReturn(StorageAccessConfig.builder().build()); + integrationProvider = mock(PolarisStorageIntegrationProvider.class); + when(integrationProvider.getStorageIntegration(any())).thenReturn(integration); + + provider = + new StorageAccessConfigProvider( + callContext, + PolarisPrincipal.of("principal", AttributeMap.EMPTY, Set.of()), + realmContext, + integrationProvider); + } + + @Test + void testContextFromResolvedTablePath() { + PolarisResolvedPathWrapper tablePath = + resolvedPath( + entity(PolarisEntityType.CATALOG, "cat"), + entity(PolarisEntityType.NAMESPACE, "ns1"), + entity(PolarisEntityType.NAMESPACE, "ns2"), + entity(PolarisEntityType.TABLE_LIKE, "tbl")); + provider.getStorageAccessConfig(TABLE, LOCATIONS, ACTIONS, Optional.empty(), tablePath); + + verify(integrationProvider).getStorageIntegration(tablePath.getRawFullPath()); + + assertThat(capturedContext()) + .returns(Optional.of("cat"), CredentialVendingContext::catalogName) + .returns(Optional.of("ns1.ns2"), CredentialVendingContext::namespace) + .returns(Optional.of("tbl"), CredentialVendingContext::tableName); + } + + @Test + void testContextFromResolvedPathWithoutCatalogRoot() { + provider.getStorageAccessConfig( + TABLE, + LOCATIONS, + ACTIONS, + Optional.empty(), + resolvedPath(entity(PolarisEntityType.TASK, "entityCleanup_1"))); + + assertThat(capturedContext()) + .returns(Optional.empty(), CredentialVendingContext::catalogName) + .returns(Optional.empty(), CredentialVendingContext::namespace) + .returns(Optional.empty(), CredentialVendingContext::tableName); + } + + static Stream taskContexts() { + return Stream.of( + Arguments.of(Optional.of("cat"), TABLE, Optional.of("ns1.ns2")), + Arguments.of(Optional.empty(), TABLE, Optional.of("ns1.ns2")), + Arguments.of(Optional.of("cat"), TableIdentifier.of("tbl"), Optional.empty())); + } + + @ParameterizedTest + @MethodSource("taskContexts") + void testTaskContextFromCatalogNameAndTableIdentifier( + Optional catalogName, + TableIdentifier tableIdentifier, + Optional expectedNamespace) { + PolarisResolvedPathWrapper taskPath = + resolvedPath(entity(PolarisEntityType.TASK, "entityCleanup_1")); + provider.getStorageAccessConfigForTask( + catalogName, tableIdentifier, LOCATIONS, ACTIONS, taskPath); + + verify(integrationProvider).getStorageIntegration(taskPath.getRawFullPath()); + + assertThat(capturedContext()) + .returns(catalogName, CredentialVendingContext::catalogName) + .returns(expectedNamespace, CredentialVendingContext::namespace) + .returns(Optional.of("tbl"), CredentialVendingContext::tableName); + } + + private CredentialVendingContext capturedContext() { + ArgumentCaptor captor = + ArgumentCaptor.forClass(CredentialVendingContext.class); + verify(integration).getStorageAccessConfig(any(), any(), captor.capture()); + return captor.getValue(); + } + + private static PolarisEntity entity(PolarisEntityType type, String name) { + return new PolarisEntity.Builder().setType(type).setName(name).build(); + } + + private static PolarisResolvedPathWrapper resolvedPath(PolarisEntity... entities) { + return new PolarisResolvedPathWrapper( + Stream.of(entities) + .map(entity -> new ResolvedPolarisEntity(entity, List.of(), List.of())) + .toList()); + } +} diff --git a/runtime/service/src/test/java/org/apache/polaris/service/task/PurgeTaskSessionTagsTest.java b/runtime/service/src/test/java/org/apache/polaris/service/task/PurgeTaskSessionTagsTest.java new file mode 100644 index 00000000000..09e28d9c7a8 --- /dev/null +++ b/runtime/service/src/test/java/org/apache/polaris/service/task/PurgeTaskSessionTagsTest.java @@ -0,0 +1,291 @@ +/* + * 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.task; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.isA; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableMap; +import io.quarkus.test.junit.QuarkusMock; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import io.smallrye.common.annotation.Identifier; +import jakarta.inject.Inject; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.CatalogProperties; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.DataFiles; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.PartitionSpec; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.Namespace; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.inmemory.InMemoryFileIO; +import org.apache.iceberg.types.Types; +import org.apache.polaris.core.PolarisDiagnostics; +import org.apache.polaris.core.admin.model.AwsStorageConfigInfo; +import org.apache.polaris.core.admin.model.CreateCatalogRequest; +import org.apache.polaris.core.admin.model.StorageConfigInfo; +import org.apache.polaris.core.auth.PolarisAuthorizer; +import org.apache.polaris.core.auth.PolarisPrincipal; +import org.apache.polaris.core.auth.PolarisPrincipalAttributes; +import org.apache.polaris.core.collection.ImmutableAttributeMap; +import org.apache.polaris.core.config.RealmConfig; +import org.apache.polaris.core.context.CallContext; +import org.apache.polaris.core.entity.CatalogEntity; +import org.apache.polaris.core.entity.PrincipalEntity; +import org.apache.polaris.core.identity.provider.ServiceIdentityProvider; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.resolver.ResolutionManifestFactory; +import org.apache.polaris.core.persistence.resolver.ResolverFactory; +import org.apache.polaris.core.secrets.UserSecretsManager; +import org.apache.polaris.core.storage.CredentialVendingContext; +import org.apache.polaris.core.storage.cache.StorageCredentialCache; +import org.apache.polaris.core.storage.cache.StorageCredentialCacheKey; +import org.apache.polaris.service.Profiles; +import org.apache.polaris.service.admin.PolarisAdminService; +import org.apache.polaris.service.admin.PolarisAdminServiceTestSupport; +import org.apache.polaris.service.catalog.PolarisPassthroughResolutionView; +import org.apache.polaris.service.catalog.iceberg.LocalIcebergCatalog; +import org.apache.polaris.service.catalog.io.FileIOFactory; +import org.apache.polaris.service.catalog.io.StorageAccessConfigProvider; +import org.apache.polaris.service.config.ReservedProperties; +import org.apache.polaris.service.context.catalog.PolarisPrincipalHolder; +import org.apache.polaris.service.context.catalog.RealmContextHolder; +import org.apache.polaris.service.events.PolarisEventDispatcher; +import org.apache.polaris.service.events.PolarisEventMetadataFactory; +import org.apache.polaris.service.storage.aws.StsClientsPool; +import org.awaitility.Awaitility; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import software.amazon.awssdk.services.sts.StsClient; +import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; +import software.amazon.awssdk.services.sts.model.AssumeRoleResponse; +import software.amazon.awssdk.services.sts.model.Credentials; +import software.amazon.awssdk.services.sts.model.Tag; + +/** + * Verifies that the cleanup tasks scheduled by a drop-with-purge vend storage credentials with the + * same catalog-derived STS session tags and role session name as request-path credential vending. + * + *

Only the STS client pool and the credential cache are mocked; credential vending, the task + * executor and the table, manifest and batch cleanup task handlers are the real beans. + */ +@QuarkusTest +@TestProfile(PurgeTaskSessionTagsTest.Profile.class) +class PurgeTaskSessionTagsTest { + + public static class Profile extends Profiles.DefaultProfile { + @Override + public Map getConfigOverrides() { + return ImmutableMap.builder() + .putAll(super.getConfigOverrides()) + .put("polaris.features.\"DROP_WITH_PURGE_ENABLED\"", "true") + .put( + "polaris.features.\"SESSION_TAGS_IN_SUBSCOPED_CREDENTIAL\"", + "[\"catalog\",\"namespace\",\"table\"]") + .put("polaris.features.\"SESSION_NAME_FIELDS_IN_SUBSCOPED_CREDENTIAL\"", "[\"catalog\"]") + .build(); + } + } + + private static final String CATALOG_NAME = "test_catalog"; + private static final String STORAGE_LOCATION = "s3://my-bucket/path/to/data"; + private static final TableIdentifier TABLE = TableIdentifier.of(Namespace.of("ns"), "tbl"); + private static final Schema SCHEMA = + new Schema(Types.NestedField.required(1, "id", Types.IntegerType.get())); + + @Inject PolarisMetaStoreManager metaStoreManager; + @Inject CallContext callContext; + @Inject RealmConfig realmConfig; + @Inject RealmContextHolder realmContextHolder; + @Inject ResolutionManifestFactory resolutionManifestFactory; + @Inject ResolverFactory resolverFactory; + @Inject ServiceIdentityProvider serviceIdentityProvider; + @Inject UserSecretsManager userSecretsManager; + @Inject PolarisAuthorizer authorizer; + @Inject ReservedProperties reservedProperties; + @Inject PolarisPrincipalHolder principalHolder; + @Inject PolarisDiagnostics diagnostics; + @Inject StorageAccessConfigProvider storageAccessConfigProvider; + @Inject FileIOFactory fileIOFactory; + @Inject TaskExecutor taskExecutor; + @Inject PolarisEventDispatcher eventDispatcher; + @Inject PolarisEventMetadataFactory eventMetadataFactory; + + @Inject + @Identifier("task-error-handler") + TaskErrorHandler taskErrorHandler; + + private record StsCall(String thread, AssumeRoleRequest request) {} + + private final List stsCalls = new CopyOnWriteArrayList<>(); + private LocalIcebergCatalog catalog; + + @BeforeEach + void setUp() { + String realmName = "realm_" + System.nanoTime(); + realmContextHolder.set(() -> realmName); + PrincipalEntity rootEntity = + metaStoreManager.findRootPrincipal(callContext.getPolarisCallContext()).orElseThrow(); + PolarisPrincipal root = + PolarisPrincipal.of( + rootEntity.getName(), + ImmutableAttributeMap.builder() + .put(PolarisPrincipalAttributes.PRINCIPAL_ENTITY_ATTRIBUTE_KEY, rootEntity) + .build(), + Set.of("service_admin")); + principalHolder.set(root); + + StsClient stsClient = Mockito.mock(StsClient.class); + when(stsClient.assumeRole(isA(AssumeRoleRequest.class))) + .thenAnswer(inv -> { + stsCalls.add(new StsCall(Thread.currentThread().getName(), inv.getArgument(0))); + return AssumeRoleResponse.builder() + .credentials( + Credentials.builder() + .accessKeyId("ak") + .secretAccessKey("sk") + .sessionToken("st") + .build()) + .build(); + }); + StsClientsPool stsClientsPool = Mockito.mock(StsClientsPool.class); + when(stsClientsPool.stsClient(any())).thenReturn(stsClient); + QuarkusMock.installMockForType(stsClientsPool, StsClientsPool.class); + + // Bypass the credential cache so that every cleanup task calls STS: with correct session + // tags, the tasks' cache keys are identical and would otherwise share one AssumeRole call. + // A zero cache duration is not enough, since concurrent loads of one key are coalesced. + StorageCredentialCache credentialCache = Mockito.mock(StorageCredentialCache.class); + when(credentialCache.getOrLoad(any())) + .thenAnswer(inv -> inv.getArgument(0).load()); + QuarkusMock.installMockForType(credentialCache, StorageCredentialCache.class); + + AwsStorageConfigInfo storageConfig = + AwsStorageConfigInfo.builder() + .setRoleArn("arn:aws:iam::012345678901:role/test-role") + .setExternalId("externalId") + .setStorageType(StorageConfigInfo.StorageTypeEnum.S3) + .setAllowedLocations(List.of(STORAGE_LOCATION)) + .build(); + PolarisAdminService adminService = + PolarisAdminServiceTestSupport.newAdminService( + callContext, + resolutionManifestFactory, + metaStoreManager, + userSecretsManager, + serviceIdentityProvider, + root, + authorizer, + reservedProperties); + adminService.createCatalog( + new CreateCatalogRequest( + new CatalogEntity.Builder() + .setName(CATALOG_NAME) + .setDefaultBaseLocation(STORAGE_LOCATION) + .addProperty(CatalogProperties.FILE_IO_IMPL, InMemoryFileIO.class.getName()) + .setStorageConfigurationInfo(realmConfig, storageConfig) + .build() + .asCatalog(serviceIdentityProvider))); + + catalog = + new LocalIcebergCatalog( + diagnostics, + resolverFactory, + metaStoreManager, + callContext, + new PolarisPassthroughResolutionView(resolutionManifestFactory, root, CATALOG_NAME), + root, + taskExecutor, + storageAccessConfigProvider, + fileIOFactory, + eventDispatcher, + eventMetadataFactory); + catalog.initialize( + CATALOG_NAME, Map.of(CatalogProperties.FILE_IO_IMPL, InMemoryFileIO.class.getName())); + } + + @Test + void testPurgeTasksVendCredentialsWithCatalogSessionTags() { + catalog.createNamespace(TABLE.namespace()); + Table table = catalog.buildTable(TABLE, SCHEMA).create(); + // One data file gives the table a manifest and a manifest list, so the table cleanup task + // schedules both a manifest file cleanup task and a batch file cleanup task. + DataFile dataFile = + DataFiles.builder(PartitionSpec.unpartitioned()) + .withPath(table.location() + "/data/f1.parquet") + .withFormat(FileFormat.PARQUET) + .withFileSizeInBytes(10) + .withRecordCount(1) + .build(); + table.newFastAppend().appendFile(dataFile).commit(); + table.refresh(); + + List filesToPurge = new ArrayList<>(); + filesToPurge.add(((BaseTable) table).operations().current().metadataFileLocation()); + filesToPurge.add(table.currentSnapshot().manifestListLocation()); + table.currentSnapshot().allManifests(table.io()).forEach(m -> filesToPurge.add(m.path())); + InMemoryFileIO fileIO = new InMemoryFileIO(); + assertThat(filesToPurge).allMatch(fileIO::fileExists); + + stsCalls.clear(); + String testThread = Thread.currentThread().getName(); + assertThat(catalog.dropTable(TABLE, true)).isTrue(); + + Awaitility.await() + .atMost(Duration.ofSeconds(30)) + .until(() -> filesToPurge.stream().noneMatch(fileIO::fileExists)); + taskErrorHandler.assertNoTaskExceptions(); + + // The table, manifest file and batch file cleanup tasks each vend credentials on a task + // executor thread. Checking this keeps the assertions below from passing without any task + // credentials to inspect. + assertThat(stsCalls) + .filteredOn(call -> !call.thread().equals(testThread)) + .hasSizeGreaterThanOrEqualTo(3); + assertThat(stsCalls) + .as("(catalog, namespace, table session tags, role session name) of drop + purge calls") + .extracting( + call -> tagValue(call.request(), CredentialVendingContext.TAG_KEY_CATALOG), + call -> tagValue(call.request(), CredentialVendingContext.TAG_KEY_NAMESPACE), + call -> tagValue(call.request(), CredentialVendingContext.TAG_KEY_TABLE), + call -> call.request().roleSessionName()) + .containsOnly(tuple(CATALOG_NAME, "ns", "tbl", "p-" + CATALOG_NAME)); + } + + private static String tagValue(AssumeRoleRequest request, String key) { + return request.tags().stream() + .filter(tag -> tag.key().equals(key)) + .map(Tag::value) + .findFirst() + .orElse(null); + } +} diff --git a/runtime/service/src/test/java/org/apache/polaris/service/task/TableCleanupTaskHandlerTest.java b/runtime/service/src/test/java/org/apache/polaris/service/task/TableCleanupTaskHandlerTest.java index 89074d04459..896ac3d5c24 100644 --- a/runtime/service/src/test/java/org/apache/polaris/service/task/TableCleanupTaskHandlerTest.java +++ b/runtime/service/src/test/java/org/apache/polaris/service/task/TableCleanupTaskHandlerTest.java @@ -46,6 +46,7 @@ import org.apache.polaris.core.entity.PolarisBaseEntity; import org.apache.polaris.core.entity.PolarisEntitySubType; import org.apache.polaris.core.entity.PolarisEntityType; +import org.apache.polaris.core.entity.PolarisTaskConstants; import org.apache.polaris.core.entity.TaskEntity; import org.apache.polaris.core.entity.table.IcebergTableLikeEntity; import org.apache.polaris.core.persistence.MetaStoreManagerFactory; @@ -110,6 +111,7 @@ public void testTableCleanup() throws IOException { .setCatalogId(1) .setCreateTimestamp(100) .build()) + .addInternalProperty(PolarisTaskConstants.CATALOG_NAME, "my_catalog") .build(); task = addTaskLocation(task); Assertions.assertThatPredicate(handler::canHandleTask).accepts(task); @@ -126,6 +128,12 @@ public void testTableCleanup() throws IOException { assertThat(taskEntity) .returns(PolarisEntityType.TASK.getCode(), PolarisBaseEntity::getTypeCode) .extracting(TaskEntity::of) + .returns( + "my_catalog", + entity -> + entity + .getInternalPropertiesAsMap() + .get(PolarisTaskConstants.CATALOG_NAME)) .returns(AsyncTaskType.MANIFEST_FILE_CLEANUP, TaskEntity::getTaskType) .returns( ManifestFileCleanupTaskHandler.ManifestCleanupTask.buildFrom( @@ -137,6 +145,12 @@ public void testTableCleanup() throws IOException { assertThat(taskEntity) .returns(PolarisEntityType.TASK.getCode(), PolarisBaseEntity::getTypeCode) .extracting(TaskEntity::of) + .returns( + "my_catalog", + entity -> + entity + .getInternalPropertiesAsMap() + .get(PolarisTaskConstants.CATALOG_NAME)) .returns(AsyncTaskType.BATCH_FILE_CLEANUP, TaskEntity::getTaskType) .returns( new BatchFileCleanupTaskHandler.BatchFileCleanupTask(