登录验证解决方案
一 、使用beeline 远程连接
启动hive 服务 和hiver server2 服务
hive --service metastore &
hive --service hiveserver2 &
使用beeline连接hive
beeline
!connect jdbc:hive2://master01.hadoop:10000

默认使用beeline连接没有验证,用户名和密码也不用输入直接回车就行

二、在hive-site.xml文件中配置参数开启权限认证
在hive-site.xml开启权限认证设置
hive.server2.enable.doAs= false

hive.security.authorization.enabled 设置为true
hive.security.authorization.createtable.owner.grants 设置为ALL
hive.server2.authentication 设置为CUSTOM



三. 重启 hive和 hive server2服务,再次无密码登录
jps获取两个服务的id

终止服务
kill 5553
kill 5421
重新启动
hive --service metastore&
hive --service hiveserver2&


使用beeline连接hive
beeline
!connect jdbc:hive2://master01.hadoop:10000

我们看到现在已经开启了验证,没有输入密码是无法登录成功的
四、编辑自定义类代码
package hive;
import javax.security.sasl.AuthenticationException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.slf4j.Logger;
public class CustomPasswordAuthenticator implements org.apache.hive.service.auth.PasswdAuthenticationProvider{
private Logger LOG=org.slf4j.LoggerFactory.getLogger(CustomPasswordAuthenticator.class);
private static final String HIVE_JDBC_PASSWD_AUTH_PREFIX="hive.jdbc_passwd.auth.%s";
private Configuration conf=null;
@Override
public void Authenticate(String userName, String passwd) throws AuthenticationException {
LOG.info("user:"+userName+"try login");
String passwdConf=getConf().get(String.format(HIVE_JDBC_PASSWD_AUTH_PREFIX,userName));
if (passwdConf==null){
String message="user's ACL configration is not found. user:"+userName;
throw new AuthenticationException(message);
}
if(!passwd.equals(passwdConf)){
String message="user name and password is mismatch. user:"+userName;
throw new AuthenticationException(message);
}
}
public Configuration getConf(){
if(conf==null){
this.conf=new Configuration(new HiveConf());
}
return conf;
}
public void setConf(Configuration conf){
this.conf=conf;
}
}
将上面的类打成jar包 hiveServer2Auth.jar,上传到hive的lib目录下,
编辑hive-site.xml
hive.server2.custom.authentication.class
hive.CustomPasswordAuthenticator
hive-site.xml中 配置用户名 密码(添加)
hive.jdbc_passwd.auth.zhangsan
123456
hive.jdbc_passwd.auth.lisi
111111
五. 重启 hive和 hive server2服务,再次使用密码登录
终止服务
kill 进程号
重新启动
hive --service metastore&
hive --service hiveserver2&
使用zhangsan 和 lisi 来进行登录验证
beeline
!connect jdbc:hive2://master01.hadoop:10000


权限解决方案(SQL Standards )
一 、使用账户 zhangsan 登录创建表
create database zhangsan_db;
use zhangsan_db;
create table customer_order(id int,price decimal(18,2));
insert into customer_order values(1,19.2);
select *from customer_order;
exit;

创建库 和创建表 都没有问题
插入操作
insert into customer_order values(2,19.2);
查报错,权限问题

我们查看该文件的权限和临时目录权限

hadoop fs -ls /tmp/hive/

我们看到在正式数据下文件都是默认的linux登录用户hadoop,而临时目录下的权限则是我们定义的CUSTOM用户,这个是有区别的,所以我们使用lisi去插入zhagnsan创建的表是没有权限的。
三、设置hive-site.xml 及创建hive 管理员账户
1.我们使用CUSTOMN方式定义一个管理员账户 admin,该账户用以beeline方式验证,
编辑hive-site.xml设置如下
hive.server2.enable.doAs= false
hive.users.in.admin.role 使用逗号分隔的用户列表,列表中的用户为管理员角色
hive.security.authorization.manager =org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory
hiveserver2相关的设置如下
hive.security.authorization.enabled=true
hive.security.authenticator.manager=org.apache.hadoop.hive.ql.security.SessionStateUserAuthenticator


四. 重启 hive和 hive server2服务,再次使用admin账户登录
终止服务
kill 进程号
重新启动
hive --service metastore&
hive --service hiveserver2&
1.使用hive定义的管理员 admin进行登录
beeline
!connect jdbc:hive2://master01.hadoop:10000

查看当前登录上了的角色
show current roles;

切换角色到admin(只有在admin组的用户才可以,这里zhangsan lisi 是没有这个权限的)
set role admin;
show current roles;

加入我们在zhangsan账户下使用 set role admin 则会引发错误如下

查看当前所有角色(只有管理员有权限)
show roles;

2. 使用admin授权 zhangsan lisi
set role admin;
grant select on zhangsan_db.customer_order to user zhangsan;
grant insert on zhangsan_db.customer_order to user lisi;

取消授权(如果没有授予过相应权限 这里取消会报错误)
revoke insert on table zhangsan_db.customer_order from user zhangsan;
revoke all on table zhangsan_db.customer_order from user zhangsan;
查看当前登录用户名(展现系统linux用户 而不是我们CUSTOM的用户)
set system:user.name
具体如何使用sql 可用从网上查看
我们到hive库中查看授权细节:
select db.NAME as db,tb.TBL_NAME ,pri.GRANTOR,GRANTOR_TYPE,PRINCIPAL_NAME,TBL_PRIV
from DBS as db
inner join TBLS as tb on tb.DB_ID=db.DB_ID
inner join TBL_PRIVS as pri on pri.TBL_ID=tb.TBL_ID

网友评论