天天看點

(三)kafka kerberos ACL權限控制寫在前面:1安裝kerberos伺服器2.zookeeper kerberos配置3.kafka kerberos配置

目錄

寫在前面:

1安裝kerberos伺服器

1.1yum安裝

1.2配置/etc/krb5.conf

1.3配置kdc.conf

1.4配置kadm5.acl

1.5建立kerberos資料庫

1.6常用指令

1.7設定開機啟動kerberos

 1.8其他注意事項

2.zookeeper kerberos配置

2.1生成票據

2.2修改zoo.cfg

2.3建立zk_server_jaas.conf檔案

2.4修改啟動檔案zkEnv.sh

2.5重新開機zookeeper

3.kafka kerberos配置

3.1生成使用者及票據

3.2配置server.properties

3.3建立配置檔案kafka_server_jaas.conf

3.4啟動檔案修改

3.5增加用戶端連接配接配置client.properties

3.6相關指令

3.7kafka ACL控制

3.8java程式改變

寫在前面:

(1)本例使用三台伺服器(VMware虛拟機搭建,centos7),已在hosts中配置主機與IP映射

主機 IP 部署内容
my108 192.168.10.108 kerberos伺服器、zookeeper、kafka
my109 192.168.10.109 zookeeper、kafka
my110 192.168.10.110 zookeeper、kafka

(2)zookeeper單獨部署,非使用kafka自帶;(zookeeper版本:zookeeper-3.4.11;kafka版本:kafka_2.11-2.1.0)

(3)zookeeper與kafka安裝部署,參考前面安裝部署

1安裝kerberos伺服器

注意:本例使用線上安裝方式,本人對kerberos不是很了解;很多内容為從網上搜集,具體配置内容含義後期補充;

1.1yum安裝

yum -y install krb5-workstation krb5-libs krb5-server
           

1.2配置/etc/krb5.conf

# Configuration snippets may be placed in this directory as well
includedir /etc/krb5.conf.d/

[logging]
 default = FILE:/var/log/krb5libs.log
 kdc = FILE:/var/log/krb5kdc.log
 admin_server = FILE:/var/log/kadmind.log

[libdefaults]
 dns_lookup_realm = false
 ticket_lifetime = 24h
 renew_lifetime = 7d
 forwardable = true
 rdns = false
 pkinit_anchors = /etc/pki/tls/certs/ca-bundle.crt
 default_realm = KAFKA.COM
 default_ccache_name = KEYRING:persistent:%{uid}

[realms]
 KAFKA.COM = {
  kdc = my108
  admin_server = my108
 }

[domain_realm]
 .kafka.com = KAFKA.COM
 kafka.com = KAFKA.COM
 my108 = KAFKA.COM
           

注意:

default_realm為預設的域名,名字随意(注意最好要大寫)

realms中的内容即為上面的域,裡面的kdc與admin_server為本機主機名

domain_realm:提供從域名或主機名到Kerberos域名稱的轉換

1.3配置kdc.conf

目錄:/var/kerberos/krb5kdc/kdc.conf

kdc_ports = 88
kdc_tcp_ports = 88

[realms]
 KAFKA.COM = {
  #master_key_type = aes256-cts
  acl_file = /var/kerberos/krb5kdc/kadm5.acl
  dict_file = /usr/share/dict/words
  admin_keytab = /var/kerberos/krb5kdc/kadm5.keytab
  supported_enctypes = aes256-cts:normal aes128-cts:normal des3-hmac-sha1:normal arcfour-hmac:normal camellia256-cts:normal camellia128-cts:normal des-hmac-sha1:normal des-cbc-md5:normal des-
cbc-crc:normal
 }
           

注意域名KAFKA.COM為前面的域名

1.4配置kadm5.acl

目錄:/var/kerberos/krb5kdc/kadm5.acl

(三)kafka kerberos ACL權限控制寫在前面:1安裝kerberos伺服器2.zookeeper kerberos配置3.kafka kerberos配置

注意前面定義的域名(本例中為KAFKA.COM)

1.5建立kerberos資料庫

建立前看到/var/kerberos/krb5kdc/ 僅有兩個檔案,建立完成後會生成4個檔案,如下

(三)kafka kerberos ACL權限控制寫在前面:1安裝kerberos伺服器2.zookeeper kerberos配置3.kafka kerberos配置

建立指令:kdb5_util create -r KAFKA.COM –s

建立過程中會需要輸入密碼

建立管理者并輸入密碼(帳号root/admin(一定要帶xxx/admin)帶/admin的為管理者,記住密碼),參考下面常用指令。

注意:如果想重新建立資料庫,把上面4個檔案删除,重新建立即可

1.6常用指令

(1)在kerberos伺服器上,可以直接使用kadmin.local來登入,之後執行建立指令

建立使用者:addprinc 使用者名/主機名@域;例:addprinc test/[email protected]
檢視所有使用者:listprincs
導出票據(目錄需要提前建立):xst -norandkey -k /opt/kafka/kafka.keytab kafka/[email protected]
           

(2)或者使用:/usr/sbin/kadmin.local -q "addprinc admin/admin" 直接執行(雙引号内容為執行的指令)

(3)後期補充kinit與klist指令

使用kinit指令用root/admin登入,使用klist檢視

(三)kafka kerberos ACL權限控制寫在前面:1安裝kerberos伺服器2.zookeeper kerberos配置3.kafka kerberos配置

1.7設定開機啟動kerberos

#啟動
service krb5kdc start
service kadmin start
或
systemctl start krb5kdc.service
systemctl start kadmin.service

#開機啟動
chkconfig krb5kdc on
chkconfig kadmin on

或
systemctl enable krb5kdc.service
systemctl enable kadmin.service
           

 1.8其他注意事項

其他非kerberos伺服器,直接将/etc/krb5.conf拷貝過去就可以(scp -r /etc/krb5.conf [email protected]:/etc/)

kerberos認證的幾台伺服器時間戳要保持同步(本例中my108/my109/my110要保持一緻非常重要)

官網提示:如果安裝的是oracle java;需要下載下傳jce政策檔案,替換掉$JAVA_HOME/jre/lib/security中的檔案,jdk8下載下傳位址:下載下傳

2.zookeeper kerberos配置

2.1生成票據

##生成使用者
kadmin.local -q "addprinc -randkey zookeeper/[email protected] "
kadmin.local -q "addprinc -randkey zookeeper/[email protected] "
kadmin.local -q "addprinc -randkey zookeeper/[email protected] "
##導出票據
kadmin.local -q "xst  -k /opt/kerberos/zookeeper.keytab  zookeeper/[email protected] "
kadmin.local -q "xst  -k /opt/kerberos/zookeeper.keytab  zookeeper/[email protected] "
kadmin.local -q "xst  -k /opt/kerberos/zookeeper.keytab  zookeeper/[email protected] "
           

需要将zookeeper.keytab拷貝到其他節點;上面導出票據會追加到zookeeper.keytab檔案中

2.2修改zoo.cfg

檔案位置:zookeeper/conf目錄下;

在檔案中添加入下内容:

authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
jaasLoginRenew=3600000
           

2.3建立zk_server_jaas.conf檔案

在zookeeper/conf目錄下,建立zk_server_jaas.conf檔案(vi zk_server_jaas.conf)

Server {
  com.sun.security.auth.module.Krb5LoginModule required
  useKeyTab=true
  storeKey=true
  useTicketCache=false
  keyTab="/opt/kerberos/zookeeper.keytab"
principal="zookeeper/[email protected]";
};
Client {
   com.sun.security.auth.module.Krb5LoginModule required
   useKeyTab=true
   keyTab="/opt/kerberos/zookeeper.keytab"
   storeKey=true
   useTicketCache=false
   principal="zookeeper/[email protected] ";
};
           

其他節點也建立此檔案,注意每個節點principal不同;

2.4修改啟動檔案zkEnv.sh

啟動檔案添加如下内容:

export JVMFLAGS=" -Djava.security.auth.login.config=$ZOOCFGDIR/zk_server_jaas.conf "
           

2.5重新開機zookeeper

zkServer.sh start
           

3.kafka kerberos配置

3.1生成使用者及票據

為broker每台伺服器在kerberos伺服器生成相應的principal和keytab,可以使用kadmin.local登入之後執行如下指令:

##建立kafka使用者
addprinc -randkey kafka/[email protected]
addprinc -randkey kafka/[email protected]
addprinc -randkey kafka/[email protected]
##導出票據,可以導到一個檔案中
xst -norandkey -k /opt/kerberos/kafka.keytab kafka/[email protected]
xst -norandkey -k /opt/kerberos/kafka.keytab kafka/[email protected]
xst -norandkey -k /opt/kerberos/kafka.keytab kafka/[email protected]
           

或者:kadmin.local -q "addprinc -randkey kafka/[email protected]"直接執行指令

3.2配置server.properties

直接修改kafka/conf下的server.properties檔案,或者複制一份修改,最後啟動的時候指向的複制的配置檔案(本例直接修改)

advertised.host.name=my108
advertised.listeners=SASL_PLAINTEXT://my108:9092
listeners=SASL_PLAINTEXT://my108:9092
#kerberos相關設定
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=GSSAPI
sasl.enabled.mechanisms=GSSAPI
#與上面為kafka建立的principal相同,kafka/[email protected]的principal服務名就是kafka
sasl.kerberos.service.name=kafka
#acl相關,broker服務采用kafka這個服務身份進行互動,隻有配置成superuser才能擷取叢集内的中繼資料資訊
super.users=User:kafka
#acl相關,配置後才能啟用acl
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer

#建立、删除topic相關(此部分可以不配置)
auto.create.topics.enable = false
delete.topic.enable=true
           

注意:不同節點對應主機名要修改

3.3建立配置檔案kafka_server_jaas.conf

在kafka/conf目錄下建立kafka_server_jaas.conf檔案(vi kafka_server_jaas.conf);用來指定票據等資訊

KafkaServer {
	com.sun.security.auth.module.Krb5LoginModule required
	useKeyTab=true
	storeKey=true
	keyTab="/opt/kerberos/kafka.keytab"
	principal="kafka/[email protected]";
};
KafkaClient {
        com.sun.security.auth.module.Krb5LoginModule required
        useKeyTab=true
        storeKey=true
        useTicketCache=true
        keyTab="/opt/kerberos/kafka.keytab"
        principal="kafka/[email protected]";
};
Client {
   com.sun.security.auth.module.Krb5LoginModule required
   useKeyTab=true
   storeKey=true
   useTicketCache=true
   keyTab="/opt/kerberos/zookeeper.keytab"
   principal="zookeeper/[email protected]";
};
           

其中:

KafkaServer是kafka的broker之間認證使用

KafkaClient是kafka用戶端連接配接kafka認證使用

Client是連接配接zookeeper認證使用

除了KafkaServer,其他兩個配置使用票據可以随意

3.4啟動檔案修改

修改kafka/bin目錄下kafka-run-class.sh

在exec $JAVA 後面增加kerberos啟動參數,然後就可以用正常的腳本啟動服務了

-Djava.security.krb5.conf=/etc/krb5.conf -Djava.security.auth.login.config=/home/kafka/config/kafka_server_jaas.conf
           
(三)kafka kerberos ACL權限控制寫在前面:1安裝kerberos伺服器2.zookeeper kerberos配置3.kafka kerberos配置

3.5增加用戶端連接配接配置client.properties

在kafka/conf目錄下增加client.properties(此檔案是用來進行生産、消費者認證的),檔案内容如下:

security.protocol=SASL_PLAINTEXT
#此處注意與前面server.properties中的保持一緻
sasl.kerberos.service.name=kafka
sasl.mechanism=GSSAPI
           

3.6相關指令

#broker啟動(背景啟動)
/home/kafka_2.11-2.1.0/bin/kafka-server-start.sh -daemon /home/kafka_2.11-2.1.0/config/server.properties
#建立topic
kafka-topics.sh --create --zookeeper my108:2181 --replication-factor 2 --partitions 3 --topic test111
#生産者啟動(在kafka/conf目錄下執行,指定client.properties)
../bin/kafka-console-producer.sh --broker-list my108:9092 --topic test111 --producer.config client.properties
#消費者啟動(在kafka/conf目錄下執行,指定client.properties)
../bin/kafka-console-consumer.sh --bootstrap-server my108:9092 --topic test111 --consumer.config client.properties
           

如果要測試不同使用者,隻需要修改kafka_server_jaas.conf檔案中的KafkaClient即可

上面的生産消費者測試,是在kafka這個使用者下執行(因為是超級使用者);其他使用者則需要在下面ACL授權後執行

3.7kafka ACL控制

#檢視通路控制清單
kafka-acls.sh --authorizer-properties zookeeper.connect=my108:2181,my109:2181,my110:2181 --list
#對使用者授權生産者權限
kafka-acls.sh --authorizer-properties zookeeper.connect=my108:2181,my109:2181,my110:2181 --add --allow-principal User:test --producer --topic test111
#對使用者授權消費者權限
kafka-acls.sh --authorizer-properties zookeeper.connect=my108:2181,my109:2181,my110:2181 --add --allow-principal User:test --consumer --group group1 --topic test111
#對使用者授權讀寫操作
kafka-acls.sh --authorizer-properties zookeeper.connect=my108:2181,my109:2181,my110:2181 --add --allow-principal User:test --operation Read --operation Write --topic test111
           

常用指令(參考官網:點這裡;或在指令行中輸入kafka-acls.sh --help即可檢視所有操作說明):

操作 描述 預設 類型
--add 增加 Action
--remove 删除 Action
--list 清單 Action
--authorizer-properties

類似于key=val格式

如:zookeeper.connect=localhost:2181

Configuration
--topic
--group 如果是消費者,必須指定group
-allow-principal

指定使用者,可以多個;

後面跟:   User:username

--deny-principal 拒絕使用者
--allow-host 允許IP,*代表所有
--deny-host 拒絕IP,*代表所有
--operation

操作,可選值:Read,Write,Create,Delete,Alter,Describe,ClusterAction,All

注意首字母大寫

All
--producer

設定為producer,即允許:

Write,Describe,Create

--consumer

設定為consumer,即允許:

Read,Describe

3.8java程式改變

生産者、消費者程式在原來生産消費(新版的生産消費)基礎上增加如下内容:

//票據相關資訊
System.setProperty("java.security.auth.login.config", "/opt/kerberos/kafka_client_jaas.conf");
System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");
//固定配置
props.put("security.protocol", "SASL_PLAINTEXT"); 
props.put("sasl.mechanism", "GSSAPI"); 
props.put("sasl.kerberos.service.name","kafka");//與server.properties中配置一緻
           

注意:

(1)krb5.conf固定配置,在前面kerberos中有;票據相關資訊的檔案位置自己決定

(2)kafka_client_jaas.conf配置類似于kafka_server_jaas.conf檔案,(用來指定kerberos生成的使用者及票據,票據需要kerberos服務端生成,參考前面3.1節)

kafka_client_jaas.conf檔案配置如下:

KafkaClient {
        com.sun.security.auth.module.Krb5LoginModule required
        useKeyTab=true
        storeKey=true
        keyTab="/opt/kerberos/kafka.keytab"
        principal="kafka/[email protected]";
};
           

其中的kafka使用者及票據可以随意替換成kerberos伺服器生成的使用者及對應票據;(非superuser使用者需要注意使用ACL進行授權)