feat(management): parquet audit export and audit archive list/verify (ScadaBridge#42)
GET /api/audit/export?format=parquet returned 501. It now streams the same 24 columns as csv/jsonl (AuditExportRow, CsvHeader order) as one Parquet file, one 10,000-row row group at a time. Each row group is encoded into a small non-seekable staging buffer and copied to the response asynchronously, because Kestrel rejects the synchronous writes the Parquet writer issues. New management commands, with CLI parity: - ListAuditArchives -> `audit archive list` (AuditArchiveListResult: enabled flag + every manifest). - VerifyAuditArchive(Name) -> `audit archive verify --name` (files, chain, purge anchor); exit 0 verified, 3 not verified, 1 error, 2 unauthorized. Both carry verify-chain's gate: OperationalAudit roles (Administrator, Viewer), site-scoped non-Administrator refused because an archive spans every site. VerifyAuditArchive uses the long-running Ask timeout. Error codes ARCHIVE_NOT_CONFIGURED, INVALID_ARGUMENT, NOT_FOUND. RequiredRoleMatrixTests freezes both gates; ManagementSurfaceParityTests now probes `audit archive list|verify` alongside `audit verify-chain`. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
||||
|
||||
/// <summary>
|
||||
/// Helpers for the <c>audit archive list</c> / <c>audit archive verify</c> subcommands
|
||||
/// (AuditLog Parquet cold archive, ScadaBridge#42). Both send a management command
|
||||
/// (<c>ListAuditArchives</c>, <c>VerifyAuditArchive</c>); the archives live on the central
|
||||
/// nodes' archive directory, not on the operator's machine.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <c>verify</c> exit codes: <c>0</c> verified, <see cref="NotVerifiedExitCode"/> when the
|
||||
/// files, the chain or the purge anchor disagree, <c>1</c> on a request error, <c>2</c> on an
|
||||
/// authorization failure.
|
||||
/// </remarks>
|
||||
public static class AuditArchiveHelpers
|
||||
{
|
||||
/// <summary>Process exit code when an archive failed verification (tamper evidence).</summary>
|
||||
public const int NotVerifiedExitCode = 3;
|
||||
|
||||
/// <summary>
|
||||
/// Client deadline for <c>verify</c>: the server hashes and re-chains a whole month under
|
||||
/// the long-running management Ask (5 min); the client waits a little longer.
|
||||
/// </summary>
|
||||
public static readonly TimeSpan VerifyRequestTimeout = TimeSpan.FromMinutes(6);
|
||||
|
||||
/// <summary>Renders a <c>ListAuditArchives</c> result.</summary>
|
||||
/// <param name="json">The success body (an <c>AuditArchiveListResult</c>, camelCase).</param>
|
||||
/// <param name="format">Resolved output format.</param>
|
||||
/// <param name="output">Writer for the report.</param>
|
||||
/// <returns>Always <c>0</c>.</returns>
|
||||
public static int RenderList(string json, string format, TextWriter output)
|
||||
{
|
||||
if (!string.Equals(format, "table", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
output.WriteLine(json);
|
||||
output.Flush();
|
||||
return 0;
|
||||
}
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var root = doc.RootElement;
|
||||
var enabled = root.TryGetProperty("enabled", out var e) && e.ValueKind == JsonValueKind.True;
|
||||
output.WriteLine($"Archiving: {(enabled ? "enabled" : "disabled")}");
|
||||
if (!root.TryGetProperty("archives", out var archives) || archives.GetArrayLength() == 0)
|
||||
{
|
||||
output.WriteLine("No archives.");
|
||||
output.Flush();
|
||||
return 0;
|
||||
}
|
||||
|
||||
output.WriteLine($"{"NAME",-42} {"PARTITION",-9} {"STATUS",-8} {"ROWS",12} {"CHAINED",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}"));
|
||||
}
|
||||
|
||||
output.Flush();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>Renders a <c>VerifyAuditArchive</c> result and maps it to the exit code.</summary>
|
||||
/// <param name="json">The success body (an <c>AuditArchiveVerificationResult</c>, camelCase).</param>
|
||||
/// <param name="format">Resolved output format.</param>
|
||||
/// <param name="output">Writer for the report.</param>
|
||||
/// <returns><c>0</c> when verified, <see cref="NotVerifiedExitCode"/> otherwise.</returns>
|
||||
public static int RenderVerify(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 : NotVerifiedExitCode;
|
||||
}
|
||||
|
||||
output.WriteLine($"Archive : {Str(root, "name")}");
|
||||
output.WriteLine($"Partition : {Str(root, "partition")}");
|
||||
output.WriteLine($"Manifest : {Str(root, "manifestStatus")}");
|
||||
output.WriteLine($"Verified : {(verified ? "yes" : "NO")}");
|
||||
output.WriteLine($"Files intact: {(root.TryGetProperty("filesIntact", out var f) && f.ValueKind == JsonValueKind.True ? "yes" : "NO")}");
|
||||
if (Str(root, "fileProblem") is { Length: > 0 } problem)
|
||||
{
|
||||
output.WriteLine($" problem : {problem}");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user