6、ElasticSearch添加密碼驗(yàn)證機(jī)制(規(guī)避了諸多小坑的操作步驟)

ElasticSearch版本7.6.2

根據(jù)官方文檔,Elasticsearch 啟用安全策略需要下面的步驟。

1、驗(yàn)證當(dāng)前版本是否支持安全功能
2、是否打開安全設(shè)置
3、基于FIPS的一些驗(yàn)證
4、配置節(jié)點(diǎn)間通訊傳輸?shù)陌踩?br> 5、配置內(nèi)置用戶的密碼/
6、選擇用戶驗(yàn)證用戶身份的領(lǐng)域類型
7、設(shè)置角色和用戶以控制對Elasticsearch的訪問
8、啟用審核以跟蹤與Elasticsearch集群的嘗試和成功的交互

上面是所有安全策略需要配置的內(nèi)容,但是對于僅僅是啟用賬號密碼這種最處理的安全策略我們只需要考慮下面幾步。

1、驗(yàn)證當(dāng)前版本是否支持安全功能
2、是否打開安全設(shè)置
3、配置節(jié)點(diǎn)間通訊傳輸?shù)陌踩?br> 4、設(shè)置ssl的keystore、truststore的密碼
5、重啟ES集群是配置生效
6、配置內(nèi)置用戶的密碼
7、修改kibana的配置

具體步驟:

1. 驗(yàn)證當(dāng)前版本是否支持安全功能

Elasticsearch的安全策略需要X-Pack插件的支持,不過對于7.X以上版本X-Pack已經(jīng)內(nèi)置,所以不需要額外的操作。

2. 是否打開安全設(shè)置

xpack.security.enabled控制安全配置的開啟,在默認(rèn)情況下此參數(shù)被設(shè)置為false。要想開啟安全策略需要在所有集群中講此參數(shù)進(jìn)行設(shè)置

xpack.security.enabled = true

3. 配置節(jié)點(diǎn)間通訊傳輸?shù)陌踩?/h2>

僅僅開啟安全設(shè)置再啟動(dòng)服務(wù)的時(shí)候會(huì)拋出錯(cuò)誤
這是因?yàn)閭鬏斁W(wǎng)絡(luò)層用于集群中節(jié)點(diǎn)之間的內(nèi)部通信。啟用安全功能后,必須使用TLS來確保節(jié)點(diǎn)之間的通信已加密。為節(jié)點(diǎn)間通訊配置安全策略需要兩個(gè)步驟:

  1. 生成節(jié)點(diǎn)間安全策略使用的證書
  2. 修改各個(gè)節(jié)點(diǎn)的安全配置

3.1 創(chuàng)建證書頒發(fā)機(jī)構(gòu)以及為節(jié)點(diǎn)生成證書

在Elasticsearch集群中驗(yàn)證證書真實(shí)性的推薦方法是信任簽署證書的證書頒發(fā)機(jī)構(gòu)(CA)。這樣,將節(jié)點(diǎn)添加到群集后,它們只需要使用由同一CA簽名的證書,即可自動(dòng)允許該節(jié)點(diǎn)加入群集。另外證書中可以包含與節(jié)點(diǎn)的IP地址和DNS名稱相對應(yīng)的主題備用名稱,以便可以執(zhí)行主機(jī)名驗(yàn)證。
*為Elasticsearch集群創(chuàng)建發(fā)證機(jī)構(gòu),使用下面的步驟為集群創(chuàng)建一個(gè)CA授權(quán)證書

bin/elasticsearch-certutil ca

整個(gè)創(chuàng)建過程是這樣的。在輸入命令后控制臺(tái)會(huì)輸出此命令的信息描述,然后你需要先執(zhí)行{①}的操作然后執(zhí)行{②}的操作

This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.
The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.
Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority
By default the 'ca' mode produces a single PKCS#12 output file which holds:
    * The CA certificate
    * The CA's private key
If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key
Please enter the desired output file [elastic-stack-ca.p12]:  {①}
Enter password for elastic-stack-ca.p12 : {②}

①:此位置設(shè)置文檔輸出地址和名稱。默認(rèn)名稱為elastic-stack-ca.p12。這個(gè)文件是PKCS#12密鑰存儲(chǔ)庫,它包含您的CA的公共證書和用于為每個(gè)節(jié)點(diǎn)簽署證書的私有密鑰。
(也可以無需輸入,直接回車,則會(huì)再當(dāng)前目錄,elasticsearch-7.6.2目錄下, 和bin同級目錄,生成文件elastic-certificates.p12,
然后在elasticsearch-7.6.2/config目錄下,新建一個(gè)文件夾certs, 把文件elastic-certificates.p12復(fù)制到certs文件夾下即可。)
②:此位置設(shè)置證書的密碼。計(jì)劃將來向集群添加更多的節(jié)點(diǎn),請記住其密碼。

為Elasticsearch集群中的節(jié)點(diǎn)生成證書
使用下面的名稱生成集群使用的生成節(jié)點(diǎn)證書。elastic-stack-ca.p12為上一步生成CA證書。

bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12

整個(gè)創(chuàng)建過程是這樣的,類似之前的內(nèi)容,在輸入命令后控制臺(tái)會(huì)輸出此命令的信息描述,
輸入elastic-stack-ca.p12的密碼后,在elasticsearch-7.6.2文件夾下執(zhí)行命令:

bin/elasticsearch-certutil cert --ca config/certs/elastic-stack-ca.p12

然后你需要先執(zhí)行{①}的操作然后執(zhí)行{②}的操作,最后執(zhí)行{③}的操作

Enter password for CA (/usr/local/es-cluster/elastic-stack-ca.p12) : ①
Please enter the desired output file [elastic-certificates.p12]: ②
Enter password for elastic-certificates.p12 : ③

① : 此位置需要輸入elastic-stack-ca.p12 CA授權(quán)證書的密碼。
② : 此位置為需要輸出證書位置。
③ : 此位置為證書的密碼。使用空密碼可以直接回車結(jié)束。
默認(rèn)情況下,elasticsearch-certutil生成的證書中沒有主機(jī)名信息。這意味著可以為集群中的任意節(jié)點(diǎn)使用此證書,但是必須關(guān)閉主機(jī)名驗(yàn)證。

es集群模式,修改每個(gè)節(jié)點(diǎn)的elasticsearch.yml配置

將elastic-certificates.p12文件(只需要此文件)復(fù)制到每個(gè)節(jié)點(diǎn)上的Elasticsearch配置目錄中的一個(gè)目錄中。比如我是放到了每個(gè)節(jié)點(diǎn)的config/certs目錄下。
然后修改每個(gè)節(jié)點(diǎn)的elasticsearch.yml配置。添加下面的參數(shù):

xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate  
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12   
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12  

xpack.security.transport.ssl.verification_mode

如果在elasticsearch-certutil cert命令中使用—dns或—ip選項(xiàng),并且希望啟用嚴(yán)格的主機(jī)名檢查,此參數(shù)需要設(shè)置為full。而之前的例子證書中并沒有輸入ip以及dns等信息,所以我們沒有使用嚴(yán)格的主機(jī)檢查。

4、設(shè)置ssl的keystore、truststore的密碼

如果你在創(chuàng)建證書時(shí)沒有輸入密碼,則這個(gè)時(shí)候重啟集群后,如果elastic用戶沒有密碼,則使用默認(rèn)的引導(dǎo)密碼。引導(dǎo)密碼是一個(gè)臨時(shí)密碼,它允許您運(yùn)行設(shè)置所有內(nèi)置用戶密碼的工具。
如果你在創(chuàng)建證書時(shí)輸入了密碼,那可以通過下面的方法設(shè)置:

bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password   輸入自定義的密碼

bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password  輸入自定義的密碼

5、重啟ES集群是配置生效

安全重啟步驟:
1、暫停數(shù)據(jù)寫入程序
2、關(guān)閉集群shard allocation,手動(dòng)執(zhí)行POST /_flush/synced
3、查看進(jìn)程和殺死進(jìn)程
4、重啟節(jié)點(diǎn)
5、.確認(rèn)進(jìn)程是否啟動(dòng),
6、并重新開啟集群shard allocation
7、等待recovery完成,集群health status變成green

1、暫停數(shù)據(jù)寫入程序

將寫入es的程序暫停

2.關(guān)閉集群的動(dòng)態(tài)分片:(動(dòng)態(tài)分片開啟狀態(tài)如果節(jié)點(diǎn)宕機(jī)了,會(huì)導(dǎo)致集群重新分配數(shù)據(jù)進(jìn)行數(shù)據(jù)轉(zhuǎn)移,會(huì)導(dǎo)致節(jié)點(diǎn)直接大量傳輸數(shù)據(jù))

curl -XPUT ‘http://10.3.14.154:9200/_cluster/settings?pretty’ -d ‘{“transient”:{“cluster.routing.allocation.disable_allocation”: true}}’

curl -XPUT 'http://10.3.14.154:9200/_cluster/settings?pretty' -d '{"transient":{"cluster.routing.allocation.disable_allocation": true}}'
{
  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
  "status" : 406
}

3. 查看進(jìn)程和殺死進(jìn)程
  • 重啟前,進(jìn)入到業(yè)務(wù)賬號很重要。見過死的誰都害怕
    ps -ef | grep elas
    kill -9 12955
4.重啟 服務(wù)

cd ~/bin
./elasticsearch -d

5.確認(rèn)進(jìn)程是否啟動(dòng)

ps -ef | grep elasticsearch

6.開啟集群的動(dòng)態(tài)分片:

curl -XPUT ‘http://10.3.14.155:9200/_cluster/settings?pretty’ -d ‘{“transient”:{“cluster.routing.allocation.disable_allocation”: false}}’
{
“error” : “Content-Type header [application/x-www-form-urlencoded] is not supported”,
“status” : 406
}

7.檢查集群的健康狀態(tài)

curl 10.3.14.154:9200/_cluster/health?pretty

6、配置內(nèi)置用戶的密碼

重啟完成后,我們需要為所有的內(nèi)置用戶設(shè)置密碼。設(shè)置密碼使用命令:

bin/elasticsearch-setup-passwords interactive

上面過程中我們需要設(shè)置多個(gè)默認(rèn)用戶的信息,每個(gè)內(nèi)置用戶負(fù)責(zé)不同的內(nèi)容。
為以下用戶分別設(shè)置密碼:
elastic
kibana
logstash_system
beats_system
apm_system
remote_monitoring_user

7、修改kibana的配置

截止到目前Elasticsearch的部分已經(jīng)修改完畢,下面修改kibana配置以便于讓其和Elasticsearch完成連接。
修改配置文件
修改kibana的配置文件config/kibana.yml在配置文件中添加下面內(nèi)容:

elasticsearch.username: "es賬號"
elasticsearch.password: "es密碼"

kibana 使用ps -ef|grep kibana是查不到進(jìn)程的,因?yàn)槠鋵?shí)運(yùn)行在node里面。但是我們也不能關(guān)閉所有node里面的軟件,所以我們需要查詢kibana監(jiān)聽端口5601的進(jìn)程。
使用下面命令關(guān)閉kibana:

netstat -tunlp|grep 5601(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:5601            0.0.0.0:*               LISTEN      16177/bin/../node/b 
 kill -9 16177

然后重啟Kibana

nohup ./kibana &

此時(shí)訪問kibana(http://localhost:5601)會(huì)提示需要輸入賬號密碼。
注意此時(shí)需要輸入的是elasticsearch的用戶密碼。
到此為止ES最基礎(chǔ)的安全策略已經(jīng)添加進(jìn)來了。

參考文章:https://blog.csdn.net/qq330983778/article/details/103537252
https://www.tianmingxing.com/2019/06/20/%E5%9C%A8ElasticSearch6.8%E5%8F%8A%E4%BB%A5%E4%B8%8A%E7%89%88%E6%9C%AC%E5%BC%80%E5%90%AF%E5%AE%89%E5%85%A8%E8%AE%A4%E8%AF%81%E5%8A%9F%E8%83%BD/

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

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

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