Securing the Stack
TLS, auth, the admin API, and Grafana permissions
The lab stack has no authentication anywhere. That's fine on a laptop and a serious problem anywhere else. Metrics leak more than people expect: hostnames, internal service names, versions with known vulnerabilities, business volumes, sometimes secrets someone put in a label. And several endpoints can change or destroy state.
What's exposed by default
An unauthenticated Alertmanager on the network lets anyone silence
every alert for a year. An unauthenticated Prometheus with the admin API
lets anyone delete all your data. Both are found regularly on the public
internet by scanners. Never expose these ports publicly, and treat
--web.enable-lifecycle and --web.enable-admin-api as privileged.
Network first
The strongest control is not being reachable. Put Prometheus, Alertmanager, exporters, and the Pushgateway on a private network, expose only Grafana (behind SSO), and let operators reach the rest through a VPN or bastion. In this compose stack, exporters already publish no host ports; only Prometheus can reach them on the Docker network.
TLS and basic auth: the web config file
All official Prometheus components (server, Alertmanager, Pushgateway,
node_exporter, blackbox_exporter, and more) share the same
--web.config.file format for TLS and basic auth:
tls_server_config:
cert_file: /etc/prometheus/tls/server.crt
key_file: /etc/prometheus/tls/server.key
basic_auth_users:
# bcrypt hash, generated with: htpasswd -nBC 10 "" | tr -d ':\n'
admin: $2y$10$...# validate it
docker exec prometheus promtool check web-config /etc/prometheus/web.yml
# then start with
--web.config.file=/etc/prometheus/web.ymlThe scraping side then needs matching credentials in its scrape config:
- job_name: node
scheme: https
tls_config:
ca_file: /etc/prometheus/tls/ca.crt
basic_auth:
username: prometheus
password_file: /etc/prometheus/secrets/node-exporterAlways use *_file options for secrets, so they live in mounted files
(Docker/Kubernetes secrets) rather than in the config.
Least privilege for exporters
Exporters need credentials to the systems they read. Give them only read access to statistics:
- Postgres: a dedicated role with
pg_monitor, not a superuser. - ClickHouse: a user with read access to
system.*tables only. - Cloud exporters: a read-only IAM role scoped to the metrics APIs.
Grafana
Grafana is usually the only component users touch, so it carries most of the access control:
| Concept | Use |
|---|---|
| SSO (OAuth/OIDC, SAML, LDAP) | Log in with company identity; disable local sign-up (GF_USERS_ALLOW_SIGN_UP=false, as this stack does) |
| Organisations | Hard separation, e.g. separate customers. Rarely needed. |
| Teams | Group users, usually synced from SSO groups |
| Folder permissions | Who can view or edit which dashboards |
| Roles | Viewer, Editor, Admin (plus fine-grained RBAC in Enterprise) |
| Service accounts | Tokens for automation (Terraform, CI), instead of personal API keys |
Change the admin password before the first start in any shared
environment. This stack reads it from .env. Grafana stores it in its
database on first boot, so changing .env later does nothing, the
same first-start-only behaviour as the Postgres image's credentials.
Use grafana cli admin reset-admin-password to change it afterwards.
Note that a Grafana user who can query a data source can query everything in it. Dashboard permissions don't restrict PromQL. If teams must not see each other's metrics, they need separate data sources (separate Prometheus servers, or Mimir tenants), not separate folders.
Data hygiene
- No secrets or PII in labels. Exporters sometimes put connection
strings or query text in labels. Drop them with
metric_relabel_configs. - Scrape config is visible to anyone with UI access, including target
URLs and, in old versions, inline credentials. Another reason for
*_filesecrets. - Grafana snapshots and public dashboards publish data outside authentication. Disable them unless you need them.
Lab
- Generate a bcrypt hash:
htpasswd -nBC 10 "" | tr -d ':\n'(or use any bcrypt generator). - Write
prometheus/config/web.ymlwith abasic_auth_usersentry. Validate it withpromtool check web-config. - Add
--web.config.file=/etc/prometheus/web.ymlto the Prometheus command and recreate the container. - Confirm
curl localhost:9090/api/v1/query?query=upnow returns 401, and works with-u admin:yourpassword. - Grafana's data source is now broken. Fix it by adding
basicAuthtodatasources.ymlwith the password from an environment variable. - The self-scrape job is broken too. Fix it with
basic_authin the scrape config. Then revert all of it, since the rest of the module assumes no auth.