hive權限管理之實踐


一、實踐心得

主要參考這個連接,里面說得也挺詳細的。http://www.aboutyun.com/thread-12549-1-1.html

總結如下:

1、若賦予用戶某個表的權限,查用戶在該表所屬數據庫的權限,是查詢不出來的,要指定到那張表
2、若要賦予用戶db1數據庫下的t1表權限,首先要在執行 use db1;
3、編寫鈎子函數時,經過我自己的測試,這邊是hive0.13版本,感覺非超級管理員的grant、revoke控制不了,而create role r_name是可以控制,證明該控制類是起作用的,不知道是HiveParser.TOK_XXXX有遺漏還是其他問題,或者可以直接用ast.getToken().getText()與"TOK_CREATEROLE"字符匹配,這樣是沒問題。
4、以上的hive權限控制,只適合於hive cli控制權限,若用jdbc、thrift接口或hue查詢頁面是不能起到權限控制的,所以不是完全安全的,只是用來防止用戶不小心做了不適合的事情,而不是防止壞人干壞事的。
5、倘若想更好更安全控制hive權限,可以使用Kerberos認證,聽說Kerberos很強大,並且可以管理hdfs與hbase等。

 

簡單歸納如下,方便以后查詢,主要分兩步,第一,修改配置文件;第二,熟悉授權語法。

 

二、修改配置文件

1、修改hive-site.xml

[html]  view plain  copy
 
  1. <!--參數調優-->  
  2. <property>   
  3.     <name>hive.exec.parallel</name>  
  4.     <value>true</value>  
  5.     <description>Whether to execute jobs in parallel</description>  
  6. </property>  
  7. <property>  
  8.     <name>hive.exec.parallel.thread.number</name>  
  9.     <value>16</value>  
  10.     <description>How many jobs at most can be executed in parallel</description>  
  11. </property>  
  12.   
  13. <!-- 權限配置-->  
  14. <!-- 開啟hive cli的控制權限 -->  
  15. <property>   
  16.     <name>hive.security.authorization.enabled</name>   
  17.     <value>true</value>   
  18.     <description>enable or disable the hive clientauthorization</description>  
  19. </property>  
  20. <!-- 定義表創建者的權限 -->  
  21. <property>   
  22.     <name>hive.security.authorization.createtable.owner.grants</name>   
  23.     <value>ALL</value>   
  24.     <description>  
  25.             the privileges automatically granted to the owner whenever a table gets created.   
  26.     </description>  
  27. </property>  
  28. <!-- 在做類似drop partition操作時,metastore是否要認證權限,默認是false -->  
  29. <property>    
  30.     <name>hive.metastore.authorization.storage.checks</name>    
  31.     <value>true</value>    
  32.     <description>  
  33.         Should the metastore do authorization checks against    
  34.         the underlying storage for operations like drop-partition (disallow    
  35.         the drop-partition if the user in question doesn't have permissions    
  36.         to delete the corresponding directory on the storage).  
  37.     </description>    
  38. </property>  
  39. <!-- 非安全模式,設置為true會令metastore以客戶端的用戶和組權限執行DFS操作,默認是false,這個屬性需要服務端和客戶端同時設置 -->  
  40. <property>  
  41.     <name>hive.metastore.execute.setugi</name>  
  42.     <value>false</value>  
  43.     <description>  
  44.         In unsecure mode, setting this property to true will cause the metastore to execute DFS operations using the client's reported user   
  45.         and group permissions. Note that this property must be set on both the client   
  46.         and server sides. Further note that its best effort. If client sets its to true and server sets it to false, client setting will be ignored.  
  47.     </description>  
  48. </property>    
  49. <!-- 配置超級管理員,需要自定義控制類繼承這個AbstractSemanticAnalyzerHook-->  
  50. <property>  
  51.    <name>hive.semantic.analyzer.hook</name>  
  52.    <value>com.kent.test.AuthorityHook</value>  
  53. </property>  
  54.   
  55. <!-- 假如出現以下錯誤:  
  56.      Error while compiling statement: FAILED: SemanticException The current builtin authorization in Hive is incomplete and disabled.  
  57.      需要配置下面的屬性 -->  
  58. <property>  
  59.     <name>hive.security.authorization.task.factory</name>  
  60.     <value>org.apache.hadoop.hive.ql.parse.authorization.HiveAuthorizationTaskFactoryImpl</value>  
  61. </property>  
 

2、自定義控制類(繼承AbstractSemanticAnalyzerHook)

[java]  view plain  copy
 
  1. package com.kent.test;  
  2. import org.apache.hadoop.hive.ql.parse.ASTNode;  
  3. import org.apache.hadoop.hive.ql.parse.AbstractSemanticAnalyzerHook;  
  4. import org.apache.hadoop.hive.ql.parse.HiveParser;  
  5. import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHookContext;  
  6. import org.apache.hadoop.hive.ql.parse.SemanticException;  
  7. import org.apache.hadoop.hive.ql.session.SessionState;  
  8. public class  AuthorityHook extends AbstractSemanticAnalyzerHook {  
  9. private static String[] admin = {"admin", "root"};  
  10.   
  11. @Override  
  12. public ASTNode preAnalyze(HiveSemanticAnalyzerHookContext context,ASTNode ast) throws SemanticException {  
  13. switch (ast.getToken().getType()) {  
  14. case HiveParser.TOK_CREATEDATABASE:  
  15. case HiveParser.TOK_DROPDATABASE:  
  16. case HiveParser.TOK_CREATEROLE:  
  17. case HiveParser.TOK_DROPROLE:  
  18. case HiveParser.TOK_GRANT:  
  19. case HiveParser.TOK_REVOKE:  
  20. case HiveParser.TOK_GRANT_ROLE:  
  21. case HiveParser.TOK_REVOKE_ROLE:  
  22.     String userName = null;  
  23.     if (SessionState.get() != null&&SessionState.get().getAuthenticator() != null){  
  24.         userName=SessionState.get().getAuthenticator().getUserName();  
  25.     }  
  26.     if (!admin[0].equalsIgnoreCase(userName) && !admin[1].equalsIgnoreCase(userName)) {  
  27.         throw new SemanticException(userName + " can't use ADMIN options, except "   
  28.                             + admin[0]+","+admin[1] +".");  
  29.     }         
  30.     break;  
  31. default:  
  32.     break;  
  33. }  
  34.     return ast;  
  35.     }  
  36. public static void main(String[] args) throws SemanticException {  
  37.     String[] admin = {"admin", "root"};  
  38.     String userName = "root";  
  39.     for(String tmp: admin){  
  40.         System.out.println(tmp);  
  41.         if (!tmp.equalsIgnoreCase(userName)) {  
  42.             throw new SemanticException(userName + " can't use ADMIN options, except "   
  43.                                 + admin[0]+","+admin[1] +".");  
  44.         }         
  45.     }  
  46. }  
  47.   
  48. }  

三、權限控制語法

1、角色權限控制

[sql]  view plain  copy
 
  1. --創建和刪除角色  
  2. create role role_name;  
  3. drop role role_name;  
  4. --展示所有roles  
  5. show roles  
  6. --賦予角色權限  
  7. grant select on database db_name to role role_name;    
  8. grant select on [table] t_name to role role_name;    
  9. --查看角色權限  
  10. show grant role role_name on database db_name;   
  11. show grant role role_name on [table] t_name;   
  12. --角色賦予用戶  
  13. grant role role_name to user user_name  
  14. --回收角色權限  
  15. revoke select on database db_name from role role_name;  
  16. revoke select on [table] t_name from role role_name;  
  17. --查看某個用戶所有角色  
  18. show role grant user user_name;  

2、用戶角色控制

 
1)權限控制表
[sql]  view plain  copy
 
  1. 操作(opera)           解釋  
  2. ALL             所有權限  
  3. ALTER           允許修改元數據(modify metadata data of  object)---表信息數據  
  4. UPDATE          允許修改物理數據(modify physical data of  object)---實際數據  
  5. CREATE          允許進行Create操作  
  6. DROP            允許進行DROP操作  
  7. INDEX           允許建索引(目前還沒有實現)  
  8. LOCK            當出現並發的使用允許用戶進行LOCK和UNLOCK操作  
  9. SELECT          允許用戶進行SELECT操作  
  10. SHOW_DATABASE   允許用戶查看可用的數據庫  


 
2)語法
[sql]  view plain  copy
 
  1. --賦予用戶權限  
  2. grant opera on database db_name to user user_name;    
  3. grant opera on [table] t_name to user user_name;   
  4. --回收用戶權限  
  5. revoke opera on database db_name from user user_name;  
  6. --查看用戶權限  
  7. show grant user user_name on database db_name;       
  8. show grant user user_name on [table] t_name;   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM