当配置 hive.security.authorization.manager=org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory 时,配置文档参考 SQL Standard Based Hive Authorization基于SQL标准的Hive授权。第一个角色怎么创建的,用哪个账号连接,用什么命令创建成了问题。
配置 hive.users.in.admin.role
在 hive-site.xml,通过配置 hive.users.in.admin.role
,这些用户自动有管理员权限。
<property>
<name>hive.users.in.admin.role</name>
<value>houzhizhen,admin</value>
<description>
Comma separated list of users who are in admin role for bootstrapping.
More users can be added in ADMIN role later.
</description>
</property>
重新启动启动 Metastore, HiveServer2
使用 houzhizhen 账号以 beeline 连接 HiveServer2
发现连不上,因为默认用户 houzhizhen 没有 default 数据库的 select 权限。日志如下:
22/02/17 15:59:20 [main]: WARN jdbc.HiveConnection: Failed to connect to localhost.localdomain:10000
22/02/17 15:59:20 [main]: ERROR jdbc.Utils: Unable to read HiveServer2 configs from ZooKeeper
Error: Could not open client transport for any of the Server URI's in ZooKeeper: Failed to open new session: org.apache.hadoop.hive.ql.metadata.HiveException: MetaException(message:No privilege 'Select' found for inputs { database:default}) (state=08S01,code=0)
Beeline version 3.1.2 by Apache Hive
因为用 beeline 连接 HiveServer2 时,如果没有指定数据库,则默认连接 default 数据库,默认任何用户的权限都需要用 grant
语句赋权后才能使用。
分配 houzhizhen 的 default 账号的权限
使用 houzhizhen 账号启动 hive 命令,进入 hive cli。执行以下命令。
第1个命令是设置当前角色为 admin。第 2 个命令是把 default 数据库的所有权限分配给 houzhizhen。
hive> set role admin;
OK
Time taken: 4756.067 seconds
hive> grant all on database default to user houzhizhen;
再次使用 houzhizhen 账号以 beeline 连接 HiveServer2
现在成功连接。
22/02/17 17:01:27 [main]: INFO jdbc.HiveConnection: Connected to localhost.localdomain:10000
Connected to: Apache Hive (version 3.1.2)
Driver: Hive JDBC (version 3.1.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 3.1.2 by Apache Hive
0: jdbc:hive2://localhost:2181/default>
代码分析
HiveMetaStore$HMSHandler#init
init 里有一个同步块,下面分别介绍同步块介绍的方法。
synchronized (HMSHandler.class) {
if (currentUrl == null || !currentUrl.equals(MetaStoreInit.getConnectionURL(conf))) {
createDefaultDB();
createDefaultRoles();
addAdminUsers();
currentUrl = MetaStoreInit.getConnectionURL(conf);
}
createDefaultRoles
createDefaultRoles 执行以下操作:
- 创建 role admin,owner: admin.
- 创建 role public, owner: public.
- 把GLOBAL 分配给角色 admin
addAdminUsers
从 配置项 hive.users.in.admin.role
取出用户名,分配角色 admin,这样这些用户就有 admin 权限,但是不一定有 select 权限,需要自己给自己赋权。