fix(metrics): restore Prometheus scraping broken by FallbackPolicy#23
Merged
Conversation
…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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
28e5ca2 ("security fixes") introduced
FallbackPolicy = RequireAuthenticatedUser, which requires every endpoint to have an authenticated principal.Since
MapPrometheusScrapingEndpoint()has no[AllowAnonymous]andMetricsAuthenticationMiddlewarewas registered afterUseAuthorization, the authorization middleware rejected all unauthenticated scrape requests with 401 before the metrics middleware ever ran — making bothRequireAuthForMetrics=false(public metrics) and=true(API key) non-functional.Three changes to fix this:
MetricsAuthenticationMiddlewareto beforeUseAuthorizationso it can set context.User before the FallbackPolicy is evaluated.context.Userto 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.Authorization: Bearer <key>as an alternativeto theX-Api-Keyheader, matching the standard Prometheusauthorization:scrape_config block documented in the README.