1eb6e972b0
Bulk CommentChecker pass: fills in <param>/<inheritdoc> tags on public APIs across all 23 src/ projects so the doc-coverage gate is green. Also adds a Sister Projects section to CLAUDE.md pointing at the MxAccess Gateway and OtOpcUa sibling repos, and gitignores local credential captures (*login*.txt) and the wonder-app-vd03 deploy/ artifacts.
167 lines
6.7 KiB
C#
167 lines
6.7 KiB
C#
using System.Globalization;
|
|
using System.Net;
|
|
|
|
namespace ScadaLink.CLI.Commands;
|
|
|
|
/// <summary>
|
|
/// Filter + destination arguments for an <c>audit export</c> invocation. Mirrors the
|
|
/// Bundle B <c>GET /api/audit/export</c> parameters.
|
|
/// <see cref="Channel"/>/<see cref="Kind"/>/<see cref="Status"/>/<see cref="Site"/>
|
|
/// are multi-valued — each supplied value becomes a repeated query-string param so
|
|
/// the server's multi-value <c>IN (…)</c> filter sees the full set, exactly like
|
|
/// the <c>audit query</c> subcommand.
|
|
/// </summary>
|
|
public sealed class AuditExportArgs
|
|
{
|
|
/// <summary>
|
|
/// Start timestamp for the export time window.
|
|
/// </summary>
|
|
public string Since { get; set; } = string.Empty;
|
|
/// <summary>
|
|
/// End timestamp for the export time window.
|
|
/// </summary>
|
|
public string Until { get; set; } = string.Empty;
|
|
/// <summary>
|
|
/// Export format (e.g., 'json', 'csv', 'parquet').
|
|
/// </summary>
|
|
public string Format { get; set; } = string.Empty;
|
|
/// <summary>
|
|
/// Output file path for the exported audit log.
|
|
/// </summary>
|
|
public string Output { get; set; } = string.Empty;
|
|
/// <summary>
|
|
/// Channel filter values (repeated query parameter).
|
|
/// </summary>
|
|
public string[] Channel { get; set; } = Array.Empty<string>();
|
|
/// <summary>
|
|
/// Kind filter values (repeated query parameter).
|
|
/// </summary>
|
|
public string[] Kind { get; set; } = Array.Empty<string>();
|
|
/// <summary>
|
|
/// Status filter values (repeated query parameter).
|
|
/// </summary>
|
|
public string[] Status { get; set; } = Array.Empty<string>();
|
|
/// <summary>
|
|
/// Site identifier filter values (repeated query parameter).
|
|
/// </summary>
|
|
public string[] Site { get; set; } = Array.Empty<string>();
|
|
/// <summary>
|
|
/// Optional target system filter.
|
|
/// </summary>
|
|
public string? Target { get; set; }
|
|
/// <summary>
|
|
/// Optional actor/user filter.
|
|
/// </summary>
|
|
public string? Actor { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helpers for the <c>audit export</c> subcommand: builds the export query string and
|
|
/// streams the HTTP response body straight to the destination file without buffering
|
|
/// the (potentially multi-megabyte) export in memory.
|
|
/// </summary>
|
|
public static class AuditExportHelpers
|
|
{
|
|
/// <summary>
|
|
/// Builds the <c>?...</c> query string for <c>GET /api/audit/export</c>: the required
|
|
/// time window + format, plus optional filters. Time-specs are resolved via
|
|
/// <see cref="AuditQueryHelpers.ResolveTimeSpec"/>. The multi-valued
|
|
/// <c>--channel</c>/<c>--kind</c>/<c>--status</c>/<c>--site</c> filters each emit ONE
|
|
/// repeated query-string key per value (e.g. <c>channel=A&channel=B</c>) so the
|
|
/// server's multi-value <c>IN (…)</c> filter receives the full set — mirroring
|
|
/// <see cref="AuditQueryHelpers.BuildQueryString"/>.
|
|
/// </summary>
|
|
/// <param name="args">The export arguments containing filters and format.</param>
|
|
/// <param name="now">The current time for resolving relative time specifications.</param>
|
|
public static string BuildQueryString(AuditExportArgs args, DateTimeOffset now)
|
|
{
|
|
var parts = new List<string>();
|
|
|
|
void Add(string key, string? value)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(value))
|
|
parts.Add($"{key}={Uri.EscapeDataString(value)}");
|
|
}
|
|
|
|
void AddEach(string key, IReadOnlyList<string> values)
|
|
{
|
|
foreach (var value in values)
|
|
{
|
|
Add(key, value);
|
|
}
|
|
}
|
|
|
|
Add("fromUtc", AuditQueryHelpers.ResolveTimeSpec(args.Since, now).ToString("o", CultureInfo.InvariantCulture));
|
|
Add("toUtc", AuditQueryHelpers.ResolveTimeSpec(args.Until, now).ToString("o", CultureInfo.InvariantCulture));
|
|
Add("format", args.Format);
|
|
AddEach("channel", args.Channel);
|
|
AddEach("kind", args.Kind);
|
|
AddEach("status", args.Status);
|
|
AddEach("sourceSiteId", args.Site);
|
|
Add("target", args.Target);
|
|
Add("actor", args.Actor);
|
|
|
|
return "?" + string.Join("&", parts);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Executes the export: GETs <c>/api/audit/export</c> and copies the response body
|
|
/// stream directly to <see cref="AuditExportArgs.Output"/>. The body is never fully
|
|
/// buffered — <see cref="Stream.CopyToAsync(Stream)"/> streams in fixed-size chunks.
|
|
/// A <c>501 Not Implemented</c> (parquet not yet supported server-side) prints the
|
|
/// server message and returns a non-zero exit code.
|
|
/// </summary>
|
|
/// <param name="client">The management HTTP client for API communication.</param>
|
|
/// <param name="args">The export arguments containing filters and output file path.</param>
|
|
/// <param name="output">Text writer for command output messages.</param>
|
|
/// <param name="now">The current time for resolving relative time specifications.</param>
|
|
public static async Task<int> RunExportAsync(
|
|
ManagementHttpClient client, AuditExportArgs args, TextWriter output, DateTimeOffset now)
|
|
{
|
|
var qs = BuildQueryString(args, now);
|
|
|
|
HttpResponseMessage response;
|
|
try
|
|
{
|
|
response = await client.SendGetStreamAsync("api/audit/export" + qs, CancellationToken.None);
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
OutputFormatter.WriteError($"Connection failed: {ex.Message}", "CONNECTION_FAILED");
|
|
return 1;
|
|
}
|
|
|
|
using (response)
|
|
{
|
|
if (response.StatusCode == HttpStatusCode.NotImplemented)
|
|
{
|
|
var message = await response.Content.ReadAsStringAsync();
|
|
OutputFormatter.WriteError(
|
|
string.IsNullOrWhiteSpace(message)
|
|
? "Export format not implemented by the server."
|
|
: message,
|
|
"NOT_IMPLEMENTED");
|
|
return 1;
|
|
}
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var message = await response.Content.ReadAsStringAsync();
|
|
OutputFormatter.WriteError(
|
|
string.IsNullOrWhiteSpace(message) ? $"Export failed (HTTP {(int)response.StatusCode})." : message,
|
|
"ERROR");
|
|
return 1;
|
|
}
|
|
|
|
await using var source = await response.Content.ReadAsStreamAsync();
|
|
await using var destination = new FileStream(
|
|
args.Output, FileMode.Create, FileAccess.Write, FileShare.None,
|
|
bufferSize: 81920, useAsync: true);
|
|
await source.CopyToAsync(destination);
|
|
}
|
|
|
|
output.WriteLine($"Exported audit log to {args.Output}");
|
|
return 0;
|
|
}
|
|
}
|