Learn Labs
Security & Operations

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

Prometheus :9090all metrics, full config (Status → Configuration), every target's addressPOST /-/reloadwith --web.enable-lifecyclePOST /-/quitwith --web.enable-lifecycle: stops Prometheus/api/v1/admin/*with --web.enable-admin-api: delete series, snapshot/api/v1/writewith --web.enable-remote-write-receiver: anyone can inject dataAlertmanager :9093create/expire silences, i.e. mute any alertPushgateway :9091anyone can push or delete metricsExporters :9100 etc.host and database internalsGrafana :3000its own auth, but default admin/admin if never changed

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.yml

The 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-exporter

Always 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:

ConceptUse
SSO (OAuth/OIDC, SAML, LDAP)Log in with company identity; disable local sign-up (GF_USERS_ALLOW_SIGN_UP=false, as this stack does)
OrganisationsHard separation, e.g. separate customers. Rarely needed.
TeamsGroup users, usually synced from SSO groups
Folder permissionsWho can view or edit which dashboards
RolesViewer, Editor, Admin (plus fine-grained RBAC in Enterprise)
Service accountsTokens for automation (Terraform, CI), instead of personal API keys

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 *_file secrets.
  • Grafana snapshots and public dashboards publish data outside authentication. Disable them unless you need them.

Lab

  1. Generate a bcrypt hash: htpasswd -nBC 10 "" | tr -d ':\n' (or use any bcrypt generator).
  2. Write prometheus/config/web.yml with a basic_auth_users entry. Validate it with promtool check web-config.
  3. Add --web.config.file=/etc/prometheus/web.yml to the Prometheus command and recreate the container.
  4. Confirm curl localhost:9090/api/v1/query?query=up now returns 401, and works with -u admin:yourpassword.
  5. Grafana's data source is now broken. Fix it by adding basicAuth to datasources.yml with the password from an environment variable.
  6. The self-scrape job is broken too. Fix it with basic_auth in the scrape config. Then revert all of it, since the rest of the module assumes no auth.

On this page