Skip to content

Commit 14c3ff1

Browse files
auth added
1 parent c2e5241 commit 14c3ff1

3 files changed

Lines changed: 93 additions & 67 deletions

File tree

src/routes/prometheus.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from src.config import app, db
77
from src.models import ExternalMonitornig
88
from src.utils import ROOT_DIR
9+
from src.routes.helper.common_helper import admin_required
910
from src.routes.helper.prometheus_helper import (
1011
load_yaml,
1112
save_yaml,
@@ -16,19 +17,26 @@
1617
update_prometheus_config)
1718

1819

19-
2020
# Define the Prometheus Blueprint
2121
prometheus_bp = Blueprint('prometheus', __name__)
2222

23+
# todo, find a better way to store the username and password
24+
username = 'admin'
25+
password = 'admin'
26+
2327
# Define a route to serve Prometheus metrics
2428
@app.route('/metrics')
2529
def metrics():
30+
auth = request.authorization
31+
if not auth or not (auth.username == username and auth.password == password):
32+
return Response('Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required"'})
2633
output = generate_latest()
2734
output = '\n'.join([line for line in output.decode().split('\n') if not line.startswith('#') and line])
2835
return Response(output, mimetype='text/plain')
2936

3037
# POST request to manage file paths
3138
@app.route('/external_monitoring', methods=['GET', 'POST'])
39+
@admin_required
3240
def external_monitoring():
3341
if request.method == 'POST':
3442
file_path = request.form.get('file_path')
@@ -54,6 +62,7 @@ def external_monitoring():
5462

5563
# POST request to delete file path
5664
@app.route('/external_monitoring/delete_file_path/<int:id>', methods=['POST'])
65+
@admin_required
5766
def delete_file_path(id):
5867
file_path = ExternalMonitornig.query.get_or_404(id)
5968
db.session.delete(file_path)
@@ -62,12 +71,14 @@ def delete_file_path(id):
6271
return redirect(url_for('external_monitoring'))
6372

6473
@app.route('/configure_targets')
74+
@admin_required
6575
def configure_targets():
6676
update_prometheus_config()
6777
targets_info = show_targets()
6878
return render_template('other/targets.html', targets_info=targets_info)
6979

7080
@app.route('/targets/restart_prometheus')
81+
@admin_required
7182
def restart_prometheus():
7283
update_prometheus_config
7384
update_prometheus_container()
@@ -104,6 +115,7 @@ def add_target():
104115
return redirect(url_for('configure_targets'))
105116

106117
@app.route('/targets/remove_target', methods=['POST'])
118+
@admin_required
107119
def remove_target():
108120
job_name = request.form.get('job_name')
109121
target_to_remove = request.form.get('target_to_remove')
@@ -131,6 +143,7 @@ def remove_target():
131143
return redirect(url_for('configure_targets'))
132144

133145
@app.route('/targets/change_interval', methods=['POST'])
146+
@admin_required
134147
def change_interval():
135148
job_name = request.form.get('job_name')
136149
new_interval = request.form.get('new_interval') + 's' # New scrape interval

src/scripts/prometheus.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ scrape_configs:
4848
static_configs:
4949
- targets:
5050
- '$FLASK_APP_IP:$FLASK_APP_PORT'
51+
basic_auth:
52+
username: admin
53+
password: admin
54+
5155
EOL
5256

5357
# Check if Docker network exists

src/templates/other/targets.html

Lines changed: 75 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,85 @@
66
{% endblock %}
77

88
{% block content %}
9-
<div class="container">
10-
<h1>SystemGuard Targets</h1>
11-
{% include 'ext/message.html' %}
9+
<div class="container">
10+
<h1>SystemGuard Targets</h1>
11+
{% include 'ext/message.html' %}
1212

13-
<div class="table-wrapper">
14-
<table class="modern-table">
15-
<thead>
16-
<tr>
17-
<th>Group Name <i class="fas fa-info-circle" title="Job Name is the name of the service that is being monitored."></i></th>
18-
<th>Targets <i class="fas fa-info-circle" title="Targets are the IP addresses of the services that are being monitored."></i></th>
19-
<th>Scrape Interval <i class="fas fa-info-circle" title="Scrape Interval is the time interval between each scrape of the target."></i></th>
20-
<th>New Interval<i class="fas fa-info-circle" title="Change the scrape interval of the target."></i></th>
21-
</tr>
22-
</thead>
23-
<tbody>
24-
{% for info in targets_info %}
25-
<tr>
26-
<td>{{ info.job_name }}</td>
27-
<td>
28-
<ul class="target-list">
29-
{% for target in info.targets %}
30-
<li>
31-
{{ target }}
32-
<form action="{{ url_for('remove_target') }}" method="post" class="inline-form">
33-
<input type="hidden" name="job_name" value="{{ info.job_name }}">
34-
<input type="hidden" name="target_to_remove" value="{{ target }}">
35-
<button type="submit" class="btn-danger">
36-
<i class="fas fa-times"></i> Remove
37-
</button>
38-
</form>
39-
</li>
40-
{% endfor %}
41-
</ul>
42-
</td>
43-
<td>{{ info.scrape_interval }}</td>
44-
<td>
45-
<form action="{{ url_for('change_interval') }}" method="post" class="inline-form">
46-
<input type="hidden" name="job_name" value="{{ info.job_name }}">
47-
<input type="text" name="new_interval" placeholder="New Interval" required class="input-field">
48-
<input type="submit" value="Change Interval" class="btn-primary">
49-
</form>
50-
</td>
51-
</tr>
52-
{% endfor %}
53-
</tbody>
54-
</table>
55-
</div>
56-
57-
<div class="section">
58-
<h2>Restart Prometheus Docker Service</h2>
59-
<form action="{{ url_for('restart_prometheus') }}">
60-
<input type="submit" value="Restart Prometheus" class="btn-warning">
61-
</form>
62-
</div>
13+
<div class="table-wrapper">
14+
<table class="modern-table">
15+
<thead>
16+
<tr>
17+
<th>Group Name <i class="fas fa-info-circle"
18+
title="Job Name is the name of the service that is being monitored."></i></th>
19+
<th>Targets <i class="fas fa-info-circle"
20+
title="Targets are the IP addresses of the services that are being monitored."></i></th>
21+
<th>Scrape Interval <i class="fas fa-info-circle"
22+
title="Scrape Interval is the time interval between each scrape of the target."></i></th>
23+
<th>New Interval<i class="fas fa-info-circle" title="Change the scrape interval of the target."></i>
24+
</th>
25+
</tr>
26+
</thead>
27+
<tbody>
28+
{% for info in targets_info %}
29+
<tr>
30+
<td>{{ info.job_name }}</td>
31+
<td>
32+
<ul class="target-list">
33+
{% for target in info.targets %}
34+
<li>
35+
{{ target }}
36+
<form action="{{ url_for('remove_target') }}" method="post" class="inline-form">
37+
<input type="hidden" name="job_name" value="{{ info.job_name }}">
38+
<input type="hidden" name="target_to_remove" value="{{ target }}">
39+
<button type="submit" class="btn-danger">
40+
<i class="fas fa-times"></i> Remove
41+
</button>
42+
</form>
43+
</li>
44+
{% endfor %}
45+
</ul>
46+
</td>
47+
<td>{{ info.scrape_interval }}</td>
48+
<td>
49+
<form action="{{ url_for('change_interval') }}" method="post" class="inline-form">
50+
<input type="hidden" name="job_name" value="{{ info.job_name }}">
51+
<input type="text" name="new_interval" placeholder="New Interval" required
52+
class="input-field">
53+
<input type="submit" value="Change Interval" class="btn-primary">
54+
</form>
55+
</td>
56+
</tr>
57+
{% endfor %}
58+
</tbody>
59+
</table>
60+
</div>
6361

64-
<div class="section">
65-
<h2>Add New Target</h2>
66-
<form action="{{ url_for('add_target') }}" method="post" class="add-target-form">
67-
<input type="text" name="job_name" placeholder="Group Name" required class="input-field">
68-
<input type="text" name="new_target" placeholder="New Target" required class="input-field">
69-
<input type="text" name="scrape_interval" placeholder="Scrape Interval" required class="input-field">
70-
<br>
71-
<button type="submit" class="btn btn-primary btn-lg">
72-
<i class="fas fa-plus"></i> Add Target
73-
</button>
74-
</form>
75-
</div>
62+
<div class="section">
63+
<h2>Restart Prometheus Docker Service</h2>
64+
<form action="{{ url_for('restart_prometheus') }}">
65+
<input type="submit" value="Restart Prometheus" class="btn-warning">
66+
</form>
67+
</div>
68+
<div class="section">
69+
<h2>Add New Target</h2>
70+
<form action="{{ url_for('add_target') }}" method="post" class="add-target-form">
71+
<input type="text" name="job_name" placeholder="Group Name" required class="input-field">
72+
<input type="text" name="new_target" placeholder="New Target" required class="input-field">
73+
<input type="text" name="scrape_interval" placeholder="Scrape Interval" required class="input-field">
74+
<input type="text" name="username" placeholder="Username" required class="input-field">
75+
<!-- New Username field -->
76+
<input type="password" name="password" placeholder="Password" required class="input-field">
77+
<!-- New Password field -->
78+
<br>
79+
<button type="submit" class="btn btn-primary btn-lg">
80+
<i class="fas fa-plus"></i> Add Target
81+
</button>
82+
</form>
7683
</div>
84+
85+
</div>
7786
{% endblock %}
7887

7988
{% block extra_scripts %}
8089
<script src="{{ url_for('static', filename='js/targets.js') }}"></script>
81-
{% endblock %}
90+
{% endblock %}

0 commit comments

Comments
 (0)