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


##########
catalogs/catalog-hadoop/src/main/java/org/apache/gravitino/catalog/hadoop/authentication/UserContext.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.authentication;
+
+import static 
org.apache.gravitino.catalog.hadoop.SecureHadoopCatalogOperations.GRAVITINO_KEYTAB_FORMAT;
+import static 
org.apache.gravitino.catalog.hadoop.authentication.AuthenticationConfig.IMPERSONATION_ENABLE_KEY;
+
+import com.google.common.collect.Maps;
+import java.io.Closeable;
+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.Map;
+import org.apache.gravitino.NameIdentifier;
+import 
org.apache.gravitino.catalog.hadoop.authentication.AuthenticationConfig.AuthenticationType;
+import org.apache.gravitino.connector.CatalogInfo;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.security.UserGroupInformation;
+
+public abstract class UserContext implements Closeable {
+
+  private static final Map<NameIdentifier, UserContext> userContextMap = 
Maps.newConcurrentMap();
+
+  private static void addUserContext(NameIdentifier nameIdentifier, 
UserContext userContext) {
+    userContextMap.put(nameIdentifier, userContext);
+  }
+
+  public static void clearUserContext(NameIdentifier nameIdentifier) {
+    UserContext userContext = userContextMap.remove(nameIdentifier);
+    if (userContext != null) {
+      try {
+        userContext.close();
+      } catch (IOException e) {
+        throw new RuntimeException("Failed to close user context", e);
+      }
+    }
+  }
+
+  public static void cleanAllUserContext() {
+    userContextMap
+        .values()
+        .forEach(
+            userContext -> {
+              try {
+                userContext.close();
+              } catch (IOException e) {
+                throw new RuntimeException("Failed to close user context", e);
+              }
+            });
+    userContextMap.clear();
+  }
+
+  public static UserContext getUserContext(
+      NameIdentifier nameIdentifier,
+      Map<String, String> properties,
+      Configuration configuration,
+      CatalogInfo catalogInfo) {
+    // Try to get the parent user context.
+    NameIdentifier currentNameIdentifier = 
NameIdentifier.of(nameIdentifier.namespace().levels());
+    UserContext parentContext = null;
+    while (!currentNameIdentifier.namespace().isEmpty()) {
+      if (userContextMap.containsKey(currentNameIdentifier)) {
+        parentContext = userContextMap.get(currentNameIdentifier);
+        break;
+      }
+      currentNameIdentifier = 
NameIdentifier.of(currentNameIdentifier.namespace().levels());
+    }
+
+    if (configuration == null) {
+      configuration = new Configuration();
+    }
+    AuthenticationConfig authenticationConfig = new 
AuthenticationConfig(properties);
+
+    // If we do not set the impersonation, we will use the parent context;
+    boolean enableUserImpersonation = false;
+    if (properties.containsKey(IMPERSONATION_ENABLE_KEY)) {
+      enableUserImpersonation = authenticationConfig.isImpersonationEnabled();
+    } else if (parentContext != null) {
+      enableUserImpersonation = parentContext.enableUserImpersonation();
+    }
+
+    AuthenticationType authenticationType = AuthenticationType.SIMPLE;
+    if (properties.containsKey(AuthenticationConfig.AUTH_TYPE_KEY)) {
+      authenticationType =
+          authenticationConfig.isSimpleAuth()
+              ? AuthenticationType.SIMPLE
+              : AuthenticationType.KERBEROS;
+    }
+
+    UserGroupInformation currentUser;
+    try {
+      currentUser = UserGroupInformation.getCurrentUser();
+    } catch (IOException ioException) {
+      throw new RuntimeException("Failed to get current user", ioException);
+    }
+
+    if (authenticationType == AuthenticationType.SIMPLE) {

Review Comment:
   done



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