yuqi1129 commented on code in PR #3852: URL: https://github.com/apache/gravitino/pull/3852#discussion_r1680942619
########## catalogs/catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/SecureHadoopCatalogOperations.java: ########## @@ -0,0 +1,377 @@ +/* + * 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.hadoop; + +import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.UndeclaredThrowableException; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Optional; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Schema; +import org.apache.gravitino.catalog.hadoop.authentication.AuthenticationConfig; +import org.apache.gravitino.catalog.hadoop.authentication.kerberos.KerberosClient; +import org.apache.gravitino.catalog.hadoop.authentication.kerberos.KerberosConfig; +import org.apache.gravitino.connector.CatalogInfo; +import org.apache.gravitino.connector.HasPropertyMetadata; +import org.apache.gravitino.exceptions.FilesetAlreadyExistsException; +import org.apache.gravitino.exceptions.NoSuchCatalogException; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.exceptions.NoSuchFilesetException; +import org.apache.gravitino.exceptions.NoSuchSchemaException; +import org.apache.gravitino.exceptions.NonEmptySchemaException; +import org.apache.gravitino.exceptions.SchemaAlreadyExistsException; +import org.apache.gravitino.file.Fileset; +import org.apache.gravitino.file.FilesetChange; +import org.apache.gravitino.meta.FilesetEntity; +import org.apache.gravitino.meta.SchemaEntity; +import org.apache.gravitino.utils.PrincipalUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * SecureHadoopCatalogOperations is a secure version of HadoopCatalogOperations that can manage + * Schema and fileset level of user authentication. + */ +public class SecureHadoopCatalogOperations extends HadoopCatalogOperations { + + public static final Logger LOG = LoggerFactory.getLogger(SecureHadoopCatalogOperations.class); + + private final List<Closeable> closeables = Lists.newArrayList(); + + private final Map<NameIdentifier, UserInfo> userInfoMap = Maps.newConcurrentMap(); + + public static final String GRAVITINO_KEYTAB_FORMAT = "keytabs/gravitino-%s"; + + private String kerberosRealm; + + public SecureHadoopCatalogOperations() { + super(); + } + + public SecureHadoopCatalogOperations(EntityStore store) { + super(store); + } + + public String getKerberosRealm() { + return kerberosRealm; + } + + static class UserInfo { + UserGroupInformation loginUser; + boolean enableUserImpersonation; + String keytabPath; + String realm; + + static UserInfo of( + UserGroupInformation loginUser, + boolean enableUserImpersonation, + String keytabPath, + String kerberosRealm) { + UserInfo userInfo = new UserInfo(); + userInfo.loginUser = loginUser; + userInfo.enableUserImpersonation = enableUserImpersonation; + userInfo.keytabPath = keytabPath; + userInfo.realm = kerberosRealm; + return userInfo; + } + } + + // We have overridden the createFileset, dropFileset, createSchema, dropSchema method to reset + // the current user based on the name identifier. + + @Override + public Fileset createFileset( + NameIdentifier ident, + String comment, + Fileset.Type type, + String storageLocation, + Map<String, String> properties) + throws NoSuchSchemaException, FilesetAlreadyExistsException { + UserGroupInformation currentUser = getUGIByIdent(properties, ident); + + // TODO: Use a more elegant way to pass api user to inner doAs block. + currentApiUser.set(PrincipalUtils.getCurrentUserName()); + + return doAs( + currentUser, + () -> super.createFileset(ident, comment, type, storageLocation, properties), + ident); + } + + @Override + public boolean dropFileset(NameIdentifier ident) { + FilesetEntity filesetEntity; + try { + filesetEntity = store.get(ident, Entity.EntityType.FILESET, FilesetEntity.class); + } catch (NoSuchEntityException e) { + LOG.warn("Fileset {} does not exist", ident); + return false; + } catch (IOException ioe) { + throw new RuntimeException("Failed to delete fileset " + ident, ioe); + } + + // Reset the current user based on the name identifier. + UserGroupInformation currentUser = getUGIByIdent(filesetEntity.properties(), ident); + + boolean r = doAs(currentUser, () -> super.dropFileset(ident), ident); + cleanUserInfo(ident); + return r; + } + + @Override + public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> properties) + throws NoSuchCatalogException, SchemaAlreadyExistsException { + // Reset the current user based on the name identifier and properties. + UserGroupInformation currentUser = getUGIByIdent(properties, ident); + currentApiUser.set(PrincipalUtils.getCurrentUserName()); Review Comment: After several attempts, I decided to use the thread-local mechanism for the following reasons. 1. As far as I see, `Subject` and `AccessControlContext` also use this mechanism to achieve thread isolation. 2. Splitting the logic into two or several parts is not very elegant for me as it will make it difficult for users to review. 3. Changing the API and making the API user a parameter to the method will bring about significant changes. It seems to be very expensive. @qqqttt123 @jerryshao If you have any good suggestions, please let me know, thanks. -- 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]
