Skip to content

Commit 526379f

Browse files
committed
Add dynamic ansible inventory from prometheus server and restrict access of /api/metrics to public
1 parent 64eb0f8 commit 526379f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
plugin: aws_ec2
3+
profile: dump-monitoring
4+
5+
regions:
6+
- eu-central-1
7+
8+
hostnames:
9+
- ip-address
10+
11+
filters:
12+
tag:Project: dump-monitoring
13+
tag:Environment: production
14+
15+
keyed_groups:
16+
- key: tags.Role
17+
leading_separator: false

infrastructure/ansible/playbooks/api/roles/api/tasks/main.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
group: '{{ ansible_user }}'
1212
mode: 0600
1313

14+
- name: Get the ip address of the prometheus production server
15+
command: cd $(git rev-parse --show-toplevel); ansible-inventory -i infrastructure/ansible/inventories/prometheus.aws_ec2.yml --list | jq -r '.aws_ec2.hosts[0]'
16+
register: production_ip
17+
1418
- name: Create new api docker container
1519
docker_container:
1620
name: '{{ container_name }}'
@@ -30,8 +34,11 @@
3034
traefik.http.middlewares.api-cors.headers.accesscontrolmaxage: '100'
3135
traefik.http.middlewares.api-cors.headers.addvaryheader: 'true'
3236
traefik.http.routers.api.middlewares: 'api-retry, api-cors'
33-
# Setting a service property ensures that the generated service name will be consistent between versions
3437
traefik.http.services.api.loadbalancer.server.scheme: 'http'
38+
# Router specifically for /api/metrics with IP restriction
39+
traefik.http.middlewares.metrics-whitelist.ipwhitelist.sourcerange: '{{ production_ip.stdout }}'
40+
traefik.http.routers.api-metrics.rule: 'Host(`{{ api_domain }}`) && PathPrefix(`/api/metrics`)'
41+
traefik.http.routers.api-metrics.middlewares: metrics-whitelist
3542

3643
- name: Swap docker containers if new one is healthy
3744
block:

0 commit comments

Comments
 (0)