Logo
Prometheus: Authentication cho các endpoints

Prometheus: Authentication cho các endpoints

November 30, 2024
6 min read
Table of Contents
index

Prometheus là một công cụ giám sát mạnh mẽ, nhưng một trong những điểm yếu mặc định của nó là không cài đặt sẵn cơ chế xác thực (authentication). Điều này có thể dẫn đến việc dữ liệu metric bị truy cập trái phép, gây rủi ro cho hệ thống hoặc tiết lộ thông tin nhạy cảm. Trong bài viết này, chúng ta sẽ khám phá cách bảo vệ các metric endpoints của Prometheus bằng cách thêm một lớp bảo mật thông qua authentication.


Prometheus server

Prometheus hỗ trợ basic auth cho các kết nối qua http. Và tất nhiên nó bao gồm cho cả các exporter.

Cách thêm basic auth cho prometheus server là thêm user và password vào trong file web.yml và chạy prometheus với file cấu hình này. Tất nhiên chúng ta có thể thêm password dưới dạng plain text hoặc hash. Well chẳng ai muốn password của mình ở plain text cả nên bước đầu sẽ là tạo hash password bằng bcrypt.

Tạo 1 file python với nội dung như sau:

import getpass
import bcrypt
password = getpass.getpass("password: ")
hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
print(hashed_password.decode())

Chạy file và nhập password. Script sẽ print ra bcrypt hash cho password của bạn

Tiếp theo sẽ là tạo file web.yml với nội dung như sau:

basic_auth_users:
{{user}}: {{password}}

Ví dụ:

basic_auth_users:
admin: $2b$12$w1rbrIlqVxNT/QnxpYTIhuJ/lq9d4QwoIPJBNOqej9yJpgRflmGGm

Tiếp đó khởi chạy server với command sau (Nhớ stop/remove container cũ)

docker run \
-p 9090:9090 -d \
-v ./prometheus.yml:/etc/prometheus/prometheus.yml \
-v ./web.yml:/etc/prometheus/web.yml \
-v prometheus-data:/prometheus \
prom/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--web.config.file=/etc/prometheus/web.yml

Lúc này khi truy cập prometheus server sẽ yêu cầu nhập username và password

Điền username và password bạn đã tạo ở các bước trên để truy cập prometheus server nhé

Node exporter

Ở bài viết trước, ta đã biết cách chạy node exporter để lấy các metrics của 1 linux system. Tuy nhiên endpoint tại port 9100 hoàn toàn không có lớp bảo mật nào tương tự như server. Vậy chỉ cần attacker scan được port thì có thể có full metrics của bạn. Không ai muốn điều này xảy ra hết. chúng ta sẽ thêm basic auth cho các endpoint của node exporter như sau:

Trong bài này mình sẽ hướng dẫn cài node exporter kĩ hơn bài trước 1 xíu nhé XD

Bước 1: Tải và giải nén node exporter

Truy cập trang https://prometheus.io/download/ và tải về phiên bản bạn muốn

Giải nén và cài đặt

tar xvfz node_exporter-*.linux-amd64.tar.gz
cd node_exporter-*.linux-amd64/
sudo mv node_exporter /usr/local/bin/

Bước 2: Tạo node exporter user

Terminal window
sudo useradd -rs /bin/false node_exporter

Bước 3: Tạo các file cần thiết cho việc khởi chạy

Terminal window
sudo mkdir -p /etc/prometheus_node_exporter/
sudo touch /etc/prometheus_node_exporter/web.yml
sudo chmod 700 /etc/prometheus_node_exporter
sudo chmod 600 /etc/prometheus_node_exporter/*
sudo chown -R node_exporter:node_exporter /etc/prometheus_node_exporter

Bước 4: Sửa nội dung của file /etc/prometheus_node_exporter/web.yml như sau:

Lưu ý rằng password được tạo giống như bước trên

basic_auth_users:
admin: {{password}}

Bước 5: Tạo systemd server khởi động tự động sau khi reboot

Terminal window
sudo cat << 'EOF' > /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter --web.config.file=/etc/prometheus_node_exporter/web.yml
[Install]
WantedBy=multi-user.target
EOF

Bước 6: Khởi chạy

Terminal window
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter

Kiểm tra node exporter hoạt động thành công

Thử kết nối tới endpoint bằng lệnh curl thất bại

Tương tự basic auth của prometheus server, bạn cần phải nhập uesrname và password để truy cập

Và bây giờ để prometheus server có thể get metrics từ endpoint này. Chúng ta cần chỉnh sửa file prometheus.yml 1 chút vì hiện tại server sẽ không thể truy cập endpoint do chưa được setup auth

Sửa file config như sau

global:
scrape_interval: 15s
scrape_configs:
- job_name: "node-exporter"
basic_auth:
username: "{{username}}"
password: "{{password}}"
static_configs:
- targets: ["192.168.133.130:9100"]

Trong đó username và password là tuơng ứng với username, password tạo ở bước trước nhưng password phải ở dạng plain text nhé (Chỗ này quả thực mình cũng không hiểu sao nó không hỗ trợ dạng hash luôn đi TT)

Kết luận

Việc thêm Basic Authentication cho Prometheus server và Node Exporter là một bước quan trọng để bảo vệ các endpoint giám sát khỏi truy cập trái phép. Tuy nhiên, như đã đề cập, đây chỉ là một lớp bảo vệ cơ bản. Kết nối vẫn sử dụng giao thức HTTP, không được mã hóa, nên không an toàn trước các dạng tấn công như Man-in-the-Middle (MITM).

Để bảo mật hoàn toàn, chúng ta cần triển khai giao thức HTTPS, nhưng việc cấu hình HTTPS cho từng dịch vụ trong môi trường local có thể phức tạp và đôi khi không cần thiết. Vì vậy, trong các hệ thống local hoặc nội bộ, mình sẽ chọn giải pháp sử dụng một proxy phía trước. Proxy này sẽ chịu trách nhiệm xử lý tất cả các vấn đề liên quan đến SSL/TLS, giúp các dịch vụ nội bộ tập trung vào nhiệm vụ chính mà vẫn đảm bảo an toàn khi kết nối từ bên ngoài.

Trong bài viết tiếp theo, mình sẽ hướng dẫn cách thiết lập proxy để xử lý SSL/TLS cho các dịch vụ như Prometheus và Node Exporter. Hãy cùng đón chờ nhé! 🚀

Tài liệu tham khảo

  1. https://prometheus.io/docs/prometheus/latest/configuration/https/

  2. https://prometheus.io/docs/guides/basic-auth/

  3. https://www.stackhero.io/en/services/Prometheus/