fix(inbound-api): RouteCall audit row honours the method's SkipBodyCapture override (arch-review F11)

RouteHelper/RouteTarget carry a new captureBodies flag (default true) threaded
through every With* copy, same shape as the site-scope field Task 3 added.
InboundScriptExecutor resolves IOptionsMonitor<AuditLogOptions> from the
per-execution DI scope and looks up PerTargetOverrides[method.Name]?.SkipBodyCapture
exactly as AuditWriteMiddleware.cs:183-187 does for the inbound row, then calls
.WithBodyCapture(!skip) when building the script's Route helper. When set, the
always-on ScriptTrace.RouteCall row omits its request/response summary, so an
operator who suppressed a method's body from the audit log doesn't get it back
via the routed call's arguments (derived from that same body).

Claude-Session: https://claude.ai/code/session_01NcMG6xXdsJbmD2wYAgkK29
This commit is contained in:
Joseph Doherty
2026-09-06 14:22:01 -04:00
parent 3f1981865a
commit 453c3dec18
4 changed files with 203 additions and 13 deletions
@@ -1,7 +1,9 @@
using NSubstitute;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.InboundApi;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
namespace ZB.MOM.WW.ScadaBridge.InboundAPI.Tests;
@@ -703,4 +705,81 @@ public class RouteHelperTests
await Assert.ThrowsAsync<RouteSiteScopeException>(
() => helper.To("inst-1").Call("doWork"));
}
// --- Arch-review F11: WithBodyCapture ---
[Fact]
public async Task RouteCallRow_WithBodyCaptureOff_HasNoRequestOrResponseSummary()
{
SiteResolves("inst-1", "SiteA");
_router.RouteToCallAsync("SiteA", Arg.Any<RouteToCallRequest>(), Arg.Any<CancellationToken>())
.Returns(ci => new RouteToCallResponse(
((RouteToCallRequest)ci[1]).CorrelationId, true, 99, null, DateTimeOffset.UtcNow));
var writer = Substitute.For<ICentralAuditWriter>();
AuditEvent? written = null;
writer.WriteAsync(Arg.Do<AuditEvent>(e => written = e), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
var helper = new RouteHelper(_locator, _router, writer, null).WithBodyCapture(false);
var result = await helper.To("inst-1").Call("doWork", new { x = 1 });
Assert.Equal(99, result);
Assert.NotNull(written);
var row = written!.AsRow();
Assert.Null(row.RequestSummary);
Assert.Null(row.ResponseSummary);
}
[Fact]
public async Task RouteCallRow_WithBodyCaptureOn_ByDefault_HasRequestAndResponseSummary()
{
SiteResolves("inst-1", "SiteA");
_router.RouteToCallAsync("SiteA", Arg.Any<RouteToCallRequest>(), Arg.Any<CancellationToken>())
.Returns(ci => new RouteToCallResponse(
((RouteToCallRequest)ci[1]).CorrelationId, true, 99, null, DateTimeOffset.UtcNow));
var writer = Substitute.For<ICentralAuditWriter>();
AuditEvent? written = null;
writer.WriteAsync(Arg.Do<AuditEvent>(e => written = e), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
var helper = new RouteHelper(_locator, _router, writer, null);
await helper.To("inst-1").Call("doWork", new { x = 1 });
Assert.NotNull(written);
var row = written!.AsRow();
Assert.Contains("\"x\":1", row.RequestSummary);
Assert.Equal("99", row.ResponseSummary);
}
[Fact]
public async Task WithBodyCapture_SurvivesTheOtherWithCopies()
{
// Mirrors WithSiteScope_SurvivesTheOtherWithCopies (Task 3): the flag must not be
// dropped by the copies InboundScriptExecutor makes (WithDeadline /
// WithRequestAborted / WithParentExecutionId / WithTrace / WithSiteScope).
SiteResolves("inst-1", "SiteA");
_router.RouteToCallAsync("SiteA", Arg.Any<RouteToCallRequest>(), Arg.Any<CancellationToken>())
.Returns(ci => new RouteToCallResponse(
((RouteToCallRequest)ci[1]).CorrelationId, true, 99, null, DateTimeOffset.UtcNow));
var writer = Substitute.For<ICentralAuditWriter>();
AuditEvent? written = null;
writer.WriteAsync(Arg.Do<AuditEvent>(e => written = e), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
var helper = new RouteHelper(_locator, _router, writer, null)
.WithBodyCapture(false)
.WithDeadline(CancellationToken.None)
.WithRequestAborted(CancellationToken.None)
.WithParentExecutionId(Guid.NewGuid())
.WithTrace(true)
.WithTraceSink(null)
.WithSiteScope(Scope("SiteA"));
await helper.To("inst-1").Call("doWork", new { x = 1 });
Assert.NotNull(written);
var row = written!.AsRow();
Assert.Null(row.RequestSummary);
Assert.Null(row.ResponseSummary);
}
}