package com.yiidata.amc.common.principal; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.conf.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * * *
* * Created by zhenqin. * User: zhenqin * Date: 2018/4/23 * Time: 10:43 * Vendor: yiidata.com * To change this template use File | Settings | File Templates. * ** * @author zhenqin */ public class HadoopPrincipalFactory { /** * Hadoop 的认证方式 */ public static final String HADOOP_SECUROTY_AUTH = "hadoop.security.auth"; /** * 无认证的值 */ public static final String HADOOP_SECUROTY_AUTH_NONE = "none"; private static Logger LOG = LoggerFactory.getLogger(HadoopPrincipal.class); /** * 获取 加密模式 * @param userPrincipal * @param userKeytabPath * @param krb5ConfPath * @return */ public static HadoopPrincipal getHadoopPrincipal( final String userPrincipal, final String userKeytabPath, final String krb5ConfPath) { if(StringUtils.isBlank(userPrincipal)) { LOG.info("use HadoopPrincipal class: " + NonHadoopPrincipal.class.getName()); return new NonHadoopPrincipal(); } LOG.info("use HadoopPrincipal class: " + KerborsHadoopPrincipal.class.getName()); return new KerborsHadoopPrincipal(userPrincipal, userKeytabPath, krb5ConfPath); } /** * 获取 加密模式 * @param conf Hadoop Conf * @return */ public static HadoopPrincipal getHadoopPrincipal(Configuration conf) { String auth = conf.get(HADOOP_SECUROTY_AUTH, HADOOP_SECUROTY_AUTH_NONE); if(StringUtils.isBlank(auth) || HADOOP_SECUROTY_AUTH_NONE.equals(auth)) { LOG.info("use HadoopPrincipal class: " + NonHadoopPrincipal.class.getName()); return new NonHadoopPrincipal(); } LOG.info("use HadoopPrincipal class: " + KerborsHadoopPrincipal.class.getName()); final String userPrincipal = conf.get("hadoop.security.auth.principal"); final String userKeytabPath = conf.get("hadoop.security.auth.keytab"); final String krb5ConfPath = conf.get("hadoop.security.auth.krb5Path"); return new KerborsHadoopPrincipal(userPrincipal, userKeytabPath, krb5ConfPath); } }