Skip to content

feat: sign module auth token, metrics password - #1429

Merged
gsanchietti merged 2 commits into
mainfrom
local-auth-token
Dec 2, 2026
Merged

gsanchietti merged 2 commits into
mainfrom
local-auth-token

Conversation

@gsanchietti

@gsanchietti gsanchietti commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Local services of the metrics module (Prometheus, Alertmanager and Grafana) trust any program that connects from the same server. This PR gives core what it needs so the metrics module can stop trusting local connections. It must be released before the matching metrics change, which will require this core version.

Nothing changes for users. api-server now adds a short-lived signed token next to the user name it already sends to Traefik. Each node derives its signing key from its existing SECRET, so there is no new key to manage on install, update or leader change. Core reads the new metrics password to list the active alerts.

  • bcrypt added to the agent Python package lists, so modules can hash passwords.
  • api-server returns X-Auth-Token (EdDSA JWT, 60 seconds, aud = module/<id>) from the module http-basic endpoint, and serves its public key without login at GET /api/module-token-pubkey.
  • list-alerts and list-nodes read the metrics password from basic_auth.json in the metrics module state and send it to Alertmanager and Prometheus. They run as root on the leader node, where metrics also runs. Core and metrics are updated together.
  • The metrics settings page no longer sends prometheus_path: metrics no longer exposes Prometheus through Traefik.

How to test

  • curl http://127.0.0.1:9311/api/module-token-pubkey returns a PEM public key, the same after an api-server restart.
  • With the new metrics module, Grafana login through the cluster admin page works, also after a leader change.
  • The cluster alerts page shows the active alerts, and the Nodes page shows CPU, memory and disk data.

See also


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

Comment thread docs/modules/metrics.md Resolved
Comment thread core/api-server/methods/token.go Resolved
@gsanchietti
gsanchietti marked this pull request as ready for review December 1, 2026 07:39

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

Works with ns8-metrics#120 as designed. Please add the Robot tests for the new response, see inline comments.

Alertmanager, read from the metrics module running on this (leader)
node, or None if it cannot be read."""
try:
metrics_id = agent.redis_connect().get('cluster/default_instance/metrics')

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.

In an uninitialized cluster Metrics is not installed.

Suggested change
metrics_id = agent.redis_connect().get('cluster/default_instance/metrics')
metrics_id = agent.redis_connect().get('cluster/default_instance/metrics')
if not metrics_id:
return None

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.

Done

# Prometheus requires the metrics module password
PROM_AUTH = cluster.metrics.basic_auth()

def discover_prom_query_url(base_url):

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.

Metrics no longer sets a URL prefix for Prometheus, so this discovery can go. Replace the function and its call with:

PROM_QUERY_URL = "http://127.0.0.1:9091/api/v1/query"

Until metrics is updated, clusters that set prometheus_path with api-cli lose node data: same recovery as the other cases, update metrics.

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.

Done

if token, errSign := SignModuleToken(configuration.Config.TokenSigningKey, username, c.Param("module_id")); errSign != nil {
utils.LogError(errors.Wrap(errSign, "[BASIC AUTH] token signing failed for user "+username))
} else {
c.Header("X-Auth-Token", token)

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.

Please extend core/api-server/test/suite/50__basic_auth_2fa.robot to cover the new response: X-Auth-Token claims on success (sub, aud, iss, 60 seconds), no token on 401 and 403, and GET /module-token-pubkey without login. The Go unit tests cover signing only, not the handler and the route.

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.

Done

Modules behind Traefik forward auth only receive X-Auth-User, a plain
header any local process can also send. api-server now adds
X-Auth-Token, a 60 seconds EdDSA JWT for the user and the module, and
serves its public key without login at /api/module-token-pubkey. Each
node derives the key from its existing SECRET, so there is nothing to
manage on install, update or leader change.

The metrics Prometheus and Alertmanager now require a password, kept in
the metrics module state. list-alerts and list-nodes run as root on
the leader node, like the metrics module, and read it from there.
bcrypt is added to the agent Python so modules can hash passwords.

Assisted-by: Claude Code:claude-opus-5.5
ns8-metrics no longer exposes Prometheus through Traefik and no longer
has the prometheus_path setting, so the metrics settings page does not
need to read or send it.

Assisted-by: Claude Code:claude-opus-5.5
DavidePrincipi

This comment was marked as duplicate.

@gsanchietti
gsanchietti merged commit 79c70b4 into main Dec 2, 2026
5 checks passed
@gsanchietti
gsanchietti deleted the local-auth-token branch December 2, 2026 11:01
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