大數(shù)據(jù)平臺(tái)之權(quán)限管理組件 - Aapche Ranger

Apache Ranger簡(jiǎn)介

Apache Ranger提供一個(gè)集中式安全管理框架, 并解決授權(quán)和審計(jì)。它可以對(duì)Hadoop生態(tài)的組件如HDFS、Yarn、Hive、Hbase等進(jìn)行細(xì)粒度的數(shù)據(jù)訪(fǎng)問(wèn)控制。通過(guò)操作Ranger控制臺(tái),管理員可以輕松的通過(guò)配置策略來(lái)控制用戶(hù)訪(fǎng)問(wèn)權(quán)限。Ranger優(yōu)點(diǎn):

  • 豐富的組件支持(HDFS,HBASE,HIVE,YARN,KAFKA,STORM)
  • 提供了細(xì)粒度級(jí)權(quán)限控制(hive列級(jí)別)
  • 權(quán)限控制插件式,統(tǒng)一方便的策略管理
  • 支持審計(jì)日志,記錄各種操作的日志,提供統(tǒng)一的查詢(xún)接口和界面
  • 支持和kerberos的集成,提供了Rest接口供二次開(kāi)發(fā)

為什么選擇Ranger:

  • 多組件支持,基本覆蓋目前現(xiàn)有技術(shù)棧的組件
  • 支持審計(jì)日志,可以查找到用戶(hù)操作明細(xì),方便問(wèn)題排查反饋
  • 擁有自己的用戶(hù)體系,方便和其他系統(tǒng)集成,提供接口調(diào)用

Ranger的架構(gòu)圖:


image.png

RangerAdmin:

  • 對(duì)于各服務(wù)策略進(jìn)行規(guī)劃,分配相應(yīng)的資源給相應(yīng)的用戶(hù)或組
  • 以RESTFUL形式提供策略的增刪改查接口
  • 統(tǒng)一查詢(xún)和管理頁(yè)面

Service Plugin:

  • 嵌入到各系統(tǒng)執(zhí)行流程中,定期從RangerAdmin拉取策略
  • 根據(jù)策略執(zhí)行訪(fǎng)問(wèn)決策樹(shù)
  • 記錄訪(fǎng)問(wèn)審計(jì)

Ranger權(quán)限模型

  • 用戶(hù):由User或Group來(lái)表達(dá)
  • 資源:不同組件有不同的資源,如HDFS的Path,Hive的DB\TABLE
  • 策略:Service可以有多條Policy,組件不同,Policy授權(quán)模型不同

以HDFS為例,與Ranger集成后的訪(fǎng)問(wèn)流程:


image.png
  • HDFS啟動(dòng)時(shí)加載Ranger插件,并從Admin拉取權(quán)限策略
  • 用戶(hù)訪(fǎng)問(wèn)請(qǐng)求到達(dá)NameNode,進(jìn)行權(quán)限驗(yàn)證
  • 驗(yàn)證后處理訪(fǎng)問(wèn)請(qǐng)求,并記錄審計(jì)日志

以Hive為例,與Ranger集成后的訪(fǎng)問(wèn)流程:


image.png
  • HiveServer2啟動(dòng)時(shí)加載Ranger插件,并從Admin拉取權(quán)限策略
  • 用戶(hù)SQL查詢(xún)請(qǐng)求到達(dá)HiveServer2,在Compile階段進(jìn)行權(quán)限驗(yàn)證
  • 驗(yàn)證后處理訪(fǎng)問(wèn)請(qǐng)求,并記錄審計(jì)日志

以YARN為例,與Ranger集成后的訪(fǎng)問(wèn)流程:


image.png
  • ResourceManger啟動(dòng)時(shí)加載Ranger插件,從Admin拉取權(quán)限策略
  • 用戶(hù)提交任務(wù)到ResourceManager,在解析任務(wù)階段進(jìn)行權(quán)限驗(yàn)證
  • 驗(yàn)證后提交任務(wù),并記錄審計(jì)日志

Apache Ranger安裝

官方文檔:

前置準(zhǔn)備

首先準(zhǔn)備好Java和Maven環(huán)境:

[root@hadoop ~]# java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
[root@hadoop ~]# mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/maven
Java version: 1.8.0_261, vendor: Oracle Corporation, runtime: /usr/local/jdk/1.8/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.el7.x86_64", arch: "amd64", family: "unix"
[root@hadoop ~]# 
  • Tips:Maven需配置好國(guó)內(nèi)的鏡像源,否則依賴(lài)下一天也下載不下來(lái)

安裝一個(gè)MySQL數(shù)據(jù)庫(kù),我這里使用的是我本地的數(shù)據(jù)庫(kù):

C:\Users\Administrator>mysql --version
mysql  Ver 8.0.21 for Win64 on x86_64 (MySQL Community Server - GPL)

搭建一個(gè)Hadoop環(huán)境,注意Hadoop的版本一定要 >= 2.7.1,因?yàn)槲抑皣L試過(guò)2.6.0版本的Hadoop無(wú)法與Ranger整合成功,本文使用的是2.8.5版本:

[root@hadoop ~]# echo $HADOOP_HOME
/usr/local/hadoop-2.8.5
[root@hadoop ~]# 

Ranger依賴(lài)了MySQL作為狀態(tài)存儲(chǔ),所以需要準(zhǔn)備一個(gè)MySQL的驅(qū)動(dòng)包:

[root@hadoop ~]# ls /usr/local/src |grep mysql
mysql-connector-java-8.0.21.jar
[root@hadoop ~]# 

編譯Ranger源碼

到官網(wǎng)上下載源碼包:

需要注意Ranger與Hadoop的對(duì)應(yīng)版本,如果你安裝的Hadoop是2.x的,那么Ranger需要采用2.x以下的版本。如果你安裝的Hadoop是3.x的,那么Ranger需要采用2.x以上的版本。例如,我這里安裝的Hadoop版本是2.8.5,所以選擇1.2.0版本的Ranger:

[root@hadoop ~]# cd /usr/local/src
[root@hadoop /usr/local/src]# wget https://mirror-hk.koddos.net/apache/ranger/1.2.0/apache-ranger-1.2.0.tar.gz

解壓源碼包:

[root@hadoop /usr/local/src]# tar -zxvf apache-ranger-1.2.0.tar.gz

進(jìn)入解壓后的目錄:cd apache-ranger-1.2.0,修改該目錄下的pom文件,將倉(cāng)庫(kù)相關(guān)配置都給注釋掉:

<!--
    <repositories>
        <repository>
            <id>apache.snapshots.https</id>
            <name>Apache Development Snapshot Repository</name>
            <url>https://repository.apache.org/content/repositories/snapshots</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>apache.public.https</id>
            <name>Apache Development Snapshot Repository</name>
            <url>https://repository.apache.org/content/repositories/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    <repository>
      <id>repo</id>
      <url>file://${basedir}/local-repo</url>
      <snapshots>
         <enabled>true</enabled>
      </snapshots>
  </repository>
    </repositories>
-->

完成以上的修改后,使用maven命令進(jìn)行編譯打包:

[root@hadoop /usr/local/src/apache-ranger-1.2.0]# mvn -DskipTests=true clean package assembly:assembly

經(jīng)過(guò)一段漫長(zhǎng)的等待后,編譯打包完成將輸出如下信息:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for ranger 1.2.0:
[INFO] 
[INFO] ranger ............................................. SUCCESS [  0.838 s]
[INFO] Jdbc SQL Connector ................................. SUCCESS [  0.861 s]
[INFO] Credential Support ................................. SUCCESS [ 26.341 s]
[INFO] Audit Component .................................... SUCCESS [  1.475 s]
[INFO] Common library for Plugins ......................... SUCCESS [  3.154 s]
[INFO] Installer Support Component ........................ SUCCESS [  0.471 s]
[INFO] Credential Builder ................................. SUCCESS [  1.074 s]
[INFO] Embedded Web Server Invoker ........................ SUCCESS [  0.807 s]
[INFO] Key Management Service ............................. SUCCESS [  3.335 s]
[INFO] ranger-plugin-classloader .......................... SUCCESS [  0.797 s]
[INFO] HBase Security Plugin Shim ......................... SUCCESS [ 17.365 s]
[INFO] HBase Security Plugin .............................. SUCCESS [  6.050 s]
[INFO] Hdfs Security Plugin ............................... SUCCESS [  5.831 s]
[INFO] Hive Security Plugin ............................... SUCCESS [02:01 min]
[INFO] Knox Security Plugin Shim .......................... SUCCESS [03:47 min]
[INFO] Knox Security Plugin ............................... SUCCESS [07:05 min]
[INFO] Storm Security Plugin .............................. SUCCESS [  1.757 s]
[INFO] YARN Security Plugin ............................... SUCCESS [  0.820 s]
[INFO] Ranger Util ........................................ SUCCESS [  0.869 s]
[INFO] Unix Authentication Client ......................... SUCCESS [ 17.494 s]
[INFO] Security Admin Web Application ..................... SUCCESS [03:01 min]
[INFO] KAFKA Security Plugin .............................. SUCCESS [  6.686 s]
[INFO] SOLR Security Plugin ............................... SUCCESS [03:07 min]
[INFO] NiFi Security Plugin ............................... SUCCESS [  1.210 s]
[INFO] NiFi Registry Security Plugin ...................... SUCCESS [  1.205 s]
[INFO] Unix User Group Synchronizer ....................... SUCCESS [  2.062 s]
[INFO] Ldap Config Check Tool ............................. SUCCESS [  3.478 s]
[INFO] Unix Authentication Service ........................ SUCCESS [  0.638 s]
[INFO] KMS Security Plugin ................................ SUCCESS [  1.430 s]
[INFO] Tag Synchronizer ................................... SUCCESS [01:58 min]
[INFO] Hdfs Security Plugin Shim .......................... SUCCESS [  0.584 s]
[INFO] Hive Security Plugin Shim .......................... SUCCESS [ 24.249 s]
[INFO] YARN Security Plugin Shim .......................... SUCCESS [  0.612 s]
[INFO] Storm Security Plugin shim ......................... SUCCESS [  0.709 s]
[INFO] KAFKA Security Plugin Shim ......................... SUCCESS [  0.617 s]
[INFO] SOLR Security Plugin Shim .......................... SUCCESS [  0.716 s]
[INFO] Atlas Security Plugin Shim ......................... SUCCESS [ 31.534 s]
[INFO] KMS Security Plugin Shim ........................... SUCCESS [  0.648 s]
[INFO] ranger-examples .................................... SUCCESS [  0.015 s]
[INFO] Ranger Examples - Conditions and ContextEnrichers .. SUCCESS [  1.108 s]
[INFO] Ranger Examples - SampleApp ........................ SUCCESS [  0.386 s]
[INFO] Ranger Examples - Ranger Plugin for SampleApp ...... SUCCESS [  0.519 s]
[INFO] Ranger Tools ....................................... SUCCESS [  1.411 s]
[INFO] Atlas Security Plugin .............................. SUCCESS [  3.977 s]
[INFO] Sqoop Security Plugin .............................. SUCCESS [  3.637 s]
[INFO] Sqoop Security Plugin Shim ......................... SUCCESS [  0.558 s]
[INFO] Kylin Security Plugin .............................. SUCCESS [01:04 min]
[INFO] Kylin Security Plugin Shim ......................... SUCCESS [  0.883 s]
[INFO] Unix Native Authenticator .......................... SUCCESS [  0.452 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

此時(shí)在target目錄下可以看到打包好的插件安裝包:

[root@hadoop /usr/local/src/apache-ranger-1.2.0]# ls target/
antrun                            ranger-1.2.0-hbase-plugin.zip     ranger-1.2.0-kms.zip                ranger-1.2.0-ranger-tools.zip     ranger-1.2.0-storm-plugin.zip
archive-tmp                       ranger-1.2.0-hdfs-plugin.tar.gz   ranger-1.2.0-knox-plugin.tar.gz     ranger-1.2.0-solr-plugin.tar.gz   ranger-1.2.0-tagsync.tar.gz
maven-shared-archive-resources    ranger-1.2.0-hdfs-plugin.zip      ranger-1.2.0-knox-plugin.zip        ranger-1.2.0-solr-plugin.zip      ranger-1.2.0-tagsync.zip
ranger-1.2.0-admin.tar.gz         ranger-1.2.0-hive-plugin.tar.gz   ranger-1.2.0-kylin-plugin.tar.gz    ranger-1.2.0-sqoop-plugin.tar.gz  ranger-1.2.0-usersync.tar.gz
ranger-1.2.0-admin.zip            ranger-1.2.0-hive-plugin.zip      ranger-1.2.0-kylin-plugin.zip       ranger-1.2.0-sqoop-plugin.zip     ranger-1.2.0-usersync.zip
ranger-1.2.0-atlas-plugin.tar.gz  ranger-1.2.0-kafka-plugin.tar.gz  ranger-1.2.0-migration-util.tar.gz  ranger-1.2.0-src.tar.gz           ranger-1.2.0-yarn-plugin.tar.gz
ranger-1.2.0-atlas-plugin.zip     ranger-1.2.0-kafka-plugin.zip     ranger-1.2.0-migration-util.zip     ranger-1.2.0-src.zip              ranger-1.2.0-yarn-plugin.zip
ranger-1.2.0-hbase-plugin.tar.gz  ranger-1.2.0-kms.tar.gz           ranger-1.2.0-ranger-tools.tar.gz    ranger-1.2.0-storm-plugin.tar.gz  version
[root@hadoop /usr/local/src/apache-ranger-1.2.0]# 

安裝Ranger Admin

將ranger admin的安裝包解壓到合適的目錄下,我這里習(xí)慣放到/usr/local

[root@hadoop /usr/local/src/apache-ranger-1.2.0]# tar -zxvf target/ranger-1.2.0-admin.tar.gz -C /usr/local/

進(jìn)入解壓后的目錄,目錄結(jié)構(gòu)如下:

[root@hadoop /usr/local/src/apache-ranger-1.2.0]# cd /usr/local/ranger-1.2.0-admin/
[root@hadoop /usr/local/ranger-1.2.0-admin]# ls
bin                    contrib  dba_script.py           ews                 ranger_credential_helper.py  set_globals.sh           templates-upgrade   upgrade.sh
changepasswordutil.py  cred     db_setup.py             install.properties  restrict_permissions.py      setup_authentication.sh  update_property.py  version
changeusernameutil.py  db       deleteUserGroupUtil.py  jisql               rolebasedusersearchutil.py   setup.sh                 upgrade_admin.py
[root@hadoop /usr/local/ranger-1.2.0-admin]# 

配置安裝選項(xiàng):

[root@hadoop /usr/local/ranger-1.2.0-admin]# vim install.properties
# 指定MySQL驅(qū)動(dòng)包所在的路徑
SQL_CONNECTOR_JAR=/usr/local/src/mysql-connector-java-8.0.21.jar

# 配置root用戶(hù)名密碼以及MySQL實(shí)例的連接地址
db_root_user=root
db_root_password=123456a.
db_host=192.168.1.11

# 配置訪(fǎng)問(wèn)數(shù)據(jù)庫(kù)的用戶(hù)名密碼
db_name=ranger_test
db_user=root
db_password=123456a.

# 指定審計(jì)日志的存儲(chǔ)方式
audit_store=db
audit_db_user=root
audit_db_name=ranger_test
audit_db_password=123456a.

在MySQL中創(chuàng)建ranger數(shù)據(jù)庫(kù):

create database ranger_test;

由于我這里使用的是MySQL8.x,需要修改一下數(shù)據(jù)庫(kù)相關(guān)的腳本,不是MySQL8版本的可以跳過(guò)這一步。打開(kāi)dba_script.pydb_setup.py文件,搜索如下內(nèi)容:

-cstring jdbc:mysql://%s/%s%s

將其全部修改為如下所示,主要是添加JDBC的serverTimezone連接參數(shù):

-cstring jdbc:mysql://%s/%s%s?serverTimezone=Asia/Shanghai

然后執(zhí)行如下命令開(kāi)始安裝ranger admin:

[root@hadoop /usr/local/ranger-1.2.0-admin]# ./setup.sh

報(bào)錯(cuò)解決

安裝過(guò)程中如果報(bào)如下錯(cuò)誤:

SQLException : SQL state: HY000 java.sql.SQLException: Operation CREATE USER failed for 'root'@'localhost' ErrorCode: 1396

SQLException : SQL state: 42000 java.sql.SQLSyntaxErrorException: Access denied for user 'root'@'192.168.1.11' to database 'mysql' ErrorCode: 1044

解決方式,就是在MySQL中執(zhí)行如下語(yǔ)句:

use mysql;
flush privileges;
grant system_user on *.* to 'root';
drop user'root'@'localhost';
create user 'root'@'localhost' identified by '123456a.';
grant all privileges on *.* to 'root'@'localhost' with grant option;

drop user'root'@'192.168.1.11';
create user 'root'@'192.168.1.11' identified by '123456a.';
grant all privileges on *.* to 'root'@'192.168.1.11' with grant option;
flush privileges;

如果報(bào)如下錯(cuò)誤:

SQLException : SQL state: HY000 java.sql.SQLException: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) ErrorCode: 1418

解決方式:

set global log_bin_trust_function_creators=TRUE;
flush privileges;

如果報(bào)如下錯(cuò)誤:

SQLException : SQL state: HY000 java.sql.SQLException: Cannot drop table 'x_policy' referenced by a foreign key constraint 'x_policy_ref_role_FK_policy_id' on table 'x_policy_ref_role'. ErrorCode: 3730

解決方式:刪除ranger庫(kù)中所有的表,再重新執(zhí)行./setup.sh

安裝完成后最終會(huì)輸出:

Installation of Ranger PolicyManager Web Application is completed.

啟動(dòng)ranger admin

修改配置文件,配置數(shù)據(jù)庫(kù)連接密碼和jdbc url時(shí)區(qū)參數(shù):

[root@hadoop /usr/local/ranger-1.2.0-admin]# vim conf/ranger-admin-site.xml
...

<property>
        <name>ranger.jpa.jdbc.url</name>
        <value>jdbc:log4jdbc:mysql://192.168.1.11/ranger_test?serverTimezone=Asia/Shanghai</value>
        <description />
</property>
<property>
        <name>ranger.jpa.jdbc.user</name>
        <value>root</value>
        <description />
</property>
<property>
        <name>ranger.jpa.jdbc.password</name>
        <value>123456a.</value>
        <description />
</property>

...

修改審計(jì)存儲(chǔ)相關(guān)配置:

[root@hadoop /usr/local/ranger-1.2.0-admin]# vim conf/ranger-admin-default-site.xml
...

<property>
        <name>ranger.jpa.audit.jdbc.url</name>
        <value>jdbc:log4jdbc:mysql://192.168.1.11:3306/ranger_test?serverTimezone=Asia/Shanghai</value>
        <description />
</property>
<property>
        <name>ranger.jpa.audit.jdbc.user</name>
        <value>root</value>
        <description />
</property>
<property>
        <name>ranger.jpa.audit.jdbc.password</name>
        <value>123456a.</value>
        <description />
</property>

...

啟動(dòng)命令如下:

[root@hadoop /usr/local/ranger-1.2.0-admin]# ranger-admin start 
Starting Apache Ranger Admin Service
Apache Ranger Admin Service with pid 21102 has started.
[root@hadoop /usr/local/ranger-1.2.0-admin]# 

檢查端口和進(jìn)程是否正常:

[root@hadoop /usr/local/ranger-1.2.0-admin]# jps
21194 Jps
21102 EmbeddedServer
[root@hadoop /usr/local/ranger-1.2.0-admin]# netstat -lntp |grep 21102
tcp6       0      0 :::6080                 :::*           LISTEN      21102/java          
tcp6       0      0 127.0.0.1:6085          :::*           LISTEN      21102/java          
[root@hadoop /usr/local/ranger-1.2.0-admin]# 

使用瀏覽器訪(fǎng)問(wèn)6080端口,進(jìn)入到登錄頁(yè)面,默認(rèn)用戶(hù)名和密碼均為admin

image.png

登錄成功后,進(jìn)入到首頁(yè),如下:


image.png

Ranger HDFS Plugin安裝

解壓hdfs plugin的安裝包到合適的目錄下:

[root@hadoop ~]# mkdir /usr/local/ranger-plugin
[root@hadoop ~]# tar -zxvf /usr/local/src/apache-ranger-1.2.0/target/ranger-1.2.0-hdfs-plugin.tar.gz -C /usr/local/ranger-plugin
[root@hadoop ~]# cd /usr/local/ranger-plugin/
[root@hadoop /usr/local/ranger-plugin]# mv ranger-1.2.0-hdfs-plugin/ hdfs-plugin

進(jìn)入解壓后的目錄,目錄結(jié)構(gòu)如下:

[root@hadoop /usr/local/ranger-plugin/hdfs-plugin]# ls
disable-hdfs-plugin.sh  enable-hdfs-plugin.sh  install  install.properties  lib  ranger_credential_helper.py  upgrade-hdfs-plugin.sh  upgrade-plugin.py
[root@hadoop /usr/local/ranger-plugin/hdfs-plugin]# 

配置安裝選項(xiàng):

[root@hadoop /usr/local/ranger-plugin/hdfs-plugin]# vim install.properties
# 指定ranger admin服務(wù)的訪(fǎng)問(wèn)地址
POLICY_MGR_URL=http://192.168.243.161:6080
# 配置倉(cāng)庫(kù)名稱(chēng),可自定義
REPOSITORY_NAME=dev_hdfs
# 配置hadoop的安裝目錄
COMPONENT_INSTALL_DIR_NAME=/usr/local/hadoop-2.8.5

# 配置用戶(hù)和用戶(hù)組
CUSTOM_USER=root
CUSTOM_GROUP=root

執(zhí)行如下腳本開(kāi)啟hdfs-plugin

[root@hadoop /usr/local/ranger-plugin/hdfs-plugin]# ./enable-hdfs-plugin.sh 

腳本執(zhí)行成功后,會(huì)輸出如下內(nèi)容:

Ranger Plugin for hadoop has been enabled. Please restart hadoop to ensure that changes are effective.

重啟Hadoop:

[root@hadoop ~]# stop-all.sh 
[root@hadoop ~]# start-all.sh

驗(yàn)證權(quán)限控制

到Ranger Admin上添加hdfs service,這里的Service Name需與配置文件中的配置所對(duì)應(yīng)上:


image.png

填寫(xiě)相應(yīng)信息:


image.png

填寫(xiě)完成后,到頁(yè)面底部點(diǎn)擊“Test Connection”測(cè)試能否正常連接,確認(rèn)可以正常連接后點(diǎn)擊“Add”完成新增:


image.png

等待一會(huì)后,到 “Audit” -> “Plugins” 頁(yè)面查看有沒(méi)有發(fā)現(xiàn)hdfs插件,如果沒(méi)有的話(huà)代表插件沒(méi)有啟用成功。正常情況如下:


image.png

確認(rèn)hdfs插件整合成功后,在hdfs中創(chuàng)建一些測(cè)試目錄和文件:

[root@hadoop ~]# hdfs dfs -mkdir /rangertest1
[root@hadoop ~]# hdfs dfs -mkdir /rangertest2
[root@hadoop ~]# echo "ranger test" > testfile
[root@hadoop ~]# hdfs dfs -put testfile /rangertest1
[root@hadoop ~]# hdfs dfs -put testfile /rangertest2

然后到Ranger Admin上添加Ranger的內(nèi)部用戶(hù),“Settings” -> “Add New User”,填寫(xiě)用戶(hù)信息:


image.png

接著添加權(quán)限策略,“Access Manager” -> “dev_hdfs” -> “Add New Policy”,配置權(quán)限策略所作用的用戶(hù)、目錄等信息:


image.png

拉到底部點(diǎn)擊“Add”完成添加后,可以看到新增了一條策略配置:


image.png

回到操作系統(tǒng),添加并切換到hive用戶(hù),測(cè)試能否正常讀取目錄、文件:

[root@hadoop ~]# sudo su - hive
[hive@hadoop ~]$ hdfs dfs -ls /
Found 2 items
drwxr-xr-x   - root supergroup          0 2020-11-12 13:48 /rangertest1
drwxr-xr-x   - root supergroup          0 2020-11-12 13:48 /rangertest2
[hive@hadoop ~]$ hdfs dfs -ls /rangertest1
Found 1 items
-rw-r--r--   1 root supergroup         12 2020-11-12 13:48 /rangertest1/testfile
[hive@hadoop ~]$ hdfs dfs -cat /rangertest1/testfile
ranger test
[hive@hadoop ~]$ hdfs dfs -ls /rangertest2
Found 1 items
-rw-r--r--   1 root supergroup         12 2020-11-12 13:48 /rangertest2/testfile
[hive@hadoop ~]$ 

通過(guò)查看目錄信息可以看到rangertest1rangertest2目錄的權(quán)限位是:drwxr-xr-x,也就說(shuō)除root外的用戶(hù)是沒(méi)權(quán)限對(duì)這兩個(gè)目錄進(jìn)行寫(xiě)操作的。

但此時(shí)測(cè)試寫(xiě)操作,會(huì)發(fā)現(xiàn)hive用戶(hù)能夠正常往rangertest1目錄添加文件,但往rangertest2目錄添加文件就會(huì)報(bào)錯(cuò),因?yàn)樵赗anger中我們只賦予了hive用戶(hù)對(duì)rangertest1目錄的讀寫(xiě)權(quán)限:

[hive@hadoop ~]$ echo "this is test file 2" > testfile2
[hive@hadoop ~]$ hdfs dfs -put testfile2 /rangertest1
[hive@hadoop ~]$ hdfs dfs -put testfile2 /rangertest2
put: Permission denied: user=hive, access=WRITE, inode="/rangertest2":root:supergroup:drwxr-xr-x
[hive@hadoop ~]$ 

如果我們想禁止hive用戶(hù)對(duì)rangertest2目錄的所有操作,那么就可以新增一條拒絕策略,“Resource Path”選擇rangertest2目錄,并且在“Deny Conditions”一欄中勾選需要deny的權(quán)限即可:

image.png

策略生效后,此時(shí)hive用戶(hù)訪(fǎng)問(wèn)rangertest2目錄就會(huì)提示權(quán)限拒絕了:

[hive@hadoop ~]$ hdfs dfs -ls /rangertest2
ls: Permission denied: user=hive, access=EXECUTE, inode="/rangertest2"
[hive@hadoop ~]$ hdfs dfs -cat /rangertest2/testfile
cat: Permission denied: user=hive, access=EXECUTE, inode="/rangertest2/testfile"
[hive@hadoop ~]$ 

至此,Ranger對(duì)HDFS的權(quán)限控制也驗(yàn)證通過(guò)了。除此之外,你也可以進(jìn)行其他的測(cè)試。


Ranger Hive Plugin安裝

首先需要搭建好Hive環(huán)境,可以參考下文:

為了與Hadoop和Ranger版本保持兼容,本文使用的Hive版本是2.3.6

[root@hadoop ~]# echo $HIVE_HOME
/usr/local/apache-hive-2.3.6-bin
[root@hadoop ~]# 

解壓hive plugin的安裝包到合適的目錄下:

[root@hadoop ~]# tar -zxvf /usr/local/src/apache-ranger-1.2.0/target/ranger-1.2.0-hive-plugin.tar.gz -C /usr/local/ranger-plugin/
[root@hadoop /usr/local/ranger-plugin]# mv ranger-1.2.0-hive-plugin/ hive-plugin

進(jìn)入解壓后的目錄,目錄結(jié)構(gòu)如下:

[root@hadoop /usr/local/ranger-plugin]# cd hive-plugin/
[root@hadoop /usr/local/ranger-plugin/hive-plugin]# ls
disable-hive-plugin.sh  enable-hive-plugin.sh  install  install.properties  lib  ranger_credential_helper.py  upgrade-hive-plugin.sh  upgrade-plugin.py
[root@hadoop /usr/local/ranger-plugin/hive-plugin]# 

配置安裝選項(xiàng):

[root@hadoop /usr/local/ranger-plugin/hive-plugin]# vim install.properties
# 指定ranger admin服務(wù)的訪(fǎng)問(wèn)地址
POLICY_MGR_URL=http://192.168.243.161:6080
# 配置倉(cāng)庫(kù)名稱(chēng),可自定義
REPOSITORY_NAME=dev_hive
# 配置hive的安裝目錄
COMPONENT_INSTALL_DIR_NAME=/usr/local/apache-hive-2.3.6-bin

# 配置用戶(hù)和用戶(hù)組
CUSTOM_USER=root
CUSTOM_GROUP=root

執(zhí)行如下腳本開(kāi)啟hive-plugin

[root@hadoop /usr/local/ranger-plugin/hive-plugin]# ./enable-hive-plugin.sh 

腳本執(zhí)行成功后,會(huì)輸出如下內(nèi)容:

Ranger Plugin for hive has been enabled. Please restart hive to ensure that changes are effective.

重啟Hive:

[root@hadoop ~]# jps
8258 SecondaryNameNode
9554 EmbeddedServer
8531 NodeManager
13764 Jps
7942 NameNode
11591 RunJar
8040 DataNode
8428 ResourceManager
[root@hadoop ~]# kill -15 11591
[root@hadoop ~]# nohup hiveserver2 -hiveconf hive.execution.engine=mr &

驗(yàn)證權(quán)限控制

到Ranger Admin上添加hive service,這里的Service Name需與配置文件中的配置所對(duì)應(yīng)上:


image.png

填寫(xiě)相應(yīng)信息,并點(diǎn)擊“Add”完成新增:


image.png
  • Tips:第一次添加hive service,點(diǎn)擊“Test Connection”時(shí)可能會(huì)提示測(cè)試連接失敗,可以暫且不用管,只要“Plugins”頁(yè)面能探測(cè)到該插件即可

等待一會(huì)后,到 “Audit” -> “Plugins” 頁(yè)面查看有沒(méi)有探測(cè)到該hive插件,如果沒(méi)有的話(huà)代表插件沒(méi)有啟用成功。正常情況如下:


image.png

確認(rèn)hive插件整合成功后,添加權(quán)限策略,“Access Manager” -> “dev_hive” -> “Add New Policy”,配置權(quán)限策略所作用的用戶(hù)、庫(kù)、表、列等信息:


image.png

回到操作系統(tǒng)上,切換到hive用戶(hù),并通過(guò)beeline進(jìn)入Hive的交互終端:

[root@hadoop ~]# sudo su - hive
上一次登錄:四 11月 12 13:53:53 CST 2020pts/1 上
[hive@hadoop ~]$ beeline -u jdbc:hive2://localhost:10000 -n hive

測(cè)試權(quán)限,可以看到除了show tables外的操作都被拒絕了:

0: jdbc:hive2://localhost:10000> show tables;
+-----------------+
|    tab_name     |
+-----------------+
| hive_wordcount  |
+-----------------+
1 row selected (0.126 seconds)
0: jdbc:hive2://localhost:10000> show databases;
Error: Error while compiling statement: FAILED: HiveAccessControlException Permission denied: user [hive] does not have [USE] privilege on [*] (state=42000,code=40000)
0: jdbc:hive2://localhost:10000> select * from hive_wordcount;
Error: Error while compiling statement: FAILED: HiveAccessControlException Permission denied: user [hive] does not have [SELECT] privilege on [default/hive_wordcount/*] (state=42000,code=40000)
0: jdbc:hive2://localhost:10000> 

因?yàn)槲覀冎唤o予了hive用戶(hù)drop hive_wordcount表的權(quán)限:

0: jdbc:hive2://localhost:10000> drop table hive_wordcount;
No rows affected (0.222 seconds)
0: jdbc:hive2://localhost:10000> 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容