Prometheus配置basic_auth

发布时间 2023-11-06 10:28:09作者: hmiking

一、什么是Prometheus?

  1. Prometheus是一个开源的系统监控和报警框架,其本身也是一个时序列数据库(TSDB),它的设计灵感来源于Google的Borgmon,就像Kubernetes是基于Borg系统开源的。
  2. Prometheus是由SoundCloud的Google前员工设计并开源的,官方网站:Prometheus - Monitoring system & time series database 。Prometheus于2016年加入云原生计算机基金会(Cloud Native Computing Foundation,简称CNCF),成为了受欢迎程度仅次于Kubernetes的开源项目。

问题

prometheus部署完成后,是通过web进行访问的,但是大部分人部署完成后是没有进行加密的安全措施,导致在公网上可以给任何人都能访问,从而暴露出公司内部监控的所有Targets。

在FOFA平台通过protocol="prometheus(http)"的语法进行搜索

可以发现很多都是没有进行加密的,导致Targets以及配置的泄露。

解决问题

使用basic_auth加密

二、配置

1、安装所需的工具包,并生成basic_auth密钥
# yum install httpd-tools -y

# htpasswd -nBC 12 '' | tr -d ':\n'
# 密码设置的是123
New password: 			
Re-type new password: 
# 生成的密钥信息
$2y$12$p/NIsggcAYK.qr89GgY6NeiSCROoX//nOELaBX3mQl5GnxtX7tNPS
2、在prometheus的文件夹下添加配置文件
# cd /usr/local/prometheus
# vim web-config.yml 
basic_auth_users:
    admin: $2y$12$p/NIsggcAYK.qr89GgY6NeiSCROoX//nOELaBX3mQl5GnxtX7tNPS
3、修改prometheus.yml配置文件
scrape_configs:
  - job_name: "prometheus"
    basic_auth:
      username: admin	# 账号为admin
      password: 123		# 密码为123
    static_configs:
      - targets: ["192.168.20.50:39090"]
4、修改prometheus的Service文件。[我这里使用的是二进制部署的prometheus]
# vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--web.listen-address=:39090 \
--web.config.file=/usr/local/prometheus/web-config.yml \
--storage.tsdb.path=/usr/local/prometheus/data/ \
--storage.tsdb.retention=15d \
--web.enable-lifecycle

ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
5、重启服务
# systemctl restart prometheus

三、检验

在浏览器使用Ctrl+F5,强制刷新

此时需要刚刚设置的账号和密码才能登录进入prometheus的页面,防止了相关数据的泄露。


欢迎关注我的CSDN个人博客知乎