当前位置:Gxlcms > mysql > 自定义Hive权限控制(3)扩展Hive以实现自定义权限控制

自定义Hive权限控制(3)扩展Hive以实现自定义权限控制

时间:2021-07-01 10:21:17 帮助过:9人阅读

简介 前两篇文章已经将需要的数据进行了准备,比如用户权限配置信息等。本节主要介绍我们的使用场景,因为使用场景的问题,我们只针对select进行相应的 权限控制 ,insert,delete,drop等动作从数据库层面上进行了限定,非本部门的人员是只拥有查询权限的。

简介
前两篇文章已经将需要的数据进行了准备,比如用户权限配置信息等。本节主要介绍我们的使用场景,因为使用场景的问题,我们只针对select进行相应的权限控制,insert,delete,drop等动作从数据库层面上进行了限定,非本部门的人员是只拥有查询权限的。所以在处理上会相对简单一些。
首先,建立一个工具包,用来处理相应的数据方面的请求。主要是获取用户权限的对应关系,并组织成我需要的格式。
包括3个类:

  1. HiveTable.java是针对hive的table建立的对象类。MakeMD5.Java 是针对MD5密码加密使用的工具类。UserAuthDataMode.java 是用于获取用户权限的方法类,本类实现了按照需要的格式获取数据库中的信息。

HiveTable类
  1. package com.anyoneking.www;?import java.util.ArrayList;import java.util.List;?public class HiveTable {
  2. private int id ;
  3. private String tableName ;
  4. private int dbid ;
  5. private String dbName ;
  6. private List partitionList = new ArrayList();
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getTableName() {
  14. return tableName;
  15. }
  16. public void setTableName(String tableName) {
  17. this.tableName = tableName;
  18. }
  19. public int getDbid() {
  20. return dbid;
  21. }
  22. public void setDbid(int dbid) {
  23. this.dbid = dbid;
  24. }
  25. public String getDbName() {
  26. return dbName;
  27. }
  28. public void setDbName(String dbName) {
  29. this.dbName = dbName;
  30. }
  31. public List getPartitionList() {
  32. return partitionList;
  33. }
  34. public void setPartitionList(List partitionList) {
  35. this.partitionList = partitionList;
  36. }?
  37. public String getFullName(){
  38. return this.dbName+"."+this.tableName;
  39. }}

UserAuthDataModel.java
  1. package com.anyoneking.www;?import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;?import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.hadoop.hive.conf.HiveConf;import org.apache.hadoop.hive.ql.Driver;/**?* 用户认证类,用于从数据库中提取相关的信息。?* @author songwei?*?*/public class UserAuthDataMode {
  2. static final private Log LOG = LogFactory.getLog(Driver.class.getName());
  3. private HiveConf conf ;
  4. private boolean isSuperUser = false;
  5. private Map allTableMap =new HashMap();
  6. //auth db name List
  7. private List dbNameList = new ArrayList();
  8. //auth table name List ex:{"dbName.tableName":HiveTable}
  9. private Map tableMap = new HashMap();?
  10. //auth table excludeColumnList ex:{"dbName.tableName":["phone"]}
  11. private Map> excludeColumnList = new HashMap>();
  12. //auth table includeColumnList ex:{"dbName.tableName":["ptdate","ptchannel"]}
  13. private Map> includeColumnList = new HashMap>();?
  14. private List ptchannelValueList = new ArrayList();?
  15. private String userName;
  16. private String password;
  17. private Connection conn ;
  18. private int userid ;
  19. private int maxMapCount =16;
  20. private int maxRedCount =16;?
  21. private void createConn() throws Exception{
  22. Class.forName("com.mysql.jdbc.Driver");
  23. String dbURL = HiveConf.getVar(this.conf,HiveConf.ConfVars.KUXUN_HIVESERVER_URL);
  24. String dbUserName = HiveConf.getVar(this.conf,HiveConf.ConfVars.KUXUN_HIVESERVER_USER);
  25. String dbPassword = HiveConf.getVar(this.conf,HiveConf.ConfVars.KUXUN_HIVESERVER_PASSWORD);
  26. this.conn = DriverManager.getConnection(dbURL,dbUserName, dbPassword);
  27. //this.conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","test", "tset");
  28. }?
  29. public UserAuthDataMode(String userName,String password,HiveConf conf) throws Exception{
  30. this.userName = userName ;
  31. this.password = password ;
  32. this.conf = conf;
  33. this.createConn();
  34. }?
  35. private ResultSet getResult(String sql) throws Exception{
  36. Statement stmt = conn.createStatement();
  37. ResultSet rs = stmt.executeQuery(sql);
  38. return rs;
  39. }?
  40. private void checkUser() throws Exception{
  41. MakeMD5 md5 = new MakeMD5();
  42. String sql = "select username,password,id,is_superuser from auth_user where username='"+this.userName+"'";
  43. LOG.debug(sql);
  44. this.password = md5.makeMD5(this.password);
  45. ResultSet rs= this.getResult(sql);
  46. int size =0 ;
  47. boolean flag = false ;
  48. if(size != 0){
  49. throw new Exception("username is error");
  50. }
  51. while(rs.next()){
  52. size +=1 ;
  53. this.userid = rs.getInt("id");
  54. int superUser = rs.getInt("is_superuser");
  55. if (superUser == 1){
  56. this.isSuperUser = true ;
  57. }else{
  58. this.isSuperUser = false ;
  59. }
  60. String db_password = rs.getString("password");
  61. if(db_password.equals(this.password)){
  62. flag = true ;
  63. }
  64. }
  65. if(size 0){
  66. String[] pt = ptInfo.split(",");
  67. ht.setPartitionList(Arrays.asList(pt));
  68. }
  69. this.allTableMap.put(tblid, ht);
  70. }?
  71. //处理有权限的db信息
  72. String dbSql = " select t2.hivedb_id,(select name from hive_db where id = t2.hivedb_id) dbname"
  73. +" from hive_user_auth t1 join hive_user_auth_dbGroups t2"
  74. +" on (t1.id = t2.hiveuserauth_id)"
  75. +"where t1.user_id ="+this.userid ;
  76. ResultSet dbrs = this.getResult(dbSql);
  77. while(dbrs.next()){
  78. this.dbNameList.add(dbrs.getString("dbname"));
  79. }?
  80. //处理有权限的表信息
  81. String tableSql = "select t2.hivetable_id "
  82. +"from hive_user_auth t1 join hive_user_auth_tableGroups t2 "
  83. +"on (t1.id = t2.hiveuserauth_id) "
  84. +"where t1.user_id ="+this.userid ;
  85. ResultSet tablers = this.getResult(tableSql);
  86. while(tablers.next()){
  87. int tableID = tablers.getInt("hivetable_id");
  88. LOG.debug("-----"+tableID);
  89. HiveTable ht = this.allTableMap.get(tableID);
  90. LOG.debug("---table_name--"+ht.getTableName());
  91. String tableFullName = ht.getFullName();
  92. LOG.debug(tableFullName);
  93. this.tableMap.put(tableFullName, ht);
  94. }?
  95. //处理不允许操作的列
  96. String exSql = "select col.name,col.table_id,col.column "
  97. +"from hive_user_auth t1 join hive_user_auth_exGroups t2 "
  98. +"on (t1.id = t2.hiveuserauth_id) "
  99. +"join hive_excludecolumn col "
  100. +"on (t2.excludecolumn_id = col.id) "
  101. +"where t1.user_id ="+this.userid ;
  102. ResultSet exrs = this.getResult(exSql);
  103. while(exrs.next()){
  104. int tableID = exrs.getInt("table_id");
  105. String column = exrs.getString("column");
  106. HiveTable ht = this.allTableMap.get(tableID);
  107. String tableFullName = ht.getFullName();
  108. String[] columnList = column.split(",");
  109. this.excludeColumnList.put(tableFullName, Arrays.asList(columnList));
  110. }?
  111. //处理必须包含的列
  112. String inSql = "select col.name,col.table_id,col.column "
  113. +"from hive_user_auth t1 join hive_user_auth_inGroups t2 "
  114. +"on (t1.id = t2.hiveuserauth_id) "
  115. +"join hive_includecolumn col "
  116. +"on (t2.includecolumn_id = col.id) "
  117. +"where t1.user_id ="+this.userid ;
  118. ResultSet inrs = this.getResult(inSql);
  119. while(inrs.next()){
  120. int tableID = inrs.getInt("table_id");
  121. String column = inrs.getString("column");
  122. HiveTable ht = this.allTableMap.get(tableID);
  123. String tableFullName = ht.getFullName();
  124. String[] columnList = column.split(",");
  125. this.includeColumnList.put(tableFullName, Arrays.asList(columnList));
  126. }?
  127. //处理ptchannel的value
  128. String ptSql = "select val.name "
  129. +"from hive_user_auth t1 join hive_user_auth_ptGroups t2 "
  130. +"on (t1.id = t2.hiveuserauth_id) "
  131. +"join hive_ptchannel_value val "
  132. +"on (t2.hiveptchannelvalue_id = val.id) "
  133. +"where t1.user_id ="+this.userid ;
  134. ResultSet ptrs = this.getResult(ptSql);
  135. while(ptrs.next()){
  136. String val = ptrs.getString("name");
  137. this.ptchannelValueList.add(val);
  138. }
  139. }?
  140. public int getMaxMapCount() {
  141. return maxMapCount;
  142. }?
  143. public void setMaxMapCount(int maxMapCount) {
  144. this.maxMapCount = maxMapCount;
  145. }?
  146. public int getMaxRedCount() {
  147. return maxRedCount;
  148. }?
  149. public void setMaxRedCount(int maxRedCount) {
  150. this.maxRedCount = maxRedCount;
  151. }?
  152. public void run() throws Exception{
  153. this.checkUser();
  154. this.parseAuth();
  155. this.checkData();
  156. this.modifyConf();
  157. this.clearData();
  158. }?
  159. public void clearData() throws Exception{
  160. this.conn.close();
  161. }?
  162. private void modifyConf(){
  163. this.conf.setInt("mapred.map.tasks",this.maxMapCount);
  164. //this.conf.setInt("hive.exec.reducers.ma", this.maxRedCount);
  165. HiveConf.setIntVar(this.conf,HiveConf.ConfVars.MAXREDUCERS,this.maxRedCount);
  166. }?
  167. private void checkData(){
  168. LOG.debug(this.allTableMap.keySet().size());
  169. LOG.debug(this.tableMap.keySet().size());
  170. LOG.debug(this.dbNameList.size());
  171. LOG.debug(this.excludeColumnList.size());
  172. LOG.debug(this.includeColumnList.size());
  173. LOG.debug(this.ptchannelValueList.size());
  174. }????
  175. public static void main(String[] args) throws Exception{
  176. UserAuthDataMode ua = new UserAuthDataMode("swtest","swtest",null);
  177. ua.run();
  178. }?
  179. public List getDbNameList() {
  180. return dbNameList;
  181. }?
  182. public void setDbNameList(List dbNameList) {
  183. this.dbNameList = dbNameList;
  184. }?
  185. public Map getTableMap() {
  186. return tableMap;
  187. }?
  188. public void setTableMap(Map tableMap) {
  189. this.tableMap = tableMap;
  190. }?
  191. public Map> getExcludeColumnList() {
  192. return excludeColumnList;
  193. }?
  194. public void setExcludeColumnList(Map> excludeColumnList) {
  195. this.excludeColumnList = excludeColumnList;
  196. }?
  197. public Map> getIncludeColumnList() {
  198. return includeColumnList;
  199. }?
  200. public void setIncludeColumnList(Map> includeColumnList) {
  201. this.includeColumnList = includeColumnList;
  202. }?
  203. public List getPtchannelValueList() {
  204. return ptchannelValueList;
  205. }?
  206. public void setPtchannelValueList(List ptchannelValueList) {
  207. this.ptchannelValueList = ptchannelValueList;
  208. }?}

MakeMD5.java

  1. package com.anyoneking.www;?import java.math.BigInteger;import java.security.MessageDigest;?public class MakeMD5 {
  2. public String makeMD5(String password) {
  3. MessageDigest md;
  4. try {
  5. // 生成一个MD5加密计算摘要
  6. md = MessageDigest.getInstance("MD5"); // 同样可以使用SHA1
  7. // 计算md5函数
  8. md.update(password.getBytes());
  9. // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
  10. // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
  11. String pwd = new BigInteger(1, md.digest()).toString(16); // 参数也可不只用16可改动,当然结果也不一样了
  12. return pwd;
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. return password;
  17. }?
  18. public static void main(String[] args) {
  19. MakeMD5 md5 = new MakeMD5();
  20. md5.makeMD5("swtest");
  21. }}

人气教程排行