测试版本: hive1.1.0/hive1.2.0 hadoop2.6.0-cdh5.4.9/hadoop2.6.0-cdh5.4.9 hbase1.3.1/hbase1.2.0
说明: 如果不用比如sentry这样的钩子,就不太好管理了,每个用户都可以通过grant语句给自己赋权,都拥有对数据库的操作权限,因此需要加一个Hook来管理,代码如下:
参考网友们的整理的知识,自己再重新整理记录
参考连接: http://blog.csdn.net/kwu_ganymede/article/details/52733021
package com.neo.hive;
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 ManagerHook extends AbstractSemanticAnalyzerHook {
private static String[] admin = {"sqoop", "hadoop", "hive"}; //配置Hive管理员
@Override
public ASTNode preAnalyze(HiveSemanticAnalyzerHookContext context,
ASTNode ast) throws SemanticException {
switch (ast.getToken().getType()) {
case HiveParser.TOK_CREATEDATABASE:
case HiveParser.TOK_DROPDATABASE:
case HiveParser.TOK_CREATEROLE:
case HiveParser.TOK_DROPROLE:
case HiveParser.TOK_GRANT:
case HiveParser.TOK_REVOKE:
case HiveParser.TOK_GRANT_ROLE:
case HiveParser.TOK_REVOKE_ROLE:
String userName = null;
if (SessionState.get() != null
&& SessionState.get().getAuthenticator() != null) {
userName = SessionState.get().getAuthenticator().getUserName();
}
if (!admin[0].equalsIgnoreCase(userName)
&& !admin[1].equalsIgnoreCase(userName)
&& !admin[2].equalsIgnoreCase(userName)) {
throw new SemanticException(userName + "can't use ADMIN options, except "
+ admin[0] + ","
+ admin[1] + ","
+ admin[2] + ".");
}
break;
default:
break;
}
return ast;
}
public static void main(String[] args) throws SemanticException {
String[] admin = {"admin", "root"};
String userName = "root";
for (String tmp : admin) {
System.out.println(tmp);
if (!admin[0].equalsIgnoreCase(userName) && !admin[1].equalsIgnoreCase(userName)) {
throw new SemanticException(userName
+ " can't use ADMIN options, except " + admin[0] + ","
+ admin[1] + ".");
}
}
}
}
maven 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hive-auth</groupId>
<artifactId>hive-auth-01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.1.0-cdh5.4.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-cdh5.4.4</version>
</dependency>
</dependencies>
</project>
打包后放到$HIVE_HOME/lib/ 目录下,相依的hive-site.xml 配置如下:
<!--权限管理 -->
<property>
<name>hive.security.authorization.enabled</name>
<value>true</value>
</property>
<property>
<name>hive.security.authorization.createtable.owner.grants</name>
<value>ALL</value>
</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.neo.hive.ManagerHook</value>
</property>
HIVE支持以下权限:
权限名称 含义
ALL 所有权限
ALTER 允许修改元数据(modify metadata data of object)---表信息数据
UPDATE 允许修改物理数据(modify physical data of object)---实际数据
CREATE 允许进行Create操作
DROP 允许进行DROP操作
INDEX 允许建索引(目前还没有实现)
LOCK 当出现并发的使用允许用户进行LOCK和UNLOCK操作
SELECT 允许用户进行SELECT操作
SHOW_DATABASE 允许用户查看可用的数据库
--权限的拥有者
1. 基于用户的管理 user
2. 基于组的权限管理 group
3. 基于角色的权限管理 role
--被管控对象
database
table
partitions
COLUMNS
我这里仅谈到了 database table 级的权限
--建立数据库的权限仅仅超级用户可以拥有,并且初始时并不具有,需要执行grant语句给自己赋权
--如下赋权后方可建立数据库
grant all to user hadoop;
revoke all from user hadoop;
基于数据库/表->角色->用户/组->用户级权限
--可以将数据库或者表的权限赋予角色,然后将角色的权限赋予用户或者组,之后属于该角色的用户或者属于该组的用户具有该角色拥有的权限
--创建和删除角色
create role admin_role;
drop role admin_role;
--查看所有角色
show roles
--将数据库的操作权限赋予角色,此时该角色具有对数据库下面的表具有相应的权限,
--用户自己可以在该库先建立相应的表,并且对自己表拥有一切权限
grant select on database xxx to role admin_role;
grant create on database xxx to role admin_role;
--查看说有权限
show grant
--权限回收
revoke select on database xxx from role admin_role;
revoke create on database xxx from role admin_role;
grant select on table ttt to role admin_role;
grant create on table ttt to role admin_role;
--查看角色权限
show grant role role_name on database db_name;
show grant role role_name on [table] t_name;
--将角色拥有的权限赋予用户(linux)
grant role admin_role to user zhaoshun;
revoke role admin_role from user zhaoshun;
--查看某个用户所有角色
show role grant user user_name;
--将角色拥有的权限赋予组(linux),属于该组的用户具有该角色的权限
grant role admin_role to group create_select_group;
revoke role admin_role from group create_select_group;
也可以跳过角色直接是: 数据库/表 ->用户/组->用户
-也可以跳过角色,直接将数据库或者表的权限直接赋值个用户或者组
grant select on database xxx to user testuser2;
revoke select on database xxx from user testuser2;
grant select on database xxx to group testuser2;
revoke select on database xxx from user testuser2;
grant select on table ttt to user testuser2;
revoke select on table ttt from user testuser2;
grant select on table ttt to group testuser2;
revoke select on table ttt from user testuser2;
其他一些可能使用到的语句或工具:
create table xxx_ttt2(
name string,
age string
) row format delimited
fields terminated by '\t';
ansible ah -m shell -a "useradd testcs_user"
ansible ah -m shell -a "echo '123456' | passwd --stdin testcs_user"