ElasticSearch版本7.6.2
根據(jù)官方文檔奔害,Elasticsearch 啟用安全策略需要下面的步驟。
1地熄、驗證當前版本是否支持安全功能
2舀武、是否打開安全設(shè)置
3、基于FIPS的一些驗證
4离斩、配置節(jié)點間通訊傳輸?shù)陌踩?br>
5银舱、配置內(nèi)置用戶的密碼/
6、選擇用戶驗證用戶身份的領(lǐng)域類型
7跛梗、設(shè)置角色和用戶以控制對Elasticsearch的訪問
8寻馏、啟用審核以跟蹤與Elasticsearch集群的嘗試和成功的交互
上面是所有安全策略需要配置的內(nèi)容,但是對于僅僅是啟用賬號密碼這種最處理的安全策略我們只需要考慮下面幾步核偿。
1诚欠、驗證當前版本是否支持安全功能
2、是否打開安全設(shè)置
3漾岳、配置節(jié)點間通訊傳輸?shù)陌踩?br>
4轰绵、設(shè)置ssl的keystore、truststore的密碼
5尼荆、重啟ES集群是配置生效
6晋修、配置內(nèi)置用戶的密碼
7恩沛、修改kibana的配置
具體步驟:
1. 驗證當前版本是否支持安全功能
Elasticsearch的安全策略需要X-Pack插件的支持,不過對于7.X以上版本X-Pack已經(jīng)內(nèi)置,所以不需要額外的操作呢堰。
2. 是否打開安全設(shè)置
xpack.security.enabled控制安全配置的開啟隔躲,在默認情況下此參數(shù)被設(shè)置為false穗泵。要想開啟安全策略需要在所有集群中講此參數(shù)進行設(shè)置
xpack.security.enabled = true
3. 配置節(jié)點間通訊傳輸?shù)陌踩?/h2>
僅僅開啟安全設(shè)置再啟動服務(wù)的時候會拋出錯誤
這是因為傳輸網(wǎng)絡(luò)層用于集群中節(jié)點之間的內(nèi)部通信瓮孙。啟用安全功能后,必須使用TLS來確保節(jié)點之間的通信已加密麸祷。為節(jié)點間通訊配置安全策略需要兩個步驟:
- 生成節(jié)點間安全策略使用的證書
- 修改各個節(jié)點的安全配置
3.1 創(chuàng)建證書頒發(fā)機構(gòu)以及為節(jié)點生成證書
在Elasticsearch集群中驗證證書真實性的推薦方法是信任簽署證書的證書頒發(fā)機構(gòu)(CA)澎怒。這樣,將節(jié)點添加到群集后阶牍,它們只需要使用由同一CA簽名的證書喷面,即可自動允許該節(jié)點加入群集。另外證書中可以包含與節(jié)點的IP地址和DNS名稱相對應(yīng)的主題備用名稱荸恕,以便可以執(zhí)行主機名驗證乖酬。
*為Elasticsearch集群創(chuàng)建發(fā)證機構(gòu),使用下面的步驟為集群創(chuàng)建一個CA授權(quán)證書
bin/elasticsearch-certutil ca
整個創(chuàng)建過程是這樣的死相。在輸入命令后控制臺會輸出此命令的信息描述融求,然后你需要先執(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è)置文檔輸出地址和名稱。默認名稱為elastic-stack-ca.p12算撮。這個文件是PKCS#12密鑰存儲庫生宛,它包含您的CA的公共證書和用于為每個節(jié)點簽署證書的私有密鑰县昂。
(也可以無需輸入,直接回車陷舅,則會再當前目錄倒彰,elasticsearch-7.6.2目錄下, 和bin同級目錄,生成文件elastic-certificates.p12莱睁,
然后在elasticsearch-7.6.2/config目錄下待讳,新建一個文件夾certs, 把文件elastic-certificates.p12復(fù)制到certs文件夾下即可。)
②:此位置設(shè)置證書的密碼仰剿。計劃將來向集群添加更多的節(jié)點创淡,請記住其密碼。
為Elasticsearch集群中的節(jié)點生成證書
使用下面的名稱生成集群使用的生成節(jié)點證書南吮。elastic-stack-ca.p12為上一步生成CA證書琳彩。
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
整個創(chuàng)建過程是這樣的,類似之前的內(nèi)容部凑,在輸入命令后控制臺會輸出此命令的信息描述露乏,
輸入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é)束猾骡。
默認情況下,elasticsearch-certutil生成的證書中沒有主機名信息敷搪。這意味著可以為集群中的任意節(jié)點使用此證書兴想,但是必須關(guān)閉主機名驗證。
es集群模式赡勘,修改每個節(jié)點的elasticsearch.yml配置
將elastic-certificates.p12文件(只需要此文件)復(fù)制到每個節(jié)點上的Elasticsearch配置目錄中的一個目錄中嫂便。比如我是放到了每個節(jié)點的config/certs目錄下。
然后修改每個節(jié)點的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選項毙替,并且希望啟用嚴格的主機名檢查,此參數(shù)需要設(shè)置為full践樱。而之前的例子證書中并沒有輸入ip以及dns等信息厂画,所以我們沒有使用嚴格的主機檢查。
4拷邢、設(shè)置ssl的keystore袱院、truststore的密碼
如果你在創(chuàng)建證書時沒有輸入密碼,則這個時候重啟集群后,如果elastic用戶沒有密碼忽洛,則使用默認的引導(dǎo)密碼腻惠。引導(dǎo)密碼是一個臨時密碼,它允許您運行設(shè)置所有內(nèi)置用戶密碼的工具欲虚。
如果你在創(chuàng)建證書時輸入了密碼集灌,那可以通過下面的方法設(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,手動執(zhí)行POST /_flush/synced
3梯找、查看進程和殺死進程
4续誉、重啟節(jié)點
5、.確認進程是否啟動初肉,
6酷鸦、并重新開啟集群shard allocation
7、等待recovery完成牙咏,集群health status變成green
1臼隔、暫停數(shù)據(jù)寫入程序
將寫入es的程序暫停
2.關(guān)閉集群的動態(tài)分片:(動態(tài)分片開啟狀態(tài)如果節(jié)點宕機了,會導(dǎo)致集群重新分配數(shù)據(jù)進行數(shù)據(jù)轉(zhuǎn)移妄壶,會導(dǎo)致節(jié)點直接大量傳輸數(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. 查看進程和殺死進程
- 重啟前摔握,進入到業(yè)務(wù)賬號很重要。見過死的誰都害怕
ps -ef | grep elas
kill -9 12955
4.重啟 服務(wù)
cd ~/bin
./elasticsearch -d
5.確認進程是否啟動
ps -ef | grep elasticsearch
6.開啟集群的動態(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è)置多個默認用戶的信息伊磺,每個內(nèi)置用戶負責(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是查不到進程的,因為其實運行在node里面摘能。但是我們也不能關(guān)閉所有node里面的軟件续崖,所以我們需要查詢kibana監(jiān)聽端口5601的進程。
使用下面命令關(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 &
此時訪問kibana(http://localhost:5601)會提示需要輸入賬號密碼团搞。
注意此時需要輸入的是elasticsearch的用戶密碼严望。
到此為止ES最基礎(chǔ)的安全策略已經(jīng)添加進來了。
參考文章: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/