Hive中没有超级管理员,如何进行权限控制

Hive中没有超级管理员,任何用户都可以进行Grant/Revoke操作

开发实现自己的权限控制类,确保某个用户为超级用户

比如任何用户都可以grant 权限给别的用户。

grant select on table test2 to user hadoop;

如何开发一个超级管理员:

创建一个项目,导入mavan jar包,然后开始编写hook类

import com.google.common.base.Joiner;
import org.apache.hadoop.hive.ql.parse.*;
import org.apache.hadoop.hive.ql.session.SessionState;

public class HiveAdmin extends AbstractSemanticAnalyzerHook {

    private static String[] admins = {"hadoop"};

    @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:
            case HiveParser.TOK_CREATETABLE:
                String userName = null;
                if (SessionState.get() != null && SessionState.get().getAuthenticator().getUserName() != null) {
                    userName = SessionState.get().getAuthenticator().getUserName();
                }
                boolean isAdmin = false;
                for (String admin : admins) {
                    if (admin.equalsIgnoreCase(userName)) {
                        isAdmin = true;
                        break;
                    }
                }
                if (!isAdmin) {
                    throw new SemanticException(userName + "is not Admin, except " + Joiner.on(",").join(admins));
                }
                break;
            default:
                break;
        }
        return ast;
    }

}

接着,将其打包,放入hive 的lib 文件夹下。

chown hadoop. /soft/home/apache-hive-2.3.6-bin/lib/udf-test-1.0-SNAPSHOT.jar

修改,hive-site.xml,将编写好的类路径配置到xml中,并且指定超级用户为hadoop


hive.users.in.admin.role
hadoop


hive.metastore.execute.setugi
false


hive.security.authorization.enabled
true
开启权限 enable or disable thehive client authorization


hive.security.authorization.createtable.owner.grants
ALL
表的创建者对表拥有所有权限the privileges automaticallygranted t
o the owner whenever a table gets created. An example like"select,drop" will
grant select and drop privilege to the owner ofthe table


hive.security.authorization.task.factory
org.apache.hadoop.hive.gl.parse.authorization.HiveAuthorizationTaskFactoryImpl
进行权限控制的配置。


hive.semantic.analyzer.hook
com.bigdata.hive.security.HiveAdmin
使用钩子程序,识别超级管理员,进行授权控制。

重启metastore,然后重新尝试,看普通用户是否可以创建一个表。

测试发现,hadoop用户可以进行授权操作

但是hive用户无法进行授权操作:

Hive中没有超级管理员,如何进行权限控制_第1张图片

你可能感兴趣的:(大数据,hive,hadoop,数据仓库)