1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-27 13:05:23 +01:00

[PM-13098] Use ILogger.BeginScope For Better Scopes (#4740)

* Use `ILogger.BeginScope` For Better Scopes

* Format

* Remove Behind Feature Flag
This commit is contained in:
Justin Baur 2024-10-03 08:30:02 -04:00 committed by GitHub
parent f3f81deb98
commit b196c8bfb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 119 additions and 27 deletions

View File

@ -143,6 +143,7 @@ public static class FeatureFlagKeys
public const string StorageReseedRefactor = "storage-reseed-refactor";
public const string TrialPayment = "PM-8163-trial-payment";
public const string Pm3478RefactorOrganizationUserApi = "pm-3478-refactor-organizationuser-api";
public const string RemoveServerVersionHeader = "remove-server-version-header";
public static List<string> GetAllKeys()
{

View File

@ -0,0 +1,117 @@
using System.Collections;
using Bit.Core;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
#nullable enable
namespace Bit.SharedWeb.Utilities;
public sealed class RequestLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<RequestLoggingMiddleware> _logger;
private readonly GlobalSettings _globalSettings;
public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger, GlobalSettings globalSettings)
{
_next = next;
_logger = logger;
_globalSettings = globalSettings;
}
public Task Invoke(HttpContext context, IFeatureService featureService)
{
if (!featureService.IsEnabled(FeatureFlagKeys.RemoveServerVersionHeader))
{
context.Response.OnStarting(() =>
{
context.Response.Headers.Append("Server-Version", AssemblyHelpers.GetVersion());
return Task.CompletedTask;
});
}
using (_logger.BeginScope(
new RequestLogScope(context.GetIpAddress(_globalSettings),
GetHeaderValue(context, "user-agent"),
GetHeaderValue(context, "device-type"),
GetHeaderValue(context, "device-type"))))
{
return _next(context);
}
static string? GetHeaderValue(HttpContext httpContext, string header)
{
if (httpContext.Request.Headers.TryGetValue(header, out var value))
{
return value;
}
return null;
}
}
private sealed class RequestLogScope : IReadOnlyList<KeyValuePair<string, object?>>
{
private string? _cachedToString;
public RequestLogScope(string? ipAddress, string? userAgent, string? deviceType, string? origin)
{
IpAddress = ipAddress;
UserAgent = userAgent;
DeviceType = deviceType;
Origin = origin;
}
public KeyValuePair<string, object?> this[int index]
{
get
{
if (index == 0)
{
return new KeyValuePair<string, object?>(nameof(IpAddress), IpAddress);
}
else if (index == 1)
{
return new KeyValuePair<string, object?>(nameof(UserAgent), UserAgent);
}
else if (index == 2)
{
return new KeyValuePair<string, object?>(nameof(DeviceType), DeviceType);
}
else if (index == 3)
{
return new KeyValuePair<string, object?>(nameof(Origin), Origin);
}
throw new ArgumentOutOfRangeException(nameof(index));
}
}
public int Count => 4;
public string? IpAddress { get; }
public string? UserAgent { get; }
public string? DeviceType { get; }
public string? Origin { get; }
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
{
for (var i = 0; i < Count; i++)
{
yield return this[i];
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public override string ToString()
{
_cachedToString ??= $"IpAddress:{IpAddress} UserAgent:{UserAgent} DeviceType:{DeviceType} Origin:{Origin}";
return _cachedToString;
}
}
}

View File

@ -48,7 +48,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Localization;
@ -60,7 +59,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog.Context;
using StackExchange.Redis;
using NoopRepos = Bit.Core.Repositories.Noop;
using Role = Bit.Core.Entities.Role;
@ -540,31 +538,7 @@ public static class ServiceCollectionExtensions
public static void UseDefaultMiddleware(this IApplicationBuilder app,
IWebHostEnvironment env, GlobalSettings globalSettings)
{
string GetHeaderValue(HttpContext httpContext, string header)
{
if (httpContext.Request.Headers.ContainsKey(header))
{
return httpContext.Request.Headers[header];
}
return null;
}
// Add version information to response headers
app.Use(async (httpContext, next) =>
{
using (LogContext.PushProperty("IPAddress", httpContext.GetIpAddress(globalSettings)))
using (LogContext.PushProperty("UserAgent", GetHeaderValue(httpContext, "user-agent")))
using (LogContext.PushProperty("DeviceType", GetHeaderValue(httpContext, "device-type")))
using (LogContext.PushProperty("Origin", GetHeaderValue(httpContext, "origin")))
{
httpContext.Response.OnStarting((state) =>
{
httpContext.Response.Headers.Append("Server-Version", AssemblyHelpers.GetVersion());
return Task.FromResult(0);
}, null);
await next.Invoke();
}
});
app.UseMiddleware<RequestLoggingMiddleware>();
}
public static void UseForwardedHeaders(this IApplicationBuilder app, IGlobalSettings globalSettings)