feat(audit): SHA-256 hash chain over the central AuditLog (ScadaBridge#41)

Audit roadmap T1 (alog.md §11.4): one chain per UTC calendar month of
OccurredAtUtc. Each chained row carries ChainSeq (gap-free within its month)
and RowHash = SHA-256(prev.RowHash || canonical(row)); the v1 canonical form
(AuditChainHasher) is pinned by a known-answer vector computed independently.

- Migration AddAuditLogHashChain: nullable ChainSeq/RowHash on dbo.AuditLog
  (metadata-only add), AuditLogChainHead (per-month chain end + partition-purge
  checkpoint anchor), AuditLogChainTombstone, two partition-aligned filtered
  indexes, scadabridge_audit_chainer role with a column-level UPDATE grant.
  Production script docs/plans/sql/AddAuditLogHashChain.sql.
- AuditLogHashChain chains unchained rows oldest month first under an
  exclusive sp_getapplock (zero timeout, so a dual-active central pair still
  yields one chain), with an optimistic head check and a ChainSeq IS NULL
  row-count check that roll a pass back instead of leaving a gap. The ingest
  path is untouched.
- Purges keep the chain verifiable: the partition switch records the month's
  checkpoint anchor and drops its tombstones; the per-channel DELETE writes a
  tombstone per chained row in the same transaction (OUTPUT INTO). Both, and
  the SourceNode backfill, hold the chain lock; the backfill skips chained
  rows. The switch staging table gains the two new columns.
- AuditChainVerifier reports the first break (partition, ChainSeq, EventId,
  kind) for edited, deleted, reordered rows and tail truncation.
- AuditLogHashChainService (central, default OFF via AuditLog:HashChain).

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 16:28:43 -05:00
parent 3cf97f6dda
commit fd4be80913
24 changed files with 4596 additions and 6 deletions
@@ -0,0 +1,61 @@
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities;
/// <summary>
/// One row per chain partition (UTC calendar month) of the AuditLog hash chain
/// (ScadaBridge#41): where the chain currently ends, plus the checkpoint anchor a
/// monthly partition purge leaves behind.
/// </summary>
/// <remarks>
/// The head is what lets verification detect <b>tail truncation</b> — deleting the last
/// rows of a month leaves a chain that is internally consistent but shorter than the
/// head says. It is written by the chaining job (<see cref="LastSeq"/>/<see cref="LastHash"/>)
/// and by the partition-switch purge (<see cref="PurgedThroughSeq"/>/<see cref="PurgedThroughHash"/>).
/// </remarks>
public sealed class AuditLogChainHead
{
/// <summary>First day of the chain's month (UTC). Primary key.</summary>
public DateTime ChainMonth { get; set; }
/// <summary>Sequence number of the last row linked into the chain.</summary>
public long LastSeq { get; set; }
/// <summary>RowHash of the last row linked into the chain (32 bytes).</summary>
public byte[] LastHash { get; set; } = [];
/// <summary>
/// Checkpoint anchor: the last sequence number removed by a monthly partition-switch
/// purge. Verification resumes at <c>PurgedThroughSeq + 1</c> with
/// <see cref="PurgedThroughHash"/> as the previous link. Null when never purged.
/// </summary>
public long? PurgedThroughSeq { get; set; }
/// <summary>RowHash at <see cref="PurgedThroughSeq"/>; null when never purged.</summary>
public byte[]? PurgedThroughHash { get; set; }
/// <summary>UTC time the head last moved.</summary>
public DateTime UpdatedAtUtc { get; set; }
}
/// <summary>
/// The link a sanctioned per-channel retention DELETE removed from the middle of a chain
/// (ScadaBridge#41). Written in the SAME statement batch as the DELETE (via
/// <c>OUTPUT … INTO</c>) so the purge can never remove a chained row without leaving its
/// hash behind; verification bridges the gap with it.
/// </summary>
public sealed class AuditLogChainTombstone
{
/// <summary>First day of the chain's month (UTC). Part of the primary key.</summary>
public DateTime ChainMonth { get; set; }
/// <summary>Sequence number of the purged row. Part of the primary key.</summary>
public long ChainSeq { get; set; }
/// <summary>RowHash of the purged row (32 bytes).</summary>
public byte[] RowHash { get; set; } = [];
/// <summary>The purged row's EventId, kept for the operator's benefit.</summary>
public Guid EventId { get; set; }
/// <summary>UTC time of the purge.</summary>
public DateTime PurgedAtUtc { get; set; }
}
@@ -138,4 +138,19 @@ public sealed record AuditLogRow
: null;
}
private readonly DateTime? _ingestedAtUtc;
// ── Hash-chain link (audit roadmap T1, ScadaBridge#41) ────────────────────
/// <summary>
/// Position of this row in its month's hash chain (1-based, gap-free per UTC calendar
/// month of <see cref="OccurredAtUtc"/>); null until the chaining job reaches the row.
/// Written ONLY by the chaining job — never by the ingest/writer path.
/// </summary>
public long? ChainSeq { get; init; }
/// <summary>
/// <c>SHA-256(prev.RowHash || canonical(row))</c> — 32 bytes; null until chained. See
/// <c>AuditChainHasher</c> for the canonical serialisation.
/// </summary>
public byte[]? RowHash { get; init; }
}