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 16ad409cd9 [#14406] fix(iceberg): Skip the HMS delegation token when 
impersonation is disabled (#14408)
16ad409cd9 is described below

commit 16ad409cd937215727b52f28ae6e903f746f296c
Author: YangJie <[email protected]>
AuthorDate: Sun Nov 1 21:13:50 2026 -0500

    [#14406] fix(iceberg): Skip the HMS delegation token when impersonation is 
disabled (#14408)
    
    ### What changes were proposed in this pull request?
    
    When impersonation is disabled,
    `ClosableHiveCatalog.doKerberosOperations` runs the operation as the
    Kerberos login user without requesting a delegation token, as
    `ClosableJdbcCatalog` already does through
    `KerberosCatalogUtils.doKerberosOperations`. The impersonation path is
    unchanged.
    
    ### Why are the changes needed?
    
    All requests share the login UGI, so it must not carry any caller's
    token. Operations that run as the login user do not need the token
    either. As a side effect, a catalog without impersonation no longer
    needs an HMS proxy-user entry for the Gravitino principal, and each
    operation saves one `get_delegation_token` call.
    
    Fix: #14406
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    New `TestClosableHiveCatalog`: without impersonation, no token is
    requested and the login UGI holds no token; with impersonation, the
    token is requested for the proxied principal and lands on the proxy UGI
    only.
---
 .../iceberg/common/ClosableHiveCatalog.java        |   5 +
 .../iceberg/common/TestClosableHiveCatalog.java    | 120 +++++++++++++++++++++
 2 files changed, 125 insertions(+)

diff --git 
a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java
 
b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java
index b93eacd2ba..6b1c98b65d 100644
--- 
a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java
+++ 
b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ClosableHiveCatalog.java
@@ -124,6 +124,11 @@ public class ClosableHiveCatalog extends HiveCatalog 
implements Closeable, Suppo
           "Kerberos is configured but KerberosClient is not initialized");
     }
 
+    if (!authenticationConfig.isImpersonationEnabled()) {
+      // The login user is shared by all requests, so it must not carry any 
user's token.
+      return KerberosCatalogUtils.executeAs(kerberosClient.getLoginUser(), 
executable);
+    }
+
     String finalPrincipalName = 
KerberosCatalogUtils.resolveProxyPrincipal(kerberosClient);
     UserGroupInformation realUser =
         KerberosCatalogUtils.createRealUser(
diff --git 
a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/TestClosableHiveCatalog.java
 
b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/TestClosableHiveCatalog.java
new file mode 100644
index 0000000000..a9c2f9f4f9
--- /dev/null
+++ 
b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/TestClosableHiveCatalog.java
@@ -0,0 +1,120 @@
+/*
+ * 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.iceberg.common;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.UserPrincipal;
+import org.apache.gravitino.catalog.hadoop.auth.KerberosClient;
+import org.apache.gravitino.iceberg.common.authentication.AuthenticationConfig;
+import org.apache.gravitino.utils.PrincipalUtils;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.thrift.DelegationTokenIdentifier;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.security.token.Token;
+import org.apache.iceberg.ClientPool;
+import org.apache.thrift.TException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+public class TestClosableHiveCatalog {
+
+  private UserGroupInformation loginUser;
+  private IMetaStoreClient metaStoreClient;
+
+  @BeforeEach
+  void setUp() throws Exception {
+    loginUser = UserGroupInformation.createRemoteUser("gravitino");
+    metaStoreClient = Mockito.mock(IMetaStoreClient.class);
+    Mockito.when(metaStoreClient.getDelegationToken(anyString(), anyString()))
+        .thenReturn(new 
Token<DelegationTokenIdentifier>().encodeToUrlString());
+  }
+
+  @Test
+  void testNoTokenWithoutImpersonation() throws Throwable {
+    ClosableHiveCatalog catalog = newCatalog(false);
+
+    String user = runAsAlice(catalog);
+
+    Assertions.assertEquals("gravitino", user);
+    Mockito.verify(metaStoreClient, 
Mockito.never()).getDelegationToken(anyString(), anyString());
+    Assertions.assertEquals(0, loginUser.getCredentials().numberOfTokens());
+  }
+
+  @Test
+  void testTokenOnProxyUserWithImpersonation() throws Throwable {
+    ClosableHiveCatalog catalog = newCatalog(true);
+
+    String user = runAsAlice(catalog);
+
+    Assertions.assertEquals("[email protected]", user);
+    Mockito.verify(metaStoreClient).getDelegationToken("[email protected]", 
"gravitino");
+    Assertions.assertEquals(0, loginUser.getCredentials().numberOfTokens());
+  }
+
+  private String runAsAlice(ClosableHiveCatalog catalog) throws Exception {
+    return PrincipalUtils.doAs(
+        new UserPrincipal("alice"),
+        () -> {
+          try {
+            return catalog.doKerberosOperations(
+                () -> {
+                  UserGroupInformation current = 
UserGroupInformation.getCurrentUser();
+                  if (!current.getUserName().equals(loginUser.getUserName())) {
+                    Assertions.assertEquals(1, 
current.getCredentials().numberOfTokens());
+                  }
+                  return current.getUserName();
+                });
+          } catch (Throwable e) {
+            throw new RuntimeException(e);
+          }
+        });
+  }
+
+  @SuppressWarnings("unchecked")
+  private ClosableHiveCatalog newCatalog(boolean impersonation) throws 
Exception {
+    Map<String, String> properties =
+        ImmutableMap.of(
+            AuthenticationConfig.AUTH_TYPE_KEY,
+            "kerberos",
+            AuthenticationConfig.IMPERSONATION_ENABLE_KEY,
+            String.valueOf(impersonation));
+    ClientPool<IMetaStoreClient, TException> clientPool = 
Mockito.mock(ClientPool.class);
+    Mockito.when(clientPool.run(any()))
+        .thenAnswer(
+            invocation ->
+                ((ClientPool.Action<?, IMetaStoreClient, TException>) 
invocation.getArgument(0))
+                    .run(metaStoreClient));
+    KerberosClient kerberosClient = Mockito.mock(KerberosClient.class);
+    Mockito.when(kerberosClient.getLoginUser()).thenReturn(loginUser);
+    Mockito.when(kerberosClient.getRealm()).thenReturn("EXAMPLE.COM");
+
+    ClosableHiveCatalog catalog = new ClosableHiveCatalog();
+    FieldUtils.writeField(catalog, "catalogProperties", properties, true);
+    FieldUtils.writeField(catalog, "clients", clientPool, true);
+    FieldUtils.writeField(catalog, "kerberosClient", kerberosClient, true);
+    return catalog;
+  }
+}

Reply via email to