agnes-xinyi-lu commented on code in PR #8189:
URL: https://github.com/apache/gravitino/pull/8189#discussion_r2363356775


##########
iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapperProxy.java:
##########
@@ -0,0 +1,143 @@
+/*
+ *  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.ops;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.security.PrivilegedExceptionAction;
+import java.util.Map;
+import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.MethodInterceptor;
+import net.sf.cglib.proxy.MethodProxy;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.iceberg.common.ClosableHiveCatalog;
+import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.apache.gravitino.iceberg.common.authentication.AuthenticationConfig;
+import 
org.apache.gravitino.iceberg.common.authentication.kerberos.KerberosClient;
+import org.apache.gravitino.iceberg.common.utils.IcebergHiveCachedClientPool;
+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.iceberg.catalog.Catalog;
+import org.apache.iceberg.hive.HiveCatalog;
+import org.apache.thrift.TException;
+
+public class IcebergCatalogWrapperProxy implements MethodInterceptor {
+
+  private final IcebergCatalogWrapper target;
+
+  boolean needProxy = false;
+  private KerberosClient kerberosClient;
+  private ClientPool<IMetaStoreClient, TException> newClientPool;
+
+  public IcebergCatalogWrapperProxy(IcebergCatalogWrapper target) {
+    this.target = target;
+    Catalog catalog = target.catalog;
+
+    // Need special handling for HiveCatalog.
+    if (catalog instanceof HiveCatalog) {
+      HiveCatalog hiveCatalog = (HiveCatalog) catalog;
+
+      try {
+        Map<String, String> properties =
+            (Map<String, String>) FieldUtils.readField(hiveCatalog, 
"catalogProperties", true);
+        AuthenticationConfig authenticationConfig = new 
AuthenticationConfig(properties);
+        if (authenticationConfig.isImpersonationEnabled()) {
+          needProxy = true;
+          kerberosClient = (KerberosClient) ((ClosableHiveCatalog) 
catalog).getResources().get(0);
+          newClientPool = resetIcebergHiveClientPool(hiveCatalog, properties);
+        }
+      } catch (Exception e) {
+        throw new RuntimeException("Failed to create HiveBackendProxy", e);
+      }
+    }
+  }
+
+  @Override
+  public Object intercept(Object o, Method method, Object[] objects, 
MethodProxy methodProxy)
+      throws Throwable {
+    if (needProxy) {
+      final String finalPrincipalName;
+      String proxyKerberosPrincipalName = 
PrincipalUtils.getCurrentPrincipal().getName();

Review Comment:
   Right, in my scenario that's the case, but I'm not asking for the exact 
solution, only suggesting to make it more generic or customizable so I could 
implement my own class to provide impersonation info instead of relying on 
Kerberos. 



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