|
3 | 3 | import os
|
4 | 4 | import yaml
|
5 | 5 | from collections import OrderedDict
|
| 6 | +from werkzeug.security import check_password_hash |
6 | 7 |
|
7 | 8 | from src.config import app, db
|
8 |
| -from src.models import ExternalMonitornig |
| 9 | +from src.models import ExternalMonitornig, UserProfile |
9 | 10 | from src.utils import ROOT_DIR
|
10 | 11 | from src.routes.helper.common_helper import admin_required
|
11 | 12 | from src.routes.helper.prometheus_helper import (
|
|
21 | 22 | # Define the Prometheus Blueprint
|
22 | 23 | prometheus_bp = Blueprint('prometheus', __name__)
|
23 | 24 |
|
24 |
| -# todo, find a better way to store the username and password |
25 |
| -username = 'prometheus_admin' |
26 |
| -password = 'prometheus_password' |
| 25 | +def verify_user(username, password): |
| 26 | + user = UserProfile.query.filter_by(username=username).first() |
| 27 | + if user and check_password_hash(user.password, password): |
| 28 | + return True |
27 | 29 |
|
28 | 30 | # Define a route to serve Prometheus metrics
|
29 | 31 | @app.route('/metrics')
|
30 | 32 | def metrics():
|
31 | 33 | auth = request.authorization
|
32 |
| - if not auth or not (auth.username == username and auth.password == password): |
| 34 | + if not verify_user(auth.username, auth.password): |
33 | 35 | return Response('Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required"'})
|
34 | 36 | output = generate_latest()
|
35 | 37 | output = '\n'.join([line for line in output.decode().split('\n') if not line.startswith('#') and line])
|
|
0 commit comments