【HIVE】Hive 通过控制角色(role)权限访问

hive有两种类型的权限控制方式:

、Storage Based Authorization in the Metastore Server

        通过Hcatcalog API访问hive数据的方式,实际是通过访问metastore元数据的形式访问hive数据,这类有MapReduce,Impala,Pig,Spark SQL,Hive Command line等方式

       其实是通过hdfs(Hadoop File System)文件系统控制-d-r-w等权限来实现,这里需要借助hdfs setfacl命令

       1)在hive-site.xml配置文件里配置如下选项

<property>
  <name>hive.security.metastore.authorization.manager</name>
  <value>org.apache.hadoop.hive.ql.security.authorization.DefaultHiveMetastoreAuthorizationProvider</value>
  <description>authorization manager class name to be used in the metastore for authorization.
  The user defined authorization class should implement interface
  org.apache.hadoop.hive.ql.security.authorization.HiveMetastoreAuthorizationProvider.
  </description>
 </property>
 
<property>
  <name>hive.security.metastore.authenticator.manager</name>
  <value>org.apache.hadoop.hive.ql.security.HadoopDefaultMetastoreAuthenticator</value>
  <description>authenticator manager class name to be used in the metastore for authentication.
  The user defined authenticator should implement interface 
  org.apache.hadoop.hive.ql.security.HiveAuthenticationProvider.
  </description>
</property>
 
<property>
  <name>hive.metastore.pre.event.listeners</name>
  <value> </value>
  <description>pre-event listener classes to be loaded on the metastore side to run code
  whenever databases, tables, and partitions are created, altered, or dropped.
  Set to org.apache.hadoop.hive.ql.security.authorization.AuthorizationPreEventListener
  if metastore-side authorization is desired.
  </description>
</property>

    2)使用setfacl为用户在hive数仓路径分配权限

    2.1) 创建一个新用户 test

hdfs dfs -mkdir /user/test

    2.2)分配权限用户组  用户组:用户  

hdfs dfs -chown test:test /user/test

   2.3)查看详情

hdfs dfs -ls /user/test


drwxr-xr-x   - test      test               0 2020-07-14 15:06 /user/test

  2.4)授予test对warehouse文件夹rwx权限

hadoop fs -setfacl -m user:test:rwx /user/hive/warehouse

 2.5)授予hivegrp对warehouse文件夹rwx权限

hadoo fs -setfacl -m group:hivegrp:rwx /user/hive/warehouse

、SQL Standards Based Authorization in HiveServer2

      通过hiveserver2的方式访问hive数据,默认提供两种角色:public和admin,所有用户默认属于角色public,而授权则必须是具有角色admin的用户才可以完成(普通用户仅可以将自己获得的权限授权给其它用户),因此我们必须添加至少一个用户拥有角色admin

  1)通过在hive-site.xml中配置获得admin权限,hive配置项

 

<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.users.in.admin.role</name>
  <value>hdfs</value>
</property>

含义分别是开启权限验证;表的创建者对表拥有所有权限

hive.security.authorization.createtable.owner.grants默认值为NULL,所以表的创建者无法访问该表,这明显是不合理的。 

Hive授权的核心就是用户、组、角色。

Hive中的角色和平常我们认知的角色是有区别的。Hive中的角色可以理解为一部分有一些相同“属性”的用户或组或角色的集合。这里有个递归的概念,就是一个角色可以是一些角色的集合。

用户角色
张三group1admin
李四group2test


2)创建/删除角色命令用户和组使用的是Linux机器上的用户和组,而角色必须自己(hive)创建。

#获取管理员权限
set role admin;

#查看当前创建的所有role
show roles;

#新建角色 tests
create role tests;

#删除角色 tests
drop role tests;

3)授权/移除权限命令

grant/revoke:

GRANT ROLE role_name [, role_name] ... TO principal_specification [, principal_specification] ... 

REVOKE ROLE role_name [, role_name] ... FROM principal_specification [, principal_specification] ... 


principal_specification   :

USER user   | GROUP group   | ROLE role 

eg:
grant role outs to user out_user; 

show role grant user yinxiu; 

result:
hive> show role grant  user out_user;
OK
admin   false   1594783028000   hadoop
outs    false   1594712987000   hadoop
public  false   0
Time taken: 0.145 seconds, Fetched: 3 row(s)

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允许用户查看可用的数据库

常用的:ALL、CREATE、SELECT(目前只使这三种)

GRANT\REVOKE:

GRANT     priv_type [(column_list)]       [, priv_type [(column_list)]] ...     [ON object_type]     TO principal_specification [, principal_specification] ...     [WITH GRANT OPTION] 

REVOKE     priv_type [(column_list)]       [, priv_type [(column_list)]] ...     [ON object_type priv_level]     FROM principal_specification [, principal_specification] ... 

REVOKE ALL PRIVILEGES, GRANT OPTION     FROM user [, user] ...  



object_type:    TABLE   | DATABASE  

priv_level:    db_name   | tbl_name 


eg:
grant select on database default to user out_user;

revoke all on database default from user out_user;

4)查看授权用户

查看权限:

SHOW GRANT principal_specification [ON object_type priv_level [(column_list)]] 

HIVE操作和权限之间的关系

As of the release of Hive 0.7, only these operations require permissions, according to org.apache.hadoop.hive.ql.plan.HiveOperation:

OperationALTERUPDATECREATEDROPINDEXLOCKSELECTSHOW_DATABASE
LOAD       
EXPORT       
IMPORT      
CREATE TABLE       
CREATE TABLE AS SELECT      
DROP TABLE       
SELECT       
ALTER TABLE ADD COLUMN       
ALTER TABLE REPLACE COLUMN       
ALTER TABLE RENAME       
ALTER TABLE ADD PARTITION       
ALTER TABLE DROP PARTITION       
ALTER TABLE ARCHIVE       
ALTER TABLE UNARCHIVE       
ALTER TABLE SET PROPERTIES       
ALTER TABLE SET SERDE       
ALTER TABLE SET SERDEPROPERTIES       
ALTER TABLE CLUSTER BY       
ALTER TABLE PROTECT MODE       
ALTER PARTITION PROTECT MODE       
ALTER TABLE SET FILEFORMAT       
ALTER TABLE SET LOCATION       
ALTER PARTITION SET LOCATION       
ALTER TABLE CONCATENATE       
ALTER PARTITION CONCATENATE       
SHOW DATABASE       
LOCK TABLE       
UNLOCK TABLE       
eg:
show grant user out_user on database default;

result:
  1594696143000   hadoop
db_apps                         out_user        USER    SELECT  false   
Time taken: 0.031 seconds, Fetched: 1 row(s)

5)主要命令如下,记住即可:

grant select on database 数据库 to 用户名;
eg: grant select on database db_source to user zhangsan;
check eg: show grant user zhangsan on database db_source;

grant select on table 表名 to 用户名;
eg: grant select on table db_source.xx_xx_log_d to  user zhangsan; //没写db_source就是default
check eg: show grant user zhangsan on table db_source.xx_xx_log_d;


grant select on database 数据库 to  role  admin;
eg: grant select on database db_source to role admin;
check eg: show grant role admin on database db_source;

grant select on table 表名 to role  admin;
eg: grant select on table db_source.xx_xx_log_d to  role admin; //没写db_source就是default
check eg: show grant role admin on table db_source.xx_xx_log_d;

拓展beeline 直接测试:

 

 beeline -u 'jdbc:hive2://192.168.10.1:2181,192.168.10.2:2181,192.168.10.3:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2' -n zhangsan --outputformat=tsv2 --showHeader=false
beeline -u 'jdbc:hive2://192.168.10.1:2181,192.168.10.2:2181,192.168.10.3:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2' -n hadoop --outputformat=tsv2 --showHeader=false

 

参考链接:

https://www.cnblogs.com/yejibigdata/p/6394719.html

https://blog.csdn.net/javajxz008/article/details/81084744
https://www.cnblogs.com/yurunmiao/p/4449439.html

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Hive可以通过元数据的权限管理和文件存储级别的权限管理来实现权限控制。在使用Hive的授权机制之前,需要在hive-site.xml中设置两个参数:hive.security.authorization.enabled和hive.security.authorization.createtable.owner.grants。hive.security.authorization.enabled用于启用或禁用Hive客户端的授权机制,而hive.security.authorization.createtable.owner.grants用于设置在创建表时自动授予表所有者的权限。 在Hive中,权限控制的核心是用户、组和角色角色可以理解为有一些相同属性的用户或组的集合。用户和组使用的是Linux机器上的用户和组,而角色需要自己创建。可以通过创建角色并赋予相应的权限,然后将角色分配给用户或组,来实现指定用户访问指定数据库的权限控制。 通过Hive权限管理,可以实现对库级别、表级别和列级别的访问权限控制。这样可以确保Hive的使用符合数据仓库的安全原则。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Hive 如何进行权限控制](https://blog.csdn.net/weixin_59295776/article/details/122372080)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Hive权限设置说明](https://download.csdn.net/download/javajxz008/10546997)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zsigner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值