| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Classification; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents provider-neutral context for resolving DLP or classification failure behavior. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class DlpFailurePolicyContext |
| | | 9 | | { |
| | 1 | 10 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 1 | 11 | | new ReadOnlyDictionary<string, string>( |
| | 1 | 12 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 13 | | |
| | 47 | 14 | | private DlpFailurePolicyContext( |
| | 47 | 15 | | DlpClassificationFailureKind failureKind, |
| | 47 | 16 | | DlpIntentRiskLevel riskLevel, |
| | 47 | 17 | | string? intentCategory, |
| | 47 | 18 | | string? environment, |
| | 47 | 19 | | string? correlationId, |
| | 47 | 20 | | string? traceId, |
| | 47 | 21 | | string? policyVersion, |
| | 47 | 22 | | string? policyHash, |
| | 47 | 23 | | TimeSpan? timeout, |
| | 47 | 24 | | IReadOnlyDictionary<string, string> metadata) |
| | | 25 | | { |
| | 47 | 26 | | if (!Enum.IsDefined(failureKind)) |
| | | 27 | | { |
| | 1 | 28 | | throw new ArgumentOutOfRangeException(nameof(failureKind), failureKind, "DLP failure kind must be defined.") |
| | | 29 | | } |
| | | 30 | | |
| | 46 | 31 | | if (!Enum.IsDefined(riskLevel)) |
| | | 32 | | { |
| | 1 | 33 | | throw new ArgumentOutOfRangeException(nameof(riskLevel), riskLevel, "DLP intent risk level must be defined." |
| | | 34 | | } |
| | | 35 | | |
| | 45 | 36 | | if (timeout < TimeSpan.Zero) |
| | | 37 | | { |
| | 1 | 38 | | throw new ArgumentOutOfRangeException(nameof(timeout), timeout, "Timeout must be greater than or equal to ze |
| | | 39 | | } |
| | | 40 | | |
| | 44 | 41 | | FailureKind = failureKind; |
| | 44 | 42 | | RiskLevel = riskLevel; |
| | 44 | 43 | | IntentCategory = NormalizeOptional(intentCategory); |
| | 44 | 44 | | Environment = NormalizeOptional(environment); |
| | 44 | 45 | | CorrelationId = NormalizeOptional(correlationId); |
| | 44 | 46 | | TraceId = NormalizeOptional(traceId); |
| | 44 | 47 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 44 | 48 | | PolicyHash = NormalizeOptional(policyHash); |
| | 44 | 49 | | Timeout = timeout; |
| | 44 | 50 | | Metadata = metadata; |
| | 44 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the provider-neutral DLP or classification failure kind. |
| | | 55 | | /// </summary> |
| | 114 | 56 | | public DlpClassificationFailureKind FailureKind { get; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets the host-assigned risk level for the intent. |
| | | 60 | | /// </summary> |
| | 72 | 61 | | public DlpIntentRiskLevel RiskLevel { get; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets the host-defined intent category, when supplied. |
| | | 65 | | /// </summary> |
| | 34 | 66 | | public string? IntentCategory { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets the host-defined environment, when supplied. |
| | | 70 | | /// </summary> |
| | 34 | 71 | | public string? Environment { get; } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the correlation identifier associated with the governed flow, when supplied. |
| | | 75 | | /// </summary> |
| | 32 | 76 | | public string? CorrelationId { get; } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Gets the trace identifier associated with the governed flow, when supplied. |
| | | 80 | | /// </summary> |
| | 32 | 81 | | public string? TraceId { get; } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the policy version associated with the governed flow, when supplied. |
| | | 85 | | /// </summary> |
| | 32 | 86 | | public string? PolicyVersion { get; } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the policy hash associated with the governed flow, when supplied. |
| | | 90 | | /// </summary> |
| | 32 | 91 | | public string? PolicyHash { get; } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the timeout value associated with the failure, when applicable. |
| | | 95 | | /// </summary> |
| | 38 | 96 | | public TimeSpan? Timeout { get; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets provider-neutral host metadata associated with the failure. |
| | | 100 | | /// </summary> |
| | 42 | 101 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Gets a value indicating whether timeout context is present. |
| | | 105 | | /// </summary> |
| | 2 | 106 | | public bool HasTimeout => Timeout.HasValue; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Gets a value indicating whether metadata is present. |
| | | 110 | | /// </summary> |
| | 5 | 111 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Creates a provider-neutral DLP or classification failure policy context. |
| | | 115 | | /// </summary> |
| | | 116 | | public static DlpFailurePolicyContext Create( |
| | | 117 | | DlpClassificationFailureKind failureKind, |
| | | 118 | | DlpIntentRiskLevel riskLevel, |
| | | 119 | | string? intentCategory = null, |
| | | 120 | | string? environment = null, |
| | | 121 | | string? correlationId = null, |
| | | 122 | | string? traceId = null, |
| | | 123 | | string? policyVersion = null, |
| | | 124 | | string? policyHash = null, |
| | | 125 | | TimeSpan? timeout = null, |
| | | 126 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 127 | | { |
| | 47 | 128 | | return new DlpFailurePolicyContext( |
| | 47 | 129 | | failureKind, |
| | 47 | 130 | | riskLevel, |
| | 47 | 131 | | intentCategory, |
| | 47 | 132 | | environment, |
| | 47 | 133 | | correlationId, |
| | 47 | 134 | | traceId, |
| | 47 | 135 | | policyVersion, |
| | 47 | 136 | | policyHash, |
| | 47 | 137 | | timeout, |
| | 47 | 138 | | NormalizeMetadata(metadata)); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Creates timeout-specific failure policy context. |
| | | 143 | | /// </summary> |
| | | 144 | | public static DlpFailurePolicyContext TimeoutFailure( |
| | | 145 | | DlpIntentRiskLevel riskLevel, |
| | | 146 | | TimeSpan? timeout = null, |
| | | 147 | | string? intentCategory = null, |
| | | 148 | | string? environment = null, |
| | | 149 | | string? correlationId = null, |
| | | 150 | | string? traceId = null, |
| | | 151 | | string? policyVersion = null, |
| | | 152 | | string? policyHash = null, |
| | | 153 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 154 | | { |
| | 6 | 155 | | return Create( |
| | 6 | 156 | | DlpClassificationFailureKind.Timeout, |
| | 6 | 157 | | riskLevel, |
| | 6 | 158 | | intentCategory, |
| | 6 | 159 | | environment, |
| | 6 | 160 | | correlationId, |
| | 6 | 161 | | traceId, |
| | 6 | 162 | | policyVersion, |
| | 6 | 163 | | policyHash, |
| | 6 | 164 | | timeout, |
| | 6 | 165 | | metadata); |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | private static string? NormalizeOptional(string? value) |
| | | 169 | | { |
| | 264 | 170 | | return string.IsNullOrWhiteSpace(value) |
| | 264 | 171 | | ? null |
| | 264 | 172 | | : value.Trim(); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 176 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 177 | | { |
| | 47 | 178 | | if (metadata is null || metadata.Count == 0) |
| | | 179 | | { |
| | 43 | 180 | | return EmptyMetadata; |
| | | 181 | | } |
| | | 182 | | |
| | 4 | 183 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 184 | | |
| | 24 | 185 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 186 | | { |
| | 8 | 187 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 188 | | { |
| | | 189 | | continue; |
| | | 190 | | } |
| | | 191 | | |
| | 5 | 192 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 193 | | } |
| | | 194 | | |
| | 4 | 195 | | return normalizedMetadata.Count == 0 |
| | 4 | 196 | | ? EmptyMetadata |
| | 4 | 197 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 198 | | } |
| | | 199 | | } |