AuthPrincipalCreator.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package com.primeton.dsp.datarelease.data.bdata;
  2. import lombok.NonNull;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.apache.commons.lang.SystemUtils;
  6. import java.io.File;
  7. import java.util.Arrays;
  8. import java.util.Set;
  9. import java.util.stream.Collectors;
  10. /**
  11. *
  12. * Hive HBase 租户验证
  13. *
  14. * <pre>
  15. *
  16. * Created by zhaopx.
  17. * User: zhaopx
  18. * Date: 2020/4/20
  19. * Time: 10:53
  20. *
  21. * </pre>
  22. *
  23. * @author zhaopx
  24. */
  25. @Slf4j
  26. public final class AuthPrincipalCreator {
  27. /**
  28. * 基础路径
  29. */
  30. private final String basePath;
  31. public AuthPrincipalCreator(String basePath) {
  32. File basePathFile = new File(basePath);
  33. if(!basePathFile.exists()) {
  34. if(!basePathFile.mkdirs()) {
  35. throw new IllegalStateException("目录不存在, 无法创建!Cause: dir: " + basePath + " not found!");
  36. }
  37. }
  38. this.basePath = basePathFile.getAbsolutePath();
  39. }
  40. /**
  41. * 采用 EOS 8 的外置目录
  42. * @return
  43. */
  44. public static AuthPrincipalCreator useExtractorConf(String basePathFirst) {
  45. // 采用 传来的 地址
  46. String externalDir = basePathFirst;
  47. if(StringUtils.isNotBlank(basePathFirst) && new File(basePathFirst).exists()) {
  48. // 如果穿来的地址存在则用传来的地址
  49. return new AuthPrincipalCreator(externalDir);
  50. }
  51. // 不存在则使用extractor_home 下的 conf/principal 的地址
  52. externalDir = System.getenv("EXTRACTOR_HOME");
  53. if(StringUtils.isBlank(externalDir)) {
  54. externalDir = System.getProperty("extractor.home");
  55. }
  56. if(StringUtils.isBlank(externalDir)) {
  57. externalDir = "./";
  58. }
  59. String principalBasePath = new File(externalDir, "config/principal").getAbsolutePath();
  60. log.info("use principal dir: {}", principalBasePath);
  61. return new AuthPrincipalCreator(principalBasePath);
  62. }
  63. /**
  64. * 返回该基础目录下所有的租户
  65. * @return
  66. */
  67. public final Set<String> listPrincipals() {
  68. // 返回基础目录下所有不以点开头的文件
  69. return Arrays.stream(new File(basePath).listFiles(it -> !it.getName().startsWith(".")))
  70. .map(it->it.getName())
  71. .collect(Collectors.toSet());
  72. }
  73. /**
  74. * 返回租户认证信息
  75. * @param principal
  76. * @return
  77. */
  78. public final AuthPrincipal getKerberosPrincipal(@NonNull String principal) {
  79. String principalName = principal;
  80. if(principal.contains("/") || principal.contains("@")) {
  81. // 取第一个字符就是登陆名称
  82. principalName = principal.split("/|@", -1)[0];
  83. }
  84. // 先判断是否存在租户
  85. if(!existsPrincipal(principalName)) {
  86. throw new IllegalStateException("不存在该租户【" + principal + "】。");
  87. }
  88. return new KerberosPrincipalImpl(principalName, principal, new File(basePath, principalName).getAbsolutePath());
  89. }
  90. /**
  91. * 返回是否存在该租户
  92. * @param principal
  93. * @return
  94. */
  95. public final boolean existsPrincipal(String principal) {
  96. return new File(basePath, principal).exists();
  97. }
  98. /**
  99. * 返回基础路径
  100. * @return
  101. */
  102. public String getAuthBasePath() {
  103. return basePath;
  104. }
  105. }
  106. class KerberosPrincipalImpl extends AuthPrincipal {
  107. /**
  108. * 租户名,目录名
  109. */
  110. String principalName;
  111. /**
  112. * 租户全称
  113. */
  114. String principal;
  115. /**
  116. * 租户目录
  117. */
  118. String principalWork;
  119. public KerberosPrincipalImpl(String principalName, String principal, String principalWork) {
  120. this.principalName = principalName;
  121. this.principal = principal;
  122. this.principalWork = principalWork;
  123. }
  124. @Override
  125. public String getPrincipal() {
  126. return this.principal;
  127. }
  128. @Override
  129. public String getPrincipalDesc() {
  130. return "TENANT:"+this.principal;
  131. }
  132. @Override
  133. public File getUserKeytabFile() {
  134. File userKeytabFile = new File(principalWork, "user.keytab");
  135. return userKeytabFile.exists() ? userKeytabFile : null;
  136. }
  137. @Override
  138. public File getKrb5File() {
  139. if(SystemUtils.IS_OS_WINDOWS) {
  140. // windows krb5File 是 ini 结尾
  141. File krb5File = new File(principalWork, "krb5.ini");
  142. if(krb5File.exists()) {
  143. return krb5File;
  144. }
  145. }
  146. // 其他系统,如果win系统不存在ini也以 conf 检测一次
  147. File krb5File = new File(principalWork, "krb5.conf");
  148. return krb5File.exists() ? krb5File : null;
  149. }
  150. @Override
  151. public File getHiveClientFile() {
  152. File hiveClientPropFile = new File(principalWork, "hiveclient.properties");
  153. return hiveClientPropFile.exists() ? hiveClientPropFile : null;
  154. }
  155. @Override
  156. public File getCoreSite() {
  157. File coreSiteFile = new File(principalWork, "core-site.xml");
  158. return coreSiteFile.exists() ? coreSiteFile : null;
  159. }
  160. @Override
  161. public File getHdfsSite() {
  162. File hdfsSiteFile = new File(principalWork, "hdfs-site.xml");
  163. return hdfsSiteFile.exists() ? hdfsSiteFile : null;
  164. }
  165. @Override
  166. public File getHiveSite() {
  167. File hiveSiteFile = new File(principalWork, "hive-site.xml");
  168. return hiveSiteFile.exists() ? hiveSiteFile : null;
  169. }
  170. @Override
  171. public File getHBaseSite() {
  172. File hbaseSiteFile = new File(principalWork, "hbase-site.xml");
  173. return hbaseSiteFile.exists() ? hbaseSiteFile : null;
  174. }
  175. }