cdh 配置

/opt/cloudera/parcels/CDH/lib/hive/lib/HiveServer2Auth.jar

hive设置引擎为tez hive启动hiveserver2_java

 

hdp

hive设置引擎为tez hive启动hiveserver2_hive设置引擎为tez_02

Run as end user instead of Hive user     true

hive设置引擎为tez hive启动hiveserver2_java_03

hive设置引擎为tez hive启动hiveserver2_hive_04

 

 

 

 

 

 

 

 

HiveServer2提供了JDBC链接操作Hive的功能,非常实用,但如果在使用HiveServer2时候,不注意安全控制,将非常危险,因为任何人都可以作为超级用户来操作Hive及HDFS数据。
比如:在配置HiveServer2的时候,hive.server2.authentication=NONE,表示没有用户认证。

使用beeline,模拟成超级用户hadoop,成功连接到HiveServer2.
创建数据库lxw1234;
0: jdbc:hive2://localhost:10000> create database lxw1234;
No rows affected (0.157 seconds)
0: jdbc:hive2://localhost:10000>

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根目录的写权限:

因此,如果使用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
2.  
3.  
4. import
5. import
6. import
7. import
8. import
9. import
10.  
11. import
12.  
13. import
14. import
15. import
16.  
17. public class CustomHiveServer2Auth implements
18.     @Override
19. public void
20. throws
21.         
22. boolean ok = false;
23. new
24. new
25. new
26. get("hive.server2.custom.authentication.file");
27. out.println("hive.server2.custom.authentication.file [" + filePath + "] ..");
28. new
29. null;
30. try
31. new BufferedReader(new
32. null;
33. while ((tempString = reader.readLine()) != null) {

34. ",", -1);
35. if(datas.length != 2) continue;
36. //ok
37. if(datas[0].equals(username) && datas[1].equals(passMd5)) {

38. true;
39. break;
40.                 }
41.             }
42.             reader.close();
43. catch
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
51.             }
52.         }
53. if(ok) {

54. out.println("user [" + username + "] auth check ok .. ");
55. else
56. out.println("user [" + username + "] auth check fail .. ");
57. throw new AuthenticationException("user [" + username + "] auth check fail .. ");
58.         }
59.     }
60.     
61. //MD5加密
62. class
63. private
64. private char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
65. public
66. try
67. "MD5");
68. catch
69. throw new
70.             }
71.         }
72.         
73. public
74. byte[] btInput = str.getBytes();
75.             digest.reset();
76.             digest.update(btInput);
77. byte[] md = digest.digest();
78. // 把密文转换成十六进制的字符串形式
79. int
80. char strChar[] = new char[j * 2];
81. int
82. for (int
83. byte
84.                 strChar[k++] = hexDigits[byte0 >>> 4 & 0xf];
85.                 strChar[k++] = hexDigits[byte0 & 0xf];
86.             }
87. return new
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连接:

再次使用hadoop用户连接,输入空密码或者不正确的密码,将不会再链接到HiveServer。

HiveServer2的日志中打印出了认证失败的消息。

再试试使用用户lxw1234密码lxw1234_password连接:

日志中打印出user [lxw1234] auth check ok ..
同样,已经配置过的user用户密码password也可以正常连接:

使用Java JDBC连接HiveServer2,密码错误将不能正常连接:

 

密码正确时才可以正常查询:

 

 

这种方式做的HiveServer2用户认证,可以动态的增加、修改和删除用户及密码,只需要修改hive.server2.custom.authentication.file 配置文件即可。

当然,用户名和密码也可以保存在Hive元数据库,每次认证时候查询即可。