Hive内置有基于底层HDFS的权限(Storage Based Authorization)和基于标准SQL的grant等命令(SQL Standards Based Authorization)。本文为您介绍Hive的两种授权方式。
背景信息
- 如果您可以直接通过HDFS或Hive Client访问Hive的数据,需要对Hive在HDFS中的数据进行相关的权限控制,通过HDFS权限控制,进而可以控制Hive SQL相关的操作权限。您可以使用Storage Based Authorization的授权方式,详情请参见方式一:Storage Based Authorization。
- 如果您不能直接通过HDFS或Hive Client访问,只能通过
HiveServer2(Beeline或JDBC)
等来执行Hive相关的命令,可以使用SQL Standards Based Authorization的授权方式,详情请参见方式二:SQL Standards Based Authorization。
说明 Storage Based Authorization(针对HiveMetaStore)和SQL Standards Based Authorization两种授权机制可以同时配置,不冲突。
方式一:Storage Based Authorization
- 授权配置
- 进入Hive页面。
- 登录阿里云E-MapReduce控制台。
- 在顶部菜单栏处,根据实际情况选择地域和资源组。
- 单击上方的集群管理页签。
- 在集群管理页面,单击相应集群所在行的详情。
- 在左侧导航栏中,选择 。
- 在Hive页面,修改配置。
- 单击配置页签。
- 在服务配置区域,单击hive-site。
- 单击右侧的自定义配置,在新增配置项对话框中,增加配置项。
Key Value hive.metastore.pre.event.listeners org.apache.hadoop.hive.ql.security.authorization.AuthorizationPreEventListener hive.security.metastore.authorization.manager org.apache.hadoop.hive.ql.security.authorization.StorageBasedAuthorizationProvider hive.security.metastore.authenticator.manager org.apache.hadoop.hive.ql.security.HadoopDefaultMetastoreAuthenticator
- 保存配置。
- 在Hive服务页面,单击右上角的保存。
- 在确认修改对话框中,输入执行原因,单击确定。
- 重启服务。
- 在Hive页面,选择 。
- 在执行集群操作对话框中,输入执行原因,单击确定。
- 在确认对话框中,单击确定。
- 进入Hive页面。
- 权限控制
EMR的Kerberos安全集群中已经设置了Hive的warehouse的HDFS相关权限。
对于非Kerberos的安全集群,您需要执行步骤设置Hive基本的HDFS权限:- 执行以下命令,配置Hive中warehouse文件夹的权限。
hadoop fs -chmod 1771 /user/hive/warehouse
也可以通过如下命令配置,其中1表示stick bit(不能删除别人创建的文件或文件夹)。hadoop fs -chmod 1777 /user/hive/warehouse
- 执行以下命令,切换为has用户。
sudo su has
- 修改用户或用户组的权限。
- 授予test对warehouse文件夹的rwx权限。
hadoop fs -setfacl -m user:test:rwx /user/hive/warehouse
- 授予hivegrp对warehouse文件夹的rwx权限。
hadoo fs -setfacl -m group:hivegrp:rwx /user/hive/warehouse
说明 您只能访问HDFS中自己账号创建的hive表的数据。
- 授予test对warehouse文件夹的rwx权限。
- 执行以下命令,配置Hive中warehouse文件夹的权限。
- 验证
- 使用test用户建表testtbl。
create table testtbl(a string);
返回如下错误信息,提示test用户没有创建表的权限,需要给test用户添加权限。FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:Got exception: org.apache.hadoop.security.AccessControlException Permission denied: user=test, access=WRITE, inode="/user/hive/warehouse/testtbl":hadoop:hadoop:drwxrwx--t at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:320) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:292)
- 使用has用户,给test账号添加ACL,配置warehouse文件夹的rwx权限。
hadoop fs -setfacl -m user:test:rwx /user/hive/warehouse;
- 使用test账号进行以下操作。
- 执行以下命令,创建表。
create table testtbl(a string);
- 执行以下命令,查看hdfs中testtbl的目录的权限。
hadoop fs -ls /user/hive/warehouse;
返回信息如下所示。
从返回信息可以看出,test用户创建的表数据只有test和hadoop组可以读取,其他用户没有任何权限。drwxr-x--- - test hadoop 0 2020-11-25 14:51 /user/hive/warehouse/testtbl
- 执行以下命令,向表中插入数据。
insert into table testtbl select "hz";
- 执行以下命令,创建表。
- 使用foo用户访问testtbl表。
select * from testtbl;
返回如下信息。FAILED: SemanticException Unable to fetch table testtbl. java.security.AccessControlException: Permission denied: user=foo, access=READ, inode="/user/hive/warehouse/testtbl":test:hadoop:drwxr-x--- at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:320) at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:219)
返回信息提示foo用户无法对test用户进行读操作。测试修改和删除操作,也同样没有权限。需要授权给foo用户,需要通过HDFS的授权来实现。
- 授权foo用户。
- 执行以下命令,切换为has用户。
su has
- 执行以下命令,授予foo用户对testtbl表的读权限。
hadoop fs -setfacl -R -m user:foo:r-x /user/hive/warehouse/testtbl
- 使用foo用户访问testtbl表。
select * from testtbl;
返回如下信息,表示查询成功。OK
说明 通常可以根据需求新建一个hive用户的group,然后通过给group授权,将新用户添加到group中,同一个group的数据权限都可以访问。
- 执行以下命令,切换为has用户。
- 使用test用户建表testtbl。
方式二:SQL Standards Based Authorization
- 授权配置
- 进入Hive页面。
- 登录阿里云E-MapReduce控制台。
- 在顶部菜单栏处,根据实际情况选择地域和资源组。
- 单击上方的集群管理页签。
- 在集群管理页面,单击相应集群所在行的详情。
- 在左侧导航栏中,选择 。
- 在Hive页面,修改配置。
- 单击配置页签。
- 在服务配置区域,单击hive-site。
- 单击右侧的自定义配置,在新增配置项对话框中,增加配置项。
Key Value hive.security.authorization.enabled true hive.users.in.admin.role hive hive.security.authorization.createtable.owner.grants ALL
- 保存配置。
- 在Hive服务页面,单击右上角的保存。
- 在确认修改对话框中,输入执行原因,单击确定。
- 重启服务。
- 在Hive页面,选择 。
- 在执行集群操作对话框中,输入执行原因,单击确定。
- 在确认对话框中,单击确定。
- 进入Hive页面。
- 权限控制
权限操作的具体命令,请参见Apache Hive。
- 验证
- 使用foo用户通过Beeline客户端,访问test用户的testtbl表。
select * from testtbl;
返回如下错误信息。Error: Error while compiling statement: FAILED: HiveAccessControlException Permission denied: Principal [name=foo, type=USER] does not have following privileges for operation QUERY [[SELECT] on Object [type=TABLE_OR_VIEW, name=default.testtbl]] (state=42000,code=40000)
- 使用test账号执行grant命令,授权foo的select操作权限。
grant select on table testtbl to user foo;
- 再次使用foo用户访问testtbl表。
select * from testtbl;
foo用户可以正常访问testtbl表,返回信息如下所示。INFO : OK +------------+--+ | testtbl.a | +------------+--+ | hz | +------------+--+ 1 row selected (0.787 seconds)
- 使用test账号回收foo的select权限。
revoke select from user foo;
返回信息如下所示。OK Time taken: 1.094 seconds
- 使用foo用户访问testtbl表。
select * from testtbl;
foo用户已经无法正常访问testtbl表,返回信息如下所示。Error: Error while compiling statement: FAILED: HiveAccessControlException Permission denied: Principal [name=foo, type=USER] does not have following privileges for operation QUERY [[SELECT] on Object [type=TABLE_OR_VIEW, name=default.testtbl]] (state=42000,code=40000)
- 使用foo用户通过Beeline客户端,访问test用户的testtbl表。