Skip to content

Commit ad2e53a

Browse files
Refactor Prometheus config update function and improve authentication security
1 parent 14c3ff1 commit ad2e53a

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

src/routes/helper/prometheus_helper.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,39 @@ def show_targets():
4343

4444
def update_prometheus_config():
4545
"""
46-
update the first target whenever the function called
47-
as the network change the ip address will also change
48-
so 1st ip address in the list is the ip adddress of the machine
49-
with the job localhost
46+
Update the first target whenever the function is called.
47+
If the network changes, the IP address will also change.
48+
This updates the first IP address in the list, assuming it's the machine with the 'localhost' job.
5049
"""
5150
print("Updating Prometheus config...")
51+
52+
# Get the machine's IP address
5253
ipv4_address = subprocess.run(['hostname', '-I'], capture_output=True, text=True, check=True).stdout.split()[0]
54+
55+
# Define the path to Prometheus config file
5356
prometheus_config_path = os.path.join(ROOT_DIR, 'prometheus_config', 'prometheus.yml')
57+
58+
# Load the existing config
5459
config = load_yaml(prometheus_config_path)
55-
# fetch the localhost job
60+
# Fetch the 'localhost' job
5661
localhost_job = next((job for job in config['scrape_configs'] if job['job_name'] == 'localhost'), None)
62+
5763
if localhost_job:
64+
# Update the IP address for the 'localhost' job target
5865
localhost_job['static_configs'][0]['targets'][0] = f'{ipv4_address}:5050'
66+
67+
# localhost_job['basic_auth'] = {
68+
# 'username': "username",
69+
# 'password': "password"
70+
# }
71+
72+
# Save the updated config
5973
save_yaml(config, prometheus_config_path)
74+
75+
print("Prometheus config updated successfully.")
6076
return True
6177

78+
print("No 'localhost' job found in Prometheus config.")
6279
return False
6380

6481
def update_prometheus_container():

src/routes/prometheus.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
prometheus_bp = Blueprint('prometheus', __name__)
2222

2323
# todo, find a better way to store the username and password
24-
username = 'admin'
25-
password = 'admin'
24+
username = 'prometheus_admin'
25+
password = 'prometheus_password'
2626

2727
# Define a route to serve Prometheus metrics
2828
@app.route('/metrics')
@@ -73,7 +73,7 @@ def delete_file_path(id):
7373
@app.route('/configure_targets')
7474
@admin_required
7575
def configure_targets():
76-
update_prometheus_config()
76+
# update_prometheus_config()
7777
targets_info = show_targets()
7878
return render_template('other/targets.html', targets_info=targets_info)
7979

src/scripts/prometheus.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ scrape_configs:
4949
- targets:
5050
- '$FLASK_APP_IP:$FLASK_APP_PORT'
5151
basic_auth:
52-
username: admin
53-
password: admin
52+
username: prometheus_admin
53+
password: prometheus_password
5454
5555
EOL
5656

0 commit comments

Comments
 (0)