ed2ed0a367
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
357 lines
16 KiB
C#
357 lines
16 KiB
C#
using System.Text.Json;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
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>
|
|
/// SQL Server integration tests for the AuditLog Parquet archive (ScadaBridge#42) against the
|
|
/// 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.
|
|
/// </remarks>
|
|
public sealed class AuditLogPartitionArchiveTests : IClassFixture<MsSqlMigrationFixture>, IDisposable
|
|
{
|
|
private readonly MsSqlMigrationFixture _fixture;
|
|
private readonly DirectoryInfo _root = Directory.CreateTempSubdirectory("auditlog-archive-");
|
|
|
|
/// <summary>Creates the test class with the shared migrated SQL Server fixture.</summary>
|
|
/// <param name="fixture">The shared migration fixture.</param>
|
|
public AuditLogPartitionArchiveTests(MsSqlMigrationFixture fixture) => _fixture = fixture;
|
|
|
|
/// <inheritdoc />
|
|
public void Dispose() => _root.Delete(recursive: true);
|
|
|
|
/// <summary>
|
|
/// 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_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 before the archive.
|
|
await InsertAsync([NewEvent(month.AddHours(1), AuditChannel.DbOutbound), NewEvent(month.AddHours(2), AuditChannel.DbOutbound)]);
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
Assert.Equal(2, await new AuditLogRepository(ctx).PurgeChannelOlderThanAsync("DbOutbound", month.AddDays(1), 100));
|
|
}
|
|
|
|
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("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())
|
|
{
|
|
Assert.Equal(11, await Archive(ctx).PurgeArchivedPartitionAsync(manifest));
|
|
}
|
|
|
|
Assert.Empty(await PartitionRowsAsync(month));
|
|
|
|
var verification = await VerifyArchiveAsync(manifest.Name);
|
|
Assert.True(verification.Verified, verification.FileProblem);
|
|
Assert.Equal(AuditArchiveStatus.Purged, verification.ManifestStatus);
|
|
Assert.Equal(11, verification.RowsRead);
|
|
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
var listed = Assert.Single(await Archive(ctx).ListAsync(), m => m.Partition == "2026-02");
|
|
Assert.Equal(AuditArchiveStatus.Purged, listed.Status);
|
|
Assert.NotNull(listed.PurgedAtUtc);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A row that lands in the partition after it was archived makes the purge refuse
|
|
/// (nothing dropped); a fresh archive then purges. The stale archive still verifies as a
|
|
/// complete, un-purged copy.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableFact]
|
|
public async Task RowArrivingAfterTheArchive_StopsThePurge()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
var month = Month(2026, 3);
|
|
await InsertAsync(Enumerable.Range(1, 5).Select(i => NewEvent(month.AddDays(i))));
|
|
var stale = await ArchiveAsync(month);
|
|
|
|
await InsertAsync([NewEvent(month.AddDays(9))]);
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
var ex = await Assert.ThrowsAsync<AuditArchiveStaleException>(() => Archive(ctx).PurgeArchivedPartitionAsync(stale));
|
|
Assert.Equal(AuditPartitionSwitchGuard.RowCountChangedError, ((SqlException)ex.InnerException!).Number);
|
|
}
|
|
|
|
Assert.Equal(6, (await PartitionRowsAsync(month)).Count);
|
|
|
|
var staleVerification = await VerifyArchiveAsync(stale.Name);
|
|
Assert.True(staleVerification.Verified);
|
|
Assert.Equal(AuditArchiveStatus.Archived, staleVerification.ManifestStatus);
|
|
|
|
// Positive control: a fresh archive holds the new row and is allowed to purge.
|
|
var fresh = await ArchiveAsync(month);
|
|
Assert.Equal(6, fresh.RowCount);
|
|
await using (var ctx = CreateContext())
|
|
{
|
|
Assert.Equal(6, await Archive(ctx).PurgeArchivedPartitionAsync(fresh));
|
|
}
|
|
|
|
Assert.Empty(await PartitionRowsAsync(month));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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 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))));
|
|
var manifest = await ArchiveAndPurgeAsync(month);
|
|
Assert.True((await VerifyArchiveAsync(manifest.Name)).Verified);
|
|
|
|
var dataPath = Path.Combine(_root.FullName, "2026", manifest.DataFile);
|
|
var manifestPath = Path.Combine(_root.FullName, "2026", manifest.Name + ".manifest.json");
|
|
var original = await File.ReadAllBytesAsync(dataPath);
|
|
var originalManifest = await File.ReadAllTextAsync(manifestPath);
|
|
|
|
// 1. A flipped byte.
|
|
var flipped = (byte[])original.Clone();
|
|
flipped[original.Length / 2] ^= 0xFF;
|
|
await File.WriteAllBytesAsync(dataPath, flipped);
|
|
var result = await VerifyArchiveAsync(manifest.Name);
|
|
Assert.False(result.Verified);
|
|
Assert.False(result.FilesIntact);
|
|
Assert.Contains("SHA-256", result.FileProblem);
|
|
|
|
// 2. A row dropped from a re-written file, manifest hash/size updated to match.
|
|
var rows = await ReadArchiveRowsAsync(manifest, original);
|
|
rows.RemoveAt(3);
|
|
await RewriteAsync(rows, manifest, dataPath, manifestPath);
|
|
result = await VerifyArchiveAsync(manifest.Name);
|
|
Assert.False(result.Verified);
|
|
Assert.False(result.FilesIntact);
|
|
Assert.Contains("7 rows", result.FileProblem);
|
|
|
|
// Positive control: the untouched archive still verifies.
|
|
await File.WriteAllBytesAsync(dataPath, original);
|
|
await File.WriteAllTextAsync(manifestPath, originalManifest);
|
|
Assert.True((await VerifyArchiveAsync(manifest.Name)).Verified);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
var month = Month(2026, 6);
|
|
await InsertAsync(Enumerable.Range(1, 3).Select(i => NewEvent(month.AddDays(i))));
|
|
var first = await ArchiveAndPurgeAsync(month);
|
|
|
|
await InsertAsync([NewEvent(month.AddDays(15)), NewEvent(month.AddDays(16))]);
|
|
await Task.Delay(5); // distinct archive name (millisecond stamp)
|
|
var second = await ArchiveAndPurgeAsync(month);
|
|
Assert.Equal(2, second.RowCount);
|
|
Assert.Empty(await PartitionRowsAsync(month));
|
|
|
|
Assert.True((await VerifyArchiveAsync(second.Name)).Verified);
|
|
Assert.True((await VerifyArchiveAsync(first.Name)).Verified);
|
|
}
|
|
|
|
/// <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>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[SkippableFact]
|
|
public async Task VerifyRejectsPathLikeNames()
|
|
{
|
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
|
await using var ctx = CreateContext();
|
|
await Assert.ThrowsAsync<ArgumentException>(() => Archive(ctx).VerifyAsync("../../etc/passwd"));
|
|
await Assert.ThrowsAsync<FileNotFoundException>(() => Archive(ctx).VerifyAsync("auditlog-2026-01-20260101T000000000Z"));
|
|
}
|
|
|
|
// ── helpers ──────────────────────────────────────────────────────────────
|
|
|
|
private static DateTime Month(int year, int month) => new(year, month, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
private AuditLogPartitionArchive Archive(ScadaBridgeDbContext ctx) => new(ctx, new AuditLogArchiveOptions
|
|
{
|
|
Enabled = true,
|
|
Directory = _root.FullName,
|
|
RowGroupSize = 1_000,
|
|
});
|
|
|
|
private async Task<AuditArchiveManifest> ArchiveAsync(DateTime month)
|
|
{
|
|
await using var ctx = CreateContext();
|
|
return await Archive(ctx).ArchivePartitionAsync(month);
|
|
}
|
|
|
|
private async Task<AuditArchiveManifest> ArchiveAndPurgeAsync(DateTime month)
|
|
{
|
|
var manifest = await ArchiveAsync(month);
|
|
await using var ctx = CreateContext();
|
|
await Archive(ctx).PurgeArchivedPartitionAsync(manifest);
|
|
return manifest;
|
|
}
|
|
|
|
private async Task<AuditArchiveVerificationResult> VerifyArchiveAsync(string name)
|
|
{
|
|
await using var ctx = CreateContext();
|
|
return await Archive(ctx).VerifyAsync(name);
|
|
}
|
|
|
|
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<AuditArchiveRow>();
|
|
await foreach (var row in AuditArchiveParquet.ReadRowsAsync(new MemoryStream(bytes)))
|
|
{
|
|
rows.Add(row);
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
private static async Task RewriteAsync(
|
|
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))
|
|
{
|
|
foreach (var row in rows)
|
|
{
|
|
await writer.AddAsync(row);
|
|
}
|
|
}
|
|
|
|
var bytes = stream.ToArray();
|
|
await File.WriteAllBytesAsync(dataPath, bytes);
|
|
var forged = manifest with
|
|
{
|
|
Status = AuditArchiveStatus.Purged,
|
|
DataSha256 = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(bytes)),
|
|
DataBytes = bytes.Length,
|
|
};
|
|
await File.WriteAllTextAsync(manifestPath, JsonSerializer.Serialize(forged, new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
}));
|
|
}
|
|
|
|
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 {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<AuditArchiveRow>();
|
|
await using var reader = await cmd.ExecuteReaderAsync();
|
|
while (await reader.ReadAsync())
|
|
{
|
|
rows.Add(AuditArchiveParquet.ReadRow(reader));
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
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>");
|
|
|
|
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 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: "archive-test",
|
|
target: "erp",
|
|
sourceNode: "central-a",
|
|
sourceSiteId: "archive-test",
|
|
correlationId: Guid.NewGuid());
|
|
}
|