Skip to content

fix(metrics): restore Prometheus scraping broken by FallbackPolicy#23

Merged
regix1 merged 2 commits into
regix1:mainfrom
fhibler:main
May 13, 2026
Merged

fix(metrics): restore Prometheus scraping broken by FallbackPolicy#23
regix1 merged 2 commits into
regix1:mainfrom
fhibler:main

Conversation

@fhibler
Copy link
Copy Markdown
Contributor

@fhibler fhibler commented May 8, 2026

28e5ca2 ("security fixes") introduced FallbackPolicy = RequireAuthenticatedUser, which requires every endpoint to have an authenticated principal.

Since MapPrometheusScrapingEndpoint() has no [AllowAnonymous] and MetricsAuthenticationMiddleware was registered after UseAuthorization, the authorization middleware rejected all unauthenticated scrape requests with 401 before the metrics middleware ever ran — making both RequireAuthForMetrics=false (public metrics) and =true (API key) non-functional.

Three changes to fix this:

  1. Program.cs: Move MetricsAuthenticationMiddleware to before UseAuthorization so it can set context.User before the FallbackPolicy is evaluated.
  2. MetricsAuthenticationMiddleware.cs: Set context.User to a synthetic "prometheus-scraper" principal in both the public path (RequireAuthForMetrics=false) and the authenticated path (valid API key), satisfying RequireAuthenticatedUser without a session cookie.
  3. AuthenticationHelper.cs: Accept Authorization: Bearer <key> as an alternativeto the X-Api-Key header, matching the standard Prometheus authorization: scrape_config block documented in the README.

fhibler added 2 commits May 6, 2026 22:15
…8e5ca2)

28e5ca2 ("security fixes") introduced `FallbackPolicy = RequireAuthenticatedUser`,
which requires every endpoint to have an authenticated principal. Since
`MapPrometheusScrapingEndpoint()` has no `[AllowAnonymous]` and
`MetricsAuthenticationMiddleware` was registered after `UseAuthorization`, the
authorization middleware rejected all unauthenticated scrape requests with 401
before the metrics middleware ever ran — making both `RequireAuthForMetrics=false`
(public metrics) and `=true` (API key) non-functional.

Three changes to fix this:

1. Program.cs: Move `MetricsAuthenticationMiddleware` to before `UseAuthorization`
   so it can set context.User before the FallbackPolicy is evaluated.

2. MetricsAuthenticationMiddleware.cs: Set `context.User` to a synthetic
   "prometheus-scraper" principal in both the public path (RequireAuthForMetrics=false)
   and the authenticated path (valid API key), satisfying RequireAuthenticatedUser
   without a session cookie.

3. AuthenticationHelper.cs: Accept `Authorization: Bearer <key>` as an alternative
   to the `X-Api-Key` header, matching the standard Prometheus `authorization:`
   scrape_config block documented in the README.
The env var is a default fallback only — once the toggle is saved via
the UI, the persisted state.json value takes priority and the env var
has no effect. Document this in the Security config table and add a
troubleshooting entry with two fix paths (UI toggle and direct
state.json edit).
@regix1 regix1 merged commit 68196cd into regix1:main May 13, 2026
2 checks passed
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