爲 Prometheus Node Exporter 加上認證

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

Prometheus 是最早由 SoundCloud 開源的監控告警解決方案。並已經成長爲繼 Kubernetes 之後,第二個從 CNCF 畢業的項目。伴隨着雲原生理念的普及和 Kubernetes 等技術的發展, Prometheus 在監控領域也有了長足的發展。

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

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

背景

Node Exporter 是 Prometheus 官方發佈的,用於採集節點的系統信息,比如 CPU,內存,磁盤和網絡等信息。
通常情況下,如果我們在使用 Prometheus 作爲監控方案,那 Node Exporter 基本都會用到的。

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

但隨着 Prometheus 在生產中的大量應用,安全問題變得更加重要。

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

這種方式能滿足需求,但未免複雜了些。近期 Prometheus 對其安全模型做了修改 , 從 Node Exporter 開始到後續其他的組件,都將支持 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

複製前面生成的 node_exporter.crtnode_exporter.key 這兩個文件到當前目錄下。

(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

當你看到最後這句日誌時,就說明你的 Node Exporter 已經啓用了 TLS 連接。

當然,我們也可以選擇手動驗證下:

# 直接 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

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

(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 鏡像均可。

注意,我這裏把上文中籤發的證書複製到了當前目錄中。

(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 已經通過 TLS 連接了。

添加 Basic Auth

上文中,我已經爲你介紹瞭如何使用讓 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:
  # 當前設置的用戶名爲 prometheus , 可以設置多個
  prometheus: $2y$12$WLw2sYa.NYZoBVoCOE84qe3xNm7kbSoKVIBXP.PvqNDna60vnZhEW

再次啓動 Node Exporter,並使用 curl 請求 metrics 接口,可以看到當前返回 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 頁面,也會看到當前提示 401 ,無法抓取 metrics 。

配置 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

現在刷新 Prometheus 的 Targets 頁面,就可以看到已經正常抓取 metrics 了。

總結

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

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

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

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


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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章