feat(audit)!: remove the AuditLog hash chain, head export and two-login maintenance hardening

Owner decision 2026-11-15, at IT security's request, pending a family-wide general solution.

Removed: the chaining service, hasher, verifier, chain head/tombstone entities and store, the #139
chain-start cutoff, AuditLog:HashChain (incl. ChainBacklog and HeadExport with its File/Syslog sinks
and export service), the audit-chain-head-export health check, the VerifyAuditChain /
VerifyAuditChainHeads management commands and the `audit verify-chain` / `audit verify-heads` CLI
commands, and the two-login plumbing (AuditLog:MaintenanceConnectionString, AuditMaintenanceContext,
the keyed audit-maintenance repository/archive). Purge, archive, retention, partition roll-forward
and backfill-source-node all run on the runtime connection again, as before #107.

Parquet archive (#42) kept, chain-free: format v2 (ten stored columns, no tombstone file, manifest
without chain state), the switch guard is the row count only (51003 gone), verify = data-file size,
SHA-256 and row count, and superseded-stale cleanup proves coverage by EventId. Format-v1 archives
still list, verify and are cleaned up (their tombstone file too).

Migration RemoveAuditLogHashChain drops the chain indexes, the scadabridge_audit_chainer role
(members first), both chain tables, ChainSeq/RowHash and the purger's AuditMaintenanceGrants grants;
writer/purger roles stay. Down restores them. Every chain-column reference is EXEC-deferred for the
idempotent script (#126); FlipAuditLogClusteredKey's two chain index statements get the same
deferral, behaviour unchanged, so re-running the idempotent script on a migrated database works.
The partition-switch staging table adds the chain columns only when the live table has them, so the
new binary purges either schema. Deploy gate: old binaries map the chain columns, so the migration
ships with the new binaries, both centrals stopped.

A leftover AuditLog:HashChain section or MaintenanceConnectionString (DEV/QA still carry
AuditLog__HashChain__Enabled=true) is ignored with one startup warning (RetiredAuditSettingsWarning).

Claude-Session: https://claude.ai/code/session_01GT67AFKC45MBz1wPZnVbX8
This commit is contained in:
Joseph Doherty
2026-11-15 17:37:33 -05:00
parent 125db03232
commit ed2ed0a367
82 changed files with 3203 additions and 7603 deletions
@@ -1,14 +1,14 @@
using Parquet;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using Parquet.Schema;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// Storage-free tests for the AuditLog archive Parquet layout (ScadaBridge#42): rows and
/// tombstones round-trip exactly (tick-precise timestamps, NULL vs empty strings, unicode,
/// the chain columns), the archived rows re-verify against the archived chain head, and an
/// edit made inside the Parquet file breaks that verification.
/// Storage-free tests for the AuditLog archive Parquet layout (ScadaBridge#42): rows round-trip
/// exactly (tick-precise timestamps, NULL vs empty strings, unicode), and a format-v1 data file
/// — written while the hash chain existed, with its <c>ChainSeq</c>/<c>RowHash</c> columns —
/// still reads.
/// </summary>
public class AuditArchiveParquetTests
{
@@ -19,8 +19,7 @@ public class AuditArchiveParquetTests
[Fact]
public async Task Rows_RoundTrip_Exactly_AcrossRowGroups()
{
var (rows, _) = BuildChain(2_503);
rows.Add(Row(9_999) with { ChainSeq = null, RowHash = null }); // an unchained row
var rows = Enumerable.Range(1, 2_503).Select(Row).ToList();
var bytes = await WriteAsync(rows, rowGroupSize: 1_000);
var back = await ReadAsync(bytes);
@@ -28,100 +27,44 @@ public class AuditArchiveParquetTests
Assert.Equal(rows.Select(Key), back.Select(Key));
await using var reader = await ParquetReader.CreateAsync(new MemoryStream(bytes));
Assert.Equal(3, reader.RowGroupCount);
Assert.Equal("1", reader.CustomMetadata[AuditArchiveParquet.FormatVersionKey]);
Assert.Equal("2", reader.CustomMetadata[AuditArchiveParquet.FormatVersionKey]);
Assert.Equal("2025-10", reader.CustomMetadata[AuditArchiveParquet.PartitionKey]);
}
/// <summary>Tombstones round-trip exactly.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
/// <summary>The v2 data file carries the ten stored columns and nothing from the removed hash chain.</summary>
[Fact]
public async Task Tombstones_RoundTrip_Exactly()
public void DataSchema_HasNoChainColumns()
{
var tombstones = Enumerable.Range(1, 5).Select(i => new AuditArchiveTombstone(
Month, i * 3, Enumerable.Repeat((byte)i, 32).ToArray(), Guid.NewGuid(),
new DateTime(2026, 1, 2, 3, 4, 5, DateTimeKind.Utc).AddMilliseconds(i))).ToList();
var stream = new MemoryStream();
await using (var writer = await AuditArchiveParquet.CreateTombstoneWriterAsync(stream, "2025-10", 2))
{
foreach (var t in tombstones)
{
await writer.AddAsync(t);
}
}
var back = new List<AuditArchiveTombstone>();
await foreach (var t in AuditArchiveParquet.ReadTombstonesAsync(new MemoryStream(stream.ToArray())))
{
back.Add(t);
}
Assert.Equal(
tombstones.Select(t => (t.ChainMonth, t.ChainSeq, Convert.ToHexString(t.RowHash), t.EventId, t.PurgedAtUtc)),
back.Select(t => (t.ChainMonth, t.ChainSeq, Convert.ToHexString(t.RowHash), t.EventId, t.PurgedAtUtc)));
var names = AuditArchiveParquet.DataSchema.DataFields.Select(f => f.Name).ToList();
Assert.DoesNotContain("ChainSeq", names);
Assert.DoesNotContain("RowHash", names);
Assert.Contains("DetailsJson", names); // positive control: the schema was read
}
/// <summary>
/// The rows read back from an archive re-verify against the archived head — the same
/// check <c>audit archive verify</c> makes long after the database dropped them.
/// A format-v1 archive data file (the v2 columns plus <c>ChainSeq</c>/<c>RowHash</c>) reads
/// with the v2 reader: the extra columns are ignored and every stored column is intact.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ArchivedRows_ReverifyAgainstTheArchivedHead()
public async Task FormatV1DataFile_WithChainColumns_StillReads()
{
var (rows, head) = BuildChain(50);
var verifier = new AuditChainVerifier(Month, head, new Dictionary<long, byte[]>());
foreach (var row in await ReadAsync(await WriteAsync(rows, 16)))
var rows = Enumerable.Range(1, 40).Select(Row).ToList();
var bytes = await WriteV1Async(rows);
await using (var raw = await ParquetReader.CreateAsync(new MemoryStream(bytes)))
{
verifier.Accept(row);
// Positive control: the file really carries the v1 chain columns.
Assert.Contains(raw.Schema.DataFields, f => f.Name == "ChainSeq");
}
var result = verifier.Complete(0);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(50, result.RowsVerified);
var back = await ReadAsync(bytes);
Assert.Equal(rows.Select(Key), back.Select(Key));
}
/// <summary>
/// Positive control for the test above: a row edited inside the archive (re-written
/// file, as a forger would) fails re-verification at that row.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RowEditedInsideTheArchive_FailsReverification()
{
var (rows, head) = BuildChain(50);
rows[17] = rows[17] with { DetailsJson = "{\"kind\":\"ApiCall\",\"forged\":true}" };
var verifier = new AuditChainVerifier(Month, head, new Dictionary<long, byte[]>());
foreach (var row in await ReadAsync(await WriteAsync(rows, 16)))
{
verifier.Accept(row);
}
var result = verifier.Complete(0);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.HashMismatch, result.FirstBreak!.Kind);
Assert.Equal(18, result.FirstBreak.ChainSeq);
}
internal static (List<AuditChainRow> Rows, AuditChainHeadState Head) BuildChain(int count)
{
var rows = new List<AuditChainRow>();
var previous = AuditChainHasher.GenesisHash.ToArray();
for (var i = 1; i <= count; i++)
{
var row = Row(i);
var hash = AuditChainHasher.ComputeRowHash(previous, Month, i, row);
rows.Add(row with { ChainSeq = i, RowHash = hash });
previous = hash;
}
return (rows, new AuditChainHeadState(count, previous, null, null));
}
private static AuditChainRow Row(int i) => new(
private static AuditArchiveRow Row(int i) => new(
Guid.NewGuid(),
// Odd ticks on purpose: the archive must keep datetime2(7) precision the hash covers.
// Odd ticks on purpose: the archive must keep datetime2(7) precision.
Month.AddMinutes(i).AddTicks(1_234_567 + i),
i % 5 == 0 ? null : i % 7 == 0 ? string.Empty : "api-key-" + i,
"ApiOutbound.ApiCall",
@@ -132,12 +75,11 @@ public class AuditArchiveParquetTests
i % 2 == 0 ? Guid.NewGuid() : null,
i % 8 == 0 ? null : $"{{\"kind\":\"ApiCall\",\"status\":\"Delivered\",\"n\":{i}}}");
private static string Key(AuditChainRow r) => string.Join('|',
private static string Key(AuditArchiveRow r) => string.Join('|',
r.EventId, r.OccurredAtUtc.Ticks, r.OccurredAtUtc.Kind, r.Actor ?? "<null>", r.Action, r.Outcome, r.Category,
r.Target ?? "<null>", r.SourceNode ?? "<null>", r.CorrelationId?.ToString() ?? "<null>", r.DetailsJson ?? "<null>",
r.ChainSeq?.ToString() ?? "<null>", r.RowHash is null ? "<null>" : Convert.ToHexString(r.RowHash));
r.Target ?? "<null>", r.SourceNode ?? "<null>", r.CorrelationId?.ToString() ?? "<null>", r.DetailsJson ?? "<null>");
private static async Task<byte[]> WriteAsync(IEnumerable<AuditChainRow> rows, int rowGroupSize)
internal static async Task<byte[]> WriteAsync(IEnumerable<AuditArchiveRow> rows, int rowGroupSize)
{
var stream = new MemoryStream();
await using (var writer = await AuditArchiveParquet.CreateDataWriterAsync(stream, "2025-10", rowGroupSize))
@@ -151,9 +93,44 @@ public class AuditArchiveParquetTests
return stream.ToArray();
}
private static async Task<List<AuditChainRow>> ReadAsync(byte[] bytes)
/// <summary>Writes rows in the format-v1 layout: the v2 columns, then the two chain columns.</summary>
private static async Task<byte[]> WriteV1Async(IReadOnlyList<AuditArchiveRow> rows)
{
var rows = new List<AuditChainRow>();
var v2 = AuditArchiveParquet.DataSchema.DataFields;
var chainSeq = new DataField<long>("ChainSeq", true);
var rowHash = new DataField<byte[]>("RowHash", true);
var schema = new ParquetSchema(v2.Cast<Field>().Append(chainSeq).Append(rowHash).ToArray());
var stream = new MemoryStream();
await using (var writer = await ParquetWriter.CreateAsync(schema, stream))
{
writer.CustomMetadata = new Dictionary<string, string>
{
[AuditArchiveParquet.FormatVersionKey] = "1",
[AuditArchiveParquet.PartitionKey] = "2025-10",
};
using var group = writer.CreateRowGroup();
await group.WriteAsync((DataField<string>)v2[0], rows.Select(r => r.EventId.ToString("D")).ToArray());
await group.WriteAsync<DateTime>((DateTimeDataField)v2[1], rows.Select(r => r.OccurredAtUtc).ToArray());
await group.WriteAsync<long>((DataField<long>)v2[2], rows.Select(r => r.OccurredAtUtc.Ticks).ToArray());
await group.WriteAsync((DataField<string>)v2[3], rows.Select(r => r.Actor).ToArray()!);
await group.WriteAsync((DataField<string>)v2[4], rows.Select(r => r.Action).ToArray());
await group.WriteAsync((DataField<string>)v2[5], rows.Select(r => r.Outcome).ToArray());
await group.WriteAsync((DataField<string>)v2[6], rows.Select(r => r.Category).ToArray());
await group.WriteAsync((DataField<string>)v2[7], rows.Select(r => r.Target).ToArray()!);
await group.WriteAsync((DataField<string>)v2[8], rows.Select(r => r.SourceNode).ToArray()!);
await group.WriteAsync((DataField<string>)v2[9], rows.Select(r => r.CorrelationId?.ToString("D")).ToArray()!);
await group.WriteAsync((DataField<string>)v2[10], rows.Select(r => r.DetailsJson).ToArray()!);
await group.WriteAsync<long>(chainSeq, rows.Select((_, i) => (long?)(i + 1)).ToArray());
await group.WriteAsync(rowHash, rows.Select(_ => new byte[32]).ToArray());
}
return stream.ToArray();
}
internal static async Task<List<AuditArchiveRow>> ReadAsync(byte[] bytes)
{
var rows = new List<AuditArchiveRow>();
await foreach (var row in AuditArchiveParquet.ReadRowsAsync(new MemoryStream(bytes)))
{
rows.Add(row);
@@ -1,81 +0,0 @@
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// Pure verdict table of <see cref="AuditChainHeadComparer"/> (ScadaBridge#107 item 1) for the
/// branches the SQL Server tests do not reach: a link carried by a tombstone, the partition
/// anchor itself, and malformed input.
/// </summary>
public sealed class AuditChainHeadComparerTests
{
private static readonly byte[] H1 = Enumerable.Repeat((byte)1, 32).ToArray();
private static readonly byte[] H2 = Enumerable.Repeat((byte)2, 32).ToArray();
private static AuditChainHeadRecord Exported(long seq, byte[] hash) => new(
1, "2026-05", seq, AuditChainHeadComparer.ToHex(hash), null, null, seq, 0, DateTime.UtcNow, DateTime.UtcNow, "central-a");
private static AuditChainHeadSnapshot Head(long seq, byte[] hash, long? anchor = null, byte[]? anchorHash = null) =>
new(new DateTime(2026, 5, 1, 0, 0, 0, DateTimeKind.Utc), seq, hash, anchor, anchorHash, seq, 0, DateTime.UtcNow);
private static Func<long, Task<AuditChainHeadComparer.StoredLink>> Link(byte[]? row, byte[]? tombstone)
=> _ => Task.FromResult(new AuditChainHeadComparer.StoredLink(row, tombstone));
private static Func<long, Task<AuditChainHeadComparer.StoredLink>> NoLookup
=> _ => throw new InvalidOperationException("no link lookup expected");
/// <summary>The exported link was later purged per channel: the tombstone carries the hash.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task GrownChain_LinkOnATombstone_IsExtended_OrMismatch()
{
Assert.Equal(AuditChainHeadStatus.Extended,
(await AuditChainHeadComparer.CompareAsync(Exported(3, H1), Head(9, H2), Link(null, H1))).Status);
Assert.Equal(AuditChainHeadStatus.Mismatch,
(await AuditChainHeadComparer.CompareAsync(Exported(3, H1), Head(9, H2), Link(null, H2))).Status);
}
/// <summary>The exported head is the partition-purge anchor: compared with the anchor's hash, no lookup.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task GrownChain_ExportedHeadIsTheAnchor_ComparesTheAnchorHash()
{
Assert.Equal(AuditChainHeadStatus.Extended,
(await AuditChainHeadComparer.CompareAsync(Exported(4, H1), Head(9, H2, 4, H1), NoLookup)).Status);
Assert.Equal(AuditChainHeadStatus.Mismatch,
(await AuditChainHeadComparer.CompareAsync(Exported(4, H1), Head(9, H2, 4, H2), NoLookup)).Status);
Assert.Equal(AuditChainHeadStatus.Purged,
(await AuditChainHeadComparer.CompareAsync(Exported(2, H1), Head(9, H2, 4, H2), NoLookup)).Status);
}
/// <summary>A record whose hash is not 32 bytes of hex can never match.</summary>
/// <param name="hash">The exported hash text.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
[Theory]
[InlineData("abc")]
[InlineData("zz0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")]
public async Task MalformedExportedHash_IsMismatch(string hash)
{
var record = Exported(3, H1) with { LastHash = hash };
Assert.Equal(AuditChainHeadStatus.Mismatch,
(await AuditChainHeadComparer.CompareAsync(record, Head(3, H1), NoLookup)).Status);
}
/// <summary>Only the tamper verdicts count as failures.</summary>
/// <param name="status">The verdict.</param>
/// <param name="failure">Whether it is a failure.</param>
[Theory]
[InlineData(AuditChainHeadStatus.Matches, false)]
[InlineData(AuditChainHeadStatus.Extended, false)]
[InlineData(AuditChainHeadStatus.Purged, false)]
[InlineData(AuditChainHeadStatus.Mismatch, true)]
[InlineData(AuditChainHeadStatus.Regressed, true)]
[InlineData(AuditChainHeadStatus.MissingHead, true)]
[InlineData(AuditChainHeadStatus.MissingLink, true)]
[InlineData(AuditChainHeadStatus.CountMismatch, true)]
public void IsFailure_IsTrueOnlyForTamperVerdicts(AuditChainHeadStatus status, bool failure)
{
Assert.Equal(failure, new AuditChainHeadCheck("2026-05", 1, DateTime.UtcNow, null, status, 1, "").IsFailure);
}
}
@@ -1,312 +0,0 @@
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Migrations;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// SQL Server tests for the chain-head export store (ScadaBridge#107 item 1): heads read with
/// their counts, and exported heads checked against the database. The key case is the forgery
/// the export exists for: rows rewritten AND every later link and the head recomputed — which
/// <c>verify-chain</c> accepts — is caught because the exported hash no longer matches.
/// </summary>
/// <remarks>Each test owns one month (partition) of the shared fixture database.</remarks>
public sealed class AuditChainHeadStoreTests : IClassFixture<MsSqlMigrationFixture>
{
private readonly MsSqlMigrationFixture _fixture;
/// <summary>Creates the test class with the shared migrated SQL Server fixture.</summary>
/// <param name="fixture">The shared migration fixture.</param>
public AuditChainHeadStoreTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
/// <summary>The head snapshot carries the chain end and the live row + tombstone counts.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ReadHeads_ReportsTheChainEnd_AndRowAndTombstoneCounts()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 3);
await InsertAsync(Enumerable.Range(1, 5).Select(i => NewEvent(month.AddDays(i))));
await InsertAsync([NewEvent(month.AddHours(1), AuditChannel.DbOutbound)]);
await DrainAsync();
await using (var ctx = CreateContext())
{
Assert.Equal(1, await new AuditLogRepository(ctx).PurgeChannelOlderThanAsync("DbOutbound", month.AddDays(1), 100));
}
var head = await HeadAsync(month);
Assert.Equal(6, head.LastSeq);
Assert.Equal(5, head.ChainedRows);
Assert.Equal(1, head.Tombstones);
Assert.Null(head.PurgedThroughSeq);
}
/// <summary>
/// Unchanged chain: Matches. The chain grew: Extended. Both centrals' duplicate exports
/// collapse to one check.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ExportedHeads_MatchTheUnchangedChain_AndAreExtendedByLaterLinks()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 4);
await InsertAsync(Enumerable.Range(1, 4).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var exported = Export(await HeadAsync(month));
var same = await VerifyAsync(exported, exported with { Node = "central-b" });
Assert.True(same.Verified);
Assert.Equal(AuditChainHeadStatus.Matches, Assert.Single(same.Checks).Status);
await InsertAsync([NewEvent(month.AddDays(10))]);
await DrainAsync();
var grown = await VerifyAsync(exported);
Assert.True(grown.Verified);
var check = Assert.Single(grown.Checks);
Assert.Equal(AuditChainHeadStatus.Extended, check.Status);
Assert.Equal(5, check.DatabaseSeq);
}
/// <summary>
/// The forgery: a chained row's content is rewritten and every link from it plus the head
/// are recomputed with the real hasher. <c>VerifyMonthAsync</c> reports the forged chain
/// Intact (positive control: the in-database chain alone cannot see it); the exported head
/// does not match it.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ARewrittenAndRehashedChain_PassesVerifyChain_ButFailsTheExportedHead()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 5);
await InsertAsync(Enumerable.Range(1, 6).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var exported = Export(await HeadAsync(month));
await ForgeChainAsync(month, fromSeq: 3);
await using (var ctx = CreateContext())
{
var chain = await new AuditLogHashChain(ctx).VerifyMonthAsync(month);
Assert.Equal(AuditChainStatus.Intact, chain.Status);
}
var result = await VerifyAsync(exported);
Assert.False(result.Verified);
Assert.Equal(AuditChainHeadStatus.Mismatch, Assert.Single(result.Checks).Status);
}
/// <summary>Each structural tampering maps to its own verdict.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task TamperedHeadsAndRows_AreReported_WithTheirOwnVerdicts()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
// Head rolled back below the export (tail deletion + head rewrite).
var regressed = Month(2026, 6);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(regressed.AddDays(i))));
await DrainAsync();
var regressedExport = Export(await HeadAsync(regressed));
await ExecAsync("UPDATE dbo.AuditLogChainHead SET LastSeq = LastSeq - 1 WHERE ChainMonth = @m;", regressed);
// Head row deleted.
var missingHead = Month(2026, 7);
await InsertAsync([NewEvent(missingHead.AddDays(1))]);
await DrainAsync();
var missingHeadExport = Export(await HeadAsync(missingHead));
await ExecAsync("DELETE FROM dbo.AuditLogChainHead WHERE ChainMonth = @m;", missingHead);
// A chained row deleted with no tombstone, head unchanged: counts no longer add up.
var counted = Month(2026, 8);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(counted.AddDays(i))));
await DrainAsync();
var countedExport = Export(await HeadAsync(counted));
await ExecAsync("DELETE FROM dbo.AuditLog WHERE ChainSeq = 2 AND OccurredAtUtc >= @m AND OccurredAtUtc < DATEADD(MONTH, 1, @m);", counted);
// The exported link itself deleted after the chain grew past it.
var missingLink = Month(2026, 9);
await InsertAsync(Enumerable.Range(1, 2).Select(i => NewEvent(missingLink.AddDays(i))));
await DrainAsync();
var missingLinkExport = Export(await HeadAsync(missingLink));
await InsertAsync([NewEvent(missingLink.AddDays(5))]);
await DrainAsync();
await ExecAsync("DELETE FROM dbo.AuditLog WHERE ChainSeq = 2 AND OccurredAtUtc >= @m AND OccurredAtUtc < DATEADD(MONTH, 1, @m);", missingLink);
var result = await VerifyAsync(regressedExport, missingHeadExport, countedExport, missingLinkExport);
Assert.False(result.Verified);
var byMonth = result.Checks.ToDictionary(c => c.Month, c => c.Status);
Assert.Equal(AuditChainHeadStatus.Regressed, byMonth["2026-06"]);
Assert.Equal(AuditChainHeadStatus.MissingHead, byMonth["2026-07"]);
Assert.Equal(AuditChainHeadStatus.CountMismatch, byMonth["2026-08"]);
Assert.Equal(AuditChainHeadStatus.MissingLink, byMonth["2026-09"]);
}
/// <summary>
/// Sanctioned purges are not tamper evidence: a per-channel purge (row to tombstone) keeps
/// Matches; after a partition switch the exported head is the anchor, so the check still
/// passes, and an older exported sequence the switch removed is reported Purged.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task SanctionedPurges_AreNotFailures()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 10);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var early = Export(await HeadAsync(month));
await InsertAsync([NewEvent(month.AddDays(5), AuditChannel.Notification)]);
await DrainAsync();
var late = Export(await HeadAsync(month));
await using (var ctx = CreateContext())
{
Assert.Equal(1, await new AuditLogRepository(ctx).PurgeChannelOlderThanAsync("Notification", month.AddDays(6), 100));
}
Assert.Equal(AuditChainHeadStatus.Matches, Assert.Single((await VerifyAsync(late)).Checks).Status);
await using (var ctx = CreateContext())
{
await new AuditLogRepository(ctx).SwitchOutPartitionAsync(month);
}
var afterSwitch = await VerifyAsync(late, early);
Assert.True(afterSwitch.Verified);
var statuses = afterSwitch.Checks.ToDictionary(c => c.ExportedSeq, c => c.Status);
Assert.Equal(AuditChainHeadStatus.Matches, statuses[4]);
Assert.Equal(AuditChainHeadStatus.Purged, statuses[3]);
}
private static AuditChainHeadRecord Export(AuditChainHeadSnapshot head) => new(
AuditChainHeadRecord.CurrentVersion,
AuditChainHasher.PartitionLabel(head.Month),
head.LastSeq,
AuditChainHeadComparer.ToHex(head.LastHash),
head.PurgedThroughSeq,
head.PurgedThroughHash is null ? null : AuditChainHeadComparer.ToHex(head.PurgedThroughHash),
head.ChainedRows,
head.Tombstones,
head.UpdatedAtUtc,
DateTime.UtcNow,
"central-a");
private async Task<AuditChainHeadSnapshot> HeadAsync(DateTime month)
{
await using var ctx = CreateContext();
var heads = await new AuditLogHashChain(ctx).ReadHeadsAsync();
return Assert.Single(heads, h => h.Month == month);
}
private async Task<AuditChainHeadVerificationResult> VerifyAsync(params AuditChainHeadRecord[] records)
{
await using var ctx = CreateContext();
return await new AuditLogHashChain(ctx).VerifyExportedHeadsAsync(records);
}
/// <summary>
/// Rewrites the row at <paramref name="fromSeq"/> and re-links it and every later row plus the
/// head with the production hasher — what a principal with full write access could do.
/// </summary>
private async Task ForgeChainAsync(DateTime month, long fromSeq)
{
await using var conn = _fixture.OpenConnection();
await using (var edit = new SqlCommand(
"UPDATE dbo.AuditLog SET Target = N'forged' WHERE ChainSeq = @s AND OccurredAtUtc >= @m AND OccurredAtUtc < DATEADD(MONTH, 1, @m);", conn))
{
edit.Parameters.AddWithValue("@s", fromSeq);
edit.Parameters.Add(new SqlParameter("@m", System.Data.SqlDbType.DateTime2) { Scale = 7, Value = month });
Assert.Equal(1, await edit.ExecuteNonQueryAsync());
}
var rows = new List<AuditChainRow>();
await using (var read = new SqlCommand(
$"SELECT {AuditLogHashChain.RowColumns}, ChainSeq, RowHash FROM dbo.AuditLog " +
"WHERE ChainSeq IS NOT NULL AND OccurredAtUtc >= @m AND OccurredAtUtc < DATEADD(MONTH, 1, @m) ORDER BY ChainSeq;", conn))
{
read.Parameters.Add(new SqlParameter("@m", System.Data.SqlDbType.DateTime2) { Scale = 7, Value = month });
await using var reader = await read.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
rows.Add(AuditLogHashChain.ReadRow(reader, withLink: true));
}
}
var previous = rows.Single(r => r.ChainSeq == fromSeq - 1).RowHash!;
foreach (var row in rows.Where(r => r.ChainSeq >= fromSeq))
{
var hash = AuditChainHasher.ComputeRowHash(previous, month, row.ChainSeq!.Value, row);
await using var update = new SqlCommand("UPDATE dbo.AuditLog SET RowHash = @h WHERE EventId = @e AND OccurredAtUtc = @o;", conn);
update.Parameters.AddWithValue("@h", hash);
update.Parameters.AddWithValue("@e", row.EventId);
update.Parameters.Add(new SqlParameter("@o", System.Data.SqlDbType.DateTime2) { Scale = 7, Value = row.OccurredAtUtc });
Assert.Equal(1, await update.ExecuteNonQueryAsync());
previous = hash;
}
await using var head = new SqlCommand("UPDATE dbo.AuditLogChainHead SET LastHash = @h WHERE ChainMonth = @m;", conn);
head.Parameters.AddWithValue("@h", previous);
head.Parameters.Add(new SqlParameter("@m", System.Data.SqlDbType.Date) { Value = month });
Assert.Equal(1, await head.ExecuteNonQueryAsync());
}
private async Task ExecAsync(string sql, DateTime month)
{
await using var conn = _fixture.OpenConnection();
await using var cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add(new SqlParameter("@m", System.Data.SqlDbType.DateTime2) { Scale = 7, Value = month });
Assert.True(await cmd.ExecuteNonQueryAsync() >= 1);
}
private async Task InsertAsync(IEnumerable<AuditEvent> events)
{
await using var ctx = CreateContext();
var repo = new AuditLogRepository(ctx);
foreach (var e in events)
{
await repo.InsertIfNotExistsAsync(e);
}
}
private async Task DrainAsync()
{
await using var ctx = CreateContext();
var chain = new AuditLogHashChain(ctx);
for (var i = 0; i < 1000; i++)
{
var r = await chain.ChainPendingAsync(500, chainBacklog: true);
if (r.RowsChained == 0 && !r.LockBusy)
{
return;
}
}
throw new InvalidOperationException("Chain did not drain.");
}
private static DateTime Month(int year, int month) => new(year, month, 1, 0, 0, 0, DateTimeKind.Utc);
private ScadaBridgeDbContext CreateContext()
=> new(new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlServer(_fixture.ConnectionString).Options);
private static AuditEvent NewEvent(DateTime occurredAtUtc, AuditChannel channel = AuditChannel.ApiOutbound) =>
ScadaBridgeAuditEventFactory.Create(
channel: channel,
kind: channel == AuditChannel.DbOutbound ? AuditKind.DbWrite : AuditKind.ApiCall,
status: AuditStatus.Delivered,
occurredAtUtc: occurredAtUtc,
actor: "head-test",
target: "erp",
sourceNode: "central-a",
sourceSiteId: "head-test",
correlationId: Guid.NewGuid());
}
@@ -1,370 +0,0 @@
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// Storage-free tests for the AuditLog hash-chain hasher + verifier (ScadaBridge#41):
/// tamper detection for an edited, a deleted and a reordered row, tombstone and
/// partition-anchor bridging, and a pinned known-answer vector for the v1 canonical form.
/// </summary>
public class AuditChainVerifierTests
{
private static readonly DateTime Month = new(2026, 5, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>An untouched chain verifies intact and covers every row.</summary>
[Fact]
public void IntactChain_Verifies()
{
var (rows, head) = BuildChain(5);
var result = Verify(rows, head);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(5, result.RowsVerified);
Assert.Null(result.FirstBreak);
}
/// <summary>Editing any stored column of a chained row is a HashMismatch at that row.</summary>
[Fact]
public void EditedRow_IsHashMismatch_AtThatRow()
{
var (rows, head) = BuildChain(5);
rows[2] = rows[2] with { Target = "tampered" };
var result = Verify(rows, head);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.HashMismatch, result.FirstBreak!.Kind);
Assert.Equal(3, result.FirstBreak.ChainSeq);
Assert.Equal(rows[2].EventId, result.FirstBreak.EventId);
Assert.Equal("2026-05", result.FirstBreak.Partition);
Assert.Equal(2, result.RowsVerified);
}
/// <summary>A row deleted from the middle (no tombstone) is a MissingRow at its sequence number.</summary>
[Fact]
public void DeletedMiddleRow_IsMissingRow()
{
var (rows, head) = BuildChain(5);
var deleted = rows[1];
rows.RemoveAt(1);
var result = Verify(rows, head);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.MissingRow, result.FirstBreak!.Kind);
Assert.Equal(deleted.ChainSeq, result.FirstBreak.ChainSeq);
Assert.Equal(rows[1].EventId, result.FirstBreak.EventId); // the next surviving row
}
/// <summary>Deleting the newest rows leaves a self-consistent prefix; the head catches it.</summary>
[Fact]
public void DeletedTailRows_IsHeadMismatch()
{
var (rows, head) = BuildChain(5);
rows.RemoveRange(3, 2);
var result = Verify(rows, head);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.HeadMismatch, result.FirstBreak!.Kind);
Assert.Equal(4, result.FirstBreak.ChainSeq);
Assert.Null(result.FirstBreak.EventId);
}
/// <summary>Swapping two rows' sequence numbers is a HashMismatch at the first moved row.</summary>
[Fact]
public void ReorderedRows_IsHashMismatch()
{
var (rows, head) = BuildChain(5);
var a = rows[1];
var b = rows[3];
rows[1] = b with { ChainSeq = a.ChainSeq };
rows[3] = a with { ChainSeq = b.ChainSeq };
var result = Verify(rows, head);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.HashMismatch, result.FirstBreak!.Kind);
Assert.Equal(2, result.FirstBreak.ChainSeq);
Assert.Equal(b.EventId, result.FirstBreak.EventId);
}
/// <summary>A duplicated sequence number is reported, not silently skipped.</summary>
[Fact]
public void DuplicateSequence_IsReported()
{
var (rows, head) = BuildChain(3);
rows.Insert(2, rows[1] with { EventId = Guid.NewGuid() });
var result = Verify(rows, head);
Assert.Equal(AuditChainBreakKind.DuplicateSequence, result.FirstBreak!.Kind);
}
/// <summary>A sanctioned per-channel purge leaves a tombstone that bridges the gap.</summary>
[Fact]
public void TombstonedRow_BridgesTheGap()
{
var (rows, head) = BuildChain(5);
var purged = rows[2];
rows.RemoveAt(2);
var tombstones = new Dictionary<long, byte[]> { [purged.ChainSeq!.Value] = purged.RowHash! };
var result = Verify(rows, head, tombstones);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(4, result.RowsVerified);
Assert.Equal(1, result.TombstonesApplied);
}
/// <summary>A tombstone with the wrong hash does not hide a deletion.</summary>
[Fact]
public void ForgedTombstone_IsDetectedAtTheNextRow()
{
var (rows, head) = BuildChain(5);
var purged = rows[2];
rows.RemoveAt(2);
var tombstones = new Dictionary<long, byte[]> { [purged.ChainSeq!.Value] = new byte[32] };
var result = Verify(rows, head, tombstones);
Assert.Equal(AuditChainBreakKind.HashMismatch, result.FirstBreak!.Kind);
Assert.Equal(4, result.FirstBreak.ChainSeq);
}
/// <summary>After a partition purge, verification resumes at the checkpoint anchor.</summary>
[Fact]
public void PartitionPurgeAnchor_VerifiesTheRest()
{
var (rows, head) = BuildChain(6);
var anchor = rows[3];
var survivors = rows.Skip(4).ToList();
var anchored = head with { PurgedThroughSeq = anchor.ChainSeq, PurgedThroughHash = anchor.RowHash };
var result = Verify(survivors, anchored);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(2, result.RowsVerified);
Assert.Equal(4, result.PurgedThroughSeq);
}
/// <summary>A month with neither rows nor head reports NotChained.</summary>
[Fact]
public void EmptyMonth_IsNotChained()
{
var result = Verify([], null, unchained: 7);
Assert.Equal(AuditChainStatus.NotChained, result.Status);
Assert.Equal(7, result.UnchainedRows);
}
/// <summary>
/// Pins the v1 canonical form: a change to the serialisation silently invalidates every
/// chain already in a database, so it must fail a test first. The expected value was
/// computed by an independent implementation of the documented v1 encoding (Python
/// hashlib/struct), not by this code.
/// </summary>
[Fact]
public void KnownAnswerVector_V1()
{
var row = new AuditChainRow(
Guid.Parse("0f8fad5b-d9cb-469f-a165-70867728950e"),
new DateTime(2026, 5, 20, 10, 30, 0, DateTimeKind.Utc).AddTicks(1234567),
"alice", "ApiOutbound.ApiCall", "Success", "ApiOutbound", "Erp", "central-a",
Guid.Parse("7c9e6679-7425-40de-944b-e07fc1f90ae7"), "{\"kind\":\"ApiCall\",\"status\":\"Delivered\"}");
var hash = AuditChainHasher.ComputeRowHash(AuditChainHasher.GenesisHash, Month, 1, row);
Assert.Equal("a3137c9961f35f931c7d84de6bced66ce1fe8bbb1dc500f9740b97efd9983ad4", Convert.ToHexStringLower(hash));
}
/// <summary>NULL and the empty string hash differently (presence byte), as do adjacent-field shifts.</summary>
[Fact]
public void Canonical_DistinguishesNullEmptyAndFieldBoundaries()
{
var baseRow = NewRow(1);
var nullTarget = baseRow with { Target = null };
var emptyTarget = baseRow with { Target = "" };
var shifted = baseRow with { Actor = "ab", Target = "c" };
var shifted2 = baseRow with { Actor = "a", Target = "bc" };
Assert.NotEqual(Hash(nullTarget), Hash(emptyTarget));
Assert.NotEqual(Hash(shifted), Hash(shifted2));
}
private static string Hash(AuditChainRow row)
=> Convert.ToHexString(AuditChainHasher.ComputeRowHash(AuditChainHasher.GenesisHash, Month, 1, row));
private static AuditChainVerificationResult Verify(
IEnumerable<AuditChainRow> rows,
AuditChainHeadState? head,
IReadOnlyDictionary<long, byte[]>? tombstones = null,
long unchained = 0)
{
var verifier = new AuditChainVerifier(Month, head, tombstones ?? new Dictionary<long, byte[]>());
foreach (var row in rows)
{
if (!verifier.Accept(row))
{
break;
}
}
return verifier.Complete(unchained);
}
// ── chain start / cutoff (ScadaBridge#139) ───────────────────────────────
private static readonly DateTime CutoffAt = Month.AddHours(12); // rows are dated Month + i days
/// <summary>A cutoff-started month links its first row to the anchor and verifies intact.</summary>
[Fact]
public void CutoffStartedChain_Verifies_FromTheAnchor()
{
var anchor = AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt);
var (rows, head) = BuildChain(4, anchor);
var result = VerifyCutoff(rows, head, anchor);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(4, result.RowsVerified);
Assert.Equal(0, result.TombstonesApplied); // the anchor is not a purge tombstone
Assert.Equal(CutoffAt, result.ChainFromUtc);
Assert.Equal(7, result.PreCutoffRows);
}
/// <summary>Without its anchor, a cutoff-started month fails at its first link.</summary>
[Fact]
public void CutoffStartedChain_WithoutItsAnchor_FailsAtTheFirstLink()
{
var anchor = AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt);
var (rows, head) = BuildChain(4, anchor);
var result = Verify(rows, head);
Assert.Equal(AuditChainBreakKind.HashMismatch, result.FirstBreak!.Kind);
Assert.Equal(1, result.FirstBreak.ChainSeq);
}
/// <summary>A different cutoff is a different anchor: the anchor hash binds the instant.</summary>
[Fact]
public void CutoffAnchorHash_BindsTheInstant()
{
Assert.False(AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt).AsSpan()
.SequenceEqual(AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt.AddTicks(1))));
Assert.Equal(AuditChainHasher.HashLength, AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt).Length);
}
/// <summary>
/// The eligibility attack: a chained row re-dated before the cutoff is a
/// ChainedBeforeCutoff break even when its hash were recomputed to match.
/// </summary>
[Fact]
public void ChainedRowDatedBeforeTheCutoff_IsChainedBeforeCutoff()
{
var anchor = AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt);
var (rows, _) = BuildChain(4, anchor);
// Re-date row 3 before the cutoff and re-hash the chain from it (an attacker who
// rewrites the links): only the cutoff rule can still see it.
rows[2] = rows[2] with { OccurredAtUtc = CutoffAt.AddMinutes(-1) };
var previous = rows[1].RowHash!;
for (var i = 2; i < rows.Count; i++)
{
var hash = AuditChainHasher.ComputeRowHash(previous, Month, i + 1, rows[i] with { ChainSeq = null, RowHash = null });
rows[i] = rows[i] with { RowHash = hash };
previous = hash;
}
var result = VerifyCutoff(rows, new AuditChainHeadState(4, previous, null, null), anchor);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.ChainedBeforeCutoff, result.FirstBreak!.Kind);
Assert.Equal(3, result.FirstBreak.ChainSeq);
Assert.Equal(rows[2].EventId, result.FirstBreak.EventId);
}
/// <summary>An anchor the caller found invalid fails the month even when every link verifies.</summary>
[Fact]
public void InvalidAnchor_IsCutoffAnchorInvalid()
{
var anchor = AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt);
var (rows, head) = BuildChain(4, anchor);
var tombstones = new Dictionary<long, byte[]> { [AuditChainHasher.CutoffAnchorSeq] = anchor };
var verifier = new AuditChainVerifier(Month, head, tombstones, CutoffAt, "anchor altered");
foreach (var row in rows)
{
verifier.Accept(row);
}
var result = verifier.Complete(0);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.CutoffAnchorInvalid, result.FirstBreak!.Kind);
Assert.Equal(AuditChainHasher.CutoffAnchorSeq, result.FirstBreak.ChainSeq);
}
/// <summary>
/// After a partition purge the checkpoint anchor, not the cutoff anchor, is where
/// verification resumes; the cutoff anchor in the same tombstone set is ignored.
/// </summary>
[Fact]
public void PurgeCheckpoint_TakesPrecedenceOverTheCutoffAnchor()
{
var anchor = AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt);
var (rows, _) = BuildChain(5, anchor);
var head = new AuditChainHeadState(5, rows[4].RowHash!, 3, rows[2].RowHash!);
var result = VerifyCutoff(rows.Skip(3), head, anchor);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(2, result.RowsVerified);
}
private static AuditChainVerificationResult VerifyCutoff(IEnumerable<AuditChainRow> rows, AuditChainHeadState head, byte[] anchor)
{
var tombstones = new Dictionary<long, byte[]> { [AuditChainHasher.CutoffAnchorSeq] = anchor };
var verifier = new AuditChainVerifier(Month, head, tombstones, CutoffAt);
foreach (var row in rows)
{
if (!verifier.Accept(row))
{
break;
}
}
return verifier.Complete(0, preCutoffRows: 7);
}
private static (List<AuditChainRow> Rows, AuditChainHeadState Head) BuildChain(int count)
=> BuildChain(count, AuditChainHasher.GenesisHash.ToArray());
private static (List<AuditChainRow> Rows, AuditChainHeadState Head) BuildChain(int count, byte[] genesis)
{
var rows = new List<AuditChainRow>();
var previous = genesis;
for (var seq = 1; seq <= count; seq++)
{
var row = NewRow(seq);
var hash = AuditChainHasher.ComputeRowHash(previous, Month, seq, row);
rows.Add(row with { ChainSeq = seq, RowHash = hash });
previous = hash;
}
return (rows, new AuditChainHeadState(count, previous, null, null));
}
private static AuditChainRow NewRow(int i)
=> new(
Guid.NewGuid(),
Month.AddDays(i).AddTicks(i),
$"actor-{i}",
"ApiOutbound.ApiCall",
"Success",
"ApiOutbound",
$"target-{i}",
"central-a",
null,
$"{{\"kind\":\"ApiCall\",\"n\":{i}}}");
}
@@ -31,9 +31,8 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
public void Dispose() => _root.Delete(recursive: true);
/// <summary>
/// The retried purge: archive A goes stale (a row arrives and the chain advances), B purges.
/// Dry run reports A as superseded and deletes nothing; the real pass deletes exactly A's
/// three files and B still verifies.
/// The retried purge: archive A goes stale (a row arrives), B purges. Dry run reports A as
/// superseded and deletes nothing; the real pass deletes exactly A's files and B still verifies.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
@@ -42,14 +41,11 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var dir = Dir("retried");
var month = Month(2027, 1);
await InsertAsync(Enumerable.Range(1, 5).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
await InsertAsync([NewEvent(month.AddDays(7))]); // unchained in A
await InsertAsync(Enumerable.Range(1, 6).Select(i => NewEvent(month.AddDays(i))));
var stale = await ArchiveAsync(dir, month);
Assert.Equal(1, stale.UnchainedRowCount);
Assert.Equal(6, stale.RowCount);
await InsertAsync([NewEvent(month.AddDays(9))]);
await DrainAsync(); // A's unchained row is now chained; the chain moved past A's end
await using (var ctx = CreateContext())
{
await Assert.ThrowsAsync<AuditArchiveStaleException>(() => Archive(ctx, dir).PurgeArchivedPartitionAsync(stale));
@@ -83,8 +79,8 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
}
/// <summary>
/// A stale archive holding an unchained row that a per-channel purge removed before the
/// later archive (no tombstone for an unchained row) is the only copy of that row: kept.
/// A stale archive holding a row that a per-channel purge removed before the later archive
/// is the only copy of that row: kept.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
@@ -94,8 +90,7 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
var dir = Dir("kept");
var month = Month(2027, 3);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
await InsertAsync([NewEvent(month.AddHours(1), AuditChannel.DbOutbound)]); // unchained
await InsertAsync([NewEvent(month.AddHours(1), AuditChannel.DbOutbound)]);
var stale = await ArchiveAsync(dir, month);
await using (var ctx = CreateContext())
@@ -114,7 +109,7 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
var kept = Assert.Single(result.Items);
Assert.Equal((stale.Name, AuditArchiveCleanupAction.KeptStale), (kept.Name, kept.Action));
Assert.Contains("unchained row", kept.Detail);
Assert.Contains("1 row(s) of this archive are not in", kept.Detail);
Assert.True(File.Exists(ManifestPath(dir, stale)));
}
@@ -130,7 +125,6 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
var dir = Dir("sole");
var month = Month(2027, 4);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var manifest = await ArchiveAsync(dir, month);
await using (var ctx = CreateContext())
{
@@ -165,6 +159,10 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
await InsertAsync([NewEvent(liveMonth.AddDays(1))]);
var live = await ArchiveAsync(dir, liveMonth); // never purged: rows still in the table
// As if it were a format-v1 archive (written with the hash chain): it also has a tombstone file.
var legacyTombstones = Path.Combine(dir, "2027", purged.Name + AuditArchiveParquet.LegacyTombstoneSuffix);
await File.WriteAllTextAsync(legacyTombstones, "v1");
var monthEnd = purgedMonth.AddMonths(1);
var farFuture = liveMonth.AddMonths(1).AddDays(1000);
Assert.Empty((await CleanupAsync(dir, dryRun: false, retentionDays: 0, now: farFuture)).Items);
@@ -175,10 +173,11 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
var expired = Assert.Single(result.Items);
Assert.Equal((purged.Name, AuditArchiveCleanupAction.DeletedExpired), (expired.Name, expired.Action));
Assert.False(File.Exists(ManifestPath(dir, purged)));
Assert.False(File.Exists(legacyTombstones)); // a format-v1 archive's tombstone file goes with it
Assert.True(File.Exists(ManifestPath(dir, live)));
}
/// <summary>Deletion uses the archive name's own three files — never a path from the manifest.</summary>
/// <summary>Deletion uses the archive name's own files — never a path from the manifest.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task Deletion_NeverFollowsAManifestPath_OutOfTheArchiveFolder()
@@ -199,7 +198,6 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
var tampered = JsonSerializer.Deserialize<AuditArchiveManifest>(await File.ReadAllTextAsync(path), Json)! with
{
DataFile = "../../outside.parquet",
TombstoneFile = "../../outside.parquet",
};
await File.WriteAllTextAsync(path, JsonSerializer.Serialize(tampered, Json));
@@ -223,7 +221,6 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
var dir = Dir("orphans");
var month = Month(2027, 6);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var committed = await ArchiveAsync(dir, month);
var year = Directory.CreateDirectory(Path.Combine(dir, "2027")).FullName;
@@ -239,7 +236,7 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
}
var oldTmpData = Make(orphanA + ".parquet.tmp");
var oldTmpTomb = Make(orphanA + ".tombstones.parquet.tmp");
var oldTmpTomb = Make(orphanA + ".tombstones.parquet.tmp"); // a format-v1 writer's temp file
var oldTmpManifest = Make(orphanA + ".manifest.json.tmp");
var oldNoManifestData = Make(orphanA + ".parquet");
var oldNoManifestTomb = Make(orphanA + ".tombstones.parquet");
@@ -252,7 +249,7 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
var otherDir = Path.Combine(dir, "misc", orphanA + ".parquet.tmp");
File.WriteAllText(otherDir, "x");
File.SetLastWriteTimeUtc(otherDir, old);
var committedFiles = new[] { ManifestPath(dir, committed), Path.Combine(year, committed.Name + ".parquet"), Path.Combine(year, committed.Name + ".tombstones.parquet") };
var committedFiles = new[] { ManifestPath(dir, committed), Path.Combine(year, committed.Name + ".parquet") };
foreach (var f in committedFiles)
{
File.SetLastWriteTimeUtc(f, old);
@@ -323,22 +320,6 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture<MsSqlMigrationFi
}
}
private async Task DrainAsync()
{
await using var ctx = CreateContext();
var chain = new AuditLogHashChain(ctx);
for (var i = 0; i < 1000; i++)
{
var r = await chain.ChainPendingAsync(500, chainBacklog: true);
if (r.RowsChained == 0 && !r.LockBusy)
{
return;
}
}
throw new InvalidOperationException("Chain did not drain.");
}
private static DateTime Month(int year, int month) => new(year, month, 1, 0, 0, 0, DateTimeKind.Utc);
private ScadaBridgeDbContext CreateContext()
@@ -1,561 +0,0 @@
using System.Data;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Migrations;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// SQL Server integration tests for the hash-chain cutoff (ScadaBridge#139): enabling the
/// chain on a database with history chains only rows from the enablement instant on (owner
/// decision on #107, 2026-11-13), verification treats the backlog as out of scope, the
/// tamper cases are still caught, moving a row (or the cutoff) across the cutoff is caught,
/// both purge paths keep the chain verifiable across the cutoff, and both central nodes
/// settle on one cutoff.
/// </summary>
/// <remarks>
/// The cutoff is one row per database, so every test starts from an empty AuditLog and empty
/// chain tables (<see cref="ResetAsync"/>). xunit runs a class's tests sequentially.
/// </remarks>
public sealed class AuditLogHashChainCutoffTests : IClassFixture<MsSqlMigrationFixture>, IDisposable
{
private static readonly DateTime Backlog = new(2026, 2, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly DateTime CutoffMonth = new(2026, 3, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly DateTime After = new(2026, 4, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly DateTime Cutoff = new(2026, 3, 15, 12, 0, 0, DateTimeKind.Utc);
private readonly MsSqlMigrationFixture _fixture;
private readonly DirectoryInfo _archiveRoot = Directory.CreateTempSubdirectory("auditlog-cutoff-archive-");
/// <summary>Creates the test class with its own migrated SQL Server database.</summary>
/// <param name="fixture">The per-class migration fixture.</param>
public AuditLogHashChainCutoffTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
/// <inheritdoc />
public void Dispose() => _archiveRoot.Delete(recursive: true);
/// <summary>
/// The owner's decision end to end: a backlog exists, the chain is enabled, only rows dated
/// from the cutoff on are chained, the backlog is untouched, and every month verifies —
/// the backlog as pre-cutoff rows, never as unchained rows or a break.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task FirstEnablement_ChainsOnlyFromTheCutoff_BacklogUntouched_VerifiesIntact()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
await ResetAsync();
var backlog = Rows(Backlog.AddDays(3), 4).Concat(Rows(CutoffMonth.AddDays(2), 3)).ToList();
await InsertAsync(backlog);
await DrainAsync(); // first enablement: records the anchor at Cutoff, chains nothing
Assert.Equal(Cutoff, await AnchorCutoffAsync());
Assert.Equal(0L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLog WHERE ChainSeq IS NOT NULL;"));
Assert.Equal(0L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLogChainHead;"));
var post = Rows(Cutoff.AddMinutes(1), 5).Concat(Rows(After.AddDays(1), 2)).ToList();
await InsertAsync(post);
await DrainAsync();
// Every backlog row stays exactly as it was: unchained.
foreach (var e in backlog)
{
Assert.Equal(DBNull.Value, await ScalarAsync<object>($"SELECT ChainSeq FROM dbo.AuditLog WHERE EventId = '{e.EventId}';"));
}
var march = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Intact, march.Status);
Assert.Equal(5, march.RowsVerified);
Assert.Equal(0, march.UnchainedRows);
Assert.Equal(3, march.PreCutoffRows);
Assert.Equal(Cutoff, march.ChainFromUtc);
var february = await VerifyAsync(Backlog);
Assert.Equal(AuditChainStatus.NotChained, february.Status);
Assert.Equal(0, february.UnchainedRows);
Assert.Equal(4, february.PreCutoffRows);
var april = await VerifyAsync(After);
Assert.Equal(AuditChainStatus.Intact, april.Status);
Assert.Equal(2, april.RowsVerified);
Assert.Equal(0, april.PreCutoffRows);
// A late site row dated before the cutoff that reaches central after enablement is
// keyed on OccurredAtUtc: it stays out of scope (documented limit), not pending.
await InsertAsync(Rows(Cutoff.AddHours(-2), 1));
await DrainAsync();
march = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Intact, march.Status);
Assert.Equal(4, march.PreCutoffRows);
Assert.Equal(0, march.UnchainedRows);
}
/// <summary>
/// The cutoff month's first row links to the anchor, not to 32 zero bytes, and the
/// ordinary tamper cases are still caught after a cutoff start: edit, delete, tail delete.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task TamperCases_AreStillDetected_AfterACutoffStart()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var events = await SeedCutoffChainAsync(postRows: 6);
Assert.Equal(AuditChainStatus.Intact, (await VerifyAsync(CutoffMonth)).Status);
// Edit the FIRST link (the one hashed against the anchor).
await ExecAsync($"UPDATE dbo.AuditLog SET Target = N'forged' WHERE EventId = '{events[0]}';");
var edited = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, edited.Status);
Assert.Equal(AuditChainBreakKind.HashMismatch, edited.FirstBreak!.Kind);
Assert.Equal(1, edited.FirstBreak.ChainSeq);
Assert.Equal(events[0], edited.FirstBreak.EventId);
await ExecAsync($"UPDATE dbo.AuditLog SET Target = N'erp' WHERE EventId = '{events[0]}';");
Assert.Equal(AuditChainStatus.Intact, (await VerifyAsync(CutoffMonth)).Status);
var middleSeq = await SeqOfAsync(events[2]);
await ExecAsync($"DELETE FROM dbo.AuditLog WHERE EventId = '{events[2]}';");
var deleted = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, deleted.Status);
Assert.Equal(AuditChainBreakKind.MissingRow, deleted.FirstBreak!.Kind);
Assert.Equal(middleSeq, deleted.FirstBreak.ChainSeq);
await ResetAsync();
events = await SeedCutoffChainAsync(postRows: 4);
await ExecAsync($"DELETE FROM dbo.AuditLog WHERE EventId = '{events[^1]}';");
var tail = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, tail.Status);
Assert.Equal(AuditChainBreakKind.HeadMismatch, tail.FirstBreak!.Kind);
}
/// <summary>
/// The cutoff-eligibility attack: move a chained row's timestamp back before the cutoff to
/// hide it among the unprotected backlog rows. Caught with its ChainSeq left in place
/// (<see cref="AuditChainBreakKind.ChainedBeforeCutoff"/>), and with its link cleared too
/// (its sequence number goes missing). Moving it into an earlier month is caught in both months.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ChainedRowMovedBeforeTheCutoff_IsDetected()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var events = await SeedCutoffChainAsync(postRows: 5);
var victim = events[2];
var victimSeq = await SeqOfAsync(victim);
// 1. Timestamp moved back across the cutoff, link left in place.
await ExecAsync($"UPDATE dbo.AuditLog SET OccurredAtUtc = '{Cutoff.AddHours(-1):yyyy-MM-dd HH:mm:ss}' WHERE EventId = '{victim}';");
var moved = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, moved.Status);
Assert.Equal(AuditChainBreakKind.ChainedBeforeCutoff, moved.FirstBreak!.Kind);
Assert.Equal(victim, moved.FirstBreak.EventId);
Assert.Equal(victimSeq, moved.FirstBreak.ChainSeq);
// 2. ...and its link cleared, so it looks like any other pre-cutoff row.
await ExecAsync($"UPDATE dbo.AuditLog SET ChainSeq = NULL, RowHash = NULL WHERE EventId = '{victim}';");
var hidden = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, hidden.Status);
Assert.Equal(AuditChainBreakKind.MissingRow, hidden.FirstBreak!.Kind);
Assert.Equal(victimSeq, hidden.FirstBreak.ChainSeq);
Assert.Equal(4, hidden.PreCutoffRows); // the 3 backlog rows + the hidden one
// 3. Moved into the previous (backlog) month with its link: caught in both months.
await ResetAsync();
events = await SeedCutoffChainAsync(postRows: 5);
victim = events[1];
await ExecAsync($"UPDATE dbo.AuditLog SET OccurredAtUtc = '{Backlog.AddDays(9):yyyy-MM-dd HH:mm:ss}' WHERE EventId = '{victim}';");
Assert.Equal(AuditChainBreakKind.MissingRow, (await VerifyAsync(CutoffMonth)).FirstBreak!.Kind);
var february = await VerifyAsync(Backlog);
Assert.Equal(AuditChainStatus.Broken, february.Status);
Assert.Equal(AuditChainBreakKind.ChainedBeforeCutoff, february.FirstBreak!.Kind);
Assert.Equal(victim, february.FirstBreak.EventId);
}
/// <summary>
/// The other half of the eligibility attack: moving the cutoff itself. Editing the anchor's
/// cutoff, re-hashing the anchor to match, deleting it, or adding a second anchor are each
/// reported, so the backlog/scope boundary cannot be shifted silently.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task CutoffAnchorTampering_IsDetected()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
await SeedCutoffChainAsync(postRows: 4);
var later = Cutoff.AddDays(5);
// Cutoff moved forward, anchor hash left alone.
await ExecAsync($"UPDATE dbo.AuditLogChainTombstone SET PurgedAtUtc = '{later:yyyy-MM-dd HH:mm:ss}' WHERE ChainSeq = 0;");
var moved = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, moved.Status);
Assert.Equal(AuditChainBreakKind.CutoffAnchorInvalid, moved.FirstBreak!.Kind);
Assert.Equal(AuditChainStatus.Broken, (await VerifyAsync(After)).Status); // every month sees it
// Cutoff moved forward AND the anchor re-hashed to match: the chained rows now precede it.
await SetAnchorAsync(later, AuditChainHasher.ComputeCutoffAnchorHash(later));
var rehashed = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, rehashed.Status);
Assert.Equal(AuditChainBreakKind.ChainedBeforeCutoff, rehashed.FirstBreak!.Kind);
// Anchor deleted: the cutoff month's first link no longer starts from it.
await ExecAsync("DELETE FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;");
var deleted = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, deleted.Status);
Assert.Equal(AuditChainBreakKind.HashMismatch, deleted.FirstBreak!.Kind);
Assert.Equal(1, deleted.FirstBreak.ChainSeq);
// Restored → intact (positive control); then a second anchor is reported.
await SetAnchorAsync(Cutoff, AuditChainHasher.ComputeCutoffAnchorHash(Cutoff));
Assert.Equal(AuditChainStatus.Intact, (await VerifyAsync(CutoffMonth)).Status);
var earlier = Backlog.AddDays(1);
await ExecAsync(
"INSERT INTO dbo.AuditLogChainTombstone (ChainMonth, ChainSeq, RowHash, EventId, PurgedAtUtc) " +
$"VALUES ('{Backlog:yyyy-MM-dd}', 0, 0x{Convert.ToHexString(AuditChainHasher.ComputeCutoffAnchorHash(earlier))}, NEWID(), '{earlier:yyyy-MM-dd HH:mm:ss}');");
var twoAnchors = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Broken, twoAnchors.Status);
Assert.Equal(AuditChainBreakKind.CutoffAnchorInvalid, twoAnchors.FirstBreak!.Kind);
}
/// <summary>
/// Both purge paths across the cutoff: a per-channel purge that deletes backlog and chained
/// rows alike leaves an intact chain; the head store and an export of it agree (the anchor is
/// not counted as a purge tombstone); the partition switch-out of the cutoff month keeps the
/// anchor and anchors the month; the archive of that month verifies from its own files; and
/// the chainer still honours the cutoff afterwards.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task PurgeAcrossTheCutoff_KeepsEveryChainVerifiable()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
await ResetAsync();
var preDb = Rows(CutoffMonth.AddDays(1), 2, AuditChannel.DbOutbound);
var preApi = Rows(CutoffMonth.AddDays(2), 2);
await InsertAsync(preDb.Concat(preApi));
await DrainAsync();
var postDb = Rows(Cutoff.AddHours(1), 3, AuditChannel.DbOutbound);
var postApi = Rows(Cutoff.AddHours(2), 3);
await InsertAsync(postDb.Concat(postApi));
await DrainAsync();
// Per-channel purge with a threshold past the cutoff: 2 backlog + 3 chained DbOutbound rows.
await using (var ctx = CreateContext())
{
var purged = await new AuditLogRepository(ctx).PurgeChannelOlderThanAsync(
nameof(AuditChannel.DbOutbound), Cutoff.AddDays(1), batchSize: 2);
Assert.Equal(5, purged);
}
var afterChannelPurge = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Intact, afterChannelPurge.Status);
Assert.Equal(3, afterChannelPurge.TombstonesApplied);
Assert.Equal(3, afterChannelPurge.RowsVerified);
Assert.Equal(2, afterChannelPurge.PreCutoffRows);
// Head store + export: the anchor is not a purge tombstone; an export verifies as Matches.
await using (var ctx = CreateContext())
{
var head = Assert.Single(await new AuditLogHashChain(ctx).ReadHeadsAsync());
Assert.Equal(3, head.Tombstones);
Assert.Equal(3, head.ChainedRows);
var exported = await new AuditLogHashChain(ctx).VerifyExportedHeadsAsync([Export(head)]);
Assert.True(exported.Verified);
Assert.Equal(AuditChainHeadStatus.Matches, Assert.Single(exported.Checks).Status);
}
// Archive the cutoff month, then switch it out through the archive guard.
AuditArchiveManifest manifest;
await using (var ctx = CreateContext())
{
manifest = await Archive(ctx).ArchivePartitionAsync(CutoffMonth);
await Archive(ctx).PurgeArchivedPartitionAsync(manifest);
}
Assert.Equal(Cutoff, await AnchorCutoffAsync()); // the anchor survives the switch-out
var switched = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Intact, switched.Status);
Assert.Equal(6, switched.PurgedThroughSeq);
Assert.Equal(0, switched.PreCutoffRows);
await using (var ctx = CreateContext())
{
var archived = await Archive(ctx).VerifyAsync(manifest.Name);
Assert.True(archived.Verified, archived.FileProblem);
var chain = Assert.Single(archived.Chains).Chain;
Assert.Equal(AuditChainStatus.Intact, chain.Status);
Assert.Equal(3, chain.RowsVerified);
Assert.Equal(3, chain.TombstonesApplied);
Assert.Equal(2, chain.PreCutoffRows); // the archive classifies its backlog rows too
Assert.Equal(0, chain.UnchainedRows);
Assert.Equal(Cutoff, chain.ChainFromUtc);
}
// After the purge the cutoff still holds: a late backlog row stays out, a late post-cutoff row chains on.
var lateBacklog = Rows(Backlog.AddDays(4), 1);
var latePost = Rows(Cutoff.AddHours(5), 1);
await InsertAsync(lateBacklog.Concat(latePost));
await DrainAsync();
Assert.Equal(DBNull.Value, await ScalarAsync<object>($"SELECT ChainSeq FROM dbo.AuditLog WHERE EventId = '{lateBacklog[0].EventId}';"));
var late = await VerifyAsync(CutoffMonth);
Assert.Equal(AuditChainStatus.Intact, late.Status);
Assert.Equal(7, late.HeadSeq);
Assert.Equal(1, (await VerifyAsync(Backlog)).PreCutoffRows);
}
/// <summary>
/// Both central nodes settle on ONE cutoff: two chainers race the first enablement on a
/// pristine database with the real database clock, exactly one anchor is written (inside the
/// database clock window of the race), and a node configured to chain the backlog that runs
/// after the decision cannot override it.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task BothNodes_AgreeOnOneCutoff()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
await ResetAsync();
var backlog = Rows(CutoffMonth.AddDays(1), 5);
await InsertAsync(backlog);
var before = await ScalarAsync<DateTime>("SELECT SYSUTCDATETIME();");
var start = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
async Task Node()
{
await start.Task;
for (var i = 0; i < 20; i++)
{
await using var ctx = CreateContext();
await new AuditLogHashChain(ctx).ChainPendingAsync(100); // production clock
}
}
var nodes = new[] { Task.Run(Node), Task.Run(Node) };
start.SetResult();
await Task.WhenAll(nodes);
var after = await ScalarAsync<DateTime>("SELECT SYSUTCDATETIME();");
Assert.Equal(1L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;"));
var cutoff = await AnchorCutoffAsync();
Assert.InRange(cutoff, before, after);
// The peer node configured with ChainBacklog=true runs after the decision: still no backlog chained.
await using (var ctx = CreateContext())
{
Assert.Equal(0, (await new AuditLogHashChain(ctx).ChainPendingAsync(100, chainBacklog: true)).RowsChained);
}
Assert.Equal(0L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLog WHERE ChainSeq IS NOT NULL;"));
Assert.Equal(1L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;"));
var verified = await VerifyAsync(AuditChainHasher.MonthOf(cutoff));
Assert.Equal(cutoff, verified.ChainFromUtc);
}
/// <summary>
/// The old behaviour stays reachable and unchanged: ChainBacklog=true on a pristine database
/// writes no anchor and chains everything, and a database that already holds a chain (legacy
/// enablement, no anchor) keeps chaining every row even with ChainBacklog=false.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ChainBacklog_AndLegacyChains_KeepChainingEverything()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
await ResetAsync();
await InsertAsync(Rows(Backlog.AddDays(1), 3));
await DrainAsync(chainBacklog: true);
Assert.Equal(0L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;"));
Assert.Equal(3L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLog WHERE ChainSeq IS NOT NULL;"));
// Now a node on the default (no backlog) sees an existing chain: no anchor, old rows still chain.
await InsertAsync(Rows(Backlog.AddDays(10), 2));
await DrainAsync();
Assert.Equal(0L, await ScalarAsync<long>("SELECT COUNT_BIG(*) FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;"));
var february = await VerifyAsync(Backlog);
Assert.Equal(AuditChainStatus.Intact, february.Status);
Assert.Equal(5, february.RowsVerified);
Assert.Null(february.ChainFromUtc);
Assert.Equal(0, february.PreCutoffRows);
}
/// <summary>
/// The hardened maintenance login (<c>scadabridge_audit_chainer</c> + <c>scadabridge_audit_purger</c>,
/// ScadaBridge#107 item 3) can write the chain-start anchor: no extra grant, no migration.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task MaintenanceLogin_CanWriteTheAnchor()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
await ResetAsync();
var login = "sbc_" + Guid.NewGuid().ToString("N")[..10];
const string password = "Sb_W22_cutoff#2026";
await ExecAsync($@"
CREATE LOGIN [{login}] WITH PASSWORD = N'{password}', CHECK_POLICY = OFF;
CREATE USER [{login}] FOR LOGIN [{login}];
ALTER ROLE scadabridge_audit_chainer ADD MEMBER [{login}];
ALTER ROLE scadabridge_audit_purger ADD MEMBER [{login}];");
try
{
await InsertAsync(Rows(Backlog.AddDays(2), 2));
var maintenance = new SqlConnectionStringBuilder(_fixture.ConnectionString)
{
IntegratedSecurity = false,
UserID = login,
Password = password,
Pooling = false,
}.ConnectionString;
await using (var ctx = new ScadaBridgeDbContext(
new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlServer(maintenance).Options))
{
var chain = new AuditLogHashChain(ctx) { AnchorClock = () => Cutoff };
Assert.Equal(0, (await chain.ChainPendingAsync(100)).RowsChained);
}
Assert.Equal(Cutoff, await AnchorCutoffAsync());
}
finally
{
SqlConnection.ClearAllPools();
await ExecAsync($@"
DECLARE @kill nvarchar(max) = N'';
SELECT @kill = @kill + N'KILL ' + CAST(session_id AS nvarchar(10)) + N';' FROM sys.dm_exec_sessions WHERE login_name = N'{login}';
EXEC sp_executesql @kill;
DROP USER [{login}];
DROP LOGIN [{login}];");
}
}
// ── helpers ──────────────────────────────────────────────────────────────
/// <summary>
/// Backlog in February and early March, first enablement at <see cref="Cutoff"/>, then
/// <paramref name="postRows"/> rows after it, all chained. Returns the post-cutoff EventIds in chain order.
/// </summary>
private async Task<List<Guid>> SeedCutoffChainAsync(int postRows)
{
await ResetAsync();
await InsertAsync(Rows(Backlog.AddDays(3), 2).Concat(Rows(CutoffMonth.AddDays(2), 3)));
await DrainAsync();
var post = Rows(Cutoff.AddMinutes(1), postRows);
await InsertAsync(post);
await DrainAsync();
return post.Select(e => e.EventId).ToList();
}
private async Task ResetAsync()
{
if (!_fixture.Available)
{
return;
}
await ExecAsync("DELETE FROM dbo.AuditLog; DELETE FROM dbo.AuditLogChainHead; DELETE FROM dbo.AuditLogChainTombstone;");
}
private async Task DrainAsync(bool chainBacklog = false)
{
await using var ctx = CreateContext();
var chain = new AuditLogHashChain(ctx) { AnchorClock = () => Cutoff };
for (var i = 0; i < 1000; i++)
{
var r = await chain.ChainPendingAsync(500, chainBacklog);
if (r.RowsChained == 0 && !r.LockBusy)
{
return;
}
}
throw new InvalidOperationException("Chain did not drain.");
}
private async Task<AuditChainVerificationResult> VerifyAsync(DateTime month)
{
await using var ctx = CreateContext();
return await new AuditLogHashChain(ctx).VerifyMonthAsync(month);
}
private async Task<DateTime> AnchorCutoffAsync()
=> DateTime.SpecifyKind(
await ScalarAsync<DateTime>("SELECT PurgedAtUtc FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;"),
DateTimeKind.Utc);
private async Task SetAnchorAsync(DateTime chainFrom, byte[] hash)
{
await ExecAsync("DELETE FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;");
await ExecAsync(
"INSERT INTO dbo.AuditLogChainTombstone (ChainMonth, ChainSeq, RowHash, EventId, PurgedAtUtc) " +
$"VALUES ('{AuditChainHasher.MonthOf(chainFrom):yyyy-MM-dd}', 0, 0x{Convert.ToHexString(hash)}, '{AuditChainHasher.CutoffAnchorEventId}', '{chainFrom:yyyy-MM-dd HH:mm:ss}');");
}
private async Task<long> SeqOfAsync(Guid eventId)
=> await ScalarAsync<long>($"SELECT ChainSeq FROM dbo.AuditLog WHERE EventId = '{eventId}';");
private async Task InsertAsync(IEnumerable<AuditEvent> events)
{
await using var ctx = CreateContext();
var repo = new AuditLogRepository(ctx);
foreach (var e in events)
{
await repo.InsertIfNotExistsAsync(e);
}
}
private async Task ExecAsync(string sql)
{
await using var conn = new SqlConnection(_fixture.ConnectionString);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
await cmd.ExecuteNonQueryAsync();
}
private async Task<T> ScalarAsync<T>(string sql)
{
await using var conn = new SqlConnection(_fixture.ConnectionString);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
var value = await cmd.ExecuteScalarAsync();
return value is DateTime dt && typeof(T) == typeof(DateTime)
? (T)(object)DateTime.SpecifyKind(dt, DateTimeKind.Utc)
: (T)value!;
}
private AuditLogPartitionArchive Archive(ScadaBridgeDbContext ctx) => new(ctx, new AuditLogArchiveOptions
{
Enabled = true,
Directory = _archiveRoot.FullName,
RowGroupSize = 1_000,
});
private static AuditChainHeadRecord Export(AuditChainHeadSnapshot head) => new(
AuditChainHeadRecord.CurrentVersion,
AuditChainHasher.PartitionLabel(head.Month),
head.LastSeq,
AuditChainHeadComparer.ToHex(head.LastHash),
head.PurgedThroughSeq,
head.PurgedThroughHash is null ? null : AuditChainHeadComparer.ToHex(head.PurgedThroughHash),
head.ChainedRows,
head.Tombstones,
head.UpdatedAtUtc,
DateTime.UtcNow,
"central-a");
private ScadaBridgeDbContext CreateContext()
=> new(new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlServer(_fixture.ConnectionString).Options);
private static List<AuditEvent> Rows(DateTime first, int count, AuditChannel channel = AuditChannel.ApiOutbound)
=> Enumerable.Range(0, count).Select(i => ScadaBridgeAuditEventFactory.Create(
channel: channel,
kind: channel == AuditChannel.DbOutbound ? AuditKind.DbWrite : AuditKind.ApiCall,
status: AuditStatus.Delivered,
occurredAtUtc: first.AddMinutes(i),
target: "erp",
sourceNode: "central-a",
sourceSiteId: "cutoff-test")).ToList();
}
@@ -1,346 +0,0 @@
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Migrations;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// SQL Server integration tests for the AuditLog hash chain (ScadaBridge#41) against the
/// real migrated, partitioned schema: continuity under concurrent writers and chainers,
/// detection of an edited / deleted / reordered row, and survival of both purge paths.
/// </summary>
/// <remarks>
/// The fixture database is shared by the class and the chain is per month across the whole
/// table, so every test owns a distinct month (xunit runs one class's tests sequentially).
/// Each test drains the chain before tampering so no other month is left unchained.
/// </remarks>
public class AuditLogHashChainTests : IClassFixture<MsSqlMigrationFixture>
{
private readonly MsSqlMigrationFixture _fixture;
/// <summary>Creates the test class with the shared migrated SQL Server fixture.</summary>
/// <param name="fixture">The shared migration fixture.</param>
public AuditLogHashChainTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
/// <summary>
/// Many concurrent INSERTs (the two central nodes + site ingest) racing two concurrent
/// chainers (a dual-active central pair) still produce one gap-free, intact chain.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ConcurrentWritersAndChainers_ProduceOneIntactGapFreeChain()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 2);
const int writers = 8;
const int rowsPerWriter = 40;
using var stop = new CancellationTokenSource();
var lockBusySeen = 0;
async Task ChainLoop()
{
while (!stop.IsCancellationRequested)
{
await using var ctx = CreateContext();
var r = await new AuditLogHashChain(ctx).ChainPendingAsync(17, chainBacklog: true);
if (r.LockBusy) Interlocked.Increment(ref lockBusySeen);
if (r.RowsChained == 0) await Task.Delay(5);
}
}
var chainers = new[] { Task.Run(ChainLoop), Task.Run(ChainLoop) };
var writerTasks = Enumerable.Range(0, writers).Select(w => Task.Run(async () =>
{
for (var i = 0; i < rowsPerWriter; i++)
{
await using var ctx = CreateContext();
// Out-of-order OccurredAtUtc on purpose: late-arriving site rows.
var at = month.AddDays((w * 7 + i * 13) % 27).AddSeconds(i);
await new AuditLogRepository(ctx).InsertIfNotExistsAsync(NewEvent(at, sourceNode: $"node-{w}"));
}
})).ToArray();
await Task.WhenAll(writerTasks);
await Task.Delay(200);
await stop.CancelAsync();
await Task.WhenAll(chainers);
await DrainAsync();
var result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(writers * rowsPerWriter, result.RowsVerified);
Assert.Equal(writers * rowsPerWriter, result.HeadSeq);
Assert.Equal(0, result.UnchainedRows);
var seqs = await ChainSeqsAsync(month);
Assert.Equal(Enumerable.Range(1, writers * rowsPerWriter).Select(i => (long)i), seqs);
}
/// <summary>Editing a chained row's content is reported at that row.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task EditedRow_IsDetected_WithRowIdAndPartition()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 3);
var events = await SeedAndChainAsync(month, 6);
Assert.Equal(AuditChainStatus.Intact, (await VerifyAsync(month)).Status);
var victim = events[3];
await ExecAsync($"UPDATE dbo.AuditLog SET Target = N'forged' WHERE EventId = '{victim}';");
var result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.HashMismatch, result.FirstBreak!.Kind);
Assert.Equal("2026-03", result.FirstBreak.Partition);
Assert.Equal(victim, result.FirstBreak.EventId);
Assert.Equal(await SeqOfAsync(victim), result.FirstBreak.ChainSeq);
}
/// <summary>Deleting a chained row outside the purge path is reported as a missing sequence number.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task DeletedRow_IsDetected()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 4);
var events = await SeedAndChainAsync(month, 6);
var victim = events[2];
var victimSeq = await SeqOfAsync(victim);
await ExecAsync($"DELETE FROM dbo.AuditLog WHERE EventId = '{victim}';");
var result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.MissingRow, result.FirstBreak!.Kind);
Assert.Equal(victimSeq, result.FirstBreak.ChainSeq);
Assert.Equal("2026-04", result.FirstBreak.Partition);
}
/// <summary>Swapping two rows' chain positions is reported.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ReorderedRows_AreDetected()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 5);
var events = await SeedAndChainAsync(month, 6);
var a = events[1];
var b = events[4];
var seqA = await SeqOfAsync(a);
var seqB = await SeqOfAsync(b);
await ExecAsync(
$"UPDATE dbo.AuditLog SET ChainSeq = CASE WHEN EventId = '{a}' THEN {seqB} ELSE {seqA} END " +
$"WHERE EventId IN ('{a}', '{b}');");
var result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Broken, result.Status);
Assert.Equal(AuditChainBreakKind.HashMismatch, result.FirstBreak!.Kind);
Assert.Equal(Math.Min(seqA, seqB), result.FirstBreak.ChainSeq);
}
/// <summary>
/// The per-channel retention DELETE removes chained rows from the middle of a month and
/// the chain still verifies (tombstones); a later chaining pass continues the chain.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task PerChannelPurge_LeavesAVerifiableChain()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 6);
var rows = new List<AuditEvent>();
for (var i = 0; i < 10; i++)
{
rows.Add(NewEvent(month.AddDays(i + 1),
channel: i % 2 == 0 ? AuditChannel.DbOutbound : AuditChannel.ApiOutbound,
kind: i % 2 == 0 ? AuditKind.DbWrite : AuditKind.ApiCall));
}
await InsertAsync(rows);
await DrainAsync();
await using (var ctx = CreateContext())
{
var purged = await new AuditLogRepository(ctx).PurgeChannelOlderThanAsync(
nameof(AuditChannel.DbOutbound), month.AddDays(8), batchSize: 2);
Assert.Equal(4, purged); // days 1,3,5,7
}
var result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(4, result.TombstonesApplied);
Assert.Equal(6, result.RowsVerified);
// A row arriving after the purge chains on from the existing head.
await InsertAsync([NewEvent(month.AddDays(20))]);
await DrainAsync();
result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(11, result.HeadSeq);
}
/// <summary>
/// The monthly partition-switch purge removes the whole month; the head's checkpoint
/// anchor keeps the month verifiable, including for a late row chained afterwards.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task PartitionSwitchPurge_LeavesAnAnchoredVerifiableChain()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 7);
await SeedAndChainAsync(month, 5);
await using (var ctx = CreateContext())
{
await new AuditLogRepository(ctx).SwitchOutPartitionAsync(month);
}
var result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(5, result.PurgedThroughSeq);
Assert.Equal(0, result.RowsVerified);
await InsertAsync([NewEvent(month.AddDays(3))]);
await DrainAsync();
result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(1, result.RowsVerified);
Assert.Equal(6, result.HeadSeq);
}
/// <summary>
/// The SourceNode backfill (the other sanctioned AuditLog UPDATE) must not touch a chained
/// row — SourceNode is inside its hash.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task SourceNodeBackfill_SkipsChainedRows()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 8);
await InsertAsync([NewEvent(month.AddDays(2), sourceNode: null)]);
await DrainAsync();
var unchained = NewEvent(month.AddDays(3), sourceNode: null);
// A row written after the drain stays unchained until the next pass.
await InsertAsync([unchained]);
await using (var ctx = CreateContext())
{
await new AuditLogRepository(ctx).BackfillSourceNodeAsync("unknown", month.AddMonths(1), batchSize: 10);
}
await DrainAsync();
var result = await VerifyAsync(month);
Assert.Equal(AuditChainStatus.Intact, result.Status);
Assert.Equal(2, result.RowsVerified);
Assert.Equal("unknown", await ScalarAsync<string>($"SELECT SourceNode FROM dbo.AuditLog WHERE EventId = '{unchained.EventId}';"));
}
// ── helpers ──────────────────────────────────────────────────────────────
private static DateTime Month(int year, int month) => new(year, month, 1, 0, 0, 0, DateTimeKind.Utc);
private async Task<List<Guid>> SeedAndChainAsync(DateTime month, int count)
{
var events = Enumerable.Range(1, count).Select(i => NewEvent(month.AddDays(i).AddMinutes(i))).ToList();
await InsertAsync(events);
await DrainAsync();
return events.Select(e => e.EventId).ToList();
}
private async Task InsertAsync(IEnumerable<AuditEvent> events)
{
await using var ctx = CreateContext();
var repo = new AuditLogRepository(ctx);
foreach (var e in events)
{
await repo.InsertIfNotExistsAsync(e);
}
}
private async Task DrainAsync()
{
await using var ctx = CreateContext();
var chain = new AuditLogHashChain(ctx);
for (var i = 0; i < 1000; i++)
{
var r = await chain.ChainPendingAsync(500, chainBacklog: true);
if (r.RowsChained == 0 && !r.LockBusy)
{
return;
}
}
throw new InvalidOperationException("Chain did not drain.");
}
private async Task<AuditChainVerificationResult> VerifyAsync(DateTime month)
{
await using var ctx = CreateContext();
return await new AuditLogHashChain(ctx).VerifyMonthAsync(month);
}
private async Task<long> SeqOfAsync(Guid eventId)
=> await ScalarAsync<long>($"SELECT ChainSeq FROM dbo.AuditLog WHERE EventId = '{eventId}';");
private async Task<List<long>> ChainSeqsAsync(DateTime month)
{
await using var conn = new SqlConnection(_fixture.ConnectionString);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT ChainSeq FROM dbo.AuditLog WHERE OccurredAtUtc >= @s AND OccurredAtUtc < @e ORDER BY ChainSeq;";
cmd.Parameters.Add(new SqlParameter("@s", System.Data.SqlDbType.DateTime2) { Value = month });
cmd.Parameters.Add(new SqlParameter("@e", System.Data.SqlDbType.DateTime2) { Value = month.AddMonths(1) });
var list = new List<long>();
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
list.Add(reader.GetInt64(0));
}
return list;
}
private async Task ExecAsync(string sql)
{
await using var conn = new SqlConnection(_fixture.ConnectionString);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
await cmd.ExecuteNonQueryAsync();
}
private async Task<T> ScalarAsync<T>(string sql)
{
await using var conn = new SqlConnection(_fixture.ConnectionString);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = sql;
return (T)(await cmd.ExecuteScalarAsync())!;
}
private ScadaBridgeDbContext CreateContext()
=> new(new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlServer(_fixture.ConnectionString).Options);
private static AuditEvent NewEvent(
DateTime occurredAtUtc,
AuditChannel channel = AuditChannel.ApiOutbound,
AuditKind kind = AuditKind.ApiCall,
string? sourceNode = "central-a") =>
ScadaBridgeAuditEventFactory.Create(
channel: channel,
kind: kind,
status: AuditStatus.Delivered,
occurredAtUtc: occurredAtUtc,
target: "erp",
sourceNode: sourceNode,
sourceSiteId: "hash-chain-test");
}
@@ -13,13 +13,12 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// SQL Server integration tests for the AuditLog Parquet archive (ScadaBridge#42) against the
/// real migrated, partitioned schema: archive-then-purge leaves the month's hash chain
/// verifiable and the archive re-verifiable against the purge anchor; the purge refuses to
/// drop a partition that changed after it was archived; tampering with an archive is caught.
/// real migrated, partitioned schema: archive-then-purge archives exactly the partition's rows
/// and then drops them; the purge refuses to drop a partition that changed after it was
/// archived; a damaged archive file is caught by verify.
/// </summary>
/// <remarks>
/// Every test owns a distinct month (partition) of the shared fixture database; the chain
/// and the per-channel purge are table-wide, so each test drains the chain itself.
/// Every test owns a distinct month (partition) of the shared fixture database.
/// </remarks>
public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigrationFixture>, IDisposable
{
@@ -34,42 +33,37 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
public void Dispose() => _root.Delete(recursive: true);
/// <summary>
/// Archive, then purge: the archive holds exactly the partition's rows (chained,
/// unchained, with their chain columns) and its tombstones; after the switch the month's
/// chain still verifies from the anchor, and the archive verifies against that anchor.
/// Archive, then purge: the archive holds exactly the partition's stored rows; the switch
/// drops them, the manifest is marked Purged, and the archive verifies.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ArchiveThenPurge_KeepsTheChainVerifiable_AndTheArchiveMatchesTheAnchor()
public async Task ArchiveThenPurge_ArchivesExactlyThePartition_AndVerifies()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 2);
await InsertAsync(Enumerable.Range(1, 10).Select(i => NewEvent(month.AddDays(i).AddTicks(i * 7_777))));
// Two rows of a short-retention channel, purged per channel mid-chain → tombstones.
// Two rows of a short-retention channel, purged per channel before the archive.
await InsertAsync([NewEvent(month.AddHours(1), AuditChannel.DbOutbound), NewEvent(month.AddHours(2), AuditChannel.DbOutbound)]);
await DrainAsync();
await using (var ctx = CreateContext())
{
Assert.Equal(2, await new AuditLogRepository(ctx).PurgeChannelOlderThanAsync("DbOutbound", month.AddDays(1), 100));
}
await InsertAsync([NewEvent(month.AddDays(20))]); // written after the drain: unchained
await InsertAsync([NewEvent(month.AddDays(20))]);
var before = await PartitionRowsAsync(month);
Assert.Equal(11, before.Count);
var manifest = await ArchiveAsync(month);
Assert.Equal(AuditArchiveStatus.Archived, manifest.Status);
Assert.Equal(AuditArchiveParquet.FormatVersion, manifest.FormatVersion);
Assert.Equal(11, manifest.RowCount);
Assert.Equal(10, manifest.ChainedRowCount);
Assert.Equal(1, manifest.UnchainedRowCount);
Assert.Equal(2, manifest.TombstoneCount);
var chain = Assert.Single(manifest.Chains);
Assert.Equal("2026-02", chain.Partition);
Assert.Equal(12, chain.LastSeq);
Assert.Equal("2026-02", manifest.Partition);
// Row equality: the archive's data file holds exactly the partition's stored rows.
var archived = await ReadArchiveRowsAsync(manifest);
Assert.Equal(before.Select(Key).Order(), archived.Select(Key).Order());
Assert.False(File.Exists(Path.Combine(_root.FullName, "2026", manifest.Name + AuditArchiveParquet.LegacyTombstoneSuffix)));
await using (var ctx = CreateContext())
{
@@ -77,20 +71,11 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
}
Assert.Empty(await PartitionRowsAsync(month));
var monthChain = await VerifyMonthAsync(month);
Assert.Equal(AuditChainStatus.Intact, monthChain.Status);
Assert.Equal(12, monthChain.PurgedThroughSeq);
var verification = await VerifyArchiveAsync(manifest.Name);
Assert.True(verification.Verified, verification.FileProblem);
Assert.Equal(AuditArchiveStatus.Purged, verification.ManifestStatus);
Assert.Equal(11, verification.RowsRead);
var v = Assert.Single(verification.Chains);
Assert.Equal(AuditChainStatus.Intact, v.Chain.Status);
Assert.Equal(10, v.Chain.RowsVerified);
Assert.Equal(2, v.Chain.TombstonesApplied);
Assert.Equal(1, v.Chain.UnchainedRows);
Assert.Equal(AuditArchiveAnchorStatus.Matches, v.Anchor);
await using (var ctx = CreateContext())
{
@@ -112,7 +97,6 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 3);
await InsertAsync(Enumerable.Range(1, 5).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var stale = await ArchiveAsync(month);
await InsertAsync([NewEvent(month.AddDays(9))]);
@@ -123,12 +107,10 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
}
Assert.Equal(6, (await PartitionRowsAsync(month)).Count);
Assert.Null((await VerifyMonthAsync(month)).PurgedThroughSeq);
var staleVerification = await VerifyArchiveAsync(stale.Name);
Assert.True(staleVerification.Verified);
Assert.Equal(AuditArchiveStatus.Archived, staleVerification.ManifestStatus);
Assert.Equal(AuditArchiveAnchorStatus.NotPurged, Assert.Single(staleVerification.Chains).Anchor);
// Positive control: a fresh archive holds the new row and is allowed to purge.
var fresh = await ArchiveAsync(month);
@@ -142,45 +124,16 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
}
/// <summary>
/// A chaining pass between the archive and the purge (the row count is unchanged, the
/// chain head moved) also stops the purge: otherwise the anchor would cover links the
/// archive does not hold.
/// A damaged archive fails verification: a flipped byte (file hash), and a re-written file
/// with one row dropped and the manifest's hash/size updated (row count).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task ChainAdvancingAfterTheArchive_StopsThePurge()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 4);
await InsertAsync(Enumerable.Range(1, 4).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
await InsertAsync([NewEvent(month.AddDays(8))]); // unchained at archive time
var manifest = await ArchiveAsync(month);
Assert.Equal(1, manifest.UnchainedRowCount);
await DrainAsync(); // links the row: head 4 → 5, row count unchanged
await using (var ctx = CreateContext())
{
var ex = await Assert.ThrowsAsync<AuditArchiveStaleException>(() => Archive(ctx).PurgeArchivedPartitionAsync(manifest));
Assert.Equal(AuditPartitionSwitchGuard.ChainAdvancedError, ((SqlException)ex.InnerException!).Number);
}
Assert.Equal(5, (await PartitionRowsAsync(month)).Count);
}
/// <summary>
/// Tampering with a purged archive is caught three ways: a flipped byte (file hash), a
/// re-written file with an edited row and a re-hashed manifest (chain), and a forger who
/// also recomputes the whole chain (the purge anchor in the database no longer matches).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task TamperedArchive_FailsVerification()
public async Task DamagedArchive_FailsVerification()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 5);
await InsertAsync(Enumerable.Range(1, 8).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var manifest = await ArchiveAndPurgeAsync(month);
Assert.True((await VerifyArchiveAsync(manifest.Name)).Verified);
@@ -198,34 +151,14 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
Assert.False(result.FilesIntact);
Assert.Contains("SHA-256", result.FileProblem);
// 2. An edited row in a re-written file, manifest hash/size updated to match.
// 2. A row dropped from a re-written file, manifest hash/size updated to match.
var rows = await ReadArchiveRowsAsync(manifest, original);
var victim = rows[3];
rows[3] = victim with { Target = "forged" };
await RewriteAsync(rows, manifest, dataPath, manifestPath, lastHash: null);
rows.RemoveAt(3);
await RewriteAsync(rows, manifest, dataPath, manifestPath);
result = await VerifyArchiveAsync(manifest.Name);
Assert.False(result.Verified);
Assert.True(result.FilesIntact);
var brk = Assert.Single(result.Chains).Chain.FirstBreak!;
Assert.Equal(AuditChainBreakKind.HashMismatch, brk.Kind);
Assert.Equal(victim.EventId, brk.EventId);
// 3. The forger recomputes every link and the manifest's chain end: the archive is
// self-consistent, but its end no longer matches the anchor the purge left.
var previous = AuditChainHasher.GenesisHash.ToArray();
for (var i = 0; i < rows.Count; i++)
{
var hash = AuditChainHasher.ComputeRowHash(previous, month, rows[i].ChainSeq!.Value, rows[i]);
rows[i] = rows[i] with { RowHash = hash };
previous = hash;
}
await RewriteAsync(rows, manifest, dataPath, manifestPath, lastHash: Convert.ToHexStringLower(previous));
result = await VerifyArchiveAsync(manifest.Name);
Assert.False(result.Verified);
var c = Assert.Single(result.Chains);
Assert.Equal(AuditChainStatus.Intact, c.Chain.Status);
Assert.Equal(AuditArchiveAnchorStatus.Mismatch, c.Anchor);
Assert.False(result.FilesIntact);
Assert.Contains("7 rows", result.FileProblem);
// Positive control: the untouched archive still verifies.
await File.WriteAllBytesAsync(dataPath, original);
@@ -234,36 +167,63 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
}
/// <summary>
/// Rows that arrive for a month after it was purged chain on from the anchor; archiving
/// and purging them again produces a second archive that resumes from the first one's end.
/// The first archive then reports its anchor as superseded, and both verify.
/// Rows that arrive for a month after it was purged are archived and purged again in a
/// second archive; both verify.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task LateRows_AreArchivedAgain_FromTheAnchor()
public async Task LateRows_AreArchivedAgain()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 6);
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(month.AddDays(i))));
await DrainAsync();
var first = await ArchiveAndPurgeAsync(month);
await InsertAsync([NewEvent(month.AddDays(15)), NewEvent(month.AddDays(16))]);
await DrainAsync();
await Task.Delay(5); // distinct archive name (millisecond stamp)
var second = await ArchiveAndPurgeAsync(month);
var chain = Assert.Single(second.Chains);
Assert.Equal(3, chain.PriorPurgedThroughSeq);
Assert.Equal(5, chain.LastSeq);
Assert.Equal(2, second.RowCount);
Assert.Empty(await PartitionRowsAsync(month));
var secondResult = await VerifyArchiveAsync(second.Name);
Assert.True(secondResult.Verified);
Assert.Equal(AuditArchiveAnchorStatus.Matches, Assert.Single(secondResult.Chains).Anchor);
Assert.Equal(2, Assert.Single(secondResult.Chains).Chain.RowsVerified);
Assert.True((await VerifyArchiveAsync(second.Name)).Verified);
Assert.True((await VerifyArchiveAsync(first.Name)).Verified);
}
var firstResult = await VerifyArchiveAsync(first.Name);
Assert.True(firstResult.Verified);
Assert.Equal(AuditArchiveAnchorStatus.Superseded, Assert.Single(firstResult.Chains).Anchor);
/// <summary>
/// A format-v1 manifest (written while the hash chain existed, with chain and tombstone
/// fields) is still listed and verified.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task FormatV1Manifest_IsListedAndVerified()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var month = Month(2026, 7);
await InsertAsync(Enumerable.Range(1, 4).Select(i => NewEvent(month.AddDays(i))));
var manifest = await ArchiveAndPurgeAsync(month);
var manifestPath = Path.Combine(_root.FullName, "2026", manifest.Name + ".manifest.json");
var node = System.Text.Json.Nodes.JsonNode.Parse(await File.ReadAllTextAsync(manifestPath))!.AsObject();
node["formatVersion"] = 1;
node["chainedRowCount"] = 4;
node["unchainedRowCount"] = 0;
node["tombstoneFile"] = manifest.Name + AuditArchiveParquet.LegacyTombstoneSuffix;
node["tombstoneSha256"] = new string('0', 64);
node["tombstoneCount"] = 0;
node["chains"] = new System.Text.Json.Nodes.JsonArray(new System.Text.Json.Nodes.JsonObject
{
["partition"] = "2026-07", ["lastSeq"] = 4, ["lastHash"] = new string('a', 64), ["tombstoneCount"] = 0,
});
await File.WriteAllTextAsync(manifestPath, node.ToJsonString());
await using (var ctx = CreateContext())
{
var listed = Assert.Single(await Archive(ctx).ListAsync(), m => m.Partition == "2026-07");
Assert.Equal(1, listed.FormatVersion);
Assert.Equal(4, listed.RowCount);
}
Assert.True((await VerifyArchiveAsync(manifest.Name)).Verified);
}
/// <summary>A name that is not an archive name (e.g. a path) is rejected before any file access.</summary>
@@ -308,16 +268,10 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
return await Archive(ctx).VerifyAsync(name);
}
private async Task<AuditChainVerificationResult> VerifyMonthAsync(DateTime month)
{
await using var ctx = CreateContext();
return await new AuditLogHashChain(ctx).VerifyMonthAsync(month);
}
private async Task<List<AuditChainRow>> ReadArchiveRowsAsync(AuditArchiveManifest manifest, byte[]? bytes = null)
private async Task<List<AuditArchiveRow>> ReadArchiveRowsAsync(AuditArchiveManifest manifest, byte[]? bytes = null)
{
bytes ??= await File.ReadAllBytesAsync(Path.Combine(_root.FullName, manifest.Partition[..4], manifest.DataFile));
var rows = new List<AuditChainRow>();
var rows = new List<AuditArchiveRow>();
await foreach (var row in AuditArchiveParquet.ReadRowsAsync(new MemoryStream(bytes)))
{
rows.Add(row);
@@ -327,7 +281,7 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
}
private static async Task RewriteAsync(
List<AuditChainRow> rows, AuditArchiveManifest manifest, string dataPath, string manifestPath, string? lastHash)
List<AuditArchiveRow> rows, AuditArchiveManifest manifest, string dataPath, string manifestPath)
{
var stream = new MemoryStream();
await using (var writer = await AuditArchiveParquet.CreateDataWriterAsync(stream, manifest.Partition, 1_000))
@@ -345,7 +299,6 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
Status = AuditArchiveStatus.Purged,
DataSha256 = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)),
DataBytes = bytes.Length,
Chains = lastHash is null ? manifest.Chains : [manifest.Chains[0] with { LastHash = lastHash }],
};
await File.WriteAllTextAsync(manifestPath, JsonSerializer.Serialize(forged, new JsonSerializerOptions
{
@@ -353,29 +306,28 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
}));
}
private async Task<List<AuditChainRow>> PartitionRowsAsync(DateTime month)
private async Task<List<AuditArchiveRow>> PartitionRowsAsync(DateTime month)
{
await using var conn = new SqlConnection(_fixture.ConnectionString);
await conn.OpenAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = $"SELECT {AuditLogHashChain.RowColumns}, ChainSeq, RowHash FROM dbo.AuditLog "
cmd.CommandText = $"SELECT {AuditArchiveParquet.RowColumns} FROM dbo.AuditLog "
+ "WHERE OccurredAtUtc >= @s AND OccurredAtUtc < @e;";
cmd.Parameters.Add(new SqlParameter("@s", System.Data.SqlDbType.DateTime2) { Value = month });
cmd.Parameters.Add(new SqlParameter("@e", System.Data.SqlDbType.DateTime2) { Value = month.AddMonths(1) });
var rows = new List<AuditChainRow>();
var rows = new List<AuditArchiveRow>();
await using var reader = await cmd.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
rows.Add(AuditLogHashChain.ReadRow(reader, withLink: true));
rows.Add(AuditArchiveParquet.ReadRow(reader));
}
return rows;
}
private static string Key(AuditChainRow r) => string.Join('|',
private static string Key(AuditArchiveRow r) => string.Join('|',
r.EventId, r.OccurredAtUtc.Ticks, r.Actor ?? "<null>", r.Action, r.Outcome, r.Category, r.Target ?? "<null>",
r.SourceNode ?? "<null>", r.CorrelationId?.ToString() ?? "<null>", r.DetailsJson ?? "<null>",
r.ChainSeq?.ToString() ?? "<null>", r.RowHash is null ? "<null>" : Convert.ToHexString(r.RowHash));
r.SourceNode ?? "<null>", r.CorrelationId?.ToString() ?? "<null>", r.DetailsJson ?? "<null>");
private async Task InsertAsync(IEnumerable<AuditEvent> events)
{
@@ -387,22 +339,6 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigration
}
}
private async Task DrainAsync()
{
await using var ctx = CreateContext();
var chain = new AuditLogHashChain(ctx);
for (var i = 0; i < 1000; i++)
{
var r = await chain.ChainPendingAsync(500, chainBacklog: true);
if (r.RowsChained == 0 && !r.LockBusy)
{
return;
}
}
throw new InvalidOperationException("Chain did not drain.");
}
private ScadaBridgeDbContext CreateContext()
=> new(new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlServer(_fixture.ConnectionString).Options);
@@ -1,249 +0,0 @@
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Migrations;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance;
/// <summary>
/// The hardened two-login setup (ScadaBridge#107 item 3), end to end on SQL Server: a writer
/// login (<c>db_datareader</c> + <c>db_datawriter</c> + <c>scadabridge_audit_writer</c>) and a
/// maintenance login (<c>scadabridge_audit_chainer</c> + <c>scadabridge_audit_purger</c> only),
/// wired through <c>AddConfigurationDatabase(writer, maintenance)</c>. The writer ingests but
/// cannot chain, update or delete; the maintenance services run on the maintenance login and
/// can chain, verify, export heads, per-channel purge, enumerate partitions, archive and switch.
/// </summary>
public sealed class AuditMaintenanceLoginTests : IClassFixture<MsSqlMigrationFixture>, IAsyncLifetime
{
private const string Password = "Sb_W22_maint#2026";
private readonly MsSqlMigrationFixture _fixture;
private readonly string _suffix = Guid.NewGuid().ToString("N")[..10];
private readonly DirectoryInfo _archiveDir = Directory.CreateTempSubdirectory("maint-login-archive-");
/// <summary>Creates the test class with its own migrated SQL Server database.</summary>
/// <param name="fixture">The per-class migration fixture.</param>
public AuditMaintenanceLoginTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
private string WriterLogin => "sbw_" + _suffix;
private string MaintenanceLogin => "sbm_" + _suffix;
/// <inheritdoc />
public async Task InitializeAsync()
{
if (!_fixture.Available)
{
return;
}
await using var conn = _fixture.OpenConnection();
await using var cmd = new SqlCommand($@"
CREATE LOGIN [{WriterLogin}] WITH PASSWORD = N'{Password}', CHECK_POLICY = OFF;
CREATE LOGIN [{MaintenanceLogin}] WITH PASSWORD = N'{Password}', CHECK_POLICY = OFF;
CREATE USER [{WriterLogin}] FOR LOGIN [{WriterLogin}];
CREATE USER [{MaintenanceLogin}] FOR LOGIN [{MaintenanceLogin}];
ALTER ROLE db_datareader ADD MEMBER [{WriterLogin}];
ALTER ROLE db_datawriter ADD MEMBER [{WriterLogin}];
ALTER ROLE scadabridge_audit_writer ADD MEMBER [{WriterLogin}];
ALTER ROLE scadabridge_audit_chainer ADD MEMBER [{MaintenanceLogin}];
ALTER ROLE scadabridge_audit_purger ADD MEMBER [{MaintenanceLogin}];", conn);
await cmd.ExecuteNonQueryAsync();
}
/// <inheritdoc />
public async Task DisposeAsync()
{
_archiveDir.Delete(recursive: true);
if (!_fixture.Available)
{
return;
}
SqlConnection.ClearAllPools();
await using var conn = _fixture.OpenConnection();
await using var cmd = new SqlCommand($@"
DECLARE @kill nvarchar(max) = N'';
SELECT @kill = @kill + N'KILL ' + CAST(session_id AS nvarchar(10)) + N';' FROM sys.dm_exec_sessions
WHERE login_name IN (N'{WriterLogin}', N'{MaintenanceLogin}');
EXEC sp_executesql @kill;
IF USER_ID(N'{WriterLogin}') IS NOT NULL DROP USER [{WriterLogin}];
IF USER_ID(N'{MaintenanceLogin}') IS NOT NULL DROP USER [{MaintenanceLogin}];
IF SUSER_ID(N'{WriterLogin}') IS NOT NULL DROP LOGIN [{WriterLogin}];
IF SUSER_ID(N'{MaintenanceLogin}') IS NOT NULL DROP LOGIN [{MaintenanceLogin}];", conn);
await cmd.ExecuteNonQueryAsync();
}
/// <summary>
/// Writer ingests and is refused every maintenance write; the maintenance services, resolved
/// from DI on the separate login, do the whole chain + purge + archive cycle.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task HardenedTwoLoginSetup_WriterIngestsOnly_MaintenanceLoginRunsChainAndPurges()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var writerCs = As(WriterLogin);
var maintenanceCs = As(MaintenanceLogin);
var month = new DateTime(2027, 2, 1, 0, 0, 0, DateTimeKind.Utc);
// The writer login ingests.
await using (var writerCtx = Context(writerCs))
{
var repo = new AuditLogRepository(writerCtx);
foreach (var i in Enumerable.Range(1, 4))
{
await repo.InsertIfNotExistsAsync(NewEvent(month.AddDays(i), AuditChannel.ApiOutbound));
}
await repo.InsertIfNotExistsAsync(NewEvent(month.AddHours(2), AuditChannel.DbOutbound));
}
// Negative controls: the writer login can neither chain nor delete.
await using (var writerCtx = Context(writerCs))
{
await Assert.ThrowsAsync<SqlException>(() => new AuditLogHashChain(writerCtx).ChainPendingAsync(100, chainBacklog: true));
await Assert.ThrowsAsync<SqlException>(() =>
new AuditLogRepository(writerCtx).PurgeChannelOlderThanAsync("DbOutbound", month.AddDays(1), 100));
}
var services = new ServiceCollection();
services.AddLogging();
services.AddConfigurationDatabase(writerCs, maintenanceCs);
services.AddSingleton(Options.Create(new AuditLogArchiveOptions
{
Enabled = true,
Directory = _archiveDir.FullName,
RowGroupSize = 1_000,
}));
await using var provider = services.BuildServiceProvider();
await using (var scope = provider.CreateAsyncScope())
{
var maintenance = scope.ServiceProvider.GetRequiredService<AuditMaintenanceContext>();
Assert.True(maintenance.UsesSeparateLogin);
Assert.Equal(MaintenanceLogin, await LoginNameAsync(maintenance.Context));
Assert.Equal(WriterLogin, await LoginNameAsync(scope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>()));
var chain = scope.ServiceProvider.GetRequiredService<IAuditLogHashChain>();
Assert.Equal(5, (await chain.ChainPendingAsync(100, chainBacklog: true)).RowsChained);
Assert.Equal(AuditChainStatus.Intact, (await chain.VerifyMonthAsync(month)).Status);
var head = Assert.Single(await scope.ServiceProvider.GetRequiredService<IAuditChainHeadStore>().ReadHeadsAsync());
Assert.Equal(5, head.LastSeq);
var repo = scope.ServiceProvider.GetRequiredKeyedService<IAuditLogRepository>(AuditMaintenanceContext.ServiceKey);
Assert.Equal(1, await repo.PurgeChannelOlderThanAsync("DbOutbound", month.AddDays(1), 100));
Assert.Contains(month, await repo.GetPartitionBoundariesOlderThanAsync(month.AddYears(5)));
var archive = scope.ServiceProvider.GetRequiredKeyedService<IAuditLogArchive>(AuditMaintenanceContext.ServiceKey);
var manifest = await archive.ArchivePartitionAsync(month);
Assert.Equal(4, manifest.RowCount);
Assert.Equal(4, await archive.PurgeArchivedPartitionAsync(manifest));
Assert.Equal(AuditChainStatus.Intact, (await chain.VerifyMonthAsync(month)).Status);
}
// Unset maintenance string: the maintenance context is the runtime context, as before.
var plain = new ServiceCollection();
plain.AddLogging();
plain.AddConfigurationDatabase(writerCs);
await using var plainProvider = plain.BuildServiceProvider();
await using var plainScope = plainProvider.CreateAsyncScope();
var shared = plainScope.ServiceProvider.GetRequiredService<AuditMaintenanceContext>();
Assert.False(shared.UsesSeparateLogin);
Assert.Same(plainScope.ServiceProvider.GetRequiredService<ScadaBridgeDbContext>(), shared.Context);
}
/// <summary>
/// ScadaBridge#113: the monthly roll-forward (<c>ALTER PARTITION FUNCTION … SPLIT RANGE</c>) and
/// the <c>SourceNode</c> backfill run on the maintenance login — and are refused to the writer
/// login — with only the role grants the <c>AuditMaintenanceGrants</c> migration provisions.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task RollForwardAndSourceNodeBackfill_RunOnTheMaintenanceLogin_NotTheWriter()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var writerCs = As(WriterLogin);
var maintenanceCs = As(MaintenanceLogin);
var month = new DateTime(2027, 4, 1, 0, 0, 0, DateTimeKind.Utc);
// A pre-feature row: no SourceNode, not chained.
await using (var writerCtx = Context(writerCs))
{
var legacy = NewEvent(month.AddDays(1), AuditChannel.ApiOutbound) with { SourceNode = null };
await new AuditLogRepository(writerCtx).InsertIfNotExistsAsync(legacy);
}
// Negative controls: the writer login can neither split the partition function nor backfill.
await using (var writerCtx = Context(writerCs))
{
await Assert.ThrowsAsync<SqlException>(() => new AuditLogPartitionMaintenance(writerCtx).EnsureLookaheadAsync(60));
await Assert.ThrowsAsync<SqlException>(() =>
new AuditLogRepository(writerCtx).BackfillSourceNodeAsync("unknown", month.AddYears(1), 100));
}
var services = new ServiceCollection();
services.AddLogging();
services.AddConfigurationDatabase(writerCs, maintenanceCs);
await using var provider = services.BuildServiceProvider();
await using (var scope = provider.CreateAsyncScope())
{
var maintenance = scope.ServiceProvider.GetRequiredService<AuditMaintenanceContext>();
var roll = scope.ServiceProvider.GetRequiredService<ZB.MOM.WW.ScadaBridge.Commons.Interfaces.IPartitionMaintenance>();
var before = await roll.GetMaxBoundaryAsync();
var added = await roll.EnsureLookaheadAsync(60);
Assert.NotEmpty(added);
Assert.True(await roll.GetMaxBoundaryAsync() > before);
var repo = scope.ServiceProvider.GetRequiredKeyedService<IAuditLogRepository>(
ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories.AuditLogServiceKeys.Maintenance);
Assert.Equal(1, await repo.BackfillSourceNodeAsync("unknown", month.AddYears(1), 100));
Assert.Equal(MaintenanceLogin, await LoginNameAsync(maintenance.Context));
}
}
private string As(string login) => new SqlConnectionStringBuilder(_fixture.ConnectionString)
{
UserID = login,
Password = Password,
IntegratedSecurity = false,
}.ConnectionString;
private static ScadaBridgeDbContext Context(string cs)
=> new(new DbContextOptionsBuilder<ScadaBridgeDbContext>().UseSqlServer(cs).Options);
private static async Task<string> LoginNameAsync(ScadaBridgeDbContext ctx)
{
var conn = ctx.Database.GetDbConnection();
await conn.OpenAsync();
try
{
await using var cmd = conn.CreateCommand();
cmd.CommandText = "SELECT SUSER_SNAME();";
return (string)(await cmd.ExecuteScalarAsync())!;
}
finally
{
await conn.CloseAsync();
}
}
private static AuditEvent NewEvent(DateTime occurredAtUtc, AuditChannel channel) =>
ScadaBridgeAuditEventFactory.Create(
channel: channel,
kind: channel == AuditChannel.DbOutbound ? AuditKind.DbWrite : AuditKind.ApiCall,
status: AuditStatus.Delivered,
occurredAtUtc: occurredAtUtc,
actor: "maint-login-test",
target: "erp",
sourceNode: "central-a",
sourceSiteId: "maint-login-test",
correlationId: Guid.NewGuid());
}