| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Constraints; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.AspNetCore.Correlation; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents framework-neutral request correlation data resolved from the current ASP.NET Core HTTP request. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Initializes a new instance of the <see cref="GovernanceHttpRequestCorrelation" /> class. |
| | | 11 | | /// </remarks> |
| | | 12 | | /// <param name="correlationId">The resolved correlation identifier, when available.</param> |
| | | 13 | | /// <param name="traceId">The resolved trace identifier, when available.</param> |
| | | 14 | | /// <param name="metadata">Safe request metadata resolved from the host.</param> |
| | 89 | 15 | | public sealed class GovernanceHttpRequestCorrelation( |
| | 89 | 16 | | string? correlationId = null, |
| | 89 | 17 | | string? traceId = null, |
| | 89 | 18 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 19 | | { |
| | 1 | 20 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 1 | 21 | | new ReadOnlyDictionary<string, string>( |
| | 1 | 22 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the request correlation identifier, when supplied by the host or propagated request headers. |
| | | 26 | | /// </summary> |
| | | 27 | | public string? CorrelationId { get; } = NormalizeOptional(correlationId); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the request trace identifier, when supplied by the host or current activity. |
| | | 31 | | /// </summary> |
| | | 32 | | public string? TraceId { get; } = NormalizeOptional(traceId); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets safe request metadata supplied by the ASP.NET Core adapter. |
| | | 36 | | /// </summary> |
| | | 37 | | public IReadOnlyDictionary<string, string> Metadata { get; } = NormalizeMetadata(metadata); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets a value indicating whether safe request metadata is available. |
| | | 41 | | /// </summary> |
| | 1 | 42 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates a framework-neutral constraint evaluation context from the resolved request correlation data. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 48 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 49 | | /// <param name="metadata">Optional host-provided metadata to merge with safe request metadata.</param> |
| | | 50 | | /// <param name="mergeRequestMetadata"> |
| | | 51 | | /// When <see langword="true" /> (the default), <see cref="Metadata" /> is merged underneath |
| | | 52 | | /// <paramref name="metadata" />. Pass <see langword="false" /> when <paramref name="metadata" /> already contains |
| | | 53 | | /// request metadata and is authoritative — for example after sanitization, where re-merging would reintroduce |
| | | 54 | | /// entries the sanitizer removed. <paramref name="metadata" /> is still key-normalized either way. |
| | | 55 | | /// </param> |
| | | 56 | | /// <returns>A framework-neutral constraint evaluation context.</returns> |
| | | 57 | | /// <remarks> |
| | | 58 | | /// Merging layers <paramref name="metadata" /> over <see cref="Metadata" /> by key, so a caller that rewrites a |
| | | 59 | | /// request-derived value overrides it, but a caller that <em>removes</em> one does not: with nothing to override |
| | | 60 | | /// the raw entry, the merge puts it back. Sanitized metadata must therefore be passed with |
| | | 61 | | /// <paramref name="mergeRequestMetadata" /> set to <see langword="false" />. |
| | | 62 | | /// </remarks> |
| | | 63 | | public GovernanceEvaluationContext ToEvaluationContext( |
| | | 64 | | string? policyVersion = null, |
| | | 65 | | string? policyHash = null, |
| | | 66 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 67 | | bool mergeRequestMetadata = true) |
| | | 68 | | { |
| | 53 | 69 | | return new GovernanceEvaluationContext( |
| | 53 | 70 | | CorrelationId, |
| | 53 | 71 | | policyVersion, |
| | 53 | 72 | | policyHash, |
| | 53 | 73 | | mergeRequestMetadata ? MergeMetadata(metadata) : NormalizeSuppliedMetadata(metadata), |
| | 53 | 74 | | TraceId); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Normalizes supplied metadata keys and values without merging request metadata underneath them. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="metadata">The metadata to normalize.</param> |
| | | 81 | | /// <returns>A normalized metadata dictionary containing only the supplied entries.</returns> |
| | | 82 | | private static IReadOnlyDictionary<string, string> NormalizeSuppliedMetadata( |
| | | 83 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 84 | | { |
| | 50 | 85 | | if (metadata is null || metadata.Count == 0) |
| | | 86 | | { |
| | 2 | 87 | | return EmptyMetadata; |
| | | 88 | | } |
| | | 89 | | |
| | 48 | 90 | | Dictionary<string, string> normalized = new(StringComparer.Ordinal); |
| | | 91 | | |
| | 588 | 92 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 93 | | { |
| | 246 | 94 | | AddIfValid(normalized, item.Key, item.Value); |
| | | 95 | | } |
| | | 96 | | |
| | 48 | 97 | | return normalized.Count == 0 |
| | 48 | 98 | | ? EmptyMetadata |
| | 48 | 99 | | : new ReadOnlyDictionary<string, string>(normalized); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Merges safe request metadata with host-provided metadata. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="metadata">Optional host-provided metadata.</param> |
| | | 106 | | /// <returns>A normalized metadata dictionary.</returns> |
| | | 107 | | public IReadOnlyDictionary<string, string> MergeMetadata(IReadOnlyDictionary<string, string>? metadata) |
| | | 108 | | { |
| | 62 | 109 | | if ((metadata is null || metadata.Count == 0) && Metadata.Count == 0) |
| | | 110 | | { |
| | 11 | 111 | | return EmptyMetadata; |
| | | 112 | | } |
| | | 113 | | |
| | 51 | 114 | | Dictionary<string, string> merged = new(StringComparer.Ordinal); |
| | | 115 | | |
| | 228 | 116 | | foreach (KeyValuePair<string, string> item in Metadata) |
| | | 117 | | { |
| | 63 | 118 | | AddIfValid(merged, item.Key, item.Value); |
| | | 119 | | } |
| | | 120 | | |
| | 51 | 121 | | if (metadata is not null) |
| | | 122 | | { |
| | 494 | 123 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 124 | | { |
| | 197 | 125 | | AddIfValid(merged, item.Key, item.Value); |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 51 | 129 | | return merged.Count == 0 |
| | 51 | 130 | | ? EmptyMetadata |
| | 51 | 131 | | : new ReadOnlyDictionary<string, string>(merged); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private static string? NormalizeOptional(string? value) |
| | | 135 | | { |
| | 178 | 136 | | return string.IsNullOrWhiteSpace(value) |
| | 178 | 137 | | ? null |
| | 178 | 138 | | : value.Trim(); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 142 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 143 | | { |
| | 89 | 144 | | if (metadata is null || metadata.Count == 0) |
| | | 145 | | { |
| | 12 | 146 | | return EmptyMetadata; |
| | | 147 | | } |
| | | 148 | | |
| | 77 | 149 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 150 | | |
| | 352 | 151 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 152 | | { |
| | 99 | 153 | | AddIfValid(normalizedMetadata, item.Key, item.Value); |
| | | 154 | | } |
| | | 155 | | |
| | 77 | 156 | | return normalizedMetadata.Count == 0 |
| | 77 | 157 | | ? EmptyMetadata |
| | 77 | 158 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private static void AddIfValid(Dictionary<string, string> metadata, string key, string? value) |
| | | 162 | | { |
| | 605 | 163 | | if (string.IsNullOrWhiteSpace(key)) |
| | | 164 | | { |
| | 3 | 165 | | return; |
| | | 166 | | } |
| | | 167 | | |
| | 602 | 168 | | metadata[key.Trim()] = value?.Trim() ?? string.Empty; |
| | 602 | 169 | | } |
| | | 170 | | } |