[一起學Hive]之二十-自定義HiveServer2的用戶安全認證


關鍵字:hiveserver2、用戶安全

HiveServer2提供了JDBC鏈接操作Hive的功能,非常實用,但如果在使用HiveServer2時候,不注意安全控制,將非常危險,因為任何人都可以作為超級用戶來操作Hive及HDFS數據。
比如:在配置HiveServer2的時候,hive.server2.authentication=NONE,表示沒有用戶認證。

hiveserver2

使用beeline,模擬成超級用戶hadoop,成功連接到HiveServer2.
創建數據庫lxw1234;
0: jdbc:hive2://localhost:10000> create database lxw1234;
No rows affected (0.157 seconds)
0: jdbc:hive2://localhost:10000>

hiveserver2

HDFS上也是以超級用戶hadoop創建的目錄。
再執行drop database,同樣沒問題。
0: jdbc:hive2://localhost:10000> drop database lxw1234;
No rows affected (0.142 seconds)
0: jdbc:hive2://localhost:10000>

如果是以普通用戶鏈接HiveServer2,執行創建數據庫,則會報權限錯誤,因為普通用戶沒有在Hive根目錄的寫權限:

hiveserver2

因此,如果使用HiverServer2來提供給用戶來鏈接Hive,必須啟用安全認證,也就是hive.server2.authentication的配置。

目前HiveServer2支持多種用戶安全認證方式:NONE,NOSASL, KERBEROS, LDAP, PAM ,CUSTOM等等。

本文介紹使用自定義的用戶認證方式,即CUSTOM;
如果將hive.server2.authentication設置成CUSTOM,則需要設置
hive.server2.custom.authentication.class來指定用於權限認證的類,這個類需要實現
org.apache.hive.service.auth.PasswdAuthenticationProvider接口。

我們將使用HiveServer2的用戶名和密碼保存起來,其中,密碼以32位小寫md5加密來保存,這個數據即可以保存在Hive元數據庫中,也可以保存在一個配置文件中。為了方便起見,這里使用配置文件來保存。

首先需要編寫用戶權限驗證的類:

  1. package com.lxw1234.hive.auth;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.security.MessageDigest;
  9. import java.security.NoSuchAlgorithmException;
  10.  
  11. import javax.security.sasl.AuthenticationException;
  12.  
  13. import org.apache.hadoop.conf.Configuration;
  14. import org.apache.hadoop.hive.conf.HiveConf;
  15. import org.apache.hive.service.auth.PasswdAuthenticationProvider;
  16.  
  17. public class CustomHiveServer2Auth implements PasswdAuthenticationProvider  {
  18.     @Override
  19.     public void Authenticate(String username, String password)
  20.             throws AuthenticationException {
  21.         
  22.         boolean ok = false;
  23.         String passMd5 = new MD5().md5(password);
  24.         HiveConf hiveConf = new HiveConf();
  25.         Configuration conf = new Configuration(hiveConf);
  26.         String filePath = conf.get("hive.server2.custom.authentication.file");
  27.         System.out.println("hive.server2.custom.authentication.file [" + filePath + "] ..");
  28.         File file = new File(filePath);
  29.         BufferedReader reader = null;
  30.         try {
  31.             reader = new BufferedReader(new FileReader(file));
  32.             String tempString = null;
  33.             while ((tempString = reader.readLine()) != null) {
  34.                 String[] datas = tempString.split(",", -1);
  35.                 if(datas.length != 2) continue;
  36.                 //ok
  37.                 if(datas[0].equals(username) && datas[1].equals(passMd5)) {
  38.                     ok = true;
  39.                     break;
  40.                 }
  41.             }
  42.             reader.close();
  43.         } catch (Exception e) {
  44.             e.printStackTrace();
  45.             throw new AuthenticationException("read auth config file error, [" + filePath + "] ..", e);
  46.         } finally {
  47.             if (reader != null) {
  48.                 try {
  49.                     reader.close();
  50.                 } catch (IOException e1) {}
  51.             }
  52.         }
  53.         if(ok) {
  54.             System.out.println("user [" + username + "] auth check ok .. ");
  55.         } else {
  56.             System.out.println("user [" + username + "] auth check fail .. ");
  57.             throw new AuthenticationException("user [" + username + "] auth check fail .. ");
  58.         }
  59.     }
  60.     
  61.     //MD5加密
  62.     class MD5 {
  63.         private MessageDigest digest;
  64.         private char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  65.         public MD5() {
  66.             try {
  67.               digest = MessageDigest.getInstance("MD5");
  68.             } catch (NoSuchAlgorithmException e) {
  69.               throw new RuntimeException(e);
  70.             }
  71.         }
  72.         
  73.          public String md5(String str) {
  74.             byte[] btInput = str.getBytes();
  75.             digest.reset();
  76.             digest.update(btInput);
  77.             byte[] md = digest.digest();
  78.             // 把密文轉換成十六進制的字符串形式
  79.             int j = md.length;
  80.             char strChar[] = new char[j * 2];
  81.             int k = 0;
  82.             for (int i = 0; i < j; i++) {
  83.                 byte byte0 = md[i];
  84.                 strChar[k++] = hexDigits[byte0 >>> 4 & 0xf];
  85.                 strChar[k++] = hexDigits[byte0 & 0xf];
  86.             }
  87.             return new String(strChar);
  88.         }
  89.     }
  90.     
  91. }
  92.  

將上面的程序打包成HiveServer2Auth.jar,放到$HIVE_HOME/lib下,
在hive-site.xml中設置以下參數:
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value>
</property>
<property>
<name>hive.server2.custom.authentication.class</name>
<value>com.lxw1234.hive.auth.CustomHiveServer2Auth</value>
</property>
<property>
<name>hive.server2.custom.authentication.file</name>
<value>/usr/local/apache-hive-0.13.1-bin/conf/hive.server2.users.conf</value>
</property>

在$HIVE_HOME/conf下新建文件hive.server2.users.conf,里面寫入內容:
[root@dev conf]# cat hive.server2.users.conf
lxw1234,48d9a656690e1b1bf5ddee4c12d1bbd7
user,5f4dcc3b5aa765d61d8327deb882cf99

其中,48d9a656690e1b1bf5ddee4c12d1bbd7為lxw1234_password的md5加密,
5f4dcc3b5aa765d61d8327deb882cf99為password的md5加密。

接下來,重新啟動HiveServer2,使用beeline連接:

hiveserver2

再次使用hadoop用戶連接,輸入空密碼或者不正確的密碼,將不會再鏈接到HiveServer。

hiveserver2

HiveServer2的日志中打印出了認證失敗的消息。

再試試使用用戶lxw1234密碼lxw1234_password連接:

hiveserver2

日志中打印出user [lxw1234] auth check ok ..
同樣,已經配置過的user用戶密碼password也可以正常連接:

hiveserver2

使用Java JDBC連接HiveServer2,密碼錯誤將不能正常連接:

hiveserver2

 

密碼正確時才可以正常查詢:

 

hiveserver2

 

這種方式做的HiveServer2用戶認證,可以動態的增加、修改和刪除用戶及密碼,只需要修改hive.server2.custom.authentication.file 配置文件即可。

當然,用戶名和密碼也可以保存在Hive元數據庫,每次認證時候查詢即可。

 

更多Hive相關的文章可查看 一起學Hive系列

您可以關注 lxw的大數據田地 ,或者 加入郵件列表 ,隨時接收博客更新的通知郵件。

 

 

轉載請注明:lxw的大數據田地 » [一起學Hive]之二十-自定義HiveServer2的用戶安全認證


免責聲明!

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



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