caican00 commented on code in PR #4075:
URL: https://github.com/apache/gravitino/pull/4075#discussion_r1666208979


##########
catalogs/catalog-lakehouse-paimon/src/main/java/com/datastrato/gravitino/catalog/lakehouse/paimon/authentication/kerberos/KerberosClient.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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 
com.datastrato.gravitino.catalog.lakehouse.paimon.authentication.kerberos;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.security.authentication.util.KerberosName;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class KerberosClient {
+  private static final Logger LOG = 
LoggerFactory.getLogger(KerberosClient.class);
+
+  public static final String GRAVITINO_KEYTAB_FORMAT =
+      "keytabs/gravitino-lakehouse-paimon-%s-keytab";
+
+  private final ScheduledThreadPoolExecutor checkTgtExecutor;
+  private final Map<String, String> conf;
+  private final Configuration hadoopConf;
+
+  public KerberosClient(Map<String, String> conf, Configuration hadoopConf) {
+    this.conf = conf;
+    this.hadoopConf = hadoopConf;
+    this.checkTgtExecutor = new ScheduledThreadPoolExecutor(1, 
getThreadFactory("check-tgt"));
+  }
+
+  public String login(String keytabFilePath) throws IOException {
+    KerberosConfig kerberosConfig = new KerberosConfig(conf);
+
+    // Check the principal and keytab file
+    String catalogPrincipal = kerberosConfig.getPrincipalName();
+    Preconditions.checkArgument(
+        StringUtils.isNotBlank(catalogPrincipal), "The principal can't be 
blank");
+    @SuppressWarnings("null")
+    List<String> principalComponents = 
Splitter.on('@').splitToList(catalogPrincipal);
+    Preconditions.checkArgument(
+        principalComponents.size() == 2, "The principal has the wrong format");
+
+    // Login
+    UserGroupInformation.setConfiguration(hadoopConf);
+    KerberosName.resetDefaultRealm();
+    UserGroupInformation.loginUserFromKeytab(catalogPrincipal, keytabFilePath);
+    UserGroupInformation kerberosLoginUgi = 
UserGroupInformation.getCurrentUser();
+
+    // Refresh the cache if it's out of date.
+    int checkInterval = kerberosConfig.getCheckIntervalSec();
+    checkTgtExecutor.scheduleAtFixedRate(
+        () -> {
+          try {
+            kerberosLoginUgi.checkTGTAndReloginFromKeytab();
+          } catch (Exception e) {
+            LOG.error("Fail to refresh ugi token: ", e);
+          }
+        },
+        checkInterval,
+        checkInterval,
+        TimeUnit.SECONDS);
+
+    return principalComponents.get(1);
+  }
+
+  public File saveKeyTabFileFromUri(String catalogId) throws IOException {
+
+    KerberosConfig kerberosConfig = new KerberosConfig(conf);
+
+    String keyTabUri = kerberosConfig.getKeytab();
+    Preconditions.checkArgument(StringUtils.isNotBlank(keyTabUri), "Keytab uri 
can't be blank");
+    // TODO: Support to download the file from Kerberos HDFS
+    Preconditions.checkArgument(

Review Comment:
   > why not support HDFS? I see HDFS is supported in `fetchFileFromUri`
   
   we have to login first and then we can download file from HDFS
   



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