yuqi1129 commented on code in PR #8189: URL: https://github.com/apache/gravitino/pull/8189#discussion_r2366605283
########## iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapperProxy.java: ########## @@ -0,0 +1,130 @@ +/* + * 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.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.IcebergConfig; +import org.apache.gravitino.iceberg.common.authentication.AuthenticationConfig; +import org.apache.gravitino.iceberg.common.authentication.SupportsKerberos; +import org.apache.gravitino.iceberg.common.authentication.kerberos.KerberosClient; +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.thrift.TException; + +public class IcebergCatalogWrapperProxy implements MethodInterceptor { + private final IcebergCatalogWrapper target; + private KerberosClient kerberosClient = null; + + public IcebergCatalogWrapperProxy(IcebergCatalogWrapper target) { + this.target = target; + Catalog catalog = target.catalog; + if (catalog instanceof SupportsKerberos) { + kerberosClient = ((SupportsKerberos) catalog).getKerberosClient(); + } + } + + @Override + public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) + throws Throwable { + // This means no kerberos is configured, just invoke the method directly. + if (kerberosClient == null) { + return methodProxy.invoke(target, objects); + } + + Map<String, String> properties = target.getIcebergConfig().getIcebergCatalogProperties(); + AuthenticationConfig authenticationConfig = new AuthenticationConfig(properties); + + final String finalPrincipalName; + String proxyKerberosPrincipalName = PrincipalUtils.getCurrentPrincipal().getName(); + + if (!proxyKerberosPrincipalName.contains("@")) { + finalPrincipalName = + String.format("%s@%s", proxyKerberosPrincipalName, kerberosClient.getRealm()); + } else { + finalPrincipalName = proxyKerberosPrincipalName; + } + + UserGroupInformation realUser = + authenticationConfig.isImpersonationEnabled() + ? UserGroupInformation.createProxyUser( + finalPrincipalName, kerberosClient.getLoginUser()) + : kerberosClient.getLoginUser(); + + try { + ClientPool<IMetaStoreClient, TException> newClientPool = + (ClientPool<IMetaStoreClient, TException>) + FieldUtils.readField(target.catalog, "clients", true); Review Comment: Would you happen to have any good suggestions? The code is related to `HiveCatalog`, it's proper to put it here, the best way is place it in the hive catalog extension, as we intend to add a wapper there, I could not come up with a more elegant away right now. -- 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]
