[一起學Hive]之二十-自定義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元數據庫中,也可以保存在一個配置文件中。爲了方便起見,這裏使用配置文件來保存。

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

package com.lxw1234.hive.auth;


 


 


import java.io.BufferedReader;


import java.io.File;


import java.io.FileReader;


import java.io.IOException;


import java.security.MessageDigest;


import java.security.NoSuchAlgorithmException;


 


import javax.security.sasl.AuthenticationException;


 


import org.apache.hadoop.conf.Configuration;


import org.apache.hadoop.hive.conf.HiveConf;


import org.apache.hive.service.auth.PasswdAuthenticationProvider;


 


public class CustomHiveServer2Auth implements PasswdAuthenticationProvider  {


    @Override


    public void Authenticate(String username, String password)


            throws AuthenticationException {


        


        boolean ok = false;


        String passMd5 = new MD5().md5(password);


        HiveConf hiveConf = new HiveConf();


        Configuration conf = new Configuration(hiveConf);


        String filePath = conf.get("hive.server2.custom.authentication.file");


        System.out.println("hive.server2.custom.authentication.file [" + filePath + "] ..");


        File file = new File(filePath);


        BufferedReader reader = null;


        try {


            reader = new BufferedReader(new FileReader(file));


            String tempString = null;


            while ((tempString = reader.readLine()) != null) {


                String[] datas = tempString.split(",", -1);


                if(datas.length != 2) continue;


                //ok


                if(datas[0].equals(username) && datas[1].equals(passMd5)) {


                    ok = true;


                    break;


                }


            }


            reader.close();


        } catch (Exception e) {


            e.printStackTrace();


            throw new AuthenticationException("read auth config file error, [" + filePath + "] ..", e);


        } finally {


            if (reader != null) {


                try {


                    reader.close();


                } catch (IOException e1) {}


            }


        }


        if(ok) {


            System.out.println("user [" + username + "] auth check ok .. ");


        } else {


            System.out.println("user [" + username + "] auth check fail .. ");


            throw new AuthenticationException("user [" + username + "] auth check fail .. ");


        }


    }


    


    //MD5加密


    class MD5 {


        private MessageDigest digest;


        private char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};


        public MD5() {


            try {


              digest = MessageDigest.getInstance("MD5");


            } catch (NoSuchAlgorithmException e) {


              throw new RuntimeException(e);


            }


        }


        


         public String md5(String str) {


            byte[] btInput = str.getBytes();


            digest.reset();


            digest.update(btInput);


            byte[] md = digest.digest();


            // 把密文轉換成十六進制的字符串形式


            int j = md.length;


            char strChar[] = new char[j * 2];


            int k = 0;


            for (int i = 0; i < j; i++) {


                byte byte0 = md[i];


                strChar[k++] = hexDigits[byte0 >>> 4 & 0xf];


                strChar[k++] = hexDigits[byte0 & 0xf];


            }


            return new String(strChar);


        }


    }


    


}


將上面的程序打包成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元數據庫,每次認證時候查詢即可。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章