Hive還是支持相當多的權限管理功能,滿足一般數據倉庫的使用。
Hive由一個默認的設置來配置新建文件的默認權限。
- <property>
- <name>hive.files.umask.value</name>
- <value>0002</value>
- <description>The dfs.umask value for the hive created folders</description>
- </property>
當hive.metastore.authorization.storage.checks屬性被設置成true時,
Hive將會阻止沒有權限的用戶進行表刪除操作。
不過這個配置的默認值是false,應該設置成true
- <property>
- <name>hive.metastore.authorization.storage.checks</name>
- <value>true</value>
- <description>Should the metastore do authorization checks against
- the underlying storage for operations like drop-partition (disallow
- the drop-partition if the user in question doesn't have permissions
- to delete the corresponding directory on the storage).</description>
- </property>
同時,Hive會儘可能地將hive.metastore.execute.setugi設置成true。
開啓Hive的身份認證功能,默認是false
- <property>
- <name>hive.security.authorization.enabled</name>
- <value>true</value>
- <description>Enable or disable the hive client authorization</description>
- </property>
另外還有一個是表格創建者用於的權限配置項:
- <property>
- <name>hive.security.authorization.createtable.owner.grants</name>
- <value>ALL</value>
- <description>The privileges automatically granted to the owner whenever
- a table gets created.An example like "select,drop" will grant select
- and drop privilege to the owner of the table</description>
- </property>
這個配置默認是NULL,我們建議將其設置成ALL,讓用戶能夠訪問自己創建的表。
試驗,在命令行環境開啓用戶認證
- hive> set hive.security.authorization.enabled=true;
- hive> CREATE TABLE authorization_test (key int, value string);
- Authorization failed:No privilege 'Create' found for outputs { database:default}.
- Use show grant to get more details.
我們可以看到,建表需要權限了。
權限可以授予給不同的主題,如用戶(USER),組(GROUP),角色(ROLES)
現在我們通過授權方式,將權限授予給當前用戶:
- hive> set system:user.name;
- system:user.name=edward
- hive> GRANT CREATE ON DATABASE default TO USER edward;
- hive> CREATE TABLE authorization_test (key INT, value STRING);
這樣就可以創建表了。
我們可以通過SHOW GRANT命令確認我們擁有的權限:
- hive> SHOW GRANT USER edward ON DATABASE default;
- database default
- principalName edward
- principalType USER
- privilege Create
- grantTime Mon Mar 19 09:18:10 EDT 2012
- grantor edward
當Hive裏面用於N多用戶和N多張表的時候,管理員給每個用戶授權每張表會讓他崩潰的。
所以,這個時候就可以進行組(GROUP)授權。
Hive裏的用戶組的定義等價於POSIX裏面的用戶組。
- hive> CREATE TABLE authorization_test_group(a int,b int);
- hive> SELECT * FROM authorization_test_group;
- Authorization failed:No privilege 'Select' found for inputs
- { database:default, table:authorization_test_group, columnName:a}.
- Use show grant to get more details.
- hive> GRANT SELECT on table authorization_test_group to group edward;
- hive> SELECT * FROM authorization_test_group;
- OK
- Time taken: 0.119 seconds
當給用戶組授權變得不夠靈活的時候,角色(ROLES)就派上用途了。
用戶可以被放在某個角色之中,然後角色可以被授權。
角色不同於用戶組,是由Hadoop控制的,它是由Hive內部進行管理的。
- hive> CREATE TABLE authentication_test_role (a int , b int);
- hive> SELECT * FROM authentication_test_role;
- Authorization failed:No privilege 'Select' found for inputs
- { database:default, table:authentication_test_role, columnName:a}.
- Use show grant to get more details.
- hive> CREATE ROLE users_who_can_select_authentication_test_role;
- hive> GRANT ROLE users_who_can_select_authentication_test_role TO USER edward;
- hive> GRANT SELECT ON TABLE authentication_test_role
- > TO ROLE users_who_can_select_authentication_test_role;
- hive> SELECT * FROM authentication_test_role;
- OK
- Time taken: 0.103 seconds
介紹一下常用的授權關鍵字:
ALTER | 更改表結構,創建分區 |
CREATE | 創建表 |
DROP | 刪除表,或分區 |
INDEX | 創建和刪除索引 |
LOCK | 鎖定表,保證併發 |
SELECT | 查詢表權限 |
SHOW_DATABASE | 查看數據庫權限 |
UPDATE |
爲表加載本地數據的權限 |
分區表級別的授權
默認情況下,分區表的授權將會跟隨表的授權
當然,也可以給每一個分區建立一個授權機制,
只需要設置表的屬性PARTITION_LEVEL_PRIVILEGE設置成TRUE:
- hive> ALTER TABLE authorization_part
- > SET TBLPROPERTIES ("PARTITION_LEVEL_PRIVILEGE"="TRUE");
- Authorization failed:No privilege 'Alter' found for inputs
- {database:default, table:authorization_part}.
- Use show grant to get more details.
自動授權
屬性hive.security.authorization.createtable.owner.grants決定了
建表者對錶擁有的權限,一版情況下,有select和drop
- <property>
- <name>hive.security.authorization.createtable.owner.grants</name>
- <value>select,drop</value>
- </property>
類似的,特定的用戶可以被在表創建的時候自動授予其權限。
- <property>
- <name>hive.security.authorization.createtable.user.grants</name>
- <value>admin1,edward:select;user1:create</value>
- </property>
當表建立的時候,管理員admin1和用戶edward授予讀所有表的權限。
而user1只能創建表。
同樣的配置也可以作用於組授權和角色授權
hive.security.authorization.createtable.group.grants
hive.security.authorization.createtable.role.grants