feat(audit): stamp CorrelationId on dashboard API-key admin audit events (mxaccessgw#213)
CanonicalForwardingApiKeyAuditStore now derives CorrelationId from the ambient request: Activity.Current trace id, else the HTTP request TraceIdentifier (GUID used verbatim, otherwise hashed to a stable GUID) via an optional IHttpContextAccessor. The CLI path has neither and stays null. Auth library unchanged (option b). Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using ZB.MOM.WW.Audit;
|
||||
using ZB.MOM.WW.Auth.Abstractions.ApiKeys;
|
||||
@@ -266,11 +268,86 @@ public sealed class CanonicalAuditStoreAndAdapterTests : IDisposable
|
||||
return new SqliteCanonicalAuditStore(new AuthSqliteConnectionFactory(directory.DatabasePath()));
|
||||
}
|
||||
|
||||
private (CanonicalForwardingApiKeyAuditStore Adapter, SqliteCanonicalAuditStore Store) CreateAdapter()
|
||||
private (CanonicalForwardingApiKeyAuditStore Adapter, SqliteCanonicalAuditStore Store) CreateAdapter(
|
||||
IHttpContextAccessor? httpContextAccessor = null)
|
||||
{
|
||||
SqliteCanonicalAuditStore store = CreateStore();
|
||||
CanonicalAuditWriter writer = new(store, NullLogger<CanonicalAuditWriter>.Instance);
|
||||
return (new CanonicalForwardingApiKeyAuditStore(writer, store), store);
|
||||
return (new CanonicalForwardingApiKeyAuditStore(writer, store, httpContextAccessor), store);
|
||||
}
|
||||
|
||||
private static ApiKeyAuditEntry KeyedEntry() => new(
|
||||
KeyId: "operator01",
|
||||
EventType: "create-key",
|
||||
RemoteAddress: "10.0.0.5",
|
||||
CreatedUtc: DateTimeOffset.UtcNow,
|
||||
Details: "created");
|
||||
|
||||
/// <summary>The dashboard path (HTTP request, no activity) stamps a correlation id from TraceIdentifier (#213).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Adapter_DashboardRequest_StampsCorrelationIdFromTraceIdentifier()
|
||||
{
|
||||
DefaultHttpContext httpContext = new() { TraceIdentifier = "0HN8EXAMPLE:00000001" };
|
||||
(CanonicalForwardingApiKeyAuditStore adapter, SqliteCanonicalAuditStore store) =
|
||||
CreateAdapter(new HttpContextAccessor { HttpContext = httpContext });
|
||||
|
||||
await adapter.AppendAsync(KeyedEntry(), CancellationToken.None);
|
||||
await adapter.AppendAsync(KeyedEntry(), CancellationToken.None);
|
||||
|
||||
IReadOnlyList<AuditEvent> events = await store.ListRecentAsync(10, CancellationToken.None);
|
||||
Assert.Equal(2, events.Count);
|
||||
Assert.All(events, e => Assert.NotNull(e.CorrelationId));
|
||||
// Stable per request: both events of the same request share the id.
|
||||
Assert.Equal(events[0].CorrelationId, events[1].CorrelationId);
|
||||
}
|
||||
|
||||
/// <summary>A GUID-shaped TraceIdentifier is used verbatim.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Adapter_GuidTraceIdentifier_IsUsedAsCorrelationId()
|
||||
{
|
||||
Guid trace = Guid.NewGuid();
|
||||
DefaultHttpContext httpContext = new() { TraceIdentifier = trace.ToString() };
|
||||
(CanonicalForwardingApiKeyAuditStore adapter, SqliteCanonicalAuditStore store) =
|
||||
CreateAdapter(new HttpContextAccessor { HttpContext = httpContext });
|
||||
|
||||
await adapter.AppendAsync(KeyedEntry(), CancellationToken.None);
|
||||
|
||||
Assert.Equal(trace, Assert.Single(await store.ListRecentAsync(10, CancellationToken.None)).CorrelationId);
|
||||
}
|
||||
|
||||
/// <summary>An ambient activity's trace id wins over the HTTP trace identifier.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Adapter_AmbientActivity_StampsTraceIdAsCorrelationId()
|
||||
{
|
||||
using Activity activity = new("test-request");
|
||||
activity.Start();
|
||||
DefaultHttpContext httpContext = new() { TraceIdentifier = "ignored" };
|
||||
(CanonicalForwardingApiKeyAuditStore adapter, SqliteCanonicalAuditStore store) =
|
||||
CreateAdapter(new HttpContextAccessor { HttpContext = httpContext });
|
||||
|
||||
await adapter.AppendAsync(KeyedEntry(), CancellationToken.None);
|
||||
|
||||
Guid expected = Guid.ParseExact(activity.TraceId.ToHexString(), "N");
|
||||
Assert.Equal(expected, Assert.Single(await store.ListRecentAsync(10, CancellationToken.None)).CorrelationId);
|
||||
}
|
||||
|
||||
/// <summary>The CLI path (no accessor, no activity, no request) leaves the correlation id null (#213).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Adapter_CliPath_LeavesCorrelationIdNull()
|
||||
{
|
||||
(CanonicalForwardingApiKeyAuditStore adapter, SqliteCanonicalAuditStore store) = CreateAdapter();
|
||||
(CanonicalForwardingApiKeyAuditStore noRequest, _) =
|
||||
CreateAdapter(new HttpContextAccessor { HttpContext = null });
|
||||
|
||||
Assert.Null(Activity.Current);
|
||||
await adapter.AppendAsync(KeyedEntry(), CancellationToken.None);
|
||||
await noRequest.AppendAsync(KeyedEntry(), CancellationToken.None);
|
||||
|
||||
Assert.Null(Assert.Single(await store.ListRecentAsync(10, CancellationToken.None)).CorrelationId);
|
||||
}
|
||||
|
||||
private static AuditEvent MakeEvent(string action) => new()
|
||||
|
||||
Reference in New Issue
Block a user