為 Prometheus Node Exporter 加上認證

這篇文章主要是為了慶祝 Node Exporter 終于迎來了 v1.0.0 版本。

Prometheus 是最早由 SoundCloud 開源的監(jiān)控告警解決方案。并已經(jīng)成長為繼 Kubernetes 之后,第二個從 CNCF 畢業(yè)的項目。伴隨著云原生理念的普及和 Kubernetes 等技術(shù)的發(fā)展, Prometheus 在監(jiān)控領(lǐng)域也有了長足的發(fā)展。

其主要組件包括 Prometheus,Alertmanager,Node Exporter,Blackbox Exporter 和 Pushgateway 等。

本文主要是為了慶祝 Node Exporter 終于迎來了 v1.0.0 版本, 所以重點主要放在一直被人詬病的安全性相關(guān)上,具體而言就是利用 TLS 和 Basic Auth 提升其安全性。

背景

Node Exporter 是 Prometheus 官方發(fā)布的,用于采集節(jié)點的系統(tǒng)信息,比如 CPU,內(nèi)存,磁盤和網(wǎng)絡(luò)等信息。
通常情況下,如果我們在使用 Prometheus 作為監(jiān)控方案,那 Node Exporter 基本都會用到的。

在 Promethues 的監(jiān)控體系中,社區(qū)中一直存在的一個觀點是,Metrics 不包含過于私密的信息。所以你可以看到,大多數(shù)的 /metrics 接口都是直接暴露出來的,沒什么特別的安全措施。

但隨著 Prometheus 在生產(chǎn)中的大量應(yīng)用,安全問題變得更加重要。

大家最先想到的解決辦法就是,為 Prometheus 與監(jiān)控目標之間的連接啟用 TLS。 但由于各類 exporter 并不原生支持 TLS 連接,所以通常情況下我們會選擇配合反向代理來完成。

這種方式能滿足需求,但未免復(fù)雜了些。近期 Prometheus 對其安全模型做了修改 , 從 Node Exporter 開始到后續(xù)其他的組件,都將支持 TLS 和 basic auth, 同時也列出了最新的安全基準(默認情況下都支持 TLS v1.2 及以上)

使用 TLS

這里我們直接實踐下看看如何啟用 TLS 。

準備證書

(MoeLove) ?  ~ mkdir -p prometheus-tls
(MoeLove) ?  ~ cd prometheus-tls
(MoeLove) ?  prometheus-tls openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout node_exporter.key -out node_exporter.crt -subj "/C=CN/ST=Beijing/L=Beijing/O=Moelove.info/CN=localhost"
Generating a RSA private key
...........................................+++++
..+++++
writing new private key to 'node_exporter.key'
-----
(MoeLove) ?  prometheus-tls ls
node_exporter.crt  node_exporter.key

通過上面的步驟,我們得到了 node_exporter.crtnode_exporter.key 這兩個文件。

Node Exporter 使用 TLS

下載 v1.0.0 版本的 Node Exporter, 并對其進行解壓等操作

(MoeLove) ?  /tmp tar -zxvf node_exporter-1.0.0.linux-amd64.tar.gz 
node_exporter-1.0.0.linux-amd64/
node_exporter-1.0.0.linux-amd64/node_exporter
node_exporter-1.0.0.linux-amd64/NOTICE
node_exporter-1.0.0.linux-amd64/LICENSE
(MoeLove) ?  /tmp cd node_exporter-1.0.0.linux-amd64 
(MoeLove) ?  node_exporter-1.0.0.linux-amd64 ls
LICENSE  node_exporter  NOTICE

復(fù)制前面生成的 node_exporter.crtnode_exporter.key 這兩個文件到當(dāng)前目錄下。

(MoeLove) ?  node_exporter-1.0.0.linux-amd64 cp ~/prometheus-tls/node_exporter.* .
(MoeLove) ?  node_exporter-1.0.0.linux-amd64 ls
LICENSE  node_exporter  node_exporter.crt  node_exporter.key  NOTICE

編寫配置文件,并保存為 config.yaml (命名隨意):

tls_server_config:
  cert_file: node_exporter.crt
  key_file: node_exporter.key

接下來就使用 --web.config 將配置文件傳遞給 Node Exporter

(MoeLove) ?  node_exporter-1.0.0.linux-amd64 ./node_exporter --web.config=config.yaml
level=info ts=2020-05-26T17:50:12.123Z caller=node_exporter.go:177 msg="Starting node_exporter" version="(version=1.0.0, branch=HEAD, revision=b9c96706a7425383902b6143d097cf6d7cfd1960)"
level=info ts=2020-05-26T17:50:12.124Z caller=node_exporter.go:178 msg="Build context" build_context="(go=go1.14.3, user=root@3e55cc20ccc0, date=20200526-06:01:48)" 
level=info ts=2020-05-26T17:50:12.130Z caller=node_exporter.go:105 msg="Enabled collectors"
...
level=info ts=2020-05-26T17:50:12.135Z caller=tls_config.go:200 msg="TLS is enabled and it cannot be disabled on the fly." http2=true

當(dāng)你看到最后這句日志時,就說明你的 Node Exporter 已經(jīng)啟用了 TLS 連接。

當(dāng)然,我們也可以選擇手動驗證下:

# 直接 curl 請求
(MoeLove) ?  prometheus-tls curl localhost:9100/metrics
Client sent an HTTP request to an HTTPS server.

(MoeLove) ?  prometheus-tls curl https://localhost:9100/metrics                 
curl: (60) SSL certificate problem: self signed certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.

可以看到不能直接 curl 進行請求了,我們可以將證書傳遞給 curl ,用來驗證剛才的配置是否正確。

(MoeLove) ?  prometheus-tls curl -s  --cacert node_exporter.crt https://localhost:9100/metrics  |grep node_exporter_build_info
# HELP node_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which node_exporter was built.
# TYPE node_exporter_build_info gauge
node_exporter_build_info{branch="HEAD",goversion="go1.14.3",revision="b9c96706a7425383902b6143d097cf6d7cfd1960",version="1.0.0"} 1

當(dāng)然,除了通過 --cacert 參數(shù)將證書傳遞給 curl 外,也可以通過 -k 參數(shù)來忽略證書檢查。

(MoeLove) ?  prometheus-tls curl -s  -k https://localhost:9100/metrics  |grep node_exporter_build_info        
# HELP node_exporter_build_info A metric with a constant '1' value labeled by version, revision, branch, and goversion from which node_exporter was built.
# TYPE node_exporter_build_info gauge
node_exporter_build_info{branch="HEAD",goversion="go1.14.3",revision="b9c96706a7425383902b6143d097cf6d7cfd1960",version="1.0.0"} 1

配置 Prometheus 使用 TLS

接下來,我們就要配置 Prometheus 通過 HTTPS 從 Node Exporter 獲取指標了。安裝過程很簡單,無論直接下載最新的二進制版本,或是直接使用 Docker 鏡像均可。

注意,我這里把上文中簽發(fā)的證書復(fù)制到了當(dāng)前目錄中。

(MoeLove) ?  prometheus-2.18.1.linux-amd64 cp ~/prometheus-tls/node_exporter.crt .
(MoeLove) ?  prometheus-2.18.1.linux-amd64 ls
console_libraries  consoles  LICENSE  node_exporter.crt  NOTICE  prometheus  prometheus.yml  promtool  tsdb

接下來,需要修改下配置文件,讓 Prometheus 可以抓取 Node Exporter 暴露的 metrics 。

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    scheme: https
    tls_config:
      ca_file: node_exporter.crt
    static_configs:
    - targets: ['localhost:9100']

這里額外增加了 scheme: https 表示通過 HTTPS 建立連接,tls_config 中指定了所用的證書文件。完整的配置可以參考 官方文檔中對 tls_config 的說明 。

最后,啟動 Prometheus 并在瀏覽器中訪問 /targets ,如果你在 endpoint 中看到 https://localhost:9100/metrics 了,那么恭喜你,Prometheus 和 Node Exporter 已經(jīng)通過 TLS 連接了。

prometheus-tls-target.png

添加 Basic Auth

上文中,我已經(jīng)為你介紹了如何使用讓 Prometheus 和 Node Exporter 間使用 TLS 連接了,接下來,我為你介紹如何增加 Basic Auth 。

這里需要注意的是,Basic Auth 和 TLS 并不是強依賴的。你可以在不開啟 TLS 的情況下使用 Basic Auth ,但我個人建議,要做就做徹底點,同時啟用好了。

為 Node Exporter 配置密碼

我們直接可以使用 htpasswd 來生成 bcrypt 密碼 hash (這個工具想必大家不會太陌生)。

(MoeLove) ?  prometheus-tls htpasswd -nBC 12 '' | tr -d ':\n'       
New password:
Re-type new password:                                              
$2y$12$WLw2sYa.NYZoBVoCOE84qe3xNm7kbSoKVIBXP.PvqNDna60vnZhEW

這里我只用它來生成了密碼 hash , 沒有傳遞用戶名。

接下來,修改上文中提到的 Node Exporter 所用的配置文件,如下:

tls_server_config:
  cert_file: node_exporter.crt
  key_file: node_exporter.key
basic_auth_users:
  # 當(dāng)前設(shè)置的用戶名為 prometheus , 可以設(shè)置多個
  prometheus: $2y$12$WLw2sYa.NYZoBVoCOE84qe3xNm7kbSoKVIBXP.PvqNDna60vnZhEW

再次啟動 Node Exporter,并使用 curl 請求 metrics 接口,可以看到當(dāng)前返回 401 。

(MoeLove) ?  prometheus-tls curl -Ik https://127.0.0.1:9100/metrics 
HTTP/1.1 401 Unauthorized
Content-Type: text/plain; charset=utf-8
Www-Authenticate: Basic
X-Content-Type-Options: nosniff
Date: Wed, 27 May 2020 11:45:16 GMT
Content-Length: 13

打開 Prometheus 的 Targets 頁面,也會看到當(dāng)前提示 401 ,無法抓取 metrics 。

prometheus-tls-target-401.png

配置 Prometheus 使用 Basic Auth

接下來,只要修改 Prometheus 的配置文件,為其增加 basic_auth 的配置項即可。

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    scheme: https
    tls_config:
      ca_file: node_exporter.crt
    basic_auth:
      username: prometheus
      password: moelove.info
    static_configs:
    - targets: ['localhost:9100']

修改配置文件后,只要讓 Prometheus 重新加載配置文件即可:

(MoeLove) ?  killall -HUP prometheus

現(xiàn)在刷新 Prometheus 的 Targets 頁面,就可以看到已經(jīng)正常抓取 metrics 了。

總結(jié)

本文介紹了如何開啟 Prometheus 和 Node Exporter 之間的 TLS 連接, 以及開啟 Node Exporter 的 Basic Auth 認證。 在此之前,可能有小伙伴是通過加反代來完成的,比如: 給 Node Exporter 加上 Basic 認證

在生產(chǎn)中使用時,建議更加規(guī)范化操作,比如 CA 的選擇,密碼的管理等,比如 Node Exporter 的 Basic Auth 其實支持配置多個用戶名密碼的。

接下來,在 Prometheus 官方提供的基礎(chǔ)組件中,都將逐步推進本文中所提到的這類安全特性的支持,包括 Prometheus,Alertmanager, Pushgateway 和官方 exporter ,后續(xù)社區(qū)提供的 exporter 大概率也會逐步跟進的。

最后,再次恭喜 Node Exporter 迎來了 v1.0.0 版本。


歡迎訂閱我的文章公眾號【MoeLove】

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

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