Skip to content

Commit

Permalink
refactor: Removing all dependency to Guard.NET. (#465)
Browse files Browse the repository at this point in the history
* wip

* wip logging core

* Finish Arcus.WebApi.Logging.Core

* Finish Arcus.WebApi.OpenApi.Extensions

* remove guard.net

* remove guard.net package from logging.core

* remove guard.net from webapi.logging

* remove guard.net package from webapi.security

* wip webapi.security

* remove guard.net from source code

* remove guard.net from test code

* fix compilation error

* fix compilation errors

* use StartsWith(char) instead of StarsWith(string)

* Correct Hosting projects

* Fix comments on .Logging

* apply comments to Logging.AzureFunctions

* Fixed PR comments in Logging.Core

* Fix comments on OpenApi.Extensions

* Fix comments .Security

* Use char check instead of string check for single character

* small polish

* add whitelines

* Revert oneliner

* Adapt to PR comments

* fix unit tests

* Update src/Arcus.WebApi.Logging.Core/Correlation/HttpCorrelationClientOptions.cs

Co-authored-by: Stijn Moreels <[email protected]>

* Update src/Arcus.WebApi.Logging.Core/Correlation/HttpCorrelationClientOptions.cs

Co-authored-by: Stijn Moreels <[email protected]>

* Update src/Arcus.WebApi.OpenApi.Extensions/OAuthAuthorizeOperationFilter.cs

Co-authored-by: Stijn Moreels <[email protected]>

* Update src/Arcus.WebApi.OpenApi.Extensions/SharedAccessKeyAuthenticationOperationFilter.cs

Co-authored-by: Stijn Moreels <[email protected]>

* Update src/Arcus.WebApi.OpenApi.Extensions/SharedAccessKeyAuthenticationOperationFilter.cs

Co-authored-by: Stijn Moreels <[email protected]>

---------

Co-authored-by: Joachim Goris <[email protected]>
Co-authored-by: Frederik Gheysels <[email protected]>
Co-authored-by: Stijn Moreels <[email protected]>
  • Loading branch information
4 people authored Jan 16, 2025
1 parent c8214f0 commit 7062a7b
Show file tree
Hide file tree
Showing 77 changed files with 1,022 additions and 514 deletions.
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

0 comments on commit 7062a7b

Please sign in to comment.