morningman commented on code in PR #30844: URL: https://github.com/apache/doris/pull/30844#discussion_r1477916617
########## fe/fe-common/src/main/java/org/apache/doris/common/security/authentication/HadoopUGI.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 org.apache.doris.common.security.authentication; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.security.UserGroupInformation; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.util.Map; + +public class HadoopUGI { + + private static final Logger LOG = LogManager.getLogger(HadoopUGI.class); + public static String HADOOP_USER_NAME = "hadoop.username"; + public static String HADOOP_SECURITY_AUTHENTICATION = "hadoop.security.authentication"; + public static String HADOOP_KERBEROS_PRINCIPAL = "hadoop.kerberos.principal"; + public static String HADOOP_KERBEROS_AUTHORIZATION = "hadoop.security.authorization"; + public static String HADOOP_KERBEROS_KEYTAB = "hadoop.kerberos.keytab"; + + /** + * login and return hadoop ugi + * @param conf config + * @return ugi + */ + public static UserGroupInformation loginWithUGI(Configuration conf) { + UserGroupInformation ugi; + String authentication = conf.get(HADOOP_SECURITY_AUTHENTICATION, null); + if (AuthType.KERBEROS.getDesc().equals(authentication)) { + String principal = conf.get(HADOOP_KERBEROS_PRINCIPAL); + String keytab = conf.get(HADOOP_KERBEROS_KEYTAB); + conf.set(HADOOP_KERBEROS_AUTHORIZATION, "true"); + UserGroupInformation.setConfiguration(conf); + try { + ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal, keytab); + UserGroupInformation.setLoginUser(ugi); + LOG.debug("Login by kerberos authentication with principal: {}", principal); + return ugi; + } catch (IOException e) { + throw new RuntimeException(e); + } + } else { + String hadoopUserName = conf.get(HADOOP_USER_NAME); + if (hadoopUserName == null) { + hadoopUserName = "hadoop"; + LOG.debug(HADOOP_USER_NAME + " is unset, use default user: hadoop"); + } + ugi = UserGroupInformation.createRemoteUser(hadoopUserName); + UserGroupInformation.setLoginUser(ugi); + LOG.debug("Login by proxy user, hadoop.username: {}", hadoopUserName); + return ugi; + } + } + + /** + * login hadoop with keytab and try checking TGT + * @param conf hadoop conf + * @return ugi + */ + public static UserGroupInformation checkTGTAndLogin(Configuration conf) { + if (AuthType.KERBEROS.getDesc().equals( + conf.get(HadoopUGI.HADOOP_SECURITY_AUTHENTICATION, null))) { + try { + UserGroupInformation ugi = UserGroupInformation.getLoginUser(); + String principal = conf.get(HadoopUGI.HADOOP_KERBEROS_PRINCIPAL); + LOG.debug("Current login user: {}", ugi.getUserName()); + if (ugi.hasKerberosCredentials() && ugi.getUserName().equals(principal)) { + // if the current user is logged by kerberos and is the same user + // just use checkTGTAndReloginFromKeytab because this method will only relogin + // when the TGT is expired or is close to expiry + ugi.checkTGTAndReloginFromKeytab(); + return ugi; + } + } catch (IOException e) { + LOG.warn("A SecurityException occurs with kerberos, do login immediately.", e); + return HadoopUGI.loginWithUGI(conf); + } + } + return HadoopUGI.loginWithUGI(conf); + } + + /** + * use for HMSExternalCatalog to login + * @param catalogProperty our properties + * @param hiveConf hive conf + */ + public static void tryKrbLogin(String catalogName, Map<String, String> catalogProperty, Configuration hiveConf) { Review Comment: Do we really need this? -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org