635461c0fd
Perf re-baseline (HotPathLatencyTests): empirical p95 on Apple M-series Release build: 4KB DetailsJson slow path ≈14 µs, small-DetailsJson no-redactors ≈2 µs, true no-op fast path ≈0 µs. Thresholds updated: 200 µs / 30 µs / 5 µs (≈15× headroom for contested CI runners). Old thresholds (50 µs / 10 µs) were set for the pre-C3 typed-field path; canonical JSON parse+rewrite is empirically faster. Adds a third test (Filter_Apply_NoDetailsJson_FastPath) that asserts same-instance return on the DetailsJson-null + within-cap fast path. Env-var overrides retained. CollapseAuditLogToCanonicalMigrationTests (new): three MSSQL-gated [SkippableFact] tests verifying Action/Category/Outcome projection, NULL Actor, DetailsJson codec round-trip, and all six persisted computed columns (Kind/Status/SourceSiteId/ ExecutionId/ParentExecutionId) for ApiOutbound, InboundAuthFailure, and Failed- status rows. AddAuditLogTableMigrationTests: rename CreatesFiveNamedIndexes → CreatesNineNamedIndexes; expand coverage from 5 original indexes to all 9 named non-clustered indexes present after CollapseAuditLogToCanonical (adds IX_AuditLog_Execution, IX_AuditLog_ParentExecution, IX_AuditLog_Node_Occurred, UX_AuditLog_EventId). Dead-cref cleanup: zero references to the deleted IAuditPayloadFilter / DefaultAuditPayloadFilter / SafeDefaultAuditPayloadFilter types remain in any .cs file (source or test). 26 occurrences across 13 files replaced with correct references to IAuditRedactor / ScadaBridgeAuditRedactor / SafeDefaultAuditRedactor or reworded as plain prose. Residual sweep: no unused transitional code found beyond the acknowledged "C3 transitional shim" comments on IngestedAtUtc stamping (active code, not dead).
123 lines
4.0 KiB
C#
123 lines
4.0 KiB
C#
using ZB.MOM.WW.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.AuditLog.Redaction;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Redaction;
|
|
|
|
/// <summary>
|
|
/// ScadaBridge audit re-architecture stage C2 (Task 2.5) tests for
|
|
/// <see cref="SafeDefaultAuditRedactor"/> — the minimal always-safe
|
|
/// <see cref="ZB.MOM.WW.Audit.IAuditRedactor"/> fallback.
|
|
/// Header-only scrub of the always-sensitive default headers inside
|
|
/// <c>DetailsJson</c>'s RequestSummary / ResponseSummary; never throws, never
|
|
/// performs body / SQL / truncation work.
|
|
/// </summary>
|
|
public class SafeDefaultAuditRedactorTests
|
|
{
|
|
private static AuditEvent EventWith(string? request = null, string? response = null)
|
|
{
|
|
var details = new AuditDetails
|
|
{
|
|
RequestSummary = request,
|
|
ResponseSummary = response,
|
|
};
|
|
return new AuditEvent
|
|
{
|
|
EventId = Guid.NewGuid(),
|
|
OccurredAtUtc = DateTimeOffset.UtcNow,
|
|
Actor = "tester",
|
|
Action = "ApiOutbound.ApiCall",
|
|
Outcome = AuditOutcome.Success,
|
|
DetailsJson = AuditDetailsCodec.Serialize(details),
|
|
};
|
|
}
|
|
|
|
private static AuditDetails Details(AuditEvent evt) =>
|
|
AuditDetailsCodec.Deserialize(evt.DetailsJson);
|
|
|
|
[Fact]
|
|
public void Redacts_DefaultSensitiveHeaders_InRequestSummary()
|
|
{
|
|
var evt = EventWith(request: "Authorization: Bearer secret-token\nContent-Type: application/json");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
var d = Details(result);
|
|
Assert.Contains("Authorization: <redacted>", d.RequestSummary!);
|
|
Assert.DoesNotContain("secret-token", d.RequestSummary!);
|
|
Assert.Contains("Content-Type: application/json", d.RequestSummary!);
|
|
}
|
|
|
|
[Fact]
|
|
public void Redacts_DefaultSensitiveHeaders_InResponseSummary()
|
|
{
|
|
var evt = EventWith(response: "Set-Cookie: sessionid=abc123\nX-Other: ok");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
var d = Details(result);
|
|
Assert.Contains("Set-Cookie: <redacted>", d.ResponseSummary!);
|
|
Assert.DoesNotContain("abc123", d.ResponseSummary!);
|
|
Assert.Contains("X-Other: ok", d.ResponseSummary!);
|
|
}
|
|
|
|
[Fact]
|
|
public void CaseInsensitive_HeaderName_Redacted()
|
|
{
|
|
var evt = EventWith(request: "authorization: Bearer x-y-z");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
Assert.Contains("<redacted>", Details(result).RequestSummary!);
|
|
Assert.DoesNotContain("x-y-z", Details(result).RequestSummary!);
|
|
}
|
|
|
|
[Fact]
|
|
public void NonSensitiveHeader_Preserved()
|
|
{
|
|
var evt = EventWith(request: "X-Request-Id: abc-123\nAccept: application/json");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
var d = Details(result);
|
|
Assert.Contains("X-Request-Id: abc-123", d.RequestSummary!);
|
|
Assert.Contains("Accept: application/json", d.RequestSummary!);
|
|
}
|
|
|
|
[Fact]
|
|
public void NullDetails_FastPath_ReturnsSameInstance()
|
|
{
|
|
var evt = new AuditEvent
|
|
{
|
|
EventId = Guid.NewGuid(),
|
|
OccurredAtUtc = DateTimeOffset.UtcNow,
|
|
Actor = "tester",
|
|
Action = "ApiOutbound.ApiCall",
|
|
Outcome = AuditOutcome.Success,
|
|
DetailsJson = null,
|
|
};
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
Assert.Same(evt, result);
|
|
}
|
|
|
|
[Fact]
|
|
public void MalformedDetailsJson_NeverThrows()
|
|
{
|
|
var evt = new AuditEvent
|
|
{
|
|
EventId = Guid.NewGuid(),
|
|
OccurredAtUtc = DateTimeOffset.UtcNow,
|
|
Actor = "tester",
|
|
Action = "ApiOutbound.ApiCall",
|
|
Outcome = AuditOutcome.Success,
|
|
DetailsJson = "{not valid json]",
|
|
};
|
|
|
|
var ex = Record.Exception(() => SafeDefaultAuditRedactor.Instance.Apply(evt));
|
|
|
|
Assert.Null(ex);
|
|
}
|
|
}
|