Skip to content

Prometheus basic auth

Хеширование пароля и создание web.config.file

Bash
sudo apt install apache2-utils -y  # для htpasswd

password='<password>'
passwordHashed=$(echo ${password} | htpasswd -inBC 10 "" | tr -d ':\n')

echo "basic_auth_users:\n  admin: '$passwordHashed'" | sudo tee /etc/prometheus/web.yml
# basic_auth_users:
#   admin: '$2y$10$VBQTOJXBEAIK086F/82Ob.TVrdExsDXRE2R5.gh3ZW.qoyFsTRQka'

# Проверяем файл 👇
promtool check web-config /etc/prometheus/web.yml
# /etc/prometheus/web.yml SUCCESS

Чтобы не было такой ошибки:

Нужно добавить basic_auth в /etc/prometheus/prometheus.yml:

YAML
scrape_configs:
  - job_name: 'prometheus'

    scrape_interval: 5s
    scrape_timeout: 5s

    static_configs:
      - targets: ['localhost:9090'] 

    basic_auth:
      username: '<your_username>'
      password: '<your_password>'

Изменение команды запуска Prometheus

Нужно поменять строку ExecStart в файле /etc/systemd/system/multi-user.target.wants/prometheus.service (может находится в чуть в другом месте):

Text Only
ExecStart=/usr/local/bin/prometheus $ARGS\
   --config.file=/etc/prometheus/prometheus.yml \
   --web.config.file=/etc/prometheus/web.yml

Перезапускай сервер:
Bash
sudo systemctl daemon-reload
sudo systemctl restart prometheus
systemctl status prometheus

Тест

Обычный curl http://localhost:9090 не пустит и выдаст Unauthorized
Нужно использовать:

Bash
curl -u admin http://localhost:9090
# Enter host password for user 'admin': 

# Либо сразу с паролем (небезопасно)
curl -u 'admin':'<password>' http://localhost:9090


Source: Защита сервера Prometheus с помощью basic аутентификации
[[Prometheus]]

prometheus #basic-http-auth #auth