Skip to content

feat!: require auth on local metrics services - #120

Merged
gsanchietti merged 6 commits into
mainfrom
hardening_2
Dec 2, 2026
Merged

gsanchietti merged 6 commits into
mainfrom
hardening_2

Conversation

@gsanchietti

@gsanchietti gsanchietti commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Prometheus, Alertmanager and Grafana listen only on 127.0.0.1, but trust any program that connects from there. Any process on the server could read metrics, mute or fake alerts, or become Grafana admin by sending one header. This PR closes those paths.

Prometheus and Alertmanager now ask for a password, and Grafana accepts only a short-lived token signed by core's api-server instead of the plain X-AUTH-USER header. Login through the cluster admin page looks the same to users. Prometheus is no longer exposed through Traefik: the /prometheus route is removed, and the README shows how to reach it for debugging. Requires NethServer/ns8-core#1429 (min-core is provisional).

  • Random password created once in basic_auth.json in the module state directory; its hash goes into the web config shared by Prometheus and Alertmanager. Prometheus, the Grafana data sources, reload-prometheus-rules and test-alert send it. Core runs as root on the same node and reads the file to list the alerts.
  • Grafana switches from [auth.proxy] to [auth.jwt], with the public key fetched from the local api-server at start.
  • restore-configuration and a new update step reuse the configure-module route code, so existing installs get routes that pass the new token.

Hand-made Loki alert rules will no longer reach Alertmanager.

How to test

  • On a node with the core from feat: sign module auth token, metrics password ns8-core#1429, install and update the module, then log in to Grafana through the cluster admin page.
  • curl http://127.0.0.1:9091/, curl http://127.0.0.1:9093/api/v2/alerts and curl -H 'X-AUTH-USER: admin' http://127.0.0.1:3000/grafana/api/user all answer 401.
  • A test alert reaches Alertmanager and the cluster alerts page.
  • Restore the module and promote another node to leader: Grafana still asks for the cluster login, and no /prometheus route exists.

CI

The automatic "Test module" run fails until NethServer/ns8-core#1429 is released: it installs the stable core, which has no token public key, so Grafana does not start. The suite passed on a temporary commit that installed the core of NethServer/ns8-core#1429 (install on debian13, update on rocky9): https://github.com/NethServer/ns8-metrics/actions/runs/46604244263. That commit is removed from this branch.

Also tested by hand on a two-node cluster with the core of NethServer/ns8-core#1429: Grafana, Prometheus and Alertmanager answer 401 without credentials, also with a forged X-AUTH-USER header or token, both on the node and through Traefik; the admin login works. Same results after promoting the worker to leader and back. The new leader installs the latest metrics release, so the instance was updated to this branch after each switch.

See also


Generated by Claude Code:claude-opus-5.5 with nethserver-pr

Comment thread README.md Resolved
Comment thread imageroot/bin/provision-grafana Resolved
Comment thread imageroot/bin/provision-grafana Resolved
Comment thread imageroot/bin/provision-grafana Resolved
Comment thread imageroot/bin/provision-prometheus Resolved
Comment thread imageroot/bin/provision-prometheus Resolved
Comment thread imageroot/bin/test-alert Resolved
@gsanchietti
gsanchietti marked this pull request as ready for review December 1, 2026 07:40

@DavidePrincipi DavidePrincipi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked with ns8-core#1429: routes on update and leader change, Grafana token claims, file permissions. Minor points inline. Optional: unset the stale PROMETHEUS_PATH env var in 30routes.

Comment thread build-images.sh
--label="org.nethserver.authorizations=traefik@node:routeadm" \
--label="org.nethserver.rootfull=0" \
--label="org.nethserver.max-per-node=1" \
--label="org.nethserver.min-core=3.26.0" \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Match also pre-releases (for tests)

Suggested change
--label="org.nethserver.min-core=3.26.0" \
--label="org.nethserver.min-core=3.26.0-0" \

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to avoid this and install with force option.
I fear to forget changing it before the merge

@DavidePrincipi DavidePrincipi Dec 2, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to change it before the merge. It's safe to merge it in the main branch and release it as stable.

Comment thread imageroot/pypkg/metrics_basic_auth.py Resolved

@DavidePrincipi DavidePrincipi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Only a merge conflict in the test suite.

Prometheus, Alertmanager and Grafana listen on 127.0.0.1 but trusted
any local process: it could read metrics, mute or fake alerts, or
become Grafana admin by sending one X-AUTH-USER header.

Prometheus and Alertmanager now require one random password, kept in
basic_auth.json in the module state and sent by Prometheus, the
Grafana data sources, reload-prometheus-rules and test-alert. Core
runs on the same node as root and reads the file to list the alerts.
Grafana switches to auth.jwt and accepts only the token signed by the
local api-server. restore-configuration and a new update step reuse
the configure-module route code, so existing installs pass the new
token. Prometheus is no longer exposed through Traefik; the README
shows how to reach it for debugging. Requires the core release that
serves the token key.

Assisted-by: Claude Code:claude-opus-5.5
Prometheus is no longer exposed through Traefik, so the setting has no
purpose. Remove it from configure-module and get-configuration, and
always serve Prometheus at the root path.

Assisted-by: Claude Code:claude-opus-5.5
metrics_alert_rules.py is a library, not a command. Move it to
imageroot/pypkg, which the module agent puts on PYTHONPATH, next to
the other module library.

Assisted-by: Claude Code:claude-opus-5.5
Grafana's Alertmanager data source has no health check and always
answers 500: query it through the Grafana proxy instead. The password
key check ran redis-cli inside a "while read" loop, so redis-cli ate
the key list and only the first key was checked.

Assisted-by: Claude Code:claude-opus-5.5
gsanchietti and others added 2 commits December 2, 2026 13:33
Robot Framework reads %{name} as an environment variable, so every
curl -w '%{http_code}' check failed with "Environment variable
'%{http_code}' not found". Escape it as \%{http_code}.

Assisted-by: Claude Code:claude-opus-5.5
Each alert rule check starts promtool in a new unnamed container. With
the default journald log driver, its output was copied to the journal
under the random container name, so the log server indexed a new log
name at every check.

The caller already captures promtool output and reports errors, so
disable the container log driver for these runs.

Assisted-by: Claude Code:claude-opus-5-5
@gsanchietti
gsanchietti merged commit ce94d31 into main Dec 2, 2026
3 checks passed
@gsanchietti
gsanchietti deleted the hardening_2 branch December 2, 2026 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants