HadoopPrincipalFactory.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.yiidata.amc.common.principal;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. /**
  7. *
  8. *
  9. *
  10. * <pre>
  11. *
  12. * Created by zhenqin.
  13. * User: zhenqin
  14. * Date: 2018/4/23
  15. * Time: 10:43
  16. * Vendor: yiidata.com
  17. * To change this template use File | Settings | File Templates.
  18. *
  19. * </pre>
  20. *
  21. * @author zhenqin
  22. */
  23. public class HadoopPrincipalFactory {
  24. /**
  25. * Hadoop 的认证方式
  26. */
  27. public static final String HADOOP_SECUROTY_AUTH = "hadoop.security.auth";
  28. /**
  29. * 无认证的值
  30. */
  31. public static final String HADOOP_SECUROTY_AUTH_NONE = "none";
  32. private static Logger LOG = LoggerFactory.getLogger(HadoopPrincipal.class);
  33. /**
  34. * 获取 加密模式
  35. * @param userPrincipal
  36. * @param userKeytabPath
  37. * @param krb5ConfPath
  38. * @return
  39. */
  40. public static HadoopPrincipal getHadoopPrincipal(
  41. final String userPrincipal,
  42. final String userKeytabPath,
  43. final String krb5ConfPath) {
  44. if(StringUtils.isBlank(userPrincipal)) {
  45. LOG.info("use HadoopPrincipal class: " + NonHadoopPrincipal.class.getName());
  46. return new NonHadoopPrincipal();
  47. }
  48. LOG.info("use HadoopPrincipal class: " + KerborsHadoopPrincipal.class.getName());
  49. return new KerborsHadoopPrincipal(userPrincipal, userKeytabPath, krb5ConfPath);
  50. }
  51. /**
  52. * 获取 加密模式
  53. * @param conf Hadoop Conf
  54. * @return
  55. */
  56. public static HadoopPrincipal getHadoopPrincipal(Configuration conf) {
  57. String auth = conf.get(HADOOP_SECUROTY_AUTH, HADOOP_SECUROTY_AUTH_NONE);
  58. if(StringUtils.isBlank(auth) || HADOOP_SECUROTY_AUTH_NONE.equals(auth)) {
  59. LOG.info("use HadoopPrincipal class: " + NonHadoopPrincipal.class.getName());
  60. return new NonHadoopPrincipal();
  61. }
  62. LOG.info("use HadoopPrincipal class: " + KerborsHadoopPrincipal.class.getName());
  63. final String userPrincipal = conf.get("hadoop.security.auth.principal");
  64. final String userKeytabPath = conf.get("hadoop.security.auth.keytab");
  65. final String krb5ConfPath = conf.get("hadoop.security.auth.krb5Path");
  66. return new KerborsHadoopPrincipal(userPrincipal, userKeytabPath, krb5ConfPath);
  67. }
  68. }