diff --git a/src/oidc-guard/Program.cs b/src/oidc-guard/Program.cs index 96e9198..9945bda 100644 --- a/src/oidc-guard/Program.cs +++ b/src/oidc-guard/Program.cs @@ -14,7 +14,6 @@ using oidc_guard.Services; using OpenTelemetry.Metrics; using OpenTelemetry.Resources; -using System.Diagnostics.Metrics; using System.Text.Json.Nodes; using System.Text.RegularExpressions; @@ -63,6 +62,8 @@ public static void Main(string[] args) builder.Services.AddMetrics(); + builder.Services.AddSingleton(); + builder.Logging.AddFilter("Default", settings.LogLevel); builder.Logging.AddFilter("Microsoft.AspNetCore", LogLevel.Warning); builder.Logging.AddFilter("Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware", settings.LogLevel); @@ -238,14 +239,9 @@ public static void Main(string[] args) app.MapGet("/userinfo", (HttpContext httpContext) => httpContext.User.Claims.GroupBy(x => x.Type).ToDictionary(x => x.Key, y => y.Count() > 1 ? (object)y.Select(x => x.Value) : y.First().Value)) .RequireAuthorization(); - app.MapGet("/auth", ([FromServices] Settings settings, [FromServices] IMeterFactory meterFactory, HttpContext httpContext) => + app.MapGet("/auth", ([FromServices] Settings settings, [FromServices] Instrumentation meters, HttpContext httpContext) => { - meterFactory.Create("oidc_guard").CreateCounter("oidc_guard_signin", description: "Number of Sign-in operations ongoing.").Add(1); - - var meter = meterFactory.Create("oidc_guard"); - - var AuthorizedCounter = meter.CreateCounter("oidc_guard_authorized", description: "Number of Authorized operations ongoing."); - var UnauthorizedCounter = meter.CreateCounter("oidc_guard_unauthorized", description: "Number of Unauthorized operations ongoing."); + meters.SignInCounter.Add(1); if (settings.SkipAuthPreflight && httpContext.Request.Headers[CustomHeaderNames.OriginalMethod][0] == HttpMethod.Options.Method && @@ -253,7 +249,7 @@ public static void Main(string[] args) !StringValues.IsNullOrEmpty(httpContext.Request.Headers.AccessControlRequestMethod) && !StringValues.IsNullOrEmpty(httpContext.Request.Headers.Origin)) { - AuthorizedCounter.Add(1); + meters.AuthorizedCounter.Add(1); return Results.Ok(); } @@ -276,7 +272,7 @@ public static void Main(string[] args) if (method == originalMethod && Regex.IsMatch(originalUrl, regex)) { - AuthorizedCounter.Add(1); + meters.AuthorizedCounter.Add(1); return Results.Ok(); } } @@ -284,7 +280,7 @@ public static void Main(string[] args) { if (Regex.IsMatch(originalUrl, item)) { - AuthorizedCounter.Add(1); + meters.AuthorizedCounter.Add(1); return Results.Ok(); } } @@ -303,7 +299,7 @@ public static void Main(string[] args) if (method != originalMethod && !Regex.IsMatch(originalUrl, regex)) { - AuthorizedCounter.Add(1); + meters.AuthorizedCounter.Add(1); return Results.Ok(); } } @@ -311,7 +307,7 @@ public static void Main(string[] args) { if (!Regex.IsMatch(originalUrl, item)) { - AuthorizedCounter.Add(1); + meters.AuthorizedCounter.Add(1); return Results.Ok(); } } @@ -321,7 +317,7 @@ public static void Main(string[] args) if (httpContext.User.Identity?.IsAuthenticated == false) { - UnauthorizedCounter.Add(1); + meters.UnauthorizedCounter.Add(1); return Results.Unauthorized(); } @@ -419,37 +415,37 @@ public static void Main(string[] args) } else if (!httpContext.User.Claims.Any(x => x.Type == item.Key && item.Value.Contains(x.Value))) { - UnauthorizedCounter.Add(1); + meters.UnauthorizedCounter.Add(1); //return Results.Unauthorized($"Claim {item.Key} does not match!"); return Results.Unauthorized(); } } } - AuthorizedCounter.Add(1); + meters.AuthorizedCounter.Add(1); return Results.Ok(); }); - app.MapGet("/signin", ([FromServices] Settings settings, [FromServices] IMeterFactory meterFactory, [FromQuery] Uri rd) => + app.MapGet("/signin", ([FromServices] Settings settings, [FromServices] Instrumentation meters, [FromQuery] Uri rd) => { if (!ValidateRedirect(rd, settings)) { return Results.BadRequest(); } - meterFactory.Create("oidc_guard").CreateCounter("oidc_guard_signin", description: "Number of Sign-in operations ongoing.").Add(1); + meters.SignInCounter.Add(1); return Results.Challenge(new AuthenticationProperties { RedirectUri = rd.ToString() }); }); - app.MapGet("/signout", ([FromServices] Settings settings, [FromServices] IMeterFactory meterFactory, [FromQuery] Uri rd) => + app.MapGet("/signout", ([FromServices] Settings settings, [FromServices] Instrumentation meters, [FromQuery] Uri rd) => { if (!ValidateRedirect(rd, settings)) { return Results.BadRequest(); } - meterFactory.Create("oidc_guard").CreateCounter("oidc_guard_signout", description: "Number of Sign-out operations ongoing.").Add(1); + meters.SignOutCounter.Add(1); return Results.SignOut(new AuthenticationProperties { RedirectUri = rd.ToString() }); }) diff --git a/src/oidc-guard/Services/Instrumentation.cs b/src/oidc-guard/Services/Instrumentation.cs new file mode 100644 index 0000000..b06e64a --- /dev/null +++ b/src/oidc-guard/Services/Instrumentation.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Mvc; +using System.Diagnostics.Metrics; + +namespace oidc_guard.Services +{ + public class Instrumentation + { + public Counter SignInCounter { get; private set; } + public Counter SignOutCounter { get; private set; } + public Counter AuthorizedCounter { get; private set; } + public Counter UnauthorizedCounter { get; private set; } + + public Instrumentation(IMeterFactory meterFactory) + { + const string prefix = "oidc_guard"; + var meter = meterFactory.Create(prefix); + + SignInCounter = meter.CreateCounter(prefix + "_signin", description: "Number of Sign-in operations ongoing."); + SignOutCounter = meter.CreateCounter(prefix + "_signout", description: "Number of Sign-out operations ongoing."); + AuthorizedCounter = meter.CreateCounter(prefix + "_authorized", description: "Number of Authorized operations ongoing."); + UnauthorizedCounter = meter.CreateCounter(prefix + "_unauthorized", description: "Number of Unauthorized operations ongoing."); + } + } +} diff --git a/src/oidc-guard/oidc-guard.csproj b/src/oidc-guard/oidc-guard.csproj index 95687a5..bac20a2 100644 --- a/src/oidc-guard/oidc-guard.csproj +++ b/src/oidc-guard/oidc-guard.csproj @@ -19,7 +19,7 @@ - +