Skip to content

Commit a822a01

Browse files
committedAug 6, 2024·
Secure scrapes for online instances
This does share some implicit configuration from the target_all but as we always deploy those together this should be fine. For online instances which are directly connected to the internet we didn't secure all metrics. This is now done for: node (builtin) mysql (builtin) fpm (builtin) nginx (Custom NGINX config)
1 parent 11b27a1 commit a822a01

File tree

7 files changed

+71
-8
lines changed

7 files changed

+71
-8
lines changed
 

‎provision-contest/ansible/roles/grafana/templates/prometheus.yml.j2

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ global:
55
scrape_configs:
66
- job_name: 'grafana'
77
static_configs:
8-
- targets: ['localhost:{{ grafana_port }}']
8+
- targets: ['localhost:3000']
99
- job_name: 'prometheus'
1010
static_configs:
1111
- targets: ['localhost:9090']
1212
- job_name: 'db'
13+
basic_auth:
14+
username: "prometheus"
15+
password: "{{ PROMETHEUS_PASS }}"
16+
tls_config:
17+
insecure_skip_verify: true
18+
scheme: https
1319
static_configs:
1420
- targets:
1521
{% for host in groups["domserver"] %}
@@ -137,6 +143,12 @@ scrape_configs:
137143
- {{ hostvars[host].ansible_host }}:9113
138144
{% endfor %}
139145
- job_name: 'web_fpm_domserver'
146+
basic_auth:
147+
username: "prometheus"
148+
password: "{{ PROMETHEUS_PASS }}"
149+
tls_config:
150+
insecure_skip_verify: true
151+
scheme: https
140152
static_configs:
141153
- targets:
142154
{% for host in groups["domserver"] %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.key
2+
*.crt

‎provision-contest/ansible/roles/prometheus_target_all/tasks/main.yml

+9
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@
2828

2929
- name: Get HTPassword
3030
delegate_to: localhost
31+
become: false
3132
shell: "echo {{ PROMETHEUS_PASS }} | htpasswd -inBC 10 \"\" | tr -d ':\n'"
3233
register: htpassd_shell
3334

35+
- name: Store HTPassword for nginx wrapper
36+
copy:
37+
content: "prometheus:{{ htpassd_shell.stdout }}"
38+
dest: /etc/prometheus/.htpasswd
39+
owner: root
40+
group: root
41+
mode: 0644
42+
3443
- name: Set certificate to encrypt node_exporter traffic
3544
template:
3645
owner: prometheus
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
basic_auth_users:
2+
prometheus: {{ htpassd_shell.stdout }}
3+
tls_server_config:
4+
cert_file: /etc/prometheus/node_exporter.crt
5+
key_file: /etc/prometheus/node_exporter.key

‎provision-contest/ansible/roles/prometheus_target_web/files/php-fpm-exporter.service

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Documentation=https://github.com/hipages/php-fpm_exporter
44
[Service]
55
User=www-data
66
Restart=always
7-
ExecStart=/usr/bin/php-fpm_exporter server --phpfpm.fix-process-count --phpfpm.scrape-uri unix:///var/run/php-fpm-domjudge.sock;/fpm_status
7+
ExecStart=/usr/bin/php-fpm_exporter server --web.listen-address :19253 --phpfpm.fix-process-count --phpfpm.scrape-uri unix:///var/run/php-fpm-domjudge.sock;/fpm_status
88
ExecReload=/bin/kill -HUP $MAINPID
99
TimeoutStopSec=20s
1010
SendSIGKILL=no

‎provision-contest/ansible/roles/prometheus_target_web/tasks/main.yml

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
- name: Scrape mysql exporter with TLS encryption
2020
lineinfile:
21-
dest: /etc/default/prometheus-mysqld-exporter
21+
dest: /etc/default/prometheus-mysqld-exporter
2222
state: present
2323
regexp: '^ARGS=""'
24-
line: 'ARGS="--web.config /etc/prometheus/prometheus-authentication.yml"'
25-
notify: Restart mysqld-exporter
24+
line: 'ARGS="--web.config.file /etc/prometheus/prometheus-authentication.yml"'
25+
notify: Restart mysqld-exporter
2626

2727
# Gather PHP-FPM statistics
2828
# The exporter from this is currently not in deb sources
@@ -66,9 +66,10 @@
6666
# Gather NGINX statistics,
6767
# Observe that we use the observed process itself in the monitoring
6868
- name: Get NGINX status
69-
synchronize:
70-
src: nginx-status.conf
69+
template:
70+
src: nginx-status.conf.j2
7171
dest: /etc/nginx/sites-enabled/nginx-status.conf
72+
mode: 0644
7273
notify: Restart nginx
7374

7475
# In the future add: --web.config /etc/prometheus/prometheus-authentication.yml"'
@@ -79,7 +80,7 @@
7980
dest: /etc/default/prometheus-nginx-exporter
8081
state: present
8182
regexp: '^ARGS=""'
82-
line: 'ARGS="-nginx.scrape-uri=http://localhost:8787/basic_status"'
83+
line: 'ARGS="-web.listen-address=127.0.0.1:19113 -nginx.scrape-uri=http://localhost:8787/basic_status"'
8384
notify: Restart nginx-exporter
8485

8586
- name: Create storage dir for exporter settings
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
server {
2+
listen 127.0.0.1:8787;
3+
server_name _default_;
4+
5+
location = /basic_status {
6+
stub_status;
7+
}
8+
}
9+
10+
server {
11+
listen 0.0.0.0:9113 ssl;
12+
ssl_certificate /etc/prometheus/node_exporter.crt;
13+
ssl_certificate_key /etc/prometheus/node_exporter.key;
14+
ssl_protocols TLSv1.3;
15+
16+
auth_basic "Prometheus scraping";
17+
auth_basic_user_file /etc/prometheus/.htpasswd;
18+
location / {
19+
proxy_pass http://127.0.0.1:19113;
20+
}
21+
}
22+
23+
server {
24+
listen 0.0.0.0:9253 ssl;
25+
ssl_certificate /etc/prometheus/node_exporter.crt;
26+
ssl_certificate_key /etc/prometheus/node_exporter.key;
27+
ssl_protocols TLSv1.3;
28+
29+
auth_basic "Prometheus scraping";
30+
auth_basic_user_file /etc/prometheus/.htpasswd;
31+
location / {
32+
proxy_pass http://127.0.0.1:19253;
33+
}
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.