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:
@@ -1,155 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.ScadaBridge.AuditLog.Central;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Central;
|
||||
|
||||
/// <summary>
|
||||
/// Policy tests for <see cref="AuditLogHashChainService"/> (ScadaBridge#41) over a scripted
|
||||
/// <see cref="IAuditLogHashChain"/>: default-off, draining a backlog within the per-tick
|
||||
/// cap, yielding to the peer node's lock, and containing failures. The SQL chaining itself
|
||||
/// is covered by <c>AuditLogHashChainTests</c> in ConfigurationDatabase.Tests.
|
||||
/// </summary>
|
||||
public class AuditLogHashChainServiceTests
|
||||
{
|
||||
private sealed class ScriptedChain(params AuditChainBatchResult[] script) : IAuditLogHashChain
|
||||
{
|
||||
private int _next;
|
||||
public int Calls => _next;
|
||||
public Exception? Throw { get; init; }
|
||||
|
||||
public List<bool> ChainBacklogSeen { get; } = [];
|
||||
|
||||
public Task<AuditChainBatchResult> ChainPendingAsync(int batchSize, bool chainBacklog = false, CancellationToken ct = default)
|
||||
{
|
||||
ChainBacklogSeen.Add(chainBacklog);
|
||||
if (Throw is not null) throw Throw;
|
||||
var i = _next++;
|
||||
return Task.FromResult(i < script.Length ? script[i] : new AuditChainBatchResult(0, false));
|
||||
}
|
||||
|
||||
public Task<AuditChainVerificationResult> VerifyMonthAsync(DateTime monthStartUtc, CancellationToken ct = default)
|
||||
=> throw new NotSupportedException();
|
||||
}
|
||||
|
||||
private static AuditLogHashChainService Create(IAuditLogHashChain chain, AuditLogHashChainOptions options)
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped(_ => chain);
|
||||
return new AuditLogHashChainService(
|
||||
services.BuildServiceProvider().GetRequiredService<IServiceScopeFactory>(),
|
||||
Options.Create(options),
|
||||
NullLogger<AuditLogHashChainService>.Instance);
|
||||
}
|
||||
|
||||
/// <summary>The job is off by default and starting it then does nothing.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Disabled_ByDefault_StartsNoLoop()
|
||||
{
|
||||
var chain = new ScriptedChain(new AuditChainBatchResult(5, false));
|
||||
Assert.False(new AuditLogHashChainOptions().Enabled);
|
||||
using var service = Create(chain, new AuditLogHashChainOptions());
|
||||
|
||||
await service.StartAsync(CancellationToken.None);
|
||||
await Task.Delay(50);
|
||||
await service.StopAsync(CancellationToken.None);
|
||||
|
||||
Assert.Equal(0, chain.Calls);
|
||||
}
|
||||
|
||||
/// <summary>A tick drains batches until one comes back empty.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Tick_DrainsUntilEmpty()
|
||||
{
|
||||
var chain = new ScriptedChain(
|
||||
new AuditChainBatchResult(10, false), new AuditChainBatchResult(10, false), new AuditChainBatchResult(3, false));
|
||||
using var service = Create(chain, new AuditLogHashChainOptions { Enabled = true, BatchSize = 10 });
|
||||
|
||||
Assert.Equal(23, await service.TickAsync(CancellationToken.None));
|
||||
Assert.Equal(4, chain.Calls); // the 4th returns 0 and ends the tick
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#139: backlog chaining is off by default (owner decision, #107) and the
|
||||
/// option reaches every chaining pass when set.
|
||||
/// </summary>
|
||||
/// <param name="chainBacklog">The configured value, or null to leave the default.</param>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData(false)]
|
||||
[InlineData(true)]
|
||||
public async Task Tick_PassesChainBacklog_DefaultOff(bool? chainBacklog)
|
||||
{
|
||||
var options = new AuditLogHashChainOptions { Enabled = true };
|
||||
if (chainBacklog is { } value)
|
||||
{
|
||||
options.ChainBacklog = value;
|
||||
}
|
||||
|
||||
var chain = new ScriptedChain(new AuditChainBatchResult(4, false));
|
||||
using var service = Create(chain, options);
|
||||
|
||||
await service.TickAsync(CancellationToken.None);
|
||||
|
||||
Assert.Equal(2, chain.ChainBacklogSeen.Count);
|
||||
Assert.All(chain.ChainBacklogSeen, seen => Assert.Equal(chainBacklog ?? false, seen));
|
||||
}
|
||||
|
||||
/// <summary>The peer node holding the lock ends the tick at once.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Tick_StopsWhenPeerHoldsTheLock()
|
||||
{
|
||||
var chain = new ScriptedChain(new AuditChainBatchResult(0, LockBusy: true), new AuditChainBatchResult(10, false));
|
||||
using var service = Create(chain, new AuditLogHashChainOptions { Enabled = true });
|
||||
|
||||
Assert.Equal(0, await service.TickAsync(CancellationToken.None));
|
||||
Assert.Equal(1, chain.Calls);
|
||||
}
|
||||
|
||||
/// <summary>A backlog yields after MaxBatchesPerTick batches.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Tick_RespectsMaxBatchesPerTick()
|
||||
{
|
||||
var chain = new ScriptedChain(Enumerable.Repeat(new AuditChainBatchResult(10, false), 50).ToArray());
|
||||
using var service = Create(chain, new AuditLogHashChainOptions { Enabled = true, MaxBatchesPerTick = 3 });
|
||||
|
||||
Assert.Equal(30, await service.TickAsync(CancellationToken.None));
|
||||
Assert.Equal(3, chain.Calls);
|
||||
}
|
||||
|
||||
/// <summary>A failing tick is contained (logged), never thrown out of the loop.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Tick_ContainsFailures()
|
||||
{
|
||||
var chain = new ScriptedChain { Throw = new InvalidOperationException("sql down") };
|
||||
using var service = Create(chain, new AuditLogHashChainOptions { Enabled = true });
|
||||
|
||||
Assert.Equal(0, await service.TickAsync(CancellationToken.None));
|
||||
}
|
||||
|
||||
/// <summary>Invalid tuning fails startup validation.</summary>
|
||||
/// <param name="interval">IntervalSeconds.</param>
|
||||
/// <param name="batch">BatchSize.</param>
|
||||
/// <param name="maxBatches">MaxBatchesPerTick.</param>
|
||||
[Theory]
|
||||
[InlineData(0, 1000, 100)]
|
||||
[InlineData(60, 0, 100)]
|
||||
[InlineData(60, 10_001, 100)]
|
||||
[InlineData(60, 1000, 0)]
|
||||
public void Validator_RejectsInvalidTuning(int interval, int batch, int maxBatches)
|
||||
{
|
||||
var result = new AuditLogHashChainOptionsValidator().Validate(null,
|
||||
new AuditLogHashChainOptions { IntervalSeconds = interval, BatchSize = batch, MaxBatchesPerTick = maxBatches });
|
||||
Assert.False(result.Succeeded);
|
||||
Assert.True(new AuditLogHashChainOptionsValidator().Validate(null, new AuditLogHashChainOptions()).Succeeded);
|
||||
}
|
||||
}
|
||||
@@ -713,9 +713,9 @@ public class AuditLogPurgeActorTests : TestKit, IClassFixture<MsSqlMigrationFixt
|
||||
}
|
||||
|
||||
private static AuditArchiveManifest ManifestFor(DateTime boundary) => new(
|
||||
1, $"auditlog-{boundary:yyyy-MM}-20261109T120000000Z", boundary, boundary.ToString("yyyy-MM"),
|
||||
DateTime.UtcNow, AuditArchiveStatus.Archived, null, 7, 7, 0, null, null,
|
||||
"d.parquet", "00", 99, "t.parquet", "00", 0, []);
|
||||
2, $"auditlog-{boundary:yyyy-MM}-20261109T120000000Z", boundary, boundary.ToString("yyyy-MM"),
|
||||
DateTime.UtcNow, AuditArchiveStatus.Archived, null, 7, null, null,
|
||||
"d.parquet", "00", 99);
|
||||
|
||||
/// <summary>
|
||||
/// With archiving enabled every expired partition is archived and then purged by the
|
||||
@@ -793,36 +793,6 @@ public class AuditLogPurgeActorTests : TestKit, IClassFixture<MsSqlMigrationFixt
|
||||
StopPurgeActor(actor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#107 item 3: when the host registered the maintenance-connection repository
|
||||
/// (keyed <c>audit-maintenance</c>), the purge runs on it, never on the runtime one.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Tick_UsesTheMaintenanceConnectionRepository_WhenRegistered()
|
||||
{
|
||||
var runtime = new RecordingRepo { Boundaries = [new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc)] };
|
||||
var maintenance = new RecordingRepo { Boundaries = [new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc)] };
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped<IAuditLogRepository>(_ => runtime);
|
||||
services.AddKeyedScoped<IAuditLogRepository>(
|
||||
ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance.AuditMaintenanceContext.ServiceKey, (_, _) => maintenance);
|
||||
var sp = services.BuildServiceProvider();
|
||||
var actor = Sys.ActorOf(Props.Create(() => new AuditLogPurgeActor(
|
||||
sp,
|
||||
Options.Create(FastTickOptions()),
|
||||
Options.Create(new AuditLogOptions()),
|
||||
NullLogger<AuditLogPurgeActor>.Instance)));
|
||||
|
||||
AwaitAssert(
|
||||
() => Assert.NotEmpty(maintenance.SwitchedBoundaries),
|
||||
duration: TimeSpan.FromSeconds(3),
|
||||
interval: TimeSpan.FromMilliseconds(50));
|
||||
Assert.Empty(runtime.ThresholdQueries);
|
||||
Assert.Empty(runtime.SwitchedBoundaries);
|
||||
|
||||
StopPurgeActor(actor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#109: with archiving enabled every tick ends with the archive cleanup pass —
|
||||
/// also on a tick with no expired partition — and never runs it when archiving is off.
|
||||
|
||||
-207
@@ -1,207 +0,0 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.ScadaBridge.AuditLog.Central;
|
||||
using ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Central.HeadExport;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="AuditChainHeadExportService"/> (ScadaBridge#107 item 1): first tick exports every
|
||||
/// head, later ticks only the changed ones, a failed write is retried with the same heads, and
|
||||
/// the options validator guards each sink's settings.
|
||||
/// </summary>
|
||||
public sealed class AuditChainHeadExportServiceTests
|
||||
{
|
||||
private sealed class FakeStore : IAuditChainHeadStore
|
||||
{
|
||||
public List<AuditChainHeadSnapshot> Heads { get; } = [];
|
||||
|
||||
public Task<IReadOnlyList<AuditChainHeadSnapshot>> ReadHeadsAsync(CancellationToken ct = default)
|
||||
=> Task.FromResult<IReadOnlyList<AuditChainHeadSnapshot>>(Heads.ToList());
|
||||
|
||||
public Task<AuditChainHeadVerificationResult> VerifyExportedHeadsAsync(
|
||||
IReadOnlyList<AuditChainHeadRecord> records, CancellationToken ct = default) => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
private sealed class RecordingSink : IAuditChainHeadSink
|
||||
{
|
||||
public List<IReadOnlyList<AuditChainHeadRecord>> Writes { get; } = [];
|
||||
|
||||
public bool Fail { get; set; }
|
||||
|
||||
public string Describe => "recording";
|
||||
|
||||
public Task WriteAsync(IReadOnlyList<AuditChainHeadRecord> records, CancellationToken ct)
|
||||
{
|
||||
if (Fail)
|
||||
{
|
||||
throw new IOException("share offline");
|
||||
}
|
||||
|
||||
Writes.Add(records);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
private static AuditChainHeadSnapshot Head(int month, long seq, long rows) => new(
|
||||
new DateTime(2026, month, 1, 0, 0, 0, DateTimeKind.Utc), seq, Enumerable.Repeat((byte)seq, 32).ToArray(),
|
||||
null, null, rows, 0, new DateTime(2026, month, 2, 0, 0, 0, DateTimeKind.Utc));
|
||||
|
||||
private static (AuditChainHeadExportService Service, FakeStore Store, RecordingSink Sink) Create(
|
||||
bool exportUnchanged = false, AuditChainHeadExportStatus? status = null, TimeProvider? time = null)
|
||||
{
|
||||
var store = new FakeStore();
|
||||
var sink = new RecordingSink();
|
||||
var services = new ServiceCollection();
|
||||
services.AddScoped<IAuditChainHeadStore>(_ => store);
|
||||
var options = Options.Create(new AuditLogHashChainOptions
|
||||
{
|
||||
HeadExport = new AuditChainHeadExportOptions { Enabled = true, ExportUnchanged = exportUnchanged },
|
||||
});
|
||||
var service = new AuditChainHeadExportService(
|
||||
services.BuildServiceProvider().GetRequiredService<IServiceScopeFactory>(),
|
||||
options, sink, "central-a", NullLogger<AuditChainHeadExportService>.Instance, time, status);
|
||||
return (service, store, sink);
|
||||
}
|
||||
|
||||
/// <summary>First tick: every head; next tick: only the head that moved; unchanged: nothing.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Tick_ExportsAllHeadsFirst_ThenOnlyChangedOnes()
|
||||
{
|
||||
var (service, store, sink) = Create();
|
||||
store.Heads.AddRange([Head(4, 10, 10), Head(5, 20, 20)]);
|
||||
|
||||
Assert.Equal(2, await service.TickAsync(default));
|
||||
Assert.Equal(0, await service.TickAsync(default));
|
||||
|
||||
store.Heads[1] = Head(5, 25, 25);
|
||||
Assert.Equal(1, await service.TickAsync(default));
|
||||
|
||||
// A per-channel purge (row -> tombstone) keeps rows + tombstones, so it is not a change;
|
||||
// a row disappearing without a tombstone is.
|
||||
store.Heads[0] = Head(4, 10, 9);
|
||||
Assert.Equal(1, await service.TickAsync(default));
|
||||
|
||||
Assert.Equal(3, sink.Writes.Count);
|
||||
Assert.Equal(["2026-04", "2026-05"], sink.Writes[0].Select(r => r.Month));
|
||||
var moved = Assert.Single(sink.Writes[1]);
|
||||
Assert.Equal(("2026-05", 25L, string.Concat(Enumerable.Repeat("19", 32))), (moved.Month, moved.LastSeq, moved.LastHash));
|
||||
Assert.Equal("central-a", moved.Node);
|
||||
Assert.Equal(AuditChainHeadRecord.CurrentVersion, moved.V);
|
||||
}
|
||||
|
||||
/// <summary>ExportUnchanged re-exports every head every tick.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Tick_WithExportUnchanged_ExportsEveryHeadEveryTick()
|
||||
{
|
||||
var (service, store, _) = Create(exportUnchanged: true);
|
||||
store.Heads.AddRange([Head(4, 10, 10), Head(5, 20, 20)]);
|
||||
|
||||
Assert.Equal(2, await service.TickAsync(default));
|
||||
Assert.Equal(2, await service.TickAsync(default));
|
||||
}
|
||||
|
||||
/// <summary>A failed write is contained and the same heads go out on the next tick.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Tick_AfterAFailedWrite_RetriesTheSameHeads()
|
||||
{
|
||||
var (service, store, sink) = Create();
|
||||
store.Heads.Add(Head(4, 10, 10));
|
||||
sink.Fail = true;
|
||||
|
||||
Assert.Equal(0, await service.TickAsync(default));
|
||||
sink.Fail = false;
|
||||
Assert.Equal(1, await service.TickAsync(default));
|
||||
Assert.Equal(10, Assert.Single(Assert.Single(sink.Writes)).LastSeq);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ScadaBridge#115: a failed tick bumps the failure counter metric and the status
|
||||
/// (consecutive + total + last error); the next success clears the consecutive count.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task FailedTick_RecordsMetricAndStatus_SuccessResetsConsecutive()
|
||||
{
|
||||
var status = new AuditChainHeadExportStatus();
|
||||
var (service, store, sink) = Create(status: status);
|
||||
store.Heads.Add(Head(4, 10, 10));
|
||||
long measured = 0;
|
||||
using var listener = new System.Diagnostics.Metrics.MeterListener();
|
||||
listener.InstrumentPublished = (i, l) =>
|
||||
{
|
||||
if (i.Name == "scadabridge.audit.chain_head_export.failures")
|
||||
{
|
||||
l.EnableMeasurementEvents(i);
|
||||
}
|
||||
};
|
||||
listener.SetMeasurementEventCallback<long>((_, v, _, _) => Interlocked.Add(ref measured, v));
|
||||
listener.Start();
|
||||
|
||||
sink.Fail = true;
|
||||
await service.TickAsync(default);
|
||||
await service.TickAsync(default);
|
||||
|
||||
Assert.Equal(2, Interlocked.Read(ref measured));
|
||||
Assert.Equal(2, status.ConsecutiveFailures);
|
||||
Assert.Equal(2, status.TotalFailures);
|
||||
Assert.Equal("share offline", status.LastError);
|
||||
Assert.Null(status.LastSuccessUtc);
|
||||
|
||||
sink.Fail = false;
|
||||
await service.TickAsync(default);
|
||||
|
||||
Assert.Equal(0, status.ConsecutiveFailures);
|
||||
Assert.Equal(2, status.TotalFailures);
|
||||
Assert.NotNull(status.LastSuccessUtc);
|
||||
Assert.Equal(2, Interlocked.Read(ref measured));
|
||||
}
|
||||
|
||||
/// <summary>The validator ignores a disabled export and checks each sink's settings when enabled.</summary>
|
||||
/// <param name="configure">Which settings to apply.</param>
|
||||
/// <param name="expectedError">A fragment of the expected failure, or null when valid.</param>
|
||||
[Theory]
|
||||
[InlineData("disabled-bad", null)]
|
||||
[InlineData("file-ok", null)]
|
||||
[InlineData("file-relative", "File:Directory")]
|
||||
[InlineData("file-missing", "File:Directory")]
|
||||
[InlineData("syslog-ok", null)]
|
||||
[InlineData("syslog-no-host", "Syslog:Host")]
|
||||
[InlineData("syslog-bad-port", "Syslog:Port")]
|
||||
[InlineData("syslog-bad-facility", "Syslog:Facility")]
|
||||
[InlineData("interval-zero", "IntervalMinutes")]
|
||||
public void Validator_ChecksTheSelectedSink(string configure, string? expectedError)
|
||||
{
|
||||
var root = OperatingSystem.IsWindows() ? @"C:\heads" : "/var/heads";
|
||||
var export = configure switch
|
||||
{
|
||||
"disabled-bad" => new AuditChainHeadExportOptions { Enabled = false, IntervalMinutes = 0 },
|
||||
"file-ok" => new AuditChainHeadExportOptions { Enabled = true, File = { Directory = root } },
|
||||
"file-relative" => new AuditChainHeadExportOptions { Enabled = true, File = { Directory = "heads" } },
|
||||
"file-missing" => new AuditChainHeadExportOptions { Enabled = true },
|
||||
"syslog-ok" => new AuditChainHeadExportOptions { Enabled = true, Sink = AuditChainHeadSinkKind.Syslog, Syslog = { Host = "collector" } },
|
||||
"syslog-no-host" => new AuditChainHeadExportOptions { Enabled = true, Sink = AuditChainHeadSinkKind.Syslog },
|
||||
"syslog-bad-port" => new AuditChainHeadExportOptions { Enabled = true, Sink = AuditChainHeadSinkKind.Syslog, Syslog = { Host = "c", Port = 0 } },
|
||||
"syslog-bad-facility" => new AuditChainHeadExportOptions { Enabled = true, Sink = AuditChainHeadSinkKind.Syslog, Syslog = { Host = "c", Facility = 24 } },
|
||||
_ => new AuditChainHeadExportOptions { Enabled = true, IntervalMinutes = 0, File = { Directory = root } },
|
||||
};
|
||||
|
||||
var result = new AuditLogHashChainOptionsValidator().Validate(null, new AuditLogHashChainOptions { HeadExport = export });
|
||||
|
||||
if (expectedError is null)
|
||||
{
|
||||
Assert.True(result.Succeeded, result.FailureMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.True(result.Failed);
|
||||
Assert.Contains(expectedError, result.FailureMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
-135
@@ -1,135 +0,0 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport;
|
||||
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Central.HeadExport;
|
||||
|
||||
/// <summary>
|
||||
/// The two off-box chain-head sinks (ScadaBridge#107 item 1): the append-only JSON-lines file
|
||||
/// sink, and RFC 5424 syslog over UDP and TCP (octet-counted framing).
|
||||
/// </summary>
|
||||
public sealed class AuditChainHeadSinkTests : IDisposable
|
||||
{
|
||||
private readonly DirectoryInfo _dir = Directory.CreateTempSubdirectory("head-sink-");
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose() => _dir.Delete(recursive: true);
|
||||
|
||||
private static AuditChainHeadRecord Record(string month, long seq) => new(
|
||||
1, month, seq, new string('a', 64), null, null, seq, 0,
|
||||
new DateTime(2026, 5, 3, 0, 0, 0, DateTimeKind.Utc), new DateTime(2026, 5, 3, 1, 0, 0, DateTimeKind.Utc), "central-a");
|
||||
|
||||
private sealed class FixedClock(DateTimeOffset now) : TimeProvider
|
||||
{
|
||||
public override DateTimeOffset GetUtcNow() => now;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes append to the node's file for the day — never truncate it — one JSON record per
|
||||
/// line, and a pre-existing line survives.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task FileSink_AppendsJsonLines_ToThePerNodePerDayFile_WithoutTruncating()
|
||||
{
|
||||
var clock = new FixedClock(new DateTimeOffset(2026, 5, 3, 1, 0, 0, TimeSpan.Zero));
|
||||
var sink = new FileAuditChainHeadSink(_dir.FullName, "central-a", clock);
|
||||
var path = sink.PathFor(clock.GetUtcNow().UtcDateTime);
|
||||
Assert.Equal(Path.Combine(_dir.FullName, "2026", "audit-chain-heads-2026-05-03-central-a.jsonl"), path);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
await File.WriteAllTextAsync(path, "earlier line\n");
|
||||
|
||||
await sink.WriteAsync([Record("2026-04", 3), Record("2026-05", 7)], default);
|
||||
await sink.WriteAsync([Record("2026-05", 9)], default);
|
||||
|
||||
var lines = await File.ReadAllLinesAsync(path);
|
||||
Assert.Equal(4, lines.Length);
|
||||
Assert.Equal("earlier line", lines[0]);
|
||||
var parsed = lines.Skip(1).Select(l => JsonSerializer.Deserialize<AuditChainHeadRecord>(l, new JsonSerializerOptions(JsonSerializerDefaults.Web))!).ToList();
|
||||
Assert.Equal([3L, 7L, 9L], parsed.Select(r => r.LastSeq));
|
||||
Assert.Equal(Record("2026-05", 9), parsed[2]);
|
||||
}
|
||||
|
||||
/// <summary>A relative export directory is refused.</summary>
|
||||
[Fact]
|
||||
public void FileSink_RejectsARelativeDirectory()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new FileAuditChainHeadSink("relative/dir", "central-a"));
|
||||
}
|
||||
|
||||
/// <summary>UDP: one RFC 5424 datagram per record — PRI, version, MSGID, nil SD, JSON body.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task SyslogSink_Udp_SendsOneRfc5424DatagramPerRecord()
|
||||
{
|
||||
using var server = new UdpClient(new IPEndPoint(IPAddress.Loopback, 0));
|
||||
var port = ((IPEndPoint)server.Client.LocalEndPoint!).Port;
|
||||
var sink = new SyslogAuditChainHeadSink(
|
||||
new AuditChainHeadSyslogSinkOptions { Host = "127.0.0.1", Port = port, Protocol = AuditSyslogProtocol.Udp, Facility = 13 },
|
||||
hostName: "central a", time: new FixedClock(new DateTimeOffset(2026, 5, 3, 1, 2, 3, 456, TimeSpan.Zero)));
|
||||
|
||||
await sink.WriteAsync([Record("2026-04", 3), Record("2026-05", 7)], default);
|
||||
|
||||
var first = Encoding.UTF8.GetString((await server.ReceiveAsync().WaitAsync(TimeSpan.FromSeconds(5))).Buffer);
|
||||
var second = Encoding.UTF8.GetString((await server.ReceiveAsync().WaitAsync(TimeSpan.FromSeconds(5))).Buffer);
|
||||
// facility 13 (log audit) * 8 + severity 5 (notice) = 109; the space in the host name is dropped.
|
||||
Assert.StartsWith($"<109>1 2026-05-03T01:02:03.456Z centrala scadabridge {Environment.ProcessId} chainhead - {{", first);
|
||||
AssertRecordBody(first, 3);
|
||||
AssertRecordBody(second, 7);
|
||||
}
|
||||
|
||||
/// <summary>TCP: octet-counted frames (RFC 6587), one per record, on one connection.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task SyslogSink_Tcp_SendsOctetCountedFrames()
|
||||
{
|
||||
var listener = new TcpListener(IPAddress.Loopback, 0);
|
||||
listener.Start();
|
||||
try
|
||||
{
|
||||
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
|
||||
var received = Task.Run(async () =>
|
||||
{
|
||||
using var client = await listener.AcceptTcpClientAsync();
|
||||
using var ms = new MemoryStream();
|
||||
await client.GetStream().CopyToAsync(ms);
|
||||
return ms.ToArray();
|
||||
});
|
||||
var sink = new SyslogAuditChainHeadSink(
|
||||
new AuditChainHeadSyslogSinkOptions { Host = "127.0.0.1", Port = port, Protocol = AuditSyslogProtocol.Tcp });
|
||||
|
||||
await sink.WriteAsync([Record("2026-04", 3), Record("2026-05", 7)], default);
|
||||
|
||||
var bytes = await received.WaitAsync(TimeSpan.FromSeconds(5));
|
||||
var frames = new List<string>();
|
||||
var i = 0;
|
||||
while (i < bytes.Length)
|
||||
{
|
||||
var space = Array.IndexOf(bytes, (byte)' ', i);
|
||||
var length = int.Parse(Encoding.ASCII.GetString(bytes, i, space - i));
|
||||
frames.Add(Encoding.UTF8.GetString(bytes, space + 1, length));
|
||||
i = space + 1 + length;
|
||||
}
|
||||
|
||||
Assert.Equal(2, frames.Count);
|
||||
Assert.All(frames, f => Assert.StartsWith("<109>1 ", f));
|
||||
AssertRecordBody(frames[0], 3);
|
||||
AssertRecordBody(frames[1], 7);
|
||||
}
|
||||
finally
|
||||
{
|
||||
listener.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AssertRecordBody(string message, long seq)
|
||||
{
|
||||
var json = message[message.IndexOf('{')..];
|
||||
var record = JsonSerializer.Deserialize<AuditChainHeadRecord>(json, new JsonSerializerOptions(JsonSerializerDefaults.Web))!;
|
||||
Assert.Equal(seq, record.LastSeq);
|
||||
Assert.Equal(new string('a', 64), record.LastHash);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using ZB.MOM.WW.ScadaBridge.AuditLog.Central;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Central;
|
||||
|
||||
/// <summary>
|
||||
/// The hash chain and the maintenance login were removed on 2026-11-15; a node still carrying
|
||||
/// their settings (DEV/QA have <c>AuditLog__HashChain__Enabled=true</c> in the Central service
|
||||
/// env) must boot and be told the keys are ignored.
|
||||
/// </summary>
|
||||
public class RetiredAuditSettingsWarningTests
|
||||
{
|
||||
private static IConfiguration Config(params (string Key, string Value)[] values) =>
|
||||
new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(values.Select(v => new KeyValuePair<string, string?>(v.Key, v.Value)))
|
||||
.Build();
|
||||
|
||||
/// <summary>The env var left on DEV/QA (and the maintenance login) are both reported.</summary>
|
||||
[Fact]
|
||||
public void FindRetired_ReportsTheHashChainSection_AndTheMaintenanceConnectionString()
|
||||
{
|
||||
var config = Config(
|
||||
("AuditLog:HashChain:Enabled", "true"),
|
||||
("AuditLog:MaintenanceConnectionString", "Server=x;Database=y"));
|
||||
|
||||
Assert.Equal(
|
||||
[RetiredAuditSettingsWarning.HashChainSection, RetiredAuditSettingsWarning.MaintenanceConnectionStringKey],
|
||||
RetiredAuditSettingsWarning.FindRetired(config));
|
||||
}
|
||||
|
||||
/// <summary>A clean configuration (and the archive settings that remain) reports nothing.</summary>
|
||||
[Fact]
|
||||
public void FindRetired_ReportsNothing_ForCurrentSettings()
|
||||
{
|
||||
var config = Config(
|
||||
("AuditLog:Archive:Enabled", "true"),
|
||||
("AuditLog:RetentionDays", "365"),
|
||||
("AuditLog:MaintenanceConnectionString", " "));
|
||||
|
||||
Assert.Empty(RetiredAuditSettingsWarning.FindRetired(config));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user