Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/Commands/AuditConfigDeprecationTests.cs
T
Joseph Doherty ed2ed0a367 feat(audit)!: remove the AuditLog hash chain, head export and two-login maintenance hardening
Owner decision 2026-11-15, at IT security's request, pending a family-wide general solution.

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

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

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

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

Claude-Session: https://claude.ai/code/session_01GT67AFKC45MBz1wPZnVbX8
2026-11-15 17:38:22 -05:00

106 lines
4.4 KiB
C#

using System.CommandLine;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands;
/// <summary>
/// Tests for the <c>audit-log</c> → <c>audit-config</c> rename:
/// the new name parses, the deprecated <c>audit-log</c> alias still resolves the full
/// subcommand tree and emits a stderr deprecation warning, and the renamed group does
/// not collide with the distinct <c>audit</c> group from Bundle A.
/// </summary>
public class AuditConfigDeprecationTests
{
private static readonly Option<string> Url = new("--url") { Recursive = true };
private static readonly Option<string> Username = new("--username") { Recursive = true };
private static readonly Option<string> Password = new("--password") { Recursive = true };
private static readonly Option<string> Format = CliOptions.CreateFormatOption();
private static RootCommand BuildRoot()
{
var root = new RootCommand();
root.Add(Url);
root.Add(Username);
root.Add(Password);
root.Add(Format);
root.Add(AuditCommands.Build(Url, Format, Username, Password));
root.Add(AuditLogCommands.Build(Url, Format, Username, Password));
return root;
}
/// <summary>Verifies that the audit-config query command works.</summary>
[Fact]
public void AuditConfig_Query_Works()
{
// The new `audit-config query` name parses cleanly with no errors.
var root = BuildRoot();
var parse = root.Parse(new[] { "audit-config", "query", "--user", "alice" });
Assert.Empty(parse.Errors);
}
/// <summary>Verifies that the legacy audit-log query command still works but emits a deprecation warning to stderr.</summary>
[Fact]
public void AuditLog_Query_StillWorks_ButEmitsDeprecationWarning_ToStderr()
{
// The deprecated `audit-log` alias still resolves the full subcommand tree...
var root = BuildRoot();
var parse = root.Parse(new[] { "audit-log", "query", "--user", "alice" });
Assert.Empty(parse.Errors);
// ...and invoking via the old name emits the deprecation warning to stderr.
var stderr = new StringWriter();
AuditLogCommands.WriteDeprecationWarningIfNeeded(
new[] { "audit-log", "query" }, stderr);
var warning = stderr.ToString();
Assert.Contains("deprecated", warning);
Assert.Contains("audit-config", warning);
}
/// <summary>Verifies that no deprecation warning is emitted when the new command name is used.</summary>
[Fact]
public void DeprecationWarning_NotEmitted_ForNewName()
{
// The new `audit-config` name must not trigger the deprecation warning.
var stderr = new StringWriter();
AuditLogCommands.WriteDeprecationWarningIfNeeded(
new[] { "audit-config", "query" }, stderr);
Assert.Equal("", stderr.ToString());
}
/// <summary>Verifies that no deprecation warning is emitted for an unrelated command.</summary>
[Fact]
public void DeprecationWarning_NotEmitted_ForUnrelatedCommand()
{
var stderr = new StringWriter();
AuditLogCommands.WriteDeprecationWarningIfNeeded(
new[] { "audit", "query" }, stderr);
Assert.Equal("", stderr.ToString());
}
/// <summary>Verifies that the audit and audit-config groups are distinct commands that do not conflict.</summary>
[Fact]
public void Audit_And_AuditConfig_AreDistinctCommands_NoConflict()
{
var root = BuildRoot();
var auditNames = new[] { "audit", "audit-config" };
foreach (var name in auditNames)
{
var match = root.Subcommands.SingleOrDefault(c => c.Name == name);
Assert.NotNull(match);
}
// The two groups are distinct objects with distinct subcommand sets:
// `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 == "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);
Assert.DoesNotContain(AuditLogCommands.DeprecatedAlias, audit.Aliases);
}
}