為 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 連接了。

prometheus-tls-target.png

添加 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-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

現在刷新 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】

TheMoeLove
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,837評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,196評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,688評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,654評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,456評論 6 406
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,955評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,044評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,195評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,725評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,608評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,802評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,318評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,048評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,422評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,673評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,424評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,762評論 2 372