feat(audit): stamp CorrelationId on dashboard API-key admin audit events (mxaccessgw#213)
ci / nightly-windev (push) Has been skipped
ci / windows-x86 (push) Successful in 1m29s
ci / java (push) Successful in 2m13s
ci / portable (push) Failing after 4m12s

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:
Joseph Doherty
2026-11-09 15:04:03 -05:00
parent fccc21ba8e
commit 259d375022
2 changed files with 117 additions and 4 deletions
@@ -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()