yuqi1129 commented on code in PR #5136:
URL: https://github.com/apache/gravitino/pull/5136#discussion_r1824250320


##########
catalogs/catalog-lakehouse-paimon/src/main/java/org/apache/gravitino/catalog/lakehouse/paimon/authentication/AuthenticationConfig.java:
##########
@@ -49,6 +52,13 @@ public AuthenticationConfig(Map<String, String> properties) {
           .stringConf()
           .createWithDefault("simple");
 
+  public static final ConfigEntry<Boolean> ENABLE_IMPERSONATION_ENTRY =
+      new ConfigBuilder(IMPERSONATION_ENABLE_KEY)
+          .doc("Whether to enable impersonation for Iceberg catalog")

Review Comment:
   Iceberg -> paimon



##########
catalogs/catalog-lakehouse-paimon/src/main/java/org/apache/gravitino/catalog/lakehouse/paimon/authentication/kerberos/HiveBackendProxy.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.catalog.lakehouse.paimon.authentication.kerberos;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.security.PrivilegedExceptionAction;
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+import 
org.apache.gravitino.catalog.lakehouse.paimon.utils.PaimonHiveCachedClientPool;
+import org.apache.gravitino.utils.PrincipalUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+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.paimon.client.ClientPool;
+import org.apache.paimon.hive.HiveCatalog;
+import org.apache.paimon.hive.HiveCatalogFactory;
+import org.apache.paimon.options.Options;
+import org.apache.thrift.TException;
+
+/**
+ * Proxy class for HiveCatalog to support kerberos authentication. We can also 
make HiveCatalog as a
+ * generic type and pass it as a parameter to the constructor.
+ */
+public class HiveBackendProxy implements MethodInterceptor {
+
+  private final HiveCatalog target;
+  private final Class<? extends HiveCatalog> targetClazz;
+  private final String kerberosRealm;
+  private final UserGroupInformation proxyUser;
+  private final Options options;
+  private final ClientPool<IMetaStoreClient, TException> newClientPool;
+
+  public HiveBackendProxy(Options options, HiveCatalog target, String 
kerberosRealm) {
+    this.target = target;
+    targetClazz = target.getClass();
+    this.options = options;
+    this.kerberosRealm = kerberosRealm;
+    try {
+      proxyUser = UserGroupInformation.getCurrentUser();
+
+      // Replace the original client pool with PaimonHiveCachedClientPool. Why 
do we need to do
+      // this? Because the original client pool in Paimon uses a fixed 
username to create the
+      // client pool, and it will not work with kerberos authentication. We 
need to create a new
+      // client pool with the current user. For more, please see 
CachedClientPool#clientPool and
+      // notice the value of `key`
+      this.newClientPool = resetPaimonHiveCachedClientPool();
+    } catch (IOException e) {
+      throw new RuntimeException("Failed to get current user", e);
+    } catch (IllegalAccessException | NoSuchFieldException e) {
+      throw new RuntimeException("Failed to reset PaimonHiveCachedClientPool", 
e);
+    }
+  }
+
+  @Override
+  public Object intercept(Object o, Method method, Object[] objects, 
MethodProxy methodProxy)
+      throws Throwable {
+
+    String proxyKerberosPrincipalName = 
PrincipalUtils.getCurrentPrincipal().getName();
+    if (!proxyKerberosPrincipalName.contains("@")) {
+      proxyKerberosPrincipalName =
+          String.format("%s@%s", proxyKerberosPrincipalName, kerberosRealm);
+    }
+
+    UserGroupInformation realUser =
+        UserGroupInformation.createProxyUser(proxyKerberosPrincipalName, 
proxyUser);
+
+    String token =
+        newClientPool.run(
+            client ->
+                client.getDelegationToken(
+                    PrincipalUtils.getCurrentPrincipal().getName(), 
proxyUser.getShortUserName()));
+
+    Token<DelegationTokenIdentifier> delegationToken = new Token<>();
+    delegationToken.decodeFromUrlString(token);
+    realUser.addToken(delegationToken);
+
+    return realUser.doAs(
+        (PrivilegedExceptionAction<Object>)
+            () -> {
+              try {
+                return methodProxy.invoke(target, objects);
+              } catch (Throwable e) {
+                if (RuntimeException.class.isAssignableFrom(e.getClass())) {
+                  throw (RuntimeException) e;
+                }
+                throw new RuntimeException("Failed to invoke method", e);
+              }
+            });
+  }
+
+  private ClientPool<IMetaStoreClient, TException> 
resetPaimonHiveCachedClientPool()
+      throws IllegalAccessException, NoSuchFieldException {
+    final Field m = HiveCatalog.class.getDeclaredField("clients");

Review Comment:
   I think so, the same goes for Iceberg Hive backend. The problem is that it's 
not so easy to close the pool and I have tried to do it in the Iceberg Hive 
backend but failed. 



##########
catalogs/catalog-lakehouse-paimon/src/main/java/org/apache/gravitino/catalog/lakehouse/paimon/utils/PaimonHiveCachedClientPool.java:
##########
@@ -0,0 +1,304 @@
+/*
+ * 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.catalog.lakehouse.paimon.utils;
+
+import static 
org.apache.paimon.hive.HiveCatalogOptions.CLIENT_POOL_CACHE_EVICTION_INTERVAL_MS;
+import static org.apache.paimon.hive.HiveCatalogOptions.CLIENT_POOL_CACHE_KEYS;
+import static org.apache.paimon.options.CatalogOptions.CLIENT_POOL_SIZE;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.paimon.annotation.VisibleForTesting;
+import org.apache.paimon.client.ClientPool;
+import org.apache.paimon.hive.HiveCatalogOptions;
+import org.apache.paimon.hive.pool.HiveClientPool;
+import org.apache.paimon.options.Options;
+import 
org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Cache;
+import 
org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Caffeine;
+import 
org.apache.paimon.shade.caffeine2.com.github.benmanes.caffeine.cache.Scheduler;
+import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;
+import org.apache.paimon.shade.guava30.com.google.common.collect.Maps;
+import org.apache.paimon.shade.guava30.com.google.common.collect.Sets;
+import 
org.apache.paimon.shade.guava30.com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.apache.paimon.utils.Preconditions;
+import org.apache.thrift.TException;
+
+/**
+ * Referred from Apache Paimon's CachedClientPool implementation
+ * 
paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/pool/CachedClientPool.java
+ */
+public class PaimonHiveCachedClientPool implements 
ClientPool<IMetaStoreClient, TException> {
+
+  private static final String CONF_ELEMENT_PREFIX = "conf:";
+
+  private static Cache<Key, HiveClientPool> clientPoolCache;
+
+  private final Configuration conf;
+  private final int clientPoolSize;
+  private final long evictionInterval;
+  private final Key key;
+  private final String clientClassName;
+
+  public PaimonHiveCachedClientPool(Configuration conf, Options options, 
String clientClassName) {
+    this.conf = conf;
+    this.clientPoolSize = options.get(CLIENT_POOL_SIZE);
+    this.evictionInterval = 
options.get(CLIENT_POOL_CACHE_EVICTION_INTERVAL_MS);
+    this.key = extractKey(clientClassName, 
options.get(CLIENT_POOL_CACHE_KEYS), conf);

Review Comment:
   So the key is a constant value, consider the following scenario:
   
   User1 access the Paimon catalog then a connection pool with the key was 
created, when user2 also wants to access the catalog, it will reuse the pool 
created by user1. Since connection contains user information, it will cause 
issues. 



##########
catalogs/catalog-lakehouse-paimon/src/main/java/org/apache/gravitino/catalog/lakehouse/paimon/authentication/AuthenticationConfig.java:
##########
@@ -61,8 +71,22 @@ public boolean isKerberosAuth() {
     return AuthenticationType.KERBEROS.name().equalsIgnoreCase(getAuthType());
   }
 
+  public boolean isImpersonationEnabled() {
+    return get(ENABLE_IMPERSONATION_ENTRY);
+  }
+
   public static final Map<String, PropertyEntry<?>> 
AUTHENTICATION_PROPERTY_ENTRIES =
       new ImmutableMap.Builder<String, PropertyEntry<?>>()
+          .put(
+              IMPERSONATION_ENABLE_KEY,
+              PropertyEntry.booleanPropertyEntry(
+                  IMPERSONATION_ENABLE_KEY,
+                  "Whether to enable impersonation for the Iceberg catalog",

Review Comment:
   ditto.



-- 
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]

Reply via email to