| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Core.Metadata; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides optional normalization and budget validation helpers for host-owned governance metadata. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// This helper does not classify, redact, encrypt, or sanitize sensitive values. Hosts should treat |
| | | 11 | | /// successful validation as a bounded-shape check only and still apply their own privacy, DLP, |
| | | 12 | | /// retention, and signing policies before durable storage or emission. |
| | | 13 | | /// </remarks> |
| | | 14 | | public static class GovernanceMetadataBudgetValidator |
| | | 15 | | { |
| | | 16 | | private const int EmptySerializedObjectBytes = 2; |
| | | 17 | | private const int SerializedEntryOverheadBytes = 6; |
| | | 18 | | |
| | 3 | 19 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 3 | 20 | | new ReadOnlyDictionary<string, string>( |
| | 3 | 21 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 22 | | |
| | 3 | 23 | | private static readonly string[] EmptyViolations = []; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Normalizes metadata by trimming keys and values, removing blank keys, and using ordinal key comparison. |
| | | 27 | | /// </summary> |
| | | 28 | | public static IReadOnlyDictionary<string, string> Normalize( |
| | | 29 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 30 | | { |
| | 449 | 31 | | if (metadata is null || metadata.Count == 0) |
| | | 32 | | { |
| | 28 | 33 | | return EmptyMetadata; |
| | | 34 | | } |
| | | 35 | | |
| | 421 | 36 | | Dictionary<string, string> normalizedMetadata = new(metadata.Count, StringComparer.Ordinal); |
| | | 37 | | |
| | 1706 | 38 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 39 | | { |
| | 432 | 40 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 41 | | { |
| | | 42 | | continue; |
| | | 43 | | } |
| | | 44 | | |
| | 414 | 45 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 46 | | } |
| | | 47 | | |
| | 421 | 48 | | return normalizedMetadata.Count == 0 |
| | 421 | 49 | | ? EmptyMetadata |
| | 421 | 50 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Validates metadata against the recommended budget or a host-supplied budget. |
| | | 55 | | /// </summary> |
| | | 56 | | public static GovernanceMetadataBudgetValidationResult Validate( |
| | | 57 | | IReadOnlyDictionary<string, string>? metadata, |
| | | 58 | | GovernanceMetadataBudget? budget = null) |
| | | 59 | | { |
| | 29 | 60 | | GovernanceMetadataBudget activeBudget = budget ?? GovernanceMetadataBudget.Recommended; |
| | 29 | 61 | | IReadOnlyDictionary<string, string> normalizedMetadata = Normalize(metadata); |
| | 29 | 62 | | List<string>? violations = null; |
| | | 63 | | |
| | 29 | 64 | | if (normalizedMetadata.Count > activeBudget.MaxCount) |
| | | 65 | | { |
| | 4 | 66 | | AddViolation( |
| | 4 | 67 | | ref violations, |
| | 4 | 68 | | $"Metadata count {normalizedMetadata.Count} exceeds maximum metadata count {activeBudget.MaxCount}."); |
| | | 69 | | } |
| | | 70 | | |
| | 106 | 71 | | foreach (KeyValuePair<string, string> item in normalizedMetadata) |
| | | 72 | | { |
| | 24 | 73 | | if (item.Key.Length > activeBudget.MaxKeyLength) |
| | | 74 | | { |
| | 2 | 75 | | AddViolation( |
| | 2 | 76 | | ref violations, |
| | 2 | 77 | | $"Metadata key '{item.Key}' length {item.Key.Length} exceeds maximum key length {activeBudget.MaxKey |
| | | 78 | | } |
| | | 79 | | |
| | 24 | 80 | | if (item.Value.Length > activeBudget.MaxValueLength) |
| | | 81 | | { |
| | 6 | 82 | | AddViolation( |
| | 6 | 83 | | ref violations, |
| | 6 | 84 | | $"Metadata value for key '{item.Key}' length {item.Value.Length} exceeds maximum value length {activ |
| | | 85 | | } |
| | | 86 | | |
| | 24 | 87 | | string? reservedFragment = FindReservedKeyFragment(item.Key, activeBudget); |
| | 24 | 88 | | if (reservedFragment is not null) |
| | | 89 | | { |
| | 4 | 90 | | AddViolation( |
| | 4 | 91 | | ref violations, |
| | 4 | 92 | | $"Metadata key '{item.Key}' matches reserved or discouraged key fragment '{reservedFragment}'. Store |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |
| | 29 | 96 | | int estimatedSerializedBytes = EstimateSerializedSizeBytesCore(normalizedMetadata); |
| | 29 | 97 | | if (estimatedSerializedBytes > activeBudget.MaxSerializedBytes) |
| | | 98 | | { |
| | 2 | 99 | | AddViolation( |
| | 2 | 100 | | ref violations, |
| | 2 | 101 | | $"Estimated serialized metadata size {estimatedSerializedBytes} bytes exceeds maximum serialized metadat |
| | | 102 | | } |
| | | 103 | | |
| | 29 | 104 | | return GovernanceMetadataBudgetValidationResult.Create( |
| | 29 | 105 | | normalizedMetadata, |
| | 29 | 106 | | violations is null ? EmptyViolations : Array.AsReadOnly([.. violations]), |
| | 29 | 107 | | estimatedSerializedBytes); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Normalizes and validates metadata, throwing when the supplied metadata exceeds the budget. |
| | | 112 | | /// </summary> |
| | | 113 | | public static IReadOnlyDictionary<string, string> NormalizeAndValidate( |
| | | 114 | | IReadOnlyDictionary<string, string>? metadata, |
| | | 115 | | GovernanceMetadataBudget? budget = null, |
| | | 116 | | string? parameterName = null) |
| | | 117 | | { |
| | 4 | 118 | | GovernanceMetadataBudgetValidationResult result = Validate(metadata, budget); |
| | 4 | 119 | | result.ThrowIfInvalid(parameterName ?? nameof(metadata)); |
| | 2 | 120 | | return result.NormalizedMetadata; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Estimates the UTF-8 serialized size of normalized metadata for budget comparison. |
| | | 125 | | /// </summary> |
| | | 126 | | public static int EstimateSerializedSizeBytes(IReadOnlyDictionary<string, string>? metadata) |
| | | 127 | | { |
| | 2 | 128 | | return EstimateSerializedSizeBytesCore(Normalize(metadata)); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private static int EstimateSerializedSizeBytesCore(IReadOnlyDictionary<string, string> normalizedMetadata) |
| | | 132 | | { |
| | 31 | 133 | | if (normalizedMetadata.Count == 0) |
| | | 134 | | { |
| | 9 | 135 | | return EmptySerializedObjectBytes; |
| | | 136 | | } |
| | | 137 | | |
| | 22 | 138 | | int totalBytes = EmptySerializedObjectBytes; |
| | | 139 | | |
| | 96 | 140 | | foreach (KeyValuePair<string, string> item in normalizedMetadata) |
| | | 141 | | { |
| | 26 | 142 | | totalBytes += SerializedEntryOverheadBytes; |
| | 26 | 143 | | totalBytes += Encoding.UTF8.GetByteCount(item.Key); |
| | 26 | 144 | | totalBytes += Encoding.UTF8.GetByteCount(item.Value); |
| | | 145 | | } |
| | | 146 | | |
| | 22 | 147 | | return totalBytes; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | private static string? FindReservedKeyFragment(string key, GovernanceMetadataBudget budget) |
| | | 151 | | { |
| | 24 | 152 | | if (budget.ReservedKeyFragments.Count == 0) |
| | | 153 | | { |
| | 14 | 154 | | return null; |
| | | 155 | | } |
| | | 156 | | |
| | 10 | 157 | | string normalizedKey = GovernanceMetadataBudget.NormalizeKeyForComparison(key); |
| | | 158 | | |
| | 176 | 159 | | foreach (string reservedFragment in budget.ReservedKeyFragments) |
| | | 160 | | { |
| | 80 | 161 | | if (normalizedKey.Contains(reservedFragment, StringComparison.Ordinal)) |
| | | 162 | | { |
| | 4 | 163 | | return reservedFragment; |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | |
| | 6 | 167 | | return null; |
| | 4 | 168 | | } |
| | | 169 | | |
| | | 170 | | private static void AddViolation(ref List<string>? violations, string violation) |
| | | 171 | | { |
| | 18 | 172 | | violations ??= []; |
| | 18 | 173 | | violations.Add(violation); |
| | 18 | 174 | | } |
| | | 175 | | } |