KerborsHadoopPrincipal.java 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.yiidata.amc.common.principal;
  2. import com.yiidata.amc.common.utils.LoginUtil;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import java.io.IOException;
  8. /**
  9. *
  10. * 无加密的
  11. *
  12. *
  13. * <pre>
  14. *
  15. * Created by zhenqin.
  16. * User: zhenqin
  17. * Date: 2018/4/23
  18. * Time: 10:15
  19. * Vendor: yiidata.com
  20. * To change this template use File | Settings | File Templates.
  21. *
  22. * </pre>
  23. *
  24. * @author zhenqin
  25. */
  26. public class KerborsHadoopPrincipal extends HadoopPrincipal {
  27. public final String ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME = "Client";
  28. public final String ZOOKEEPER_SERVER_PRINCIPAL_KEY = "zookeeper.api.principal";
  29. public final String zkServerPrincipal = "zookeeper/hadoop.hadoop.com";
  30. final String userPrincipal;
  31. final String userKeytabPath;
  32. final String krb5ConfPath;
  33. final static Logger LOG = LoggerFactory.getLogger(KerborsHadoopPrincipal.class);
  34. public KerborsHadoopPrincipal(String userPrincipal, String userKeytabPath, String krb5ConfPath) {
  35. this.userPrincipal = userPrincipal;
  36. this.userKeytabPath = userKeytabPath;
  37. this.krb5ConfPath = krb5ConfPath;
  38. }
  39. /**
  40. * 无加密的 Conf
  41. * @param conf 配置
  42. * @return 返回加密的 Hadoop Conf
  43. */
  44. @Override
  45. public Configuration getPrincipalConf(Configuration conf, String... res) {
  46. for (String re : res) {
  47. if(StringUtils.isBlank(re)) {
  48. continue;
  49. }
  50. LOG.info("add {} conf resource to hadoop config.", re);
  51. conf.addResource(re);
  52. }
  53. try {
  54. LOG.info("try to setup hadoop kerberos auth.");
  55. LoginUtil.setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, userPrincipal, userKeytabPath);
  56. LoginUtil.setZookeeperServerPrincipal(ZOOKEEPER_SERVER_PRINCIPAL_KEY, zkServerPrincipal);
  57. LoginUtil.login(userPrincipal, userKeytabPath, krb5ConfPath, conf);
  58. LOG.info("setup hadoop kerberos auth success.");
  59. } catch (IOException e) {
  60. throw new IllegalStateException(e);
  61. }
  62. return conf;
  63. }
  64. public static void main(String[] args) {
  65. HadoopPrincipal hadoopPrincipal = new KerborsHadoopPrincipal("user_shisuo",
  66. "F:/Deploy/sichuan-update/user.keytab",
  67. "F:/Deploy/sichuan-update/krb5.conf");
  68. Configuration conf = hadoopPrincipal.getConf();
  69. System.out.println(conf);
  70. }
  71. }