This is an automated email from the ASF dual-hosted git repository.
roryqi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new 27efb7cb4c [#13383] fix(iceberg): Fix alter table metadata read under
Kerberos impersonation (#13384)
27efb7cb4c is described below
commit 27efb7cb4c4342704bb1acc52061be8a231479eb
Author: ahrilee <[email protected]>
AuthorDate: Thu Nov 12 20:03:55 2026 +0900
[#13383] fix(iceberg): Fix alter table metadata read under Kerberos
impersonation (#13384)
### What changes were proposed in this pull request?
Move the `loadTable()` call inside the Kerberos proxy boundary during
`alterTable`.
- Add an overload of `buildIcebergTableChanges()` in
IcebergCatalogWrapperHelper
that takes an explicit Catalog instead of the internally held raw one.
- Add `buildAndUpdateTable()` to IcebergCatalogWrapper, which passes
`getCatalog()`
to the builder and commits the transaction in a single proxied call.
- Update `internalUpdateTable()` in IcebergCatalogOperations to use it.
### Why are the changes needed?
IcebergCatalogWrapperHelper holds the raw Catalog obtained via
`getCatalog()` at
init time. When Kerberos impersonation is enabled,
`buildIcebergTableChanges()`
calls `loadTable()` on that raw object, bypassing
KerberosAwareIcebergCatalogProxy
and therefore `UGI.doAs()`.
The metadata.json read then runs as the service principal instead of the
impersonated user, failing with AccessControlException on secured HDFS
clusters.
Fix: #13383
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Verified on a Kerberos-secured HDFS cluster where the service account
has no
access to the warehouse directory. `ALTER TABLE ADD COLUMNS` now
succeeds under
impersonation, and the HDFS audit log shows the user identity for the
metadata
read, directory creation and new metadata file write.
---------
Co-authored-by: roryqi <[email protected]>
---
.../catalog/lakehouse/iceberg/IcebergBackend.java | 11 +-
.../iceberg/IcebergCatalogOperations.java | 13 +--
.../iceberg/ops/IcebergCatalogWrapperHelper.java | 22 ++--
.../iceberg/ops/TestIcebergTableUpdate.java | 12 +--
.../iceberg/common/ops/IcebergCatalogWrapper.java | 40 +++++++-
.../common/ops/TestIcebergCatalogWrapper.java | 114 ++++++++++++++++++++-
.../ops/TestKerberosAwareIcebergCatalogProxy.java | 87 ++++++++++++++++
7 files changed, 271 insertions(+), 28 deletions(-)
diff --git
a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.java
b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.java
index 2bf7d4aa8a..1fbb606970 100644
---
a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.java
+++
b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergBackend.java
@@ -29,6 +29,7 @@ import
org.apache.gravitino.iceberg.common.authentication.SupportsKerberos;
import org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper;
import
org.apache.gravitino.iceberg.common.ops.KerberosAwareIcebergCatalogProxy;
import org.apache.gravitino.iceberg.common.utils.IcebergCatalogUtil;
+import org.apache.iceberg.catalog.Catalog;
import org.apache.iceberg.exceptions.NoSuchWarehouseException;
import org.apache.iceberg.exceptions.RESTException;
import org.slf4j.Logger;
@@ -115,13 +116,17 @@ final class IcebergBackend implements AutoCloseable {
IcebergCatalogUtil.withGcsServiceAccountCredentials(new
IcebergConfig(conf));
try {
IcebergCatalogWrapper rawWrapper = new
IcebergCatalogWrapper(icebergConfig);
+ // Bootstrap the catalog before creating the proxy: initialize() creates
the KerberosClient
+ // needed by doKerberosOperations(). The previous Kerberos capability
check also initialized
+ // the raw catalog here. Table metadata reads happen later through the
proxied wrapper.
+ // Keep eager initialization so configuration errors are reported during
reload.
+ Catalog catalog = rawWrapper.getCatalog();
AuthenticationConfig authenticationConfig = new
AuthenticationConfig(catalogConf);
this.wrapper =
- authenticationConfig.isKerberosAuth()
- && rawWrapper.getCatalog() instanceof SupportsKerberos
+ authenticationConfig.isKerberosAuth() && catalog instanceof
SupportsKerberos
? new KerberosAwareIcebergCatalogProxy(rawWrapper).getProxy()
: rawWrapper;
- this.helper = new IcebergCatalogWrapperHelper(wrapper.getCatalog());
+ this.helper = new IcebergCatalogWrapperHelper();
this.views = new IcebergViewCatalogOperations(wrapper);
} catch (NoSuchWarehouseException e) {
throw new IllegalArgumentException(
diff --git
a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
index c21f613ca8..cf5fc8cdb4 100644
---
a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
+++
b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogOperations.java
@@ -52,7 +52,6 @@ import
org.apache.gravitino.exceptions.NonEmptySchemaException;
import org.apache.gravitino.exceptions.SchemaAlreadyExistsException;
import org.apache.gravitino.exceptions.TableAlreadyExistsException;
import org.apache.gravitino.exceptions.ViewAlreadyExistsException;
-import
org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper.IcebergTableChange;
import org.apache.gravitino.iceberg.common.utils.IcebergCatalogUtil;
import org.apache.gravitino.iceberg.common.utils.IcebergIdentifierUtils;
import org.apache.gravitino.meta.AuditInfo;
@@ -466,12 +465,14 @@ public class IcebergCatalogOperations
throws NoSuchTableException, IllegalArgumentException {
try {
String[] levels = tableIdent.namespace().levels();
- IcebergTableChange icebergTableChange =
+ NameIdentifier schemaTableIdent =
+ NameIdentifier.of(levels[levels.length - 1], tableIdent.name());
+ IcebergCatalogWrapperHelper helper = backend.helper();
+ LoadTableResponse loadTableResponse =
backend
- .helper()
- .buildIcebergTableChanges(
- NameIdentifier.of(levels[levels.length - 1],
tableIdent.name()), changes);
- LoadTableResponse loadTableResponse =
backend.wrapper().updateTable(icebergTableChange);
+ .wrapper()
+ .buildAndUpdateTable(
+ catalog -> helper.buildIcebergTableChanges(catalog,
schemaTableIdent, changes));
loadTableResponse.validate();
return IcebergTable.fromIcebergTable(loadTableResponse.tableMetadata(),
tableIdent.name());
} catch (ValidationException e) {
diff --git
a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergCatalogWrapperHelper.java
b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergCatalogWrapperHelper.java
index 413cd4f4bf..20c3032e00 100644
---
a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergCatalogWrapperHelper.java
+++
b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/IcebergCatalogWrapperHelper.java
@@ -59,6 +59,7 @@ import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.types.Type.PrimitiveType;
import org.apache.iceberg.types.Types.NestedField;
+/** Converts Gravitino table changes and identifiers to Iceberg operations. */
public class IcebergCatalogWrapperHelper {
@VisibleForTesting public static final Joiner DOT = Joiner.on(".");
private static final Set<String> IcebergReservedProperties =
@@ -71,12 +72,6 @@ public class IcebergCatalogWrapperHelper {
"sort-order",
"identifier-fields");
- private Catalog icebergCatalog;
-
- public IcebergCatalogWrapperHelper(Catalog icebergCatalog) {
- this.icebergCatalog = icebergCatalog;
- }
-
private void doDeleteColumn(
UpdateSchema icebergUpdateSchema, DeleteColumn deleteColumn, Schema
icebergTableSchema) {
NestedField deleteField =
icebergTableSchema.findField(DOT.join(deleteColumn.fieldName()));
@@ -233,8 +228,19 @@ public class IcebergCatalogWrapperHelper {
icebergUpdateSchema.commit();
}
+ /**
+ * Builds an {@link IcebergTableChange} using the given catalog to load the
table. Call this
+ * method inside {@code IcebergCatalogWrapper.buildAndUpdateTable} so that
the metadata.json read
+ * and transaction construction run within the proxy's Kerberos
impersonation context. Passing a
+ * catalog alone does not establish that context.
+ *
+ * @param catalog the Iceberg catalog to use for loading the table
+ * @param gravitinoNameIdentifier the Gravitino name identifier of the table
+ * @param tableChanges the changes to apply
+ * @return the built table change containing the transaction
+ */
public IcebergTableChange buildIcebergTableChanges(
- NameIdentifier gravitinoNameIdentifier, TableChange... tableChanges) {
+ Catalog catalog, NameIdentifier gravitinoNameIdentifier, TableChange...
tableChanges) {
TableIdentifier icebergTableIdentifier =
buildIcebergTableIdentifier(gravitinoNameIdentifier);
@@ -266,7 +272,7 @@ public class IcebergCatalogWrapperHelper {
}
}
- Table icebergBaseTable = icebergCatalog.loadTable(icebergTableIdentifier);
+ Table icebergBaseTable = catalog.loadTable(icebergTableIdentifier);
Transaction transaction = icebergBaseTable.newTransaction();
IcebergTableChange icebergTableChange =
new IcebergTableChange(icebergTableIdentifier, transaction);
diff --git
a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/TestIcebergTableUpdate.java
b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/TestIcebergTableUpdate.java
index 34cd91a7f7..a3985f3802 100644
---
a/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/TestIcebergTableUpdate.java
+++
b/catalogs/catalog-lakehouse-iceberg/src/test/java/org/apache/gravitino/catalog/lakehouse/iceberg/ops/TestIcebergTableUpdate.java
@@ -24,7 +24,6 @@ import java.util.stream.Collectors;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.iceberg.common.IcebergConfig;
import org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper;
-import
org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper.IcebergTableChange;
import org.apache.gravitino.rel.TableChange;
import org.apache.gravitino.rel.TableChange.ColumnPosition;
import org.apache.gravitino.rel.types.Types;
@@ -83,18 +82,17 @@ public class TestIcebergTableUpdate {
@BeforeEach
public void init() {
icebergCatalogWrapper = new IcebergCatalogWrapper(new
IcebergConfig(Collections.emptyMap()));
- icebergCatalogWrapperHelper =
- new IcebergCatalogWrapperHelper(icebergCatalogWrapper.getCatalog());
+ icebergCatalogWrapperHelper = new IcebergCatalogWrapperHelper();
createNamespace(TEST_NAMESPACE_NAME);
createTable(TEST_NAMESPACE_NAME, TEST_TABLE_NAME);
}
public LoadTableResponse updateTable(
NameIdentifier gravitinoNameIdentifier, TableChange...
gravitinoTableChanges) {
- IcebergTableChange icebergTableChange =
- icebergCatalogWrapperHelper.buildIcebergTableChanges(
- gravitinoNameIdentifier, gravitinoTableChanges);
- return icebergCatalogWrapper.updateTable(icebergTableChange);
+ return icebergCatalogWrapper.buildAndUpdateTable(
+ catalog ->
+ icebergCatalogWrapperHelper.buildIcebergTableChanges(
+ catalog, gravitinoNameIdentifier, gravitinoTableChanges));
}
private void createNamespace(String namespace) {
diff --git
a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
index 167b3bdc65..673e804ffa 100644
---
a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
+++
b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
@@ -25,6 +25,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import java.util.function.Function;
import javax.annotation.Nullable;
import lombok.Getter;
import lombok.Setter;
@@ -477,12 +478,49 @@ public class IcebergCatalogWrapper implements
AutoCloseable {
return loadTableResponse;
}
+ /**
+ * Commits an already built table change and reloads the table metadata.
+ *
+ * <p>This method does not establish an impersonation context for the
earlier table load or
+ * transaction construction. Callers that require Kerberos impersonation
must build changes inside
+ * {@link #buildAndUpdateTable(Function)} on the proxied wrapper instead of
building them with
+ * {@link #getCatalog()} before calling this method.
+ *
+ * @param icebergTableChange the table change to commit
+ * @return the response after committing the table change
+ */
public LoadTableResponse updateTable(IcebergTableChange icebergTableChange) {
Transaction transaction = icebergTableChange.getTransaction();
- transaction.commitTransaction();
+ try {
+ transaction.commitTransaction();
+ } finally {
+ // A load during the commit can cache old metadata, even if the commit
ultimately fails.
+ getMetadataCache().invalidate(icebergTableChange.getTableIdentifier());
+ }
return loadTable(icebergTableChange.getTableIdentifier());
}
+ /**
+ * Builds an {@link IcebergTableChange} and commits it within a single
proxied call. When this
+ * wrapper is behind the {@link KerberosAwareIcebergCatalogProxy}, the
entire operation, including
+ * the metadata.json read triggered by {@code loadTable()} inside the
builder, runs under the
+ * impersonated user's UGI context.
+ *
+ * <p>The catalog passed to the builder is the same underlying instance
returned by {@link
+ * #getCatalog()}; it is not itself proxied. The builder must execute inside
this wrapper method
+ * so that table loading and transaction construction remain within the
proxy's Kerberos boundary.
+ * Moving the builder call outside this method would bypass impersonation
even when using the same
+ * catalog.
+ *
+ * @param builder a function that uses the underlying catalog to build an
{@link
+ * IcebergTableChange}
+ * @return the response after committing the table change
+ */
+ public LoadTableResponse buildAndUpdateTable(Function<Catalog,
IcebergTableChange> builder) {
+ IcebergTableChange change = builder.apply(getCatalog());
+ return updateTable(change);
+ }
+
public LoadViewResponse createView(Namespace namespace, CreateViewRequest
request) {
request.validate();
return CatalogHandlers.createView(getViewCatalog(), namespace, request);
diff --git
a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.java
b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.java
index 16babdc396..2d37a9e601 100644
---
a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.java
+++
b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestIcebergCatalogWrapper.java
@@ -33,10 +33,12 @@ import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants;
import org.apache.gravitino.iceberg.common.IcebergConfig;
import org.apache.gravitino.iceberg.common.cache.SupportsMetadataLocation;
import org.apache.gravitino.iceberg.common.cache.TableMetadataCache;
+import
org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper.IcebergTableChange;
import org.apache.gravitino.iceberg.common.utils.IcebergCatalogUtil;
import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.DataFiles;
@@ -47,6 +49,7 @@ import org.apache.iceberg.Schema;
import org.apache.iceberg.Table;
import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.TableMetadataParser;
+import org.apache.iceberg.Transaction;
import org.apache.iceberg.catalog.Catalog;
import org.apache.iceberg.catalog.Namespace;
import org.apache.iceberg.catalog.TableIdentifier;
@@ -61,6 +64,7 @@ import
org.apache.iceberg.rest.requests.ImmutableRegisterViewRequest;
import org.apache.iceberg.rest.requests.RegisterTableRequest;
import org.apache.iceberg.rest.requests.RegisterViewRequest;
import org.apache.iceberg.rest.requests.UpdateTableRequest;
+import org.apache.iceberg.rest.responses.LoadTableResponse;
import org.apache.iceberg.rest.responses.LoadViewResponse;
import org.apache.iceberg.types.Types;
import org.apache.iceberg.view.ViewOperations;
@@ -403,6 +407,105 @@ public class TestIcebergCatalogWrapper {
}
}
+ @ParameterizedTest
+ @ValueSource(booleans = {true, false})
+ void testTransactionUpdateInvalidatesMetadataCachedDuringCommit(
+ boolean useBuilder, @TempDir Path warehouseDir) throws Exception {
+ try (IcebergCatalogWrapper wrapper =
+ new IcebergCatalogWrapper(new
IcebergConfig(metadataConfig(warehouseDir)))) {
+ Namespace namespace = Namespace.of("db");
+ TableIdentifier identifier = TableIdentifier.of(namespace, "t");
+
wrapper.createNamespace(CreateNamespaceRequest.builder().withNamespace(namespace).build());
+ wrapper.createTable(
+ namespace,
+ CreateTableRequest.builder()
+ .withName("t")
+ .withSchema(new Schema(Types.NestedField.required(1, "id",
Types.LongType.get())))
+ .build());
+ TableMetadataCache cache = invokeGetMetadataCache(wrapper);
+ Assertions.assertTrue(cache.getTableMetadata(identifier).isPresent());
+ Assertions.assertFalse(
+
cache.getTableMetadata(identifier).get().properties().containsKey("key"));
+
+ Function<Catalog, IcebergTableChange> builder =
+ catalog -> {
+ Transaction transaction =
catalog.loadTable(identifier).newTransaction();
+ transaction.updateProperties().set("key", "value").commit();
+ Transaction committingTransaction =
Mockito.mock(Transaction.class);
+ Mockito.doAnswer(
+ invocation -> {
+ // Reproduce a concurrent load before the commit becomes
visible.
+ Assertions.assertFalse(
+ wrapper
+ .loadTable(identifier)
+ .tableMetadata()
+ .properties()
+ .containsKey("key"));
+
Assertions.assertTrue(cache.getTableMetadata(identifier).isPresent());
+ transaction.commitTransaction();
+ return null;
+ })
+ .when(committingTransaction)
+ .commitTransaction();
+ return new IcebergTableChange(identifier, committingTransaction);
+ };
+ LoadTableResponse response =
+ useBuilder
+ ? wrapper.buildAndUpdateTable(builder)
+ : wrapper.updateTable(builder.apply(wrapper.getCatalog()));
+
+ Assertions.assertEquals("value",
response.tableMetadata().properties().get("key"));
+ Assertions.assertEquals(
+ "value",
wrapper.loadTable(identifier).tableMetadata().properties().get("key"));
+ Assertions.assertEquals(
+ "value",
cache.getTableMetadata(identifier).get().properties().get("key"));
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {true, false})
+ void testTransactionUpdateInvalidatesMetadataAfterCommitFailure(
+ boolean useBuilder, @TempDir Path warehouseDir) throws Exception {
+ try (IcebergCatalogWrapper wrapper =
+ new IcebergCatalogWrapper(new
IcebergConfig(metadataConfig(warehouseDir)))) {
+ Namespace namespace = Namespace.of("db");
+ TableIdentifier identifier = TableIdentifier.of(namespace, "t");
+
wrapper.createNamespace(CreateNamespaceRequest.builder().withNamespace(namespace).build());
+ wrapper.createTable(
+ namespace,
+ CreateTableRequest.builder()
+ .withName("t")
+ .withSchema(new Schema(Types.NestedField.required(1, "id",
Types.LongType.get())))
+ .build());
+ TableMetadataCache cache = invokeGetMetadataCache(wrapper);
+ RuntimeException failure = new RuntimeException("Commit failed");
+ Transaction transaction = Mockito.mock(Transaction.class);
+ Mockito.doAnswer(
+ invocation -> {
+ wrapper.loadTable(identifier);
+
Assertions.assertTrue(cache.getTableMetadata(identifier).isPresent());
+ throw failure;
+ })
+ .when(transaction)
+ .commitTransaction();
+ IcebergTableChange change = new IcebergTableChange(identifier,
transaction);
+
+ RuntimeException actual =
+ Assertions.assertThrows(
+ RuntimeException.class,
+ () -> {
+ if (useBuilder) {
+ wrapper.buildAndUpdateTable(catalog -> change);
+ } else {
+ wrapper.updateTable(change);
+ }
+ });
+
+ Assertions.assertSame(failure, actual);
+ Assertions.assertFalse(cache.getTableMetadata(identifier).isPresent());
+ }
+ }
+
/**
* Request-supplied metadata-location miss on registerTable → BadRequest,
not Iceberg NotFound.
*/
@@ -599,6 +702,7 @@ public class TestIcebergCatalogWrapper {
public static class TrackingTableMetadataCache implements TableMetadataCache
{
private static final AtomicInteger INITIALIZE_COUNT = new AtomicInteger();
private static final AtomicBoolean CLOSED = new AtomicBoolean();
+ private final Map<TableIdentifier, TableMetadata> metadata = new
HashMap<>();
static void reset() {
INITIALIZE_COUNT.set(0);
@@ -615,15 +719,19 @@ public class TestIcebergCatalogWrapper {
}
@Override
- public void invalidate(TableIdentifier tableIdentifier) {}
+ public void invalidate(TableIdentifier tableIdentifier) {
+ metadata.remove(tableIdentifier);
+ }
@Override
public Optional<TableMetadata> getTableMetadata(TableIdentifier
tableIdentifier) {
- return Optional.empty();
+ return Optional.ofNullable(metadata.get(tableIdentifier));
}
@Override
- public void updateTableMetadata(TableIdentifier tableIdentifier,
TableMetadata tableMetadata) {}
+ public void updateTableMetadata(TableIdentifier tableIdentifier,
TableMetadata tableMetadata) {
+ metadata.put(tableIdentifier, tableMetadata);
+ }
@Override
public void close() throws IOException {
diff --git
a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestKerberosAwareIcebergCatalogProxy.java
b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestKerberosAwareIcebergCatalogProxy.java
index 1f05b0d7bd..7497f81d80 100644
---
a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestKerberosAwareIcebergCatalogProxy.java
+++
b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/ops/TestKerberosAwareIcebergCatalogProxy.java
@@ -21,14 +21,23 @@ package org.apache.gravitino.iceberg.common.ops;
import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.gravitino.iceberg.common.IcebergConfig;
import org.apache.gravitino.iceberg.common.authentication.SupportsKerberos;
+import
org.apache.gravitino.iceberg.common.authentication.SupportsKerberos.Executable;
+import
org.apache.gravitino.iceberg.common.ops.IcebergCatalogWrapper.IcebergTableChange;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.Transaction;
import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.TableIdentifier;
import org.apache.iceberg.inmemory.InMemoryCatalog;
+import org.apache.iceberg.rest.responses.LoadTableResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.mockito.InOrder;
+import org.mockito.Mockito;
public class TestKerberosAwareIcebergCatalogProxy {
@@ -105,6 +114,12 @@ public class TestKerberosAwareIcebergCatalogProxy {
Assertions.assertInstanceOf(CountingWrapper.class, proxy);
Assertions.assertEquals(1, WRAPPER_CONSTRUCTIONS.get());
Assertions.assertEquals(1, CATALOG_CONSTRUCTIONS.get());
+ // Catalog initialization bootstraps authentication before any proxied
operation can use it.
+ Assertions.assertEquals(0, KERBEROS_OPERATIONS.get());
+
+ Assertions.assertSame(target.getCatalog(), proxy.getCatalog());
+ Assertions.assertEquals(1, CATALOG_CONSTRUCTIONS.get());
+ Assertions.assertEquals(1, KERBEROS_OPERATIONS.get());
}
@Test
@@ -116,6 +131,78 @@ public class TestKerberosAwareIcebergCatalogProxy {
Assertions.assertTrue(KERBEROS_OPERATIONS.get() > 0);
}
+ @Test
+ void testBuildAndUpdateTableRunsInsideKerberosContext() throws Throwable {
+ Catalog catalog =
+ Mockito.mock(Catalog.class,
Mockito.withSettings().extraInterfaces(SupportsKerberos.class));
+ AtomicBoolean insideKerberos = new AtomicBoolean();
+ Mockito.doAnswer(
+ invocation -> {
+ Assertions.assertFalse(insideKerberos.getAndSet(true));
+ try {
+ return invocation.<Executable<?>>getArgument(0).execute();
+ } finally {
+ insideKerberos.set(false);
+ }
+ })
+ .when((SupportsKerberos) catalog)
+ .doKerberosOperations(Mockito.any());
+
+ TableIdentifier identifier = TableIdentifier.of("db", "table");
+ Table table = Mockito.mock(Table.class);
+ Transaction transaction = Mockito.mock(Transaction.class);
+ LoadTableResponse response = Mockito.mock(LoadTableResponse.class);
+ Mockito.when(catalog.loadTable(identifier))
+ .thenAnswer(
+ invocation -> {
+ Assertions.assertTrue(insideKerberos.get(), "Metadata must load
under Kerberos");
+ return table;
+ });
+ Mockito.when(table.newTransaction())
+ .thenAnswer(
+ invocation -> {
+ Assertions.assertTrue(insideKerberos.get(), "Transaction must
start under Kerberos");
+ return transaction;
+ });
+ Mockito.doAnswer(
+ invocation -> {
+ Assertions.assertTrue(insideKerberos.get(), "Transaction must
commit under Kerberos");
+ return null;
+ })
+ .when(transaction)
+ .commitTransaction();
+
+ try (IcebergCatalogWrapper target = Mockito.spy(new
IcebergCatalogWrapper(memoryConfig()))) {
+ Mockito.doReturn(catalog).when(target).getCatalog();
+ Mockito.doAnswer(
+ invocation -> {
+ Assertions.assertTrue(insideKerberos.get(), "Response must
reload under Kerberos");
+ return response;
+ })
+ .when(target)
+ .loadTable(identifier);
+ IcebergCatalogWrapper proxy = new
KerberosAwareIcebergCatalogProxy(target).getProxy();
+
+ LoadTableResponse actual =
+ proxy.buildAndUpdateTable(
+ builderCatalog -> {
+ Assertions.assertTrue(insideKerberos.get(), "Builder must run
under Kerberos");
+ Assertions.assertSame(catalog, builderCatalog);
+ return new IcebergTableChange(
+ identifier,
builderCatalog.loadTable(identifier).newTransaction());
+ });
+
+ Assertions.assertSame(response, actual);
+ Assertions.assertFalse(insideKerberos.get());
+ Mockito.verify((SupportsKerberos)
catalog).doKerberosOperations(Mockito.any());
+ InOrder order = Mockito.inOrder(catalog, table, transaction, target);
+ order.verify(catalog).loadTable(identifier);
+ order.verify(table).newTransaction();
+ order.verify(transaction).commitTransaction();
+ order.verify(target).loadTable(identifier);
+ }
+ }
+
@Test
public void testCloseProxyClosesTargetCatalogOnce() throws Exception {
CountingWrapper target = new CountingWrapper(memoryConfig());