mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:42:50 +08:00
Change logging for various middleware
As part #35 logging is being checked. This commit changes the first four middlewares within the pipeline to be more standardised. Also added an extension method to easily print out the errors from a list of errors.
This commit is contained in:
parent
0ad41aa3fa
commit
9774580c4e
@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Ocelot.Authentication.Handler.Factory;
|
using Ocelot.Authentication.Handler.Factory;
|
||||||
using Ocelot.Configuration;
|
using Ocelot.Configuration;
|
||||||
using Ocelot.Errors;
|
using Ocelot.Errors;
|
||||||
|
using Ocelot.Infrastructure.Extensions;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.Middleware;
|
using Ocelot.Middleware;
|
||||||
@ -33,47 +34,57 @@ namespace Ocelot.Authentication.Middleware
|
|||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("started authentication");
|
_logger.LogTrace($"entered {MiddlwareName}");
|
||||||
|
|
||||||
if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
|
if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
|
||||||
{
|
{
|
||||||
|
_logger.LogDebug($"{context.Request.Path} is an authenticated route. {MiddlwareName} checking if client is authenticated");
|
||||||
|
|
||||||
var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);
|
var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);
|
||||||
|
|
||||||
if (!authenticationHandler.IsError)
|
if (authenticationHandler.IsError)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("calling authentication handler for ReRoute");
|
_logger.LogError($"Error getting authentication handler for {context.Request.Path}. {authenticationHandler.Errors.ToErrorString()}");
|
||||||
|
SetPipelineError(authenticationHandler.Errors);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await authenticationHandler.Data.Handler.Handle(context);
|
await authenticationHandler.Data.Handler.Handle(context);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.LogDebug("there was an error getting authentication handler for ReRoute");
|
|
||||||
|
|
||||||
SetPipelineError(authenticationHandler.Errors);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context.User.Identity.IsAuthenticated)
|
if (context.User.Identity.IsAuthenticated)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("the user was authenticated");
|
_logger.LogDebug($"Client has been authenticated for {context.Request.Path}");
|
||||||
|
_logger.LogTrace($"{MiddlwareName} invoking next middleware");
|
||||||
|
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
|
|
||||||
_logger.LogDebug("succesfully called next middleware");
|
_logger.LogTrace($"returned to {MiddlwareName} after next middleware completed");
|
||||||
|
_logger.LogTrace($"completed {MiddlwareName}");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.LogDebug("the user was not authenticated");
|
var error = new List<Error>
|
||||||
|
{
|
||||||
|
new UnauthenticatedError(
|
||||||
|
$"Request for authenticated route {context.Request.Path} by {context.User.Identity.Name} was unauthenticated")
|
||||||
|
};
|
||||||
|
|
||||||
SetPipelineError(new List<Error> { new UnauthenticatedError($"Request for authenticated route {context.Request.Path} by {context.User.Identity.Name} was unauthenticated") });
|
_logger.LogError($"Client has NOT been authenticated for {context.Request.Path} and pipeline error set. {error.ToErrorString()}");
|
||||||
|
SetPipelineError(error);
|
||||||
|
|
||||||
|
_logger.LogTrace($"completed {MiddlwareName}");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.LogDebug("calling next middleware");
|
_logger.LogTrace($"No authentication needed for {context.Request.Path}. Invoking next middleware from {MiddlwareName}");
|
||||||
|
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
|
|
||||||
_logger.LogDebug("succesfully called next middleware");
|
_logger.LogTrace($"returned to {MiddlwareName} after next middleware completed");
|
||||||
|
_logger.LogTrace($"completed {MiddlwareName}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,3 +94,4 @@ namespace Ocelot.Authentication.Middleware
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Ocelot.DownstreamRouteFinder.Finder;
|
using Ocelot.DownstreamRouteFinder.Finder;
|
||||||
|
using Ocelot.Infrastructure.Extensions;
|
||||||
using Ocelot.Infrastructure.RequestData;
|
using Ocelot.Infrastructure.RequestData;
|
||||||
using Ocelot.Logging;
|
using Ocelot.Logging;
|
||||||
using Ocelot.Middleware;
|
using Ocelot.Middleware;
|
||||||
@ -27,7 +30,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
|
|||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("started calling downstream route finder middleware");
|
_logger.LogTrace($"entered {MiddlwareName}");
|
||||||
|
|
||||||
var upstreamUrlPath = context.Request.Path.ToString().SetLastCharacterAs('/');
|
var upstreamUrlPath = context.Request.Path.ToString().SetLastCharacterAs('/');
|
||||||
|
|
||||||
@ -37,7 +40,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
|
|||||||
|
|
||||||
if (downstreamRoute.IsError)
|
if (downstreamRoute.IsError)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("IDownstreamRouteFinder returned an error, setting pipeline error");
|
_logger.LogError($"{MiddlwareName} setting pipeline errors. IDownstreamRouteFinder returned {downstreamRoute.Errors.ToErrorString()}");
|
||||||
|
|
||||||
SetPipelineError(downstreamRoute.Errors);
|
SetPipelineError(downstreamRoute.Errors);
|
||||||
return;
|
return;
|
||||||
@ -47,12 +50,12 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
|
|||||||
|
|
||||||
SetDownstreamRouteForThisRequest(downstreamRoute.Data);
|
SetDownstreamRouteForThisRequest(downstreamRoute.Data);
|
||||||
|
|
||||||
_logger.LogDebug("calling next middleware");
|
_logger.LogTrace($"invoking next middleware from {MiddlwareName}");
|
||||||
|
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
|
|
||||||
_logger.LogDebug("succesfully called next middleware");
|
_logger.LogTrace($"returned to {MiddlwareName} after next middleware completed");
|
||||||
|
_logger.LogTrace($"completed {MiddlwareName}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
18
src/Ocelot/Infrastructure/Extensions/ErrorListExtensions.cs
Normal file
18
src/Ocelot/Infrastructure/Extensions/ErrorListExtensions.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using Ocelot.Errors;
|
||||||
|
|
||||||
|
namespace Ocelot.Infrastructure.Extensions
|
||||||
|
{
|
||||||
|
public static class ErrorListExtensions
|
||||||
|
{
|
||||||
|
public static string ToErrorString(this List<Error> errors)
|
||||||
|
{
|
||||||
|
var listOfErrorStrings = errors.Select(x => "Error Code: " + x.Code.ToString() + " Message: " + x.Message);
|
||||||
|
return string.Join(" ", listOfErrorStrings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -31,11 +31,13 @@ namespace Ocelot.RateLimit.Middleware
|
|||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("started calling RateLimit middleware");
|
_logger.LogTrace($"entered {MiddlwareName}");
|
||||||
|
|
||||||
var options = DownstreamRoute.ReRoute.RateLimitOptions;
|
var options = DownstreamRoute.ReRoute.RateLimitOptions;
|
||||||
// check if rate limiting is enabled
|
// check if rate limiting is enabled
|
||||||
if (!DownstreamRoute.ReRoute.EnableEndpointRateLimiting)
|
if (!DownstreamRoute.ReRoute.EnableEndpointRateLimiting)
|
||||||
{
|
{
|
||||||
|
_logger.LogDebug($"EndpointRateLimiting is not enabled for {DownstreamRoute.ReRoute.DownstreamPathTemplate}. Invoking next middleware from {MiddlwareName}.");
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -45,6 +47,7 @@ namespace Ocelot.RateLimit.Middleware
|
|||||||
// check white list
|
// check white list
|
||||||
if (IsWhitelisted(identity, options))
|
if (IsWhitelisted(identity, options))
|
||||||
{
|
{
|
||||||
|
_logger.LogDebug($"{DownstreamRoute.ReRoute.DownstreamPathTemplate} is white listed from rate limiting. Invoking next middleware from {MiddlwareName}.");
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -76,7 +79,12 @@ namespace Ocelot.RateLimit.Middleware
|
|||||||
context.Response.OnStarting(SetRateLimitHeaders, state: headers);
|
context.Response.OnStarting(SetRateLimitHeaders, state: headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogTrace($"invoking next middleware from {MiddlwareName}");
|
||||||
|
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
|
|
||||||
|
_logger.LogTrace($"returned to {MiddlwareName} after next middleware completed");
|
||||||
|
_logger.LogTrace($"completed {MiddlwareName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual ClientRequestIdentity SetIdentity(HttpContext httpContext, RateLimitOptions option)
|
public virtual ClientRequestIdentity SetIdentity(HttpContext httpContext, RateLimitOptions option)
|
||||||
|
@ -26,17 +26,18 @@ namespace Ocelot.RequestId.Middleware
|
|||||||
|
|
||||||
public async Task Invoke(HttpContext context)
|
public async Task Invoke(HttpContext context)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("started calling request id middleware");
|
_logger.LogTrace($"entered {MiddlwareName}");
|
||||||
|
|
||||||
SetOcelotRequestId(context);
|
SetOcelotRequestId(context);
|
||||||
|
|
||||||
_logger.LogDebug("set request id");
|
_logger.LogDebug("set requestId");
|
||||||
|
|
||||||
_logger.LogDebug("calling next middleware");
|
_logger.LogTrace($"invoking next middleware from {MiddlwareName}");
|
||||||
|
|
||||||
await _next.Invoke(context);
|
await _next.Invoke(context);
|
||||||
|
|
||||||
_logger.LogDebug("succesfully called next middleware");
|
_logger.LogTrace($"returned to {MiddlwareName} after next middleware completed");
|
||||||
|
_logger.LogTrace($"completed {MiddlwareName}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetOcelotRequestId(HttpContext context)
|
private void SetOcelotRequestId(HttpContext context)
|
||||||
|
@ -27,6 +27,11 @@ namespace Ocelot.Responder
|
|||||||
return new OkResponse<int>(503);
|
return new OkResponse<int>(503);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
|
||||||
|
{
|
||||||
|
return new OkResponse<int>(404);
|
||||||
|
}
|
||||||
|
|
||||||
return new OkResponse<int>(404);
|
return new OkResponse<int>(404);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user