package com.yiidata.amc.common.principal; import com.yiidata.amc.common.utils.LoginUtil; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.conf.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; /** * * 无加密的 * * *
* * Created by zhenqin. * User: zhenqin * Date: 2018/4/23 * Time: 10:15 * Vendor: yiidata.com * To change this template use File | Settings | File Templates. * ** * @author zhenqin */ public class KerborsHadoopPrincipal extends HadoopPrincipal { public final String ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME = "Client"; public final String ZOOKEEPER_SERVER_PRINCIPAL_KEY = "zookeeper.api.principal"; public final String zkServerPrincipal = "zookeeper/hadoop.hadoop.com"; final String userPrincipal; final String userKeytabPath; final String krb5ConfPath; final static Logger LOG = LoggerFactory.getLogger(KerborsHadoopPrincipal.class); public KerborsHadoopPrincipal(String userPrincipal, String userKeytabPath, String krb5ConfPath) { this.userPrincipal = userPrincipal; this.userKeytabPath = userKeytabPath; this.krb5ConfPath = krb5ConfPath; } /** * 无加密的 Conf * @param conf 配置 * @return 返回加密的 Hadoop Conf */ @Override public Configuration getPrincipalConf(Configuration conf, String... res) { for (String re : res) { if(StringUtils.isBlank(re)) { continue; } LOG.info("add {} conf resource to hadoop config.", re); conf.addResource(re); } try { LOG.info("try to setup hadoop kerberos auth."); LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, userPrincipal, userKeytabPath); LoginUtil.setZookeeperServerPrincipal(ZOOKEEPER_SERVER_PRINCIPAL_KEY, zkServerPrincipal); LoginUtil.login(userPrincipal, userKeytabPath, krb5ConfPath, conf); LOG.info("setup hadoop kerberos auth success."); } catch (IOException e) { throw new IllegalStateException(e); } return conf; } public static void main(String[] args) { HadoopPrincipal hadoopPrincipal = new KerborsHadoopPrincipal("user_shisuo", "F:/Deploy/sichuan-update/user.keytab", "F:/Deploy/sichuan-update/krb5.conf"); Configuration conf = hadoopPrincipal.getConf(); System.out.println(conf); } }