因为在生产环境中大量使用hive。而hive的权限又较弱,假设可以记录全部hive操作,在增强安全性的同一时候,还可以统计hive表的使用频率;同一时候假设可以记录hql的開始和结束时间,则可以找出系统中花费时间较多的job,针对性的进行优化,因此跟踪hive的使用轨迹,增强安全的同一时候也能方便问题定位。

怎样记录用户操作了?Hive Hook为我们提供的方便的开放接口。

我们对hive的使用主要有两种使用场景,一是平时直接在命令行下运行的hql操作,此时运行hql的实体就是OS的登录用户。第二种是从webapp获取的业务数据需求人员创建定时报表的hql脚本。此时运行hql的真正实体事实上是报表创建者,系统不过代理运行而已,此时记录用户的行为则须要重写hive.security.authenticator.manager。

Hive默认使用HadoopDefaultAuthenticator获取运行hql的用户,使用其返回的用户进行权限验证。

为了使hive可以以代理的模式去运行,我们须要提供自己的authenticator。返回真正的hql运行者。下面配置可设置authenticator:

<property>

<name>hive.security.authenticator.manager</name>

<value>com.pplive.bip.hive.auth.Authenticator</value>

<description>bip user authenticator</description>

</property>

仅仅有管理员能够开启代理模式。能够使用下面方式传递代理用户:

Hive -d bip.user=xxx 或 hive --define bip.user=xxx

重写authenticator代码演示样例:

public
class
Authenticator implements HiveAuthenticationProvider {

private
finalstatic
String BIP_USER="bip.user";

privateStringuserName;

privateStringbipUser;

privateList<String>groupNames;

privateConfigurationconf;

@Override

publicList<String> getGroupNames() {

returngroupNames;

}

@Override

publicStringgetUserName() {

this.bipUser = SessionState.get().getHiveVariables().get(BIP_USER);

if(this.bipUser !=null &&!this.bipUser.isEmpty())
{

if( AdminManager.isAdmin(this.userName)) {

returnthis.bipUser;

} else {

thrownewRuntimeException("bip.user is set while youare not admin");

}

} else{

returnthis.userName;

}

}

@Override

publicvoidsetConf(Configuration conf) {

this.conf = conf;

UserGroupInformation ugi = null;

try{

ugi = ShimLoader.getHadoopShims().getUGIForConf(conf);

//     UserGroupInformation.createProxyUser(user, realUser);

} catch(Exception e) {

thrownewRuntimeException(e);

}

if(ugi ==
null
){

thrownewRuntimeException(

"Can not initialize PPLive Authenticator.");

}

this.userName = ugi.getUserName();

if(ugi.getGroupNames() !=null) {

this.groupNames = Arrays.asList(ugi.getGroupNames());

}

}

publicString getProxy() {

return 
this
.userName;

}

Hive提供的SemanticHook能够方便我们记录hql语义分析前后的状态。Execute Hook能够记录hql翻译成job提交运行前后的状态。 Driver Hook能够记录包含整个编译运行过程前后的状态。

SemanticHook记录语义分析后的行为:

public
void
postAnalyze(HiveSemanticAnalyzerHookContext context,

List<Task<?

extendsSerializable>> rootTasks)

throws SemanticException {

Hivehive = null;

try {

hive= context.getHive();

}catch(HiveException e) {

e.printStackTrace();

throw
new
RuntimeException(e);

}

Set<ReadEntity>inputs = context.getInputs();

Set<WriteEntity>outputs = context.getOutputs();

Set<String>readTables = newHashSet<String>();

for(ReadEntity input :inputs) {

Table table = input.getT();

if(table!=null) {

readTables.add(table.getTableName());

}

}

Set<String>writeTables = newHashSet<String>();

for(WriteEntity output :outputs) {

Table table = output.getT();

if(table!=null) {

writeTables.add(table.getTableName());

}

}

HiveAuthenticationProviderauthenticationProvider = SessionState.get().getAuthenticator();

if(authenticationProviderinstanceof Authenticator) {

Authenticatorauthenticator = (Authenticator)authenticationProvider;       //ip

this.logger.info(String.format("phase=SA&executor=%s&proxy=%s&db=%s&cmd=%s&readTables=%s&writeTables=%s",
authenticator.getUserName(),

authenticator.getProxy(), hive.getCurrentDatabase(),context.getCommand(),readTables.toString(),writeTables.toString()));

}

StringuserName = SessionState.get().getAuthenticator().getUserName();

logger.debug(String.format("%s execute %s, read tables:%s, writetables:%s", userName, context.getCommand(),readTables, writeTables));

}

Execute Hook记录job状态:

public
class
ExecuteHook implements ExecuteWithHookContext {

Loggerlogger= Logger.getLogger(DriverRunHook.class);

privateHiveAuthenticationProviderauthenticationProvider =
null;

private
static final
String JOB_START_TIME="PRE_EXEC_HOOK";

private
static
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@Override

public
void
run(HookContexthookContext) throwsException {

QueryPlanqueryPlan = hookContext.getQueryPlan();

StringqueryId = queryPlan.getQueryId();

StringqueryStr = queryPlan.getQueryStr();

if(authenticationProvider==null){

authenticationProvider= SessionState.get().getAuthenticator();

}

Stringresult = null;

switch(hookContext.getHookType()){

//hive.exec.pre.hooks

case
PRE_EXEC_HOOK
:

hookContext.getConf().setLong(JOB_START_TIME,System.currentTimeMillis());

break;

//hive.exec.post.hooks

case
POST_EXEC_HOOK
:

result= "Success";

break;

//hive.exec.failure.hooks

case
ON_FAILURE_HOOK
:

result= "Failure";

break;

default:

break;

}

if(hookContext.getHookType()!= HookContext.HookType.PRE_EXEC_HOOK&&authenticationProvider
instanceofAuthenticator) {

long jobEndTime = System.currentTimeMillis();

HiveConfconf = hookContext.getConf();

long jobStartTime =conf.getLong(JOB_START_TIME, jobEndTime);

long timeTaken =(jobEndTime-jobStartTime)/1000;

Authenticatorauthenticator = (Authenticator)authenticationProvider;       //ip

this.logger.info(String.format("phase=EXEC&result=%s&executor=%s&proxy=%s&db=%s&queryId=%s&queryStr=%s&jobName=%s&jobStartTime=%s&jobEndTime=%s&timeTaken=%d",
result,authenticator.getUserName(),authenticator.getProxy(),

Hive.get().getCurrentDatabase(),queryId, queryStr,conf.getVar(HiveConf.ConfVars.HADOOPJOBNAME),dateFormat.format(new
Date(jobStartTime)),

dateFormat.format(newDate(jobEndTime)),timeTaken));

}

}

}

DriverHook记录整个过程运行时间:

public
class
DriverRunHook implements HiveDriverRunHook{

Loggerlogger= Logger.getLogger(DriverRunHook.class);

privateHiveAuthenticationProviderauthenticationProvider =
null;

private
static
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private
long
startTime = 0;

@Override

public
void
preDriverRun(HiveDriverRunHookContext hookContext)

throws Exception {

if(authenticationProvider==null){

authenticationProvider= SessionState.get().getAuthenticator();

}

startTime = System.currentTimeMillis();

}

@Override

public
void
postDriverRun(HiveDriverRunHookContext hookContext)

throws Exception {

if(authenticationProviderinstanceofAuthenticator) {

long endTime = System.currentTimeMillis();

long timeTaken = (endTime-startTime)/1000;

Authenticatorauthenticator = (Authenticator)authenticationProvider;       //ip

this.logger.info(String.format("phase=DriverRun&executor=%s&proxy=%s&db=%s&cmd=%s&startTime=%s&endTime=%s&timeTaken=%d",
authenticator.getUserName(),authenticator.getProxy(),

Hive.get().getCurrentDatabase(),hookContext.getCommand(),dateFormat.format(newDate(startTime)),dateFormat.format(new
Date(endTime)),timeTaken));

}

}

}

Hive权限之审计的更多相关文章

  1. Hive权限之改进

    不足 即使开启hive权限认证的情况下,不论什么用户仍然是超级用户.能够通过grant给不论什么人赋予不论什么权限,这样权限认证基本没有意义.因此必须在开启权限认证的同一时候.对运行grant/rev ...

  2. Hive权限控制和超级管理员的实现

    Hive权限控制 Hive权限机制: Hive从0.10可以通过元数据控制权限.但是Hive的权限控制并不是完全安全的.基本的授权方案的目的是防止用户不小心做了不合适的事情. 先决条件: 为了使用Hi ...

  3. hive权限管理之实践

    一.实践心得 主要参考这个连接,里面说得也挺详细的.http://www.aboutyun.com/thread-12549-1-1.html 总结如下: 1.若赋予用户某个表的权限,查用户在该表所属 ...

  4. hive权限配置

    基于CDH5.x的Hive权限配置 1.打开权限控制,默认是没有限制的 set hive.security.authorization.enabled=true; 2.配置默认权限 hive.secu ...

  5. Hive记录-hive权限控制

    在使用Hive的元数据配置权限之前必须现在hive-site.xml中配置两个参数,配置参数如下: <property> <name>hive.security.authori ...

  6. Hive权限管理

    最近遇到一个hive权限的问题,先简单记录一下,目前自己的理解不一定对,后续根据自己的理解程度更新 一.hive用户的概念 hive本身没有创建用户的命令,hive的用户就是Linux用户,若当前是用 ...

  7. HADOOP docker(七):hive权限管理

    1. hive权限简介1.1 hive中的用户与组1.2 使用场景1.3 权限模型1.3 hive的超级用户2. 授权管理2.1 开启权限管理2.2 实现超级用户2.3 实现hiveserver2用户 ...

  8. Hive权限管理(十)

    Hive权限管理 1.hive授权模型介绍 (1)Storage Based Authorization in the Metastore Server 基于存储的授权 - 可以对Metastore中 ...

  9. hive 权限知识点整理

    一,hive 权限授权模型 1.Storage Based Authorization in the Metastore Server基于存储的授权(也就是HDFS的授权模型) - 可以对Metast ...

随机推荐

  1. Centos7 时间不正确修复

    查看系统支持的时区列表 timedatectl list-timezones 使用 date -R 查看时区是否正确 date -R 修改时区 timedatectl set-timezone Asi ...

  2. vue学习记录(一)—— vue开发调试神器vue-devtools安装

    网上有些贴子少了至关重要的一步导致我一直没装上, 切记!!install后还需build,且install和build都在vue-devtools文件夹内执行 github下载地址 点击跳转 具体步骤 ...

  3. 【洛谷4770/UOJ395】[NOI2018]你的名字(后缀数组_线段树合并)

    题目: 洛谷4770 UOJ395 分析: 一个很好的SAM应用题-- 一句话题意:给定一个字符串\(S\).每次询问给定字符串\(T\)和两个整数\(l\).\(r\),求\(T\)有多少个本质不同 ...

  4. MVC系列学习(二)-初步了解ORM框架-EF

    1.新建 一个控制台项目 2.添加一个数据项 a.选择数据库 注:数据库中的表如下: b.选择EF版本 c.选择表 3.初步了解EF框架 看到了多了一个以 edmx后缀的文件 在edmx文件上,右击打 ...

  5. 开发日记(项目中SQL查询的优化)

    今天发现自己之前写的一些SQL查询在执行效率方面非常不理想,于是尝试做了些改进. 需求为查询国地税表和税源表中,国税有而税源没有的条目数,之前的查询如下: SELECT COUNT(NAME)     ...

  6. Linux 一些小知识点汇总(持续更新....)

    一.符号 1.$@:传递的参数. 2.$# :传递参数的数量. 3.$?:指上一次执行命令后的返回值.一般0表示运行成功. 补充:$?只表示上一个命令执行后的退出状态,当命令执行后,又执行了其他命令, ...

  7. Lazarus 1.6 增加了新的窗体编辑器——Sparta_DockedFormEditor.ipk

    一下是该控件官网的介绍 "Hello A package for a docked form editor can be found in : components/sparta/docke ...

  8. 微服务的一种开源实现方式——dubbo+zookeeper

    转自: http://blog.csdn.NET/zhdd99/article/details/52263609 微服务架构成了当下的技术热点,实现微服务是要付出很大成本的,但也许是因为微服务的优点太 ...

  9. ubuntu14.04禁用USB外存储设备

    ubuntu 14.04中禁用usb外存储设备: 在网上找了很多方法,大概都是下面的命令,而实际测试的时候没有什么作用. gsettings set org.gnome.desktop.media-h ...

  10. CodeCombat代码全记录(Python学习利器)--Kithgard地牢代码1

    Kithgard地牢注意:在调用函数时,要在函数的后面加上括号内容,否则在python中,将不会认为你在调用这个函数内容,而你的英雄将像木头一样站在原地不会执行上左下右的移动!!! hero.move ...