Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #90

Merged
merged 2 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/oidc-guard/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -63,6 +62,8 @@

builder.Services.AddMetrics();

builder.Services.AddSingleton<Instrumentation>();

builder.Logging.AddFilter("Default", settings.LogLevel);
builder.Logging.AddFilter("Microsoft.AspNetCore", LogLevel.Warning);
builder.Logging.AddFilter("Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware", settings.LogLevel);
Expand Down Expand Up @@ -238,22 +239,17 @@
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<long>("oidc_guard_signin", description: "Number of Sign-in operations ongoing.").Add(1);

var meter = meterFactory.Create("oidc_guard");

var AuthorizedCounter = meter.CreateCounter<long>("oidc_guard_authorized", description: "Number of Authorized operations ongoing.");
var UnauthorizedCounter = meter.CreateCounter<long>("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 &&
!StringValues.IsNullOrEmpty(httpContext.Request.Headers.AccessControlRequestHeaders) &&
!StringValues.IsNullOrEmpty(httpContext.Request.Headers.AccessControlRequestMethod) &&
!StringValues.IsNullOrEmpty(httpContext.Request.Headers.Origin))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}

Expand All @@ -268,7 +264,7 @@
{
foreach (var item in skipEquals)
{
var commaIndex = item.IndexOf(',');

Check warning on line 267 in src/oidc-guard/Program.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Dereference of a possibly null reference.

Check warning on line 267 in src/oidc-guard/Program.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Dereference of a possibly null reference.
if (commaIndex != -1)
{
var method = item[..commaIndex];
Expand All @@ -276,15 +272,15 @@

if (method == originalMethod && Regex.IsMatch(originalUrl, regex))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
else
{
if (Regex.IsMatch(originalUrl, item))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
Expand All @@ -295,7 +291,7 @@
{
foreach (var item in skipNotEquals)
{
var commaIndex = item.IndexOf(',');

Check warning on line 294 in src/oidc-guard/Program.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Dereference of a possibly null reference.

Check warning on line 294 in src/oidc-guard/Program.cs

View workflow job for this annotation

GitHub Actions / Build & Test

Dereference of a possibly null reference.
if (commaIndex != -1)
{
var method = item[..commaIndex];
Expand All @@ -303,15 +299,15 @@

if (method != originalMethod && !Regex.IsMatch(originalUrl, regex))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
else
{
if (!Regex.IsMatch(originalUrl, item))
{
AuthorizedCounter.Add(1);
meters.AuthorizedCounter.Add(1);
return Results.Ok();
}
}
Expand All @@ -321,7 +317,7 @@

if (httpContext.User.Identity?.IsAuthenticated == false)
{
UnauthorizedCounter.Add(1);
meters.UnauthorizedCounter.Add(1);
return Results.Unauthorized();
}

Expand Down Expand Up @@ -419,37 +415,37 @@
}
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<long>("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<long>("oidc_guard_signout", description: "Number of Sign-out operations ongoing.").Add(1);
meters.SignOutCounter.Add(1);

return Results.SignOut(new AuthenticationProperties { RedirectUri = rd.ToString() });
})
Expand Down
24 changes: 24 additions & 0 deletions src/oidc-guard/Services/Instrumentation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.Metrics;

namespace oidc_guard.Services
{
public class Instrumentation
{
public Counter<long> SignInCounter { get; private set; }
public Counter<long> SignOutCounter { get; private set; }
public Counter<long> AuthorizedCounter { get; private set; }
public Counter<long> UnauthorizedCounter { get; private set; }

public Instrumentation(IMeterFactory meterFactory)
{
const string prefix = "oidc_guard";
var meter = meterFactory.Create(prefix);

SignInCounter = meter.CreateCounter<long>(prefix + "_signin", description: "Number of Sign-in operations ongoing.");
SignOutCounter = meter.CreateCounter<long>(prefix + "_signout", description: "Number of Sign-out operations ongoing.");
AuthorizedCounter = meter.CreateCounter<long>(prefix + "_authorized", description: "Number of Authorized operations ongoing.");
UnauthorizedCounter = meter.CreateCounter<long>(prefix + "_unauthorized", description: "Number of Unauthorized operations ongoing.");
}
}
}
2 changes: 1 addition & 1 deletion src/oidc-guard/oidc-guard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="7.0.3" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.6.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.6.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.EventCounters" Version="1.5.1-alpha.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.1" />
<PackageReference Include="System.Private.Uri" Version="4.3.2" />
Expand Down
Loading