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

refactor: Removing all dependency to Guard.NET. #465

Merged
merged 32 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d8e6a31
wip
Nov 29, 2024
011f946
wip logging core
joachimgoris Dec 3, 2024
0d6462a
Finish Arcus.WebApi.Logging.Core
joachimgoris Dec 4, 2024
38ba612
Finish Arcus.WebApi.OpenApi.Extensions
joachimgoris Dec 4, 2024
333166d
remove guard.net
Dec 31, 2024
d6f6299
remove guard.net package from logging.core
Dec 31, 2024
3a4451b
Merge branch 'joachim/463' of https://github.com/joachimgoris/arcus.w…
Dec 31, 2024
6781a19
remove guard.net from webapi.logging
Dec 31, 2024
d1c0350
remove guard.net package from webapi.security
Dec 31, 2024
0a8c36d
wip webapi.security
Dec 31, 2024
3b364d5
remove guard.net from source code
joachimgoris Dec 31, 2024
1c25569
remove guard.net from test code
joachimgoris Dec 31, 2024
3925d90
fix compilation error
joachimgoris Dec 31, 2024
e7370d3
fix compilation errors
joachimgoris Dec 31, 2024
8f1be5f
use StartsWith(char) instead of StarsWith(string)
joachimgoris Dec 31, 2024
14eab78
Correct Hosting projects
joachimgoris Jan 6, 2025
2652089
Fix comments on .Logging
joachimgoris Jan 6, 2025
c0acdeb
apply comments to Logging.AzureFunctions
joachimgoris Jan 6, 2025
a6e0e09
Fixed PR comments in Logging.Core
joachimgoris Jan 6, 2025
1162dd1
Fix comments on OpenApi.Extensions
joachimgoris Jan 6, 2025
60fd60a
Fix comments .Security
joachimgoris Jan 6, 2025
53074a9
Use char check instead of string check for single character
joachimgoris Jan 6, 2025
1366d0e
small polish
joachimgoris Jan 6, 2025
cbb0c0d
add whitelines
joachimgoris Jan 6, 2025
a2c4f49
Revert oneliner
joachimgoris Jan 9, 2025
f9b4671
Adapt to PR comments
joachimgoris Jan 9, 2025
d73675f
fix unit tests
joachimgoris Jan 14, 2025
f6ff118
Update src/Arcus.WebApi.Logging.Core/Correlation/HttpCorrelationClien…
fgheysels Jan 16, 2025
99cc4cd
Update src/Arcus.WebApi.Logging.Core/Correlation/HttpCorrelationClien…
fgheysels Jan 16, 2025
3705279
Update src/Arcus.WebApi.OpenApi.Extensions/OAuthAuthorizeOperationFil…
fgheysels Jan 16, 2025
7d17a69
Update src/Arcus.WebApi.OpenApi.Extensions/SharedAccessKeyAuthenticat…
fgheysels Jan 16, 2025
bf4a3b5
Update src/Arcus.WebApi.OpenApi.Extensions/SharedAccessKeyAuthenticat…
fgheysels Jan 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Worker.Core" Version="1.6.0" />
<PackageReference Include="Guard.Net" Version="3.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
private static async Task<HttpRequestData> DetermineHttpRequestAsync(FunctionContext context)
{
HttpRequestData request = await context.GetHttpRequestDataAsync();

if (request is null)
{
throw new InvalidOperationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Arcus.WebApi.Hosting.AzureFunctions.Formatting;
using GuardNet;
using Microsoft.Azure.Functions.Worker;

// ReSharper disable once CheckNamespace
Expand All @@ -20,7 +19,10 @@ public static class IFunctionsWorkerApplicationBuilderExtensions
public static IFunctionsWorkerApplicationBuilder UseOnlyJsonFormatting(
this IFunctionsWorkerApplicationBuilder builder)
{
Guard.NotNull(builder, nameof(builder), "Requires a function worker builder instance to add the JSON formatting middleware");
if (builder is null)
{
throw new ArgumentNullException(nameof(builder), "Requires a function worker builder instance to add the JSON formatting middleware");
}

builder.UseMiddleware<AzureFunctionsJsonFormattingMiddleware>();
return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Text.Json;
using Azure.Core.Serialization;
using GuardNet;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -24,8 +23,15 @@ public static IServiceCollection ConfigureJsonFormatting(
this IFunctionsWorkerApplicationBuilder builder,
Action<JsonSerializerOptions> configureOptions)
{
Guard.NotNull(builder, nameof(builder), "Requires an Azure Functions application builder instance to add the JSON serializer to the application services");
Guard.NotNull(configureOptions, nameof(configureOptions), "Requires a function to configure the JSON serialization options to add the JSON serializer to the application services");
if (builder is null)
{
throw new ArgumentNullException(nameof(builder), "Requires an Azure Functions application builder instance to add the JSON serializer to the application services");
}

if (configureOptions is null)
{
throw new ArgumentNullException(nameof(configureOptions), "Requires a function to configure the JSON serialization options to add the JSON serializer to the application services");
}

var options = new JsonSerializerOptions();
configureOptions(options);
Expand Down
4 changes: 0 additions & 4 deletions src/Arcus.WebApi.Hosting/Arcus.WebApi.Hosting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Guard.Net" Version="3.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Linq;
using System.Text.Json;
using GuardNet;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -20,7 +19,10 @@ public static class MvcOptionsExtensions
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="options"/> is <c>null</c>.</exception>
public static MvcOptions OnlyAllowJsonFormatting(this MvcOptions options)
{
Guard.NotNull(options, nameof(options), "Requires MVC options to restrict the formatting to only JSON formatting");
if (options is null)
{
throw new ArgumentNullException(nameof(options), "Requires MVC options to restrict the formatting to only JSON formatting");
}

IInputFormatter[] allButJsonInputFormatters =
options.InputFormatters.Where(formatter => !(formatter is SystemTextJsonInputFormatter))
Expand All @@ -46,8 +48,15 @@ public static MvcOptions OnlyAllowJsonFormatting(this MvcOptions options)
[Obsolete("Use the " + nameof(MvcCoreMvcBuilderExtensions) + "." + nameof(MvcCoreMvcBuilderExtensions.AddJsonOptions) + " instead to configure the JSON formatters")]
public static MvcOptions ConfigureJsonFormatting(this MvcOptions options, Action<JsonSerializerOptions> configureOptions)
{
Guard.NotNull(options, nameof(options), "Requires MVC options to configure the JSON formatters");
Guard.NotNull(configureOptions, nameof(configureOptions), "Requires a function to configure the JSON formatters in the MVC options");
if (options is null)
{
throw new ArgumentNullException(nameof(options), "Requires MVC options to configure the JSON formatters");
}

if (configureOptions is null)
{
throw new ArgumentNullException(nameof(configureOptions), "Requires a function to configure the JSON formatters in the MVC options");
}

SystemTextJsonInputFormatter[] onlyJsonInputFormatters =
options.InputFormatters.OfType<SystemTextJsonInputFormatter>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
ILogger logger = context.GetLogger<AzureFunctionsRequestTrackingMiddleware>();

HttpRequestData request = await context.GetHttpRequestDataAsync();

if (request is null || IsRequestPathOmitted(PathString.FromUriComponent(request.Url), logger))
{
await next(context);
Expand All @@ -60,12 +61,12 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
HttpResponseData response = context.GetHttpResponseData();
var attributeTrackedStatusCodes = Enumerable.Empty<StatusCodeRange>();

if (response != null && AllowedToTrackStatusCode((int) response.StatusCode, attributeTrackedStatusCodes, logger))
if (response != null && AllowedToTrackStatusCode((int)response.StatusCode, attributeTrackedStatusCodes, logger))
{
string responseBody = await GetPotentialResponseBodyAsync(response, logger);
LogRequest(requestBody, responseBody, request, response, measurement, logger);
LogRequest(requestBody, responseBody, request, response, measurement, logger);
}
}
}
}
}
}
Expand Down Expand Up @@ -150,7 +151,7 @@ private void LogRequest(string requestBody, string responseBody, HttpRequestData
request.Url.Host,
request.Url.AbsolutePath,
operationName: null,
(int) response.StatusCode,
(int)response.StatusCode,
duration.StartTime,
duration.Elapsed,
logContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Net;
using System.Threading.Tasks;
using Arcus.WebApi.Logging.Core.Correlation;
using GuardNet;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.Functions.Worker.Middleware;
Expand All @@ -25,8 +24,15 @@ public class AzureFunctionsCorrelationMiddleware : IFunctionsWorkerMiddleware
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="context"/> or <paramref name="next"/> is <c>nul</c>.</exception>
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
Guard.NotNull(context, nameof(context), "Requires a function context instance of the current Azure Function invocation to HTTP correlate the HTTP request");
Guard.NotNull(next, nameof(next), "Requires a 'next' function to chain this HTTP correlation middleware to the next action in the HTTP request pipeline");
if (context is null)
{
throw new ArgumentNullException(nameof(context), "Requires a function context instance of the current Azure Function invocation to HTTP correlate the HTTP request");
}

if (next is null)
{
throw new ArgumentNullException(nameof(next), "Requires a 'next' function to chain this HTTP correlation middleware to the next action in the HTTP request pipeline");
}

var service = context.InstanceServices.GetRequiredService<AzureFunctionsHttpCorrelation>();
HttpRequestData request = await DetermineHttpRequestAsync(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using Arcus.Observability.Correlation;
using Arcus.WebApi.Logging.Core.Correlation;
using GuardNet;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.Functions.Worker.Http;
Expand Down Expand Up @@ -63,7 +62,10 @@ public AzureFunctionsHttpCorrelation(
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="request"/> is <c>null</c>.</exception>
protected override IHeaderDictionary GetRequestHeaders(HttpRequestData request)
{
Guard.NotNull(request, nameof(request), "Requires a HTTP request instance to retrieve the HTTP request headers");
if (request is null)
{
throw new ArgumentNullException(nameof(request), "Requires a HTTP request instance to retrieve the HTTP request headers");
}

Dictionary<string, StringValues> dictionary =
request.Headers.ToDictionary(
Expand Down Expand Up @@ -128,9 +130,20 @@ protected override HttpCorrelationResult CorrelateW3CForExistingParent(IHeaderDi
/// <exception cref="ArgumentException">Thrown when the <paramref name="headerName"/> or <paramref name="headerValue"/> is blank.</exception>
protected override void SetHttpResponseHeader(HttpResponseData response, string headerName, string headerValue)
{
Guard.NotNull(response, nameof(response), "Requires a HTTP response to set the HTTP correlation headers");
Guard.NotNullOrWhitespace(headerName, nameof(headerName), "Requires a non-blank HTTP correlation header name to set the HTTP correlation header in the HTTP request");
Guard.NotNullOrWhitespace(headerValue, nameof(headerValue), "Requires a non-blank HTTP correlation header value to set the HTTP correlation header in the HTTP request");
if (response is null)
{
throw new ArgumentNullException(nameof(response), "Requires a HTTP response to set the HTTP correlation headers");
}

if (string.IsNullOrWhiteSpace(headerName))
{
throw new ArgumentException("Requires a non-blank HTTP correlation header name to set the HTTP correlation header in the HTTP request", nameof(headerName));
}

if (string.IsNullOrWhiteSpace(headerValue))
{
throw new ArgumentException("Requires a non-blank HTTP correlation header value to set the HTTP correlation header in the HTTP request", nameof(headerValue));
}

response.Headers.Add(headerName, headerValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Arcus.Observability.Correlation;
using Arcus.WebApi.Logging.Core.Correlation;
using GuardNet;
using Microsoft.Azure.Functions.Worker;

namespace Arcus.WebApi.Logging.AzureFunctions.Correlation
Expand All @@ -20,8 +19,7 @@ public class AzureFunctionsHttpCorrelationInfoAccessor : IHttpCorrelationInfoAcc
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="contextAccessor"/> is <c>null</c>.</exception>
public AzureFunctionsHttpCorrelationInfoAccessor(IFunctionContextAccessor contextAccessor)
{
Guard.NotNull(contextAccessor, nameof(contextAccessor), "Requires a function context accessor instance to get/set the correlation information in the function context");
_contextAccessor = contextAccessor;
_contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor), "Requires a function context accessor instance to get/set the correlation information in the function context");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Threading.Tasks;
using Arcus.Observability.Correlation;
using Arcus.WebApi.Logging.Core.Correlation;
using GuardNet;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
Expand Down Expand Up @@ -33,9 +32,20 @@ public AzureFunctionsInProcessHttpCorrelation(
IHttpCorrelationInfoAccessor correlationInfoAccessor,
ILogger<AzureFunctionsInProcessHttpCorrelation> logger)
{
Guard.NotNull(options, nameof(options), "Requires a set of HTTP correlation options to determine where the correlation information should be added to the HTTP response headers");
Guard.NotNull(correlationInfoAccessor, nameof(correlationInfoAccessor), "Requires a HTTP correlation accessor to retrieve the current correlation information");
Guard.NotNull(logger, nameof(logger), "Requires a logging instance to write diagnostic trace messages while adding the correlation information to the HTTP response headers");
if (options is null)
{
throw new ArgumentNullException(nameof(options), "Requires a set of HTTP correlation options to determine where the correlation information should be added to the HTTP response headers");
}

if (correlationInfoAccessor is null)
{
throw new ArgumentNullException(nameof(correlationInfoAccessor), "Requires a HTTP correlation accessor to retrieve the current correlation information");
}

if (logger is null)
{
throw new ArgumentNullException(nameof(logger), "Requires a logging instance to write diagnostic trace messages while adding the correlation information to the HTTP response headers");
}

_options = options;
_correlationInfoAccessor = correlationInfoAccessor;
Expand All @@ -57,8 +67,15 @@ public CorrelationInfo GetCorrelationInfo()
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="httpContext"/> is <c>null</c> or does not have a response present.</exception>
public void AddCorrelationResponseHeaders(HttpContext httpContext)
{
Guard.NotNull(httpContext, nameof(httpContext), "Requires a HTTP context to add the correlation information to the response headers");
Guard.NotNull(httpContext.Response, nameof(httpContext), "Requires a HTTP response in the HTTP context to add the correlation information to the response headers");
if (httpContext is null)
{
throw new ArgumentNullException(nameof(httpContext), "Requires a HTTP context to add the correlation information to the response headers");
}

if (httpContext.Response is null)
{
throw new ArgumentNullException(nameof(httpContext), "Requires a HTTP response in the HTTP context to add the correlation information to the response headers");
}

if (_options.Operation.IncludeInResponse)
{
Expand Down
Loading
Loading