hue中实现权限的管理

来源:互联网 发布:数据共享交换系统 编辑:程序博客网 时间:2024/04/28 18:48

一、jar包代码:


注意:

1.hdfs和hive的jar引用最好和你的cdh中支持的版本一致

2.下面代码中case后的数字,可能会根据你版本的不同而发生改变,但是你可以从log日志中查找到对应的编码(/opt/log/hive);

package com.xx.hive;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hive.ql.parse.ASTNode;
import org.apache.hadoop.hive.ql.parse.AbstractSemanticAnalyzerHook;
import org.apache.hadoop.hive.ql.parse.HiveParser;
import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHookContext;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.hadoop.hive.ql.session.SessionState;


public class HiveAdmin extends AbstractSemanticAnalyzerHook {
private static String admin = "admin";
private static final Log LOG = LogFactory.getLog(HiveAdmin.class);


String userName = null;
String Authenticator="null";
@Override
//添加管理员权限
public ASTNode preAnalyze(HiveSemanticAnalyzerHookContext context,
ASTNode ast) throws SemanticException {

switch (ast.getToken().getType()) { //这里的权限编码可能会根据机器的不同而改变

case 645://create database wlt; //这里虽然是有创建数据库的权限,但是还要grant权限create给具体的用户和角色才能创建数据库
adminprovilige();
break;
case 650:// CREATE ROLE role1; //创建角色
adminprovilige();


break;
case 679:// DROP ROLE role1//删除角色
adminprovilige();

break;
case 696: //grant select on database wl to role role1; //授予查询权限
adminprovilige();


break;
case 797 ://revoke create from role selectrole; //收回角色创建的权限 revoke select on database wl from role role1;
adminprovilige();


break;
case 698: //grant role role1 to user admin; //把用户admin分配给角色role1
adminprovilige();


break;
case 798:// REVOKE ROLE role1 FROM user admin; 从角色里把该用户给删除掉
adminprovilige();


break;
case 822://show grant;
adminprovilige();


break;



default:
break;
}

int tokeType=ast.getToken().getType();
LOG.error("tokeType:"+tokeType+"=======user " + SessionState.get().getAuthenticator().getUserName()+" Authenticator "+SessionState.get().getAuthenticator().toString());
System.out.println("tokeType:"+tokeType+"===================user: " + SessionState.get().getAuthenticator().getUserName()+" Authenticator "+SessionState.get().getAuthenticator().toString());
return ast;
// return super.preAnalyze(context, ast);
}

public void adminprovilige() throws SemanticException
{
if (SessionState.get() != null
&& SessionState.get().getAuthenticator() != null) {
Authenticator = SessionState.get().getAuthenticator().toString();
userName = SessionState.get().getAuthenticator().getUserName();

}
LOG.error("admin:"+admin+"username:"+userName);
if (!admin.equalsIgnoreCase(userName)) {
System.out.println("===================can't use ADMIN options, except");
LOG.error("tokeType:不一致");
throw new SemanticException(userName+ " can't use ADMIN options, except " + admin + ".");


}
}



// @Override
// public void postAnalyze(HiveSemanticAnalyzerHookContext context,
// List<Task<? extends Serializable>> rootTasks)
// throws SemanticException {
// console.printInfo("!! SimpleSemanticPreAnalyzerHook postAnalyze called !!");
// super.postAnalyze(context, rootTasks);
// }

}

二、配置hive-site.xml文件

(一).如果只针对hue生效的话,就只设置hive-site.xml 的 HiveServer2 高级配置代码段(安全阀) ;

(二).如果还针对hive生效的话,还得设置hive-site.xml 的 Hive 客户端高级配置代码段(安全阀);
具体配置如下:
找到chd的hive菜单,进入到配置选项,搜索hive-site.xml 的 HiveServer2 高级配置代码段(安全阀)
#功能:开启权限。
<property>
<name>hive.security.authorization.enabled</name>
<value>true</value>
<description>enable or disable the hive client authorization</description>
</property>
#表的创建者对表拥有所有权限
<property>
<name>hive.security.authorization.createtable.owner.grants</name>
<value>ALL</value>
<description>the privileges automatically granted to the owner whenever a table gets created. An example like"select,drop" will grant select and drop privilege to the owner of the table</description>
</property>
#进行权限控制的配置。
<property>
<name>hive.security.authorization.task.factory</name>
<value>org.apache.hadoop.hive.ql.parse.authorization.HiveAuthorizationTaskFactoryImpl</value>
</property>
#根据代码实现的管理员用户,注意这里一定要加上包名和类名,否则会提示类找不到
<property>
<name>hive.semantic.analyzer.hook</name>
<value>com.xx.hive.HiveAdmin</value>
</property>
注意:一定要重启hive!!!

否则改再多也白费

三、权限设置:

关于权限设置,你可以在hue中进行,也可以在beeline模式下进行,方便起见还是在hue中操作;

首先你需要在hue中创建对应的管理员账户,比如说admin,因为之前的jar包中也是把admin设置为了管理员账户;

其次你也需要创建其他普通用户,比说如user1,user2等;

然后下面这些查询和赋予权限的操作都是在admin账户下进行的,你在管理员账户下赋予普通用户创建、删除、查询的权限等;

最后,关于beeline,你可以使用hue中创建的用户登录beeline,密码什么的beeline不验证(据说是因为hdfs采用了验证用户名模式,所以beeline随了hdfs);

#查看用户admin的是属于哪个角色的;
SHOW ROLE GRANT user admin;
#查看角色selectrole 有哪些权限
show grant role selectrole on database wl;
#查看用户admin 有哪些权限
show grant user admin on database wl;
#一句话搞定查看所有权限
show grant;
#查看所有角色
show roles;
#创建角色
create role testrole;
#删除juese
drop role testrole;
#给角色授权 把数据库wl的查询权限授予selectrole角色
grant select on database wl to role selectrole;
#赋予角色selectrole对数据库wlt做删除权限,包括删除表和删除库的权限
grant drop on database wlt to role selectrole;
#给用户删除具体数据库的权限
grant drop on database mm to user admin;
#赋予角色selectrole创建的权限,此创建无法创建表也无法创建数据库
grant create to role selectrole;
#(之所以无法创建数据库,是因为缺少在代码中createdatabse的权限,这里的创建表是可以创建所有数据库的表);
grant create to user admin;
#给角色添加用户 往角色selectrole中添加用户admin
grant roleselectroleto user admin;
#收回角色创建的权限
revoke create from role selectrole;
#收回角色查询的权限
revoke select on database wl from role role1;


0 0
原创粉丝点击