大数据Hive系列之HiveServer2用户安全认证

x33g5p2x  于2021-12-25 转载在 其他  
字(4.5k)|赞(0)|评价(0)|浏览(605)

1. 概述

1.1 目的

HiveServer2提供了JDBC链接操作Hive的功能,非常实用,但如果在使用HiveServer2时候,不注意安全控制,将非常危险,因为任何人都可以作为超级用户来操作Hive及HDFS数据。

1.2 认证方式

HiveServer2支持多种用户安全认证方式:NONE,NOSASL, KERBEROS, LDAP, PAM ,CUSTOM等等,本文采用CUSTOM。

2. 编写代码

  • 所需jar包

  • commons-logging-1.2.jar

  • hadoop-common-2.7.3.jar

  • hive-service-2.1.1.jar

2.1 编写权限认证类

  1. package org.apache.hive;
  2. import javax.security.sasl.AuthenticationException;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. import org.apache.hadoop.conf.Configurable;
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hive.service.auth.PasswdAuthenticationProvider;
  8. /** * 权限认证类 * * @author volitation * */
  9. public class CustomHiveServer2Auth implements PasswdAuthenticationProvider, Configurable {
  10. private static final Log LOG = LogFactory.getLog(CustomHiveServer2Auth.class);
  11. private Configuration conf = null;
  12. private static final String HIVE_JDBC_PASSWD_AUTH_PREFIX = "hive.jdbc_passwd.auth.%s";
  13. public CustomHiveServer2Auth() {
  14. init();
  15. }
  16. public void init() {
  17. }
  18. public void Authenticate(String userName, String passwd) throws AuthenticationException {
  19. LOG.info("user: " + userName + " try login.");
  20. String passwdMD5 = getConf().get(String.format(HIVE_JDBC_PASSWD_AUTH_PREFIX, userName));
  21. if (passwdMD5 == null) {
  22. String message = "user's ACL configration is not found. user:" + userName;
  23. LOG.info(message);
  24. throw new AuthenticationException(message);
  25. }
  26. String md5 = new MD5().md5(passwd);
  27. if (!md5.equals(passwdMD5)) {
  28. String message = "user name and password is mismatch. user:" + userName;
  29. throw new AuthenticationException(message);
  30. }
  31. LOG.info("user " + userName + " login system successfully.");
  32. }
  33. public Configuration getConf() {
  34. if (conf == null) {
  35. this.conf = new Configuration();
  36. }
  37. return conf;
  38. }
  39. public void setConf(Configuration arg0) {
  40. this.conf = arg0;
  41. }
  42. }

2.2 编写MD5加密类

  1. package org.apache.hive;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. /** * MD5加密类 * * @author volitation * */
  5. public class MD5 {
  6. private MessageDigest digest;
  7. private char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  8. public MD5() {
  9. try {
  10. digest = MessageDigest.getInstance("MD5");
  11. } catch (NoSuchAlgorithmException e) {
  12. throw new RuntimeException(e);
  13. }
  14. }
  15. public String md5(String str) {
  16. byte[] btInput = str.getBytes();
  17. digest.reset();
  18. digest.update(btInput);
  19. byte[] md = digest.digest();
  20. // 把密文转换成十六进制的字符串形式
  21. int j = md.length;
  22. char strChar[] = new char[j * 2];
  23. int k = 0;
  24. for (int i = 0; i < j; i++) {
  25. byte byte0 = md[i];
  26. strChar[k++] = hexDigits[byte0 >>> 4 & 0xf];
  27. strChar[k++] = hexDigits[byte0 & 0xf];
  28. }
  29. return new String(strChar);
  30. }
  31. public static void main(String[] args) {
  32. String pwd = new MD5().md5("NFJD1234");
  33. System.out.println(pwd);
  34. }
  35. }

2.3 配置pom,用Maven打jar包

  1. <dependencies>
  2. <dependency>
  3. <groupId>hive-service</groupId>
  4. <artifactId>hive-service</artifactId>
  5. <version>2.1.1</version>
  6. <scope>system</scope>
  7. <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/hive-service-2.1.1.jar</systemPath>
  8. </dependency>
  9. <dependency>
  10. <groupId>commons-logging</groupId>
  11. <artifactId>commons-logging</artifactId>
  12. <version>1.2</version>
  13. <scope>system</scope>
  14. <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/commons-logging-1.2.jar</systemPath>
  15. </dependency>
  16. <dependency>
  17. <groupId>hadoop-common</groupId>
  18. <artifactId>hadoop-common</artifactId>
  19. <version>2.7.3</version>
  20. <scope>system</scope>
  21. <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/hadoop-common-2.7.3.jar</systemPath>
  22. </dependency>
  23. </dependencies>

3. Hive配置

3.1 上传jar包

  • $ cp ~/hive-jar/hive-server2-2.1.1.jar /apps/svr/hive/apache-hive-2.1.1-bin/lib/

3.2 配置hive-site.xml

  • $ cd /apps/svr/hive/apache-hive-2.1.1-bin/ && vim conf/hive-site.xml
  1. <property>
  2. <name>hive.server2.thrift.port</name>
  3. <value>10000</value>
  4. </property>
  5. <property>
  6. <name>hive.server2.authentication</name>
  7. <value>CUSTOM</value>
  8. </property>
  9. <property>
  10. <name>hive.server2.custom.authentication.class</name>
  11. <value>org.apache.hive.CustomHiveServer2Auth</value>
  12. </property>
  13. <!-- username:hive ; password:hive!@#123 -->
  14. <property>
  15. <name>hive.jdbc_passwd.auth.hive</name>
  16. <value>84fea338063c80fde150cb17995056d3</value>
  17. <description/>
  18. </property>

4. HiveServer2启动验证

4.1 启动hiveserver2

  • $ hive --service hiveserver2 &

4.2 验证

$ beeline
beeline> !connect jdbc:hive2://192.168.9.87:10000 hive hive!@#123
0: jdbc:hive2://192.168.9.87:10000>

4.3 Web UI验证

http://192.168.9.87:10002

相关文章