diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-08-29 12:50:15 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2022-08-29 12:50:15 -0500 |
| commit | 6d4a5106f918c474695a99aad3a4c2571e0905a4 (patch) | |
| tree | edf87f67d1d4abf64bc3e281d240deb01fb39a89 /src/slskd/Program.cs | |
| parent | 558c550761bc30665af76c7b01d20f6606105a27 (diff) | |
| download | slskd-6d4a5106f918c474695a99aad3a4c2571e0905a4.tar.gz | |
refactor Prometheus routing to support auth
Diffstat (limited to 'src/slskd/Program.cs')
| -rw-r--r-- | src/slskd/Program.cs | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 6357302b..4d109ee9 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,12 +659,6 @@ 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(); @@ -680,6 +667,7 @@ namespace slskd app.UseAuthentication(); app.UseRouting(); + app.UseHttpMetrics(); app.UseAuthorization(); app.UseEndpoints(endpoints => { @@ -690,9 +678,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}"); + + if (options.Authentication.Disabled) + { + Log.Warning("Authentication of 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); + }); } }); |
