搜索衛士保駕護航,開源輪子不裸奔

  • 2019 年 12 月 18 日
  • 筆記

近日一則 ElasticSearch 數據泄露的訊息映入眼帘,搞的心發毛。

之前咱們在《傻瓜也能玩轉日誌歸集》中一起認識、體驗過 ELK 輪子,在微服務盛行的當下,做日誌歸集分析效果確實杠杠滴,但是有一點不得不提,那就是它們都在瘋狂的裸奔。

01. 那些年做過的事兒


場景一:訪問 ElasticSearch 暢通無阻。

試驗效果,控制台輸入 curl http://localhost:9200/ 或者瀏覽器直接訪問 http://localhost:9200/,效果如下。

只要拿到訪問鏈接,就相當於一庫在手,應有盡有。

場景二:訪問 Kibana 暢通無阻。

試驗效果,在瀏覽器中訪問 http://localhost:5601,效果圖如下。

Kibana 訪問居然也是很順暢,毫無遮攔,那豈不是擁有這個鏈接,也可以為所欲為,就可以把 ElasticSearch 搞個底朝天!

面對上面兩個場景,那麼就不得不尋覓一款能對 ElasticSearch 加入一道防線的開源輪子。

02. 邂逅搜索衛士


為了讓大家初步認識一下 Search Guard 是個啥?肆意從 Search Guard 官網截一圖,大概就是說 Search Guard 是適用於 Elasticsearch 和整個 ELK 技術棧的開源安全插件,提供加密,身份驗證,授權,審核日誌記錄等合規性功能

Search Guard 針對 Elasticsearch 提供不同版本的插件支援,可謂是好馬配好鞍。

03. 香不香,嘗後才知道


上次我們演示日誌歸集的版本是 7.1.1,那本次不妨繼續以此版本為基礎進行演示。

一、讓ElasticSearch擁抱Search Guard。

第一步:下載安裝 Search Guard。

Search Guard 提供兩種安裝方式,進入 Elasticsearch 安裝目錄。

方式一:在線安裝

bin/elasticsearch-plugin install -b com.floragunn:search-guard-7:7.1.1-37.0.0

方式二:離線安裝(本次使用方式)

下載 https://releases.floragunn.com/search-guard-7/7.1.1-37.0.0/search-guard-7-7.1.1-37.0.0.zip 執行 bin/elasticsearch-plugin install -b file:///你的/目錄/search-guard-7-7.1.1-37.0.0.zip

本次採取離線安裝方式,效果如下。

第二步:快速安裝(忽略細節)

1. 進入 Elasticsearch 下的 search-guard 下的 tools目錄 cd <Elasticsearch directory>/plugins/search-guard-7/tools 2. 執行./install_demo_configuration.sh 如果提示沒有許可權,請關注文末的「問題匯總」,連續輸入3個 Y

  • 是否安裝用於演示證書;【輸入 Y】
  • 是否自動初始化 Search Guard;【輸入 Y】
  • 是否啟用集群模式;【輸入 Y】

第三步:到底香不香?

瀏覽器訪問 https://localhost:9200/_searchguard/authinfo,果不其然有了一道攔截,讓輸入用戶名密碼才能訪問。

用戶名輸入 admin,密碼也輸入 admin,資訊就出來了(不用糾結是啥)。

至此,再訪問 http://localhost:9200/ 就不會那麼暢通無阻了,開篇提到場景一的問題也就解決了,因為 Search Guard 在訪問 ElasticSearch 的路上,加了一道華麗麗的登錄驗證的屏障。

二、讓 Search Guard 進入 Kibana 的懷抱。

面對開篇提到場景二 Kibana 訪問暢通無阻的問題,Search Guard 有沒有解呢?接下來讓我們拭目以待。

第一步:下載安裝與 Kibana 版本匹配的 Search Guard Kibana插件包。同樣是兩種安裝方式,我們先進入 Kibana 安裝目錄。

方式一:在線安裝

bin/kibana-plugin install https://releases.floragunn.com/search-guard-kibana-plugin/7.1.1-36.1.0/search-guard-kibana-plugin-7.1.1-36.1.0.zip

方式二:離線安裝(本次使用方式)

下載: https://releases.floragunn.com/search-guard-kibana-plugin/7.1.1-36.1.0/search-guard-kibana-plugin-7.1.1-36.1.0.zip 執行: bin/kibana-plugin install file:///你的/目錄/search-guard-kibana-plugin-7.1.1-36.1.0.zip

本次使用離線方式安裝,安裝效果如下。

第二步:停止 Kibana,如果 Kibana 已經啟動,不妨先停一會。

第三步:修改 kibana.yml 配置文件。

在 kibana.yml 上面修改是最合適的,但是鑒於上次咱們體驗時未做調整,考慮到你 copy 的方便,本次就直接在 kibana.yml 文件後面追加如下內容。

# Use HTTPS instead of HTTP  elasticsearch.hosts: "https://localhost:9200"    # Configure the Kibana internal server user  elasticsearch.username: "kibanaserver"  elasticsearch.password: "kibanaserver"    # Disable SSL verification because we use self-signed demo certificates  elasticsearch.ssl.verificationMode: none    # Whitelist the Search Guard Multi Tenancy Header  elasticsearch.requestHeadersWhitelist: ["Authorization","sgtenant"]    # xpack config  xpack.security.enabled: false

第四步:啟動 Kibana,觀察控制台日誌輸出。輸入 bin/kibana,日誌輸出效果如下。

第五步:到底香不香?

輸入http://localhost:5601 一探究竟,不負眾望,提示輸入用戶名、密碼,效果如下。

此時用戶名我們輸入 admin,密碼輸入 admin,點擊登錄,效果如下。

Kibana 確實多了一些東東,貌似有了許可權相關的內容出來了(本次不細究)。接下來點擊右上角的退出圖標,果真跳轉到登錄頁面,確實好使!

至此,我們又知道了 Search Guard Kibana 插件向 Kibana 添加了身份驗證以及配置的介面,讓許可權配置成為可能,並且讓我們開篇場景二的問題也得以解決。

04. 一些疑問?


由於 Elasticsearch 加入了身份驗證,面對輕量級採集(Beats + Elasticsearch + Kibana)方案下,Beats 訪問 ElasticSearch 的方式要怎麼調整?

以 filebeat 為例,不妨先看看 filebeat.yml 默認配置內容。

此時就需要開啟 https 協議傳入,配置用戶名、密碼。

# Optional protocol and basic auth credentials.    protocol: "https"    username: "admin"    password: "admin"    ssl.verification_mode: "none"

啟動 filebeat,輸入命令 ./filebeat -e -c filebeat.yml 先觀察控制台日誌輸出,然後再去 Kibana 確認一下對應的索引是否創建了。

至此,輕量級的採集方案中的採集 Beats 調整應該清晰了。

面對重量級採集方案(Beats + Kafka + Logstash + Elasticsearch + Kibana)的模式下該如何修改呢?由於 Elasticsearch 加入了身份驗證,那麼 Beats 無需做任何調整,只需把 Logstash 訪問 Elasticsearch 的方式就要進行調整即可,本次不深究啦,感興趣的參考如下鏈接去體驗。

https://docs.search-guard.com/latest/elasticsearch-logstash-search-guard#using-logstash-with-search-guard

05. 問題匯總


問題一:

執行 ./install_demo_configuration.sh 提示 -bash: ./install_demo_configuration.sh: Permission denied 解決: chmod u+x install_demo_configuration.sh

問題二:

啟動 kibana 時提示如下資訊: [error][status][plugin:[email protected]] Status changed from uninitialized to red – X-Pack Security needs to be disabled for Search Guard to work properly. Please set 'xpack.security.enabled' to false in your kibana.yml 解決: kibana.yml 加入配置 xpack.security.enabled: false

06. 寫在最後


隨著一條條配置、一步步體驗,Search Guard 讓 ELK 技術棧的輪子不再瘋狂的裸奔,品嘗之後也確實香。

本次只是帶著大家進行一次快速的體驗,很多細節都沒有深究,例如如何修改 Kibana 中 Search Guard 默認密碼?如何使用自己生成的證書;許可權該如何配置等等一系列的問題,有機會再續吧。