| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Actors; |
| | | 3 | | using AsiBackbone.Core.Constraints; |
| | | 4 | | using AsiBackbone.Core.Decisions; |
| | | 5 | | using AsiBackbone.Core.Serialization; |
| | | 6 | | |
| | | 7 | | namespace AsiBackbone.Core.Audit; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents the framework-neutral audit residue produced by an AsiBackbone operation. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class AuditResidue : IAsiBackboneAuditResidue |
| | | 13 | | { |
| | | 14 | | private const string OutcomeAcknowledgmentRequired = "AcknowledgmentRequired"; |
| | | 15 | | private const string OutcomeAllowed = "Allowed"; |
| | | 16 | | private const string OutcomeDenied = "Denied"; |
| | | 17 | | private const string OutcomeDeferred = "Deferred"; |
| | | 18 | | private const string OutcomeEscalationRecommended = "EscalationRecommended"; |
| | | 19 | | private const string OutcomeNotApplicable = "NotApplicable"; |
| | | 20 | | private const string OutcomeWarning = "Warning"; |
| | | 21 | | |
| | 9 | 22 | | private static readonly ReadOnlyCollection<string> EmptyReasonCodes = |
| | 9 | 23 | | Array.AsReadOnly(Array.Empty<string>()); |
| | | 24 | | |
| | 9 | 25 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 9 | 26 | | new ReadOnlyDictionary<string, string>( |
| | 9 | 27 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 28 | | |
| | 251 | 29 | | private AuditResidue( |
| | 251 | 30 | | string eventId, |
| | 251 | 31 | | string auditResidueId, |
| | 251 | 32 | | string schemaVersion, |
| | 251 | 33 | | DateTimeOffset occurredUtc, |
| | 251 | 34 | | string actorId, |
| | 251 | 35 | | AsiBackboneActorType actorType, |
| | 251 | 36 | | string? actorDisplayName, |
| | 251 | 37 | | string operationName, |
| | 251 | 38 | | string outcome, |
| | 251 | 39 | | IReadOnlyList<string> reasonCodes, |
| | 251 | 40 | | string? correlationId, |
| | 251 | 41 | | string? traceId, |
| | 251 | 42 | | string? spanId, |
| | 251 | 43 | | string? parentSpanId, |
| | 251 | 44 | | long? decisionLatencyMs, |
| | 251 | 45 | | string? constraintSetHash, |
| | 251 | 46 | | int? constraintCount, |
| | 251 | 47 | | double? riskScore, |
| | 251 | 48 | | string? policyScope, |
| | 251 | 49 | | string? tenantHash, |
| | 251 | 50 | | string? organizationHash, |
| | 251 | 51 | | string? emitterStatus, |
| | 251 | 52 | | string? emitterProvider, |
| | 251 | 53 | | long? outboxSequence, |
| | 251 | 54 | | string? gatewayExecutionId, |
| | 251 | 55 | | string? decisionStage, |
| | 251 | 56 | | string? policyVersion, |
| | 251 | 57 | | string? policyHash, |
| | 251 | 58 | | IReadOnlyDictionary<string, string> metadata) |
| | | 59 | | { |
| | 251 | 60 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | 251 | 61 | | ArgumentException.ThrowIfNullOrWhiteSpace(auditResidueId); |
| | 251 | 62 | | ArgumentException.ThrowIfNullOrWhiteSpace(actorId); |
| | 251 | 63 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | 245 | 64 | | ArgumentException.ThrowIfNullOrWhiteSpace(outcome); |
| | | 65 | | |
| | 239 | 66 | | EventId = eventId.Trim(); |
| | 239 | 67 | | AuditResidueId = auditResidueId.Trim(); |
| | 239 | 68 | | SchemaVersion = AsiBackboneSchemaVersions.Normalize(schemaVersion); |
| | 239 | 69 | | OccurredUtc = occurredUtc.ToUniversalTime(); |
| | 239 | 70 | | ActorId = actorId.Trim(); |
| | 239 | 71 | | ActorType = actorType; |
| | 239 | 72 | | ActorDisplayName = NormalizeOptional(actorDisplayName); |
| | 239 | 73 | | OperationName = operationName.Trim(); |
| | 239 | 74 | | Outcome = outcome.Trim(); |
| | 239 | 75 | | ReasonCodes = reasonCodes; |
| | 239 | 76 | | CorrelationId = NormalizeOptional(correlationId); |
| | 239 | 77 | | TraceId = NormalizeOptional(traceId); |
| | 239 | 78 | | SpanId = NormalizeOptional(spanId); |
| | 239 | 79 | | ParentSpanId = NormalizeOptional(parentSpanId); |
| | 239 | 80 | | DecisionLatencyMs = NormalizeNonNegative(decisionLatencyMs, nameof(decisionLatencyMs)); |
| | 233 | 81 | | ConstraintSetHash = NormalizeOptional(constraintSetHash); |
| | 233 | 82 | | ConstraintCount = NormalizeNonNegative(constraintCount, nameof(constraintCount)); |
| | 227 | 83 | | RiskScore = NormalizeRiskScore(riskScore); |
| | 213 | 84 | | PolicyScope = NormalizeOptional(policyScope); |
| | 213 | 85 | | TenantHash = NormalizeOptional(tenantHash); |
| | 213 | 86 | | OrganizationHash = NormalizeOptional(organizationHash); |
| | 213 | 87 | | EmitterStatus = NormalizeOptional(emitterStatus); |
| | 213 | 88 | | EmitterProvider = NormalizeOptional(emitterProvider); |
| | 213 | 89 | | OutboxSequence = NormalizeNonNegative(outboxSequence, nameof(outboxSequence)); |
| | 207 | 90 | | GatewayExecutionId = NormalizeOptional(gatewayExecutionId); |
| | 207 | 91 | | DecisionStage = NormalizeOptional(decisionStage); |
| | 207 | 92 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 207 | 93 | | PolicyHash = NormalizeOptional(policyHash); |
| | 207 | 94 | | Metadata = metadata; |
| | 207 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <inheritdoc /> |
| | 142 | 98 | | public string EventId { get; } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc /> |
| | 114 | 101 | | public string AuditResidueId { get; } |
| | | 102 | | |
| | | 103 | | /// <inheritdoc /> |
| | 106 | 104 | | public string SchemaVersion { get; } |
| | | 105 | | |
| | | 106 | | /// <inheritdoc /> |
| | 108 | 107 | | public DateTimeOffset OccurredUtc { get; } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc /> |
| | 104 | 110 | | public string ActorId { get; } |
| | | 111 | | |
| | | 112 | | /// <inheritdoc /> |
| | 96 | 113 | | public AsiBackboneActorType ActorType { get; } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc /> |
| | 100 | 116 | | public string? ActorDisplayName { get; } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | 112 | 119 | | public string OperationName { get; } |
| | | 120 | | |
| | | 121 | | /// <inheritdoc /> |
| | 140 | 122 | | public string Outcome { get; } |
| | | 123 | | |
| | | 124 | | /// <inheritdoc /> |
| | 194 | 125 | | public IReadOnlyList<string> ReasonCodes { get; } |
| | | 126 | | |
| | | 127 | | /// <inheritdoc /> |
| | 146 | 128 | | public string? CorrelationId { get; } |
| | | 129 | | |
| | | 130 | | /// <inheritdoc /> |
| | 140 | 131 | | public string? TraceId { get; } |
| | | 132 | | |
| | | 133 | | /// <inheritdoc /> |
| | 104 | 134 | | public string? SpanId { get; } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc /> |
| | 104 | 137 | | public string? ParentSpanId { get; } |
| | | 138 | | |
| | | 139 | | /// <inheritdoc /> |
| | 100 | 140 | | public long? DecisionLatencyMs { get; } |
| | | 141 | | |
| | | 142 | | /// <inheritdoc /> |
| | 100 | 143 | | public string? ConstraintSetHash { get; } |
| | | 144 | | |
| | | 145 | | /// <inheritdoc /> |
| | 100 | 146 | | public int? ConstraintCount { get; } |
| | | 147 | | |
| | | 148 | | /// <inheritdoc /> |
| | 100 | 149 | | public double? RiskScore { get; } |
| | | 150 | | |
| | | 151 | | /// <inheritdoc /> |
| | 100 | 152 | | public string? PolicyScope { get; } |
| | | 153 | | |
| | | 154 | | /// <inheritdoc /> |
| | 100 | 155 | | public string? TenantHash { get; } |
| | | 156 | | |
| | | 157 | | /// <inheritdoc /> |
| | 100 | 158 | | public string? OrganizationHash { get; } |
| | | 159 | | |
| | | 160 | | /// <inheritdoc /> |
| | 104 | 161 | | public string? EmitterStatus { get; } |
| | | 162 | | |
| | | 163 | | /// <inheritdoc /> |
| | 104 | 164 | | public string? EmitterProvider { get; } |
| | | 165 | | |
| | | 166 | | /// <inheritdoc /> |
| | 104 | 167 | | public long? OutboxSequence { get; } |
| | | 168 | | |
| | | 169 | | /// <inheritdoc /> |
| | 104 | 170 | | public string? GatewayExecutionId { get; } |
| | | 171 | | |
| | | 172 | | /// <inheritdoc /> |
| | 104 | 173 | | public string? DecisionStage { get; } |
| | | 174 | | |
| | | 175 | | /// <inheritdoc /> |
| | 133 | 176 | | public string? PolicyVersion { get; } |
| | | 177 | | |
| | | 178 | | /// <inheritdoc /> |
| | 131 | 179 | | public string? PolicyHash { get; } |
| | | 180 | | |
| | | 181 | | /// <inheritdoc /> |
| | 203 | 182 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Gets a value indicating whether this audit residue contains reason codes. |
| | | 186 | | /// </summary> |
| | 30 | 187 | | public bool HasReasonCodes => ReasonCodes.Count > 0; |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Gets a value indicating whether this audit residue contains metadata. |
| | | 191 | | /// </summary> |
| | 20 | 192 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Creates audit residue from a host-defined operation outcome. |
| | | 196 | | /// </summary> |
| | | 197 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 198 | | /// <param name="operationName">The operation name.</param> |
| | | 199 | | /// <param name="outcome">The governance, constraint, or host-defined outcome.</param> |
| | | 200 | | /// <param name="reasonCodes">Optional machine-readable reason codes.</param> |
| | | 201 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 202 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 203 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 204 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 205 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 206 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 207 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 208 | | /// <param name="auditResidueId">Optional audit residue identifier. When omitted, the normalized event identifier is |
| | | 209 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 210 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 211 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 212 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 213 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 214 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 215 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 216 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 217 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 218 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 219 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 220 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 221 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 222 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 223 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 224 | | /// <returns>An audit residue value.</returns> |
| | | 225 | | public static AuditResidue Create( |
| | | 226 | | IAsiBackboneActorContext actor, |
| | | 227 | | string operationName, |
| | | 228 | | string outcome, |
| | | 229 | | IEnumerable<string>? reasonCodes = null, |
| | | 230 | | string? eventId = null, |
| | | 231 | | DateTimeOffset? occurredUtc = null, |
| | | 232 | | string? correlationId = null, |
| | | 233 | | string? traceId = null, |
| | | 234 | | string? policyVersion = null, |
| | | 235 | | string? policyHash = null, |
| | | 236 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 237 | | string? auditResidueId = null, |
| | | 238 | | string? spanId = null, |
| | | 239 | | string? parentSpanId = null, |
| | | 240 | | long? decisionLatencyMs = null, |
| | | 241 | | string? constraintSetHash = null, |
| | | 242 | | int? constraintCount = null, |
| | | 243 | | double? riskScore = null, |
| | | 244 | | string? policyScope = null, |
| | | 245 | | string? tenantHash = null, |
| | | 246 | | string? organizationHash = null, |
| | | 247 | | string? emitterStatus = null, |
| | | 248 | | string? emitterProvider = null, |
| | | 249 | | long? outboxSequence = null, |
| | | 250 | | string? gatewayExecutionId = null, |
| | | 251 | | string? decisionStage = null, |
| | | 252 | | string? schemaVersion = null) |
| | | 253 | | { |
| | 213 | 254 | | return CreateCore( |
| | 213 | 255 | | actor, |
| | 213 | 256 | | operationName, |
| | 213 | 257 | | outcome, |
| | 213 | 258 | | NormalizeReasonCodes(reasonCodes), |
| | 213 | 259 | | eventId, |
| | 213 | 260 | | occurredUtc, |
| | 213 | 261 | | correlationId, |
| | 213 | 262 | | traceId, |
| | 213 | 263 | | policyVersion, |
| | 213 | 264 | | policyHash, |
| | 213 | 265 | | metadata, |
| | 213 | 266 | | auditResidueId, |
| | 213 | 267 | | spanId, |
| | 213 | 268 | | parentSpanId, |
| | 213 | 269 | | decisionLatencyMs, |
| | 213 | 270 | | constraintSetHash, |
| | 213 | 271 | | constraintCount, |
| | 213 | 272 | | riskScore, |
| | 213 | 273 | | policyScope, |
| | 213 | 274 | | tenantHash, |
| | 213 | 275 | | organizationHash, |
| | 213 | 276 | | emitterStatus, |
| | 213 | 277 | | emitterProvider, |
| | 213 | 278 | | outboxSequence, |
| | 213 | 279 | | gatewayExecutionId, |
| | 213 | 280 | | decisionStage, |
| | 213 | 281 | | schemaVersion); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | /// <summary> |
| | | 285 | | /// Creates audit residue from a governance decision. |
| | | 286 | | /// </summary> |
| | | 287 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 288 | | /// <param name="operationName">The operation name.</param> |
| | | 289 | | /// <param name="decision">The governance decision to audit.</param> |
| | | 290 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 291 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 292 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 293 | | /// <param name="auditResidueId">Optional audit residue identifier. When omitted, the normalized event identifier is |
| | | 294 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 295 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 296 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 297 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 298 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 299 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 300 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 301 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 302 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 303 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 304 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 305 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 306 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 307 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 308 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 309 | | /// <returns>An audit residue value.</returns> |
| | | 310 | | public static AuditResidue FromDecision( |
| | | 311 | | IAsiBackboneActorContext actor, |
| | | 312 | | string operationName, |
| | | 313 | | GovernanceDecision decision, |
| | | 314 | | string? eventId = null, |
| | | 315 | | DateTimeOffset? occurredUtc = null, |
| | | 316 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 317 | | string? auditResidueId = null, |
| | | 318 | | string? spanId = null, |
| | | 319 | | string? parentSpanId = null, |
| | | 320 | | long? decisionLatencyMs = null, |
| | | 321 | | string? constraintSetHash = null, |
| | | 322 | | int? constraintCount = null, |
| | | 323 | | double? riskScore = null, |
| | | 324 | | string? policyScope = null, |
| | | 325 | | string? tenantHash = null, |
| | | 326 | | string? organizationHash = null, |
| | | 327 | | string? emitterStatus = null, |
| | | 328 | | string? emitterProvider = null, |
| | | 329 | | long? outboxSequence = null, |
| | | 330 | | string? gatewayExecutionId = null, |
| | | 331 | | string? decisionStage = null, |
| | | 332 | | string? schemaVersion = null) |
| | | 333 | | { |
| | 28 | 334 | | ArgumentNullException.ThrowIfNull(decision); |
| | | 335 | | |
| | 26 | 336 | | return CreateCore( |
| | 26 | 337 | | actor, |
| | 26 | 338 | | operationName, |
| | 26 | 339 | | GetOutcomeName(decision.Outcome), |
| | 26 | 340 | | UseTrustedReasonCodes(decision.ReasonCodes), |
| | 26 | 341 | | eventId, |
| | 26 | 342 | | occurredUtc, |
| | 26 | 343 | | decision.CorrelationId, |
| | 26 | 344 | | decision.TraceId, |
| | 26 | 345 | | decision.PolicyVersion, |
| | 26 | 346 | | decision.PolicyHash, |
| | 26 | 347 | | metadata, |
| | 26 | 348 | | auditResidueId, |
| | 26 | 349 | | spanId, |
| | 26 | 350 | | parentSpanId, |
| | 26 | 351 | | decisionLatencyMs, |
| | 26 | 352 | | constraintSetHash, |
| | 26 | 353 | | constraintCount, |
| | 26 | 354 | | riskScore, |
| | 26 | 355 | | policyScope, |
| | 26 | 356 | | tenantHash, |
| | 26 | 357 | | organizationHash, |
| | 26 | 358 | | emitterStatus, |
| | 26 | 359 | | emitterProvider, |
| | 26 | 360 | | outboxSequence, |
| | 26 | 361 | | gatewayExecutionId, |
| | 26 | 362 | | decisionStage, |
| | 26 | 363 | | schemaVersion); |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | /// <summary> |
| | | 367 | | /// Creates audit residue from a constraint evaluation result. |
| | | 368 | | /// </summary> |
| | | 369 | | /// <param name="actor">The actor associated with the operation.</param> |
| | | 370 | | /// <param name="operationName">The operation name.</param> |
| | | 371 | | /// <param name="constraintResult">The constraint evaluation result to audit.</param> |
| | | 372 | | /// <param name="eventId">Optional audit event identifier. When omitted, a new identifier is generated.</param> |
| | | 373 | | /// <param name="occurredUtc">Optional event timestamp. When omitted, the current UTC timestamp is used.</param> |
| | | 374 | | /// <param name="correlationId">Optional correlation identifier.</param> |
| | | 375 | | /// <param name="traceId">Optional trace identifier.</param> |
| | | 376 | | /// <param name="policyVersion">Optional policy version.</param> |
| | | 377 | | /// <param name="policyHash">Optional policy hash.</param> |
| | | 378 | | /// <param name="metadata">Optional host-provided audit metadata.</param> |
| | | 379 | | /// <param name="auditResidueId">Optional audit residue identifier. When omitted, the normalized event identifier is |
| | | 380 | | /// <param name="spanId">Optional span identifier.</param> |
| | | 381 | | /// <param name="parentSpanId">Optional parent span identifier.</param> |
| | | 382 | | /// <param name="decisionLatencyMs">Optional decision latency in milliseconds.</param> |
| | | 383 | | /// <param name="constraintSetHash">Optional evaluated constraint-set hash.</param> |
| | | 384 | | /// <param name="constraintCount">Optional evaluated constraint count.</param> |
| | | 385 | | /// <param name="riskScore">Optional host-defined risk score.</param> |
| | | 386 | | /// <param name="policyScope">Optional host-defined policy scope.</param> |
| | | 387 | | /// <param name="tenantHash">Optional privacy-preserving tenant hash.</param> |
| | | 388 | | /// <param name="organizationHash">Optional privacy-preserving organization hash.</param> |
| | | 389 | | /// <param name="emitterStatus">Optional provider-neutral emitter status.</param> |
| | | 390 | | /// <param name="emitterProvider">Optional provider-neutral emitter provider name.</param> |
| | | 391 | | /// <param name="outboxSequence">Optional outbox sequence.</param> |
| | | 392 | | /// <param name="gatewayExecutionId">Optional gateway execution identifier.</param> |
| | | 393 | | /// <param name="decisionStage">Optional provider-neutral decision stage.</param> |
| | | 394 | | /// <param name="schemaVersion">Optional schema version. When omitted, the stable artifact schema version is used.</ |
| | | 395 | | /// <returns>An audit residue value.</returns> |
| | | 396 | | public static AuditResidue FromConstraint( |
| | | 397 | | IAsiBackboneActorContext actor, |
| | | 398 | | string operationName, |
| | | 399 | | ConstraintEvaluationResult constraintResult, |
| | | 400 | | string? eventId = null, |
| | | 401 | | DateTimeOffset? occurredUtc = null, |
| | | 402 | | string? correlationId = null, |
| | | 403 | | string? traceId = null, |
| | | 404 | | string? policyVersion = null, |
| | | 405 | | string? policyHash = null, |
| | | 406 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 407 | | string? auditResidueId = null, |
| | | 408 | | string? spanId = null, |
| | | 409 | | string? parentSpanId = null, |
| | | 410 | | long? decisionLatencyMs = null, |
| | | 411 | | string? constraintSetHash = null, |
| | | 412 | | int? constraintCount = null, |
| | | 413 | | double? riskScore = null, |
| | | 414 | | string? policyScope = null, |
| | | 415 | | string? tenantHash = null, |
| | | 416 | | string? organizationHash = null, |
| | | 417 | | string? emitterStatus = null, |
| | | 418 | | string? emitterProvider = null, |
| | | 419 | | long? outboxSequence = null, |
| | | 420 | | string? gatewayExecutionId = null, |
| | | 421 | | string? decisionStage = null, |
| | | 422 | | string? schemaVersion = null) |
| | | 423 | | { |
| | 16 | 424 | | ArgumentNullException.ThrowIfNull(constraintResult); |
| | | 425 | | |
| | 14 | 426 | | return CreateCore( |
| | 14 | 427 | | actor, |
| | 14 | 428 | | operationName, |
| | 14 | 429 | | GetOutcomeName(constraintResult.Outcome), |
| | 14 | 430 | | UseTrustedReasonCodes(constraintResult.ReasonCodes), |
| | 14 | 431 | | eventId, |
| | 14 | 432 | | occurredUtc, |
| | 14 | 433 | | correlationId, |
| | 14 | 434 | | traceId, |
| | 14 | 435 | | policyVersion, |
| | 14 | 436 | | policyHash, |
| | 14 | 437 | | metadata, |
| | 14 | 438 | | auditResidueId, |
| | 14 | 439 | | spanId, |
| | 14 | 440 | | parentSpanId, |
| | 14 | 441 | | decisionLatencyMs, |
| | 14 | 442 | | constraintSetHash, |
| | 14 | 443 | | constraintCount, |
| | 14 | 444 | | riskScore, |
| | 14 | 445 | | policyScope, |
| | 14 | 446 | | tenantHash, |
| | 14 | 447 | | organizationHash, |
| | 14 | 448 | | emitterStatus, |
| | 14 | 449 | | emitterProvider, |
| | 14 | 450 | | outboxSequence, |
| | 14 | 451 | | gatewayExecutionId, |
| | 14 | 452 | | decisionStage, |
| | 14 | 453 | | schemaVersion); |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | private static AuditResidue CreateCore( |
| | | 457 | | IAsiBackboneActorContext actor, |
| | | 458 | | string operationName, |
| | | 459 | | string outcome, |
| | | 460 | | IReadOnlyList<string> reasonCodes, |
| | | 461 | | string? eventId, |
| | | 462 | | DateTimeOffset? occurredUtc, |
| | | 463 | | string? correlationId, |
| | | 464 | | string? traceId, |
| | | 465 | | string? policyVersion, |
| | | 466 | | string? policyHash, |
| | | 467 | | IReadOnlyDictionary<string, string>? metadata, |
| | | 468 | | string? auditResidueId, |
| | | 469 | | string? spanId, |
| | | 470 | | string? parentSpanId, |
| | | 471 | | long? decisionLatencyMs, |
| | | 472 | | string? constraintSetHash, |
| | | 473 | | int? constraintCount, |
| | | 474 | | double? riskScore, |
| | | 475 | | string? policyScope, |
| | | 476 | | string? tenantHash, |
| | | 477 | | string? organizationHash, |
| | | 478 | | string? emitterStatus, |
| | | 479 | | string? emitterProvider, |
| | | 480 | | long? outboxSequence, |
| | | 481 | | string? gatewayExecutionId, |
| | | 482 | | string? decisionStage, |
| | | 483 | | string? schemaVersion) |
| | | 484 | | { |
| | 253 | 485 | | ArgumentNullException.ThrowIfNull(actor); |
| | | 486 | | |
| | 251 | 487 | | string normalizedEventId = NormalizeIdentifier(eventId); |
| | | 488 | | |
| | 251 | 489 | | return new AuditResidue( |
| | 251 | 490 | | normalizedEventId, |
| | 251 | 491 | | NormalizeAuditResidueId(auditResidueId, normalizedEventId), |
| | 251 | 492 | | schemaVersion ?? AsiBackboneSchemaVersions.StableArtifactsV1, |
| | 251 | 493 | | occurredUtc ?? DateTimeOffset.UtcNow, |
| | 251 | 494 | | actor.ActorId, |
| | 251 | 495 | | actor.ActorType, |
| | 251 | 496 | | actor.DisplayName, |
| | 251 | 497 | | operationName, |
| | 251 | 498 | | outcome, |
| | 251 | 499 | | reasonCodes, |
| | 251 | 500 | | correlationId, |
| | 251 | 501 | | traceId, |
| | 251 | 502 | | spanId, |
| | 251 | 503 | | parentSpanId, |
| | 251 | 504 | | decisionLatencyMs, |
| | 251 | 505 | | constraintSetHash, |
| | 251 | 506 | | constraintCount, |
| | 251 | 507 | | riskScore, |
| | 251 | 508 | | policyScope, |
| | 251 | 509 | | tenantHash, |
| | 251 | 510 | | organizationHash, |
| | 251 | 511 | | emitterStatus, |
| | 251 | 512 | | emitterProvider, |
| | 251 | 513 | | outboxSequence, |
| | 251 | 514 | | gatewayExecutionId, |
| | 251 | 515 | | decisionStage, |
| | 251 | 516 | | policyVersion, |
| | 251 | 517 | | policyHash, |
| | 251 | 518 | | NormalizeMetadata(metadata)); |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | private static string NormalizeIdentifier(string? identifier) |
| | | 522 | | { |
| | 251 | 523 | | return string.IsNullOrWhiteSpace(identifier) |
| | 251 | 524 | | ? Guid.NewGuid().ToString("N") |
| | 251 | 525 | | : identifier.Trim(); |
| | | 526 | | } |
| | | 527 | | |
| | | 528 | | private static string NormalizeAuditResidueId(string? auditResidueId, string eventId) |
| | | 529 | | { |
| | 251 | 530 | | return string.IsNullOrWhiteSpace(auditResidueId) |
| | 251 | 531 | | ? eventId |
| | 251 | 532 | | : auditResidueId.Trim(); |
| | | 533 | | } |
| | | 534 | | |
| | | 535 | | private static string? NormalizeOptional(string? value) |
| | | 536 | | { |
| | 3321 | 537 | | return string.IsNullOrWhiteSpace(value) |
| | 3321 | 538 | | ? null |
| | 3321 | 539 | | : value.Trim(); |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | private static long? NormalizeNonNegative(long? value, string parameterName) |
| | | 543 | | { |
| | 452 | 544 | | return value < 0 |
| | 452 | 545 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 452 | 546 | | : value; |
| | | 547 | | } |
| | | 548 | | |
| | | 549 | | private static int? NormalizeNonNegative(int? value, string parameterName) |
| | | 550 | | { |
| | 233 | 551 | | return value < 0 |
| | 233 | 552 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 233 | 553 | | : value; |
| | | 554 | | } |
| | | 555 | | |
| | | 556 | | private static double? NormalizeRiskScore(double? riskScore) |
| | | 557 | | { |
| | 227 | 558 | | return riskScore is null |
| | 227 | 559 | | ? null |
| | 227 | 560 | | : double.IsNaN(riskScore.Value) || double.IsInfinity(riskScore.Value) || riskScore.Value < 0 |
| | 227 | 561 | | ? throw new ArgumentOutOfRangeException(nameof(riskScore), riskScore, "Risk score must be a finite value gre |
| | 227 | 562 | | : riskScore; |
| | | 563 | | } |
| | | 564 | | |
| | | 565 | | private static ReadOnlyCollection<string> NormalizeReasonCodes(IEnumerable<string>? reasonCodes) |
| | | 566 | | { |
| | 213 | 567 | | if (reasonCodes is null) |
| | | 568 | | { |
| | 118 | 569 | | return EmptyReasonCodes; |
| | | 570 | | } |
| | | 571 | | |
| | 95 | 572 | | if (reasonCodes is ICollection<string> collection) |
| | | 573 | | { |
| | 91 | 574 | | if (collection.Count == 0) |
| | | 575 | | { |
| | 21 | 576 | | return EmptyReasonCodes; |
| | | 577 | | } |
| | | 578 | | |
| | 70 | 579 | | string[] normalizedReasonCodes = new string[collection.Count]; |
| | 70 | 580 | | int normalizedCount = 0; |
| | | 581 | | |
| | 368 | 582 | | foreach (string? reasonCode in collection) |
| | | 583 | | { |
| | 114 | 584 | | AddNormalizedReasonCode(reasonCode, normalizedReasonCodes, ref normalizedCount); |
| | | 585 | | } |
| | | 586 | | |
| | 70 | 587 | | return CreateReasonCodeCollection(normalizedReasonCodes, normalizedCount); |
| | | 588 | | } |
| | | 589 | | |
| | 4 | 590 | | List<string>? normalizedList = null; |
| | | 591 | | |
| | 32 | 592 | | foreach (string? reasonCode in reasonCodes) |
| | | 593 | | { |
| | 12 | 594 | | if (string.IsNullOrWhiteSpace(reasonCode)) |
| | | 595 | | { |
| | | 596 | | continue; |
| | | 597 | | } |
| | | 598 | | |
| | 4 | 599 | | normalizedList ??= []; |
| | 4 | 600 | | normalizedList.Add(reasonCode.Trim()); |
| | | 601 | | } |
| | | 602 | | |
| | 4 | 603 | | return normalizedList is null || normalizedList.Count == 0 |
| | 4 | 604 | | ? EmptyReasonCodes |
| | 4 | 605 | | : Array.AsReadOnly([.. normalizedList]); |
| | | 606 | | } |
| | | 607 | | |
| | | 608 | | private static IReadOnlyList<string> UseTrustedReasonCodes(IReadOnlyList<string> reasonCodes) |
| | | 609 | | { |
| | 40 | 610 | | return reasonCodes.Count == 0 |
| | 40 | 611 | | ? EmptyReasonCodes |
| | 40 | 612 | | : reasonCodes; |
| | | 613 | | } |
| | | 614 | | |
| | | 615 | | private static void AddNormalizedReasonCode( |
| | | 616 | | string? reasonCode, |
| | | 617 | | string[] normalizedReasonCodes, |
| | | 618 | | ref int normalizedCount) |
| | | 619 | | { |
| | 114 | 620 | | if (string.IsNullOrWhiteSpace(reasonCode)) |
| | | 621 | | { |
| | 20 | 622 | | return; |
| | | 623 | | } |
| | | 624 | | |
| | 94 | 625 | | normalizedReasonCodes[normalizedCount] = reasonCode.Trim(); |
| | 94 | 626 | | normalizedCount++; |
| | 94 | 627 | | } |
| | | 628 | | |
| | | 629 | | private static ReadOnlyCollection<string> CreateReasonCodeCollection( |
| | | 630 | | string[] normalizedReasonCodes, |
| | | 631 | | int normalizedCount) |
| | | 632 | | { |
| | 70 | 633 | | if (normalizedCount == 0) |
| | | 634 | | { |
| | 2 | 635 | | return EmptyReasonCodes; |
| | | 636 | | } |
| | | 637 | | |
| | 68 | 638 | | if (normalizedCount == normalizedReasonCodes.Length) |
| | | 639 | | { |
| | 58 | 640 | | return Array.AsReadOnly(normalizedReasonCodes); |
| | | 641 | | } |
| | | 642 | | |
| | 10 | 643 | | string[] filteredReasonCodes = new string[normalizedCount]; |
| | 10 | 644 | | Array.Copy(normalizedReasonCodes, filteredReasonCodes, normalizedCount); |
| | | 645 | | |
| | 10 | 646 | | return Array.AsReadOnly(filteredReasonCodes); |
| | | 647 | | } |
| | | 648 | | |
| | | 649 | | private static string GetOutcomeName(GovernanceDecisionOutcome outcome) |
| | | 650 | | { |
| | 26 | 651 | | return outcome switch |
| | 26 | 652 | | { |
| | 6 | 653 | | GovernanceDecisionOutcome.Allowed => OutcomeAllowed, |
| | 4 | 654 | | GovernanceDecisionOutcome.Warning => OutcomeWarning, |
| | 8 | 655 | | GovernanceDecisionOutcome.Denied => OutcomeDenied, |
| | 2 | 656 | | GovernanceDecisionOutcome.Deferred => OutcomeDeferred, |
| | 4 | 657 | | GovernanceDecisionOutcome.AcknowledgmentRequired => OutcomeAcknowledgmentRequired, |
| | 2 | 658 | | GovernanceDecisionOutcome.EscalationRecommended => OutcomeEscalationRecommended, |
| | 0 | 659 | | _ => outcome.ToString() |
| | 26 | 660 | | }; |
| | | 661 | | } |
| | | 662 | | |
| | | 663 | | private static string GetOutcomeName(ConstraintEvaluationOutcome outcome) |
| | | 664 | | { |
| | 14 | 665 | | return outcome switch |
| | 14 | 666 | | { |
| | 2 | 667 | | ConstraintEvaluationOutcome.NotApplicable => OutcomeNotApplicable, |
| | 2 | 668 | | ConstraintEvaluationOutcome.Allowed => OutcomeAllowed, |
| | 6 | 669 | | ConstraintEvaluationOutcome.Warning => OutcomeWarning, |
| | 4 | 670 | | ConstraintEvaluationOutcome.Denied => OutcomeDenied, |
| | 0 | 671 | | _ => outcome.ToString() |
| | 14 | 672 | | }; |
| | | 673 | | } |
| | | 674 | | |
| | | 675 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 676 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 677 | | { |
| | 251 | 678 | | if (metadata is null || metadata.Count == 0) |
| | | 679 | | { |
| | 174 | 680 | | return EmptyMetadata; |
| | | 681 | | } |
| | | 682 | | |
| | 77 | 683 | | Dictionary<string, string> normalizedMetadata = new(metadata.Count, StringComparer.Ordinal); |
| | | 684 | | |
| | 404 | 685 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 686 | | { |
| | 125 | 687 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 688 | | { |
| | | 689 | | continue; |
| | | 690 | | } |
| | | 691 | | |
| | 111 | 692 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 693 | | } |
| | | 694 | | |
| | 77 | 695 | | return normalizedMetadata.Count == 0 |
| | 77 | 696 | | ? EmptyMetadata |
| | 77 | 697 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 698 | | } |
| | | 699 | | } |