Hive內建有兩種授權機制:

  • 基於底層HDFS的許可權(Storage Based Authorization)
  • 基於標準SQL的grant等命令( SQL Standards Based Authorization)

詳見Hive官方文檔

说明 兩種授權機制可以同時配置,不衝突。

Storage Based Authorization(針對HiveMetaStore)

情境:

如果叢集的使用者可以直接通過HDFS/Hive Client訪問Hive的資料,需要對Hive在HDFS中的資料進行相關的許可權控制,通過HDFS許可權控制,進而可以控制Hive SQL相關的操作許可權。

詳見Hive文檔

添加配置

在叢集的組態管理頁面選擇Hive > 配置 > hive-site.xml > 添加自訂配置

<property>
<name>hive.metastore.pre.event.listeners</name>
    <value>org.apache.hadoop.hive.ql.security.authorization.AuthorizationPreEventListener</value>
</property>
<property>
<name>hive.security.metastore.authorization.manager</name>
    <value>org.apache.hadoop.hive.ql.security.authorization.StorageBasedAuthorizationProvider</value>
</property>
<property>
<name>hive.security.metastore.authenticator.manager</name>
    <value>org.apache.hadoop.hive.ql.security.HadoopDefaultMetastoreAuthenticator</value>
</property>

重啟HiveMetaStore

在叢集組態管理頁面重啟HiveMetaStore

HDFS許可權控制

EMR的Kerberos安全叢集已經設定了Hive的warehouse的HDFS相關許可權;

對於非Kerberos安全叢集,使用者需要做如下步驟設定hive基本的HDFS許可權:

  • 開啟HDFS的許可權
  • 配置Hive的warehouse許可權
    hadoop fs -chmod 1771 /user/hive/warehouse
    也可以設定成,1表示stick bit(不能刪除別人建立的檔案/檔案夾)
    hadoop fs -chmod 1777 /user/hive/warehouse

有了上述設定基礎許可權後,可以通過對warehouse檔案夾授權,讓相關使用者/使用者組能夠正常建立表/讀寫表等

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表在HDFS中的資料只能自己的帳號才能訪問。

驗證

  • test使用者建表testtbl
    hive> create table testtbl(a string);
    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)

    上面顯示錯誤沒有許可權,需要給test使用者添加許可權

    #從root帳號切到has帳號
    su has
    #給test帳號添加acl,對warehouse目錄的rwx許可權
     hadoop fs -setfacl -m user:test:rwx /user/hive/warehouse

    test帳號再建立database,成功

    hive> create table testtbl(a string);
    OK
    Time taken: 1.371 seconds
    #查看hdfs中testtbl的目錄,從許可權可以看出test使用者建立的表資料只有test和hadoop組可以讀取,其他使用者沒有任何許可權
    hadoop fs -ls /user/hive/warehouse
    drwxr-x---   - test hadoop          0 2017-11-25 14:51 /user/hive/warehouse/testtbl
    #插入一條資料
    hive>insert into table testtbl select "hz"
  • foo使用者訪問testtbl
    #drop table
    hive> drop table testtbl;
    FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message: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)
        at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:190)
    #alter table
    hive> alter table testtbl add columns(b string);
    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)
        at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(FSPermissionChecker.java:190)
        at org.apache.hadoop.hdfs.server.namenode.FSDirectory.checkPermission(FSDirectory.java:1720)
    #select
    hive> 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的授權來實現

    su has
    #只授權讀的許可權,也可以根據情況授權寫入權限(比如alter)
    #備忘: -R 將testtbl檔案夾下的檔案也設定可讀
    hadoop fs -setfacl -R -m user:foo:r-x /user/hive/warehouse/testtbl
    #可以select成功
    hive> select * from testtbl;
    OK
    hz
    Time taken: 2.134 seconds, Fetched: 1 row(s)
    说明 一般可以根據需求建立一個hive使用者的group,然後通過給group授權,後續將新使用者添加到group中,同一個group的資料許可權都可以訪問。

SQL Standards Based Authorization

  • 情境

    如果叢集的使用者不能直接通過hdfs/hive client訪問,只能通過HiveServer(beeline/jdbc等)來執行hive相關的命令,可以使用 SQL Standards Based Authorization的授權方式。

    如果使用者能夠使用hive shell等方式,即使做下面的一些設定作業,只要使用者用戶端的hive-site.xml沒有相關配置,都可以正常訪問hive。

    詳見Hive文檔

  • 添加配置
    • 配置是提供給HiveServer
    • 在叢集的組態管理頁面選擇Hive > 配置 > hive-site.xml > 添加自訂配置
      <property>
      <name>hive.security.authorization.enabled</name>
       <value>true</value>
      </property>
       <property>
      <name>hive.users.in.admin.role</name>
       <value>hive</value>
      </property>
       <property>
      <name>hive.security.authorization.createtable.owner.grants</name>
       <value>ALL</value>
      </property>
  • 重啟HiveServer2

    在叢集組態管理頁面重啟HiveServer2

  • 許可權操作命令

    具體命令操作詳見

  • 驗證
    • 使用者foo通過beeline訪問test使用者的testtbl表
      2: jdbc:hive2://emr-header-1.cluster-xxx:10> 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)
    • grant許可權
      切到test帳號執行grant給foo授權select操作
      hive> grant select on table testtbl to user foo;
       OK
       Time taken: 1.205 seconds
    • foo可以正常select
      0: jdbc:hive2://emr-header-1.cluster-xxxxx:10> select * from testtbl;
      INFO  : OK
      +------------+--+
      | testtbl.a  |
      +------------+--+
      | hz         |
      +------------+--+
      1 row selected (0.787 seconds)
    • 回收許可權
      切換到test帳號,回收許可權foo的select許可權
        hive> revoke select from user foo;
          OK
          Time taken: 1.094 seconds
    • foo無法select testtbl的資料
      0: jdbc:hive2://emr-header-1.cluster-xxxxx:10> 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)