stabilizaiton work - projects rework for maintenanceability and ui livening
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace StellaOps.Audit.ReplayToken;
|
||||
|
||||
public sealed partial class Sha256ReplayTokenGenerator
|
||||
{
|
||||
private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
WriteIndented = false,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
private static string? NormalizeValue(string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return value.Trim();
|
||||
}
|
||||
private static List<string> NormalizeSortedList(IReadOnlyList<string>? values)
|
||||
{
|
||||
if (values is null || values.Count == 0)
|
||||
{
|
||||
return new List<string>();
|
||||
}
|
||||
return values
|
||||
.Where(static x => !string.IsNullOrWhiteSpace(x))
|
||||
.Select(static x => x.Trim())
|
||||
.OrderBy(static x => x, StringComparer.Ordinal)
|
||||
.ToList();
|
||||
}
|
||||
private static Dictionary<string, string> NormalizeSortedDictionary(IReadOnlyDictionary<string, string>? values)
|
||||
{
|
||||
if (values is null || values.Count == 0)
|
||||
{
|
||||
return new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
var normalized = new List<KeyValuePair<string, string>>(values.Count);
|
||||
var seen = new HashSet<string>(StringComparer.Ordinal);
|
||||
|
||||
foreach (var kvp in values)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(kvp.Key))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var key = kvp.Key.Trim();
|
||||
if (!seen.Add(key))
|
||||
{
|
||||
throw new ArgumentException($"AdditionalContext contains duplicate key after normalization: '{key}'.", nameof(values));
|
||||
}
|
||||
|
||||
normalized.Add(new KeyValuePair<string, string>(key, kvp.Value?.Trim() ?? string.Empty));
|
||||
}
|
||||
|
||||
return normalized
|
||||
.OrderBy(static kvp => kvp.Key, StringComparer.Ordinal)
|
||||
.ToDictionary(static kvp => kvp.Key, static kvp => kvp.Value, StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Produces deterministic canonical representation of inputs.
|
||||
/// </summary>
|
||||
private static string Canonicalize(ReplayTokenRequest request, string version)
|
||||
{
|
||||
var canonical = new CanonicalReplayInput
|
||||
{
|
||||
Version = version,
|
||||
FeedManifests = NormalizeSortedList(request.FeedManifests),
|
||||
RulesVersion = NormalizeValue(request.RulesVersion),
|
||||
RulesHash = NormalizeValue(request.RulesHash),
|
||||
LatticePolicyVersion = NormalizeValue(request.LatticePolicyVersion),
|
||||
LatticePolicyHash = NormalizeValue(request.LatticePolicyHash),
|
||||
InputHashes = NormalizeSortedList(request.InputHashes),
|
||||
ScoringConfigVersion = NormalizeValue(request.ScoringConfigVersion),
|
||||
EvidenceHashes = NormalizeSortedList(request.EvidenceHashes),
|
||||
AdditionalContext = NormalizeSortedDictionary(request.AdditionalContext)
|
||||
};
|
||||
|
||||
return JsonSerializer.Serialize(canonical, _jsonOptions);
|
||||
}
|
||||
|
||||
private sealed class CanonicalReplayInput
|
||||
{
|
||||
public required string Version { get; init; }
|
||||
public required List<string> FeedManifests { get; init; }
|
||||
public string? RulesVersion { get; init; }
|
||||
public string? RulesHash { get; init; }
|
||||
public string? LatticePolicyVersion { get; init; }
|
||||
public string? LatticePolicyHash { get; init; }
|
||||
public required List<string> InputHashes { get; init; }
|
||||
public string? ScoringConfigVersion { get; init; }
|
||||
public required List<string> EvidenceHashes { get; init; }
|
||||
public required Dictionary<string, string> AdditionalContext { get; init; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user