diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-08-29 16:53:23 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-29 16:53:23 -0500 |
| commit | 3b84bc0fb84069491aa6a801c710b3c264563a97 (patch) | |
| tree | c18afa5445065a99e4a5c1d990189e5f39a88dfb /src/slskd/Program.cs | |
| parent | 0085fa79cbe0b2589abc9ca6a99c367ff5ff0fd5 (diff) | |
| parent | d7520caa80629ea0821d936ebf2f236e14f89abe (diff) | |
| download | slskd-3b84bc0fb84069491aa6a801c710b3c264563a97.tar.gz | |
Merge pull request #619 from slskd/metrics-auth
Refactor Prometheus implementation and add basic authentication for the /metrics endpoint
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 55 |
1 files changed, 35 insertions, 20 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 6357302b..8621a584 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -336,11 +336,6 @@ namespace slskd // bootstrap the ASP.NET application try { - if (OptionsAtStartup.Feature.Prometheus) - { - using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); - } - var builder = WebApplication.CreateBuilder(args); builder.Configuration @@ -513,6 +508,9 @@ namespace slskd .AllowCredentials() .WithExposedHeaders("X-URL-Base"))); + services.AddSystemMetrics(); + using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); + services.AddDataProtection() .PersistKeysToFileSystem(new DirectoryInfo(Path.Combine(AppDirectory, "data", ".DataProtection-Keys"))); @@ -614,11 +612,6 @@ namespace slskd }); } - if (OptionsAtStartup.Feature.Prometheus) - { - services.AddSystemMetrics(); - } - return services; } @@ -666,19 +659,12 @@ namespace slskd app.UseFileServer(fileServerOptions); Log.Information("Serving static content from {ContentPath}", contentPath); - if (OptionsAtStartup.Feature.Prometheus) - { - app.UseHttpMetrics(); - Log.Information("Publishing Prometheus metrics to /metrics"); - } - if (OptionsAtStartup.Web.Logging) { app.UseSerilogRequestLogging(); } app.UseAuthentication(); - app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => @@ -690,9 +676,38 @@ namespace slskd endpoints.MapControllers(); endpoints.MapHealthChecks("/health"); - if (OptionsAtStartup.Feature.Prometheus) + if (OptionsAtStartup.Metrics.Enabled) { - endpoints.MapMetrics("/metrics"); + var options = OptionsAtStartup.Metrics; + var url = options.Url.StartsWith('/') ? options.Url : "/" + options.Url; + + Log.Information("Publishing Prometheus metrics to {URL}", url); + + if (options.Authentication.Disabled) + { + Log.Warning("Authentication for the metrics endpoint is DISABLED"); + } + + endpoints.MapGet(url, async context => + { + if (!options.Authentication.Disabled) + { + var auth = context.Request.Headers["Authorization"].FirstOrDefault(); + var providedCreds = auth?.Split(' ').Last(); + var validCreds = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{options.Authentication.Username}:{options.Authentication.Password}")); + + if (string.IsNullOrEmpty(auth) || + !auth.StartsWith("Basic", StringComparison.InvariantCultureIgnoreCase) || + !string.Equals(providedCreds, validCreds, StringComparison.InvariantCultureIgnoreCase)) + { + context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; + return; + } + } + + var response = await Metrics.BuildAsync(); + await context.Response.WriteAsync(response); + }); } }); @@ -714,7 +729,7 @@ namespace slskd app.UseSwaggerUI(options => app.Services.GetRequiredService<IApiVersionDescriptionProvider>().ApiVersionDescriptions.ToList() .ForEach(description => options.SwaggerEndpoint($"{(urlBase == "/" ? string.Empty : urlBase)}/swagger/{description.GroupName}/swagger.json", description.GroupName))); - Log.Information("Publishing Swagger documentation to /swagger"); + Log.Information("Publishing Swagger documentation to {URL}", "/swagger"); } // if we made it this far, the caller is either looking for a route that was synthesized with a SPA router, or is genuinely confused. |
