Krb5HBaseConnectionServiceImpl.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.primeton.dsp.datarelease.data.bdata;
  2. import com.primeton.dsp.datarelease.server.model.DspHbaseResource;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.hbase.HBaseConfiguration;
  6. import org.apache.hadoop.hbase.TableName;
  7. import org.apache.hadoop.hbase.client.Admin;
  8. import org.apache.hadoop.hbase.client.Connection;
  9. import org.apache.hadoop.hbase.client.ConnectionFactory;
  10. import org.apache.hadoop.hbase.client.Table;
  11. import org.apache.hadoop.hbase.exceptions.HBaseException;
  12. import java.io.Closeable;
  13. import java.io.IOException;
  14. import java.util.Set;
  15. /**
  16. *
  17. * Hive Kerberos 认证方式获得连接
  18. *
  19. *
  20. * <pre>
  21. *
  22. * Created by zhaopx.
  23. * User: zhaopx
  24. * Date: 2020/4/22
  25. * Time: 18:02
  26. *
  27. * </pre>
  28. *
  29. * @author zhaopx
  30. */
  31. @Slf4j
  32. public class Krb5HBaseConnectionServiceImpl implements HBaseConnectionService, Closeable {
  33. /**
  34. * Hive 数据源
  35. */
  36. final DspHbaseResource hbaseResource;
  37. /**
  38. * HBase 链接
  39. */
  40. Connection connection;
  41. public Krb5HBaseConnectionServiceImpl(DspHbaseResource hbaseResource) {
  42. this.hbaseResource = hbaseResource;
  43. }
  44. @Override
  45. public boolean doAuth() {
  46. //KrbUser = "hadoop/cdh-node1@HADOOP.COM";
  47. AuthPrincipalCreator authPrincipalCreator = AuthPrincipalCreator.useDataReleaseConf(hbaseResource.getAuthBasePath());
  48. Set<String> principals = authPrincipalCreator.listPrincipals();
  49. log.info("find existed principals: {}", principals);
  50. AuthPrincipal kerberosPrincipal = authPrincipalCreator.getKerberosPrincipal(hbaseResource.getAuthUser());
  51. String userKeytab = kerberosPrincipal.getUserKeytabFile().getAbsolutePath();
  52. String krb5File = kerberosPrincipal.getKrb5File().getAbsolutePath();
  53. String krbUser = kerberosPrincipal.getPrincipal();
  54. // 分别加载 core、hdfs、hbase site 文件
  55. Configuration conf = new Configuration();
  56. try {
  57. if (kerberosPrincipal.getCoreSite() != null) {
  58. conf.addResource(kerberosPrincipal.getCoreSite().toURL());
  59. log.info("add config: {}", kerberosPrincipal.getCoreSite().getAbsolutePath());
  60. }
  61. if (kerberosPrincipal.getHdfsSite() != null) {
  62. conf.addResource(kerberosPrincipal.getHdfsSite().toURL());
  63. log.info("add config: {}", kerberosPrincipal.getHdfsSite().getAbsolutePath());
  64. }
  65. conf.reloadConfiguration();
  66. Configuration hbaseConf = HBaseConfiguration.create(conf);
  67. if (kerberosPrincipal.getHBaseSite() != null) {
  68. hbaseConf.addResource(kerberosPrincipal.getHBaseSite().toURL());
  69. log.info("add config: {}", kerberosPrincipal.getHBaseSite().getAbsolutePath());
  70. }
  71. hbaseConf.reloadConfiguration();
  72. // Kerberos 认证
  73. KerberosUtil.loginKerberos(hbaseConf, krbUser, userKeytab, krb5File);
  74. connection = ConnectionFactory.createConnection(hbaseConf);
  75. log.info("hbase kerberos 认证通过。");
  76. return true;
  77. } catch (Exception e) {
  78. throw new SecurityException("HBase Kerberos 认证异常。", e);
  79. }
  80. }
  81. @Override
  82. public Admin getConnection() throws HBaseException {
  83. try {
  84. return connection.getAdmin();
  85. } catch (Exception e) {
  86. throw new HBaseException("连接 HBase 异常。", e);
  87. }
  88. }
  89. @Override
  90. public Table getTable(String tableName) throws HBaseException {
  91. try {
  92. return connection.getTable(TableName.valueOf(tableName));
  93. } catch (IOException e) {
  94. throw new HBaseException("无法获取Hbase " + tableName + " 表链接。", e);
  95. }
  96. }
  97. @Override
  98. public void close() throws IOException {
  99. log.info("关闭 HBase 连接。");
  100. if(connection != null) {
  101. connection.close();
  102. }
  103. }
  104. }