hive开放平台设计--使用CUSTOM方式实现hiveserver2的登录管理

HiverServer2支持远程多客户端的并发和认证,支持通过JDBC、Beeline等连接操作,是一个比较方便并且安全的方式。

但是自己的开发集群上面为了方便开发都是做了不做权限监控的设定,即设定

<property>
    <name>hive.security.authorization.enabled</name>
    <value>false</value>
    <description>enable or disable the Hive client authorization</description>
  </property>

这样在使用hiveserver2连接的时候无论你输不输入用户名密码,都会正常进入,并且能进行各种操作。

但是这个明显是不安全的,毕竟正常情况下,如何设定需要校验用户登录呢,修改hive-site.xml中代码:

    <property>
        <name>hive.security.authorization.enabled</name>
        <value>false</value>
        <description>enable or disable the Hive client authorization</description>
   </property>
   <property>
       <name>hive.server2.thrift.client.user</name>
       <value>用户名</value>
       <description>Username to use against thrift client</description>
  </property>
  <property>
      <name>hive.server2.thrift.client.password</name>
      <value>用户名密码</value>
      <description>Password to use against thrift client</description>
  </property>

这样,就可以用设定的用户名密码进行登录了。

但是,现在我需要做的是一个hive的开放平台,多用户管理。
这种方式并不适用。

现有几种种方式:
1、采用PAM的模式
2、采用kerberos验证
3、自定义验证

现在使用hive的自定义验证:

编写用户权限验证的类:
需要实现org.apache.hive.service.auth.PasswdAuthenticationProvider接口,
代码如下:这里直接引用了https://www.cnblogs.com/1130136248wlxk/articles/5519285.html

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);
    }
}   
}

打包,上传至HIVE_HOME下的lib中。

然后修改配置:

<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value>
</property>
<property>
<name>hive.server2.custom.authentication.class</name>
<value>自己编写的类的引用路径</value>
</property>
<property>
<name>hive.server2.custom.authentication.file</name>
<value>校验文本的路径</value>
</property>

然后,校验文本的格式是

用户名,密码的md5加密

很重要的一点,core-site.xml中必须设定该登录用户拥有冒用hdfs用户的权限,为方便起见,此处不做校验,直接设定任意用户都可冒用.

<property>
<name>hadoop.proxyuser.hdfs.users</name>
<value>*</value>
<description>Allow the superuser adorechen to impersonate users of hadoop, hive, anonymous</description>
</property>

以及该登录用户需要在/user/hive/warehouse和/tmp拥有读写的权限.

这样便可以在连接hiveserver2时进行用户校验了
此处的用户名密码校验文本是存储在文档中的,它也可以存储在关系型数据库中,还没有测试过,就暂时不放出来了。

此文仅仅是做了登录管理,对hive中的权限管理下次再写

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值