diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogArchiveOptionsValidator.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogArchiveOptionsValidator.cs index abe0b567..ccb6872b 100644 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogArchiveOptionsValidator.cs +++ b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogArchiveOptionsValidator.cs @@ -30,8 +30,6 @@ public sealed class AuditLogArchiveOptionsValidator : OptionsValidatorBase 0, $"AuditLog:Archive:{nameof(AuditLogArchiveOptions.ReadCommandTimeoutMinutes)} ({options.ReadCommandTimeoutMinutes}) must be > 0."); - builder.RequireThat(options.ChainLockTimeoutSeconds > 0, - $"AuditLog:Archive:{nameof(AuditLogArchiveOptions.ChainLockTimeoutSeconds)} ({options.ChainLockTimeoutSeconds}) must be > 0."); builder.RequireThat(options.RetentionDays is >= 0 and <= 36_500, $"AuditLog:Archive:{nameof(AuditLogArchiveOptions.RetentionDays)} ({options.RetentionDays}) must be between 0 (never delete) and 36500."); } diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogHashChainOptions.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogHashChainOptions.cs deleted file mode 100644 index 66e801a5..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogHashChainOptions.cs +++ /dev/null @@ -1,99 +0,0 @@ -using ZB.MOM.WW.Configuration; -using ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central; - -/// -/// Options for the central (section -/// AuditLog:HashChain) — hash-chain tamper evidence for the AuditLog -/// (audit roadmap T1, alog.md §11.4, ScadaBridge#41). -/// -/// -/// -/// Default OFF (alog.md §11.4: "Default off … to avoid operational burden"). When -/// enabled, both central nodes run the job; the SQL application lock means only one of -/// them chains at a time, so a dual-active pair still produces one gap-free chain per month. -/// -/// -/// Chain start (ScadaBridge#139). Enabling it on an existing database does NOT chain the -/// historical backlog: the first chaining pass records a chain-start anchor in the database at -/// the database's current UTC time, and only rows dated from then on are chained. Older rows -/// stay unchained and are reported by audit verify-chain as pre-cutoff rows, never as a -/// break. The anchor is written once and is the same for both central nodes. Set -/// to chain the backlog instead (only effective on the very first -/// enablement). Run audit backfill-source-node BEFORE enabling either way: a chained -/// row's SourceNode is frozen. -/// -/// -public sealed class AuditLogHashChainOptions -{ - /// Whether the chaining job runs (default false). - public bool Enabled { get; set; } - - /// - /// Chain the historical backlog on first enablement instead of starting the chain at the - /// enablement instant (default false — owner decision on ScadaBridge#107, 2026-11-13: no - /// backlog chaining). Only read while nothing has been chained yet: once the database holds - /// a chain-start anchor or any chained row, the decision is fixed and this value is ignored. - /// Set it the same on both central nodes; if they differ, whichever chains first decides. - /// - public bool ChainBacklog { get; set; } - - /// Seconds between chaining ticks (default 60). - public int IntervalSeconds { get; set; } = 60; - - /// Rows linked per transaction (default 1000). - public int BatchSize { get; set; } = 1000; - - /// Upper bound on batches per tick so a backlog yields between ticks (default 100). - public int MaxBatchesPerTick { get; set; } = 100; - - /// - /// Off-box export of the per-month chain heads (AuditLog:HashChain:HeadExport, - /// ScadaBridge#107 item 1) — the chain's external anchor. Default off. - /// - public AuditChainHeadExportOptions HeadExport { get; set; } = new(); -} - -/// Validates at host startup. -public sealed class AuditLogHashChainOptionsValidator : OptionsValidatorBase -{ - /// - protected override void Validate(ValidationBuilder builder, AuditLogHashChainOptions options) - { - builder.RequireThat(options.IntervalSeconds > 0, - $"AuditLog:HashChain:{nameof(AuditLogHashChainOptions.IntervalSeconds)} ({options.IntervalSeconds}) must be > 0."); - builder.RequireThat(options.BatchSize is > 0 and <= 10_000, - $"AuditLog:HashChain:{nameof(AuditLogHashChainOptions.BatchSize)} ({options.BatchSize}) must be between 1 and 10000."); - builder.RequireThat(options.MaxBatchesPerTick > 0, - $"AuditLog:HashChain:{nameof(AuditLogHashChainOptions.MaxBatchesPerTick)} ({options.MaxBatchesPerTick}) must be > 0."); - - var export = options.HeadExport ?? new AuditChainHeadExportOptions(); - if (!export.Enabled) - { - return; - } - - const string section = "AuditLog:HashChain:HeadExport"; - builder.RequireThat(export.IntervalMinutes > 0, - $"{section}:IntervalMinutes ({export.IntervalMinutes}) must be > 0."); - if (export.Sink == AuditChainHeadSinkKind.File) - { - var dir = export.File?.Directory; - builder.RequireThat(!string.IsNullOrWhiteSpace(dir) && Path.IsPathFullyQualified(dir), - $"{section}:File:Directory ('{dir}') must be an absolute path (local or UNC) when Sink=File."); - } - else - { - var syslog = export.Syslog ?? new AuditChainHeadSyslogSinkOptions(); - builder.RequireThat(!string.IsNullOrWhiteSpace(syslog.Host), - $"{section}:Syslog:Host is required when Sink=Syslog."); - builder.RequireThat(syslog.Port is > 0 and <= 65535, - $"{section}:Syslog:Port ({syslog.Port}) must be between 1 and 65535."); - builder.RequireThat(syslog.Facility is >= 0 and <= 23, - $"{section}:Syslog:Facility ({syslog.Facility}) must be between 0 and 23."); - builder.RequireThat(syslog.TimeoutSeconds > 0, - $"{section}:Syslog:TimeoutSeconds ({syslog.TimeoutSeconds}) must be > 0."); - } - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogHashChainService.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogHashChainService.cs deleted file mode 100644 index df2f148c..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogHashChainService.cs +++ /dev/null @@ -1,151 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central; - -/// -/// Central hosted service that links newly written AuditLog rows into their month's -/// SHA-256 hash chain (audit roadmap T1, alog.md §11.4, ScadaBridge#41). The chaining -/// job of alog.md §11.4, run incrementally — every -/// rather than only once a month has closed, so a row is tamper-evident within about a -/// minute of reaching central instead of up to a month later. -/// -/// -/// -/// Same shape as : a background loop, a -/// fresh DI scope per tick, every failure logged and contained. Runs on BOTH central nodes; -/// takes an exclusive SQL application -/// lock with a zero timeout, so the peer's pass skips instead of racing (and correctness -/// does not depend on Akka's view of which node is active). -/// -/// -public sealed class AuditLogHashChainService : IHostedService, IDisposable -{ - private readonly IServiceScopeFactory _scopeFactory; - private readonly IOptions _options; - private readonly ILogger _logger; - private CancellationTokenSource? _cts; - private Task? _loop; - - /// Initializes the service. - /// Scope factory used to resolve the scoped chain implementation per tick. - /// Hash-chain options. - /// Logger. - public AuditLogHashChainService( - IServiceScopeFactory scopeFactory, - IOptions options, - ILogger logger) - { - _scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); - _options = options ?? throw new ArgumentNullException(nameof(options)); - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - } - - /// - public Task StartAsync(CancellationToken cancellationToken) - { - if (!_options.Value.Enabled) - { - _logger.LogInformation("AuditLog hash chain disabled (AuditLog:HashChain:Enabled=false); no chaining job started."); - return Task.CompletedTask; - } - - var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - _cts = cts; - var token = cts.Token; - _loop = Task.Run(() => RunLoopAsync(token), CancellationToken.None); - return Task.CompletedTask; - } - - private async Task RunLoopAsync(CancellationToken ct) - { - while (!ct.IsCancellationRequested) - { - await TickAsync(ct).ConfigureAwait(false); - try - { - await Task.Delay(TimeSpan.FromSeconds(_options.Value.IntervalSeconds), ct).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - break; - } - } - } - - /// - /// Runs one tick: chains batches until the backlog is empty, the peer holds the lock, - /// or is reached. - /// - /// Cancellation token. - /// The number of rows chained in this tick. - internal async Task TickAsync(CancellationToken ct) - { - long total = 0; - try - { - var options = _options.Value; - await using var scope = _scopeFactory.CreateAsyncScope(); - var chain = scope.ServiceProvider.GetRequiredService(); - for (var i = 0; i < options.MaxBatchesPerTick && !ct.IsCancellationRequested; i++) - { - var result = await chain.ChainPendingAsync(options.BatchSize, options.ChainBacklog, ct).ConfigureAwait(false); - total += result.RowsChained; - if (result.LockBusy || result.RowsChained == 0) - { - break; - } - } - - if (total > 0) - { - _logger.LogDebug("AuditLog hash chain linked {Rows} rows this tick.", total); - } - } - catch (OperationCanceledException) when (ct.IsCancellationRequested) - { - // Shutdown. - } - catch (Exception ex) - { - // Contained: a transient SQL failure must not end the loop; the next tick - // resumes from wherever the last committed pass left the chain. - _logger.LogError(ex, "AuditLog hash-chain tick failed after linking {Rows} rows.", total); - } - - return total; - } - - /// - public Task StopAsync(CancellationToken cancellationToken) - { - try - { - _cts?.Cancel(); - } - catch (ObjectDisposedException) - { - // Dispose already cancelled the loop. - } - - return _loop ?? Task.CompletedTask; - } - - /// - public void Dispose() - { - try - { - _cts?.Cancel(); - } - catch (ObjectDisposedException) - { - // Idempotent. - } - - _cts?.Dispose(); - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogPurgeActor.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogPurgeActor.cs index bdfec5e2..815ee4ad 100644 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogPurgeActor.cs +++ b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/AuditLogPurgeActor.cs @@ -49,9 +49,7 @@ namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central; /// /// /// DI scopes. is a scoped EF Core -/// service registered by AddConfigurationDatabase — the -/// -keyed one, on the AuditLog maintenance -/// connection (ScadaBridge#107 item 3), when registered. The singleton +/// service registered by AddConfigurationDatabase. The singleton /// opens one DI scope per tick and reuses the same repository across every /// boundary in that tick — mirrors the /// pattern. @@ -152,11 +150,7 @@ public class AuditLogPurgeActor : ReceiveActor IAuditLogRepository repository; try { - // The maintenance-connection repository when the host registered one - // (AddConfigurationDatabase, ScadaBridge#107 item 3 — on the separate maintenance - // login if AuditLog:MaintenanceConnectionString is set), else the plain one. - repository = scope.ServiceProvider.GetKeyedService(AuditMaintenanceContext.ServiceKey) - ?? scope.ServiceProvider.GetRequiredService(); + repository = scope.ServiceProvider.GetRequiredService(); } catch (Exception ex) { @@ -210,8 +204,7 @@ public class AuditLogPurgeActor : ReceiveActor { try { - archive = scope.ServiceProvider.GetKeyedService(AuditMaintenanceContext.ServiceKey) - ?? scope.ServiceProvider.GetRequiredService(); + archive = scope.ServiceProvider.GetRequiredService(); } catch (Exception ex) { @@ -305,8 +298,7 @@ public class AuditLogPurgeActor : ReceiveActor return; } - archive = services.GetKeyedService(AuditMaintenanceContext.ServiceKey) - ?? services.GetRequiredService(); + archive = services.GetRequiredService(); } var result = await archive.CleanupAsync(dryRun: false).ConfigureAwait(false); diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportOptions.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportOptions.cs deleted file mode 100644 index cf00b8d5..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportOptions.cs +++ /dev/null @@ -1,87 +0,0 @@ -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -/// Where exported chain heads go. -public enum AuditChainHeadSinkKind -{ - /// Append-only JSON-lines files in a directory (local or UNC). - File, - - /// RFC 5424 syslog over UDP or TCP. - Syslog, -} - -/// Syslog transport. -public enum AuditSyslogProtocol -{ - /// One datagram per record (RFC 5426). - Udp, - - /// Octet-counted frames on one TCP connection per export (RFC 6587). - Tcp, -} - -/// -/// Off-box export of the AuditLog hash-chain heads (section AuditLog:HashChain:HeadExport, -/// ScadaBridge#107 item 1). Default OFF. Every both central -/// nodes read each month's chain head (month, last sequence + hash, partition-purge anchor, -/// chained-row and tombstone counts) and append the heads that changed since their previous -/// export to a sink the database login cannot write. audit verify-heads later compares -/// those records with the database: a chain someone rewrote and re-hashed inside SQL Server no -/// longer carries the hash that was exported. -/// -public sealed class AuditChainHeadExportOptions -{ - /// Whether the export runs (default false). - public bool Enabled { get; set; } - - /// Minutes between exports (default 60). - public int IntervalMinutes { get; set; } = 60; - - /// - /// Re-export unchanged heads every tick too (default false: only heads whose sequence, - /// anchor or counts moved since this node's previous export, plus every head on the first - /// tick after start). - /// - public bool ExportUnchanged { get; set; } - - /// Which sink (default ). - public AuditChainHeadSinkKind Sink { get; set; } = AuditChainHeadSinkKind.File; - - /// File sink settings. - public AuditChainHeadFileSinkOptions File { get; set; } = new(); - - /// Syslog sink settings. - public AuditChainHeadSyslogSinkOptions Syslog { get; set; } = new(); -} - -/// File sink: {Directory}/{yyyy}/audit-chain-heads-{yyyy-MM-dd}-{node}.jsonl, opened for append only. -public sealed class AuditChainHeadFileSinkOptions -{ - /// - /// Absolute directory (local path or UNC share). Give the service account create + append - /// rights only; the SQL Server login has no access to it by construction. - /// - public string? Directory { get; set; } -} - -/// Syslog sink (RFC 5424). -public sealed class AuditChainHeadSyslogSinkOptions -{ - /// Collector host name or address. - public string? Host { get; set; } - - /// Collector port (default 514). - public int Port { get; set; } = 514; - - /// Transport (default UDP). - public AuditSyslogProtocol Protocol { get; set; } = AuditSyslogProtocol.Udp; - - /// Syslog facility 0–23 (default 13, "log audit"). - public int Facility { get; set; } = 13; - - /// APP-NAME field (default scadabridge). - public string AppName { get; set; } = "scadabridge"; - - /// Send/connect timeout in seconds (default 10). - public int TimeoutSeconds { get; set; } = 10; -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportService.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportService.cs deleted file mode 100644 index 999f304d..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportService.cs +++ /dev/null @@ -1,205 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; -using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -/// -/// Central hosted service that exports the AuditLog hash-chain heads off-box -/// (AuditLog:HashChain:HeadExport, ScadaBridge#107 item 1). Default off. -/// -/// -/// -/// Runs on both central nodes (each record names its node). A tick reads every month's head in -/// one consistent snapshot (, chain lock -/// SHARED) and writes the heads that changed since this node's last successful export — all of -/// them on the first tick after start — to the configured . -/// A failed write is logged and retried on the next tick with the same heads (the in-memory -/// "last exported" state only advances on success). No signing: the protection is that the -/// sink sits outside the database login's reach. -/// -/// -public sealed class AuditChainHeadExportService : IHostedService, IDisposable -{ - private readonly IServiceScopeFactory _scopeFactory; - private readonly AuditChainHeadExportOptions _options; - private readonly IAuditChainHeadSink? _sink; - private readonly string? _node; - private readonly TimeProvider _time; - private readonly ILogger _logger; - private readonly AuditChainHeadExportStatus _status; - private readonly Dictionary _lastExported = new(); - private CancellationTokenSource? _cts; - private Task? _loop; - - /// Initializes the service. - /// Scope factory for the scoped head store. - /// Hash-chain options (the HeadExport sub-section is used). - /// The sink (null when the export is disabled). - /// This node's name for the records. - /// Logger. - /// Clock. - /// Health status the tick outcome is recorded on (ScadaBridge#115). - public AuditChainHeadExportService( - IServiceScopeFactory scopeFactory, - IOptions options, - IAuditChainHeadSink? sink, - string? nodeName, - ILogger logger, - TimeProvider? time = null, - AuditChainHeadExportStatus? status = null) - { - _status = status ?? new AuditChainHeadExportStatus(); - _scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); - _options = (options ?? throw new ArgumentNullException(nameof(options))).Value.HeadExport; - _sink = sink; - _node = nodeName; - _logger = logger ?? throw new ArgumentNullException(nameof(logger)); - _time = time ?? TimeProvider.System; - } - - /// - public Task StartAsync(CancellationToken cancellationToken) - { - if (!_options.Enabled || _sink is null) - { - _logger.LogInformation("AuditLog chain-head export disabled (AuditLog:HashChain:HeadExport:Enabled=false)."); - return Task.CompletedTask; - } - - _status.MarkStarted(_options.IntervalMinutes, _time.GetUtcNow().UtcDateTime); - _logger.LogInformation("AuditLog chain-head export every {Minutes} min to {Sink}.", _options.IntervalMinutes, _sink.Describe); - var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); - _cts = cts; - var token = cts.Token; - _loop = Task.Run(() => RunLoopAsync(token), CancellationToken.None); - return Task.CompletedTask; - } - - private async Task RunLoopAsync(CancellationToken ct) - { - while (!ct.IsCancellationRequested) - { - await TickAsync(ct).ConfigureAwait(false); - try - { - await Task.Delay(TimeSpan.FromMinutes(_options.IntervalMinutes), ct).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - break; - } - } - } - - /// Runs one export tick. - /// Cancellation token. - /// The number of records written (0 on failure or nothing changed). - internal async Task TickAsync(CancellationToken ct) - { - if (_sink is null) - { - return 0; - } - - try - { - IReadOnlyList heads; - await using (var scope = _scopeFactory.CreateAsyncScope()) - { - heads = await scope.ServiceProvider.GetRequiredService() - .ReadHeadsAsync(ct).ConfigureAwait(false); - } - - var now = _time.GetUtcNow().UtcDateTime; - var changed = heads - .Where(h => _options.ExportUnchanged - || !_lastExported.TryGetValue(h.Month, out var last) - || last != (h.LastSeq, h.PurgedThroughSeq, h.ChainedRows + h.Tombstones)) - .ToList(); - if (changed.Count == 0) - { - _status.RecordSuccess(now); - return 0; - } - - var records = changed.Select(h => ToRecord(h, now, _node)).ToList(); - await _sink.WriteAsync(records, ct).ConfigureAwait(false); - foreach (var h in changed) - { - _lastExported[h.Month] = (h.LastSeq, h.PurgedThroughSeq, h.ChainedRows + h.Tombstones); - } - - _status.RecordSuccess(now); - _logger.LogDebug("Exported {Count} AuditLog chain head(s) to {Sink}.", records.Count, _sink.Describe); - return records.Count; - } - catch (OperationCanceledException) when (ct.IsCancellationRequested) - { - return 0; - } - catch (Exception ex) - { - // Contained: the next tick retries the same heads (nothing was marked exported). - _logger.LogError(ex, "AuditLog chain-head export to {Sink} failed; retrying next tick.", _sink.Describe); - _status.RecordFailure(_time.GetUtcNow().UtcDateTime, ex.Message); - return 0; - } - } - - /// Maps a database head to its exported record. - /// The head. - /// Export time. - /// Exporting node. - /// The record. - public static AuditChainHeadRecord ToRecord(AuditChainHeadSnapshot head, DateTime exportedAtUtc, string? node) - { - ArgumentNullException.ThrowIfNull(head); - return new AuditChainHeadRecord( - 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, - exportedAtUtc, - node); - } - - /// - public Task StopAsync(CancellationToken cancellationToken) - { - try - { - _cts?.Cancel(); - } - catch (ObjectDisposedException) - { - // Dispose already cancelled the loop. - } - - return _loop ?? Task.CompletedTask; - } - - /// - public void Dispose() - { - try - { - _cts?.Cancel(); - } - catch (ObjectDisposedException) - { - // Idempotent. - } - - _cts?.Dispose(); - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportStatus.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportStatus.cs deleted file mode 100644 index e323256c..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadExportStatus.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System.Diagnostics.Metrics; -using ZB.MOM.WW.ScadaBridge.Commons.Observability; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -/// -/// Health signal of the off-box chain-head export (ScadaBridge#115). Updated by -/// on every tick; read by the Host's -/// audit-chain-head-export /health/ready check. A sink that stays down used to be a -/// log line only, silently stopping the external anchor. -/// -public sealed class AuditChainHeadExportStatus -{ - private static readonly Meter Meter = new(ScadaBridgeTelemetry.MeterName); - - private static readonly Counter FailuresCounter = - Meter.CreateCounter("scadabridge.audit.chain_head_export.failures", unit: "1", - description: "AuditLog chain-head export ticks that failed to read or write the heads (sink offline / unreachable); retried next tick."); - - private readonly object _gate = new(); - private long _totalFailures; - private int _consecutiveFailures; - private DateTime? _lastSuccessUtc; - private DateTime? _lastFailureUtc; - private string? _lastError; - private DateTime? _startedUtc; - - /// Whether the export is running at all (false = disabled, nothing to report). - public bool Enabled { get; private set; } - - /// Minutes between export ticks (for the staleness test). - public int IntervalMinutes { get; private set; } = 60; - - /// Total failed ticks since process start. - public long TotalFailures { get { lock (_gate) { return _totalFailures; } } } - - /// Failed ticks since the last success. - public int ConsecutiveFailures { get { lock (_gate) { return _consecutiveFailures; } } } - - /// UTC time of the last successful tick, or null. - public DateTime? LastSuccessUtc { get { lock (_gate) { return _lastSuccessUtc; } } } - - /// UTC time of the last failed tick, or null. - public DateTime? LastFailureUtc { get { lock (_gate) { return _lastFailureUtc; } } } - - /// Message of the last failure (never a secret: the exception message only), or null. - public string? LastError { get { lock (_gate) { return _lastError; } } } - - /// UTC time the export loop started, or null. - public DateTime? StartedUtc { get { lock (_gate) { return _startedUtc; } } } - - /// Marks the export as running. - /// Tick interval. - /// Start time. - public void MarkStarted(int intervalMinutes, DateTime nowUtc) - { - lock (_gate) - { - Enabled = true; - IntervalMinutes = intervalMinutes; - _startedUtc = nowUtc; - } - } - - /// Records a successful tick. - /// Time of the tick. - public void RecordSuccess(DateTime nowUtc) - { - lock (_gate) - { - _consecutiveFailures = 0; - _lastSuccessUtc = nowUtc; - } - } - - /// Records a failed tick and bumps the failure counter metric. - /// Time of the tick. - /// The failure message. - public void RecordFailure(DateTime nowUtc, string error) - { - lock (_gate) - { - _totalFailures++; - _consecutiveFailures++; - _lastFailureUtc = nowUtc; - _lastError = error; - } - - FailuresCounter.Add(1); - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadJson.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadJson.cs deleted file mode 100644 index 51bbc52e..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/AuditChainHeadJson.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System.Text.Json; -using System.Text.Json.Serialization; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -/// The one JSON shape every sink writes (and audit verify-heads reads). -public static class AuditChainHeadJson -{ - /// camelCase, compact, nulls kept so every record has every field. - public static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web) - { - WriteIndented = false, - DefaultIgnoreCondition = JsonIgnoreCondition.Never, - }; - - /// Serialises one record to a single line. - /// The record. - /// Compact JSON. - public static string Serialize(AuditChainHeadRecord record) => JsonSerializer.Serialize(record, Options); -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/FileAuditChainHeadSink.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/FileAuditChainHeadSink.cs deleted file mode 100644 index 9a6c4ba1..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/FileAuditChainHeadSink.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System.Text; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -/// -/// Appends exported heads as JSON lines to {Directory}/{yyyy}/audit-chain-heads-{yyyy-MM-dd}-{node}.jsonl. -/// -/// -/// The file is only ever opened with — never truncated, rewritten -/// or deleted by ScadaBridge — so the share can grant the service account create + append data -/// only. One file per node per UTC day keeps the two central nodes from interleaving writes in -/// one file. Each batch is flushed to disk before the call returns. -/// -public sealed class FileAuditChainHeadSink : IAuditChainHeadSink -{ - private readonly string _directory; - private readonly string _node; - private readonly TimeProvider _time; - - /// Creates the sink. - /// Absolute export directory. - /// This node's name (part of the file name). - /// Clock (file-per-day naming). - public FileAuditChainHeadSink(string directory, string node, TimeProvider? time = null) - { - ArgumentException.ThrowIfNullOrWhiteSpace(directory); - if (!Path.IsPathFullyQualified(directory)) - { - throw new ArgumentException($"Head-export directory '{directory}' must be absolute.", nameof(directory)); - } - - _directory = directory; - _node = SafeName(string.IsNullOrWhiteSpace(node) ? "node" : node); - _time = time ?? TimeProvider.System; - } - - /// - public string Describe => _directory; - - /// The file a write at lands in. - /// The write time. - /// The full path. - public string PathFor(DateTime nowUtc) => Path.Combine( - _directory, nowUtc.ToString("yyyy", System.Globalization.CultureInfo.InvariantCulture), - $"audit-chain-heads-{nowUtc:yyyy-MM-dd}-{_node}.jsonl"); - - /// - public async Task WriteAsync(IReadOnlyList records, CancellationToken ct) - { - ArgumentNullException.ThrowIfNull(records); - if (records.Count == 0) - { - return; - } - - var path = PathFor(_time.GetUtcNow().UtcDateTime); - Directory.CreateDirectory(Path.GetDirectoryName(path)!); - - var text = new StringBuilder(); - foreach (var record in records) - { - text.Append(AuditChainHeadJson.Serialize(record)).Append('\n'); - } - - var bytes = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false).GetBytes(text.ToString()); - await using var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, useAsync: true); - await stream.WriteAsync(bytes, ct).ConfigureAwait(false); - await stream.FlushAsync(ct).ConfigureAwait(false); - stream.Flush(flushToDisk: true); - } - - private static string SafeName(string value) - { - var invalid = Path.GetInvalidFileNameChars(); - var chars = value.Select(c => invalid.Contains(c) || c == ' ' ? '_' : c).ToArray(); - return new string(chars); - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/IAuditChainHeadSink.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/IAuditChainHeadSink.cs deleted file mode 100644 index 70935578..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/IAuditChainHeadSink.cs +++ /dev/null @@ -1,16 +0,0 @@ -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -/// An off-box destination for exported chain heads (ScadaBridge#107 item 1). -public interface IAuditChainHeadSink -{ - /// Short description for logs (e.g. the directory or host:port/udp). - string Describe { get; } - - /// Writes the records; throws when they could not all be delivered. - /// The records, oldest month first. - /// Cancellation token. - /// A task that completes when the records are durably handed off. - Task WriteAsync(IReadOnlyList records, CancellationToken ct); -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/SyslogAuditChainHeadSink.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/SyslogAuditChainHeadSink.cs deleted file mode 100644 index 7bb95354..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/HeadExport/SyslogAuditChainHeadSink.cs +++ /dev/null @@ -1,108 +0,0 @@ -using System.Globalization; -using System.Net.Sockets; -using System.Text; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -/// -/// Sends exported heads as RFC 5424 syslog messages — one message per head, the JSON record as -/// MSG — over UDP (RFC 5426, one datagram each) or TCP (RFC 6587 octet-counting framing, one -/// connection per export). -/// -/// -/// Message: <PRI>1 TIMESTAMP HOSTNAME APP-NAME PROCID chainhead - {json}, severity -/// Notice (5), PRI = facility × 8 + 5. No structured data (a private enterprise number would be -/// needed); the collector keeps the JSON body, which audit verify-heads reads back from -/// any line that contains it. UDP is fire-and-forget — use TCP where a lost datagram matters. -/// -public sealed class SyslogAuditChainHeadSink : IAuditChainHeadSink -{ - /// MSGID of every head message. - public const string MessageId = "chainhead"; - - private const int SeverityNotice = 5; - - private readonly AuditChainHeadSyslogSinkOptions _options; - private readonly string _hostName; - private readonly TimeProvider _time; - - /// Creates the sink. - /// Syslog settings. - /// HOSTNAME field (defaults to the machine name). - /// Clock for TIMESTAMP. - public SyslogAuditChainHeadSink(AuditChainHeadSyslogSinkOptions options, string? hostName = null, TimeProvider? time = null) - { - _options = options ?? throw new ArgumentNullException(nameof(options)); - ArgumentException.ThrowIfNullOrWhiteSpace(options.Host); - _hostName = Token(hostName ?? Environment.MachineName, 255); - _time = time ?? TimeProvider.System; - } - - /// - public string Describe => $"{_options.Host}:{_options.Port}/{_options.Protocol.ToString().ToLowerInvariant()}"; - - /// Formats one RFC 5424 message (no framing). - /// The record. - /// The message text. - public string Format(AuditChainHeadRecord record) - { - var pri = (_options.Facility * 8) + SeverityNotice; - var timestamp = _time.GetUtcNow().UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture); - return string.Create(CultureInfo.InvariantCulture, - $"<{pri}>1 {timestamp} {_hostName} {Token(_options.AppName, 48)} {Environment.ProcessId} {MessageId} - {AuditChainHeadJson.Serialize(record)}"); - } - - /// - public async Task WriteAsync(IReadOnlyList records, CancellationToken ct) - { - ArgumentNullException.ThrowIfNull(records); - if (records.Count == 0) - { - return; - } - - using var timeout = CancellationTokenSource.CreateLinkedTokenSource(ct); - timeout.CancelAfter(TimeSpan.FromSeconds(Math.Max(1, _options.TimeoutSeconds))); - var token = timeout.Token; - - if (_options.Protocol == AuditSyslogProtocol.Udp) - { - using var udp = new UdpClient(); - udp.Connect(_options.Host!, _options.Port); - foreach (var record in records) - { - var bytes = Encoding.UTF8.GetBytes(Format(record)); - await udp.SendAsync(bytes, token).ConfigureAwait(false); - } - - return; - } - - using var tcp = new TcpClient(); - await tcp.ConnectAsync(_options.Host!, _options.Port, token).ConfigureAwait(false); - var stream = tcp.GetStream(); - foreach (var record in records) - { - var message = Encoding.UTF8.GetBytes(Format(record)); - var frame = Encoding.ASCII.GetBytes(message.Length.ToString(CultureInfo.InvariantCulture) + " "); - await stream.WriteAsync(frame, token).ConfigureAwait(false); - await stream.WriteAsync(message, token).ConfigureAwait(false); - } - - await stream.FlushAsync(token).ConfigureAwait(false); - tcp.Client.Shutdown(SocketShutdown.Send); - } - - // RFC 5424 header fields are PRINTUSASCII (33-126), no spaces; "-" when empty. - private static string Token(string? value, int max) - { - if (string.IsNullOrEmpty(value)) - { - return "-"; - } - - var chars = value.Where(c => c is >= '!' and <= '~').Take(max).ToArray(); - return chars.Length == 0 ? "-" : new string(chars); - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/RetiredAuditSettingsWarning.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/RetiredAuditSettingsWarning.cs new file mode 100644 index 00000000..557a5bc5 --- /dev/null +++ b/src/ZB.MOM.WW.ScadaBridge.AuditLog/Central/RetiredAuditSettingsWarning.cs @@ -0,0 +1,73 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace ZB.MOM.WW.ScadaBridge.AuditLog.Central; + +/// +/// Logs one warning at central startup for each AuditLog setting that no longer does anything. +/// +/// +/// The hash chain (AuditLog:HashChain, including its head export) and the two-login +/// maintenance hardening (AuditLog:MaintenanceConnectionString) were removed on +/// 2026-11-15 at IT security's request. Nothing validates unknown keys, so a node still carrying +/// them (for example AuditLog__HashChain__Enabled=true in its service environment) boots +/// normally; this warning tells the operator the keys can be deleted. +/// +public sealed class RetiredAuditSettingsWarning : IHostedService +{ + /// The retired hash-chain section. + public const string HashChainSection = "AuditLog:HashChain"; + + /// The retired maintenance-login connection string. + public const string MaintenanceConnectionStringKey = "AuditLog:MaintenanceConnectionString"; + + private readonly IConfiguration _configuration; + private readonly ILogger _logger; + + /// Initializes a new instance of the class. + /// Application configuration. + /// Logger. + public RetiredAuditSettingsWarning(IConfiguration configuration, ILogger logger) + { + _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + /// The retired keys present in . + /// The configuration to inspect. + /// Each retired key or section that is set. + public static IReadOnlyList FindRetired(IConfiguration configuration) + { + ArgumentNullException.ThrowIfNull(configuration); + var found = new List(); + if (configuration.GetSection(HashChainSection).Exists()) + { + found.Add(HashChainSection); + } + + if (!string.IsNullOrWhiteSpace(configuration[MaintenanceConnectionStringKey])) + { + found.Add(MaintenanceConnectionStringKey); + } + + return found; + } + + /// + public Task StartAsync(CancellationToken cancellationToken) + { + foreach (var key in FindRetired(_configuration)) + { + _logger.LogWarning( + "{Setting} is no longer supported and is ignored: the AuditLog hash chain and the two-login " + + "maintenance hardening were removed on 2026-11-15. Delete the setting.", + key); + } + + return Task.CompletedTask; + } + + /// + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +} diff --git a/src/ZB.MOM.WW.ScadaBridge.AuditLog/ServiceCollectionExtensions.cs b/src/ZB.MOM.WW.ScadaBridge.AuditLog/ServiceCollectionExtensions.cs index c41fdbe0..a1b2c17e 100644 --- a/src/ZB.MOM.WW.ScadaBridge.AuditLog/ServiceCollectionExtensions.cs +++ b/src/ZB.MOM.WW.ScadaBridge.AuditLog/ServiceCollectionExtensions.cs @@ -52,15 +52,6 @@ public static class ServiceCollectionExtensions /// Configuration section bound to . public const string PartitionMaintenanceSectionName = "AuditLog:PartitionMaintenance"; - /// - /// Optional connection string for the AuditLog maintenance paths — the hash-chain job and both - /// purge paths (ScadaBridge#107 item 3). Unset: they run on ScadaBridge:Database:ConfigurationDb. - /// - public const string MaintenanceConnectionStringKey = "AuditLog:MaintenanceConnectionString"; - - /// Configuration section bound to (ScadaBridge#41). - public const string HashChainSectionName = "AuditLog:HashChain"; - /// /// Configuration section bound to /// (ScadaBridge#42). @@ -410,34 +401,9 @@ public static class ServiceCollectionExtensions AuditLogPartitionMaintenanceOptionsValidator>(config, PartitionMaintenanceSectionName); services.AddHostedService(); - // Hash-chain tamper evidence (ScadaBridge#41). Default OFF; the hosted service is - // always registered and returns from StartAsync without a loop when disabled. - // IAuditLogHashChain is registered by AddConfigurationDatabase (central only). - services.AddValidatedOptions( - config, HashChainSectionName); - services.AddHostedService(); - - // Off-box chain-head export (ScadaBridge#107 item 1, AuditLog:HashChain:HeadExport, - // default off). The sink is built from the bound options; null when disabled. - services.AddSingleton(); - services.AddHostedService(sp => - { - var options = sp.GetRequiredService>(); - var node = sp.GetService()?.NodeName; - var export = options.Value.HeadExport; - Central.HeadExport.IAuditChainHeadSink? sink = !export.Enabled ? null : export.Sink switch - { - Central.HeadExport.AuditChainHeadSinkKind.Syslog => new Central.HeadExport.SyslogAuditChainHeadSink(export.Syslog), - _ => new Central.HeadExport.FileAuditChainHeadSink(export.File.Directory!, node ?? Environment.MachineName), - }; - return new Central.HeadExport.AuditChainHeadExportService( - sp.GetRequiredService(), - options, - sink, - node, - sp.GetRequiredService>(), - status: sp.GetRequiredService()); - }); + // The hash chain and the two-login maintenance hardening were removed on 2026-11-15; + // a node still carrying their settings boots normally and is told to delete them. + services.AddHostedService(); // Parquet cold archive (ScadaBridge#42). Default OFF; when enabled the purge actor // archives each expired partition before switching it out. IAuditLogArchive is diff --git a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditArchiveHelpers.cs b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditArchiveHelpers.cs index 4f52679c..7e748ba9 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditArchiveHelpers.cs +++ b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditArchiveHelpers.cs @@ -11,16 +11,16 @@ namespace ZB.MOM.WW.ScadaBridge.CLI.Commands; /// /// /// verify exit codes: 0 verified, when the -/// files, the chain or the purge anchor disagree, 1 on a request error, 2 on an +/// data file disagrees with its manifest, 1 on a request error, 2 on an /// authorization failure. /// public static class AuditArchiveHelpers { - /// Process exit code when an archive failed verification (tamper evidence). + /// Process exit code when an archive failed verification. public const int NotVerifiedExitCode = 3; /// - /// Client deadline for verify: the server hashes and re-chains a whole month under + /// Client deadline for verify: the server hashes and reads a whole month under /// the long-running management Ask (5 min); the client waits a little longer. /// public static readonly TimeSpan VerifyRequestTimeout = TimeSpan.FromMinutes(6); @@ -50,11 +50,11 @@ public static class AuditArchiveHelpers return 0; } - output.WriteLine($"{"NAME",-42} {"PARTITION",-9} {"STATUS",-8} {"ROWS",12} {"CHAINED",12} {"BYTES",14}"); + output.WriteLine($"{"NAME",-42} {"PARTITION",-9} {"STATUS",-8} {"ROWS",12} {"BYTES",14}"); foreach (var a in archives.EnumerateArray()) { output.WriteLine(string.Create(CultureInfo.InvariantCulture, - $"{Str(a, "name"),-42} {Str(a, "partition"),-9} {Str(a, "status"),-8} {Num(a, "rowCount"),12} {Num(a, "chainedRowCount"),12} {Num(a, "dataBytes"),14}")); + $"{Str(a, "name"),-42} {Str(a, "partition"),-9} {Str(a, "status"),-8} {Num(a, "rowCount"),12} {Num(a, "dataBytes"),14}")); } output.Flush(); @@ -90,20 +90,6 @@ public static class AuditArchiveHelpers } output.WriteLine($"Rows read : {Num(root, "rowsRead")}"); - if (root.TryGetProperty("chains", out var chains) && chains.ValueKind == JsonValueKind.Array) - { - foreach (var c in chains.EnumerateArray()) - { - var chain = c.GetProperty("chain"); - output.WriteLine($"Chain {Str(chain, "partition")}: {Str(chain, "status")}, {Num(chain, "rowsVerified")} rows verified, " - + $"{Num(chain, "tombstonesApplied")} tombstones, {Num(chain, "unchainedRows")} unchained; anchor {Str(c, "anchor")}"); - output.WriteLine($" {Str(c, "anchorDetail")}"); - if (chain.TryGetProperty("firstBreak", out var brk) && brk.ValueKind == JsonValueKind.Object) - { - output.WriteLine($" first break: chainSeq {Num(brk, "chainSeq")}, eventId {Str(brk, "eventId", "-")}, {Str(brk, "kind")} — {Str(brk, "detail")}"); - } - } - } output.Flush(); return verified ? 0 : NotVerifiedExitCode; diff --git a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditCommands.cs b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditCommands.cs index e693b399..e14676c0 100644 --- a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditCommands.cs +++ b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditCommands.cs @@ -8,17 +8,14 @@ namespace ZB.MOM.WW.ScadaBridge.CLI.Commands; /// The scadabridge audit command group. Provides read access to /// the centralized append-only Audit Log via the REST endpoints /// (GET /api/audit/query, GET /api/audit/export, -/// GET /api/audit/tree), plus verify-chain, which sends the -/// VerifyAuditChain management command (hash-chain tamper evidence, ScadaBridge#41), -/// verify-heads, which checks off-box exported chain heads with -/// VerifyAuditChainHeads (ScadaBridge#107), and archive list / archive verify +/// GET /api/audit/tree), plus archive list / archive verify /// / archive cleanup, which send ListAuditArchives / VerifyAuditArchive /// (Parquet cold archive, ScadaBridge#42) / CleanupAuditArchives (ScadaBridge#109). /// public static class AuditCommands { /// - /// Builds the audit command group with query, export, tree, and verify-chain + /// Builds the audit command group with query, export, tree and archive /// sub-commands. /// /// Global --url option for the management API endpoint. @@ -33,8 +30,6 @@ public static class AuditCommands command.Add(BuildQuery(urlOption, formatOption, usernameOption, passwordOption)); command.Add(BuildExport(urlOption, formatOption, usernameOption, passwordOption)); command.Add(BuildTree(urlOption, formatOption, usernameOption, passwordOption)); - command.Add(BuildVerifyChain(urlOption, formatOption, usernameOption, passwordOption)); - command.Add(BuildVerifyHeads(urlOption, formatOption, usernameOption, passwordOption)); command.Add(BuildBackfillSourceNode(urlOption, formatOption, usernameOption, passwordOption)); command.Add(BuildArchive(urlOption, formatOption, usernameOption, passwordOption)); @@ -293,87 +288,10 @@ public static class AuditCommands return cmd; } - private static Command BuildVerifyChain(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption) - { - var monthOption = new Option("--month") { Description = "UTC calendar month to verify (YYYY-MM)", Required = true }; - - var cmd = new Command("verify-chain") - { - Description = "Verify the audit log hash chain for a month; exit 3 and report the first break if it was tampered with", - }; - cmd.Add(monthOption); - cmd.SetAction(async (ParseResult result) => - { - var month = result.GetValue(monthOption)!; - if (!AuditVerifyChainHelpers.IsValidMonth(month)) - { - OutputFormatter.WriteError( - $"Invalid month '{month}'. Expected YYYY-MM (e.g. 2026-05).", "INVALID_ARGUMENT"); - return 1; - } - - var format = AuditCommandHelpers.ResolveFormat(result, formatOption); - return await CommandHelpers.ExecuteCommandAsync( - result, urlOption, formatOption, usernameOption, passwordOption, - new VerifyAuditChainCommand(month), - timeout: AuditVerifyChainHelpers.RequestTimeout, - onSuccess: json => AuditVerifyChainHelpers.Render(json, format, Console.Out)); - }); - return cmd; - } - /// - /// Builds the audit archive group (ScadaBridge#42): list and verify - /// over the AuditLog Parquet cold archives on the central archive directory. + /// Builds the audit archive group (ScadaBridge#42): list, verify and + /// cleanup over the AuditLog Parquet cold archives on the central archive directory. /// - private static Command BuildVerifyHeads(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption) - { - var pathOption = new Option("--path") - { - Description = "Export file, or directory of *.jsonl/*.log/*.txt export files (file sink or syslog collector output)", - Required = true, - }; - var cmd = new Command("verify-heads") - { - Description = "Check AuditLog chain heads exported off-box against the database chain; exit 3 if any no longer matches", - }; - cmd.Add(pathOption); - cmd.SetAction(async (ParseResult result) => - { - AuditVerifyHeadsHelpers.ReadResult read; - try - { - read = AuditVerifyHeadsHelpers.ReadRecords(result.GetValue(pathOption)!); - } - catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) - { - OutputFormatter.WriteError(ex.Message, "INVALID_ARGUMENT"); - return 1; - } - - if (read.Records.Count == 0) - { - OutputFormatter.WriteError( - $"No chain-head records found in {read.Files} file(s) ({read.LinesSkipped} unparsable line(s)).", - "INVALID_ARGUMENT"); - return 1; - } - - if (read.LinesSkipped > 0) - { - Console.Error.WriteLine($"warning: skipped {read.LinesSkipped} line(s) without a chain-head record."); - } - - var format = AuditCommandHelpers.ResolveFormat(result, formatOption); - return await CommandHelpers.ExecuteCommandAsync( - result, urlOption, formatOption, usernameOption, passwordOption, - new VerifyAuditChainHeadsCommand(read.Records), - timeout: AuditVerifyHeadsHelpers.RequestTimeout, - onSuccess: json => AuditVerifyHeadsHelpers.Render(json, format, Console.Out)); - }); - return cmd; - } - private static Command BuildArchive(Option urlOption, Option formatOption, Option usernameOption, Option passwordOption) { var group = new Command("archive") { Description = "List, verify and clean up the AuditLog Parquet cold archives" }; @@ -396,7 +314,7 @@ public static class AuditCommands }; var verify = new Command("verify") { - Description = "Verify an archive's files, its hash chain and its purge anchor; exit 3 if it does not verify", + Description = "Verify an archive's data file against its manifest; exit 3 if it does not verify", }; verify.Add(nameOption); verify.SetAction(async (ParseResult result) => diff --git a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditVerifyChainHelpers.cs b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditVerifyChainHelpers.cs deleted file mode 100644 index e3dde5a5..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditVerifyChainHelpers.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System.Globalization; -using System.Text.Json; - -namespace ZB.MOM.WW.ScadaBridge.CLI.Commands; - -/// -/// Helpers for the audit verify-chain subcommand (ScadaBridge#41). The command sends -/// the VerifyAuditChain management command for one UTC calendar month and reports -/// whether that month's AuditLog hash chain is intact, or the first break (chain partition, -/// ChainSeq, EventId, kind). -/// -/// -/// Exit codes: 0 intact (or nothing chained yet), when -/// the chain is broken, 1 on a request error, 2 on an authorization failure. -/// -public static class AuditVerifyChainHelpers -{ - /// Process exit code when verification found a break (tamper evidence). - public const int BrokenExitCode = 3; - - /// - /// Client deadline. The server bounds the call with the long-running management Ask - /// (5 min); the client waits a little longer so the server's answer always arrives. - /// - public static readonly TimeSpan RequestTimeout = TimeSpan.FromMinutes(6); - - /// - /// Returns true if is a well-formed YYYY-MM value - /// with a real month (01-12). A malformed month (e.g. 2026-13) is rejected. - /// - /// The month string to validate in YYYY-MM format. - /// true if the string is a well-formed YYYY-MM value with a real month; otherwise false. - public static bool IsValidMonth(string? month) - => !string.IsNullOrWhiteSpace(month) - && DateTime.TryParseExact(month, "yyyy-MM", CultureInfo.InvariantCulture, - DateTimeStyles.None, out _); - - /// - /// Renders a VerifyAuditChain result and maps it to the process exit code. - /// json prints the server body verbatim; table prints a short report. - /// - /// The success body (an AuditChainVerificationResult, camelCase). - /// Resolved output format. - /// Writer for the report. - /// 0 when intact / not chained, when broken. - public static int Render(string json, string format, TextWriter output) - { - using var doc = JsonDocument.Parse(json); - var root = doc.RootElement; - var status = root.TryGetProperty("status", out var s) ? s.GetString() ?? "" : ""; - var broken = string.Equals(status, "Broken", StringComparison.OrdinalIgnoreCase); - - if (!string.Equals(format, "table", StringComparison.OrdinalIgnoreCase)) - { - output.WriteLine(json); - output.Flush(); - return broken ? BrokenExitCode : 0; - } - - output.WriteLine($"Partition : {Str(root, "partition")}"); - output.WriteLine($"Status : {status}"); - output.WriteLine($"Rows verified : {Num(root, "rowsVerified")}"); - output.WriteLine($"Tombstones applied: {Num(root, "tombstonesApplied")}"); - output.WriteLine($"Purged through : {Num(root, "purgedThroughSeq", "-")}"); - output.WriteLine($"Head sequence : {Num(root, "headSeq", "-")}"); - output.WriteLine($"Unchained rows : {Num(root, "unchainedRows")}"); - output.WriteLine($"Chain from : {Str(root, "chainFromUtc", "- (whole table in scope)")}"); - output.WriteLine($"Pre-cutoff rows : {Num(root, "preCutoffRows")}"); - if (root.TryGetProperty("firstBreak", out var brk) && brk.ValueKind == JsonValueKind.Object) - { - output.WriteLine("First break:"); - output.WriteLine($" partition : {Str(brk, "partition")}"); - output.WriteLine($" chainSeq : {Num(brk, "chainSeq")}"); - output.WriteLine($" eventId : {Str(brk, "eventId", "-")}"); - output.WriteLine($" kind : {Str(brk, "kind")}"); - output.WriteLine($" detail : {Str(brk, "detail")}"); - } - - output.Flush(); - return broken ? BrokenExitCode : 0; - } - - private static string Str(JsonElement e, string name, string missing = "") - => e.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() ?? missing : missing; - - private static string Num(JsonElement e, string name, string missing = "0") - => e.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.Number ? v.GetInt64().ToString(CultureInfo.InvariantCulture) : missing; -} diff --git a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditVerifyHeadsHelpers.cs b/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditVerifyHeadsHelpers.cs deleted file mode 100644 index c4cd73f4..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.CLI/Commands/AuditVerifyHeadsHelpers.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System.Globalization; -using System.Text.Json; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.CLI.Commands; - -/// -/// Helpers for audit verify-heads (ScadaBridge#107 item 1): read AuditLog chain heads -/// exported off-box (the AuditLog:HashChain:HeadExport JSON lines — straight from the -/// file sink, or from a syslog collector's log), send them in a -/// VerifyAuditChainHeads management command, and render the verdicts. -/// -/// -/// Exit codes: 0 every exported head is consistent with the database, -/// at least one is not (tamper evidence), 1 on a request -/// or input error, 2 on an authorization failure. -/// -public static class AuditVerifyHeadsHelpers -{ - /// Process exit code when a head check failed. - public const int FailedExitCode = 3; - - /// Client deadline (the server bounds the call with the long-running Ask). - public static readonly TimeSpan RequestTimeout = TimeSpan.FromMinutes(6); - - private static readonly JsonSerializerOptions ReadOptions = new(JsonSerializerDefaults.Web); - - /// The result of reading export files. - /// Distinct records (by month, sequence, hash), latest export kept. - /// Lines that held a head record. - /// Non-empty lines without a parsable record. - /// Files read. - public sealed record ReadResult(IReadOnlyList Records, int LinesRead, int LinesSkipped, int Files); - - /// - /// Reads every record from — one file, or every *.jsonl / - /// *.log / *.txt file below a directory. Each line's JSON object is taken from - /// its first { to its last }, so syslog-prefixed lines parse too. - /// - /// File or directory. - /// The records read. - /// The path does not exist. - public static ReadResult ReadRecords(string path) - { - string[] files; - if (File.Exists(path)) - { - files = [path]; - } - else if (Directory.Exists(path)) - { - files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories) - .Where(f => f.EndsWith(".jsonl", StringComparison.OrdinalIgnoreCase) - || f.EndsWith(".log", StringComparison.OrdinalIgnoreCase) - || f.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) - .Order(StringComparer.Ordinal) - .ToArray(); - } - else - { - throw new FileNotFoundException($"'{path}' is neither a file nor a directory.", path); - } - - var records = new List(); - int read = 0, skipped = 0; - foreach (var file in files) - { - foreach (var line in File.ReadLines(file)) - { - if (string.IsNullOrWhiteSpace(line)) - { - continue; - } - - var record = TryParseLine(line); - if (record is null) - { - skipped++; - continue; - } - - read++; - records.Add(record); - } - } - - var distinct = records - .GroupBy(r => (r.Month, r.LastSeq, Hash: r.LastHash.ToLowerInvariant())) - .Select(g => g.OrderByDescending(r => r.ExportedAtUtc).First()) - .OrderBy(r => r.Month, StringComparer.Ordinal) - .ThenBy(r => r.LastSeq) - .ToList(); - return new ReadResult(distinct, read, skipped, files.Length); - } - - /// Parses one export line; null when it holds no valid record. - /// The line. - /// The record, or null. - public static AuditChainHeadRecord? TryParseLine(string line) - { - var start = line.IndexOf('{'); - var end = line.LastIndexOf('}'); - if (start < 0 || end <= start) - { - return null; - } - - try - { - var record = JsonSerializer.Deserialize(line.AsSpan(start, end - start + 1), ReadOptions); - return record is { V: >= 1, Month.Length: 7, LastHash.Length: > 0 } && record.LastSeq > 0 ? record : null; - } - catch (JsonException) - { - return null; - } - } - - /// Renders a VerifyAuditChainHeads result and maps it to the exit code. - /// Success body (an AuditChainHeadVerificationResult, camelCase). - /// Resolved output format. - /// Writer for the report. - /// 0 when verified, otherwise. - public static int Render(string json, string format, TextWriter output) - { - using var doc = JsonDocument.Parse(json); - var root = doc.RootElement; - var verified = root.TryGetProperty("verified", out var v) && v.ValueKind == JsonValueKind.True; - - if (!string.Equals(format, "table", StringComparison.OrdinalIgnoreCase)) - { - output.WriteLine(json); - output.Flush(); - return verified ? 0 : FailedExitCode; - } - - output.WriteLine($"Verified : {(verified ? "yes" : "NO")}"); - output.WriteLine($"Heads checked : {Num(root, "recordsChecked")}"); - if (root.TryGetProperty("checks", out var checks) && checks.ValueKind == JsonValueKind.Array) - { - output.WriteLine($"{"MONTH",-8} {"EXPORTED SEQ",12} {"DB SEQ",8} {"STATUS",-13} DETAIL"); - foreach (var c in checks.EnumerateArray()) - { - output.WriteLine(string.Create(CultureInfo.InvariantCulture, - $"{Str(c, "month"),-8} {Num(c, "exportedSeq"),12} {Num(c, "databaseSeq", "-"),8} {Str(c, "status"),-13} {Str(c, "detail")}")); - } - } - - output.Flush(); - return verified ? 0 : FailedExitCode; - } - - private static string Str(JsonElement e, string name) - => e.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.String ? v.GetString() ?? "" : ""; - - private static string Num(JsonElement e, string name, string missing = "0") - => e.TryGetProperty(name, out var v) && v.ValueKind == JsonValueKind.Number - ? v.GetInt64().ToString(CultureInfo.InvariantCulture) - : missing; -} diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/AuditLogServiceKeys.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/AuditLogServiceKeys.cs deleted file mode 100644 index 158c2c2b..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/AuditLogServiceKeys.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; - -/// DI keys for the AuditLog maintenance-login registrations (ScadaBridge#107/#113). -public static class AuditLogServiceKeys -{ - /// - /// Key of the maintenance-login / IAuditLogArchive: - /// the connection from AuditLog:MaintenanceConnectionString, or the runtime connection when unset. - /// - public const string Maintenance = "audit-maintenance"; -} diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditChainHeadStore.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditChainHeadStore.cs deleted file mode 100644 index f6aa122a..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditChainHeadStore.cs +++ /dev/null @@ -1,28 +0,0 @@ -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; - -/// -/// Reads the AuditLog hash-chain heads for off-box export and checks exported heads against -/// the database chain (ScadaBridge#107 item 1 — the external anchor of the #41 chain). -/// -public interface IAuditChainHeadStore -{ - /// - /// Reads every month's chain head with its live chained-row and tombstone counts, in one - /// consistent snapshot (under the chain lock SHARED, so no chaining pass or purge is half-way). - /// - /// Cancellation token. - /// One snapshot per month that has a head, oldest first. - Task> ReadHeadsAsync(CancellationToken ct = default); - - /// - /// Checks exported head records against the database: for each distinct exported head, does - /// the database still carry the exported hash at the exported sequence? - /// - /// Exported records (duplicates are collapsed). - /// Cancellation token. - /// The per-record verdicts. - Task VerifyExportedHeadsAsync( - IReadOnlyList records, CancellationToken ct = default); -} diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogArchive.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogArchive.cs index 8bb2714f..256c55a6 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogArchive.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogArchive.cs @@ -10,16 +10,10 @@ namespace ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; /// /// /// Archive before purge. writes the partition's -/// rows (with their hash-chain columns), the month's chain head and its per-channel purge -/// tombstones; then switches the partition out -/// inside a guard that refuses to drop it if the partition gained or lost a row, or its -/// chain advanced, since the archive was written. A partition is therefore never purged -/// without an archive that holds exactly the rows the purge drops. -/// -/// -/// Hash chain. The switch records each month's chain end as the checkpoint anchor -/// (ScadaBridge#41); the archive records the same chain end, so -/// can recompute the archived chain and tie its end to the anchor still in the database. +/// rows; then switches the partition out inside a +/// guard that refuses to drop it if the partition gained or lost a row since the archive was +/// written. A partition is therefore never purged without an archive that holds exactly the +/// rows the purge drops. /// /// public interface IAuditLogArchive @@ -58,8 +52,7 @@ public interface IAuditLogArchive Task> ListAsync(CancellationToken ct = default); /// - /// Verifies one archive: file integrity against the manifest, the hash chain recomputed - /// over the archived rows, and the chain end against the database's checkpoint anchor. + /// Verifies one archive: data-file integrity (size, SHA-256, row count) against the manifest. /// /// The archive name (as reports it). /// Cancellation token. @@ -70,7 +63,7 @@ public interface IAuditLogArchive /// /// Archive cleanup (ScadaBridge#109): deletes superseded stale archives a retried purge left /// behind (when AuditLog:Archive:DeleteSupersededStaleArchives), and archives past - /// AuditLog:Archive:RetentionDays (0 = never). Only ever deletes the three files of a + /// AuditLog:Archive:RetentionDays (0 = never). Only ever deletes the files of a /// well-formed archive inside the configured directory, manifest first. /// /// Report only; delete nothing. @@ -81,7 +74,7 @@ public interface IAuditLogArchive /// /// Thrown by when the partition -/// gained or lost rows, or its hash chain advanced, after the archive was written. Nothing +/// gained or lost rows after the archive was written. Nothing /// is purged; the next purge tick writes a fresh archive. /// public sealed class AuditArchiveStaleException : Exception diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogHashChain.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogHashChain.cs deleted file mode 100644 index 78e852d9..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogHashChain.cs +++ /dev/null @@ -1,58 +0,0 @@ -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; - -/// -/// Hash-chain tamper evidence for the central AuditLog table (audit roadmap T1, -/// alog.md §11.4, ScadaBridge#41). -/// -/// -/// -/// One chain per partition = UTC calendar month of OccurredAtUtc (the same -/// month the ps_AuditLog_Month scheme partitions on). Each chained row carries a -/// ChainSeq (1, 2, 3 … within its month) and -/// RowHash = SHA-256(prev.RowHash || canonical(row)), where prev is the row -/// at ChainSeq - 1 (32 zero bytes for the first). The canonical serialisation -/// covers every writable column plus the month and sequence number. -/// -/// -/// Rows are linked by a chaining job, not by the hot-path writers: two central -/// nodes and the site-ingest path all INSERT concurrently, so a write-time chain would -/// need a global serialisation point on every audit write. The job serialises itself -/// with an exclusive SQL Server application lock, so even a dual-active central pair -/// produces one gap-free chain per month. -/// -/// -public interface IAuditLogHashChain -{ - /// - /// Links up to unchained rows (oldest - /// OccurredAtUtc first) onto the end of their month's chain, in one transaction. - /// Returns immediately with set when another - /// chainer holds the chain lock. - /// - /// - /// Chain start (ScadaBridge#139). On a pristine database (nothing ever chained) and - /// with false, the first pass records a chain-start anchor - /// at the database's current UTC time; from then on only rows dated at or after it are - /// chained, whatever either node passes later. With true on - /// that first pass, no anchor is written and every row is chained, oldest month first. A - /// database that already has a chain and no anchor keeps chaining every row. - /// - /// Maximum rows to chain in this pass. - /// - /// Only consulted on a pristine database: true chains the historical backlog instead of - /// starting the chain now. - /// - /// Cancellation token. - /// How many rows were chained, and whether the lock was busy. - Task ChainPendingAsync(int batchSize, bool chainBacklog = false, CancellationToken ct = default); - - /// - /// Verifies one month's chain end to end and reports the first break. - /// - /// The first instant of the month (UTC, day 1, midnight). - /// Cancellation token. - /// The verification result. - Task VerifyMonthAsync(DateTime monthStartUtc, CancellationToken ct = default); -} diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs index 52c9d4c8..f4905224 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Commons/Interfaces/Repositories/IAuditLogRepository.cs @@ -158,11 +158,6 @@ public interface IAuditLogRepository /// OccurredAtUtc is strictly older than , in /// bounded batches of rows, looping until no further /// rows match. Returns the total number of rows deleted across all batches. - /// - /// Hash chain (ScadaBridge#41): every CHAINED row the DELETE removes leaves a - /// dbo.AuditLogChainTombstone (month, ChainSeq, RowHash) written in the same - /// transaction, under the exclusive chain lock, so the month's chain still verifies. - /// /// /// /// diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Messages/Management/AuditLogCommands.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Messages/Management/AuditLogCommands.cs index 6dd9564e..436f9098 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Messages/Management/AuditLogCommands.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Commons/Messages/Management/AuditLogCommands.cs @@ -25,23 +25,6 @@ public record QueryAuditLogCommand( /// Total number of rows matching the filters (unpaged). public record AuditLogQueryResult(IReadOnlyList Entries, int TotalCount); -/// -/// Verifies the central AuditLog hash chain for one UTC calendar month (audit roadmap T1, -/// ScadaBridge#41). Read-only; gated on the OperationalAudit roles and refused for a -/// site-scoped caller, because a month's chain spans every site. -/// -/// The month to verify, YYYY-MM. -public record VerifyAuditChainCommand(string Month); - -/// -/// Checks AuditLog chain heads exported off-box (AuditLog:HashChain:HeadExport, -/// ScadaBridge#107 item 1) against the database chain: does the database still carry each -/// exported hash at its exported sequence? Read-only; same gate as -/// . The CLI reads the records from the export files. -/// -/// The exported head records (at most 50,000; duplicates are collapsed). -public record VerifyAuditChainHeadsCommand(IReadOnlyList Records); - /// /// Lists the AuditLog Parquet cold archives under the configured archive directory (audit /// roadmap T2, ScadaBridge#42). Read-only; OperationalAudit roles, refused for a site-scoped @@ -50,9 +33,8 @@ public record VerifyAuditChainHeadsCommand(IReadOnlyList R public record ListAuditArchivesCommand; /// -/// Verifies one AuditLog Parquet archive (ScadaBridge#42): file integrity against its -/// manifest, the hash chain recomputed over the archived rows, and the chain end against the -/// checkpoint anchor the partition purge left. Read-only; same gate as +/// Verifies one AuditLog Parquet archive (ScadaBridge#42): data-file integrity against its +/// manifest (size, SHA-256, row count). Read-only; same gate as /// . /// /// The archive name, as audit archive list reports it. diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditArchive.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditArchive.cs index 80e0b478..efc63e70 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditArchive.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditArchive.cs @@ -11,8 +11,7 @@ public enum AuditArchiveStatus /// /// The archive files are durable but the partition switch that should follow has not /// been confirmed: either it failed (the archive went stale and a later tick wrote a - /// fresh one) or the node stopped between the switch and the manifest update. Verify - /// tells the two apart: only the second one's chain end matches the database anchor. + /// fresh one) or the node stopped between the switch and the manifest update. /// Archived, @@ -20,48 +19,25 @@ public enum AuditArchiveStatus Purged, } -/// -/// One chain partition (UTC calendar month) as it stood when its rows were archived: the -/// chain head and the checkpoint anchor an earlier partition purge had left, so the archived -/// rows can be re-verified with AuditChainVerifier long after the database dropped them. -/// -/// The chain partition, YYYY-MM. -/// The head's last sequence number at archive time. -/// The head's last hash at archive time (lowercase hex). -/// The checkpoint anchor an earlier purge of this month left, or null. -/// The hash at (lowercase hex), or null. -/// Per-channel purge tombstones of this month carried in the archive. -public sealed record AuditArchiveChainState( - string Partition, - long LastSeq, - string LastHash, - long? PriorPurgedThroughSeq, - string? PriorPurgedThroughHash, - long TombstoneCount); - /// /// The JSON manifest written beside every AuditLog Parquet archive (ScadaBridge#42). It is -/// written LAST, so its presence is the commit marker for the archive's two Parquet files. +/// written LAST, so its presence is the commit marker for the archive's Parquet data file. +/// A format-v1 manifest (written while the audit hash chain existed, before 2026-11-15) also +/// carries chain and tombstone fields; they are ignored when it is read. /// -/// Manifest/file layout version (currently 1). -/// Archive name, auditlog-YYYY-MM-yyyyMMddTHHmmssfffZ; the file stem of all three files. +/// Manifest/file layout version (currently 2; 1 = written with the hash chain). +/// Archive name, auditlog-YYYY-MM-yyyyMMddTHHmmssfffZ; the file stem of the archive's files. /// The pf_AuditLog_Month lower boundary of the archived partition. /// The archived partition, YYYY-MM. /// When the archive was written. /// Whether the partition switch that follows the archive has been confirmed. /// When the partition switch completed, or null. /// Rows in the data file. -/// Rows that carried a ChainSeq/RowHash. -/// Rows the chaining job had not reached (not covered by the chain). /// Earliest OccurredAtUtc in the data file, or null when empty. /// Latest OccurredAtUtc in the data file, or null when empty. /// Data file name, relative to the manifest's directory. /// SHA-256 of the data file (lowercase hex). /// Size of the data file in bytes. -/// Tombstone file name, relative to the manifest's directory. -/// SHA-256 of the tombstone file (lowercase hex). -/// Tombstones in the tombstone file. -/// One entry per chain partition that had a head when the rows were archived. public sealed record AuditArchiveManifest( int FormatVersion, string Name, @@ -71,66 +47,23 @@ public sealed record AuditArchiveManifest( AuditArchiveStatus Status, DateTime? PurgedAtUtc, long RowCount, - long ChainedRowCount, - long UnchainedRowCount, DateTime? MinOccurredAtUtc, DateTime? MaxOccurredAtUtc, string DataFile, string DataSha256, - long DataBytes, - string TombstoneFile, - string TombstoneSha256, - long TombstoneCount, - IReadOnlyList Chains); + long DataBytes); /// -/// How an archive's recorded chain end relates to the checkpoint anchor the partition purge -/// left on the database's chain head. -/// -[JsonConverter(typeof(JsonStringEnumConverter))] -public enum AuditArchiveAnchorStatus -{ - /// The database anchor is exactly the archive's chain end (sequence and hash). - Matches, - - /// - /// The database anchor is past the archive's chain end: rows that arrived late for this - /// month were chained and purged (and archived) again afterwards. - /// - Superseded, - - /// The partition has not been purged: the archive's rows are still in dbo.AuditLog. - NotPurged, - - /// - /// The anchor disagrees with the archive (an earlier sequence number, a different hash, or - /// no chain head at all): the archive or the head was altered. - /// - Mismatch, -} - -/// Verification of one chain partition inside an archive. -/// The chain recomputed over the archived rows, the archived head and tombstones. -/// How the archived chain end relates to the database's checkpoint anchor. -/// Human-readable explanation of . -public sealed record AuditArchiveChainVerification( - AuditChainVerificationResult Chain, - AuditArchiveAnchorStatus Anchor, - string AnchorDetail); - -/// -/// Result of audit archive verify (ScadaBridge#42): the archive files match their -/// manifest, every archived chain recomputes to the archived head, and each chain end is the -/// anchor the partition purge left in the database. +/// Result of audit archive verify (ScadaBridge#42): the data file matches its manifest +/// (size, SHA-256 and row count). /// /// The archive verified. /// The archived partition, YYYY-MM. /// The manifest's lifecycle status. -/// True when the files are intact, no chain is broken and no anchor mismatches. -/// True when both files' size and SHA-256 and the row/tombstone counts match the manifest. +/// True when the data file is intact. +/// True when the data file's size, SHA-256 and row count match the manifest. /// Why is false, or null. /// Rows read from the data file. -/// Per chain partition verification. public sealed record AuditArchiveVerificationResult( string Name, string Partition, @@ -138,8 +71,7 @@ public sealed record AuditArchiveVerificationResult( bool Verified, bool FilesIntact, string? FileProblem, - long RowsRead, - IReadOnlyList Chains); + long RowsRead); /// Result of audit archive list (ScadaBridge#42). /// Whether the retention purge is archiving partitions (AuditLog:Archive:Enabled). @@ -159,8 +91,7 @@ public enum AuditArchiveCleanupAction /// /// Deleted as a superseded stale archive: status Archived (its partition switch never /// completed — a retried purge), and a LATER, verified, Purged archive of the same - /// partition provably holds every row it holds (same chain through its chain end, every - /// unchained row present by EventId). + /// partition provably holds every row it holds (every row present by EventId). /// DeletedSuperseded, @@ -172,7 +103,7 @@ public enum AuditArchiveCleanupAction /// /// Deleted as an orphan of an interrupted archive write (ScadaBridge#114): a temp file of the - /// archive writer (*.parquet.tmp, *.tombstones.parquet.tmp, *.manifest.json.tmp) + /// archive writer (*.parquet.tmp, *.manifest.json.tmp, or a format-v1 *.tombstones.parquet.tmp) /// or a data file whose .manifest.json was never written, older than the safe age. /// DeletedOrphan, diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditChainHeads.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditChainHeads.cs deleted file mode 100644 index 05edff96..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditChainHeads.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System.Text.Json.Serialization; - -namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -/// -/// One month's AuditLog hash-chain head as exported off-box (ScadaBridge#107 item 1). The -/// export is the external anchor the in-database chain lacks: a principal able to rewrite rows, -/// recompute every later link and update AuditLogChainHead produces a chain that -/// verify-chain accepts, but cannot change a head hash that already left the box. -/// -/// -/// Serialised as one JSON object per line (camelCase) by both export sinks; the -/// audit verify-heads CLI reads those lines back and the server compares them with the -/// database. Hashes are lowercase hex. The format is versioned by . -/// -/// Record format version (1). -/// Chain partition, the UTC calendar month YYYY-MM. -/// The head's last sequence number — links written for the month so far. -/// The head's last link hash (hex). -/// The partition-purge checkpoint anchor's sequence, or null. -/// The anchor's hash (hex), or null. -/// Chained rows of the month still in dbo.AuditLog at export time. -/// Per-channel purge tombstones of the month at export time. -/// When the head row was last advanced. -/// When this record was exported. -/// The central node that exported it. -public sealed record AuditChainHeadRecord( - int V, - string Month, - long LastSeq, - string LastHash, - long? PurgedThroughSeq, - string? PurgedThroughHash, - long ChainedRows, - long Tombstones, - DateTime HeadUpdatedAtUtc, - DateTime ExportedAtUtc, - string? Node) -{ - /// The current record format version. - public const int CurrentVersion = 1; -} - -/// Outcome of comparing one exported head with the database chain. -[JsonConverter(typeof(JsonStringEnumConverter))] -public enum AuditChainHeadStatus -{ - /// The database head is exactly the exported head (same sequence, same hash, same row + tombstone count). - Matches, - - /// The chain has grown since the export and the link at the exported sequence still carries the exported hash. - Extended, - - /// - /// The link at the exported sequence was removed by a monthly partition purge (it is at or - /// before the checkpoint anchor and not the anchor itself), so the database cannot confirm - /// it; verify the month's Parquet archive instead (audit archive verify). - /// - Purged, - - /// The link at the exported sequence carries a different hash: the chain was rewritten. - Mismatch, - - /// The database head's sequence is BELOW the exported one: links were removed and the head rolled back. - Regressed, - - /// The month's head row is gone although links had been exported for it. - MissingHead, - - /// No row, tombstone or anchor carries the exported sequence any more: a chained row was deleted outside the purge paths. - MissingLink, - - /// - /// Same head as exported, no partition purge since, but chained rows + tombstones differ - /// from the exported counts: a chained row was removed or its link cleared. - /// - CountMismatch, -} - -/// One exported head checked against the database. -/// Chain partition (YYYY-MM). -/// The exported head's sequence. -/// When it was exported. -/// Which node exported it. -/// The verdict. -/// The database head's current sequence, or null when it has none. -/// Human-readable explanation. -public sealed record AuditChainHeadCheck( - string Month, - long ExportedSeq, - DateTime ExportedAtUtc, - string? Node, - AuditChainHeadStatus Status, - long? DatabaseSeq, - string Detail) -{ - /// True for the verdicts that are tamper evidence. - [JsonIgnore] - public bool IsFailure => Status is AuditChainHeadStatus.Mismatch or AuditChainHeadStatus.Regressed - or AuditChainHeadStatus.MissingHead or AuditChainHeadStatus.MissingLink or AuditChainHeadStatus.CountMismatch; -} - -/// Result of audit verify-heads. -/// True when no check is a failure. -/// Distinct exported heads checked. -/// Every check, failures first, then by month and sequence. -public sealed record AuditChainHeadVerificationResult(bool Verified, int RecordsChecked, IReadOnlyList Checks); - -/// The database's current chain head for a month, as read for export. -/// First instant of the month (UTC). -/// Head sequence. -/// Head hash. -/// Checkpoint anchor sequence, or null. -/// Checkpoint anchor hash, or null. -/// Chained rows of the month still present. -/// Tombstones of the month. -/// When the head row was last advanced. -public sealed record AuditChainHeadSnapshot( - DateTime Month, - long LastSeq, - byte[] LastHash, - long? PurgedThroughSeq, - byte[]? PurgedThroughHash, - long ChainedRows, - long Tombstones, - DateTime UpdatedAtUtc); diff --git a/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditChainVerification.cs b/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditChainVerification.cs deleted file mode 100644 index b987ccd0..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.Commons/Types/Audit/AuditChainVerification.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System.Text.Json.Serialization; - -namespace ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -/// -/// Overall outcome of verifying one month's AuditLog hash chain (audit roadmap T1, -/// ScadaBridge#41). -/// -[JsonConverter(typeof(JsonStringEnumConverter))] -public enum AuditChainStatus -{ - /// Every chained row, tombstone and the chain head agree. - Intact, - - /// The chain is broken; names where. - Broken, - - /// The month has no chained rows and no chain head — nothing to verify yet. - NotChained, -} - -/// How the first chain break manifested. -[JsonConverter(typeof(JsonStringEnumConverter))] -public enum AuditChainBreakKind -{ - /// - /// A row's recomputed hash differs from its stored RowHash: the row's content - /// was edited, or rows were reordered (their ChainSeq values swapped). - /// - HashMismatch, - - /// - /// A sequence number is absent and no purge tombstone covers it: a chained row was - /// deleted outside the sanctioned purge path, or its ChainSeq was cleared. - /// - MissingRow, - - /// Two rows carry the same ChainSeq. - DuplicateSequence, - - /// A chained row carries a ChainSeq but no RowHash (or vice versa). - IncompleteLink, - - /// - /// The chain ends before (or after) the sequence/hash the chain head records: rows were - /// deleted from the tail, or the head itself was altered. - /// - HeadMismatch, - - /// - /// A chained row is dated before the chain cutoff (ScadaBridge#139). Rows before the cutoff - /// are never chained, so the row's OccurredAtUtc was moved back (to hide it among the - /// unprotected pre-cutoff rows) or the cutoff itself was moved forward. - /// - ChainedBeforeCutoff, - - /// - /// The chain-start (cutoff) anchor is inconsistent (ScadaBridge#139): its stored hash does not - /// match its cutoff, it sits in the wrong month, or more than one anchor exists. The cutoff - /// that decides which rows are in scope cannot be trusted. - /// - CutoffAnchorInvalid, -} - -/// -/// The first point at which a month's chain fails verification. -/// -/// The chain partition — the UTC calendar month, formatted YYYY-MM. -/// The sequence number at which the break was detected. -/// -/// The EventId of the row at (or, for , -/// immediately after) the break; null when no row is present there (tail truncation). -/// -/// How the break manifested. -/// Human-readable explanation. -public sealed record AuditChainBreak( - string Partition, - long ChainSeq, - Guid? EventId, - AuditChainBreakKind Kind, - string Detail); - -/// -/// Result of audit verify-chain --month YYYY-MM: whether the month's AuditLog hash -/// chain is intact, and if not, the first break. -/// -/// The chain partition verified (YYYY-MM). -/// Overall outcome. -/// Chained rows whose hash was recomputed and matched before the first break (or in total when intact). -/// Sequence numbers bridged by a sanctioned purge tombstone (per-channel retention). -/// -/// The checkpoint anchor left by a monthly partition purge: the last sequence number the -/// purge removed. Verification starts immediately after it. Null when the month was never purged. -/// -/// The last sequence number the chain head records, or null when the month has no head. -/// -/// Rows in the month, at or after the chain cutoff, that the chaining job has not reached yet -/// (written since the last tick, or written while chaining was disabled). They are not covered -/// by the chain yet. -/// -/// The first break, or null when is not . -/// -/// Rows in the month dated before the chain cutoff (ScadaBridge#139). They are out of scope by -/// design — the chain covers only rows written from its first enablement on — so they are -/// never chained and never a break. Zero when the chain has no cutoff. -/// -/// -/// The chain cutoff: the instant the chain was first enabled without backlog chaining. Null -/// when the chain covers the whole table (legacy / ChainBacklog enablement). -/// -public sealed record AuditChainVerificationResult( - string Partition, - AuditChainStatus Status, - long RowsVerified, - long TombstonesApplied, - long? PurgedThroughSeq, - long? HeadSeq, - long UnchainedRows, - AuditChainBreak? FirstBreak, - long PreCutoffRows = 0, - DateTime? ChainFromUtc = null); - -/// Outcome of one chaining pass over unchained AuditLog rows. -/// Rows linked into a chain by this pass. -/// True when another chainer (the peer central node) held the chain lock, so this pass did nothing. -public sealed record AuditChainBatchResult(int RowsChained, bool LockBusy); diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Configurations/AuditLogEntityTypeConfiguration.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Configurations/AuditLogEntityTypeConfiguration.cs index b8c00397..6a0fe5a3 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Configurations/AuditLogEntityTypeConfiguration.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Configurations/AuditLogEntityTypeConfiguration.cs @@ -169,17 +169,6 @@ public class AuditLogEntityTypeConfiguration : IEntityTypeConfiguration e.ChainSeq); - - builder.Property(e => e.RowHash) - .HasMaxLength(32) - .IsFixedLength(); - // ── Keys + indexes ─────────────────────────────────────────────────── // Clustered PK (OccurredAtUtc, EventId) — FlipAuditLogClusteredKey (ScadaBridge#111, @@ -262,46 +251,5 @@ public class AuditLogEntityTypeConfiguration : IEntityTypeConfiguration new { e.Actor, e.OccurredAtUtc }) .IsDescending(false, true) .HasDatabaseName("IX_AuditLog_Actor_Occurred"); - - // Hash-chain indexes (AddAuditLogHashChain migration, raw SQL ON ps_AuditLog_Month - // so they stay partition-aligned). Unchained: the chaining job's work queue — - // filtered to ChainSeq IS NULL, so it shrinks to the rows written since the last - // tick. ChainSeq: verification streams one month's chain in sequence order. - builder.HasIndex(e => new { e.OccurredAtUtc, e.EventId }) - .HasFilter("[ChainSeq] IS NULL") - .HasDatabaseName("IX_AuditLog_Unchained"); - - builder.HasIndex(e => e.ChainSeq) - .HasFilter("[ChainSeq] IS NOT NULL") - .HasDatabaseName("IX_AuditLog_ChainSeq"); - } -} - -/// Maps to dbo.AuditLogChainHead (ScadaBridge#41). -public class AuditLogChainHeadEntityTypeConfiguration : IEntityTypeConfiguration -{ - /// Applies the EF Core type configuration for . - /// The entity type builder to configure. - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable("AuditLogChainHead"); - builder.HasKey(e => e.ChainMonth); - builder.Property(e => e.ChainMonth).HasColumnType("date"); - builder.Property(e => e.LastHash).HasMaxLength(32).IsFixedLength().IsRequired(); - builder.Property(e => e.PurgedThroughHash).HasMaxLength(32).IsFixedLength(); - } -} - -/// Maps to dbo.AuditLogChainTombstone (ScadaBridge#41). -public class AuditLogChainTombstoneEntityTypeConfiguration : IEntityTypeConfiguration -{ - /// Applies the EF Core type configuration for . - /// The entity type builder to configure. - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable("AuditLogChainTombstone"); - builder.HasKey(e => new { e.ChainMonth, e.ChainSeq }); - builder.Property(e => e.ChainMonth).HasColumnType("date"); - builder.Property(e => e.RowHash).HasMaxLength(32).IsFixedLength().IsRequired(); } } diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Entities/AuditLogChainEntities.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Entities/AuditLogChainEntities.cs deleted file mode 100644 index a2584408..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Entities/AuditLogChainEntities.cs +++ /dev/null @@ -1,61 +0,0 @@ -namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities; - -/// -/// 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. -/// -/// -/// The head is what lets verification detect tail truncation — 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 (/) -/// and by the partition-switch purge (/). -/// -public sealed class AuditLogChainHead -{ - /// First day of the chain's month (UTC). Primary key. - public DateTime ChainMonth { get; set; } - - /// Sequence number of the last row linked into the chain. - public long LastSeq { get; set; } - - /// RowHash of the last row linked into the chain (32 bytes). - public byte[] LastHash { get; set; } = []; - - /// - /// Checkpoint anchor: the last sequence number removed by a monthly partition-switch - /// purge. Verification resumes at PurgedThroughSeq + 1 with - /// as the previous link. Null when never purged. - /// - public long? PurgedThroughSeq { get; set; } - - /// RowHash at ; null when never purged. - public byte[]? PurgedThroughHash { get; set; } - - /// UTC time the head last moved. - public DateTime UpdatedAtUtc { get; set; } -} - -/// -/// 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 -/// OUTPUT … INTO) so the purge can never remove a chained row without leaving its -/// hash behind; verification bridges the gap with it. -/// -public sealed class AuditLogChainTombstone -{ - /// First day of the chain's month (UTC). Part of the primary key. - public DateTime ChainMonth { get; set; } - - /// Sequence number of the purged row. Part of the primary key. - public long ChainSeq { get; set; } - - /// RowHash of the purged row (32 bytes). - public byte[] RowHash { get; set; } = []; - - /// The purged row's EventId, kept for the operator's benefit. - public Guid EventId { get; set; } - - /// UTC time of the purge. - public DateTime PurgedAtUtc { get; set; } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Entities/AuditLogRow.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Entities/AuditLogRow.cs index 83a9ef2b..200d6404 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Entities/AuditLogRow.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Entities/AuditLogRow.cs @@ -138,19 +138,4 @@ public sealed record AuditLogRow : null; } private readonly DateTime? _ingestedAtUtc; - - // ── Hash-chain link (audit roadmap T1, ScadaBridge#41) ──────────────────── - - /// - /// Position of this row in its month's hash chain (1-based, gap-free per UTC calendar - /// month of ); null until the chaining job reaches the row. - /// Written ONLY by the chaining job — never by the ingest/writer path. - /// - public long? ChainSeq { get; init; } - - /// - /// SHA-256(prev.RowHash || canonical(row)) — 32 bytes; null until chained. See - /// AuditChainHasher for the canonical serialisation. - /// - public byte[]? RowHash { get; init; } } diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditArchiveParquet.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditArchiveParquet.cs index 8d4ac259..d354e989 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditArchiveParquet.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditArchiveParquet.cs @@ -1,3 +1,5 @@ +using System.Data; +using System.Data.Common; using System.Globalization; using System.Runtime.CompilerServices; using System.Security.Cryptography; @@ -7,50 +9,58 @@ using Parquet.Schema; namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; /// -/// One per-channel purge tombstone carried in an archive (ScadaBridge#41/#42): the link a -/// sanctioned per-channel retention DELETE removed from the middle of a month's chain. +/// One archived dbo.AuditLog row: the ten stored (writable) columns. The persisted +/// computed columns (Kind, Status, SourceSiteId, …) are derived from DetailsJson and +/// are not carried. /// -/// The chain partition's first instant (UTC). -/// The purged row's sequence number. -/// The purged row's 32-byte hash. -/// The purged row's EventId. -/// When the per-channel purge removed it. -public sealed record AuditArchiveTombstone( - DateTime ChainMonth, - long ChainSeq, - byte[] RowHash, +/// The row's EventId. +/// When the event occurred (UTC, datetime2(7) precision). +/// Actor, or null. +/// Action. +/// Outcome. +/// Channel (the canonical Category column). +/// Target, or null. +/// Node of origin, or null. +/// Correlation id, or null. +/// Details JSON, or null. +public sealed record AuditArchiveRow( Guid EventId, - DateTime PurgedAtUtc); + DateTime OccurredAtUtc, + string? Actor, + string Action, + string Outcome, + string Category, + string? Target, + string? SourceNode, + Guid? CorrelationId, + string? DetailsJson); /// -/// The Parquet layout of an AuditLog archive (audit roadmap T2, ScadaBridge#42), format v1. +/// The Parquet layout of an AuditLog archive (audit roadmap T2, ScadaBridge#42), format v2. /// /// /// /// Data file — one row per archived dbo.AuditLog row, the ten stored (writable) -/// columns plus the two hash-chain columns, so a row can be re-hashed with -/// exactly as the database chained it: -/// EventId (string, D format), OccurredAtUtc (timestamp, UTC, microseconds — -/// for analytics), OccurredAtUtcTicks (int64 — the exact datetime2(7) value the hash -/// covers), Actor, Action, Outcome, Category, Target, -/// SourceNode, CorrelationId, DetailsJson (strings), ChainSeq (int64, -/// null when unchained) and RowHash (binary, null when unchained). The persisted computed -/// columns (Kind, Status, SourceSiteId, …) are not stored: they are derived from -/// DetailsJson, which the hash covers. +/// columns: EventId (string, D format), OccurredAtUtc (timestamp, UTC, +/// microseconds — for analytics), OccurredAtUtcTicks (int64 — the exact +/// datetime2(7) value), Actor, Action, Outcome, Category, +/// Target, SourceNode, CorrelationId, DetailsJson (strings). /// /// -/// Tombstone file — ChainMonth (YYYY-MM), ChainSeq, RowHash, -/// EventId, PurgedAtUtc. +/// Format v1 (written before 2026-11-15, when the audit hash chain was removed) also +/// carried ChainSeq/RowHash columns and a .tombstones.parquet sibling. The +/// reader reads a v1 data file by column name and ignores those columns; cleanup still deletes a +/// v1 archive's tombstone file with it. /// /// -/// Both are plain Parquet: DuckDB (SELECT * FROM 'auditlog-….parquet'), pandas/pyarrow -/// and Spark read them without any ScadaBridge code. +/// Plain Parquet: DuckDB (SELECT * FROM 'auditlog-….parquet'), pandas/pyarrow and Spark +/// read it without any ScadaBridge code. /// /// public static class AuditArchiveParquet { /// The archive layout version written into the file metadata and the manifest. - public const int FormatVersion = 1; + public const int FormatVersion = 2; /// Key-value metadata key carrying . public const string FormatVersionKey = "scadabridge.auditlog.archive.formatVersion"; @@ -70,24 +80,60 @@ public static class AuditArchiveParquet private static readonly DataField SourceNodeField = new("SourceNode", true); private static readonly DataField CorrelationIdField = new("CorrelationId", true); private static readonly DataField DetailsJsonField = new("DetailsJson", true); - private static readonly DataField ChainSeqField = new("ChainSeq", true); - private static readonly DataField RowHashField = new("RowHash", true); - private static readonly DataField TombstoneMonthField = new("ChainMonth", false); - private static readonly DataField TombstoneSeqField = new("ChainSeq", false); - private static readonly DataField TombstoneHashField = new("RowHash", false); - private static readonly DataField TombstoneEventIdField = new("EventId", false); - private static readonly DateTimeDataField TombstonePurgedAtField = - new("PurgedAtUtc", DateTimeFormat.Timestamp, isAdjustedToUTC: true, unit: DateTimeTimeUnit.Micros, isNullable: false); + /// Suffix of the per-channel-purge tombstone file a format-v1 archive carried. + public const string LegacyTombstoneSuffix = ".tombstones.parquet"; + + /// The ten stored dbo.AuditLog columns, in table order (see ). + internal const string RowColumns = + "EventId, OccurredAtUtc, Actor, Action, Outcome, Category, Target, SourceNode, CorrelationId, DetailsJson"; /// The data-file schema. public static ParquetSchema DataSchema { get; } = new( EventIdField, OccurredAtField, OccurredAtTicksField, ActorField, ActionField, OutcomeField, CategoryField, - TargetField, SourceNodeField, CorrelationIdField, DetailsJsonField, ChainSeqField, RowHashField); + TargetField, SourceNodeField, CorrelationIdField, DetailsJsonField); - /// The tombstone-file schema. - public static ParquetSchema TombstoneSchema { get; } = new( - TombstoneMonthField, TombstoneSeqField, TombstoneHashField, TombstoneEventIdField, TombstonePurgedAtField); + /// The first instant (UTC) of the calendar month holding . + /// A UTC instant. + /// Day 1, midnight UTC. + public static DateTime MonthOf(DateTime occurredAtUtc) + => new(occurredAtUtc.Year, occurredAtUtc.Month, 1, 0, 0, 0, DateTimeKind.Utc); + + /// Formats a month as YYYY-MM. + /// The month's first instant. + /// The partition label. + public static string PartitionLabel(DateTime monthStartUtc) + => monthStartUtc.ToString("yyyy-MM", CultureInfo.InvariantCulture); + + /// Reads one row selected as , strictly in ordinal order (SequentialAccess-safe). + /// The reader, positioned on a row. + /// The row. + internal static AuditArchiveRow ReadRow(DbDataReader reader) => new( + reader.GetGuid(0), + DateTime.SpecifyKind(reader.GetDateTime(1), DateTimeKind.Utc), + reader.IsDBNull(2) ? null : reader.GetString(2), + reader.GetString(3), + reader.GetString(4), + reader.GetString(5), + reader.IsDBNull(6) ? null : reader.GetString(6), + reader.IsDBNull(7) ? null : reader.GetString(7), + reader.IsDBNull(8) ? null : reader.GetGuid(8), + reader.IsDBNull(9) ? null : reader.GetString(9)); + + /// Opens when it is not already open. + /// The connection. + /// Cancellation token. + /// True when this call opened it (the caller closes it). + internal static async Task OpenAsync(DbConnection conn, CancellationToken ct) + { + if (conn.State == ConnectionState.Open) + { + return false; + } + + await conn.OpenAsync(ct).ConfigureAwait(false); + return true; + } /// Opens a buffered writer of archive data rows over . /// Destination stream (need not be seekable). @@ -102,24 +148,11 @@ public static class AuditArchiveParquet return new DataWriter(writer, rowGroupSize); } - /// Opens a buffered writer of tombstones over . - /// Destination stream (need not be seekable). - /// The archived partition (YYYY-MM), stamped into the file metadata. - /// Rows per Parquet row group. - /// Cancellation token. - /// The writer; dispose it to flush the last row group and the footer. - public static async Task CreateTombstoneWriterAsync(Stream output, string partition, int rowGroupSize, CancellationToken ct = default) - { - var writer = await ParquetWriter.CreateAsync(TombstoneSchema, output, cancellationToken: ct).ConfigureAwait(false); - writer.CustomMetadata = Metadata(partition); - return new TombstoneWriter(writer, rowGroupSize); - } - /// Streams the rows of an archive data file in file order. /// A readable, seekable stream over the data file. /// Cancellation token. - /// The rows, as the chain hasher sees them. - public static async IAsyncEnumerable ReadRowsAsync(Stream input, [EnumeratorCancellation] CancellationToken ct = default) + /// The rows. + public static async IAsyncEnumerable ReadRowsAsync(Stream input, [EnumeratorCancellation] CancellationToken ct = default) { await using var reader = await ParquetReader.CreateAsync(input, leaveStreamOpen: true, cancellationToken: ct).ConfigureAwait(false); for (var g = 0; g < reader.RowGroupCount; g++) @@ -137,14 +170,10 @@ public static class AuditArchiveParquet var nodes = await Strings(group, SourceNodeField, n, ct).ConfigureAwait(false); var correlations = await Strings(group, CorrelationIdField, n, ct).ConfigureAwait(false); var details = await Strings(group, DetailsJsonField, n, ct).ConfigureAwait(false); - var seqs = new long?[n]; - await group.ReadAsync(ChainSeqField, seqs, cancellationToken: ct).ConfigureAwait(false); - var hashes = new byte[]?[n]; - await group.ReadAsync(RowHashField, hashes!, cancellationToken: ct).ConfigureAwait(false); for (var i = 0; i < n; i++) { - yield return new AuditChainRow( + yield return new AuditArchiveRow( Guid.Parse(eventIds[i]!), new DateTime(ticks[i], DateTimeKind.Utc), actors[i], @@ -154,46 +183,12 @@ public static class AuditArchiveParquet targets[i], nodes[i], correlations[i] is { } c ? Guid.Parse(c) : null, - details[i], - seqs[i], - hashes[i]); + details[i]); } } } - /// Streams the tombstones of an archive tombstone file. - /// A readable, seekable stream over the tombstone file. - /// Cancellation token. - /// The tombstones. - public static async IAsyncEnumerable ReadTombstonesAsync(Stream input, [EnumeratorCancellation] CancellationToken ct = default) - { - await using var reader = await ParquetReader.CreateAsync(input, leaveStreamOpen: true, cancellationToken: ct).ConfigureAwait(false); - for (var g = 0; g < reader.RowGroupCount; g++) - { - using var group = reader.OpenRowGroupReader(g); - var n = checked((int)group.RowCount); - var months = await Strings(group, TombstoneMonthField, n, ct).ConfigureAwait(false); - var seqs = new long[n]; - await group.ReadAsync(TombstoneSeqField, seqs, cancellationToken: ct).ConfigureAwait(false); - var hashes = new byte[]?[n]; - await group.ReadAsync(TombstoneHashField, hashes!, cancellationToken: ct).ConfigureAwait(false); - var eventIds = await Strings(group, TombstoneEventIdField, n, ct).ConfigureAwait(false); - var purgedAt = new DateTime[n]; - await group.ReadAsync(TombstonePurgedAtField, purgedAt, cancellationToken: ct).ConfigureAwait(false); - - for (var i = 0; i < n; i++) - { - yield return new AuditArchiveTombstone( - ParseMonth(months[i]!), - seqs[i], - hashes[i]!, - Guid.Parse(eventIds[i]!), - DateTime.SpecifyKind(purgedAt[i], DateTimeKind.Utc)); - } - } - } - - /// Parses a YYYY-MM chain partition label to its first instant (UTC). + /// Parses a YYYY-MM partition label to its first instant (UTC). /// The label. /// Day 1, midnight UTC. public static DateTime ParseMonth(string partition) @@ -220,14 +215,14 @@ public static class AuditArchiveParquet { private readonly ParquetWriter _writer; private readonly int _rowGroupSize; - private readonly List _buffer; + private readonly List _buffer; private bool _disposed; internal DataWriter(ParquetWriter writer, int rowGroupSize) { _writer = writer; _rowGroupSize = Math.Max(1, rowGroupSize); - _buffer = new List(Math.Min(_rowGroupSize, 65_536)); + _buffer = new List(Math.Min(_rowGroupSize, 65_536)); } /// Rows added so far. @@ -237,7 +232,7 @@ public static class AuditArchiveParquet /// The row. /// Cancellation token. /// A task that completes when the row is buffered (or its row group written). - public async Task AddAsync(AuditChainRow row, CancellationToken ct = default) + public async Task AddAsync(AuditArchiveRow row, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(row); _buffer.Add(row); @@ -283,75 +278,6 @@ public static class AuditArchiveParquet await group.WriteAsync(SourceNodeField, rows.Select(r => r.SourceNode).ToArray()!).ConfigureAwait(false); await group.WriteAsync(CorrelationIdField, rows.Select(r => r.CorrelationId?.ToString("D")).ToArray()!).ConfigureAwait(false); await group.WriteAsync(DetailsJsonField, rows.Select(r => r.DetailsJson).ToArray()!).ConfigureAwait(false); - await group.WriteAsync(ChainSeqField, rows.Select(r => r.ChainSeq).ToArray(), cancellationToken: ct).ConfigureAwait(false); - await group.WriteAsync(RowHashField, rows.Select(r => r.RowHash).ToArray()!).ConfigureAwait(false); - } - - _buffer.Clear(); - } - } - - /// Buffers tombstones and writes them a row group at a time. - public sealed class TombstoneWriter : IAsyncDisposable - { - private readonly ParquetWriter _writer; - private readonly int _rowGroupSize; - private readonly List _buffer = []; - private bool _disposed; - - internal TombstoneWriter(ParquetWriter writer, int rowGroupSize) - { - _writer = writer; - _rowGroupSize = Math.Max(1, rowGroupSize); - } - - /// Tombstones added so far. - public long Count { get; private set; } - - /// Adds one tombstone, flushing a row group when the buffer is full. - /// The tombstone. - /// Cancellation token. - /// A task that completes when the tombstone is buffered (or its row group written). - public async Task AddAsync(AuditArchiveTombstone tombstone, CancellationToken ct = default) - { - ArgumentNullException.ThrowIfNull(tombstone); - _buffer.Add(tombstone); - Count++; - if (_buffer.Count >= _rowGroupSize) - { - await FlushAsync(ct).ConfigureAwait(false); - } - } - - /// Writes any buffered tombstones as a row group, then the file footer. - /// A task that completes when the file is finished. - public async ValueTask DisposeAsync() - { - if (_disposed) - { - return; - } - - _disposed = true; - await FlushAsync(CancellationToken.None).ConfigureAwait(false); - await _writer.DisposeAsync().ConfigureAwait(false); - } - - private async Task FlushAsync(CancellationToken ct) - { - if (_buffer.Count == 0) - { - return; - } - - var rows = _buffer; - using (var group = _writer.CreateRowGroup()) - { - await group.WriteAsync(TombstoneMonthField, rows.Select(t => AuditChainHasher.PartitionLabel(t.ChainMonth)).ToArray()).ConfigureAwait(false); - await group.WriteAsync(TombstoneSeqField, rows.Select(t => t.ChainSeq).ToArray(), cancellationToken: ct).ConfigureAwait(false); - await group.WriteAsync(TombstoneHashField, rows.Select(t => t.RowHash).ToArray()).ConfigureAwait(false); - await group.WriteAsync(TombstoneEventIdField, rows.Select(t => t.EventId.ToString("D")).ToArray()).ConfigureAwait(false); - await group.WriteAsync(TombstonePurgedAtField, rows.Select(t => t.PurgedAtUtc).ToArray(), cancellationToken: ct).ConfigureAwait(false); } _buffer.Clear(); diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainHasher.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainHasher.cs deleted file mode 100644 index eb8f489a..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainHasher.cs +++ /dev/null @@ -1,184 +0,0 @@ -using System.Buffers.Binary; -using System.Globalization; -using System.Security.Cryptography; -using System.Text; - -namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; - -/// -/// The stored column values of one dbo.AuditLog row as the hash chain sees them -/// (ScadaBridge#41). Strings are the raw column values exactly as SQL Server returns them -/// (e.g. / are the stored enum NAMES), so -/// the hash never depends on how a later build happens to map them to CLR enums. -/// -/// The row's EventId. -/// The row's OccurredAtUtc (datetime2(7) — full tick precision). -/// Actor column. -/// Action column. -/// Outcome column. -/// Category (channel) column. -/// Target column. -/// SourceNode column. -/// CorrelationId column. -/// DetailsJson column. -/// Stored ChainSeq, or null when unchained. -/// Stored RowHash, or null when unchained. -public sealed record AuditChainRow( - Guid EventId, - DateTime OccurredAtUtc, - string? Actor, - string Action, - string Outcome, - string Category, - string? Target, - string? SourceNode, - Guid? CorrelationId, - string? DetailsJson, - long? ChainSeq = null, - byte[]? RowHash = null); - -/// -/// Canonical serialisation + SHA-256 link computation for the AuditLog hash chain -/// (alog.md §11.4, ScadaBridge#41): RowHash = SHA-256(prev.RowHash || canonical(row)). -/// -/// -/// -/// Canonical form (v1), all integers big-endian: the ASCII domain tag -/// "ScadaBridge.AuditLog.Chain.v1"; the chain partition (YYYY-MM); the -/// 64-bit ChainSeq; then the ten writable columns in table order — EventId (as its -/// lowercase D-format string), OccurredAtUtc (64-bit ticks), Actor, Action, Outcome, -/// Category, Target, SourceNode, CorrelationId (D-format string), DetailsJson. -/// Every string-valued field is encoded as one presence byte (0 = SQL NULL, -/// 1 = present) followed, when present, by a 32-bit byte length and its UTF-8 bytes — -/// so NULL and the empty string hash differently, and no field boundary is ambiguous. -/// -/// -/// The partition and sequence number are inside the hashed bytes so a row moved to a -/// different position, or into another month's chain, fails verification on its own, -/// independently of the previous-hash link. The computed columns (Kind, Status, …) are -/// derived from DetailsJson, so hashing DetailsJson covers them. -/// -/// -/// Changing anything here invalidates every chain already written. A new form needs a new -/// domain tag and a migration story; never edit v1 in place. -/// -/// -public static class AuditChainHasher -{ - /// Length of a link hash in bytes. - public const int HashLength = 32; - - private static readonly byte[] DomainTag = Encoding.ASCII.GetBytes("ScadaBridge.AuditLog.Chain.v1"); - - /// The previous-hash value the first row of every chain links to (32 zero bytes). - public static ReadOnlySpan GenesisHash => new byte[HashLength]; - - /// Returns the first instant (UTC) of the month containing . - /// A UTC timestamp. - /// Day 1, 00:00:00 UTC of that month. - public static DateTime MonthOf(DateTime occurredAtUtc) - => new(occurredAtUtc.Year, occurredAtUtc.Month, 1, 0, 0, 0, DateTimeKind.Utc); - - /// Formats a chain partition as YYYY-MM. - /// The month's first instant. - /// The partition label. - public static string PartitionLabel(DateTime monthStartUtc) - => monthStartUtc.ToString("yyyy-MM", CultureInfo.InvariantCulture); - - /// Computes the link hash for at . - /// The RowHash of the row at chainSeq - 1, or . - /// The chain partition the row belongs to. - /// The row's position in the chain. - /// The row's stored column values. - /// The 32-byte SHA-256 link hash. - public static byte[] ComputeRowHash(ReadOnlySpan previousHash, DateTime monthStartUtc, long chainSeq, AuditChainRow row) - { - ArgumentNullException.ThrowIfNull(row); - if (previousHash.Length != HashLength) - { - throw new ArgumentException($"Previous hash must be {HashLength} bytes.", nameof(previousHash)); - } - - using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); - hash.AppendData(previousHash); - hash.AppendData(Canonicalize(monthStartUtc, chainSeq, row)); - return hash.GetHashAndReset(); - } - - /// Builds the canonical v1 byte form of at its chain position. - /// The chain partition. - /// The row's position in the chain. - /// The row's stored column values. - /// The canonical bytes (excluding the previous hash). - public static byte[] Canonicalize(DateTime monthStartUtc, long chainSeq, AuditChainRow row) - { - ArgumentNullException.ThrowIfNull(row); - using var buffer = new MemoryStream(256 + (row.DetailsJson?.Length ?? 0) * 3); - buffer.Write(DomainTag); - WriteString(buffer, PartitionLabel(monthStartUtc)); - WriteInt64(buffer, chainSeq); - WriteString(buffer, row.EventId.ToString("D")); - WriteInt64(buffer, row.OccurredAtUtc.Ticks); - WriteString(buffer, row.Actor); - WriteString(buffer, row.Action); - WriteString(buffer, row.Outcome); - WriteString(buffer, row.Category); - WriteString(buffer, row.Target); - WriteString(buffer, row.SourceNode); - WriteString(buffer, row.CorrelationId?.ToString("D")); - WriteString(buffer, row.DetailsJson); - return buffer.ToArray(); - } - - /// - /// The ChainSeq of the chain-start (cutoff) anchor in dbo.AuditLogChainTombstone - /// (ScadaBridge#139). Real links start at 1, so slot 0 is "the link before the first row": - /// the anchor's RowHash is the genesis hash of the cutoff month's chain. - /// - public const long CutoffAnchorSeq = 0; - - /// The fixed EventId the chain-start anchor row carries, so it is recognisable by eye. - public static readonly Guid CutoffAnchorEventId = new("5cadab1d-c4a1-4c0f-a000-000000000139"); - - private static readonly byte[] CutoffDomainTag = Encoding.ASCII.GetBytes("ScadaBridge.AuditLog.ChainCutoff.v1"); - - /// - /// Computes the chain-start anchor hash for a cutoff instant (ScadaBridge#139): - /// SHA-256("ScadaBridge.AuditLog.ChainCutoff.v1" || YYYY-MM || ticks), big-endian like - /// the row form. It is both the anchor row's stored RowHash and the previous-hash the - /// first row of the cutoff month links to, so moving the cutoff breaks that month's chain. - /// - /// The cutoff: rows with an earlier OccurredAtUtc are never chained. - /// The 32-byte anchor hash. - public static byte[] ComputeCutoffAnchorHash(DateTime chainFromUtc) - { - using var buffer = new MemoryStream(64); - buffer.Write(CutoffDomainTag); - WriteString(buffer, PartitionLabel(MonthOf(chainFromUtc))); - WriteInt64(buffer, chainFromUtc.Ticks); - return SHA256.HashData(buffer.ToArray()); - } - - private static void WriteInt64(Stream stream, long value) - { - Span bytes = stackalloc byte[8]; - BinaryPrimitives.WriteInt64BigEndian(bytes, value); - stream.Write(bytes); - } - - private static void WriteString(Stream stream, string? value) - { - if (value is null) - { - stream.WriteByte(0); - return; - } - - stream.WriteByte(1); - var bytes = Encoding.UTF8.GetBytes(value); - Span length = stackalloc byte[4]; - BinaryPrimitives.WriteInt32BigEndian(length, bytes.Length); - stream.Write(length); - stream.Write(bytes); - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainHeadComparer.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainHeadComparer.cs deleted file mode 100644 index d4c20006..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainHeadComparer.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System.Globalization; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; - -/// -/// Pure comparison of one exported chain head (ScadaBridge#107 item 1) with the database's -/// current state for that month. Kept free of I/O so every verdict is unit-testable; the -/// database side supplies the head snapshot and, when needed, the link stored at the -/// exported sequence number. -/// -public static class AuditChainHeadComparer -{ - /// What the database stores at one chain sequence number. - /// The RowHash of the live row carrying the sequence, or null. - /// The RowHash of a per-channel purge tombstone carrying it, or null. - public readonly record struct StoredLink(byte[]? RowHash, byte[]? TombstoneHash); - - /// Compares one exported head with the database. - /// The exported head. - /// The database head for the record's month, or null when there is none. - /// Reads the stored link at a sequence number (only called when the chain has grown). - /// The verdict. - public static async Task CompareAsync( - AuditChainHeadRecord record, AuditChainHeadSnapshot? head, Func> linkAt) - { - ArgumentNullException.ThrowIfNull(record); - ArgumentNullException.ThrowIfNull(linkAt); - - AuditChainHeadCheck Result(AuditChainHeadStatus status, string detail) => - new(record.Month, record.LastSeq, record.ExportedAtUtc, record.Node, status, head?.LastSeq, detail); - - if (!TryParseHex(record.LastHash, out var exported)) - { - return Result(AuditChainHeadStatus.Mismatch, $"Exported hash '{record.LastHash}' is not 32 bytes of hex."); - } - - if (head is null) - { - return Result(AuditChainHeadStatus.MissingHead, - $"{record.Month} has no chain head in the database, but {record.LastSeq} links were exported for it."); - } - - if (head.LastSeq < record.LastSeq) - { - return Result(AuditChainHeadStatus.Regressed, - $"Database head is at sequence {head.LastSeq}, below the exported {record.LastSeq}: links were removed and the head rolled back."); - } - - if (head.LastSeq == record.LastSeq) - { - if (!exported.AsSpan().SequenceEqual(head.LastHash)) - { - return Result(AuditChainHeadStatus.Mismatch, - $"Database head hash at sequence {head.LastSeq} is {ToHex(head.LastHash)}, exported {record.LastHash}: the chain was rewritten."); - } - - // No link was added since the export. Unless a partition purge set (or moved) the - // anchor since, rows + tombstones are invariant: the per-channel purge turns a row - // into a tombstone, nothing else may remove a chained row. - if (head.PurgedThroughSeq == record.PurgedThroughSeq - && head.ChainedRows + head.Tombstones != record.ChainedRows + record.Tombstones) - { - return Result(AuditChainHeadStatus.CountMismatch, - $"Chained rows + tombstones are {head.ChainedRows} + {head.Tombstones} in the database, {record.ChainedRows} + {record.Tombstones} exported."); - } - - return Result(AuditChainHeadStatus.Matches, "Database head equals the exported head."); - } - - // The chain has grown: the link at the exported sequence must still carry the exported hash. - if (head.PurgedThroughSeq is { } anchor && record.LastSeq <= anchor) - { - if (record.LastSeq < anchor) - { - return Result(AuditChainHeadStatus.Purged, - $"Sequence {record.LastSeq} was removed by a partition purge (anchor at {anchor}); verify the month's archive."); - } - - return head.PurgedThroughHash is { } anchorHash && exported.AsSpan().SequenceEqual(anchorHash) - ? Result(AuditChainHeadStatus.Extended, $"Exported head is the partition-purge anchor at {anchor}.") - : Result(AuditChainHeadStatus.Mismatch, $"Partition-purge anchor at {anchor} does not carry the exported hash."); - } - - var link = await linkAt(record.LastSeq).ConfigureAwait(false); - var stored = link.RowHash ?? link.TombstoneHash; - if (stored is null) - { - return Result(AuditChainHeadStatus.MissingLink, - $"No row, tombstone or anchor carries sequence {record.LastSeq}: a chained row was deleted outside the purge paths."); - } - - return exported.AsSpan().SequenceEqual(stored) - ? Result(AuditChainHeadStatus.Extended, - $"Chain grew to {head.LastSeq}; the {(link.RowHash is null ? "tombstone" : "row")} at {record.LastSeq} still carries the exported hash.") - : Result(AuditChainHeadStatus.Mismatch, - $"The {(link.RowHash is null ? "tombstone" : "row")} at sequence {record.LastSeq} carries {ToHex(stored)}, exported {record.LastHash}: the chain was rewritten."); - } - - /// Lowercase hex of a hash. - /// The bytes. - /// Hex string. - public static string ToHex(byte[] bytes) => Convert.ToHexString(bytes).ToLowerInvariant(); - - /// Parses a YYYY-MM month into its first instant (UTC). - /// The month text. - /// The parsed month. - /// True when well-formed. - public static bool TryParseMonth(string? month, out DateTime monthStart) - { - if (DateTime.TryParseExact(month, "yyyy-MM", CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsed)) - { - monthStart = new DateTime(parsed.Year, parsed.Month, 1, 0, 0, 0, DateTimeKind.Utc); - return true; - } - - monthStart = default; - return false; - } - - private static bool TryParseHex(string? hex, out byte[] bytes) - { - bytes = []; - if (hex is null || hex.Length != AuditChainHasher.HashLength * 2) - { - return false; - } - - try - { - bytes = Convert.FromHexString(hex); - return true; - } - catch (FormatException) - { - return false; - } - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainVerifier.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainVerifier.cs deleted file mode 100644 index b5226c6e..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditChainVerifier.cs +++ /dev/null @@ -1,252 +0,0 @@ -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; - -/// A month's chain head as verification sees it (ScadaBridge#41). -/// Last sequence number linked. -/// RowHash at . -/// Checkpoint anchor left by a partition-switch purge, or null. -/// RowHash at , or null. -public sealed record AuditChainHeadState( - long LastSeq, - byte[] LastHash, - long? PurgedThroughSeq, - byte[]? PurgedThroughHash); - -/// -/// Streaming verifier for one month's AuditLog hash chain (ScadaBridge#41). Feed it the -/// month's chained rows in ChainSeq order via , then call -/// . Storage-agnostic so the detection rules are unit-testable -/// without SQL Server. -/// -/// -/// -/// Verification starts at the checkpoint anchor (PurgedThroughSeq + 1, previous -/// link PurgedThroughHash) when a partition purge left one, otherwise at sequence 1 -/// with the genesis hash. Each row's hash is recomputed from the previous link and its own -/// stored columns; a sequence number with no row is bridged only by a purge tombstone. At -/// the end the chain must land exactly on the head's (LastSeq, LastHash), which is -/// what catches deletion of the newest rows. -/// -/// -/// Detection, by tampering act: an edited row → -/// at that row; a deleted row → at its -/// sequence number (or if it was the tail); -/// reordered rows (ChainSeq values swapped) → -/// at the first moved row, because the sequence number is inside the hashed bytes. -/// -/// -/// Chain start (cutoff, ScadaBridge#139). When the chain was enabled with no backlog, -/// a link-0 anchor in the tombstones of the cutoff month is that month's genesis hash (instead -/// of 32 zero bytes), and no row before the cutoff is ever chained. So a chained row whose -/// OccurredAtUtc is before the cutoff is a break (): -/// either the row's timestamp was moved back to hide it among the unprotected rows, or the -/// cutoff was moved forward. An anchor that does not hash to its own cutoff is reported as -/// by the caller. -/// -/// -public sealed class AuditChainVerifier -{ - private readonly DateTime _monthStartUtc; - private readonly string _partition; - private readonly AuditChainHeadState? _head; - private readonly IReadOnlyDictionary _tombstones; - private readonly long _startAfter; - private readonly DateTime? _chainFromUtc; - private byte[] _previous; - private long _expected; - private long _verified; - private long _tombstonesApplied; - private AuditChainBreak? _break; - - /// Initializes a verifier for one month. - /// The chain partition (first instant of the month, UTC). - /// The month's chain head, or null when none exists. - /// - /// Purge tombstones for the month, keyed by ChainSeq; a key of - /// is the chain-start anchor (the month's genesis). - /// - /// The chain cutoff, or null when the whole table is in scope (legacy / backlog chaining). - /// When non-null, the chain-start anchor is invalid; verification fails with this detail. - public AuditChainVerifier( - DateTime monthStartUtc, - AuditChainHeadState? head, - IReadOnlyDictionary tombstones, - DateTime? chainFromUtc = null, - string? anchorProblem = null) - { - _monthStartUtc = monthStartUtc; - _partition = AuditChainHasher.PartitionLabel(monthStartUtc); - _head = head; - _tombstones = tombstones ?? throw new ArgumentNullException(nameof(tombstones)); - - if (head?.PurgedThroughSeq is { } purgedThrough && head.PurgedThroughHash is { } anchor) - { - _startAfter = purgedThrough; - _previous = anchor; - } - else - { - _startAfter = 0; - _previous = _tombstones.TryGetValue(AuditChainHasher.CutoffAnchorSeq, out var cutoffAnchor) - ? cutoffAnchor - : AuditChainHasher.GenesisHash.ToArray(); - } - - _expected = _startAfter + 1; - _chainFromUtc = chainFromUtc; - if (anchorProblem is not null) - { - Fail(AuditChainHasher.CutoffAnchorSeq, AuditChainHasher.CutoffAnchorEventId, - AuditChainBreakKind.CutoffAnchorInvalid, anchorProblem); - } - } - - /// The chain cutoff this verification applies, or null. - public DateTime? ChainFromUtc => _chainFromUtc; - - /// The chain partition label (YYYY-MM). - public string Partition => _partition; - - /// The checkpoint anchor this verification resumed after, or null. - public long? PurgedThroughSeq => _head?.PurgedThroughSeq; - - /// True once a break has been found; further rows are ignored. - public bool IsBroken => _break is not null; - - /// Feeds the next chained row (ascending ChainSeq). - /// The row; its must be non-null. - /// False once a break has been found, so the caller can stop reading. - public bool Accept(AuditChainRow row) - { - ArgumentNullException.ThrowIfNull(row); - if (_break is not null) - { - return false; - } - - var seq = row.ChainSeq ?? throw new ArgumentException("Only chained rows can be verified.", nameof(row)); - - // Rows at or below a partition-purge anchor cannot exist (the switch removed the - // whole month's partition); ignore rather than mis-report them. - if (seq <= _startAfter) - { - return true; - } - - if (_chainFromUtc is { } chainFrom && row.OccurredAtUtc < chainFrom) - { - return Fail(seq, row.EventId, AuditChainBreakKind.ChainedBeforeCutoff, - $"Row at ChainSeq {seq} is dated {row.OccurredAtUtc:O}, before the chain cutoff {chainFrom:O}: " - + "no row before the cutoff is ever chained, so its timestamp was moved back or the cutoff was moved forward."); - } - - if (seq < _expected) - { - return Fail(seq, row.EventId, AuditChainBreakKind.DuplicateSequence, - $"ChainSeq {seq} appears more than once."); - } - - while (_expected < seq) - { - if (!_tombstones.TryGetValue(_expected, out var tombstone)) - { - return Fail(_expected, row.EventId, AuditChainBreakKind.MissingRow, - $"ChainSeq {_expected} is missing and no purge tombstone covers it; " - + $"the next chained row is ChainSeq {seq} (EventId {row.EventId})."); - } - - _previous = tombstone; - _tombstonesApplied++; - _expected++; - } - - if (row.RowHash is not { Length: AuditChainHasher.HashLength } stored) - { - return Fail(seq, row.EventId, AuditChainBreakKind.IncompleteLink, - $"Row at ChainSeq {seq} has a sequence number but no 32-byte RowHash."); - } - - var computed = AuditChainHasher.ComputeRowHash(_previous, _monthStartUtc, seq, row); - if (!computed.AsSpan().SequenceEqual(stored)) - { - return Fail(seq, row.EventId, AuditChainBreakKind.HashMismatch, - $"Recomputed hash of the row at ChainSeq {seq} does not match its stored RowHash: " - + "the row was edited, moved to a different position, or an earlier link was altered."); - } - - _previous = stored; - _expected++; - _verified++; - return true; - } - - /// Finishes verification against the chain head. - /// Rows in the month, at or after the cutoff, not yet chained (reported, never a break). - /// Rows in the month before the chain cutoff: out of scope, never chained (reported, never a break). - /// The verification result. - public AuditChainVerificationResult Complete(long unchainedRows, long preCutoffRows = 0) - { - if (_break is null) - { - // A purge tombstone may cover the newest links. - while (_tombstones.TryGetValue(_expected, out var tombstone)) - { - _previous = tombstone; - _tombstonesApplied++; - _expected++; - } - - var lastSeq = _expected - 1; - if (_head is null) - { - if (lastSeq > _startAfter) - { - Fail(lastSeq, null, AuditChainBreakKind.HeadMismatch, - $"Chained rows exist through ChainSeq {lastSeq} but the month has no chain head."); - } - else - { - return Result(AuditChainStatus.NotChained, unchainedRows, preCutoffRows); - } - } - else if (lastSeq < _head.LastSeq) - { - Fail(lastSeq + 1, null, AuditChainBreakKind.HeadMismatch, - $"The chain ends at ChainSeq {lastSeq} but the head records {_head.LastSeq}: " - + $"ChainSeq {lastSeq + 1}..{_head.LastSeq} are missing (rows deleted from the end)."); - } - else if (lastSeq > _head.LastSeq) - { - Fail(_head.LastSeq + 1, null, AuditChainBreakKind.HeadMismatch, - $"The chain continues to ChainSeq {lastSeq} past the head's {_head.LastSeq}."); - } - else if (!_previous.AsSpan().SequenceEqual(_head.LastHash)) - { - Fail(lastSeq, null, AuditChainBreakKind.HeadMismatch, - $"The hash at ChainSeq {lastSeq} does not match the chain head's recorded hash."); - } - } - - return Result(_break is null ? AuditChainStatus.Intact : AuditChainStatus.Broken, unchainedRows, preCutoffRows); - } - - private AuditChainVerificationResult Result(AuditChainStatus status, long unchainedRows, long preCutoffRows) - => new( - _partition, - status, - _verified, - _tombstonesApplied, - _head?.PurgedThroughSeq, - _head?.LastSeq, - unchainedRows, - _break, - preCutoffRows, - _chainFromUtc); - - private bool Fail(long seq, Guid? eventId, AuditChainBreakKind kind, string detail) - { - _break ??= new AuditChainBreak(_partition, seq, eventId, kind, detail); - return false; - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogArchiveOptions.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogArchiveOptions.cs index 839de381..f433f9ca 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogArchiveOptions.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogArchiveOptions.cs @@ -38,12 +38,6 @@ public sealed class AuditLogArchiveOptions /// public int ReadCommandTimeoutMinutes { get; set; } = 60; - /// - /// How long, in seconds, archiving waits for a chaining pass or another purge to release - /// the hash-chain lock before giving up for this tick (default 300). - /// - public int ChainLockTimeoutSeconds { get; set; } = 300; - /// /// Archive retention in days, counted from the END of the archived month (ScadaBridge#109). /// 0 (the default) = archives are never deleted — until compliance names a period. diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogHashChain.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogHashChain.cs deleted file mode 100644 index d154b8a2..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogHashChain.cs +++ /dev/null @@ -1,729 +0,0 @@ -using System.Data; -using System.Data.Common; -using System.Text; -using Microsoft.Data.SqlClient; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; - -/// -/// The SQL Server application lock that serialises every writer of AuditLog hash-chain -/// state (ScadaBridge#41): the chaining job and both purge paths take it EXCLUSIVE; -/// verification takes it SHARED so it reads a chain no writer is half-way through. -/// -public static class AuditChainLock -{ - /// The sp_getapplock resource name. - public const string Resource = "ScadaBridge.AuditLogHashChain"; -} - -/// -/// SQL Server implementation of (ScadaBridge#41, -/// alog.md §11.4). See the interface for the chain model. -/// -/// -/// -/// Concurrency. Two central nodes and the site-ingest path INSERT into -/// dbo.AuditLog concurrently; none of them touches the chain. The chaining pass runs -/// in one transaction that first takes the exclusive with a -/// zero timeout (the peer node's pass simply skips its tick), then reads the head row with -/// UPDLOCK, HOLDLOCK and advances it with an optimistic LastSeq check. Every -/// link UPDATE is guarded by ChainSeq IS NULL and its row count is checked, so a row -/// purged or chained underneath the pass rolls the whole pass back instead of leaving a gap. -/// A row INSERTed while the pass runs is invisible to it (READ COMMITTED) and is chained on -/// a later pass with a later sequence number — chain order is chaining order, not -/// OccurredAtUtc order, which is why late-arriving site rows need no special case. -/// -/// -/// Chain start (ScadaBridge#139). Unless the caller asks for backlog chaining, the first -/// pass on a pristine database (no chain head, no chained row) records a chain-start -/// anchor and only rows dated at or after it are ever chained; the historical backlog stays -/// unchained and out of scope. The anchor is one row in dbo.AuditLogChainTombstone at -/// ChainSeq = 0 of the cutoff month: PurgedAtUtc holds the cutoff (the database's -/// SYSUTCDATETIME() at first enablement, so both central nodes see the same instant -/// whatever their own clocks say), RowHash holds -/// of it, and that hash is the genesis -/// the cutoff month's first row links to. It is written once, under the exclusive chain lock, -/// and never updated; the partition-switch purge keeps it. A database that already has a -/// chain (heads or chained rows) and no anchor keeps chaining every row, as before. -/// -/// -/// Append-only. The only AuditLog columns this class writes are ChainSeq and -/// RowHash, and only while they are NULL. In production that UPDATE runs under the -/// scadabridge_audit_chainer role, which holds a COLUMN-level UPDATE grant on just -/// those two columns; the ingest writer role keeps its table-wide DENY UPDATE. -/// -/// -public sealed class AuditLogHashChain : IAuditLogHashChain, IAuditChainHeadStore -{ - // 4 parameters per row; 200 rows = 800 parameters, far under SQL Server's 2,100 cap. - private const int UpdateChunkRows = 200; - - /// The ten writable AuditLog columns, in table order — the hashed row content. - internal const string RowColumns = - "EventId, OccurredAtUtc, Actor, Action, Outcome, Category, Target, SourceNode, CorrelationId, DetailsJson"; - - private readonly ScadaBridgeDbContext _context; - private readonly ILogger _logger; - - /// Initializes a new instance of the class. - /// The database context whose connection is used. - /// Optional logger. - public AuditLogHashChain(ScadaBridgeDbContext context, ILogger? logger = null) - { - _context = context ?? throw new ArgumentNullException(nameof(context)); - _logger = logger ?? NullLogger.Instance; - } - - /// - /// Command timeout for verification statements, in seconds. A month can hold millions - /// of rows; the management Ask (long-running, 5 min) is the real deadline. - /// - public int VerifyCommandTimeoutSeconds { get; init; } = 300; - - /// - /// Test seam: the cutoff a new chain-start anchor records. Null (production) uses the - /// database's SYSUTCDATETIME() so both central nodes agree regardless of node clocks. - /// - internal Func? AnchorClock { get; init; } - - /// How long verification waits for an in-flight chaining/purge pass to release the lock. - public TimeSpan VerifyLockTimeout { get; init; } = TimeSpan.FromSeconds(60); - - /// - public async Task ChainPendingAsync(int batchSize, bool chainBacklog = false, CancellationToken ct = default) - { - if (batchSize <= 0) - { - throw new ArgumentOutOfRangeException(nameof(batchSize), batchSize, "Batch size must be > 0."); - } - - var conn = _context.Database.GetDbConnection(); - var openedHere = await OpenAsync(conn, ct).ConfigureAwait(false); - try - { - await using var tx = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct).ConfigureAwait(false); - - if (!await TryAcquireLockAsync(conn, tx, "Exclusive", TimeSpan.Zero, ct).ConfigureAwait(false)) - { - await tx.RollbackAsync(ct).ConfigureAwait(false); - return new AuditChainBatchResult(0, LockBusy: true); - } - - // Chain start (ScadaBridge#139): the cutoff, recorded on the first pass. - var cutoff = await ReadChainerCutoffAsync(conn, tx, ct).ConfigureAwait(false); - if (cutoff is null && !chainBacklog && await IsPristineAsync(conn, tx, ct).ConfigureAwait(false)) - { - cutoff = await CreateCutoffAnchorAsync(conn, tx, ct).ConfigureAwait(false); - } - - // Oldest month that still has unchained rows in scope. One month per pass keeps the - // candidate read partition-eliminated and index-ordered (no cross-partition sort). - DateTime? oldest; - await using (var cmd = Command(conn, tx, - "SELECT TOP (1) OccurredAtUtc FROM dbo.AuditLog WHERE ChainSeq IS NULL AND OccurredAtUtc >= @chainFrom ORDER BY OccurredAtUtc;")) - { - AddChainFrom(cmd, cutoff?.ChainFromUtc); - var scalar = await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false); - oldest = scalar is DateTime dt ? DateTime.SpecifyKind(dt, DateTimeKind.Utc) : null; - } - - if (oldest is null) - { - await tx.CommitAsync(ct).ConfigureAwait(false); - return new AuditChainBatchResult(0, LockBusy: false); - } - - var month = AuditChainHasher.MonthOf(oldest.Value); - var candidates = new List(Math.Min(batchSize, 4096)); - await using (var cmd = Command(conn, tx, - $"SELECT TOP (@batch) {RowColumns} FROM dbo.AuditLog " + - "WHERE ChainSeq IS NULL AND OccurredAtUtc >= @monthStart AND OccurredAtUtc < @monthEnd " + - "AND OccurredAtUtc >= @chainFrom " + - "ORDER BY OccurredAtUtc, EventId;")) - { - AddInt(cmd, "@batch", batchSize); - AddMonthRange(cmd, month); - AddChainFrom(cmd, cutoff?.ChainFromUtc); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - candidates.Add(ReadRow(reader, withLink: false)); - } - } - - // Current chain end for the month, locked until commit. - long? headSeq = null; - byte[] previous = AuditChainHasher.GenesisHash.ToArray(); - await using (var cmd = Command(conn, tx, - "SELECT LastSeq, LastHash FROM dbo.AuditLogChainHead WITH (UPDLOCK, HOLDLOCK) WHERE ChainMonth = @month;")) - { - AddDate(cmd, "@month", month); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - if (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - headSeq = reader.GetInt64(0); - previous = (byte[])reader.GetValue(1); - } - } - - // The cutoff month's chain starts from the anchor, not from 32 zero bytes, so the - // cutoff is inside every later link of that month. - if (headSeq is null && cutoff is not null && cutoff.Month == month) - { - previous = cutoff.AnchorHash.ToArray(); - } - - var seq = headSeq ?? 0; - var links = new List<(AuditChainRow Row, long Seq, byte[] Hash)>(candidates.Count); - foreach (var row in candidates) - { - seq++; - var hash = AuditChainHasher.ComputeRowHash(previous, month, seq, row); - links.Add((row, seq, hash)); - previous = hash; - } - - for (var offset = 0; offset < links.Count; offset += UpdateChunkRows) - { - var chunk = links.Skip(offset).Take(UpdateChunkRows).ToList(); - var updated = await WriteLinksAsync(conn, tx, chunk, ct).ConfigureAwait(false); - if (updated != chunk.Count) - { - // A candidate was purged (or chained by a pass that bypassed the lock) - // between the read and the write. Linking the rest would leave a gap. - _logger.LogWarning( - "AuditLog hash chain: {Updated} of {Expected} links written for {Partition}; rolling the pass back to retry.", - updated, chunk.Count, AuditChainHasher.PartitionLabel(month)); - await tx.RollbackAsync(ct).ConfigureAwait(false); - return new AuditChainBatchResult(0, LockBusy: false); - } - } - - if (links.Count > 0 && !await AdvanceHeadAsync(conn, tx, month, headSeq, seq, previous, ct).ConfigureAwait(false)) - { - _logger.LogWarning( - "AuditLog hash chain: head for {Partition} moved underneath the pass; rolling back to retry.", - AuditChainHasher.PartitionLabel(month)); - await tx.RollbackAsync(ct).ConfigureAwait(false); - return new AuditChainBatchResult(0, LockBusy: false); - } - - await tx.CommitAsync(ct).ConfigureAwait(false); - return new AuditChainBatchResult(links.Count, LockBusy: false); - } - finally - { - if (openedHere) - { - await conn.CloseAsync().ConfigureAwait(false); - } - } - } - - /// - public async Task VerifyMonthAsync(DateTime monthStartUtc, CancellationToken ct = default) - { - var month = AuditChainHasher.MonthOf(DateTime.SpecifyKind(monthStartUtc, DateTimeKind.Utc)); - - var conn = _context.Database.GetDbConnection(); - var openedHere = await OpenAsync(conn, ct).ConfigureAwait(false); - try - { - await using var tx = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct).ConfigureAwait(false); - if (!await TryAcquireLockAsync(conn, tx, "Shared", VerifyLockTimeout, ct).ConfigureAwait(false)) - { - await tx.RollbackAsync(ct).ConfigureAwait(false); - throw new TimeoutException( - $"The AuditLog hash chain was busy (chaining or purge in progress) for {VerifyLockTimeout.TotalSeconds:0}s; retry."); - } - - AuditChainHeadState? head = null; - await using (var cmd = Command(conn, tx, - "SELECT LastSeq, LastHash, PurgedThroughSeq, PurgedThroughHash FROM dbo.AuditLogChainHead WHERE ChainMonth = @month;")) - { - AddDate(cmd, "@month", month); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - if (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - head = new AuditChainHeadState( - reader.GetInt64(0), - (byte[])reader.GetValue(1), - reader.IsDBNull(2) ? null : reader.GetInt64(2), - reader.IsDBNull(3) ? null : (byte[])reader.GetValue(3)); - } - } - - var cutoff = await ReadVerifierCutoffAsync(conn, tx, ct).ConfigureAwait(false); - - var tombstones = new Dictionary(); - await using (var cmd = Command(conn, tx, - "SELECT ChainSeq, RowHash FROM dbo.AuditLogChainTombstone WHERE ChainMonth = @month;")) - { - AddDate(cmd, "@month", month); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - tombstones[reader.GetInt64(0)] = (byte[])reader.GetValue(1); - } - } - - // Unchained rows split at the cutoff: pending (in scope, not reached yet) versus - // pre-cutoff (out of scope by design, never chained). - long unchained = 0, preCutoff = 0; - await using (var cmd = Command(conn, tx, - "SELECT COUNT_BIG(CASE WHEN OccurredAtUtc >= @chainFrom THEN 1 END), COUNT_BIG(CASE WHEN OccurredAtUtc < @chainFrom THEN 1 END) " + - "FROM dbo.AuditLog WHERE ChainSeq IS NULL AND OccurredAtUtc >= @monthStart AND OccurredAtUtc < @monthEnd;")) - { - cmd.CommandTimeout = VerifyCommandTimeoutSeconds; - AddMonthRange(cmd, month); - AddChainFrom(cmd, cutoff.ChainFromUtc); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - if (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - unchained = reader.GetInt64(0); - preCutoff = reader.GetInt64(1); - } - } - - var verifier = new AuditChainVerifier(month, head, tombstones, cutoff.ChainFromUtc, cutoff.Problem); - await using (var cmd = Command(conn, tx, - $"SELECT {RowColumns}, ChainSeq, RowHash FROM dbo.AuditLog " + - "WHERE ChainSeq IS NOT NULL AND OccurredAtUtc >= @monthStart AND OccurredAtUtc < @monthEnd " + - "ORDER BY ChainSeq;")) - { - cmd.CommandTimeout = VerifyCommandTimeoutSeconds; - AddMonthRange(cmd, month); - await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess, ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - if (!verifier.Accept(ReadRow(reader, withLink: true))) - { - break; - } - } - } - - await tx.CommitAsync(ct).ConfigureAwait(false); - return verifier.Complete(unchained, preCutoff); - } - finally - { - if (openedHere) - { - await conn.CloseAsync().ConfigureAwait(false); - } - } - } - - /// - public async Task> ReadHeadsAsync(CancellationToken ct = default) - { - var conn = _context.Database.GetDbConnection(); - var openedHere = await OpenAsync(conn, ct).ConfigureAwait(false); - try - { - await using var tx = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct).ConfigureAwait(false); - await AcquireSharedOrThrowAsync(conn, tx, ct).ConfigureAwait(false); - var heads = await ReadHeadsCoreAsync(conn, tx, ct).ConfigureAwait(false); - await tx.CommitAsync(ct).ConfigureAwait(false); - return heads; - } - finally - { - if (openedHere) - { - await conn.CloseAsync().ConfigureAwait(false); - } - } - } - - /// - public async Task VerifyExportedHeadsAsync( - IReadOnlyList records, CancellationToken ct = default) - { - ArgumentNullException.ThrowIfNull(records); - - // Collapse duplicates (both centrals export, and every unchanged re-export repeats a - // record): one check per distinct (month, sequence, hash) claim. - var distinct = records - .GroupBy(r => (r.Month, r.LastSeq, Hash: r.LastHash?.ToLowerInvariant())) - .Select(g => g.OrderByDescending(r => r.ExportedAtUtc).First()) - .ToList(); - - var conn = _context.Database.GetDbConnection(); - var openedHere = await OpenAsync(conn, ct).ConfigureAwait(false); - try - { - await using var tx = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct).ConfigureAwait(false); - await AcquireSharedOrThrowAsync(conn, tx, ct).ConfigureAwait(false); - var heads = (await ReadHeadsCoreAsync(conn, tx, ct).ConfigureAwait(false)) - .ToDictionary(h => AuditChainHasher.PartitionLabel(h.Month), StringComparer.Ordinal); - - var checks = new List(distinct.Count); - foreach (var record in distinct) - { - if (!AuditChainHeadComparer.TryParseMonth(record.Month, out var month)) - { - checks.Add(new AuditChainHeadCheck(record.Month ?? "", record.LastSeq, record.ExportedAtUtc, record.Node, - AuditChainHeadStatus.Mismatch, null, $"Exported month '{record.Month}' is not YYYY-MM.")); - continue; - } - - heads.TryGetValue(AuditChainHasher.PartitionLabel(month), out var head); - checks.Add(await AuditChainHeadComparer.CompareAsync( - record, head, seq => ReadStoredLinkAsync(conn, tx, month, seq, ct)).ConfigureAwait(false)); - } - - await tx.CommitAsync(ct).ConfigureAwait(false); - var ordered = checks - .OrderByDescending(c => c.IsFailure) - .ThenBy(c => c.Month, StringComparer.Ordinal) - .ThenBy(c => c.ExportedSeq) - .ToList(); - return new AuditChainHeadVerificationResult(ordered.All(c => !c.IsFailure), ordered.Count, ordered); - } - finally - { - if (openedHere) - { - await conn.CloseAsync().ConfigureAwait(false); - } - } - } - - private async Task AcquireSharedOrThrowAsync(DbConnection conn, DbTransaction tx, CancellationToken ct) - { - if (!await TryAcquireLockAsync(conn, tx, "Shared", VerifyLockTimeout, ct).ConfigureAwait(false)) - { - await tx.RollbackAsync(ct).ConfigureAwait(false); - throw new TimeoutException( - $"The AuditLog hash chain was busy (chaining or purge in progress) for {VerifyLockTimeout.TotalSeconds:0}s; retry."); - } - } - - private async Task> ReadHeadsCoreAsync(DbConnection conn, DbTransaction tx, CancellationToken ct) - { - // Per month: the head row, the chained rows still present (the filtered ChainSeq index, - // partition-eliminated by the month range) and the tombstones. The head table holds one - // row per month ever chained, so this is a few dozen rows. - var heads = new List(); - await using var cmd = Command(conn, tx, @" -SELECT h.ChainMonth, h.LastSeq, h.LastHash, h.PurgedThroughSeq, h.PurgedThroughHash, h.UpdatedAtUtc, - (SELECT COUNT_BIG(*) FROM dbo.AuditLog a - WHERE a.ChainSeq IS NOT NULL - AND a.OccurredAtUtc >= CAST(h.ChainMonth AS datetime2(7)) - AND a.OccurredAtUtc < DATEADD(MONTH, 1, CAST(h.ChainMonth AS datetime2(7)))) AS ChainedRows, - (SELECT COUNT_BIG(*) FROM dbo.AuditLogChainTombstone t WHERE t.ChainMonth = h.ChainMonth AND t.ChainSeq > 0) AS Tombstones -FROM dbo.AuditLogChainHead h -ORDER BY h.ChainMonth;"); - cmd.CommandTimeout = VerifyCommandTimeoutSeconds; - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - heads.Add(new AuditChainHeadSnapshot( - DateTime.SpecifyKind(reader.GetDateTime(0), DateTimeKind.Utc), - reader.GetInt64(1), - (byte[])reader.GetValue(2), - reader.IsDBNull(3) ? null : reader.GetInt64(3), - reader.IsDBNull(4) ? null : (byte[])reader.GetValue(4), - reader.GetInt64(6), - reader.GetInt64(7), - DateTime.SpecifyKind(reader.GetDateTime(5), DateTimeKind.Utc))); - } - - return heads; - } - - private async Task ReadStoredLinkAsync( - DbConnection conn, DbTransaction tx, DateTime month, long seq, CancellationToken ct) - { - byte[]? rowHash; - await using (var cmd = Command(conn, tx, - "SELECT TOP (1) RowHash FROM dbo.AuditLog WHERE ChainSeq = @seq AND OccurredAtUtc >= @monthStart AND OccurredAtUtc < @monthEnd;")) - { - cmd.CommandTimeout = VerifyCommandTimeoutSeconds; - cmd.Parameters.Add(new SqlParameter("@seq", SqlDbType.BigInt) { Value = seq }); - AddMonthRange(cmd, month); - rowHash = await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false) as byte[]; - } - - byte[]? tombstoneHash = null; - if (rowHash is null) - { - await using var cmd = Command(conn, tx, - "SELECT RowHash FROM dbo.AuditLogChainTombstone WHERE ChainMonth = @month AND ChainSeq = @seq;"); - AddDate(cmd, "@month", month); - cmd.Parameters.Add(new SqlParameter("@seq", SqlDbType.BigInt) { Value = seq }); - tombstoneHash = await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false) as byte[]; - } - - return new AuditChainHeadComparer.StoredLink(rowHash, tombstoneHash); - } - - /// The chain-start anchor as the chainer uses it (ScadaBridge#139). - /// The cutoff. - /// The cutoff month (the anchor's ChainMonth). - /// The anchor's stored hash: the cutoff month's genesis. - private sealed record ChainCutoff(DateTime ChainFromUtc, DateTime Month, byte[] AnchorHash); - - /// The cutoff as verification sees it (ScadaBridge#139). - /// The cutoff, or null when the chain covers the whole table. - /// Why the anchor cannot be trusted, or null. - private sealed record VerifierCutoff(DateTime? ChainFromUtc, string? Problem); - - /// - /// Reads the chain-start anchor for the chaining pass. It can only sit in a month at or - /// before the earliest chain head (it is written before anything is chained, and nothing - /// before it ever is), so the read is bounded to those months rather than scanning every - /// per-channel purge tombstone each tick. Verification reads them all. - /// - private static async Task ReadChainerCutoffAsync(DbConnection conn, DbTransaction tx, CancellationToken ct) - { - await using var cmd = Command(conn, tx, - "SELECT TOP (1) ChainMonth, RowHash, PurgedAtUtc FROM dbo.AuditLogChainTombstone " + - "WHERE ChainSeq = 0 AND ChainMonth <= ISNULL((SELECT MIN(ChainMonth) FROM dbo.AuditLogChainHead), '9999-12-01') " + - "ORDER BY PurgedAtUtc, ChainMonth;"); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - if (!await reader.ReadAsync(ct).ConfigureAwait(false)) - { - return null; - } - - return new ChainCutoff( - DateTime.SpecifyKind(reader.GetDateTime(2), DateTimeKind.Utc), - DateTime.SpecifyKind(reader.GetDateTime(0), DateTimeKind.Utc), - (byte[])reader.GetValue(1)); - } - - /// - /// Reads every chain-start anchor and checks it: exactly one, in its cutoff's month, hashing - /// to its own cutoff. The earliest anchor's cutoff is used either way, matching the chainer. - /// - private async Task ReadVerifierCutoffAsync(DbConnection conn, DbTransaction tx, CancellationToken ct) - { - var anchors = new List<(DateTime Month, byte[] Hash, DateTime ChainFrom)>(); - await using (var cmd = Command(conn, tx, - "SELECT ChainMonth, RowHash, PurgedAtUtc FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0 ORDER BY PurgedAtUtc, ChainMonth;")) - { - cmd.CommandTimeout = VerifyCommandTimeoutSeconds; - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - anchors.Add(( - DateTime.SpecifyKind(reader.GetDateTime(0), DateTimeKind.Utc), - (byte[])reader.GetValue(1), - DateTime.SpecifyKind(reader.GetDateTime(2), DateTimeKind.Utc))); - } - } - - if (anchors.Count == 0) - { - return new VerifierCutoff(null, null); - } - - var (anchorMonth, hash, chainFrom) = anchors[0]; - string? problem = null; - if (anchors.Count > 1) - { - problem = $"{anchors.Count} chain-start anchors exist (cutoffs {string.Join(", ", anchors.Select(a => a.ChainFrom.ToString("O", System.Globalization.CultureInfo.InvariantCulture)))}); " - + "exactly one is ever written."; - } - else if (anchorMonth != AuditChainHasher.MonthOf(chainFrom)) - { - problem = $"The chain-start anchor is filed under {AuditChainHasher.PartitionLabel(anchorMonth)} but its cutoff {chainFrom:O} is in another month."; - } - else if (!AuditChainHasher.ComputeCutoffAnchorHash(chainFrom).AsSpan().SequenceEqual(hash)) - { - problem = $"The chain-start anchor's hash does not match its cutoff {chainFrom:O}: the cutoff or the anchor was altered."; - } - - return new VerifierCutoff(chainFrom, problem); - } - - /// True when nothing has ever been chained: no chain head and no chained row. - private static async Task IsPristineAsync(DbConnection conn, DbTransaction tx, CancellationToken ct) - { - await using var cmd = Command(conn, tx, - "SELECT CASE WHEN EXISTS (SELECT 1 FROM dbo.AuditLogChainHead) " + - "OR EXISTS (SELECT 1 FROM dbo.AuditLog WHERE ChainSeq IS NOT NULL) THEN 0 ELSE 1 END;"); - return Convert.ToInt32(await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false)) == 1; - } - - /// - /// Writes the chain-start anchor at the database's current UTC time (ScadaBridge#139). Runs - /// inside the chaining transaction, under the exclusive chain lock, so exactly one node - /// writes it and the peer's next pass reads it. - /// - private async Task CreateCutoffAnchorAsync(DbConnection conn, DbTransaction tx, CancellationToken ct) - { - DateTime chainFrom; - if (AnchorClock is { } clock) - { - chainFrom = DateTime.SpecifyKind(clock(), DateTimeKind.Utc); - } - else - { - await using var cmd = Command(conn, tx, "SELECT SYSUTCDATETIME();"); - chainFrom = DateTime.SpecifyKind((DateTime)(await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false))!, DateTimeKind.Utc); - } - - var month = AuditChainHasher.MonthOf(chainFrom); - var hash = AuditChainHasher.ComputeCutoffAnchorHash(chainFrom); - await using (var cmd = Command(conn, tx, - "INSERT INTO dbo.AuditLogChainTombstone (ChainMonth, ChainSeq, RowHash, EventId, PurgedAtUtc) " + - "VALUES (@month, 0, @hash, @eventId, @chainFrom);")) - { - AddDate(cmd, "@month", month); - cmd.Parameters.Add(new SqlParameter("@hash", SqlDbType.Binary, AuditChainHasher.HashLength) { Value = hash }); - cmd.Parameters.Add(new SqlParameter("@eventId", SqlDbType.UniqueIdentifier) { Value = AuditChainHasher.CutoffAnchorEventId }); - cmd.Parameters.Add(new SqlParameter("@chainFrom", SqlDbType.DateTime2) { Scale = 7, Value = chainFrom }); - await cmd.ExecuteNonQueryAsync(ct).ConfigureAwait(false); - } - - _logger.LogInformation( - "AuditLog hash chain started at {ChainFrom:O}: only rows dated from then on are chained; earlier rows stay unchained and out of scope (ScadaBridge#139).", - chainFrom); - return new ChainCutoff(chainFrom, month, hash); - } - - private static void AddChainFrom(DbCommand cmd, DateTime? chainFromUtc) - => cmd.Parameters.Add(new SqlParameter("@chainFrom", SqlDbType.DateTime2) { Scale = 7, Value = chainFromUtc ?? DateTime.MinValue }); - - /// - /// Takes the chain application lock inside (released at commit / - /// rollback). Returns false when the lock could not be granted within . - /// - internal static async Task TryAcquireLockAsync( - DbConnection conn, DbTransaction tx, string mode, TimeSpan timeout, CancellationToken ct) - { - await using var cmd = Command(conn, tx, - "DECLARE @result int; " + - "EXEC @result = sp_getapplock @Resource = @resource, @LockMode = @mode, @LockOwner = 'Transaction', @LockTimeout = @timeoutMs; " + - "SELECT @result;"); - cmd.Parameters.Add(new SqlParameter("@resource", SqlDbType.NVarChar, 255) { Value = AuditChainLock.Resource }); - cmd.Parameters.Add(new SqlParameter("@mode", SqlDbType.VarChar, 32) { Value = mode }); - AddInt(cmd, "@timeoutMs", (int)Math.Min(int.MaxValue, timeout.TotalMilliseconds)); - if (timeout > TimeSpan.Zero) - { - cmd.CommandTimeout = (int)Math.Ceiling(timeout.TotalSeconds) + 30; - } - - var result = Convert.ToInt32(await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false)); - return result >= 0; - } - - private static async Task WriteLinksAsync( - DbConnection conn, DbTransaction tx, IReadOnlyList<(AuditChainRow Row, long Seq, byte[] Hash)> chunk, CancellationToken ct) - { - var sql = new StringBuilder(); - // The ONE sanctioned UPDATE against AuditLog outside the maintenance backfill: it - // writes only the two chain columns, only while they are still NULL. - sql.Append("UPDATE dbo.AuditLog SET ChainSeq = v.ChainSeq, RowHash = v.RowHash\n"); // AUDIT-PURGE-ALLOWED: hash-chain link (ChainSeq/RowHash only, NULL→value), chaining job - sql.Append("FROM dbo.AuditLog INNER JOIN (VALUES "); - await using var cmd = conn.CreateCommand(); - cmd.Transaction = tx; - for (var i = 0; i < chunk.Count; i++) - { - if (i > 0) - { - sql.Append(", "); - } - - sql.Append($"(@e{i}, @o{i}, @s{i}, @h{i})"); - cmd.Parameters.Add(new SqlParameter($"@e{i}", SqlDbType.UniqueIdentifier) { Value = chunk[i].Row.EventId }); - cmd.Parameters.Add(new SqlParameter($"@o{i}", SqlDbType.DateTime2) { Scale = 7, Value = chunk[i].Row.OccurredAtUtc }); - cmd.Parameters.Add(new SqlParameter($"@s{i}", SqlDbType.BigInt) { Value = chunk[i].Seq }); - cmd.Parameters.Add(new SqlParameter($"@h{i}", SqlDbType.Binary, AuditChainHasher.HashLength) { Value = chunk[i].Hash }); - } - - sql.Append(") AS v (EventId, OccurredAtUtc, ChainSeq, RowHash)\n"); - sql.Append("ON dbo.AuditLog.EventId = v.EventId AND dbo.AuditLog.OccurredAtUtc = v.OccurredAtUtc\n"); - sql.Append("WHERE dbo.AuditLog.ChainSeq IS NULL;"); - cmd.CommandText = sql.ToString(); - return await cmd.ExecuteNonQueryAsync(ct).ConfigureAwait(false); - } - - private static async Task AdvanceHeadAsync( - DbConnection conn, DbTransaction tx, DateTime month, long? oldSeq, long newSeq, byte[] newHash, CancellationToken ct) - { - await using var cmd = Command(conn, tx, oldSeq is null - ? "INSERT INTO dbo.AuditLogChainHead (ChainMonth, LastSeq, LastHash, UpdatedAtUtc) VALUES (@month, @seq, @hash, SYSUTCDATETIME());" - : "UPDATE dbo.AuditLogChainHead SET LastSeq = @seq, LastHash = @hash, UpdatedAtUtc = SYSUTCDATETIME() " + - "WHERE ChainMonth = @month AND LastSeq = @oldSeq;"); - AddDate(cmd, "@month", month); - cmd.Parameters.Add(new SqlParameter("@seq", SqlDbType.BigInt) { Value = newSeq }); - cmd.Parameters.Add(new SqlParameter("@hash", SqlDbType.Binary, AuditChainHasher.HashLength) { Value = newHash }); - if (oldSeq is not null) - { - cmd.Parameters.Add(new SqlParameter("@oldSeq", SqlDbType.BigInt) { Value = oldSeq.Value }); - } - - return await cmd.ExecuteNonQueryAsync(ct).ConfigureAwait(false) == 1; - } - - /// - /// Reads one row selected as (plus ChainSeq, RowHash when - /// ), strictly in ordinal order so it is safe under SequentialAccess. - /// - internal static AuditChainRow ReadRow(DbDataReader reader, bool withLink) - { - // Ordinals follow RowColumns (+ ChainSeq, RowHash). Read strictly in order — the - // verification reader is opened with SequentialAccess. - var eventId = reader.GetGuid(0); - var occurred = DateTime.SpecifyKind(reader.GetDateTime(1), DateTimeKind.Utc); - var actor = reader.IsDBNull(2) ? null : reader.GetString(2); - var action = reader.GetString(3); - var outcome = reader.GetString(4); - var category = reader.GetString(5); - var target = reader.IsDBNull(6) ? null : reader.GetString(6); - var sourceNode = reader.IsDBNull(7) ? null : reader.GetString(7); - Guid? correlationId = reader.IsDBNull(8) ? null : reader.GetGuid(8); - var details = reader.IsDBNull(9) ? null : reader.GetString(9); - long? chainSeq = null; - byte[]? rowHash = null; - if (withLink) - { - chainSeq = reader.IsDBNull(10) ? null : reader.GetInt64(10); - rowHash = reader.IsDBNull(11) ? null : (byte[])reader.GetValue(11); - } - - return new AuditChainRow(eventId, occurred, actor, action, outcome, category, target, sourceNode, - correlationId, details, chainSeq, rowHash); - } - - private static DbCommand Command(DbConnection conn, DbTransaction tx, string sql) - { - var cmd = conn.CreateCommand(); - cmd.Transaction = tx; - cmd.CommandText = sql; - return cmd; - } - - private static void AddInt(DbCommand cmd, string name, int value) - => cmd.Parameters.Add(new SqlParameter(name, SqlDbType.Int) { Value = value }); - - private static void AddDate(DbCommand cmd, string name, DateTime month) - => cmd.Parameters.Add(new SqlParameter(name, SqlDbType.Date) { Value = month.Date }); - - private static void AddMonthRange(DbCommand cmd, DateTime month) - { - cmd.Parameters.Add(new SqlParameter("@monthStart", SqlDbType.DateTime2) { Scale = 7, Value = month }); - cmd.Parameters.Add(new SqlParameter("@monthEnd", SqlDbType.DateTime2) { Scale = 7, Value = month.AddMonths(1) }); - } - - internal static async Task OpenAsync(DbConnection conn, CancellationToken ct) - { - if (conn.State == ConnectionState.Open) - { - return false; - } - - await conn.OpenAsync(ct).ConfigureAwait(false); - return true; - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.Cleanup.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.Cleanup.cs index eb8baaec..8dfd2f47 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.Cleanup.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.Cleanup.cs @@ -12,16 +12,13 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; /// /// /// Superseded stale archives. A purge whose guard finds the partition changed after it -/// was archived (51002/51003) purges nothing and leaves its archive at status +/// was archived (51002) purges nothing and leaves its archive at status /// ; the next tick writes a fresh archive and purges /// with it. The stale one is redundant only if a LATER archive B of the same partition, which /// is and verifies, holds everything the stale archive A -/// holds. That is proven, not assumed: for every chain month of A, B starts from the same -/// earlier purge anchor and carries A's chain-end hash at A's chain-end sequence (as a row or a -/// tombstone) — so, the chain being a hash chain B itself verifies, every chained row of A is -/// in B; and every UNCHAINED row of A is found in B (row or tombstone) by EventId. Anything -/// else — notably an unchained row a per-channel purge removed in between, which leaves no -/// tombstone — keeps A (). An archive left at +/// holds. That is proven, not assumed: every row of A is found in B by EventId. Anything +/// else — notably a row a per-channel purge removed in between — keeps A +/// (). An archive left at /// Archived because the node stopped between the switch and the manifest update has no /// later purged archive and is kept: it is the only copy. /// @@ -31,8 +28,9 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; /// an Archived one — when its partition no longer holds rows in dbo.AuditLog. /// /// -/// Safe deletion. Only the three files a well-formed archive name implies -/// ({name}.manifest.json, {name}.parquet, {name}.tombstones.parquet) in the +/// Safe deletion. Only the files a well-formed archive name implies +/// ({name}.manifest.json, {name}.parquet, and a format-v1 archive's +/// {name}.tombstones.parquet) in the /// archive's own {Directory}/{YYYY} folder are deleted — never a path read from the /// manifest — and the manifest goes FIRST, so an interrupted deletion leaves leftovers that are /// no longer an archive rather than a manifest naming missing files. @@ -41,10 +39,10 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; public sealed partial class AuditLogPartitionArchive { /// - /// Upper bound on unchained rows compared by EventId when proving a stale archive redundant; - /// a larger set keeps the archive rather than spend the memory. + /// Upper bound on rows compared by EventId when proving a stale archive redundant; + /// a larger archive is kept rather than spend the memory. /// - internal const long MaxUnchainedRowsCompared = 5_000_000; + internal const long MaxRowsCompared = 5_000_000; /// public async Task CleanupAsync(bool dryRun, CancellationToken ct = default) @@ -104,7 +102,7 @@ public sealed partial class AuditLogPartitionArchive var now = _time.GetUtcNow().UtcDateTime; foreach (var manifest in manifests.Where(m => !deleted.Contains(m.Name))) { - var monthEnd = AuditChainHasher.MonthOf(manifest.PartitionBoundaryUtc).AddMonths(1); + var monthEnd = AuditArchiveParquet.MonthOf(manifest.PartitionBoundaryUtc).AddMonths(1); if (monthEnd.AddDays(_options.RetentionDays) > now) { continue; @@ -230,102 +228,44 @@ public sealed partial class AuditLogPartitionArchive /// private async Task ProveCoveredAsync(AuditArchiveManifest stale, AuditArchiveManifest later, CancellationToken ct) { - // Same chain generation and at least as far, for every chain month the stale archive has. - var laterChains = later.Chains.ToDictionary(c => c.Partition, StringComparer.Ordinal); - var targets = new Dictionary<(DateTime Month, long Seq), string>(); - foreach (var chain in stale.Chains) + if (stale.RowCount > MaxRowsCompared) { - if (!laterChains.TryGetValue(chain.Partition, out var next)) - { - return $"{later.Name} has no chain for {chain.Partition}."; - } - - if (next.PriorPurgedThroughSeq != chain.PriorPurgedThroughSeq || next.LastSeq < chain.LastSeq) - { - return $"{later.Name}'s chain for {chain.Partition} does not extend this archive's (prior anchor or end differs)."; - } - - if (chain.LastSeq > (chain.PriorPurgedThroughSeq ?? 0)) - { - targets[(AuditArchiveParquet.ParseMonth(chain.Partition), chain.LastSeq)] = chain.LastHash.ToLowerInvariant(); - } + return $"{stale.RowCount} rows exceed the {MaxRowsCompared} compared by EventId."; } - if (stale.UnchainedRowCount > MaxUnchainedRowsCompared) - { - return $"{stale.UnchainedRowCount} unchained rows exceed the {MaxUnchainedRowsCompared} compared by EventId."; - } - - // The later archive must itself be intact and tied to the database. + // The later archive must itself be intact. var verification = await VerifyAsync(later.Name, ct).ConfigureAwait(false); if (!verification.Verified) { - return $"{later.Name} does not verify ({verification.FileProblem ?? "chain or anchor problem"})."; + return $"{later.Name} does not verify ({verification.FileProblem})."; } - var directory = ArchiveDirectory(stale); - var unchained = new HashSet(); - await using (var input = File.OpenRead(Path.Combine(directory, stale.Name + ".parquet"))) + var missing = new HashSet(); + await using (var input = File.OpenRead(Path.Combine(ArchiveDirectory(stale), stale.Name + ".parquet"))) { await foreach (var row in AuditArchiveParquet.ReadRowsAsync(input, ct).ConfigureAwait(false)) { - if (row.ChainSeq is null) - { - unchained.Add(row.EventId); - } + missing.Add(row.EventId); } } - var laterDirectory = ArchiveDirectory(later); - await using (var input = File.OpenRead(Path.Combine(laterDirectory, later.Name + ".parquet"))) + await using (var input = File.OpenRead(Path.Combine(ArchiveDirectory(later), later.Name + ".parquet"))) { await foreach (var row in AuditArchiveParquet.ReadRowsAsync(input, ct).ConfigureAwait(false)) { - unchained.Remove(row.EventId); - if (row.ChainSeq is { } seq - && targets.TryGetValue((AuditChainHasher.MonthOf(row.OccurredAtUtc), seq), out var expected)) - { - if (row.RowHash is null || !string.Equals(Convert.ToHexStringLower(row.RowHash), expected, StringComparison.Ordinal)) - { - return $"{later.Name} carries a different hash at the stale archive's chain end."; - } - - targets.Remove((AuditChainHasher.MonthOf(row.OccurredAtUtc), seq)); - } + missing.Remove(row.EventId); } } - await using (var input = File.OpenRead(Path.Combine(laterDirectory, later.Name + ".tombstones.parquet"))) - { - await foreach (var tombstone in AuditArchiveParquet.ReadTombstonesAsync(input, ct).ConfigureAwait(false)) - { - unchained.Remove(tombstone.EventId); - if (targets.TryGetValue((tombstone.ChainMonth, tombstone.ChainSeq), out var expected)) - { - if (!string.Equals(Convert.ToHexStringLower(tombstone.RowHash), expected, StringComparison.Ordinal)) - { - return $"{later.Name} carries a different hash at the stale archive's chain end."; - } - - targets.Remove((tombstone.ChainMonth, tombstone.ChainSeq)); - } - } - } - - if (targets.Count > 0) - { - return $"{later.Name} does not contain the stale archive's chain end."; - } - - return unchained.Count == 0 + return missing.Count == 0 ? null - : $"{unchained.Count} unchained row(s) of this archive are not in {later.Name} (e.g. removed by a per-channel purge, which leaves no tombstone for an unchained row)."; + : $"{missing.Count} row(s) of this archive are not in {later.Name} (e.g. removed by a per-channel purge in between)."; } private async Task PartitionHasRowsAsync(DateTime boundary, CancellationToken ct) { var conn = _context.Database.GetDbConnection(); - var openedHere = await AuditLogHashChain.OpenAsync(conn, ct).ConfigureAwait(false); + var openedHere = await AuditArchiveParquet.OpenAsync(conn, ct).ConfigureAwait(false); try { await using var cmd = conn.CreateCommand(); @@ -354,7 +294,7 @@ public sealed partial class AuditLogPartitionArchive } /// - /// Deletes exactly the archive's three files, manifest first. File names are derived from + /// Deletes exactly the archive's files (a format-v1 archive also has a tombstone file), manifest first. File names are derived from /// the validated archive name, never taken from the manifest. /// private void DeleteArchiveFiles(AuditArchiveManifest manifest) diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.cs index d6890f3b..cc1dc505 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditLogPartitionArchive.cs @@ -21,8 +21,7 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; /// /// /// -/// Layout. {Directory}/{YYYY}/{name}.parquet (rows), -/// {name}.tombstones.parquet (per-channel purge tombstones) and +/// Layout. {Directory}/{YYYY}/{name}.parquet (rows) and /// {name}.manifest.json, where name is /// auditlog-{YYYY-MM}-{archivedAt:yyyyMMddTHHmmssfff}Z. Each file is written to a /// .tmp sibling, flushed to disk and renamed; the manifest goes last, so a manifest @@ -30,10 +29,9 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; /// new archive, and the superseded one keeps status . /// /// -/// Consistency with the hash chain. The partition is read inside one transaction that -/// holds the chain lock SHARED, so no chaining pass or purge moves a head or writes a -/// tombstone while the rows, heads and tombstones are read. The purge then re-checks the -/// row count and heads under the EXCLUSIVE lock (). +/// Consistency with the purge. The partition is read inside one transaction; the purge +/// then re-counts the partition inside the switch transaction +/// () and purges nothing when the count moved. /// /// public sealed partial class AuditLogPartitionArchive : IAuditLogArchive @@ -86,93 +84,27 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive } var boundary = DateTime.SpecifyKind(partitionBoundaryUtc, DateTimeKind.Utc); - var partition = AuditChainHasher.PartitionLabel(AuditChainHasher.MonthOf(boundary)); + var partition = AuditArchiveParquet.PartitionLabel(AuditArchiveParquet.MonthOf(boundary)); var archivedAt = _time.GetUtcNow().UtcDateTime; var name = $"auditlog-{partition}-{archivedAt.ToString("yyyyMMdd'T'HHmmssfff", CultureInfo.InvariantCulture)}Z"; var directory = Path.Combine(_options.Directory!, partition[..4]); Directory.CreateDirectory(directory); var dataFile = name + ".parquet"; - var tombstoneFile = name + ".tombstones.parquet"; var dataTmp = Path.Combine(directory, dataFile + ".tmp"); - var tombstoneTmp = Path.Combine(directory, tombstoneFile + ".tmp"); - var chains = new List(); - long rowCount = 0, chained = 0; + long rowCount = 0; DateTime? minOccurred = null, maxOccurred = null; - string dataSha, tombstoneSha; - long dataBytes, tombstoneCount; + string dataSha; + long dataBytes; try { var conn = _context.Database.GetDbConnection(); - var openedHere = await AuditLogHashChain.OpenAsync(conn, ct).ConfigureAwait(false); + var openedHere = await AuditArchiveParquet.OpenAsync(conn, ct).ConfigureAwait(false); try { await using var tx = await conn.BeginTransactionAsync(IsolationLevel.ReadCommitted, ct).ConfigureAwait(false); - if (!await AuditLogHashChain.TryAcquireLockAsync( - conn, tx, "Shared", TimeSpan.FromSeconds(Math.Max(1, _options.ChainLockTimeoutSeconds)), ct) - .ConfigureAwait(false)) - { - await tx.RollbackAsync(ct).ConfigureAwait(false); - throw new TimeoutException( - $"The AuditLog hash-chain lock stayed busy for {_options.ChainLockTimeoutSeconds}s; partition {partition} not archived this tick."); - } - - // 1. Chain heads of every month the partition holds (normally exactly one). - var tombstoneCounts = new Dictionary(); - var heads = new List<(DateTime Month, long LastSeq, byte[] LastHash, long? PurgedSeq, byte[]? PurgedHash)>(); - await using (var cmd = Command(conn, tx, - "SELECT ChainMonth, LastSeq, LastHash, PurgedThroughSeq, PurgedThroughHash FROM dbo.AuditLogChainHead " + - "WHERE $partition.pf_AuditLog_Month(CAST(ChainMonth AS datetime2(7))) = $partition.pf_AuditLog_Month(@boundary) " + - "ORDER BY ChainMonth;", boundary)) - { - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - heads.Add(( - DateTime.SpecifyKind(reader.GetDateTime(0), DateTimeKind.Utc), - reader.GetInt64(1), - (byte[])reader.GetValue(2), - reader.IsDBNull(3) ? null : reader.GetInt64(3), - reader.IsDBNull(4) ? null : (byte[])reader.GetValue(4))); - } - } - - // 2. Per-channel purge tombstones (verification bridges gaps with them). - await using (var file = NewFile(tombstoneTmp)) - { - var hashing = new HashingWriteStream(file); - await using (var writer = await AuditArchiveParquet - .CreateTombstoneWriterAsync(hashing, partition, _options.RowGroupSize, ct).ConfigureAwait(false)) - { - await using var cmd = Command(conn, tx, - "SELECT ChainMonth, ChainSeq, RowHash, EventId, PurgedAtUtc FROM dbo.AuditLogChainTombstone " + - "WHERE $partition.pf_AuditLog_Month(CAST(ChainMonth AS datetime2(7))) = $partition.pf_AuditLog_Month(@boundary) " + - "ORDER BY ChainMonth, ChainSeq;", boundary); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - while (await reader.ReadAsync(ct).ConfigureAwait(false)) - { - var tombstone = new AuditArchiveTombstone( - DateTime.SpecifyKind(reader.GetDateTime(0), DateTimeKind.Utc), - reader.GetInt64(1), - (byte[])reader.GetValue(2), - reader.GetGuid(3), - DateTime.SpecifyKind(reader.GetDateTime(4), DateTimeKind.Utc)); - tombstoneCounts[tombstone.ChainMonth] = tombstoneCounts.GetValueOrDefault(tombstone.ChainMonth) + 1; - await writer.AddAsync(tombstone, ct).ConfigureAwait(false); - } - - tombstoneCount = writer.Count; - } - - await hashing.FlushAsync(ct).ConfigureAwait(false); - file.Flush(flushToDisk: true); - tombstoneSha = hashing.HashHex(); - } - - // 3. The rows, grouped by chain month and in chain order within it (unchained - // rows last), which is the order verification needs. await using (var file = NewFile(dataTmp)) { var hashing = new HashingWriteStream(file); @@ -180,20 +112,14 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive .CreateDataWriterAsync(hashing, partition, _options.RowGroupSize, ct).ConfigureAwait(false)) { await using var cmd = Command(conn, tx, - $"SELECT {AuditLogHashChain.RowColumns}, ChainSeq, RowHash FROM dbo.AuditLog " + + $"SELECT {AuditArchiveParquet.RowColumns} FROM dbo.AuditLog " + "WHERE $PARTITION.pf_AuditLog_Month(OccurredAtUtc) = $partition.pf_AuditLog_Month(@boundary) " + - "ORDER BY DATEFROMPARTS(YEAR(OccurredAtUtc), MONTH(OccurredAtUtc), 1), " + - "CASE WHEN ChainSeq IS NULL THEN 1 ELSE 0 END, ChainSeq, OccurredAtUtc, EventId;", boundary); + "ORDER BY OccurredAtUtc, EventId;", boundary); cmd.CommandTimeout = Math.Max(1, _options.ReadCommandTimeoutMinutes) * 60; await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess, ct).ConfigureAwait(false); while (await reader.ReadAsync(ct).ConfigureAwait(false)) { - var row = AuditLogHashChain.ReadRow(reader, withLink: true); - if (row.ChainSeq is not null) - { - chained++; - } - + var row = AuditArchiveParquet.ReadRow(reader); minOccurred = minOccurred is { } lo && lo <= row.OccurredAtUtc ? lo : row.OccurredAtUtc; maxOccurred = maxOccurred is { } hi && hi >= row.OccurredAtUtc ? hi : row.OccurredAtUtc; await writer.AddAsync(row, ct).ConfigureAwait(false); @@ -209,17 +135,6 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive } await tx.CommitAsync(ct).ConfigureAwait(false); - - foreach (var head in heads) - { - chains.Add(new AuditArchiveChainState( - AuditChainHasher.PartitionLabel(head.Month), - head.LastSeq, - Convert.ToHexStringLower(head.LastHash), - head.PurgedSeq, - head.PurgedHash is null ? null : Convert.ToHexStringLower(head.PurgedHash), - tombstoneCounts.GetValueOrDefault(head.Month))); - } } finally { @@ -230,12 +145,10 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive } File.Move(dataTmp, Path.Combine(directory, dataFile)); - File.Move(tombstoneTmp, Path.Combine(directory, tombstoneFile)); } catch { TryDelete(dataTmp); - TryDelete(tombstoneTmp); throw; } @@ -248,22 +161,16 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive AuditArchiveStatus.Archived, PurgedAtUtc: null, rowCount, - chained, - rowCount - chained, minOccurred, maxOccurred, dataFile, dataSha, - dataBytes, - tombstoneFile, - tombstoneSha, - tombstoneCount, - chains); + dataBytes); await WriteManifestAsync(directory, manifest, ct).ConfigureAwait(false); _logger.LogInformation( - "Archived AuditLog partition {Partition} to {Archive}: {Rows} rows ({Chained} chained), {Tombstones} tombstones, {Bytes} bytes.", - partition, name, rowCount, chained, tombstoneCount, dataBytes); + "Archived AuditLog partition {Partition} to {Archive}: {Rows} rows, {Bytes} bytes.", + partition, name, rowCount, dataBytes); return manifest; } @@ -272,9 +179,7 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive AuditArchiveManifest manifest, TimeSpan? commandTimeout = null, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(manifest); - var guard = new AuditPartitionSwitchGuard( - manifest.RowCount, - manifest.Chains.Select(c => (AuditArchiveParquet.ParseMonth(c.Partition), c.LastSeq)).ToList()); + var guard = new AuditPartitionSwitchGuard(manifest.RowCount); long purged; try @@ -283,8 +188,7 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive .SwitchOutPartitionAsync(manifest.PartitionBoundaryUtc, guard, commandTimeout, ct) .ConfigureAwait(false); } - catch (Exception ex) when (FindSqlError(ex) is { } number - && number is AuditPartitionSwitchGuard.RowCountChangedError or AuditPartitionSwitchGuard.ChainAdvancedError) + catch (Exception ex) when (FindSqlError(ex) is AuditPartitionSwitchGuard.RowCountChangedError) { throw new AuditArchiveStaleException( $"Partition {manifest.Partition} changed after archive {manifest.Name} was written; nothing was purged, " @@ -302,8 +206,7 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) { - // The rows are gone and the archive files are complete; only the status flag is - // stale. Verify still ties the archive to the purge anchor (anchor Matches). + // The rows are gone and the archive file is complete; only the status flag is stale. _logger.LogError(ex, "AuditLog partition {Partition} was purged but archive {Archive}'s manifest could not be marked Purged.", manifest.Partition, manifest.Name); @@ -365,85 +268,20 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive var manifest = await ReadManifestAsync(manifestPath, ct).ConfigureAwait(false); var problems = new List(); var dataPath = Path.Combine(directory, Path.GetFileName(manifest.DataFile)); - var tombstonePath = Path.Combine(directory, Path.GetFileName(manifest.TombstoneFile)); await CheckFileAsync(dataPath, manifest.DataSha256, manifest.DataBytes, problems, ct).ConfigureAwait(false); - await CheckFileAsync(tombstonePath, manifest.TombstoneSha256, null, problems, ct).ConfigureAwait(false); if (problems.Count > 0) { return new AuditArchiveVerificationResult( manifest.Name, manifest.Partition, manifest.Status, Verified: false, FilesIntact: false, - string.Join(" ", problems), RowsRead: 0, []); - } - - // Tombstones per month, then one streaming verifier per chain month. - var tombstones = new Dictionary>(); - long tombstonesRead = 0; - DateTime? chainFrom = null; - await using (var input = File.OpenRead(tombstonePath)) - { - await foreach (var t in AuditArchiveParquet.ReadTombstonesAsync(input, ct).ConfigureAwait(false)) - { - if (!tombstones.TryGetValue(t.ChainMonth, out var byMonth)) - { - tombstones[t.ChainMonth] = byMonth = []; - } - - byMonth[t.ChainSeq] = t.RowHash; - tombstonesRead++; - if (t.ChainSeq == AuditChainHasher.CutoffAnchorSeq) - { - // The chain-start anchor (ScadaBridge#139) travels with the archive of its month. - chainFrom = chainFrom is { } seen && seen <= t.PurgedAtUtc ? seen : t.PurgedAtUtc; - } - } - } - - // Otherwise the cutoff comes from the database's anchor, which outlives every purge. - chainFrom ??= await ReadChainFromAsync(ct).ConfigureAwait(false); - - var states = manifest.Chains.ToDictionary(c => AuditArchiveParquet.ParseMonth(c.Partition)); - var verifiers = new SortedDictionary(); - var unchained = new Dictionary(); - var preCutoff = new Dictionary(); - AuditChainVerifier VerifierFor(DateTime month) - { - if (!verifiers.TryGetValue(month, out var verifier)) - { - AuditChainHeadState? head = states.TryGetValue(month, out var s) - ? new AuditChainHeadState( - s.LastSeq, - Convert.FromHexString(s.LastHash), - s.PriorPurgedThroughSeq, - s.PriorPurgedThroughHash is null ? null : Convert.FromHexString(s.PriorPurgedThroughHash)) - : null; - verifier = new AuditChainVerifier(month, head, tombstones.GetValueOrDefault(month) ?? [], chainFrom); - verifiers[month] = verifier; - } - - return verifier; - } - - foreach (var month in states.Keys) - { - VerifierFor(month); + string.Join(" ", problems), RowsRead: 0); } long rowsRead = 0; await using (var input = File.OpenRead(dataPath)) { - await foreach (var row in AuditArchiveParquet.ReadRowsAsync(input, ct).ConfigureAwait(false)) + await foreach (var _ in AuditArchiveParquet.ReadRowsAsync(input, ct).ConfigureAwait(false)) { rowsRead++; - var month = AuditChainHasher.MonthOf(row.OccurredAtUtc); - if (row.ChainSeq is null) - { - var bucket = chainFrom is { } cutoff && row.OccurredAtUtc < cutoff ? preCutoff : unchained; - bucket[month] = bucket.GetValueOrDefault(month) + 1; - VerifierFor(month); - continue; - } - - VerifierFor(month).Accept(row); } } @@ -452,126 +290,10 @@ public sealed partial class AuditLogPartitionArchive : IAuditLogArchive problems.Add($"The data file holds {rowsRead} rows; the manifest records {manifest.RowCount}."); } - if (tombstonesRead != manifest.TombstoneCount) - { - problems.Add($"The tombstone file holds {tombstonesRead} tombstones; the manifest records {manifest.TombstoneCount}."); - } - - var anchors = await ReadAnchorsAsync(verifiers.Keys, ct).ConfigureAwait(false); - var results = new List(); - foreach (var (month, verifier) in verifiers) - { - var chain = verifier.Complete(unchained.GetValueOrDefault(month), preCutoff.GetValueOrDefault(month)); - var (anchor, detail) = CompareAnchor(states.GetValueOrDefault(month), anchors.GetValueOrDefault(month)); - results.Add(new AuditArchiveChainVerification(chain, anchor, detail)); - } - var filesIntact = problems.Count == 0; - var verified = filesIntact - && results.All(r => r.Chain.Status != AuditChainStatus.Broken && r.Anchor != AuditArchiveAnchorStatus.Mismatch); return new AuditArchiveVerificationResult( - manifest.Name, manifest.Partition, manifest.Status, verified, filesIntact, - filesIntact ? null : string.Join(" ", problems), rowsRead, results); - } - - /// - /// Compares an archived chain end with the database's current checkpoint anchor for the month. - /// - internal static (AuditArchiveAnchorStatus Status, string Detail) CompareAnchor( - AuditArchiveChainState? archived, (bool HeadExists, long? PurgedSeq, byte[]? PurgedHash) database) - { - if (archived is null) - { - // No chain head existed when the rows were archived (chaining off, or the month was - // never reached): nothing to tie. A later anchor can only come from a later purge. - return database.PurgedSeq is null - ? (AuditArchiveAnchorStatus.Matches, "No hash chain existed for this month when it was archived.") - : (AuditArchiveAnchorStatus.Superseded, - $"No chain at archive time; a later purge anchored the month at ChainSeq {database.PurgedSeq}."); - } - - if (!database.HeadExists) - { - return (AuditArchiveAnchorStatus.Mismatch, - $"The archive records a chain through ChainSeq {archived.LastSeq} but the database has no chain head for {archived.Partition}."); - } - - if (database.PurgedSeq is null) - { - return (AuditArchiveAnchorStatus.NotPurged, - $"{archived.Partition} has not been purged: the archived rows are still in dbo.AuditLog."); - } - - if (database.PurgedSeq == archived.LastSeq) - { - return database.PurgedHash is not null - && Convert.ToHexStringLower(database.PurgedHash) == archived.LastHash.ToLowerInvariant() - ? (AuditArchiveAnchorStatus.Matches, - $"The archived chain ends at the purge anchor (ChainSeq {archived.LastSeq}).") - : (AuditArchiveAnchorStatus.Mismatch, - $"The purge anchor is at ChainSeq {archived.LastSeq} but its hash differs from the archive's chain end."); - } - - return database.PurgedSeq > archived.LastSeq - ? (AuditArchiveAnchorStatus.Superseded, - $"The purge anchor (ChainSeq {database.PurgedSeq}) is past this archive's chain end ({archived.LastSeq}): " - + "late rows were purged and archived again afterwards.") - : (AuditArchiveAnchorStatus.Mismatch, - $"The purge anchor (ChainSeq {database.PurgedSeq}) is before this archive's chain end ({archived.LastSeq})."); - } - - /// The database's chain cutoff (ScadaBridge#139), or null when the chain has none. - private async Task ReadChainFromAsync(CancellationToken ct) - { - var conn = _context.Database.GetDbConnection(); - var openedHere = await AuditLogHashChain.OpenAsync(conn, ct).ConfigureAwait(false); - try - { - await using var cmd = conn.CreateCommand(); - cmd.CommandText = "SELECT MIN(PurgedAtUtc) FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;"; - return await cmd.ExecuteScalarAsync(ct).ConfigureAwait(false) is DateTime dt - ? DateTime.SpecifyKind(dt, DateTimeKind.Utc) - : null; - } - finally - { - if (openedHere) - { - await conn.CloseAsync().ConfigureAwait(false); - } - } - } - - private async Task> ReadAnchorsAsync( - IEnumerable months, CancellationToken ct) - { - var result = new Dictionary(); - var conn = _context.Database.GetDbConnection(); - var openedHere = await AuditLogHashChain.OpenAsync(conn, ct).ConfigureAwait(false); - try - { - foreach (var month in months) - { - await using var cmd = conn.CreateCommand(); - cmd.CommandText = "SELECT PurgedThroughSeq, PurgedThroughHash FROM dbo.AuditLogChainHead WHERE ChainMonth = @month;"; - cmd.Parameters.Add(new SqlParameter("@month", SqlDbType.Date) { Value = month.Date }); - await using var reader = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false); - result[month] = await reader.ReadAsync(ct).ConfigureAwait(false) - ? (true, - reader.IsDBNull(0) ? null : reader.GetInt64(0), - reader.IsDBNull(1) ? null : (byte[])reader.GetValue(1)) - : (false, null, null); - } - } - finally - { - if (openedHere) - { - await conn.CloseAsync().ConfigureAwait(false); - } - } - - return result; + manifest.Name, manifest.Partition, manifest.Status, filesIntact, filesIntact, + filesIntact ? null : string.Join(" ", problems), rowsRead); } private static async Task CheckFileAsync(string path, string expectedSha, long? expectedBytes, List problems, CancellationToken ct) diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditMaintenanceContext.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditMaintenanceContext.cs deleted file mode 100644 index 0d767f36..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditMaintenanceContext.cs +++ /dev/null @@ -1,83 +0,0 @@ -using Microsoft.AspNetCore.DataProtection; -using Microsoft.EntityFrameworkCore; -using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; - -namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; - -/// -/// The connection the AuditLog maintenance paths run on — the hash-chain job and both purge -/// paths (partition switch-out with its archive, per-channel DELETE) — ScadaBridge#107 item 3. -/// -/// -/// -/// With AuditLog:MaintenanceConnectionString unset (the default), this is the scope's -/// ordinary : one login for everything, exactly as before. -/// With it set, the maintenance paths get their OWN context on that connection string, so a -/// DBA can give the runtime (writer) login only scadabridge_audit_writer — whose DENYs -/// stop it writing chain state or deleting audit rows — and a separate maintenance login the -/// scadabridge_audit_chainer + scadabridge_audit_purger roles. The string is read -/// after the pre-host ${secret:} expansion, so it may (and in production should) be a -/// secret reference. -/// -/// Scoped: an owned context is disposed with the scope; a borrowed one is left to DI. -/// -public sealed class AuditMaintenanceContext : IAsyncDisposable, IDisposable -{ - /// DI key of the maintenance-login IAuditLogRepository and IAuditLogArchive. - public const string ServiceKey = AuditLogServiceKeys.Maintenance; - - private readonly bool _owned; - - private AuditMaintenanceContext(ScadaBridgeDbContext context, bool owned) - { - Context = context; - _owned = owned; - } - - /// The context to run maintenance SQL on. - public ScadaBridgeDbContext Context { get; } - - /// True when is on the separate maintenance connection string. - public bool UsesSeparateLogin => _owned; - - /// Wraps the scope's ordinary context (no separate maintenance login configured). - /// The scope's context. - /// The wrapper. - public static AuditMaintenanceContext Shared(ScadaBridgeDbContext context) - => new(context ?? throw new ArgumentNullException(nameof(context)), owned: false); - - /// Opens an owned context on the maintenance connection string. - /// The maintenance connection string. - /// Data protection for secret-bearing columns (same as the runtime context). - /// The wrapper; disposing it disposes the context. - public static AuditMaintenanceContext Separate(string connectionString, IDataProtectionProvider? protectionProvider) - { - ArgumentException.ThrowIfNullOrWhiteSpace(connectionString); - var options = new DbContextOptionsBuilder() - .UseSqlServer(connectionString, sql => sql.EnableRetryOnFailure( - maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null)) - .ConfigureWarnings(w => w.Ignore( - Microsoft.EntityFrameworkCore.Diagnostics.RelationalEventId.PendingModelChangesWarning)) - .Options; - var context = protectionProvider is null - ? new ScadaBridgeDbContext(options) - : new ScadaBridgeDbContext(options, protectionProvider); - return new AuditMaintenanceContext(context, owned: true); - } - - /// - public ValueTask DisposeAsync() => _owned ? Context.DisposeAsync() : ValueTask.CompletedTask; - - /// - public void Dispose() - { - if (_owned) - { - Context.Dispose(); - } - } -} - -/// The configured maintenance connection string (null = use the runtime connection). -/// The connection string, or null. -public sealed record AuditMaintenanceConnection(string? ConnectionString); diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditPartitionSwitchGuard.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditPartitionSwitchGuard.cs index 2a3dd42b..41e21d1c 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditPartitionSwitchGuard.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Maintenance/AuditPartitionSwitchGuard.cs @@ -8,49 +8,28 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance; /// partition-switch transaction so the purge drops exactly the rows the archive holds. /// /// Rows the archive holds for the partition. -/// Each chain month's LastSeq at archive time. -public sealed record AuditPartitionSwitchGuard(long ExpectedRows, IReadOnlyList<(DateTime ChainMonth, long LastSeq)> ExpectedHeads) +public sealed record AuditPartitionSwitchGuard(long ExpectedRows) { /// SQL error number THROWn when the partition row count changed. public const int RowCountChangedError = 51002; - /// SQL error number THROWn when a chain head in the partition changed. - public const int ChainAdvancedError = 51003; - /// - /// Renders the guard as T-SQL for the switch batch. Every value is a number or a - /// yyyy-MM-dd date produced here, so inlining them as literals is injection-safe and - /// keeps the switch a parameterless ExecuteSqlRaw batch. + /// Renders the guard as T-SQL for the switch batch. The only value is a number produced + /// here, so inlining it as a literal is injection-safe and keeps the switch a + /// parameterless ExecuteSqlRaw batch. /// /// The boundary literal the switch batch already uses. - /// The guard statements (run after the chain lock, inside the TRY). + /// The guard statements (run inside the switch TRY, before the SWITCH). internal string ToSql(string monthBoundaryLiteral) { var sql = new StringBuilder(); sql.AppendLine(); - sql.AppendLine(" -- 0b. Archive guard (ScadaBridge#42): the archive must hold exactly the rows this"); - sql.AppendLine(" -- switch drops. Under the exclusive chain lock, so nothing chains or per-channel"); - sql.AppendLine(" -- purges between this check and the SWITCH."); + sql.AppendLine(" -- 0. Archive guard (ScadaBridge#42): the archive must hold exactly the rows this"); + sql.AppendLine(" -- switch drops."); sql.AppendLine(CultureInfo.InvariantCulture, $" DECLARE @guardPartition int = $partition.pf_AuditLog_Month('{monthBoundaryLiteral}');"); sql.AppendLine(" IF (SELECT COUNT_BIG(*) FROM dbo.AuditLog WHERE $PARTITION.pf_AuditLog_Month(OccurredAtUtc) = @guardPartition)"); sql.AppendLine(CultureInfo.InvariantCulture, $" <> {ExpectedRows}"); sql.AppendLine(CultureInfo.InvariantCulture, $" THROW {RowCountChangedError}, 'AuditLog archive is stale: the partition row count changed after it was archived.', 1;"); - sql.AppendLine(" DECLARE @guardHeads TABLE (ChainMonth date NOT NULL PRIMARY KEY, LastSeq bigint NOT NULL);"); - foreach (var (month, lastSeq) in ExpectedHeads) - { - sql.AppendLine(CultureInfo.InvariantCulture, - $" INSERT INTO @guardHeads (ChainMonth, LastSeq) VALUES ('{month:yyyy-MM-dd}', {lastSeq});"); - } - - sql.AppendLine(" IF EXISTS ("); - sql.AppendLine(" SELECT h.ChainMonth, h.LastSeq FROM dbo.AuditLogChainHead h"); - sql.AppendLine(" WHERE $partition.pf_AuditLog_Month(CAST(h.ChainMonth AS datetime2(7))) = @guardPartition"); - sql.AppendLine(" EXCEPT SELECT ChainMonth, LastSeq FROM @guardHeads)"); - sql.AppendLine(" OR EXISTS ("); - sql.AppendLine(" SELECT ChainMonth, LastSeq FROM @guardHeads"); - sql.AppendLine(" EXCEPT SELECT h.ChainMonth, h.LastSeq FROM dbo.AuditLogChainHead h"); - sql.AppendLine(" WHERE $partition.pf_AuditLog_Month(CAST(h.ChainMonth AS datetime2(7))) = @guardPartition)"); - sql.AppendLine(CultureInfo.InvariantCulture, $" THROW {ChainAdvancedError}, 'AuditLog archive is stale: the hash chain advanced after it was archived.', 1;"); return sql.ToString(); } } diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261110033732_FlipAuditLogClusteredKey.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261110033732_FlipAuditLogClusteredKey.cs index 9db29aaa..bac59dfd 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261110033732_FlipAuditLogClusteredKey.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261110033732_FlipAuditLogClusteredKey.cs @@ -69,8 +69,11 @@ CREATE NONCLUSTERED INDEX IX_AuditLog_ParentExecution ON dbo.AuditLog (ParentExe CREATE NONCLUSTERED INDEX IX_AuditLog_Node_Occurred ON dbo.AuditLog (SourceNode, OccurredAtUtc) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Kind_Occurred ON dbo.AuditLog (Kind ASC, OccurredAtUtc DESC) ON ps_AuditLog_Month(OccurredAtUtc); CREATE NONCLUSTERED INDEX IX_AuditLog_Actor_Occurred ON dbo.AuditLog (Actor ASC, OccurredAtUtc DESC) ON ps_AuditLog_Month(OccurredAtUtc); -CREATE NONCLUSTERED INDEX IX_AuditLog_Unchained ON dbo.AuditLog (OccurredAtUtc, EventId) WHERE ChainSeq IS NULL ON ps_AuditLog_Month(OccurredAtUtc); -CREATE NONCLUSTERED INDEX IX_AuditLog_ChainSeq ON dbo.AuditLog (ChainSeq) WHERE ChainSeq IS NOT NULL ON ps_AuditLog_Month(OccurredAtUtc);"; +-- The two hash-chain indexes go through EXEC (compiled only when run): RemoveAuditLogHashChain +-- (2026-11-15) drops ChainSeq, and the idempotent script compiles this migration's batch against +-- that schema on a re-run, where a direct reference is Msg 207 (ScadaBridge#126). Same indexes. +EXEC(N'CREATE NONCLUSTERED INDEX IX_AuditLog_Unchained ON dbo.AuditLog (OccurredAtUtc, EventId) WHERE ChainSeq IS NULL ON ps_AuditLog_Month(OccurredAtUtc);'); +EXEC(N'CREATE NONCLUSTERED INDEX IX_AuditLog_ChainSeq ON dbo.AuditLog (ChainSeq) WHERE ChainSeq IS NOT NULL ON ps_AuditLog_Month(OccurredAtUtc);');"; /// Drops every non-clustered index on dbo.AuditLog (the twelve above plus the B2 unique index), each existence-guarded. internal const string DropNonClusteredIndexesSql = @" diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261115221558_RemoveAuditLogHashChain.Designer.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261115221558_RemoveAuditLogHashChain.Designer.cs new file mode 100644 index 00000000..85f130f9 --- /dev/null +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261115221558_RemoveAuditLogHashChain.Designer.cs @@ -0,0 +1,2314 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase; + +#nullable disable + +namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations +{ + [DbContext(typeof(ScadaBridgeDbContext))] + [Migration("20261115221558_RemoveAuditLogHashChain")] + partial class RemoveAuditLogHashChain + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "10.0.13") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.DataProtectionKey", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("FriendlyName") + .HasColumnType("nvarchar(max)"); + + b.Property("Xml") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("DataProtectionKeys"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit.AuditLogEntry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Action") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("AfterStateJson") + .HasColumnType("nvarchar(max)"); + + b.Property("BundleImportId") + .HasColumnType("uniqueidentifier"); + + b.Property("EntityId") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("EntityName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("EntityType") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("Timestamp") + .HasColumnType("datetimeoffset"); + + b.Property("User") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.HasKey("Id"); + + b.HasIndex("Action"); + + b.HasIndex("BundleImportId") + .HasDatabaseName("IX_AuditLogEntries_BundleImportId"); + + b.HasIndex("EntityId"); + + b.HasIndex("EntityName") + .HasDatabaseName("IX_AuditLogEntries_EntityName"); + + b.HasIndex("EntityType"); + + b.HasIndex("Timestamp"); + + b.HasIndex("User"); + + b.HasIndex("EntityType", "Timestamp") + .IsDescending(false, true) + .HasDatabaseName("IX_AuditLogEntries_EntityType_Timestamp"); + + b.HasIndex("Timestamp", "Id") + .IsDescending() + .HasDatabaseName("IX_AuditLogEntries_Timestamp_Id"); + + b.HasIndex("User", "Timestamp") + .IsDescending(false, true) + .HasDatabaseName("IX_AuditLogEntries_User_Timestamp"); + + b.ToTable("AuditLogEntries"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Audit.SiteCall", b => + { + b.Property("TrackedOperationId") + .HasMaxLength(36) + .IsUnicode(false) + .HasColumnType("varchar(36)"); + + b.Property("Channel") + .IsRequired() + .HasMaxLength(32) + .IsUnicode(false) + .HasColumnType("varchar(32)"); + + b.Property("CreatedAtUtc") + .HasColumnType("datetime2"); + + b.Property("HttpStatus") + .HasColumnType("int"); + + b.Property("IngestedAtUtc") + .HasColumnType("datetime2"); + + b.Property("LastError") + .HasMaxLength(1024) + .HasColumnType("nvarchar(1024)"); + + b.Property("RetryCount") + .HasColumnType("int"); + + b.Property("SourceNode") + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("SourceSite") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(32) + .IsUnicode(false) + .HasColumnType("varchar(32)"); + + b.Property("Target") + .IsRequired() + .HasMaxLength(256) + .IsUnicode(false) + .HasColumnType("varchar(256)"); + + b.Property("TerminalAtUtc") + .HasColumnType("datetime2"); + + b.Property("UpdatedAtUtc") + .HasColumnType("datetime2"); + + b.HasKey("TrackedOperationId"); + + b.HasIndex("CreatedAtUtc") + .HasDatabaseName("IX_SiteCalls_NonTerminal") + .HasFilter("[TerminalAtUtc] IS NULL"); + + SqlServerIndexBuilderExtensions.IncludeProperties(b.HasIndex("CreatedAtUtc"), new[] { "SourceSite", "SourceNode", "Status" }); + + b.HasIndex("TerminalAtUtc") + .HasDatabaseName("IX_SiteCalls_Terminal") + .HasFilter("[TerminalAtUtc] IS NOT NULL"); + + b.HasIndex("CreatedAtUtc", "TrackedOperationId") + .IsDescending() + .HasDatabaseName("IX_SiteCalls_Created_TrackedOperationId"); + + b.HasIndex("SourceSite", "CreatedAtUtc") + .IsDescending(false, true) + .HasDatabaseName("IX_SiteCalls_Source_Created"); + + b.HasIndex("Status", "UpdatedAtUtc") + .IsDescending(false, true) + .HasDatabaseName("IX_SiteCalls_Status_Updated"); + + b.ToTable("SiteCalls", (string)null); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.DeployedConfigSnapshot", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConfigurationJson") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DeployedAt") + .HasColumnType("datetimeoffset"); + + b.Property("DeploymentId") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("InstanceId") + .HasColumnType("int"); + + b.Property("RevisionHash") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.HasKey("Id"); + + b.HasIndex("DeploymentId"); + + b.HasIndex("InstanceId") + .IsUnique(); + + b.HasIndex("RowVersion"); + + b.ToTable("DeployedConfigSnapshots"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.DeploymentRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CompletedAt") + .HasColumnType("datetimeoffset"); + + b.Property("DeployedAt") + .HasColumnType("datetimeoffset"); + + b.Property("DeployedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("DeploymentId") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("ErrorMessage") + .HasColumnType("nvarchar(max)"); + + b.Property("InstanceId") + .HasColumnType("int"); + + b.Property("RevisionHash") + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("RowVersion") + .IsConcurrencyToken() + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.HasIndex("DeployedBy") + .HasDatabaseName("IX_DeploymentRecords_DeployedBy"); + + b.HasIndex("DeploymentId") + .IsUnique(); + + b.HasIndex("InstanceId"); + + b.HasIndex("RevisionHash") + .HasDatabaseName("IX_DeploymentRecords_RevisionHash"); + + b.HasIndex("DeployedAt", "Id") + .IsDescending() + .HasDatabaseName("IX_DeploymentRecords_DeployedAt_Id"); + + b.ToTable("DeploymentRecords"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.PendingDeployment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConfigurationJson") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedAtUtc") + .HasColumnType("datetimeoffset"); + + b.Property("DeploymentId") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("ExpiresAtUtc") + .HasColumnType("datetimeoffset"); + + b.Property("InstanceId") + .HasColumnType("int"); + + b.Property("RevisionHash") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("Token") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("nvarchar(128)"); + + b.HasKey("Id"); + + b.HasIndex("DeploymentId") + .IsUnique(); + + b.HasIndex("ExpiresAtUtc"); + + b.HasIndex("InstanceId"); + + b.ToTable("PendingDeployments", (string)null); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.SystemArtifactDeploymentRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ArtifactType") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("DeployedAt") + .HasColumnType("datetimeoffset"); + + b.Property("DeployedBy") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("PerSiteStatus") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.HasKey("Id"); + + b.HasIndex("DeployedAt"); + + b.ToTable("SystemArtifactDeploymentRecords"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems.DatabaseConnectionDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConnectionString") + .IsRequired() + .HasMaxLength(8000) + .HasColumnType("nvarchar(max)"); + + b.Property("MaxRetries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("RetryDelay") + .HasColumnType("time"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("RowVersion"); + + b.ToTable("DatabaseConnectionDefinitions"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems.ExternalSystemDefinition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AuthConfiguration") + .HasMaxLength(8000) + .HasColumnType("nvarchar(max)"); + + b.Property("AuthType") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("EndpointUrl") + .IsRequired() + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("MaxRetries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("RetryDelay") + .HasColumnType("time"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("TimeoutSeconds") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasDefaultValue(0); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("RowVersion"); + + b.ToTable("ExternalSystemDefinitions"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems.ExternalSystemMethod", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ExternalSystemDefinitionId") + .HasColumnType("int"); + + b.Property("HttpMethod") + .IsRequired() + .HasMaxLength(10) + .HasColumnType("nvarchar(10)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ParameterDefinitions") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("Path") + .IsRequired() + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("ReturnDefinition") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("ExternalSystemDefinitionId", "Name") + .IsUnique(); + + b.ToTable("ExternalSystemMethods"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.InboundApi.ApiMethod", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ParameterDefinitions") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("ReturnDefinition") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("Script") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("TimeoutSeconds") + .HasColumnType("int"); + + b.Property("TraceAllCalls") + .ValueGeneratedOnAdd() + .HasColumnType("bit") + .HasDefaultValue(false); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("ApiMethods"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Area", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ParentAreaId") + .HasColumnType("int"); + + b.Property("SiteId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ParentAreaId"); + + b.HasIndex("SiteId", "ParentAreaId", "Name") + .IsUnique() + .HasFilter("[ParentAreaId] IS NOT NULL"); + + b.ToTable("Areas"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AreaId") + .HasColumnType("int"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("SiteId") + .HasColumnType("int"); + + b.Property("State") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("TemplateId") + .HasColumnType("int"); + + b.Property("UniqueName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.HasKey("Id"); + + b.HasIndex("AreaId"); + + b.HasIndex("RowVersion"); + + b.HasIndex("SiteId"); + + b.HasIndex("TemplateId"); + + b.HasIndex("UniqueName") + .IsUnique(); + + b.ToTable("Instances"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceAlarmOverride", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AlarmCanonicalName") + .IsRequired() + .HasMaxLength(400) + .HasColumnType("nvarchar(400)"); + + b.Property("InstanceId") + .HasColumnType("int"); + + b.Property("IsMesReportableOverride") + .HasColumnType("bit"); + + b.Property("PriorityLevelOverride") + .HasColumnType("int"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("TriggerConfigurationOverride") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("InstanceId", "AlarmCanonicalName") + .IsUnique(); + + b.ToTable("InstanceAlarmOverrides"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceAttributeOverride", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AttributeName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ElementDataType") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("InstanceId") + .HasColumnType("int"); + + b.Property("OverrideValue") + .HasColumnType("nvarchar(max)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("InstanceId", "AttributeName") + .IsUnique(); + + b.ToTable("InstanceAttributeOverrides"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceConnectionBinding", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AttributeName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("DataConnectionId") + .HasColumnType("int"); + + b.Property("DataSourceReferenceOverride") + .HasMaxLength(512) + .HasColumnType("nvarchar(512)"); + + b.Property("InstanceId") + .HasColumnType("int"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.HasKey("Id"); + + b.HasIndex("DataConnectionId"); + + b.HasIndex("RowVersion"); + + b.HasIndex("InstanceId", "AttributeName") + .IsUnique(); + + b.ToTable("InstanceConnectionBindings"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceNativeAlarmSourceOverride", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionFilterOverride") + .HasMaxLength(1000) + .HasColumnType("nvarchar(1000)"); + + b.Property("ConnectionNameOverride") + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("InstanceId") + .HasColumnType("int"); + + b.Property("IsMesReportableOverride") + .HasColumnType("bit"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("SourceCanonicalName") + .IsRequired() + .HasMaxLength(400) + .HasColumnType("nvarchar(400)"); + + b.Property("SourceReferenceOverride") + .HasMaxLength(1000) + .HasColumnType("nvarchar(1000)"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("InstanceId", "SourceCanonicalName") + .IsUnique(); + + b.ToTable("InstanceNativeAlarmSourceOverrides"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi.KpiRollupHourly", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("HourStartUtc") + .HasColumnType("datetime2"); + + b.Property("MaxValue") + .HasColumnType("float"); + + b.Property("Metric") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("MinValue") + .HasColumnType("float"); + + b.Property("SampleCount") + .HasColumnType("int"); + + b.Property("Scope") + .IsRequired() + .HasMaxLength(16) + .IsUnicode(false) + .HasColumnType("varchar(16)"); + + b.Property("ScopeKey") + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("Value") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("HourStartUtc") + .HasDatabaseName("IX_KpiRollupHourly_Hour"); + + b.HasIndex("Source", "Metric", "Scope", "ScopeKey", "HourStartUtc") + .IsUnique() + .HasDatabaseName("IX_KpiRollupHourly_Series"); + + b.ToTable("KpiRollupHourly", (string)null); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi.KpiSample", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CapturedAtUtc") + .HasColumnType("datetime2"); + + b.Property("Metric") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("Scope") + .IsRequired() + .HasMaxLength(16) + .IsUnicode(false) + .HasColumnType("varchar(16)"); + + b.Property("ScopeKey") + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("Source") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("Value") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("CapturedAtUtc") + .HasDatabaseName("IX_KpiSample_Captured"); + + b.HasIndex("Source", "Metric", "Scope", "ScopeKey", "CapturedAtUtc") + .HasDatabaseName("IX_KpiSample_Series"); + + b.ToTable("KpiSample", (string)null); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.Notification", b => + { + b.Property("NotificationId") + .HasMaxLength(64) + .HasColumnType("nvarchar(64)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedAt") + .HasColumnType("datetimeoffset"); + + b.Property("DeliveredAt") + .HasColumnType("datetimeoffset"); + + b.Property("LastAttemptAt") + .HasColumnType("datetimeoffset"); + + b.Property("LastError") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("ListName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NextAttemptAt") + .HasColumnType("datetimeoffset"); + + b.Property("OriginExecutionId") + .HasColumnType("uniqueidentifier"); + + b.Property("OriginParentExecutionId") + .HasColumnType("uniqueidentifier"); + + b.Property("ResolvedTargets") + .HasColumnType("nvarchar(max)"); + + b.Property("RetryCount") + .HasColumnType("int"); + + b.Property("SiteEnqueuedAt") + .HasColumnType("datetimeoffset"); + + b.Property("SourceInstanceId") + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("SourceNode") + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("SourceScript") + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("SourceSiteId") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("Subject") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("nvarchar(1000)"); + + b.Property("Type") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("TypeData") + .HasColumnType("nvarchar(max)"); + + b.HasKey("NotificationId"); + + b.HasIndex("DeliveredAt") + .HasDatabaseName("IX_Notifications_Delivered") + .HasFilter("[Status] = 'Delivered'"); + + b.HasIndex("CreatedAt", "NotificationId") + .IsDescending() + .HasDatabaseName("IX_Notifications_Created_NotificationId"); + + b.HasIndex("SourceSiteId", "CreatedAt"); + + b.HasIndex("Status", "NextAttemptAt"); + + b.ToTable("Notifications"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.NotificationList", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("Type") + .IsRequired() + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("NotificationLists"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.NotificationRecipient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("EmailAddress") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NotificationListId") + .HasColumnType("int"); + + b.Property("PhoneNumber") + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.HasKey("Id"); + + b.HasIndex("NotificationListId"); + + b.ToTable("NotificationRecipients"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.SmsConfiguration", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AccountSid") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("ApiBaseUrl") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("AuthToken") + .HasMaxLength(8000) + .HasColumnType("nvarchar(max)"); + + b.Property("ConnectionTimeoutSeconds") + .HasColumnType("int"); + + b.Property("FromNumber") + .HasMaxLength(32) + .HasColumnType("nvarchar(32)"); + + b.Property("MaxRetries") + .HasColumnType("int"); + + b.Property("MessagingServiceSid") + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("RetryDelay") + .HasColumnType("time"); + + b.HasKey("Id"); + + b.ToTable("SmsConfigurations"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.SmtpConfiguration", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AuthType") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("ConnectionTimeoutSeconds") + .HasColumnType("int"); + + b.Property("Credentials") + .HasMaxLength(8000) + .HasColumnType("nvarchar(max)"); + + b.Property("FromAddress") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("Host") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("MaxConcurrentConnections") + .HasColumnType("int"); + + b.Property("MaxRetries") + .HasColumnType("int"); + + b.Property("OAuth2Authority") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("OAuth2Scope") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("Port") + .HasColumnType("int"); + + b.Property("RetryDelay") + .HasColumnType("time"); + + b.Property("TlsMode") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Transport") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.ToTable("SmtpConfigurations"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Schemas.SharedSchema", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("SchemaJson") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Scope") + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("SharedSchemas"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Scripts.SharedScript", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Code") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ParameterDefinitions") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("ReturnDefinition") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("RowVersion"); + + b.ToTable("SharedScripts"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.SecuredWrites.PendingSecuredWrite", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConnectionName") + .IsRequired() + .HasMaxLength(128) + .IsUnicode(false) + .HasColumnType("varchar(128)"); + + b.Property("DecidedAtUtc") + .HasColumnType("datetime2"); + + b.Property("ExecutedAtUtc") + .HasColumnType("datetime2"); + + b.Property("ExecutionError") + .HasMaxLength(2048) + .IsUnicode(false) + .HasColumnType("varchar(2048)"); + + b.Property("OperatorComment") + .HasMaxLength(1024) + .IsUnicode(false) + .HasColumnType("varchar(1024)"); + + b.Property("OperatorUser") + .IsRequired() + .HasMaxLength(256) + .IsUnicode(false) + .HasColumnType("varchar(256)"); + + b.Property("SiteId") + .IsRequired() + .HasMaxLength(128) + .IsUnicode(false) + .HasColumnType("varchar(128)"); + + b.Property("Status") + .IsRequired() + .HasMaxLength(32) + .IsUnicode(false) + .HasColumnType("varchar(32)"); + + b.Property("SubmittedAtUtc") + .HasColumnType("datetime2"); + + b.Property("TagPath") + .IsRequired() + .HasMaxLength(512) + .IsUnicode(false) + .HasColumnType("varchar(512)"); + + b.Property("ValueJson") + .IsRequired() + .HasMaxLength(4000) + .IsUnicode(false) + .HasColumnType("varchar(4000)"); + + b.Property("ValueType") + .IsRequired() + .HasMaxLength(128) + .IsUnicode(false) + .HasColumnType("varchar(128)"); + + b.Property("VerifierComment") + .HasMaxLength(1024) + .IsUnicode(false) + .HasColumnType("varchar(1024)"); + + b.Property("VerifierUser") + .HasMaxLength(256) + .IsUnicode(false) + .HasColumnType("varchar(256)"); + + b.HasKey("Id"); + + b.HasIndex("SiteId") + .HasDatabaseName("IX_PendingSecuredWrites_Site"); + + b.HasIndex("Status", "SubmittedAtUtc") + .HasDatabaseName("IX_PendingSecuredWrites_Status_Submitted"); + + b.ToTable("PendingSecuredWrites", (string)null); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Security.LdapGroupMapping", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("LdapGroupName") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("Role") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.HasKey("Id"); + + b.HasIndex("LdapGroupName") + .IsUnique(); + + b.ToTable("LdapGroupMappings"); + + b.HasData( + new + { + Id = 1, + LdapGroupName = "SCADA-Admins", + Role = "Administrator" + }, + new + { + Id = 2, + LdapGroupName = "SCADA-Designers", + Role = "Designer" + }, + new + { + Id = 3, + LdapGroupName = "SCADA-Deploy-All", + Role = "Deployer" + }, + new + { + Id = 4, + LdapGroupName = "SCADA-Deploy-SiteA", + Role = "Deployer" + }, + new + { + Id = 5, + LdapGroupName = "SCADA-Viewers", + Role = "Viewer" + }); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Security.SiteScopeRule", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("LdapGroupMappingId") + .HasColumnType("int"); + + b.Property("SiteId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("SiteId"); + + b.HasIndex("LdapGroupMappingId", "SiteId") + .IsUnique(); + + b.ToTable("SiteScopeRules"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.DataConnection", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BackupConfiguration") + .HasColumnType("nvarchar(max)"); + + b.Property("FailoverRetryCount") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasDefaultValue(3); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("PrimaryConfiguration") + .HasColumnType("nvarchar(max)"); + + b.Property("Protocol") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("SiteId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("SiteId", "Name") + .IsUnique(); + + b.ToTable("DataConnections"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.Site", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ArtifactRevisionHash") + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.Property("ArtifactsDeployedAt") + .HasColumnType("datetimeoffset"); + + b.Property("Description") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("GrpcNodeAAddress") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("GrpcNodeBAddress") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("NodeAAddress") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("NodeBAddress") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("SiteIdentifier") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("nvarchar(100)"); + + b.HasKey("Id"); + + b.HasIndex("Name") + .IsUnique(); + + b.HasIndex("RowVersion"); + + b.HasIndex("SiteIdentifier") + .IsUnique(); + + b.ToTable("Sites"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("FolderId") + .HasColumnType("int"); + + b.Property("IsDerived") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("OwnerCompositionId") + .HasColumnType("int"); + + b.Property("ParentTemplateId") + .HasColumnType("int"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.HasKey("Id"); + + b.HasIndex("FolderId"); + + b.HasIndex("Name") + .IsUnique() + .HasFilter("[IsDerived] = 0"); + + b.HasIndex("ParentTemplateId"); + + b.HasIndex("RowVersion"); + + b.ToTable("Templates"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateAlarm", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("IsInherited") + .HasColumnType("bit"); + + b.Property("IsLocked") + .HasColumnType("bit"); + + b.Property("IsMesReportable") + .HasColumnType("bit"); + + b.Property("LockedInDerived") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("OnTriggerScriptId") + .HasColumnType("int"); + + b.Property("PriorityLevel") + .HasColumnType("int"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("TemplateId") + .HasColumnType("int"); + + b.Property("TriggerConfiguration") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("TriggerType") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("TemplateId", "Name") + .IsUnique(); + + b.ToTable("TemplateAlarms"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateAttribute", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("DataSourceReference") + .HasMaxLength(500) + .HasColumnType("nvarchar(500)"); + + b.Property("DataType") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("Description") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("ElementDataType") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.Property("IsInherited") + .HasColumnType("bit"); + + b.Property("IsLocked") + .HasColumnType("bit"); + + b.Property("LockedInDerived") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("TemplateId") + .HasColumnType("int"); + + b.Property("Value") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("TemplateId", "Name") + .IsUnique(); + + b.ToTable("TemplateAttributes"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateComposition", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComposedTemplateId") + .HasColumnType("int"); + + b.Property("InstanceName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("TemplateId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComposedTemplateId"); + + b.HasIndex("RowVersion"); + + b.HasIndex("TemplateId", "InstanceName") + .IsUnique(); + + b.ToTable("TemplateCompositions"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateFolder", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ParentFolderId") + .HasColumnType("int"); + + b.Property("SortOrder") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ParentFolderId", "Name") + .IsUnique() + .HasFilter("[ParentFolderId] IS NOT NULL"); + + b.ToTable("TemplateFolders"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateNativeAlarmSource", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ConditionFilter") + .HasMaxLength(1000) + .HasColumnType("nvarchar(1000)"); + + b.Property("ConnectionName") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("Description") + .HasMaxLength(2000) + .HasColumnType("nvarchar(2000)"); + + b.Property("IsInherited") + .HasColumnType("bit"); + + b.Property("IsLocked") + .HasColumnType("bit"); + + b.Property("IsMesReportable") + .HasColumnType("bit"); + + b.Property("LockedInDerived") + .HasColumnType("bit"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("SourceReference") + .IsRequired() + .HasMaxLength(1000) + .HasColumnType("nvarchar(1000)"); + + b.Property("TemplateId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("TemplateId", "Name") + .IsUnique(); + + b.ToTable("TemplateNativeAlarmSources"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateScript", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Code") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ExecutionTimeoutSeconds") + .HasColumnType("int"); + + b.Property("IsInherited") + .HasColumnType("bit"); + + b.Property("IsLocked") + .HasColumnType("bit"); + + b.Property("LockedInDerived") + .HasColumnType("bit"); + + b.Property("MinTimeBetweenRuns") + .HasColumnType("time"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(200) + .HasColumnType("nvarchar(200)"); + + b.Property("ParameterDefinitions") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("ReturnDefinition") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("RowVersion") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("rowversion"); + + b.Property("TemplateId") + .HasColumnType("int"); + + b.Property("TriggerConfiguration") + .HasMaxLength(4000) + .HasColumnType("nvarchar(4000)"); + + b.Property("TriggerType") + .HasMaxLength(50) + .HasColumnType("nvarchar(50)"); + + b.HasKey("Id"); + + b.HasIndex("RowVersion"); + + b.HasIndex("TemplateId", "Name") + .IsUnique(); + + b.ToTable("TemplateScripts"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Transport.BundleUnlockAttempt", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("AttemptedAtUtc") + .HasColumnType("datetime2"); + + b.Property("ClientKey") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.HasKey("Id"); + + b.HasIndex("ClientKey", "AttemptedAtUtc") + .HasDatabaseName("IX_BundleUnlockAttempt_Client_Attempted"); + + b.ToTable("BundleUnlockAttempt", (string)null); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities.AuditLogRow", b => + { + b.Property("OccurredAtUtc") + .HasColumnType("datetime2"); + + b.Property("EventId") + .HasColumnType("uniqueidentifier"); + + b.Property("Action") + .IsRequired() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("Actor") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.Property("Channel") + .IsRequired() + .HasMaxLength(32) + .IsUnicode(false) + .HasColumnType("varchar(32)") + .HasColumnName("Category"); + + b.Property("CorrelationId") + .HasColumnType("uniqueidentifier"); + + b.Property("DetailsJson") + .HasColumnType("nvarchar(max)"); + + b.Property("ExecutionId") + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("uniqueidentifier") + .HasComputedColumnSql("CAST(JSON_VALUE(DetailsJson,'$.executionId') AS uniqueidentifier)", true); + + b.Property("IngestedAtUtc") + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("datetime2(7)") + .HasComputedColumnSql("CAST(SWITCHOFFSET(CAST(JSON_VALUE(DetailsJson,'$.ingestedAtUtc') AS datetimeoffset), 0) AS datetime2(7))", false); + + b.Property("Kind") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasMaxLength(32) + .IsUnicode(false) + .HasColumnType("varchar(32)") + .HasComputedColumnSql("JSON_VALUE(DetailsJson,'$.kind')", true); + + b.Property("Outcome") + .IsRequired() + .HasMaxLength(16) + .IsUnicode(false) + .HasColumnType("varchar(16)"); + + b.Property("ParentExecutionId") + .ValueGeneratedOnAddOrUpdate() + .HasColumnType("uniqueidentifier") + .HasComputedColumnSql("CAST(JSON_VALUE(DetailsJson,'$.parentExecutionId') AS uniqueidentifier)", true); + + b.Property("SourceNode") + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)"); + + b.Property("SourceSiteId") + .ValueGeneratedOnAddOrUpdate() + .HasMaxLength(64) + .IsUnicode(false) + .HasColumnType("varchar(64)") + .HasComputedColumnSql("JSON_VALUE(DetailsJson,'$.sourceSiteId')", true); + + b.Property("Status") + .IsRequired() + .ValueGeneratedOnAddOrUpdate() + .HasMaxLength(32) + .IsUnicode(false) + .HasColumnType("varchar(32)") + .HasComputedColumnSql("JSON_VALUE(DetailsJson,'$.status')", true); + + b.Property("Target") + .HasMaxLength(256) + .HasColumnType("nvarchar(256)"); + + b.HasKey("OccurredAtUtc", "EventId"); + + b.HasIndex("CorrelationId") + .HasDatabaseName("IX_AuditLog_CorrelationId") + .HasFilter("[CorrelationId] IS NOT NULL"); + + b.HasIndex("ExecutionId") + .HasDatabaseName("IX_AuditLog_Execution"); + + b.HasIndex("OccurredAtUtc") + .IsDescending() + .HasDatabaseName("IX_AuditLog_OccurredAtUtc"); + + SqlServerIndexBuilderExtensions.IncludeProperties(b.HasIndex("OccurredAtUtc"), new[] { "Status" }); + + b.HasIndex("ParentExecutionId") + .HasDatabaseName("IX_AuditLog_ParentExecution"); + + b.HasIndex("Actor", "OccurredAtUtc") + .IsDescending(false, true) + .HasDatabaseName("IX_AuditLog_Actor_Occurred"); + + b.HasIndex("EventId", "OccurredAtUtc") + .IsUnique() + .HasDatabaseName("UX_AuditLog_EventId_Occurred"); + + b.HasIndex("Kind", "OccurredAtUtc") + .IsDescending(false, true) + .HasDatabaseName("IX_AuditLog_Kind_Occurred"); + + b.HasIndex("SourceNode", "OccurredAtUtc") + .HasDatabaseName("IX_AuditLog_Node_Occurred"); + + b.HasIndex("SourceSiteId", "OccurredAtUtc") + .IsDescending(false, true) + .HasDatabaseName("IX_AuditLog_Site_Occurred"); + + b.HasIndex("Target", "OccurredAtUtc") + .IsDescending(false, true) + .HasDatabaseName("IX_AuditLog_Target_Occurred") + .HasFilter("[Target] IS NOT NULL"); + + b.HasIndex("Channel", "Status", "OccurredAtUtc") + .IsDescending(false, false, true) + .HasDatabaseName("IX_AuditLog_Channel_Status_Occurred"); + + b.ToTable("AuditLog", (string)null); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.DeployedConfigSnapshot", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", null) + .WithMany() + .HasForeignKey("InstanceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.DeploymentRecord", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", null) + .WithMany() + .HasForeignKey("InstanceId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment.PendingDeployment", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", null) + .WithMany() + .HasForeignKey("InstanceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems.ExternalSystemMethod", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.ExternalSystems.ExternalSystemDefinition", null) + .WithMany() + .HasForeignKey("ExternalSystemDefinitionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Area", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Area", null) + .WithMany("Children") + .HasForeignKey("ParentAreaId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.Site", null) + .WithMany() + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Area", null) + .WithMany() + .HasForeignKey("AreaId") + .OnDelete(DeleteBehavior.SetNull); + + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.Site", null) + .WithMany() + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany() + .HasForeignKey("TemplateId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceAlarmOverride", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", null) + .WithMany("AlarmOverrides") + .HasForeignKey("InstanceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceAttributeOverride", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", null) + .WithMany("AttributeOverrides") + .HasForeignKey("InstanceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceConnectionBinding", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.DataConnection", null) + .WithMany() + .HasForeignKey("DataConnectionId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", null) + .WithMany("ConnectionBindings") + .HasForeignKey("InstanceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.InstanceNativeAlarmSourceOverride", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", null) + .WithMany("NativeAlarmSourceOverrides") + .HasForeignKey("InstanceId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.NotificationRecipient", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.NotificationList", null) + .WithMany("Recipients") + .HasForeignKey("NotificationListId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Security.SiteScopeRule", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Security.LdapGroupMapping", null) + .WithMany() + .HasForeignKey("LdapGroupMappingId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.Site", null) + .WithMany() + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.DataConnection", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites.Site", null) + .WithMany() + .HasForeignKey("SiteId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateFolder", null) + .WithMany() + .HasForeignKey("FolderId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany() + .HasForeignKey("ParentTemplateId") + .OnDelete(DeleteBehavior.Restrict); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateAlarm", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany("Alarms") + .HasForeignKey("TemplateId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateAttribute", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany("Attributes") + .HasForeignKey("TemplateId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateComposition", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany() + .HasForeignKey("ComposedTemplateId") + .OnDelete(DeleteBehavior.Restrict) + .IsRequired(); + + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany("Compositions") + .HasForeignKey("TemplateId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateFolder", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateFolder", null) + .WithMany() + .HasForeignKey("ParentFolderId") + .OnDelete(DeleteBehavior.Restrict); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateNativeAlarmSource", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany("NativeAlarmSources") + .HasForeignKey("TemplateId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.TemplateScript", b => + { + b.HasOne("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", null) + .WithMany("Scripts") + .HasForeignKey("TemplateId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Area", b => + { + b.Navigation("Children"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances.Instance", b => + { + b.Navigation("AlarmOverrides"); + + b.Navigation("AttributeOverrides"); + + b.Navigation("ConnectionBindings"); + + b.Navigation("NativeAlarmSourceOverrides"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications.NotificationList", b => + { + b.Navigation("Recipients"); + }); + + modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates.Template", b => + { + b.Navigation("Alarms"); + + b.Navigation("Attributes"); + + b.Navigation("Compositions"); + + b.Navigation("NativeAlarmSources"); + + b.Navigation("Scripts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261115221558_RemoveAuditLogHashChain.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261115221558_RemoveAuditLogHashChain.cs new file mode 100644 index 00000000..05d1e768 --- /dev/null +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/20261115221558_RemoveAuditLogHashChain.cs @@ -0,0 +1,153 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations +{ + /// + /// Removes the AuditLog hash chain (ScadaBridge#41, #107, #139) and the two-login maintenance + /// hardening (#113), at IT security's request (2026-11-15), pending a family-wide general + /// solution. Drops what AddAuditLogHashChain and AuditMaintenanceGrants added: + /// + /// the two partition-aligned filtered indexes IX_AuditLog_Unchained and + /// IX_AuditLog_ChainSeq; + /// the scadabridge_audit_chainer role (members removed first) and its + /// column-level grant on (ChainSeq, RowHash); + /// dbo.AuditLogChainHead and dbo.AuditLogChainTombstone (their grants + /// and the writer role's DENYs on them go with the tables); + /// dbo.AuditLog.ChainSeq and RowHash; + /// the two grants AuditMaintenanceGrants gave scadabridge_audit_purger + /// (ALTER ANY DATASPACE, UPDATE (SourceNode)). + /// + /// + /// scadabridge_audit_purger and scadabridge_audit_writer themselves are KEPT: + /// they predate the chain (AddAuditLogTable, FixAuditPurgerRoleGrants) and are + /// the original append-only design. + /// + /// + /// Every statement is existence-guarded, and every reference to a chain column goes through + /// EXEC: dotnet ef migrations script --idempotent puts a migration in one + /// batch, and SQL Server compiles the batch before any guard runs, so a direct reference to + /// a column that is (or is not yet) there fails with Msg 207 (ScadaBridge#126). + /// + /// + /// Deploy gate. Binaries before this change map ChainSeq/RowHash and read + /// the chain tables, so they cannot run against the migrated schema: apply with both centrals + /// stopped, together with the new binaries. The new binaries purge either schema (the + /// partition-switch staging table copies the chain columns only when the live table has them). + /// + /// + public partial class RemoveAuditLogHashChain : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql(@" +IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_ChainSeq' AND object_id = OBJECT_ID('dbo.AuditLog')) + EXEC(N'DROP INDEX IX_AuditLog_ChainSeq ON dbo.AuditLog;'); +IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_Unchained' AND object_id = OBJECT_ID('dbo.AuditLog')) + EXEC(N'DROP INDEX IX_AuditLog_Unchained ON dbo.AuditLog;');"); + + migrationBuilder.Sql(@" +IF DATABASE_PRINCIPAL_ID('scadabridge_audit_chainer') IS NOT NULL +BEGIN + IF COL_LENGTH('dbo.AuditLog', 'ChainSeq') IS NOT NULL + EXEC(N'REVOKE UPDATE (ChainSeq, RowHash) ON dbo.AuditLog FROM scadabridge_audit_chainer;'); + EXEC(N'REVOKE SELECT ON dbo.AuditLog FROM scadabridge_audit_chainer;'); + + DECLARE @dropMembers nvarchar(max) = N''; + SELECT @dropMembers = @dropMembers + N'ALTER ROLE scadabridge_audit_chainer DROP MEMBER ' + QUOTENAME(m.name) + N';' + CHAR(10) + FROM sys.database_role_members rm + JOIN sys.database_principals r ON r.principal_id = rm.role_principal_id + JOIN sys.database_principals m ON m.principal_id = rm.member_principal_id + WHERE r.name = N'scadabridge_audit_chainer'; + EXEC sp_executesql @dropMembers; + + EXEC(N'DROP ROLE scadabridge_audit_chainer;'); +END"); + + migrationBuilder.Sql(@" +IF OBJECT_ID('dbo.AuditLogChainHead', 'U') IS NOT NULL + EXEC(N'DROP TABLE dbo.AuditLogChainHead;'); +IF OBJECT_ID('dbo.AuditLogChainTombstone', 'U') IS NOT NULL + EXEC(N'DROP TABLE dbo.AuditLogChainTombstone;');"); + + migrationBuilder.Sql(@" +IF COL_LENGTH('dbo.AuditLog', 'ChainSeq') IS NOT NULL + EXEC(N'ALTER TABLE dbo.AuditLog DROP COLUMN ChainSeq;'); +IF COL_LENGTH('dbo.AuditLog', 'RowHash') IS NOT NULL + EXEC(N'ALTER TABLE dbo.AuditLog DROP COLUMN RowHash;');"); + + migrationBuilder.Sql(@" +IF DATABASE_PRINCIPAL_ID('scadabridge_audit_purger') IS NOT NULL +BEGIN + EXEC(N'REVOKE ALTER ANY DATASPACE FROM scadabridge_audit_purger;'); + EXEC(N'REVOKE UPDATE (SourceNode) ON dbo.AuditLog FROM scadabridge_audit_purger;'); +END"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + // Recreates exactly what AddAuditLogHashChain and AuditMaintenanceGrants created, so + // the migrations before this one (and their Down methods) find the schema they expect. + // The chain itself is not restored: every row comes back unchained. + migrationBuilder.Sql(@" +IF COL_LENGTH('dbo.AuditLog', 'ChainSeq') IS NULL + EXEC(N'ALTER TABLE dbo.AuditLog ADD ChainSeq bigint NULL;'); +IF COL_LENGTH('dbo.AuditLog', 'RowHash') IS NULL + EXEC(N'ALTER TABLE dbo.AuditLog ADD RowHash binary(32) NULL;');"); + + migrationBuilder.Sql(@" +IF OBJECT_ID('dbo.AuditLogChainHead', 'U') IS NULL + EXEC(N'CREATE TABLE dbo.AuditLogChainHead ( + ChainMonth date NOT NULL, + LastSeq bigint NOT NULL, + LastHash binary(32) NOT NULL, + PurgedThroughSeq bigint NULL, + PurgedThroughHash binary(32) NULL, + UpdatedAtUtc datetime2 NOT NULL, + CONSTRAINT PK_AuditLogChainHead PRIMARY KEY (ChainMonth));'); +IF OBJECT_ID('dbo.AuditLogChainTombstone', 'U') IS NULL + EXEC(N'CREATE TABLE dbo.AuditLogChainTombstone ( + ChainMonth date NOT NULL, + ChainSeq bigint NOT NULL, + RowHash binary(32) NOT NULL, + EventId uniqueidentifier NOT NULL, + PurgedAtUtc datetime2 NOT NULL, + CONSTRAINT PK_AuditLogChainTombstone PRIMARY KEY (ChainMonth, ChainSeq));');"); + + migrationBuilder.Sql(@" +IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_Unchained' AND object_id = OBJECT_ID('dbo.AuditLog')) + EXEC(N'CREATE NONCLUSTERED INDEX IX_AuditLog_Unchained +ON dbo.AuditLog (OccurredAtUtc, EventId) +WHERE ChainSeq IS NULL +ON ps_AuditLog_Month(OccurredAtUtc);'); +IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_ChainSeq' AND object_id = OBJECT_ID('dbo.AuditLog')) + EXEC(N'CREATE NONCLUSTERED INDEX IX_AuditLog_ChainSeq +ON dbo.AuditLog (ChainSeq) +WHERE ChainSeq IS NOT NULL +ON ps_AuditLog_Month(OccurredAtUtc);');"); + + migrationBuilder.Sql(@" +IF DATABASE_PRINCIPAL_ID('scadabridge_audit_chainer') IS NULL + EXEC sp_executesql N'CREATE ROLE scadabridge_audit_chainer'; +EXEC(N'GRANT SELECT ON dbo.AuditLog TO scadabridge_audit_chainer; +GRANT UPDATE (ChainSeq, RowHash) ON dbo.AuditLog TO scadabridge_audit_chainer; +GRANT SELECT, INSERT, UPDATE ON dbo.AuditLogChainHead TO scadabridge_audit_chainer; +GRANT SELECT ON dbo.AuditLogChainTombstone TO scadabridge_audit_chainer;'); + +IF DATABASE_PRINCIPAL_ID('scadabridge_audit_purger') IS NULL + EXEC sp_executesql N'CREATE ROLE scadabridge_audit_purger'; +EXEC(N'GRANT SELECT, UPDATE ON dbo.AuditLogChainHead TO scadabridge_audit_purger; +GRANT SELECT, INSERT, DELETE ON dbo.AuditLogChainTombstone TO scadabridge_audit_purger; +GRANT ALTER ANY DATASPACE TO scadabridge_audit_purger; +GRANT UPDATE (SourceNode) ON dbo.AuditLog TO scadabridge_audit_purger;'); + +IF DATABASE_PRINCIPAL_ID('scadabridge_audit_writer') IS NULL + EXEC sp_executesql N'CREATE ROLE scadabridge_audit_writer'; +EXEC(N'DENY INSERT, UPDATE, DELETE ON dbo.AuditLogChainHead TO scadabridge_audit_writer; +DENY INSERT, UPDATE, DELETE ON dbo.AuditLogChainTombstone TO scadabridge_audit_writer;');"); + } + } +} diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/ScadaBridgeDbContextModelSnapshot.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/ScadaBridgeDbContextModelSnapshot.cs index 6188a476..150d4bd5 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/ScadaBridgeDbContextModelSnapshot.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Migrations/ScadaBridgeDbContextModelSnapshot.cs @@ -1922,61 +1922,6 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations b.ToTable("BundleUnlockAttempt", (string)null); }); - modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities.AuditLogChainHead", b => - { - b.Property("ChainMonth") - .HasColumnType("date"); - - b.Property("LastHash") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("binary(32)") - .IsFixedLength(); - - b.Property("LastSeq") - .HasColumnType("bigint"); - - b.Property("PurgedThroughHash") - .HasMaxLength(32) - .HasColumnType("binary(32)") - .IsFixedLength(); - - b.Property("PurgedThroughSeq") - .HasColumnType("bigint"); - - b.Property("UpdatedAtUtc") - .HasColumnType("datetime2"); - - b.HasKey("ChainMonth"); - - b.ToTable("AuditLogChainHead", (string)null); - }); - - modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities.AuditLogChainTombstone", b => - { - b.Property("ChainMonth") - .HasColumnType("date"); - - b.Property("ChainSeq") - .HasColumnType("bigint"); - - b.Property("EventId") - .HasColumnType("uniqueidentifier"); - - b.Property("PurgedAtUtc") - .HasColumnType("datetime2"); - - b.Property("RowHash") - .IsRequired() - .HasMaxLength(32) - .HasColumnType("binary(32)") - .IsFixedLength(); - - b.HasKey("ChainMonth", "ChainSeq"); - - b.ToTable("AuditLogChainTombstone", (string)null); - }); - modelBuilder.Entity("ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Entities.AuditLogRow", b => { b.Property("OccurredAtUtc") @@ -1995,9 +1940,6 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations .HasMaxLength(256) .HasColumnType("nvarchar(256)"); - b.Property("ChainSeq") - .HasColumnType("bigint"); - b.Property("Channel") .IsRequired() .HasMaxLength(32) @@ -2040,11 +1982,6 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations .HasColumnType("uniqueidentifier") .HasComputedColumnSql("CAST(JSON_VALUE(DetailsJson,'$.parentExecutionId') AS uniqueidentifier)", true); - b.Property("RowHash") - .HasMaxLength(32) - .HasColumnType("binary(32)") - .IsFixedLength(); - b.Property("SourceNode") .HasMaxLength(64) .IsUnicode(false) @@ -2071,10 +2008,6 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations b.HasKey("OccurredAtUtc", "EventId"); - b.HasIndex("ChainSeq") - .HasDatabaseName("IX_AuditLog_ChainSeq") - .HasFilter("[ChainSeq] IS NOT NULL"); - b.HasIndex("CorrelationId") .HasDatabaseName("IX_AuditLog_CorrelationId") .HasFilter("[CorrelationId] IS NOT NULL"); @@ -2103,10 +2036,6 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations .IsDescending(false, true) .HasDatabaseName("IX_AuditLog_Kind_Occurred"); - b.HasIndex("OccurredAtUtc", "EventId") - .HasDatabaseName("IX_AuditLog_Unchained") - .HasFilter("[ChainSeq] IS NULL"); - b.HasIndex("SourceNode", "OccurredAtUtc") .HasDatabaseName("IX_AuditLog_Node_Occurred"); diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs index 4497871b..1c6daddc 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/Repositories/AuditLogRepository.cs @@ -49,22 +49,6 @@ public class AuditLogRepository : IAuditLogRepository private const string CanonicalColumnList = "EventId, OccurredAtUtc, Actor, Action, Outcome, Category, Target, SourceNode, CorrelationId, DetailsJson"; - // Hash chain (ScadaBridge#41). Wraps a maintenance-path batch in a transaction holding - // the EXCLUSIVE chain application lock (Maintenance.AuditChainLock), so it never - // interleaves with a chaining pass or a verification. NOCOUNT is ON for everything but - // the one mutating statement the caller places between prefix and suffix, so - // ExecuteNonQuery still returns exactly that statement's row count. XACT_ABORT rolls - // the transaction back on any error or client timeout (attention). - private const string ChainLockedBatchPrefix = - "SET XACT_ABORT ON;\nSET NOCOUNT ON;\nBEGIN TRANSACTION;\n" + - "DECLARE @chainLock int;\n" + - "EXEC @chainLock = sp_getapplock @Resource = N'" + Maintenance.AuditChainLock.Resource + "', @LockMode = 'Exclusive', @LockOwner = 'Transaction', @LockTimeout = -1;\n" + - "IF @chainLock < 0 THROW 51000, 'AuditLog hash-chain lock was not granted.', 1;\n" + - "SET NOCOUNT OFF;\n"; - - private const string ChainLockedBatchSuffix = - "\nSET NOCOUNT ON;\nCOMMIT TRANSACTION;"; - private readonly ScadaBridgeDbContext _context; private readonly ILogger _logger; @@ -494,11 +478,10 @@ VALUES /// /// The partition switch-out, optionally guarded by an archive's expectations - /// (ScadaBridge#42). With a , the switch transaction — after it - /// holds the exclusive chain lock — re-counts the partition and re-reads its months' - /// chain heads, and THROWs (error 51002 / 51003, nothing purged) when either differs - /// from what the archive captured. That is what makes "archive, then purge" safe - /// against a row arriving, a per-channel purge or a chaining pass in between. + /// (ScadaBridge#42). With a , the switch transaction re-counts + /// the partition and THROWs (error 51002, nothing purged) when the count differs from + /// what the archive captured, so "archive, then purge" never drops a row the archive + /// does not hold. /// /// The partition's lower boundary. /// Archive expectations, or null for the unguarded purge. @@ -556,21 +539,15 @@ VALUES WHERE $PARTITION.pf_AuditLog_Month(OccurredAtUtc) = $partition.pf_AuditLog_Month('{monthBoundaryStr}');"; - // ScadaBridge#42 archive guard (empty for the unguarded purge). Runs under the chain - // lock, so no chaining pass or per-channel purge can move the partition between the - // check and the SWITCH; an INSERT racing it is caught by the next tick's archive. + // ScadaBridge#42 archive guard (empty for the unguarded purge). The per-channel purge + // and this switch run on the same purge actor, one after the other, so nothing else + // removes rows between the check and the SWITCH; an INSERT racing it is caught by the + // next tick's archive. var guardSql = guard is null ? string.Empty : guard.ToSql(monthBoundaryStr); var sql = $@" BEGIN TRY BEGIN TRANSACTION; - - -- 0. Hash chain (ScadaBridge#41): hold the exclusive chain lock for the whole - -- switch so no chaining pass / verification interleaves with the anchor - -- update in step 3b. - DECLARE @chainLock int; - EXEC @chainLock = sp_getapplock @Resource = N'{Maintenance.AuditChainLock.Resource}', @LockMode = 'Exclusive', @LockOwner = 'Transaction', @LockTimeout = -1; - IF @chainLock < 0 THROW 51000, 'AuditLog hash-chain lock was not granted.', 1; {guardSql} -- 1. Defensive cleanup for databases created before -- AlignAuditLogEventIdUniqueness: the migration drops the @@ -588,8 +565,7 @@ VALUES -- expression text + PERSISTED) or ALTER TABLE ... SWITCH PARTITION -- rejects the operation with msg 4904/4948. The ordinal order also -- matches dbo.AuditLog_v2 (the CollapseAuditLogToCanonical migration): - -- 10 canonical columns first, then the 6 computed columns, then the two - -- hash-chain columns AddAuditLogHashChain appended (ScadaBridge#41). + -- 10 canonical columns first, then the 6 computed columns. CREATE TABLE dbo.[{stagingTableName}] ( EventId uniqueidentifier NOT NULL, OccurredAtUtc datetime2(7) NOT NULL, @@ -606,11 +582,15 @@ VALUES SourceSiteId AS JSON_VALUE(DetailsJson,'$.sourceSiteId') PERSISTED, ExecutionId AS CAST(JSON_VALUE(DetailsJson,'$.executionId') AS uniqueidentifier) PERSISTED, ParentExecutionId AS CAST(JSON_VALUE(DetailsJson,'$.parentExecutionId') AS uniqueidentifier) PERSISTED, - IngestedAtUtc AS CAST(SWITCHOFFSET(CAST(JSON_VALUE(DetailsJson,'$.ingestedAtUtc') AS datetimeoffset), 0) AS datetime2(7)), - ChainSeq bigint NULL, - RowHash binary(32) NULL + IngestedAtUtc AS CAST(SWITCHOFFSET(CAST(JSON_VALUE(DetailsJson,'$.ingestedAtUtc') AS datetimeoffset), 0) AS datetime2(7)) ) ON [PRIMARY]; + -- 2a. A database that has not yet run RemoveAuditLogHashChain still carries the two + -- columns AddAuditLogHashChain appended; SWITCH needs them on staging too. Read + -- the live table instead of assuming, so this binary purges either schema. + IF COL_LENGTH('dbo.AuditLog', 'ChainSeq') IS NOT NULL + EXEC(N'ALTER TABLE dbo.[{stagingTableName}] ADD ChainSeq bigint NULL, RowHash binary(32) NULL;'); + -- 2b. The staging clustered key must match dbo.AuditLog's or SWITCH refuses -- (msg 4913/4947). FlipAuditLogClusteredKey (ScadaBridge#111) changed it from -- (EventId, OccurredAtUtc) to (OccurredAtUtc, EventId), so read the live key @@ -631,19 +611,6 @@ VALUES DECLARE @sql nvarchar(max) = 'ALTER TABLE dbo.AuditLog SWITCH PARTITION ' + CAST(@partitionNumber AS nvarchar(10)) + ' TO dbo.[{stagingTableName}];'; EXEC sp_executesql @sql; - -- 3b. Hash chain (ScadaBridge#41): the months this partition held are gone as a - -- whole, so record each month's current chain end as its checkpoint anchor - -- (verification resumes after it; a late-arriving row chains on from it) - -- and drop the per-channel tombstones the anchor now subsumes. The chain-start - -- anchor (ChainSeq 0, ScadaBridge#139) is NOT a purge tombstone: it records the - -- cutoff for the life of the database, so it stays. - UPDATE dbo.AuditLogChainHead - SET PurgedThroughSeq = LastSeq, PurgedThroughHash = LastHash, UpdatedAtUtc = SYSUTCDATETIME() - WHERE $partition.pf_AuditLog_Month(CAST(ChainMonth AS datetime2(7))) = @partitionNumber; - DELETE FROM dbo.AuditLogChainTombstone - WHERE $partition.pf_AuditLog_Month(CAST(ChainMonth AS datetime2(7))) = @partitionNumber - AND ChainSeq > 0; - -- 4. Drop staging — the rows are discarded here. This is the purge. DROP TABLE dbo.[{stagingTableName}]; @@ -771,22 +738,8 @@ VALUES // The trailing AUDIT-PURGE-ALLOWED marker on the DELETE line below is the // single narrow exemption the append-only CI guard (AuditLogAppendOnlyGuardTests) // recognizes; any other UPDATE/DELETE targeting AuditLog still trips the guard. - // - // Hash chain (ScadaBridge#41): a row deleted from the middle of a month's chain must - // leave its link behind or verification reports the gap as tampering. The DELETE - // OUTPUTs every removed row into a table variable and the same transaction writes a - // tombstone (month, ChainSeq, RowHash) for each CHAINED one; the whole batch runs - // under the exclusive chain lock so a chaining pass or a verification never sees the - // row gone without its tombstone. Rows not yet chained need no tombstone. const string deleteBatchSql = - ChainLockedBatchPrefix + - "DECLARE @purged TABLE (EventId uniqueidentifier NOT NULL, OccurredAtUtc datetime2(7) NOT NULL, ChainSeq bigint NULL, RowHash binary(32) NULL);\n" + - "DELETE TOP (@batch) FROM dbo.AuditLog OUTPUT deleted.EventId, deleted.OccurredAtUtc, deleted.ChainSeq, deleted.RowHash INTO @purged WHERE Category = @channel AND OccurredAtUtc < @threshold;\n" + // AUDIT-PURGE-ALLOWED: per-channel retention override, maintenance path - "SET NOCOUNT ON;\n" + - "INSERT INTO dbo.AuditLogChainTombstone (ChainMonth, ChainSeq, RowHash, EventId, PurgedAtUtc)\n" + - "SELECT DATEFROMPARTS(YEAR(OccurredAtUtc), MONTH(OccurredAtUtc), 1), ChainSeq, RowHash, EventId, SYSUTCDATETIME()\n" + - "FROM @purged WHERE ChainSeq IS NOT NULL AND RowHash IS NOT NULL;\n" + - ChainLockedBatchSuffix; + "DELETE TOP (@batch) FROM dbo.AuditLog WHERE Category = @channel AND OccurredAtUtc < @threshold;"; // AUDIT-PURGE-ALLOWED: per-channel retention override, maintenance path long totalDeleted = 0; @@ -1462,16 +1415,8 @@ VALUES // The trailing AUDIT-PURGE-ALLOWED marker on the UPDATE line below is the // single narrow exemption the append-only CI guard (AuditLogAppendOnlyGuardTests) // recognises for an UPDATE; any other UPDATE targeting AuditLog still trips the guard. - // - // Hash chain (ScadaBridge#41): SourceNode is inside every chained row's hash, so the - // backfill touches ONLY unchained rows (ChainSeq IS NULL) and runs under the - // exclusive chain lock — a chaining pass can never hash a row whose SourceNode this - // batch is about to change. Run the backfill BEFORE enabling AuditLog:HashChain; - // once a row is chained its SourceNode is frozen. const string updateBatchSql = - ChainLockedBatchPrefix + - "UPDATE TOP (@batch) dbo.AuditLog SET SourceNode = @sentinel WHERE SourceNode IS NULL AND OccurredAtUtc < @before AND ChainSeq IS NULL;" + // AUDIT-PURGE-ALLOWED: SourceNode sentinel backfill, maintenance path - ChainLockedBatchSuffix; + "UPDATE TOP (@batch) dbo.AuditLog SET SourceNode = @sentinel WHERE SourceNode IS NULL AND OccurredAtUtc < @before;"; // AUDIT-PURGE-ALLOWED: SourceNode sentinel backfill, maintenance path long totalUpdated = 0; diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ScadaBridgeDbContext.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ScadaBridgeDbContext.cs index 3cf8a506..d72c4b04 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ScadaBridgeDbContext.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ScadaBridgeDbContext.cs @@ -139,10 +139,6 @@ public class ScadaBridgeDbContext : DbContext, IDataProtectionKeyContext public DbSet AuditLogEntries => Set(); /// Gets the set of audit log rows (central dbo.AuditLog persistence shape; mapped to/from the canonical record at the repository boundary). public DbSet AuditLogs => Set(); - /// Gets the per-month AuditLog hash-chain heads (ScadaBridge#41). - public DbSet AuditLogChainHeads => Set(); - /// Gets the AuditLog hash-chain purge tombstones (ScadaBridge#41). - public DbSet AuditLogChainTombstones => Set(); /// Gets the set of site calls. public DbSet SiteCalls => Set(); diff --git a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ServiceCollectionExtensions.cs b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ServiceCollectionExtensions.cs index f07498d6..00addecd 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ServiceCollectionExtensions.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ServiceCollectionExtensions.cs @@ -20,14 +20,9 @@ public static class ServiceCollectionExtensions /// /// The service collection to register into. /// SQL Server connection string for the central configuration database. - /// - /// Optional separate connection string for the AuditLog maintenance paths — hash-chain job - /// and both purge paths (AuditLog:MaintenanceConnectionString, ScadaBridge#107 item 3). - /// Null or blank runs them on as before. - /// /// The same collection, for chaining. public static IServiceCollection AddConfigurationDatabase( - this IServiceCollection services, string connectionString, string? auditMaintenanceConnectionString = null) + this IServiceCollection services, string connectionString) { // The DbContext is constructed via the (options, IDataProtectionProvider) overload so // secret-bearing configuration columns are encrypted at rest. AddDataProtection below @@ -115,37 +110,7 @@ public static class ServiceCollectionExtensions // hosted service. Scoped because the implementation reuses the per-scope // ScadaBridgeDbContext for raw-SQL execution; the hosted service opens a // fresh scope on each tick (mirrors AuditLogPurgeActor / AuditLogIngestActor). - // Runs on the maintenance connection when AuditLog:MaintenanceConnectionString is set - // (ScadaBridge#113), else on the scope's own context as before. - services.AddScoped(sp => new AuditLogPartitionMaintenance( - sp.GetRequiredService().Context, - sp.GetService>())); - - // AuditLog maintenance connection (ScadaBridge#107 item 3): the chain job and both purge - // paths run on AuditMaintenanceContext — the scope's own context unless a separate - // maintenance login is configured, in which case an owned context on that string. - services.AddSingleton(new AuditMaintenanceConnection( - string.IsNullOrWhiteSpace(auditMaintenanceConnectionString) ? null : auditMaintenanceConnectionString)); - services.AddScoped(sp => - { - var maintenance = sp.GetRequiredService().ConnectionString; - return maintenance is null - ? AuditMaintenanceContext.Shared(sp.GetRequiredService()) - : AuditMaintenanceContext.Separate(maintenance, sp.GetService()); - }); - services.AddKeyedScoped(AuditMaintenanceContext.ServiceKey, (sp, _) => new AuditLogRepository( - sp.GetRequiredService().Context, - sp.GetService>())); - - // AuditLog hash-chain tamper evidence (ScadaBridge#41): chained by the central - // AuditLogHashChainService, verified by the VerifyAuditChain management command; the - // same instance reads/verifies the off-box head export (#107 item 1). Both on the - // maintenance connection. - services.AddScoped(sp => new AuditLogHashChain( - sp.GetRequiredService().Context, - sp.GetService>())); - services.AddScoped(sp => sp.GetRequiredService()); - services.AddScoped(sp => sp.GetRequiredService()); + services.AddScoped(); // AuditLog Parquet cold archive (ScadaBridge#42): written by the purge actor before a // partition switch, listed/verified by the ListAuditArchives / VerifyAuditArchive @@ -155,12 +120,6 @@ public static class ServiceCollectionExtensions sp.GetRequiredService(), sp.GetService>()?.Value ?? new AuditLogArchiveOptions(), sp.GetService>())); - // The purge actor's archive-then-purge runs on the maintenance connection (#107 item 3); - // list/verify from the management surface stay on the runtime connection (reads only). - services.AddKeyedScoped(AuditMaintenanceContext.ServiceKey, (sp, _) => new AuditLogPartitionArchive( - sp.GetRequiredService().Context, - sp.GetService>()?.Value ?? new AuditLogArchiveOptions(), - sp.GetService>())); // Opt-in config-audit retention (ScadaBridge:ConfigAudit:RetentionDays, default 0 = off). // Options binding to configuration is done by the host; without it the defaults apply diff --git a/src/ZB.MOM.WW.ScadaBridge.Host/Health/AuditChainHeadExportHealthCheck.cs b/src/ZB.MOM.WW.ScadaBridge.Host/Health/AuditChainHeadExportHealthCheck.cs deleted file mode 100644 index 172acbb1..00000000 --- a/src/ZB.MOM.WW.ScadaBridge.Host/Health/AuditChainHeadExportHealthCheck.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Microsoft.Extensions.Diagnostics.HealthChecks; -using ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; - -namespace ZB.MOM.WW.ScadaBridge.Host.Health; - -/// -/// /health/ready entry (ScadaBridge#115): Degraded while the off-box AuditLog chain-head export -/// (AuditLog:HashChain:HeadExport) is failing, or has had no success for more than two -/// intervals. Degraded maps to HTTP 200, so it shows on the Overview dashboard without taking -/// the node out of rotation. Healthy when the export is disabled. -/// -public sealed class AuditChainHeadExportHealthCheck : IHealthCheck -{ - private readonly AuditChainHeadExportStatus _status; - private readonly TimeProvider _time; - - /// Initializes the check. - /// The export status singleton. - /// Clock (defaults to the system clock). - public AuditChainHeadExportHealthCheck(AuditChainHeadExportStatus status, TimeProvider? time = null) - { - _status = status ?? throw new ArgumentNullException(nameof(status)); - _time = time ?? TimeProvider.System; - } - - /// - public Task CheckHealthAsync( - HealthCheckContext context, CancellationToken cancellationToken = default) - { - if (!_status.Enabled) - { - return Task.FromResult(HealthCheckResult.Healthy("Chain-head export disabled.")); - } - - var now = _time.GetUtcNow().UtcDateTime; - var data = new Dictionary(StringComparer.Ordinal) - { - ["totalFailures"] = _status.TotalFailures, - ["consecutiveFailures"] = _status.ConsecutiveFailures, - ["intervalMinutes"] = _status.IntervalMinutes, - }; - if (_status.LastSuccessUtc is { } ok) - { - data["lastSuccessUtc"] = ok; - } - - if (_status.ConsecutiveFailures > 0) - { - return Task.FromResult(HealthCheckResult.Degraded( - $"Chain-head export failing ({_status.ConsecutiveFailures} consecutive): {_status.LastError}", data: data)); - } - - var limit = TimeSpan.FromMinutes(2L * _status.IntervalMinutes); - var reference = _status.LastSuccessUtc ?? _status.StartedUtc; - if (reference is { } r && now - r > limit) - { - return Task.FromResult(HealthCheckResult.Degraded( - $"No successful chain-head export for more than 2 intervals (last {(_status.LastSuccessUtc is null ? "never" : _status.LastSuccessUtc.Value.ToString("O"))}).", data: data)); - } - - return Task.FromResult(HealthCheckResult.Healthy("Chain-head export OK.", data)); - } -} diff --git a/src/ZB.MOM.WW.ScadaBridge.Host/Program.cs b/src/ZB.MOM.WW.ScadaBridge.Host/Program.cs index 8bccaa19..a15f8ff0 100644 --- a/src/ZB.MOM.WW.ScadaBridge.Host/Program.cs +++ b/src/ZB.MOM.WW.ScadaBridge.Host/Program.cs @@ -541,12 +541,7 @@ try var configDbConnectionString = configuration["ScadaBridge:Database:ConfigurationDb"] ?? throw new InvalidOperationException("ScadaBridge:Database:ConfigurationDb connection string is required for Central role."); - // Optional second login for the AuditLog maintenance paths (chain job + both purge - // paths, ScadaBridge#107 item 3). Read from the pre-host-expanded configuration, so it - // may be a ${secret:} reference; unset = everything on the runtime connection, as before. - builder.Services.AddConfigurationDatabase( - configDbConnectionString, - configuration[ZB.MOM.WW.ScadaBridge.AuditLog.ServiceCollectionExtensions.MaintenanceConnectionStringKey]); + builder.Services.AddConfigurationDatabase(configDbConnectionString); builder.Services.AddOptions() .Bind(configuration.GetSection(ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance.ConfigAuditOptions.SectionName)) .ValidateOnStart(); @@ -589,12 +584,6 @@ try "bundle-signing-policy", failureStatus: null, tags: new[] { ZbHealthTags.Ready }) - // ScadaBridge#115: Degraded (HTTP 200) while the off-box chain-head export is failing - // or stale; healthy when the export is disabled. - .AddTypeActivatedCheck( - "audit-chain-head-export", - failureStatus: null, - tags: new[] { ZbHealthTags.Ready }) // Oldest-Up-member check (review 01 [High]), unscoped: every central member competes for // the one active slot. Since ZB.MOM.WW.Health 0.3.0 this IS the shared check — it selects // by age, so the host-local OldestNodeActiveHealthCheck that existed only to avoid the old diff --git a/src/ZB.MOM.WW.ScadaBridge.ManagementService/AuditEndpoints.cs b/src/ZB.MOM.WW.ScadaBridge.ManagementService/AuditEndpoints.cs index 21e4ec24..01c44dce 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ManagementService/AuditEndpoints.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ManagementService/AuditEndpoints.cs @@ -426,10 +426,7 @@ public static class AuditEndpoints statusCode: 400); } - // Maintenance login when AuditLog:MaintenanceConnectionString is set (ScadaBridge#113); - // otherwise the keyed registration resolves to the runtime connection's context. - var repo = context.RequestServices.GetKeyedService(AuditLogServiceKeys.Maintenance) - ?? context.RequestServices.GetRequiredService(); + var repo = context.RequestServices.GetRequiredService(); var rowsUpdated = await repo.BackfillSourceNodeAsync(sentinel, beforeUtc.Value, batchSize, context.RequestAborted); return Results.Json(new diff --git a/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementActor.cs b/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementActor.cs index 67a2c977..45cda871 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementActor.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementActor.cs @@ -246,7 +246,7 @@ public class ManagementActor : ReceiveActor // three Live run commands stay DeployerOnly. private static readonly string[] FlowDebuggerReplayRoles = AuthorizationPolicies.FlowDebuggerAccessRoles; - // ScadaBridge#41: verify-chain is an audit read — the OperationalAudit role set. + // AuditLog archive list/verify are audit reads — the OperationalAudit role set. private static readonly string[] OperationalAuditReaders = AuthorizationPolicies.OperationalAuditRoles; // arch-review C6: area management is reconciled to any-of [Designer, Deployer]. @@ -387,16 +387,8 @@ public class ManagementActor : ReceiveActor // (arch-review UA1). ListSecuredWritesCommand => SecuredWriteReaders, - // AuditLog hash-chain verification (ScadaBridge#41): an audit read, so the - // OperationalAudit roles (alog.md §15.1 — verify-chain carries audit query's - // permission). A site-scoped caller is refused in the handler: a month's chain - // spans every site. - VerifyAuditChainCommand => OperationalAuditReaders, - // Off-box chain-head check (ScadaBridge#107 item 1): the same audit read. - VerifyAuditChainHeadsCommand => OperationalAuditReaders, - - // AuditLog Parquet archives (ScadaBridge#42): audit reads with verify-chain's gate — - // OperationalAudit roles, site-scoped callers refused in the handler. + // AuditLog Parquet archives (ScadaBridge#42): audit reads — OperationalAudit roles + // (alog.md §15.1, audit query's permission), site-scoped callers refused in the handler. ListAuditArchivesCommand => OperationalAuditReaders, // Archive cleanup deletes archive files (ScadaBridge#109): Administrator only. CleanupAuditArchivesCommand => AdminOnly, @@ -572,8 +564,6 @@ public class ManagementActor : ReceiveActor // Audit Log QueryAuditLogCommand cmd => await HandleQueryAuditLog(sp, cmd), - VerifyAuditChainCommand cmd => await HandleVerifyAuditChain(sp, cmd, user), - VerifyAuditChainHeadsCommand cmd => await HandleVerifyAuditChainHeads(sp, cmd, user), ListAuditArchivesCommand => await HandleListAuditArchives(sp, user), VerifyAuditArchiveCommand cmd => await HandleVerifyAuditArchive(sp, cmd, user), CleanupAuditArchivesCommand cmd => await HandleCleanupAuditArchives(sp, cmd, user), @@ -3230,42 +3220,6 @@ public class ManagementActor : ReceiveActor return new AuditLogQueryResult(entries, totalCount); } - /// - /// Verifies one month of the AuditLog hash chain (ScadaBridge#41). The month must be - /// YYYY-MM; a site-scoped, non-Administrator caller is refused because the chain - /// covers every site's rows and a break report names rows by EventId. - /// - private static async Task HandleVerifyAuditChain( - IServiceProvider sp, VerifyAuditChainCommand cmd, AuthenticatedUser user) - { - if (string.IsNullOrWhiteSpace(cmd.Month) - || !DateTime.TryParseExact(cmd.Month, "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture, - System.Globalization.DateTimeStyles.None, out var month)) - { - throw new ManagementCommandException( - $"Invalid month '{cmd.Month}'. Expected YYYY-MM (e.g. 2026-05).", "INVALID_ARGUMENT"); - } - - if (user.PermittedSiteIds.Length > 0 - && !user.Roles.Contains(Roles.Administrator, StringComparer.OrdinalIgnoreCase)) - { - throw new SiteScopeViolationException( - "Access denied: the audit hash chain spans every site; verify-chain needs an " - + "OperationalAudit role that is not scoped to specific sites."); - } - - var chain = sp.GetRequiredService(); - try - { - return await chain.VerifyMonthAsync( - new DateTime(month.Year, month.Month, 1, 0, 0, 0, DateTimeKind.Utc)); - } - catch (TimeoutException ex) - { - throw new ManagementCommandException(ex.Message, "CHAIN_BUSY"); - } - } - /// /// Lists the AuditLog Parquet archives (ScadaBridge#42). Refused for a site-scoped, /// non-Administrator caller: an archive holds every site's rows. @@ -3276,51 +3230,8 @@ public class ManagementActor : ReceiveActor return new AuditArchiveListResult(archive.IsEnabled, await archive.ListAsync()); } - /// Upper bound on records per . - internal const int MaxChainHeadRecords = 50_000; - /// - /// Checks exported AuditLog chain heads against the database (ScadaBridge#107 item 1). Same - /// gate as verify-chain: a site-scoped, non-Administrator caller is refused because the - /// chain spans every site. - /// - private static async Task HandleVerifyAuditChainHeads( - IServiceProvider sp, VerifyAuditChainHeadsCommand cmd, AuthenticatedUser user) - { - if (cmd.Records is null || cmd.Records.Count == 0) - { - throw new ManagementCommandException("No exported chain-head records were supplied.", "INVALID_ARGUMENT"); - } - - if (cmd.Records.Count > MaxChainHeadRecords) - { - throw new ManagementCommandException( - $"{cmd.Records.Count} records exceed the {MaxChainHeadRecords} limit; verify fewer export files at a time.", - "INVALID_ARGUMENT"); - } - - if (user.PermittedSiteIds.Length > 0 - && !user.Roles.Contains(Roles.Administrator, StringComparer.OrdinalIgnoreCase)) - { - throw new SiteScopeViolationException( - "Access denied: the audit hash chain spans every site; verify-heads needs an " - + "OperationalAudit role that is not scoped to specific sites."); - } - - var store = sp.GetRequiredService(); - try - { - return await store.VerifyExportedHeadsAsync(cmd.Records); - } - catch (TimeoutException ex) - { - throw new ManagementCommandException(ex.Message, "CHAIN_BUSY"); - } - } - - /// - /// Verifies one AuditLog Parquet archive (ScadaBridge#42): files against the manifest, - /// the chain recomputed from the archived rows, the chain end against the purge anchor. + /// Verifies one AuditLog Parquet archive (ScadaBridge#42): the data file against its manifest. /// private static async Task HandleVerifyAuditArchive( IServiceProvider sp, VerifyAuditArchiveCommand cmd, AuthenticatedUser user) @@ -3341,16 +3252,12 @@ public class ManagementActor : ReceiveActor } /// - /// Runs the archive cleanup now (ScadaBridge#109), on the maintenance-connection archive when - /// registered. Dry run reports only. + /// Runs the archive cleanup now (ScadaBridge#109). Dry run reports only. /// private static async Task HandleCleanupAuditArchives( IServiceProvider sp, CleanupAuditArchivesCommand cmd, AuthenticatedUser user) { - RequireAuditArchive(sp, user); - var archive = sp.GetKeyedService( - ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Maintenance.AuditMaintenanceContext.ServiceKey) - ?? sp.GetRequiredService(); + var archive = RequireAuditArchive(sp, user); return await archive.CleanupAsync(cmd.DryRun); } diff --git a/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementEndpoints.cs b/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementEndpoints.cs index 69e0b1d9..1966898f 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementEndpoints.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ManagementService/ManagementEndpoints.cs @@ -60,13 +60,7 @@ public static class ManagementEndpoints // script entry (design 2026-09-08): a direct call lasts up to the script's timeout // (+ grace), a write kind up to its wait window. typeof(FlowDebugRunScriptCommand), - // AuditLog hash-chain verification (ScadaBridge#41) recomputes one hash per row of - // a whole month — millions of rows on a busy central. - typeof(VerifyAuditChainCommand), - // Off-box chain-head check (ScadaBridge#107): one link lookup per exported head, under - // the chain lock SHARED (waits behind a chaining pass). - typeof(VerifyAuditChainHeadsCommand), - // AuditLog archive verification (ScadaBridge#42) hashes and re-chains a whole month's + // AuditLog archive verification (ScadaBridge#42) hashes and reads a whole month's // Parquet archive. typeof(VerifyAuditArchiveCommand), // Archive cleanup proves stale archives redundant by reading Parquet files (ScadaBridge#109). diff --git a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/AuditLogHashChainServiceTests.cs b/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/AuditLogHashChainServiceTests.cs deleted file mode 100644 index 6b56c57e..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/AuditLogHashChainServiceTests.cs +++ /dev/null @@ -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; - -/// -/// Policy tests for (ScadaBridge#41) over a scripted -/// : 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 AuditLogHashChainTests in ConfigurationDatabase.Tests. -/// -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 ChainBacklogSeen { get; } = []; - - public Task 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 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(), - Options.Create(options), - NullLogger.Instance); - } - - /// The job is off by default and starting it then does nothing. - /// A task that represents the asynchronous operation. - [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); - } - - /// A tick drains batches until one comes back empty. - /// A task that represents the asynchronous operation. - [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 - } - - /// - /// ScadaBridge#139: backlog chaining is off by default (owner decision, #107) and the - /// option reaches every chaining pass when set. - /// - /// The configured value, or null to leave the default. - /// A task that represents the asynchronous operation. - [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)); - } - - /// The peer node holding the lock ends the tick at once. - /// A task that represents the asynchronous operation. - [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); - } - - /// A backlog yields after MaxBatchesPerTick batches. - /// A task that represents the asynchronous operation. - [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); - } - - /// A failing tick is contained (logged), never thrown out of the loop. - /// A task that represents the asynchronous operation. - [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)); - } - - /// Invalid tuning fails startup validation. - /// IntervalSeconds. - /// BatchSize. - /// MaxBatchesPerTick. - [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); - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/AuditLogPurgeActorTests.cs b/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/AuditLogPurgeActorTests.cs index b4bdb096..36414a40 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/AuditLogPurgeActorTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/AuditLogPurgeActorTests.cs @@ -713,9 +713,9 @@ public class AuditLogPurgeActorTests : TestKit, IClassFixture 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); /// /// With archiving enabled every expired partition is archived and then purged by the @@ -793,36 +793,6 @@ public class AuditLogPurgeActorTests : TestKit, IClassFixture - /// ScadaBridge#107 item 3: when the host registered the maintenance-connection repository - /// (keyed audit-maintenance), the purge runs on it, never on the runtime one. - /// - [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(_ => runtime); - services.AddKeyedScoped( - 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.Instance))); - - AwaitAssert( - () => Assert.NotEmpty(maintenance.SwitchedBoundaries), - duration: TimeSpan.FromSeconds(3), - interval: TimeSpan.FromMilliseconds(50)); - Assert.Empty(runtime.ThresholdQueries); - Assert.Empty(runtime.SwitchedBoundaries); - - StopPurgeActor(actor); - } - /// /// 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. diff --git a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/HeadExport/AuditChainHeadExportServiceTests.cs b/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/HeadExport/AuditChainHeadExportServiceTests.cs deleted file mode 100644 index eeda8baa..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/HeadExport/AuditChainHeadExportServiceTests.cs +++ /dev/null @@ -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; - -/// -/// (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. -/// -public sealed class AuditChainHeadExportServiceTests -{ - private sealed class FakeStore : IAuditChainHeadStore - { - public List Heads { get; } = []; - - public Task> ReadHeadsAsync(CancellationToken ct = default) - => Task.FromResult>(Heads.ToList()); - - public Task VerifyExportedHeadsAsync( - IReadOnlyList records, CancellationToken ct = default) => throw new NotSupportedException(); - } - - private sealed class RecordingSink : IAuditChainHeadSink - { - public List> Writes { get; } = []; - - public bool Fail { get; set; } - - public string Describe => "recording"; - - public Task WriteAsync(IReadOnlyList 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(_ => store); - var options = Options.Create(new AuditLogHashChainOptions - { - HeadExport = new AuditChainHeadExportOptions { Enabled = true, ExportUnchanged = exportUnchanged }, - }); - var service = new AuditChainHeadExportService( - services.BuildServiceProvider().GetRequiredService(), - options, sink, "central-a", NullLogger.Instance, time, status); - return (service, store, sink); - } - - /// First tick: every head; next tick: only the head that moved; unchanged: nothing. - /// A task that represents the asynchronous operation. - [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); - } - - /// ExportUnchanged re-exports every head every tick. - /// A task that represents the asynchronous operation. - [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)); - } - - /// A failed write is contained and the same heads go out on the next tick. - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// ScadaBridge#115: a failed tick bumps the failure counter metric and the status - /// (consecutive + total + last error); the next success clears the consecutive count. - /// - /// A task that represents the asynchronous operation. - [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((_, 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)); - } - - /// The validator ignores a disabled export and checks each sink's settings when enabled. - /// Which settings to apply. - /// A fragment of the expected failure, or null when valid. - [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); - } - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/HeadExport/AuditChainHeadSinkTests.cs b/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/HeadExport/AuditChainHeadSinkTests.cs deleted file mode 100644 index a882b8f0..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/HeadExport/AuditChainHeadSinkTests.cs +++ /dev/null @@ -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; - -/// -/// 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). -/// -public sealed class AuditChainHeadSinkTests : IDisposable -{ - private readonly DirectoryInfo _dir = Directory.CreateTempSubdirectory("head-sink-"); - - /// - 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; - } - - /// - /// Writes append to the node's file for the day — never truncate it — one JSON record per - /// line, and a pre-existing line survives. - /// - /// A task that represents the asynchronous operation. - [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(l, new JsonSerializerOptions(JsonSerializerDefaults.Web))!).ToList(); - Assert.Equal([3L, 7L, 9L], parsed.Select(r => r.LastSeq)); - Assert.Equal(Record("2026-05", 9), parsed[2]); - } - - /// A relative export directory is refused. - [Fact] - public void FileSink_RejectsARelativeDirectory() - { - Assert.Throws(() => new FileAuditChainHeadSink("relative/dir", "central-a")); - } - - /// UDP: one RFC 5424 datagram per record — PRI, version, MSGID, nil SD, JSON body. - /// A task that represents the asynchronous operation. - [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); - } - - /// TCP: octet-counted frames (RFC 6587), one per record, on one connection. - /// A task that represents the asynchronous operation. - [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(); - 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(json, new JsonSerializerOptions(JsonSerializerDefaults.Web))!; - Assert.Equal(seq, record.LastSeq); - Assert.Equal(new string('a', 64), record.LastHash); - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/RetiredAuditSettingsWarningTests.cs b/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/RetiredAuditSettingsWarningTests.cs new file mode 100644 index 00000000..11a8601a --- /dev/null +++ b/tests/ZB.MOM.WW.ScadaBridge.AuditLog.Tests/Central/RetiredAuditSettingsWarningTests.cs @@ -0,0 +1,42 @@ +using Microsoft.Extensions.Configuration; +using ZB.MOM.WW.ScadaBridge.AuditLog.Central; + +namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Central; + +/// +/// The hash chain and the maintenance login were removed on 2026-11-15; a node still carrying +/// their settings (DEV/QA have AuditLog__HashChain__Enabled=true in the Central service +/// env) must boot and be told the keys are ignored. +/// +public class RetiredAuditSettingsWarningTests +{ + private static IConfiguration Config(params (string Key, string Value)[] values) => + new ConfigurationBuilder() + .AddInMemoryCollection(values.Select(v => new KeyValuePair(v.Key, v.Value))) + .Build(); + + /// The env var left on DEV/QA (and the maintenance login) are both reported. + [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)); + } + + /// A clean configuration (and the archive settings that remain) reports nothing. + [Fact] + public void FindRetired_ReportsNothing_ForCurrentSettings() + { + var config = Config( + ("AuditLog:Archive:Enabled", "true"), + ("AuditLog:RetentionDays", "365"), + ("AuditLog:MaintenanceConnectionString", " ")); + + Assert.Empty(RetiredAuditSettingsWarning.FindRetired(config)); + } +} diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/CommandTreeTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/CommandTreeTests.cs index 0d17ebc8..e2017630 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/CommandTreeTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/CommandTreeTests.cs @@ -69,9 +69,9 @@ public class CommandTreeTests Assert.Contains("bundle", groupNames); } - /// Verifies that the audit command group exposes the query, export, verify-chain and archive subcommands. + /// Verifies that the audit command group exposes the query, export and archive subcommands, and no hash-chain verify (removed 2026-11-15). [Fact] - public void AuditCommandGroup_HasQueryExportAndVerifyChain() + public void AuditCommandGroup_HasQueryExportAndArchive() { // Pin the audit sub-command surface so a rename / accidental // removal of one of these is caught. @@ -79,8 +79,9 @@ public class CommandTreeTests var subNames = audit.Subcommands.Select(c => c.Name).ToHashSet(); Assert.Contains("query", subNames); Assert.Contains("export", subNames); - Assert.Contains("verify-chain", subNames); Assert.Contains("archive", subNames); + Assert.DoesNotContain("verify-chain", subNames); + Assert.DoesNotContain("verify-heads", subNames); } /// Verifies that the bundle command group exposes the export, preview and import subcommands. diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditArchiveCommandTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditArchiveCommandTests.cs index 0f283c6d..0f0ade51 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditArchiveCommandTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditArchiveCommandTests.cs @@ -18,17 +18,15 @@ public class AuditArchiveCommandTests private const string ListBody = "{\"enabled\":true,\"archives\":[{\"name\":\"" + Name + "\",\"partition\":\"2025-10\",\"status\":\"Purged\"," + - "\"rowCount\":11,\"chainedRowCount\":10,\"dataBytes\":4096}]}"; + "\"rowCount\":11,\"dataBytes\":4096}]}"; private const string VerifiedBody = "{\"name\":\"" + Name + "\",\"partition\":\"2025-10\",\"manifestStatus\":\"Purged\",\"verified\":true,\"filesIntact\":true," + - "\"rowsRead\":11,\"chains\":[{\"chain\":{\"partition\":\"2025-10\",\"status\":\"Intact\",\"rowsVerified\":10," + - "\"tombstonesApplied\":2,\"unchainedRows\":1},\"anchor\":\"Matches\",\"anchorDetail\":\"ends at the anchor\"}]}"; + "\"rowsRead\":11}"; private const string MismatchBody = - "{\"name\":\"" + Name + "\",\"partition\":\"2025-10\",\"manifestStatus\":\"Purged\",\"verified\":false,\"filesIntact\":true," + - "\"rowsRead\":11,\"chains\":[{\"chain\":{\"partition\":\"2025-10\",\"status\":\"Intact\",\"rowsVerified\":10," + - "\"tombstonesApplied\":0,\"unchainedRows\":0},\"anchor\":\"Mismatch\",\"anchorDetail\":\"hash differs\"}]}"; + "{\"name\":\"" + Name + "\",\"partition\":\"2025-10\",\"manifestStatus\":\"Purged\",\"verified\":false,\"filesIntact\":false," + + "\"fileProblem\":\"The data file holds 10 rows; the manifest records 11.\",\"rowsRead\":10}"; /// The audit group carries the archive group with list + verify + cleanup (ScadaBridge#109). [Fact] @@ -60,10 +58,10 @@ public class AuditArchiveCommandTests /// Text the report must contain. /// A task that represents the asynchronous operation. [Theory] - [InlineData(VerifiedBody, "table", 0, "anchor Matches")] + [InlineData(VerifiedBody, "table", 0, "Rows read : 11")] [InlineData(VerifiedBody, "json", 0, "\"verified\":true")] [InlineData(MismatchBody, "table", 3, "Verified : NO")] - [InlineData(MismatchBody, "json", 3, "\"anchor\":\"Mismatch\"")] + [InlineData(MismatchBody, "json", 3, "\"filesIntact\":false")] public async Task Verify_SendsVerifyAuditArchive_AndMapsVerdictToExitCode( string body, string format, int expectedExit, string expectedText) { diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditCommandsScaffoldTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditCommandsScaffoldTests.cs index 2bd680d7..bce9f1ad 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditCommandsScaffoldTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditCommandsScaffoldTests.cs @@ -27,14 +27,14 @@ public class AuditCommandsScaffoldTests Assert.False(string.IsNullOrWhiteSpace(audit.Description)); } - /// Verifies that the audit group exposes the query, export, tree, verify-chain, verify-heads, backfill-source-node and archive subcommands. + /// Verifies that the audit group exposes the query, export, tree, backfill-source-node and archive subcommands (verify-chain / verify-heads removed with the hash chain, 2026-11-15). [Fact] - public void Audit_HasSevenSubcommands_QueryExportTreeVerifyChainVerifyHeadsBackfillSourceNodeArchive() + public void Audit_HasFiveSubcommands_QueryExportTreeBackfillSourceNodeArchive() { var audit = BuildAudit(); var names = audit.Subcommands.Select(c => c.Name).OrderBy(n => n).ToArray(); Assert.Equal( - new[] { "archive", "backfill-source-node", "export", "query", "tree", "verify-chain", "verify-heads" }, + new[] { "archive", "backfill-source-node", "export", "query", "tree" }, names); } @@ -54,7 +54,7 @@ public class AuditCommandsScaffoldTests Assert.Contains("query", text); Assert.Contains("export", text); Assert.Contains("tree", text); - Assert.Contains("verify-chain", text); + Assert.DoesNotContain("verify-chain", text); Assert.Contains("backfill-source-node", text); } @@ -65,7 +65,7 @@ public class AuditCommandsScaffoldTests // `archive` is a group (list / verify, ScadaBridge#42; cleanup, ScadaBridge#109): check its leaves, not it. var audit = BuildAudit(); var leaves = audit.Subcommands.SelectMany(c => c.Subcommands.Count == 0 ? [c] : c.Subcommands).ToList(); - Assert.Equal(9, leaves.Count); // + verify-heads (ScadaBridge#107), archive cleanup (ScadaBridge#109) + Assert.Equal(7, leaves.Count); // query, export, tree, backfill-source-node, archive list|verify|cleanup Assert.All(leaves, sub => Assert.True(sub.Action != null, $"Leaf command '{sub.Name}' has no action.")); } diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditConfigDeprecationTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditConfigDeprecationTests.cs index c518255f..a63b9c3c 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditConfigDeprecationTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditConfigDeprecationTests.cs @@ -91,12 +91,12 @@ public class AuditConfigDeprecationTests } // The two groups are distinct objects with distinct subcommand sets: - // `audit` has query/export/verify-chain; `audit-config` has only query. + // `audit` has query/export/archive; `audit-config` has only query. var audit = root.Subcommands.Single(c => c.Name == "audit"); var auditConfig = root.Subcommands.Single(c => c.Name == "audit-config"); Assert.NotSame(audit, auditConfig); - Assert.Contains(audit.Subcommands, c => c.Name == "verify-chain"); - Assert.DoesNotContain(auditConfig.Subcommands, c => c.Name == "verify-chain"); + Assert.Contains(audit.Subcommands, c => c.Name == "archive"); + Assert.DoesNotContain(auditConfig.Subcommands, c => c.Name == "archive"); // `audit-config` carries the deprecated `audit-log` alias; `audit` does not. Assert.Contains(AuditLogCommands.DeprecatedAlias, auditConfig.Aliases); diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditVerifyChainCommandTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditVerifyChainCommandTests.cs deleted file mode 100644 index 0bb934c2..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditVerifyChainCommandTests.cs +++ /dev/null @@ -1,161 +0,0 @@ -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Text.Json; -using ZB.MOM.WW.ScadaBridge.CLI.Commands; - -namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands; - -/// -/// Tests for the scadabridge audit verify-chain subcommand (ScadaBridge#41). The -/// command sends the VerifyAuditChain management command for the month and maps -/// the verification status onto the exit code: 0 intact / not chained, 3 broken. -/// -[Collection("Console")] -public class AuditVerifyChainCommandTests -{ - private const string IntactBody = - "{\"partition\":\"2026-05\",\"status\":\"Intact\",\"rowsVerified\":42,\"tombstonesApplied\":1,\"headSeq\":43,\"unchainedRows\":0}"; - - private const string BrokenBody = - "{\"partition\":\"2026-05\",\"status\":\"Broken\",\"rowsVerified\":6,\"tombstonesApplied\":0,\"headSeq\":43,\"unchainedRows\":2," + - "\"firstBreak\":{\"partition\":\"2026-05\",\"chainSeq\":7,\"eventId\":\"11111111-2222-3333-4444-555555555555\",\"kind\":\"HashMismatch\",\"detail\":\"edited\"}}"; - - /// - /// CLI round trip: the request carries VerifyAuditChain + the month, and the - /// server's verdict drives the exit code and the report. - /// - /// The verification result the fake management endpoint serves. - /// Output format. - /// Expected exit code. - /// Text the report must contain. - /// A task that represents the asynchronous operation. - [Theory] - [InlineData(IntactBody, "table", 0, "Status : Intact")] - [InlineData(IntactBody, "json", 0, "\"status\":\"Intact\"")] - [InlineData(BrokenBody, "table", 3, "eventId : 11111111-2222-3333-4444-555555555555")] - [InlineData(BrokenBody, "json", 3, "\"chainSeq\":7")] - public async Task VerifyChain_SendsVerifyAuditChain_AndMapsVerdictToExitCode( - string body, string format, int expectedExit, string expectedText) - { - var listener = new TcpListener(IPAddress.Loopback, 0); - listener.Start(); - try - { - var port = ((IPEndPoint)listener.LocalEndpoint).Port; - string? requestBody = null; - var server = Task.Run(async () => - { - using var client = await listener.AcceptTcpClientAsync(); - await using var stream = client.GetStream(); - using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true); - var length = 0; - while (await reader.ReadLineAsync() is { Length: > 0 } line) - if (line.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase)) - length = int.Parse(line[15..].Trim()); - var buffer = new char[length]; - var read = 0; - while (read < length) - { - var count = await reader.ReadAsync(buffer.AsMemory(read)); - if (count == 0) throw new EndOfStreamException(); - read += count; - } - requestBody = new string(buffer); - var bytes = Encoding.UTF8.GetBytes(body); - var headers = Encoding.ASCII.GetBytes( - $"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {bytes.Length}\r\nConnection: close\r\n\r\n"); - await stream.WriteAsync(headers); - await stream.WriteAsync(bytes); - }); - - var root = AuditCommandTestHarness.BuildRoot(); - var (exit, output, _) = AuditCommandTestHarness.Invoke( - root, "audit", "verify-chain", "--month", "2026-05", - "--url", $"http://127.0.0.1:{port}", "--username", "u", "--password", "p", "--format", format); - await server.WaitAsync(TimeSpan.FromSeconds(5)); - - Assert.Equal(expectedExit, exit); - Assert.Contains(expectedText, output); - using var payload = JsonDocument.Parse(requestBody!); - Assert.Equal("VerifyAuditChain", payload.RootElement.GetProperty("command").GetString()); - var month = payload.RootElement.GetProperty("payload").EnumerateObject() - .First(p => string.Equals(p.Name, "month", StringComparison.OrdinalIgnoreCase)).Value.GetString(); - Assert.Equal("2026-05", month); - } - finally - { - listener.Stop(); - } - } - - /// Verifies that a malformed month value exits non-zero without contacting the server. - [Fact] - public void VerifyChain_MalformedMonth_ExitsNonZero() - { - var root = AuditCommandTestHarness.BuildRoot(); - var (exit, _, _) = AuditCommandTestHarness.Invoke( - root, "audit", "verify-chain", "--month", "2026-13"); - - Assert.NotEqual(0, exit); - } - - /// Verifies that omitting the month option produces the required-option error. - [Fact] - public void VerifyChain_MissingMonth_ProducesRequiredFlagError() - { - var root = AuditCommandTestHarness.BuildRoot(); - var (exit, _, err) = AuditCommandTestHarness.Invoke(root, "audit", "verify-chain"); - - Assert.NotEqual(0, exit); - Assert.Contains("--month", err); - } - - /// - /// ScadaBridge#139: the table report shows the chain cutoff and the pre-cutoff rows, and a - /// month whose backlog is out of scope still exits 0 (pre-cutoff rows are never a break). - /// - [Fact] - public void Render_Table_ShowsCutoffAndPreCutoffRows() - { - const string body = "{\"partition\":\"2026-11\",\"status\":\"Intact\",\"rowsVerified\":12,\"tombstonesApplied\":0," + - "\"purgedThroughSeq\":null,\"headSeq\":12,\"unchainedRows\":0,\"firstBreak\":null," + - "\"preCutoffRows\":3441,\"chainFromUtc\":\"2026-11-13T09:30:00.1234567Z\"}"; - using var output = new StringWriter(); - - var exit = AuditVerifyChainHelpers.Render(body, "table", output); - - Assert.Equal(0, exit); - Assert.Contains("Chain from : 2026-11-13T09:30:00.1234567Z", output.ToString()); - Assert.Contains("Pre-cutoff rows : 3441", output.ToString()); - } - - /// A legacy (whole-table) chain renders without a cutoff. - [Fact] - public void Render_Table_NoCutoff_SaysWholeTable() - { - const string body = "{\"partition\":\"2026-05\",\"status\":\"Intact\",\"rowsVerified\":1,\"unchainedRows\":0}"; - using var output = new StringWriter(); - - Assert.Equal(0, AuditVerifyChainHelpers.Render(body, "table", output)); - Assert.Contains("Chain from : - (whole table in scope)", output.ToString()); - Assert.Contains("Pre-cutoff rows : 0", output.ToString()); - } - - /// Verifies that month validation accepts only values in yyyy-MM format. - /// The month string to validate. - /// Whether the month is expected to be valid. - [Theory] - [InlineData("2026-05", true)] - [InlineData("2026-01", true)] - [InlineData("2026-12", true)] - [InlineData("2026-13", false)] - [InlineData("2026-00", false)] - [InlineData("2026-5", false)] - [InlineData("not-a-month", false)] - [InlineData("", false)] - public void IsValidMonth_ValidatesYyyyMm(string month, bool expected) - { - Assert.Equal(expected, AuditVerifyChainHelpers.IsValidMonth(month)); - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditVerifyHeadsCommandTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditVerifyHeadsCommandTests.cs deleted file mode 100644 index b9dbcff2..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditVerifyHeadsCommandTests.cs +++ /dev/null @@ -1,158 +0,0 @@ -using System.Net; -using System.Net.Sockets; -using System.Text; -using System.Text.Json; -using ZB.MOM.WW.ScadaBridge.CLI.Commands; - -namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands; - -/// -/// Tests for scadabridge audit verify-heads (ScadaBridge#107 item 1): reading off-box -/// exported chain heads (file-sink lines and syslog-collector lines), collapsing duplicates, -/// sending VerifyAuditChainHeads, and mapping the verdict to the exit code. -/// -[Collection("Console")] -public sealed class AuditVerifyHeadsCommandTests : IDisposable -{ - private readonly DirectoryInfo _dir = Directory.CreateTempSubdirectory("verify-heads-"); - - /// - public void Dispose() => _dir.Delete(recursive: true); - - private static string Line(string month, long seq, char hash, string exportedAt = "2026-05-02T00:00:00Z", string node = "central-a") => - "{\"v\":1,\"month\":\"" + month + "\",\"lastSeq\":" + seq + ",\"lastHash\":\"" + new string(hash, 64) - + "\",\"purgedThroughSeq\":null,\"purgedThroughHash\":null,\"chainedRows\":" + seq + ",\"tombstones\":0," - + "\"headUpdatedAtUtc\":\"2026-05-02T00:00:00Z\",\"exportedAtUtc\":\"" + exportedAt + "\",\"node\":\"" + node + "\"}"; - - /// - /// File-sink lines and RFC 5424 syslog lines both parse; junk is skipped; the same head - /// exported by both nodes (and re-exported) collapses to one record. - /// - [Fact] - public void ReadRecords_ParsesFileAndSyslogLines_SkipsJunk_AndCollapsesDuplicates() - { - Directory.CreateDirectory(Path.Combine(_dir.FullName, "2026")); - File.WriteAllLines(Path.Combine(_dir.FullName, "2026", "audit-chain-heads-2026-05-02-central-a.jsonl"), - [ - Line("2026-05", 10, 'a'), - Line("2026-05", 12, 'b'), - "not json at all", - "", - ]); - File.WriteAllLines(Path.Combine(_dir.FullName, "syslog.log"), - [ - "Nov 10 03:00:00 collector <109>1 2026-05-02T00:00:00.000Z central-b scadabridge 42 chainhead - " - + Line("2026-05", 12, 'b', "2026-05-02T01:00:00Z", "central-b"), - "<109>1 2026-05-02T00:00:00.000Z central-b scadabridge 42 chainhead - " + Line("2026-04", 7, 'c'), - ]); - File.WriteAllText(Path.Combine(_dir.FullName, "ignored.csv"), Line("2026-03", 1, 'd')); - - var read = AuditVerifyHeadsHelpers.ReadRecords(_dir.FullName); - - Assert.Equal(2, read.Files); - Assert.Equal(4, read.LinesRead); - Assert.Equal(1, read.LinesSkipped); - Assert.Equal(["2026-04/7", "2026-05/10", "2026-05/12"], read.Records.Select(r => $"{r.Month}/{r.LastSeq}")); - // The duplicate kept is the latest export. - Assert.Equal("central-b", read.Records.Single(r => r.LastSeq == 12).Node); - } - - /// A record missing its hash or sequence is not a record. - /// The line. - [Theory] - [InlineData("{\"v\":1,\"month\":\"2026-05\",\"lastSeq\":0,\"lastHash\":\"aa\"}")] - [InlineData("{\"v\":1,\"month\":\"2026-05\",\"lastSeq\":3}")] - [InlineData("{\"month\":\"2026-05\",\"lastSeq\":3,\"lastHash\":\"aa\"}")] - [InlineData("{ broken")] - public void TryParseLine_RejectsIncompleteRecords(string line) - { - Assert.Null(AuditVerifyHeadsHelpers.TryParseLine(line)); - } - - /// - /// Round trip: the request carries VerifyAuditChainHeads with the distinct records, - /// and the verdict drives the exit code (0 verified, 3 not). - /// - /// Whether the fake server reports verified. - /// Output format. - /// Expected exit code. - /// Text the report must contain. - /// A task that represents the asynchronous operation. - [Theory] - [InlineData(true, "table", 0, "Verified : yes")] - [InlineData(false, "table", 3, "Mismatch")] - [InlineData(false, "json", 3, "\"verified\":false")] - public async Task VerifyHeads_SendsTheRecords_AndMapsVerdictToExitCode(bool verified, string format, int expectedExit, string expectedText) - { - var file = Path.Combine(_dir.FullName, "heads.jsonl"); - File.WriteAllLines(file, [Line("2026-05", 12, 'b'), Line("2026-05", 12, 'b', "2026-05-02T02:00:00Z")]); - var body = "{\"verified\":" + (verified ? "true" : "false") + ",\"recordsChecked\":1,\"checks\":[{\"month\":\"2026-05\"," - + "\"exportedSeq\":12,\"exportedAtUtc\":\"2026-05-02T02:00:00Z\",\"node\":\"central-a\",\"status\":\"" - + (verified ? "Matches" : "Mismatch") + "\",\"databaseSeq\":12,\"detail\":\"d\"}]}"; - - var listener = new TcpListener(IPAddress.Loopback, 0); - listener.Start(); - try - { - var port = ((IPEndPoint)listener.LocalEndpoint).Port; - string? requestBody = null; - var server = Task.Run(async () => - { - using var client = await listener.AcceptTcpClientAsync(); - await using var stream = client.GetStream(); - using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true); - var length = 0; - while (await reader.ReadLineAsync() is { Length: > 0 } line) - if (line.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase)) - length = int.Parse(line[15..].Trim()); - var buffer = new char[length]; - var read = 0; - while (read < length) - { - var count = await reader.ReadAsync(buffer.AsMemory(read)); - if (count == 0) throw new EndOfStreamException(); - read += count; - } - requestBody = new string(buffer); - var bytes = Encoding.UTF8.GetBytes(body); - var headers = Encoding.ASCII.GetBytes( - $"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {bytes.Length}\r\nConnection: close\r\n\r\n"); - await stream.WriteAsync(headers); - await stream.WriteAsync(bytes); - }); - - var root = AuditCommandTestHarness.BuildRoot(); - var (exit, output, _) = AuditCommandTestHarness.Invoke( - root, "audit", "verify-heads", "--path", file, - "--url", $"http://127.0.0.1:{port}", "--username", "u", "--password", "p", "--format", format); - await server.WaitAsync(TimeSpan.FromSeconds(5)); - - Assert.Equal(expectedExit, exit); - Assert.Contains(expectedText, output); - using var payload = JsonDocument.Parse(requestBody!); - Assert.Equal("VerifyAuditChainHeads", payload.RootElement.GetProperty("command").GetString()); - var records = payload.RootElement.GetProperty("payload").EnumerateObject() - .First(p => string.Equals(p.Name, "records", StringComparison.OrdinalIgnoreCase)).Value; - Assert.Equal(1, records.GetArrayLength()); - Assert.Equal(new string('b', 64), records[0].EnumerateObject() - .First(p => string.Equals(p.Name, "lastHash", StringComparison.OrdinalIgnoreCase)).Value.GetString()); - } - finally - { - listener.Stop(); - } - } - - /// No records found is an input error, and nothing is sent. - [Fact] - public void VerifyHeads_NoRecords_ExitsOneWithoutContactingTheServer() - { - var file = Path.Combine(_dir.FullName, "empty.jsonl"); - File.WriteAllText(file, "nothing here\n"); - var root = AuditCommandTestHarness.BuildRoot(); - var (exit, _, _) = AuditCommandTestHarness.Invoke( - root, "audit", "verify-heads", "--path", file, "--url", "http://127.0.0.1:1", "--username", "u", "--password", "p"); - - Assert.Equal(1, exit); - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/ManagementSurfaceParityTests.cs b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/ManagementSurfaceParityTests.cs index 53b617d9..03e7fb23 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/ManagementSurfaceParityTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/ManagementSurfaceParityTests.cs @@ -45,7 +45,7 @@ public class ManagementSurfaceParityTests } private static readonly HashSet AuditManagementLeaves = - ["audit verify-chain", "audit verify-heads", "audit archive list", "audit archive verify", "audit archive cleanup"]; + ["audit archive list", "audit archive verify", "audit archive cleanup"]; private static async Task> ProbeProductionTree(string? omitGroup = null) { @@ -56,12 +56,6 @@ public class ManagementSurfaceParityTests await File.WriteAllTextAsync(file, "{}"); var csv = Path.Combine(directory.FullName, "native.csv"); await File.WriteAllTextAsync(csv, "SourceName,Connection,SourceReference,Filter\nSource,Connection,Path,\n"); - // `audit verify-heads` (ScadaBridge#107) reads exported chain heads before it sends anything. - HeadsFile = Path.Combine(directory.FullName, "heads.jsonl"); - await File.WriteAllTextAsync(HeadsFile, - "{\"v\":1,\"month\":\"2026-05\",\"lastSeq\":3,\"lastHash\":\"" + new string('a', 64) - + "\",\"purgedThroughSeq\":null,\"purgedThroughHash\":null,\"chainedRows\":3,\"tombstones\":0," - + "\"headUpdatedAtUtc\":\"2026-05-02T00:00:00Z\",\"exportedAtUtc\":\"2026-05-02T00:00:00Z\",\"node\":\"central-a\"}\n"); using var stop = new CancellationTokenSource(TimeSpan.FromSeconds(30)); var listener = new TcpListener(IPAddress.Loopback, 0); listener.Start(); @@ -102,10 +96,9 @@ public class ManagementSurfaceParityTests foreach (var (leaf, path) in Leaves(root, [])) { // Separate REST and persistent SignalR surfaces, not management contracts. - // `audit verify-chain` (VerifyAuditChain, ScadaBridge#41), `audit verify-heads` - // (VerifyAuditChainHeads, ScadaBridge#107) and `audit archive - // list|verify` (ListAuditArchives / VerifyAuditArchive, ScadaBridge#42) are the - // audit leaves that ARE management contracts, so they stay in the probe. + // `audit archive list|verify|cleanup` (ListAuditArchives / VerifyAuditArchive / + // CleanupAuditArchives, ScadaBridge#42/#109) are the audit leaves that ARE + // management contracts, so they stay in the probe. if (path.Length == 0 || (path[0] == "audit" && !AuditManagementLeaves.Contains(string.Join(' ', path))) || path[0] == omitGroup || string.Join(' ', path) == "debug stream") continue; @@ -142,11 +135,8 @@ public class ManagementSurfaceParityTests foreach (var entry in Leaves(child, [.. path, child.Name])) yield return entry; } - private static string HeadsFile = ""; - private static string ValueFor(Option option, string path, string file, string csv) { - if (path == "audit verify-heads" && option.Name == "--path") return HeadsFile; var type = option.GetType().GetGenericArguments().FirstOrDefault(); type = Nullable.GetUnderlyingType(type ?? typeof(string)) ?? type; if (type == typeof(int) || type == typeof(long) || type == typeof(double)) return "1"; diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/AuditLogAppendOnlyGuardTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/AuditLogAppendOnlyGuardTests.cs index 3136c893..1710a5b7 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/AuditLogAppendOnlyGuardTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/AuditLogAppendOnlyGuardTests.cs @@ -47,11 +47,6 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests; /// one-time ops procedure; the append-only invariant still applies to all /// other columns and all other UPDATE forms. /// -/// -/// AuditLogHashChain.WriteLinksAsync (ScadaBridge#41): the chaining job's -/// UPDATE dbo.AuditLog SET ChainSeq, RowHash, guarded by -/// ChainSeq IS NULL — it only ever fills the two hash-chain link columns once. -/// /// /// The allow-list is applied in the file-scan test only /// () — the diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Configurations/AuditLogEntityTypeConfigurationTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Configurations/AuditLogEntityTypeConfigurationTests.cs index 2270842d..b6e400ff 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Configurations/AuditLogEntityTypeConfigurationTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Configurations/AuditLogEntityTypeConfigurationTests.cs @@ -105,9 +105,9 @@ public class AuditLogEntityTypeConfigurationTests : IDisposable // C5: 10 canonical columns (EventId, OccurredAtUtc, Actor, Action, Outcome, // Channel[=Category], Target, SourceNode, CorrelationId, DetailsJson) + 6 // persisted computed columns (Kind, Status, SourceSiteId, ExecutionId, - // ParentExecutionId, IngestedAtUtc) + 2 hash-chain link columns (ChainSeq, - // RowHash — ScadaBridge#41) = 18. - Assert.Equal(18, properties.Count); + // ParentExecutionId, IngestedAtUtc) = 16. (The two hash-chain link columns were + // removed with the hash chain on 2026-11-15.) + Assert.Equal(16, properties.Count); } /// Verifies that the channel property maps to the canonical category column. @@ -186,7 +186,6 @@ public class AuditLogEntityTypeConfigurationTests : IDisposable var expected = new[] { "IX_AuditLog_Actor_Occurred", - "IX_AuditLog_ChainSeq", // hash chain (ScadaBridge#41) "IX_AuditLog_Channel_Status_Occurred", "IX_AuditLog_CorrelationId", "IX_AuditLog_Execution", @@ -196,7 +195,6 @@ public class AuditLogEntityTypeConfigurationTests : IDisposable "IX_AuditLog_ParentExecution", "IX_AuditLog_Site_Occurred", "IX_AuditLog_Target_Occurred", - "IX_AuditLog_Unchained", // hash chain (ScadaBridge#41) "UX_AuditLog_EventId_Occurred", // FlipAuditLogClusteredKey (ScadaBridge#111) // UX_AuditLog_EventId (single-column, non-aligned) is intentionally absent — // dropped by AlignAuditLogEventIdUniqueness. diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditArchiveParquetTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditArchiveParquetTests.cs index d95880b2..3a2a2939 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditArchiveParquetTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditArchiveParquetTests.cs @@ -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; /// -/// 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 ChainSeq/RowHash columns — +/// still reads. /// 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]); } - /// Tombstones round-trip exactly. - /// A task that represents the asynchronous operation. + /// The v2 data file carries the ten stored columns and nothing from the removed hash chain. [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(); - 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 } /// - /// The rows read back from an archive re-verify against the archived head — the same - /// check audit archive verify makes long after the database dropped them. + /// A format-v1 archive data file (the v2 columns plus ChainSeq/RowHash) reads + /// with the v2 reader: the extra columns are ignored and every stored column is intact. /// /// A task that represents the asynchronous operation. [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()); - 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)); } - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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()); - 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 Rows, AuditChainHeadState Head) BuildChain(int count) - { - var rows = new List(); - 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 ?? "", r.Action, r.Outcome, r.Category, - r.Target ?? "", r.SourceNode ?? "", r.CorrelationId?.ToString() ?? "", r.DetailsJson ?? "", - r.ChainSeq?.ToString() ?? "", r.RowHash is null ? "" : Convert.ToHexString(r.RowHash)); + r.Target ?? "", r.SourceNode ?? "", r.CorrelationId?.ToString() ?? "", r.DetailsJson ?? ""); - private static async Task WriteAsync(IEnumerable rows, int rowGroupSize) + internal static async Task WriteAsync(IEnumerable 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> ReadAsync(byte[] bytes) + /// Writes rows in the format-v1 layout: the v2 columns, then the two chain columns. + private static async Task WriteV1Async(IReadOnlyList rows) { - var rows = new List(); + var v2 = AuditArchiveParquet.DataSchema.DataFields; + var chainSeq = new DataField("ChainSeq", true); + var rowHash = new DataField("RowHash", true); + var schema = new ParquetSchema(v2.Cast().Append(chainSeq).Append(rowHash).ToArray()); + + var stream = new MemoryStream(); + await using (var writer = await ParquetWriter.CreateAsync(schema, stream)) + { + writer.CustomMetadata = new Dictionary + { + [AuditArchiveParquet.FormatVersionKey] = "1", + [AuditArchiveParquet.PartitionKey] = "2025-10", + }; + using var group = writer.CreateRowGroup(); + await group.WriteAsync((DataField)v2[0], rows.Select(r => r.EventId.ToString("D")).ToArray()); + await group.WriteAsync((DateTimeDataField)v2[1], rows.Select(r => r.OccurredAtUtc).ToArray()); + await group.WriteAsync((DataField)v2[2], rows.Select(r => r.OccurredAtUtc.Ticks).ToArray()); + await group.WriteAsync((DataField)v2[3], rows.Select(r => r.Actor).ToArray()!); + await group.WriteAsync((DataField)v2[4], rows.Select(r => r.Action).ToArray()); + await group.WriteAsync((DataField)v2[5], rows.Select(r => r.Outcome).ToArray()); + await group.WriteAsync((DataField)v2[6], rows.Select(r => r.Category).ToArray()); + await group.WriteAsync((DataField)v2[7], rows.Select(r => r.Target).ToArray()!); + await group.WriteAsync((DataField)v2[8], rows.Select(r => r.SourceNode).ToArray()!); + await group.WriteAsync((DataField)v2[9], rows.Select(r => r.CorrelationId?.ToString("D")).ToArray()!); + await group.WriteAsync((DataField)v2[10], rows.Select(r => r.DetailsJson).ToArray()!); + await group.WriteAsync(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> ReadAsync(byte[] bytes) + { + var rows = new List(); await foreach (var row in AuditArchiveParquet.ReadRowsAsync(new MemoryStream(bytes))) { rows.Add(row); diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainHeadComparerTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainHeadComparerTests.cs deleted file mode 100644 index 78f7a7ad..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainHeadComparerTests.cs +++ /dev/null @@ -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; - -/// -/// Pure verdict table of (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. -/// -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> Link(byte[]? row, byte[]? tombstone) - => _ => Task.FromResult(new AuditChainHeadComparer.StoredLink(row, tombstone)); - - private static Func> NoLookup - => _ => throw new InvalidOperationException("no link lookup expected"); - - /// The exported link was later purged per channel: the tombstone carries the hash. - /// A task that represents the asynchronous operation. - [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); - } - - /// The exported head is the partition-purge anchor: compared with the anchor's hash, no lookup. - /// A task that represents the asynchronous operation. - [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); - } - - /// A record whose hash is not 32 bytes of hex can never match. - /// The exported hash text. - /// A task that represents the asynchronous operation. - [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); - } - - /// Only the tamper verdicts count as failures. - /// The verdict. - /// Whether it is a failure. - [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); - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainHeadStoreTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainHeadStoreTests.cs deleted file mode 100644 index 0ccbd588..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainHeadStoreTests.cs +++ /dev/null @@ -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; - -/// -/// 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 -/// verify-chain accepts — is caught because the exported hash no longer matches. -/// -/// Each test owns one month (partition) of the shared fixture database. -public sealed class AuditChainHeadStoreTests : IClassFixture -{ - private readonly MsSqlMigrationFixture _fixture; - - /// Creates the test class with the shared migrated SQL Server fixture. - /// The shared migration fixture. - public AuditChainHeadStoreTests(MsSqlMigrationFixture fixture) => _fixture = fixture; - - /// The head snapshot carries the chain end and the live row + tombstone counts. - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// Unchanged chain: Matches. The chain grew: Extended. Both centrals' duplicate exports - /// collapse to one check. - /// - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// The forgery: a chained row's content is rewritten and every link from it plus the head - /// are recomputed with the real hasher. VerifyMonthAsync reports the forged chain - /// Intact (positive control: the in-database chain alone cannot see it); the exported head - /// does not match it. - /// - /// A task that represents the asynchronous operation. - [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); - } - - /// Each structural tampering maps to its own verdict. - /// A task that represents the asynchronous operation. - [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"]); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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 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 VerifyAsync(params AuditChainHeadRecord[] records) - { - await using var ctx = CreateContext(); - return await new AuditLogHashChain(ctx).VerifyExportedHeadsAsync(records); - } - - /// - /// Rewrites the row at and re-links it and every later row plus the - /// head with the production hasher — what a principal with full write access could do. - /// - 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(); - 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 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().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()); -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainVerifierTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainVerifierTests.cs deleted file mode 100644 index 70538480..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditChainVerifierTests.cs +++ /dev/null @@ -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; - -/// -/// 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. -/// -public class AuditChainVerifierTests -{ - private static readonly DateTime Month = new(2026, 5, 1, 0, 0, 0, DateTimeKind.Utc); - - /// An untouched chain verifies intact and covers every row. - [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); - } - - /// Editing any stored column of a chained row is a HashMismatch at that row. - [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); - } - - /// A row deleted from the middle (no tombstone) is a MissingRow at its sequence number. - [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 - } - - /// Deleting the newest rows leaves a self-consistent prefix; the head catches it. - [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); - } - - /// Swapping two rows' sequence numbers is a HashMismatch at the first moved row. - [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); - } - - /// A duplicated sequence number is reported, not silently skipped. - [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); - } - - /// A sanctioned per-channel purge leaves a tombstone that bridges the gap. - [Fact] - public void TombstonedRow_BridgesTheGap() - { - var (rows, head) = BuildChain(5); - var purged = rows[2]; - rows.RemoveAt(2); - var tombstones = new Dictionary { [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); - } - - /// A tombstone with the wrong hash does not hide a deletion. - [Fact] - public void ForgedTombstone_IsDetectedAtTheNextRow() - { - var (rows, head) = BuildChain(5); - var purged = rows[2]; - rows.RemoveAt(2); - var tombstones = new Dictionary { [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); - } - - /// After a partition purge, verification resumes at the checkpoint anchor. - [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); - } - - /// A month with neither rows nor head reports NotChained. - [Fact] - public void EmptyMonth_IsNotChained() - { - var result = Verify([], null, unchained: 7); - - Assert.Equal(AuditChainStatus.NotChained, result.Status); - Assert.Equal(7, result.UnchainedRows); - } - - /// - /// 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. - /// - [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)); - } - - /// NULL and the empty string hash differently (presence byte), as do adjacent-field shifts. - [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 rows, - AuditChainHeadState? head, - IReadOnlyDictionary? tombstones = null, - long unchained = 0) - { - var verifier = new AuditChainVerifier(Month, head, tombstones ?? new Dictionary()); - 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 - - /// A cutoff-started month links its first row to the anchor and verifies intact. - [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); - } - - /// Without its anchor, a cutoff-started month fails at its first link. - [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); - } - - /// A different cutoff is a different anchor: the anchor hash binds the instant. - [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); - } - - /// - /// The eligibility attack: a chained row re-dated before the cutoff is a - /// ChainedBeforeCutoff break even when its hash were recomputed to match. - /// - [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); - } - - /// An anchor the caller found invalid fails the month even when every link verifies. - [Fact] - public void InvalidAnchor_IsCutoffAnchorInvalid() - { - var anchor = AuditChainHasher.ComputeCutoffAnchorHash(CutoffAt); - var (rows, head) = BuildChain(4, anchor); - var tombstones = new Dictionary { [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); - } - - /// - /// 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. - /// - [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 rows, AuditChainHeadState head, byte[] anchor) - { - var tombstones = new Dictionary { [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 Rows, AuditChainHeadState Head) BuildChain(int count) - => BuildChain(count, AuditChainHasher.GenesisHash.ToArray()); - - private static (List Rows, AuditChainHeadState Head) BuildChain(int count, byte[] genesis) - { - var rows = new List(); - 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}}}"); -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogArchiveCleanupTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogArchiveCleanupTests.cs index 66bb58dd..f7e3ddd1 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogArchiveCleanupTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogArchiveCleanupTests.cs @@ -31,9 +31,8 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture _root.Delete(recursive: true); /// - /// 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. /// /// A task that represents the asynchronous operation. [SkippableFact] @@ -42,14 +41,11 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture 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(() => Archive(ctx, dir).PurgeArchivedPartitionAsync(stale)); @@ -83,8 +79,8 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture - /// 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. /// /// A task that represents the asynchronous operation. [SkippableFact] @@ -94,8 +90,7 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture 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 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 : IClassFixtureDeletion uses the archive name's own three files — never a path from the manifest. + /// Deletion uses the archive name's own files — never a path from the manifest. /// A task that represents the asynchronous operation. [SkippableFact] public async Task Deletion_NeverFollowsAManifestPath_OutOfTheArchiveFolder() @@ -199,7 +198,6 @@ public sealed class AuditLogArchiveCleanupTests : IClassFixture(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 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 new(year, month, 1, 0, 0, 0, DateTimeKind.Utc); private ScadaBridgeDbContext CreateContext() diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogHashChainCutoffTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogHashChainCutoffTests.cs deleted file mode 100644 index a284792b..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogHashChainCutoffTests.cs +++ /dev/null @@ -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; - -/// -/// 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. -/// -/// -/// The cutoff is one row per database, so every test starts from an empty AuditLog and empty -/// chain tables (). xunit runs a class's tests sequentially. -/// -public sealed class AuditLogHashChainCutoffTests : IClassFixture, 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-"); - - /// Creates the test class with its own migrated SQL Server database. - /// The per-class migration fixture. - public AuditLogHashChainCutoffTests(MsSqlMigrationFixture fixture) => _fixture = fixture; - - /// - public void Dispose() => _archiveRoot.Delete(recursive: true); - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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("SELECT COUNT_BIG(*) FROM dbo.AuditLog WHERE ChainSeq IS NOT NULL;")); - Assert.Equal(0L, await ScalarAsync("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($"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); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// 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 - /// (), and with its link cleared too - /// (its sequence number goes missing). Moving it into an earlier month is caught in both months. - /// - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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($"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); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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("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("SELECT SYSUTCDATETIME();"); - - Assert.Equal(1L, await ScalarAsync("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("SELECT COUNT_BIG(*) FROM dbo.AuditLog WHERE ChainSeq IS NOT NULL;")); - Assert.Equal(1L, await ScalarAsync("SELECT COUNT_BIG(*) FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;")); - var verified = await VerifyAsync(AuditChainHasher.MonthOf(cutoff)); - Assert.Equal(cutoff, verified.ChainFromUtc); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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("SELECT COUNT_BIG(*) FROM dbo.AuditLogChainTombstone WHERE ChainSeq = 0;")); - Assert.Equal(3L, await ScalarAsync("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("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); - } - - /// - /// The hardened maintenance login (scadabridge_audit_chainer + scadabridge_audit_purger, - /// ScadaBridge#107 item 3) can write the chain-start anchor: no extra grant, no migration. - /// - /// A task that represents the asynchronous operation. - [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().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 ────────────────────────────────────────────────────────────── - - /// - /// Backlog in February and early March, first enablement at , then - /// rows after it, all chained. Returns the post-cutoff EventIds in chain order. - /// - private async Task> 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 VerifyAsync(DateTime month) - { - await using var ctx = CreateContext(); - return await new AuditLogHashChain(ctx).VerifyMonthAsync(month); - } - - private async Task AnchorCutoffAsync() - => DateTime.SpecifyKind( - await ScalarAsync("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 SeqOfAsync(Guid eventId) - => await ScalarAsync($"SELECT ChainSeq FROM dbo.AuditLog WHERE EventId = '{eventId}';"); - - private async Task InsertAsync(IEnumerable 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 ScalarAsync(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().UseSqlServer(_fixture.ConnectionString).Options); - - private static List 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(); -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogHashChainTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogHashChainTests.cs deleted file mode 100644 index 57e0ff76..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogHashChainTests.cs +++ /dev/null @@ -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; - -/// -/// 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. -/// -/// -/// 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. -/// -public class AuditLogHashChainTests : IClassFixture -{ - private readonly MsSqlMigrationFixture _fixture; - - /// Creates the test class with the shared migrated SQL Server fixture. - /// The shared migration fixture. - public AuditLogHashChainTests(MsSqlMigrationFixture fixture) => _fixture = fixture; - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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); - } - - /// Editing a chained row's content is reported at that row. - /// A task that represents the asynchronous operation. - [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); - } - - /// Deleting a chained row outside the purge path is reported as a missing sequence number. - /// A task that represents the asynchronous operation. - [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); - } - - /// Swapping two rows' chain positions is reported. - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [SkippableFact] - public async Task PerChannelPurge_LeavesAVerifiableChain() - { - Skip.IfNot(_fixture.Available, _fixture.SkipReason); - var month = Month(2026, 6); - var rows = new List(); - 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); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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); - } - - /// - /// The SourceNode backfill (the other sanctioned AuditLog UPDATE) must not touch a chained - /// row — SourceNode is inside its hash. - /// - /// A task that represents the asynchronous operation. - [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($"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> 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 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 VerifyAsync(DateTime month) - { - await using var ctx = CreateContext(); - return await new AuditLogHashChain(ctx).VerifyMonthAsync(month); - } - - private async Task SeqOfAsync(Guid eventId) - => await ScalarAsync($"SELECT ChainSeq FROM dbo.AuditLog WHERE EventId = '{eventId}';"); - - private async Task> 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(); - 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 ScalarAsync(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().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"); -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogPartitionArchiveTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogPartitionArchiveTests.cs index 77fb1c61..b7533a11 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogPartitionArchiveTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditLogPartitionArchiveTests.cs @@ -13,13 +13,12 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Maintenance; /// /// 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. /// /// -/// 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. /// public sealed class AuditLogPartitionArchiveTests : IClassFixture, IDisposable { @@ -34,42 +33,37 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture _root.Delete(recursive: true); /// - /// 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. /// /// A task that represents the asynchronous operation. [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 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 - /// 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). /// /// A task that represents the asynchronous operation. [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(() => Archive(ctx).PurgeArchivedPartitionAsync(manifest)); - Assert.Equal(AuditPartitionSwitchGuard.ChainAdvancedError, ((SqlException)ex.InnerException!).Number); - } - - Assert.Equal(5, (await PartitionRowsAsync(month)).Count); - } - - /// - /// 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). - /// - /// A task that represents the asynchronous operation. - [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 - /// 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. /// /// A task that represents the asynchronous operation. [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); + /// + /// A format-v1 manifest (written while the hash chain existed, with chain and tombstone + /// fields) is still listed and verified. + /// + /// A task that represents the asynchronous operation. + [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); } /// A name that is not an archive name (e.g. a path) is rejected before any file access. @@ -308,16 +268,10 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture VerifyMonthAsync(DateTime month) - { - await using var ctx = CreateContext(); - return await new AuditLogHashChain(ctx).VerifyMonthAsync(month); - } - - private async Task> ReadArchiveRowsAsync(AuditArchiveManifest manifest, byte[]? bytes = null) + private async Task> ReadArchiveRowsAsync(AuditArchiveManifest manifest, byte[]? bytes = null) { bytes ??= await File.ReadAllBytesAsync(Path.Combine(_root.FullName, manifest.Partition[..4], manifest.DataFile)); - var rows = new List(); + var rows = new List(); await foreach (var row in AuditArchiveParquet.ReadRowsAsync(new MemoryStream(bytes))) { rows.Add(row); @@ -327,7 +281,7 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture rows, AuditArchiveManifest manifest, string dataPath, string manifestPath, string? lastHash) + List 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> PartitionRowsAsync(DateTime month) + private async Task> 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(); + var rows = new List(); 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 ?? "", r.Action, r.Outcome, r.Category, r.Target ?? "", - r.SourceNode ?? "", r.CorrelationId?.ToString() ?? "", r.DetailsJson ?? "", - r.ChainSeq?.ToString() ?? "", r.RowHash is null ? "" : Convert.ToHexString(r.RowHash)); + r.SourceNode ?? "", r.CorrelationId?.ToString() ?? "", r.DetailsJson ?? ""); private async Task InsertAsync(IEnumerable events) { @@ -387,22 +339,6 @@ public sealed class AuditLogPartitionArchiveTests : IClassFixture new(new DbContextOptionsBuilder().UseSqlServer(_fixture.ConnectionString).Options); diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditMaintenanceLoginTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditMaintenanceLoginTests.cs deleted file mode 100644 index a9ebd074..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Maintenance/AuditMaintenanceLoginTests.cs +++ /dev/null @@ -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; - -/// -/// The hardened two-login setup (ScadaBridge#107 item 3), end to end on SQL Server: a writer -/// login (db_datareader + db_datawriter + scadabridge_audit_writer) and a -/// maintenance login (scadabridge_audit_chainer + scadabridge_audit_purger only), -/// wired through AddConfigurationDatabase(writer, maintenance). 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. -/// -public sealed class AuditMaintenanceLoginTests : IClassFixture, 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-"); - - /// Creates the test class with its own migrated SQL Server database. - /// The per-class migration fixture. - public AuditMaintenanceLoginTests(MsSqlMigrationFixture fixture) => _fixture = fixture; - - private string WriterLogin => "sbw_" + _suffix; - - private string MaintenanceLogin => "sbm_" + _suffix; - - /// - 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(); - } - - /// - 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(); - } - - /// - /// 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. - /// - /// A task that represents the asynchronous operation. - [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(() => new AuditLogHashChain(writerCtx).ChainPendingAsync(100, chainBacklog: true)); - await Assert.ThrowsAsync(() => - 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(); - Assert.True(maintenance.UsesSeparateLogin); - Assert.Equal(MaintenanceLogin, await LoginNameAsync(maintenance.Context)); - Assert.Equal(WriterLogin, await LoginNameAsync(scope.ServiceProvider.GetRequiredService())); - - var chain = scope.ServiceProvider.GetRequiredService(); - 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().ReadHeadsAsync()); - Assert.Equal(5, head.LastSeq); - - var repo = scope.ServiceProvider.GetRequiredKeyedService(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(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(); - Assert.False(shared.UsesSeparateLogin); - Assert.Same(plainScope.ServiceProvider.GetRequiredService(), shared.Context); - } - - /// - /// ScadaBridge#113: the monthly roll-forward (ALTER PARTITION FUNCTION … SPLIT RANGE) and - /// the SourceNode backfill run on the maintenance login — and are refused to the writer - /// login — with only the role grants the AuditMaintenanceGrants migration provisions. - /// - /// A task that represents the asynchronous operation. - [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(() => new AuditLogPartitionMaintenance(writerCtx).EnsureLookaheadAsync(60)); - await Assert.ThrowsAsync(() => - 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(); - var roll = scope.ServiceProvider.GetRequiredService(); - 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( - 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().UseSqlServer(cs).Options); - - private static async Task 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()); -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/FlipAuditLogClusteredKeyMigrationTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/FlipAuditLogClusteredKeyMigrationTests.cs index afbc9052..b5847590 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/FlipAuditLogClusteredKeyMigrationTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/FlipAuditLogClusteredKeyMigrationTests.cs @@ -29,14 +29,18 @@ public sealed class FlipAuditLogClusteredKeyMigrationTests : IClassFixture - /// After the full chain: clustered PK (OccurredAtUtc, EventId), the unique aligned - /// (EventId, OccurredAtUtc) index, all twelve non-clustered indexes with unchanged keys and - /// filters, everything on ps_AuditLog_Month, and every append-only/chain grant intact. + /// After the full migration chain: clustered PK (OccurredAtUtc, EventId), the unique aligned + /// (EventId, OccurredAtUtc) index, the ten non-clustered indexes with unchanged keys and + /// filters (the two hash-chain indexes gone), everything on ps_AuditLog_Month, and every + /// append-only grant intact. /// /// A task that represents the asynchronous operation. [SkippableFact] @@ -67,7 +72,7 @@ public sealed class FlipAuditLogClusteredKeyMigrationTests : IClassFixture Assert.Equal("ps_AuditLog_Month", i.DataSpace)); - AssertTwelveNonClusteredIndexes(indexes); + AssertNonClusteredIndexes(indexes, withChainIndexes: false); Assert.Equal(ExpectedGrants, await ReadGrantsAsync()); } @@ -86,14 +91,16 @@ public sealed class FlipAuditLogClusteredKeyMigrationTests : IClassFixture !g.EndsWith("|SourceNode", StringComparison.Ordinal)).ToArray(); + // One migration before the flip: the hash chain's chainer grants exist (AddAuditLogHashChain) + // and AuditMaintenanceGrants is not applied yet. + private static readonly string[] GrantsBeforeTheFlip = + ExpectedGrants.Concat( + [ + "scadabridge_audit_chainer|SELECT|GRANT|", + "scadabridge_audit_chainer|UPDATE|GRANT|ChainSeq", + "scadabridge_audit_chainer|UPDATE|GRANT|RowHash", + ]).Order(StringComparer.Ordinal).ToArray(); - private static void AssertTwelveNonClusteredIndexes(IReadOnlyDictionary indexes) + private static void AssertNonClusteredIndexes(IReadOnlyDictionary indexes, bool withChainIndexes) { Assert.Equal("OccurredAtUtc DESC", indexes["IX_AuditLog_OccurredAtUtc"].Keys); Assert.Equal("Status", indexes["IX_AuditLog_OccurredAtUtc"].Included); @@ -180,11 +190,18 @@ public sealed class FlipAuditLogClusteredKeyMigrationTests : IClassFixture Assert.False(indexes[n].Clustered, n)); + if (withChainIndexes) + { + Assert.Equal("OccurredAtUtc, EventId", indexes["IX_AuditLog_Unchained"].Keys); + Assert.Equal("([ChainSeq] IS NULL)", indexes["IX_AuditLog_Unchained"].Filter); + Assert.Equal("ChainSeq", indexes["IX_AuditLog_ChainSeq"].Keys); + Assert.Equal("([ChainSeq] IS NOT NULL)", indexes["IX_AuditLog_ChainSeq"].Filter); + } + else + { + Assert.All(ChainIndexes, n => Assert.False(indexes.ContainsKey(n), n)); + } } private async Task AssertSwitchOutPurgesAsync(DateTime month) diff --git a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/IdempotentMigrationScriptTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/IdempotentMigrationScriptTests.cs index 198917c8..de64b392 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/IdempotentMigrationScriptTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests/Migrations/IdempotentMigrationScriptTests.cs @@ -60,7 +60,8 @@ public sealed class IdempotentMigrationScriptTests : IClassFixture +/// SQL Server tests for the RemoveAuditLogHashChain migration (2026-11-15): at head the +/// hash-chain columns, tables, indexes and chainer role are gone and the purger keeps only its +/// original grants; Down restores what AddAuditLogHashChain + AuditMaintenanceGrants +/// created; and Up is re-runnable and drops a chainer role that still has members. +/// +/// +/// The tests in this class migrate the fixture database down and back up, so they own their +/// fixture (one database per test class) and every test leaves the database fully migrated. +/// +public sealed class RemoveAuditLogHashChainMigrationTests : IClassFixture +{ + private const string RemoveMigration = "20261115221558_RemoveAuditLogHashChain"; + + private static readonly string PreviousMigration = MigrationBefore(RemoveMigration); + + private readonly MsSqlMigrationFixture _fixture; + + /// Creates the test class with its own migrated SQL Server database. + /// The per-class migration fixture. + public RemoveAuditLogHashChainMigrationTests(MsSqlMigrationFixture fixture) => _fixture = fixture; + + /// At head nothing of the hash chain or the maintenance grants is left. + /// A task that represents the asynchronous operation. + [SkippableFact] + public async Task AtHead_TheChainSchema_AndTheMaintenanceGrants_AreGone() + { + Skip.IfNot(_fixture.Available, _fixture.SkipReason); + await AssertRemovedAsync(); + } + + /// + /// Down restores the chain schema exactly (columns, tables, indexes, chainer role and grants, + /// the purger's maintenance grants); Up then drops it again, also when the chainer role has a + /// member; and re-running Up's SQL on the migrated schema changes nothing. + /// + /// A task that represents the asynchronous operation. + [SkippableFact] + public async Task DownUp_RoundTrips_DropsAChainerRoleWithMembers_AndIsReRunnable() + { + Skip.IfNot(_fixture.Available, _fixture.SkipReason); + + await MigrateToAsync(PreviousMigration); + try + { + Assert.True(await ScalarAsync("SELECT CASE WHEN COL_LENGTH('dbo.AuditLog','ChainSeq') IS NULL THEN 0 ELSE 1 END;") == 1); + Assert.True(await ScalarAsync("SELECT CASE WHEN COL_LENGTH('dbo.AuditLog','RowHash') IS NULL THEN 0 ELSE 1 END;") == 1); + Assert.True(await ScalarAsync("SELECT CASE WHEN OBJECT_ID('dbo.AuditLogChainHead','U') IS NULL THEN 0 ELSE 1 END;") == 1); + Assert.True(await ScalarAsync("SELECT CASE WHEN OBJECT_ID('dbo.AuditLogChainTombstone','U') IS NULL THEN 0 ELSE 1 END;") == 1); + Assert.Equal(2, await ScalarAsync( + "SELECT COUNT(*) FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.AuditLog') AND name IN ('IX_AuditLog_Unchained','IX_AuditLog_ChainSeq');")); + Assert.Contains("scadabridge_audit_chainer|UPDATE|GRANT|ChainSeq", await ReadAuditLogGrantsAsync()); + Assert.Contains("scadabridge_audit_purger|UPDATE|GRANT|SourceNode", await ReadAuditLogGrantsAsync()); + Assert.Equal(1, await ScalarAsync( + "SELECT COUNT(*) FROM sys.database_permissions p JOIN sys.database_principals pr ON pr.principal_id = p.grantee_principal_id " + + "WHERE pr.name = 'scadabridge_audit_purger' AND p.class = 0 AND p.permission_name = 'ALTER ANY DATASPACE' AND p.state_desc = 'GRANT';")); + + // A login was put in the chainer role (the hardened two-login setup): Up must still drop the role. + await ExecAsync("IF DATABASE_PRINCIPAL_ID('rm_chain_test_user') IS NULL CREATE USER rm_chain_test_user WITHOUT LOGIN;" + + "ALTER ROLE scadabridge_audit_chainer ADD MEMBER rm_chain_test_user;"); + } + finally + { + await MigrateToHeadAsync(); + } + + await AssertRemovedAsync(); + Assert.Equal(1, await ScalarAsync("SELECT CASE WHEN DATABASE_PRINCIPAL_ID('rm_chain_test_user') IS NULL THEN 0 ELSE 1 END;")); + + // Re-run Up's SQL against the migrated schema: every statement is guarded. + foreach (var op in new RemoveAuditLogHashChain().UpOperations.OfType()) + { + await ExecAsync(op.Sql); + } + + await AssertRemovedAsync(); + } + + private async Task AssertRemovedAsync() + { + Assert.Equal(0, await ScalarAsync("SELECT CASE WHEN COL_LENGTH('dbo.AuditLog','ChainSeq') IS NULL THEN 0 ELSE 1 END;")); + Assert.Equal(0, await ScalarAsync("SELECT CASE WHEN COL_LENGTH('dbo.AuditLog','RowHash') IS NULL THEN 0 ELSE 1 END;")); + Assert.Equal(0, await ScalarAsync("SELECT CASE WHEN OBJECT_ID('dbo.AuditLogChainHead','U') IS NULL THEN 0 ELSE 1 END;")); + Assert.Equal(0, await ScalarAsync("SELECT CASE WHEN OBJECT_ID('dbo.AuditLogChainTombstone','U') IS NULL THEN 0 ELSE 1 END;")); + Assert.Equal(0, await ScalarAsync( + "SELECT COUNT(*) FROM sys.indexes WHERE object_id = OBJECT_ID('dbo.AuditLog') AND name IN ('IX_AuditLog_Unchained','IX_AuditLog_ChainSeq');")); + Assert.Equal(0, await ScalarAsync("SELECT CASE WHEN DATABASE_PRINCIPAL_ID('scadabridge_audit_chainer') IS NULL THEN 0 ELSE 1 END;")); + Assert.Equal(0, await ScalarAsync( + "SELECT COUNT(*) FROM sys.database_permissions p JOIN sys.database_principals pr ON pr.principal_id = p.grantee_principal_id " + + "WHERE pr.name = 'scadabridge_audit_purger' AND p.class = 0 AND p.permission_name = 'ALTER ANY DATASPACE';")); + + // The roles that predate the chain stay, with exactly their original AuditLog grants. + Assert.Equal( + new[] + { + "scadabridge_audit_purger|DELETE|GRANT|", + "scadabridge_audit_purger|SELECT|GRANT|", + "scadabridge_audit_writer|DELETE|DENY|", + "scadabridge_audit_writer|INSERT|GRANT|", + "scadabridge_audit_writer|SELECT|GRANT|", + "scadabridge_audit_writer|UPDATE|DENY|", + }, + await ReadAuditLogGrantsAsync()); + } + + private async Task ScalarAsync(string sql) + { + await using var conn = _fixture.OpenConnection(); + await using var cmd = new SqlCommand(sql, conn); + return (T)Convert.ChangeType((await cmd.ExecuteScalarAsync())!, typeof(T), System.Globalization.CultureInfo.InvariantCulture); + } + + private async Task ExecAsync(string sql) + { + await using var conn = _fixture.OpenConnection(); + await using var cmd = new SqlCommand(sql, conn) { CommandTimeout = 300 }; + await cmd.ExecuteNonQueryAsync(); + } + + private async Task ReadAuditLogGrantsAsync() + { + await using var conn = _fixture.OpenConnection(); + await using var cmd = new SqlCommand(@" +SELECT pr.name, p.permission_name, p.state_desc, COL_NAME(p.major_id, p.minor_id) +FROM sys.database_permissions p JOIN sys.database_principals pr ON pr.principal_id = p.grantee_principal_id +WHERE p.major_id = OBJECT_ID('dbo.AuditLog') AND p.class = 1;", conn); + var list = new List(); + await using var reader = await cmd.ExecuteReaderAsync(); + while (await reader.ReadAsync()) + { + list.Add($"{reader.GetString(0)}|{reader.GetString(1)}|{reader.GetString(2)}|{(reader.IsDBNull(3) ? "" : reader.GetString(3))}"); + } + + return list.Order(StringComparer.Ordinal).ToArray(); + } + + private async Task MigrateToAsync(string migration) + { + await using var ctx = new ScadaBridgeDbContext(new DbContextOptionsBuilder() + .UseSqlServer(_fixture.ConnectionString, sql => sql.CommandTimeout(600)).Options); + await ctx.GetService().MigrateAsync(migration); + } + + private async Task MigrateToHeadAsync() + { + await using var ctx = new ScadaBridgeDbContext(new DbContextOptionsBuilder() + .UseSqlServer(_fixture.ConnectionString, sql => sql.CommandTimeout(600)).Options); + await ctx.GetService().MigrateAsync(); + } + + private static string MigrationBefore(string migration) + { + using var ctx = new ScadaBridgeDbContext(new DbContextOptionsBuilder() + .UseSqlServer("Server=unused;Database=unused").Options); + var all = ctx.Database.GetMigrations().ToList(); + return all[all.IndexOf(migration) - 1]; + } +} diff --git a/tests/ZB.MOM.WW.ScadaBridge.Host.Tests/AuditChainHeadExportHealthCheckTests.cs b/tests/ZB.MOM.WW.ScadaBridge.Host.Tests/AuditChainHeadExportHealthCheckTests.cs deleted file mode 100644 index 6c0aa08f..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.Host.Tests/AuditChainHeadExportHealthCheckTests.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Microsoft.Extensions.Diagnostics.HealthChecks; -using ZB.MOM.WW.ScadaBridge.AuditLog.Central.HeadExport; -using ZB.MOM.WW.ScadaBridge.Host.Health; - -namespace ZB.MOM.WW.ScadaBridge.Host.Tests; - -/// ScadaBridge#115: the chain-head export readiness entry. -public sealed class AuditChainHeadExportHealthCheckTests -{ - private sealed class Clock(DateTime now) : TimeProvider - { - public DateTime Now { get; set; } = now; - public override DateTimeOffset GetUtcNow() => new(DateTime.SpecifyKind(Now, DateTimeKind.Utc)); - } - - private static readonly DateTime T0 = new(2026, 11, 10, 12, 0, 0, DateTimeKind.Utc); - - private static async Task Check(AuditChainHeadExportStatus s, Clock c) - => (await new AuditChainHeadExportHealthCheck(s, c).CheckHealthAsync(new HealthCheckContext())).Status; - - /// Disabled export is healthy; failing or stale export is Degraded; recovery clears it. - [Fact] - public async Task Status_follows_export_outcome() - { - var clock = new Clock(T0); - var status = new AuditChainHeadExportStatus(); - Assert.Equal(HealthStatus.Healthy, await Check(status, clock)); // disabled - - status.MarkStarted(60, T0); - Assert.Equal(HealthStatus.Healthy, await Check(status, clock)); - - status.RecordFailure(T0, "share offline"); - var result = await new AuditChainHeadExportHealthCheck(status, clock).CheckHealthAsync(new HealthCheckContext()); - Assert.Equal(HealthStatus.Degraded, result.Status); - Assert.Contains("share offline", result.Description); - - status.RecordSuccess(T0); - Assert.Equal(HealthStatus.Healthy, await Check(status, clock)); - - clock.Now = T0.AddMinutes(121); // no success for > 2 intervals - Assert.Equal(HealthStatus.Degraded, await Check(status, clock)); - status.RecordSuccess(clock.Now); - Assert.Equal(HealthStatus.Healthy, await Check(status, clock)); - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/AuditArchiveCommandTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/AuditArchiveCommandTests.cs index 9a706d7e..e3911864 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/AuditArchiveCommandTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/AuditArchiveCommandTests.cs @@ -44,11 +44,10 @@ public class AuditArchiveCommandTests : TestKit new(new AuthenticatedUser("u", "U", roles, sites), command, Guid.NewGuid().ToString("N")); private static AuditArchiveManifest Manifest() => new( - 1, Name, new DateTime(2025, 10, 1, 0, 0, 0, DateTimeKind.Utc), "2025-10", + 2, Name, new DateTime(2025, 10, 1, 0, 0, 0, DateTimeKind.Utc), "2025-10", new DateTime(2026, 11, 9, 12, 0, 0, DateTimeKind.Utc), AuditArchiveStatus.Purged, - new DateTime(2026, 11, 9, 12, 0, 1, DateTimeKind.Utc), 10, 9, 1, null, null, - Name + ".parquet", "ab", 1234, Name + ".tombstones.parquet", "cd", 0, - [new AuditArchiveChainState("2025-10", 9, new string('0', 64), null, null, 0)]); + new DateTime(2026, 11, 9, 12, 0, 1, DateTimeKind.Utc), 10, null, null, + Name + ".parquet", "ab", 1234); /// A Viewer lists the archives; status renders as a name. [Fact] @@ -66,23 +65,21 @@ public class AuditArchiveCommandTests : TestKit Assert.Equal("Purged", archive.GetProperty("status").GetString()); } - /// Verification results reach the wire with the anchor and chain status as names. + /// Verification results reach the wire, status as a name. [Fact] public void Viewer_GetsVerificationResult() { - var chain = new AuditChainVerificationResult("2025-10", AuditChainStatus.Intact, 9, 0, null, 9, 1, null); _archive.VerifyAsync(Name, Arg.Any()).Returns(new AuditArchiveVerificationResult( - Name, "2025-10", AuditArchiveStatus.Purged, true, true, null, 10, - [new AuditArchiveChainVerification(chain, AuditArchiveAnchorStatus.Matches, "ends at the anchor")])); + Name, "2025-10", AuditArchiveStatus.Purged, true, true, null, 10)); CreateActor().Tell(Envelope(new VerifyAuditArchiveCommand(Name), [], "Viewer")); var response = ExpectMsg(TimeSpan.FromSeconds(5)); using var json = JsonDocument.Parse(response.JsonData); Assert.True(json.RootElement.GetProperty("verified").GetBoolean()); - var c = json.RootElement.GetProperty("chains")[0]; - Assert.Equal("Matches", c.GetProperty("anchor").GetString()); - Assert.Equal("Intact", c.GetProperty("chain").GetProperty("status").GetString()); + Assert.Equal("Purged", json.RootElement.GetProperty("manifestStatus").GetString()); + Assert.Equal(10, json.RootElement.GetProperty("rowsRead").GetInt64()); + Assert.False(json.RootElement.TryGetProperty("chains", out _)); } /// Designer/Deployer without an OperationalAudit role are refused both commands. diff --git a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/RequiredRoleMatrixTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/RequiredRoleMatrixTests.cs index f4b9d3b4..1577d505 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/RequiredRoleMatrixTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/RequiredRoleMatrixTests.cs @@ -168,12 +168,8 @@ public class RequiredRoleMatrixTests ["FlowDebugReplay"] = FlowDebuggerReplayers, // ---- Audit reads (OperationalAudit) ----------------------------------------- - // AuditLog hash-chain verification (ScadaBridge#41) carries audit query's - // OperationalAudit permission (alog.md §15.1); written out by hand to keep the freeze. - ["VerifyAuditChain"] = [Roles.Administrator, Roles.Viewer], - // Off-box exported chain heads (ScadaBridge#107): same audit-read gate. - ["VerifyAuditChainHeads"] = [Roles.Administrator, Roles.Viewer], - // AuditLog Parquet archives (ScadaBridge#42): same audit-read gate as verify-chain. + // AuditLog Parquet archives (ScadaBridge#42) carry audit query's OperationalAudit + // permission (alog.md §15.1); written out by hand to keep the freeze. ["ListAuditArchives"] = [Roles.Administrator, Roles.Viewer], ["VerifyAuditArchive"] = [Roles.Administrator, Roles.Viewer], // Archive cleanup deletes files (ScadaBridge#109): Administrator only. diff --git a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/VerifyAuditChainCommandTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/VerifyAuditChainCommandTests.cs deleted file mode 100644 index 482c9e08..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/VerifyAuditChainCommandTests.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System.Text.Json; -using Akka.Actor; -using Akka.TestKit.Xunit2; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging.Abstractions; -using NSubstitute; -using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; -using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.ManagementService.Tests; - -/// -/// The VerifyAuditChain management command (ScadaBridge#41): role gate, month -/// parsing, site-scope refusal, and the verification result reaching the wire with its -/// enums as names. -/// -public class VerifyAuditChainCommandTests : TestKit -{ - private readonly IAuditLogHashChain _chain = Substitute.For(); - - private IActorRef CreateActor() - { - var services = new ServiceCollection().AddTestConfigurationTransaction(); - services.AddScoped(_ => _chain); - return Sys.ActorOf(Props.Create(() => new ManagementActor( - services.BuildServiceProvider(), NullLogger.Instance))); - } - - private static ManagementEnvelope Envelope(object command, string[] sites, params string[] roles) => - new(new AuthenticatedUser("u", "U", roles, sites), command, Guid.NewGuid().ToString("N")); - - /// A Viewer gets the verifier's result for the requested month, enums as names. - [Fact] - public void Viewer_GetsVerificationResult_ForTheRequestedMonth() - { - var brk = new AuditChainBreak("2026-05", 7, Guid.Parse("11111111-2222-3333-4444-555555555555"), - AuditChainBreakKind.HashMismatch, "edited"); - _chain.VerifyMonthAsync(Arg.Any(), Arg.Any()) - .Returns(new AuditChainVerificationResult("2026-05", AuditChainStatus.Broken, 6, 0, null, 43, 2, brk)); - - CreateActor().Tell(Envelope(new VerifyAuditChainCommand("2026-05"), [], "Viewer")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - using var json = JsonDocument.Parse(response.JsonData); - Assert.Equal("Broken", json.RootElement.GetProperty("status").GetString()); - Assert.Equal("HashMismatch", json.RootElement.GetProperty("firstBreak").GetProperty("kind").GetString()); - Assert.Equal(7, json.RootElement.GetProperty("firstBreak").GetProperty("chainSeq").GetInt64()); - _chain.Received(1).VerifyMonthAsync( - new DateTime(2026, 5, 1, 0, 0, 0, DateTimeKind.Utc), Arg.Any()); - } - - /// - /// ScadaBridge#139: the cutoff, the pre-cutoff row count and the new break kinds reach the - /// wire (the CLI renders them; the kinds as names like every other kind). - /// - [Fact] - public void CutoffFields_AndCutoffBreakKind_ReachTheWire() - { - var chainFrom = new DateTime(2026, 11, 13, 9, 30, 0, DateTimeKind.Utc); - var brk = new AuditChainBreak("2026-11", 4, Guid.NewGuid(), AuditChainBreakKind.ChainedBeforeCutoff, "moved back"); - _chain.VerifyMonthAsync(Arg.Any(), Arg.Any()) - .Returns(new AuditChainVerificationResult("2026-11", AuditChainStatus.Broken, 3, 0, null, 9, 0, brk, 3441, chainFrom)); - - CreateActor().Tell(Envelope(new VerifyAuditChainCommand("2026-11"), [], "Viewer")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - using var json = JsonDocument.Parse(response.JsonData); - Assert.Equal(3441, json.RootElement.GetProperty("preCutoffRows").GetInt64()); - Assert.Equal(chainFrom, json.RootElement.GetProperty("chainFromUtc").GetDateTime().ToUniversalTime()); - Assert.Equal("ChainedBeforeCutoff", json.RootElement.GetProperty("firstBreak").GetProperty("kind").GetString()); - } - - /// A Designer/Deployer without an OperationalAudit role is refused. - [Fact] - public void NonAuditRole_IsUnauthorized() - { - CreateActor().Tell(Envelope(new VerifyAuditChainCommand("2026-05"), [], "Designer", "Deployer")); - - ExpectMsg(TimeSpan.FromSeconds(5)); - _chain.DidNotReceiveWithAnyArgs().VerifyMonthAsync(default, default); - } - - /// A site-scoped Viewer is refused: the chain spans every site. - [Fact] - public void SiteScopedViewer_IsRefused() - { - CreateActor().Tell(Envelope(new VerifyAuditChainCommand("2026-05"), ["3"], "Viewer")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - Assert.Contains("spans every site", response.Message); - _chain.DidNotReceiveWithAnyArgs().VerifyMonthAsync(default, default); - } - - /// A malformed month is a caller error, not an internal fault. - [Fact] - public void MalformedMonth_IsInvalidArgument() - { - CreateActor().Tell(Envelope(new VerifyAuditChainCommand("2026-13"), [], "Administrator")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - Assert.Equal("INVALID_ARGUMENT", response.ErrorCode); - } - - /// A verification that cannot get the chain lock surfaces as CHAIN_BUSY. - [Fact] - public void BusyChain_IsChainBusy() - { - _chain.VerifyMonthAsync(Arg.Any(), Arg.Any()) - .Returns>(_ => throw new TimeoutException("busy")); - - CreateActor().Tell(Envelope(new VerifyAuditChainCommand("2026-05"), [], "Administrator")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - Assert.Equal("CHAIN_BUSY", response.ErrorCode); - } - - /// Verification runs under the long-running Ask timeout, not the 30 s default. - [Fact] - public void VerifyAuditChain_UsesLongRunningAskTimeout() - { - Assert.Equal(TimeSpan.FromMinutes(5), - ManagementEndpoints.ResolveAskTimeout(new ManagementServiceOptions(), typeof(VerifyAuditChainCommand))); - } -} diff --git a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/VerifyAuditChainHeadsCommandTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/VerifyAuditChainHeadsCommandTests.cs deleted file mode 100644 index b6d0247e..00000000 --- a/tests/ZB.MOM.WW.ScadaBridge.ManagementService.Tests/VerifyAuditChainHeadsCommandTests.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System.Text.Json; -using Akka.Actor; -using Akka.TestKit.Xunit2; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging.Abstractions; -using NSubstitute; -using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories; -using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management; -using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit; - -namespace ZB.MOM.WW.ScadaBridge.ManagementService.Tests; - -/// -/// The VerifyAuditChainHeads management command (ScadaBridge#107 item 1): role gate, -/// site-scope refusal, input bounds, and the verdicts reaching the wire with enums as names. -/// -public class VerifyAuditChainHeadsCommandTests : TestKit -{ - private readonly IAuditChainHeadStore _store = Substitute.For(); - - private static readonly AuditChainHeadRecord Record = new( - 1, "2026-05", 12, new string('a', 64), null, null, 12, 0, - new DateTime(2026, 5, 3, 0, 0, 0, DateTimeKind.Utc), new DateTime(2026, 5, 3, 1, 0, 0, DateTimeKind.Utc), "central-a"); - - private IActorRef CreateActor() - { - var services = new ServiceCollection().AddTestConfigurationTransaction(); - services.AddScoped(_ => _store); - return Sys.ActorOf(Props.Create(() => new ManagementActor( - services.BuildServiceProvider(), NullLogger.Instance))); - } - - private static ManagementEnvelope Envelope(object command, string[] sites, params string[] roles) => - new(new AuthenticatedUser("u", "U", roles, sites), command, Guid.NewGuid().ToString("N")); - - /// A Viewer gets the store's verdicts; the records reach the store unchanged. - [Fact] - public void Viewer_GetsTheVerdicts_ForTheSuppliedRecords() - { - var check = new AuditChainHeadCheck("2026-05", 12, Record.ExportedAtUtc, "central-a", - AuditChainHeadStatus.Mismatch, 12, "rewritten"); - _store.VerifyExportedHeadsAsync(Arg.Any>(), Arg.Any()) - .Returns(new AuditChainHeadVerificationResult(false, 1, [check])); - - CreateActor().Tell(Envelope(new VerifyAuditChainHeadsCommand([Record]), [], "Viewer")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - using var json = JsonDocument.Parse(response.JsonData); - Assert.False(json.RootElement.GetProperty("verified").GetBoolean()); - Assert.Equal("Mismatch", json.RootElement.GetProperty("checks")[0].GetProperty("status").GetString()); - _store.Received(1).VerifyExportedHeadsAsync( - Arg.Is>(r => r.Count == 1 && r[0] == Record), Arg.Any()); - } - - /// A Designer/Deployer without an OperationalAudit role is refused. - [Fact] - public void NonAuditRole_IsUnauthorized() - { - CreateActor().Tell(Envelope(new VerifyAuditChainHeadsCommand([Record]), [], "Designer", "Deployer")); - - ExpectMsg(TimeSpan.FromSeconds(5)); - _store.DidNotReceiveWithAnyArgs().VerifyExportedHeadsAsync(default!, default); - } - - /// A site-scoped Viewer is refused: the chain spans every site. - [Fact] - public void SiteScopedViewer_IsRefused() - { - CreateActor().Tell(Envelope(new VerifyAuditChainHeadsCommand([Record]), ["3"], "Viewer")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - Assert.Contains("spans every site", response.Message); - _store.DidNotReceiveWithAnyArgs().VerifyExportedHeadsAsync(default!, default); - } - - /// No records, or more than the cap, is a caller error. - /// Records supplied. - [Theory] - [InlineData(0)] - [InlineData(ManagementActor.MaxChainHeadRecords + 1)] - public void EmptyOrOversizedInput_IsInvalidArgument(int count) - { - CreateActor().Tell(Envelope( - new VerifyAuditChainHeadsCommand(Enumerable.Repeat(Record, count).ToList()), [], "Administrator")); - - var response = ExpectMsg(TimeSpan.FromSeconds(5)); - Assert.Equal("INVALID_ARGUMENT", response.ErrorCode); - _store.DidNotReceiveWithAnyArgs().VerifyExportedHeadsAsync(default!, default); - } - - /// A chain lock that cannot be had surfaces as CHAIN_BUSY. - [Fact] - public void BusyChain_IsChainBusy() - { - _store.VerifyExportedHeadsAsync(Arg.Any>(), Arg.Any()) - .Returns>(_ => throw new TimeoutException("busy")); - - CreateActor().Tell(Envelope(new VerifyAuditChainHeadsCommand([Record]), [], "Administrator")); - - Assert.Equal("CHAIN_BUSY", ExpectMsg(TimeSpan.FromSeconds(5)).ErrorCode); - } - - /// Runs under the long-running Ask timeout. - [Fact] - public void VerifyAuditChainHeads_UsesLongRunningAskTimeout() - { - Assert.Equal(TimeSpan.FromMinutes(5), - ManagementEndpoints.ResolveAskTimeout(new ManagementServiceOptions(), typeof(VerifyAuditChainHeadsCommand))); - } -}