| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Actors; |
| | | 3 | | using AsiBackbone.Core.Serialization; |
| | | 4 | | using SigningMetadataValue = AsiBackbone.Core.Signing.SigningMetadata; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.Core.Audit; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a persistence-ready audit ledger record captured from AsiBackbone decision receipt. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class AuditLedgerRecord : IDecisionReceipt |
| | | 12 | | { |
| | 4 | 13 | | private static readonly ReadOnlyCollection<string> EmptyReasonCodes = |
| | 4 | 14 | | Array.AsReadOnly(Array.Empty<string>()); |
| | | 15 | | |
| | 4 | 16 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 4 | 17 | | new ReadOnlyDictionary<string, string>( |
| | 4 | 18 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 19 | | |
| | 90 | 20 | | private AuditLedgerRecord( |
| | 90 | 21 | | string recordId, |
| | 90 | 22 | | string? schemaVersion, |
| | 90 | 23 | | string eventId, |
| | 90 | 24 | | string? decisionReceiptId, |
| | 90 | 25 | | DateTimeOffset occurredUtc, |
| | 90 | 26 | | DateTimeOffset recordedUtc, |
| | 90 | 27 | | string actorId, |
| | 90 | 28 | | GovernanceActorType actorType, |
| | 90 | 29 | | string? actorDisplayName, |
| | 90 | 30 | | string operationName, |
| | 90 | 31 | | string outcome, |
| | 90 | 32 | | IReadOnlyList<string> reasonCodes, |
| | 90 | 33 | | string? correlationId, |
| | 90 | 34 | | string? traceId, |
| | 90 | 35 | | string? spanId, |
| | 90 | 36 | | string? parentSpanId, |
| | 90 | 37 | | long? decisionLatencyMs, |
| | 90 | 38 | | string? constraintSetHash, |
| | 90 | 39 | | int? constraintCount, |
| | 90 | 40 | | double? riskScore, |
| | 90 | 41 | | string? policyScope, |
| | 90 | 42 | | string? tenantHash, |
| | 90 | 43 | | string? organizationHash, |
| | 90 | 44 | | string? emitterStatus, |
| | 90 | 45 | | string? emitterProvider, |
| | 90 | 46 | | long? outboxSequence, |
| | 90 | 47 | | string? gatewayExecutionId, |
| | 90 | 48 | | string? decisionStage, |
| | 90 | 49 | | string? policyVersion, |
| | 90 | 50 | | string? policyHash, |
| | 90 | 51 | | string? handshakeId, |
| | 90 | 52 | | string? acknowledgmentId, |
| | 90 | 53 | | string? capabilityTokenId, |
| | 90 | 54 | | string? previousRecordHash, |
| | 90 | 55 | | string? recordHash, |
| | 90 | 56 | | string? signatureKeyId, |
| | 90 | 57 | | string? signatureAlgorithm, |
| | 90 | 58 | | string? signatureValue, |
| | 90 | 59 | | string? signingHash, |
| | 90 | 60 | | string? signatureKeyVersion, |
| | 90 | 61 | | string? signatureProvider, |
| | 90 | 62 | | DateTimeOffset? signedUtc, |
| | 90 | 63 | | IReadOnlyDictionary<string, string> metadata) |
| | | 64 | | { |
| | 90 | 65 | | ArgumentException.ThrowIfNullOrWhiteSpace(recordId); |
| | 90 | 66 | | ArgumentException.ThrowIfNullOrWhiteSpace(eventId); |
| | 89 | 67 | | ArgumentException.ThrowIfNullOrWhiteSpace(actorId); |
| | 88 | 68 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | 87 | 69 | | ArgumentException.ThrowIfNullOrWhiteSpace(outcome); |
| | | 70 | | |
| | 86 | 71 | | RecordId = recordId.Trim(); |
| | 86 | 72 | | SchemaVersion = GovernanceSchemaVersions.Normalize(schemaVersion); |
| | 86 | 73 | | EventId = eventId.Trim(); |
| | 86 | 74 | | DecisionReceiptId = NormalizeOptional(decisionReceiptId) ?? EventId; |
| | 86 | 75 | | OccurredUtc = occurredUtc.ToUniversalTime(); |
| | 86 | 76 | | RecordedUtc = recordedUtc.ToUniversalTime(); |
| | 86 | 77 | | ActorId = actorId.Trim(); |
| | 86 | 78 | | ActorType = actorType; |
| | 86 | 79 | | ActorDisplayName = NormalizeOptional(actorDisplayName); |
| | 86 | 80 | | OperationName = operationName.Trim(); |
| | 86 | 81 | | Outcome = outcome.Trim(); |
| | 86 | 82 | | ReasonCodes = reasonCodes; |
| | 86 | 83 | | CorrelationId = NormalizeOptional(correlationId); |
| | 86 | 84 | | TraceId = NormalizeOptional(traceId); |
| | 86 | 85 | | SpanId = NormalizeOptional(spanId); |
| | 86 | 86 | | ParentSpanId = NormalizeOptional(parentSpanId); |
| | 86 | 87 | | DecisionLatencyMs = NormalizeNonNegative(decisionLatencyMs, nameof(decisionLatencyMs)); |
| | 86 | 88 | | ConstraintSetHash = NormalizeOptional(constraintSetHash); |
| | 86 | 89 | | ConstraintCount = NormalizeNonNegative(constraintCount, nameof(constraintCount)); |
| | 86 | 90 | | RiskScore = NormalizeRiskScore(riskScore); |
| | 86 | 91 | | PolicyScope = NormalizeOptional(policyScope); |
| | 86 | 92 | | TenantHash = NormalizeOptional(tenantHash); |
| | 86 | 93 | | OrganizationHash = NormalizeOptional(organizationHash); |
| | 86 | 94 | | EmitterStatus = NormalizeOptional(emitterStatus); |
| | 86 | 95 | | EmitterProvider = NormalizeOptional(emitterProvider); |
| | 86 | 96 | | OutboxSequence = NormalizeNonNegative(outboxSequence, nameof(outboxSequence)); |
| | 86 | 97 | | GatewayExecutionId = NormalizeOptional(gatewayExecutionId); |
| | 86 | 98 | | DecisionStage = NormalizeOptional(decisionStage); |
| | 86 | 99 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 86 | 100 | | PolicyHash = NormalizeOptional(policyHash); |
| | 86 | 101 | | HandshakeId = NormalizeOptional(handshakeId); |
| | 86 | 102 | | AcknowledgmentId = NormalizeOptional(acknowledgmentId); |
| | 86 | 103 | | CapabilityTokenId = NormalizeOptional(capabilityTokenId); |
| | 86 | 104 | | PreviousRecordHash = NormalizeOptional(previousRecordHash); |
| | 86 | 105 | | RecordHash = NormalizeOptional(recordHash); |
| | 86 | 106 | | SignatureKeyId = NormalizeOptional(signatureKeyId); |
| | 86 | 107 | | SignatureAlgorithm = NormalizeOptional(signatureAlgorithm); |
| | 86 | 108 | | SignatureValue = NormalizeOptional(signatureValue); |
| | 86 | 109 | | SigningHash = NormalizeOptional(signingHash); |
| | 86 | 110 | | SignatureKeyVersion = NormalizeOptional(signatureKeyVersion); |
| | 86 | 111 | | SignatureProvider = NormalizeOptional(signatureProvider); |
| | 86 | 112 | | SignedUtc = signedUtc?.ToUniversalTime(); |
| | 86 | 113 | | SigningMetadata = SigningMetadataValue.Create( |
| | 86 | 114 | | SigningHash, |
| | 86 | 115 | | null, |
| | 86 | 116 | | SignatureValue, |
| | 86 | 117 | | SignatureAlgorithm, |
| | 86 | 118 | | SignatureKeyId, |
| | 86 | 119 | | SignatureKeyVersion, |
| | 86 | 120 | | SignatureProvider, |
| | 86 | 121 | | SignedUtc); |
| | 86 | 122 | | Metadata = metadata; |
| | 86 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Gets the unique identifier of this ledger record. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <remarks> |
| | | 129 | | /// A new identifier is generated when none is supplied to <see cref="FromDecisionReceipt" />. |
| | | 130 | | /// </remarks> |
| | | 131 | | public string RecordId { get; } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Gets the normalized serialized schema version for the record shape. |
| | | 135 | | /// </summary> |
| | | 136 | | public string SchemaVersion { get; } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Gets the stable audit event identifier copied from the source decision receipt. |
| | | 140 | | /// </summary> |
| | | 141 | | public string EventId { get; } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Gets the decision receipt identifier copied from the source receipt. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <remarks> |
| | | 147 | | /// Falls back to <see cref="EventId" /> when the source receipt does not supply a distinct identifier, so this |
| | | 148 | | /// value is never <see langword="null" />. |
| | | 149 | | /// </remarks> |
| | | 150 | | public string DecisionReceiptId { get; } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Gets the UTC timestamp when the audited event occurred. |
| | | 154 | | /// </summary> |
| | | 155 | | public DateTimeOffset OccurredUtc { get; } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Gets the UTC timestamp when this record was captured for the ledger. |
| | | 159 | | /// </summary> |
| | | 160 | | public DateTimeOffset RecordedUtc { get; } |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Gets the stable actor identifier associated with the event. |
| | | 164 | | /// </summary> |
| | | 165 | | public string ActorId { get; } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Gets the actor type associated with the event. |
| | | 169 | | /// </summary> |
| | | 170 | | public GovernanceActorType ActorType { get; } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Gets the optional display name or label associated with the actor. |
| | | 174 | | /// </summary> |
| | | 175 | | public string? ActorDisplayName { get; } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Gets the operation name associated with the audited event. |
| | | 179 | | /// </summary> |
| | | 180 | | public string OperationName { get; } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Gets the governance, constraint, or host-defined outcome associated with the event. |
| | | 184 | | /// </summary> |
| | | 185 | | public string Outcome { get; } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Gets the trimmed, non-empty machine-readable reason codes associated with the event. |
| | | 189 | | /// </summary> |
| | | 190 | | public IReadOnlyList<string> ReasonCodes { get; } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Gets the correlation identifier associated with the event, when supplied by the host. |
| | | 194 | | /// </summary> |
| | | 195 | | public string? CorrelationId { get; } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Gets the trace identifier associated with the event, when supplied by the host. |
| | | 199 | | /// </summary> |
| | | 200 | | public string? TraceId { get; } |
| | | 201 | | |
| | | 202 | | /// <summary> |
| | | 203 | | /// Gets the span identifier associated with the event, when supplied by the host or observability adapter. |
| | | 204 | | /// </summary> |
| | | 205 | | public string? SpanId { get; } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Gets the parent span identifier associated with the event, when supplied by the host or observability adapter. |
| | | 209 | | /// </summary> |
| | | 210 | | public string? ParentSpanId { get; } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Gets the non-negative decision latency in milliseconds, when supplied by the host. |
| | | 214 | | /// </summary> |
| | | 215 | | public long? DecisionLatencyMs { get; } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Gets the hash of the evaluated constraint set, when supplied by the host. |
| | | 219 | | /// </summary> |
| | | 220 | | public string? ConstraintSetHash { get; } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Gets the non-negative number of constraints evaluated for the decision, when supplied by the host. |
| | | 224 | | /// </summary> |
| | | 225 | | public int? ConstraintCount { get; } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Gets the finite, non-negative host-defined risk score associated with the decision, when supplied by the host. |
| | | 229 | | /// </summary> |
| | | 230 | | public double? RiskScore { get; } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Gets the policy scope associated with the decision, when supplied by the host. |
| | | 234 | | /// </summary> |
| | | 235 | | public string? PolicyScope { get; } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Gets the privacy-preserving tenant hash associated with the decision, when supplied by the host. |
| | | 239 | | /// </summary> |
| | | 240 | | public string? TenantHash { get; } |
| | | 241 | | |
| | | 242 | | /// <summary> |
| | | 243 | | /// Gets the privacy-preserving organization hash associated with the decision, when supplied by the host. |
| | | 244 | | /// </summary> |
| | | 245 | | public string? OrganizationHash { get; } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Gets the provider-neutral emitter status, when supplied by the host or outbox provider. |
| | | 249 | | /// </summary> |
| | | 250 | | public string? EmitterStatus { get; } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Gets the provider-neutral emitter provider name, when supplied by the host or outbox provider. |
| | | 254 | | /// </summary> |
| | | 255 | | public string? EmitterProvider { get; } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Gets the non-negative outbox sequence associated with the event, when supplied by the host or outbox provider. |
| | | 259 | | /// </summary> |
| | | 260 | | public long? OutboxSequence { get; } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Gets the gateway execution identifier associated with the event, when supplied by the host or gateway provider. |
| | | 264 | | /// </summary> |
| | | 265 | | public string? GatewayExecutionId { get; } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Gets the provider-neutral decision stage associated with the event, when supplied by the host. |
| | | 269 | | /// </summary> |
| | | 270 | | public string? DecisionStage { get; } |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Gets the policy version associated with the event, when supplied by the host. |
| | | 274 | | /// </summary> |
| | | 275 | | public string? PolicyVersion { get; } |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Gets the policy hash associated with the event, when supplied by the host. |
| | | 279 | | /// </summary> |
| | | 280 | | public string? PolicyHash { get; } |
| | | 281 | | |
| | | 282 | | /// <summary> |
| | | 283 | | /// Gets the liability handshake identifier linked to this record, when supplied. |
| | | 284 | | /// </summary> |
| | | 285 | | public string? HandshakeId { get; } |
| | | 286 | | |
| | | 287 | | /// <summary> |
| | | 288 | | /// Gets the handshake acknowledgment identifier linked to this record, when supplied. |
| | | 289 | | /// </summary> |
| | | 290 | | public string? AcknowledgmentId { get; } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Gets the capability token or grant identifier linked to this record, when supplied. |
| | | 294 | | /// </summary> |
| | | 295 | | public string? CapabilityTokenId { get; } |
| | | 296 | | |
| | | 297 | | /// <inheritdoc /> |
| | 33 | 298 | | string? IDecisionReceipt.CapabilityGrantId => CapabilityTokenId; |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Gets the hash of the preceding ledger record, when the host chains records for tamper evidence. |
| | | 302 | | /// </summary> |
| | | 303 | | public string? PreviousRecordHash { get; } |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Gets the hash of this ledger record, when computed by the host or ledger store. |
| | | 307 | | /// </summary> |
| | | 308 | | public string? RecordHash { get; } |
| | | 309 | | |
| | | 310 | | /// <summary> |
| | | 311 | | /// Gets the hash that was signed or is intended to be signed, when supplied by the host or signing provider. |
| | | 312 | | /// </summary> |
| | | 313 | | public string? SigningHash { get; } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Gets the identifier of the key used to sign the record, when signed. |
| | | 317 | | /// </summary> |
| | | 318 | | public string? SignatureKeyId { get; } |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Gets the version of the key used to sign the record, when supplied. |
| | | 322 | | /// </summary> |
| | | 323 | | public string? SignatureKeyVersion { get; } |
| | | 324 | | |
| | | 325 | | /// <summary> |
| | | 326 | | /// Gets the signature algorithm used to sign the record, when signed. |
| | | 327 | | /// </summary> |
| | | 328 | | public string? SignatureAlgorithm { get; } |
| | | 329 | | |
| | | 330 | | /// <summary> |
| | | 331 | | /// Gets the encoded signature value for the record, when signed. |
| | | 332 | | /// </summary> |
| | | 333 | | public string? SignatureValue { get; } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Gets the name of the signing provider that produced the signature, when supplied. |
| | | 337 | | /// </summary> |
| | | 338 | | public string? SignatureProvider { get; } |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Gets the UTC timestamp when the record was signed, when supplied. |
| | | 342 | | /// </summary> |
| | | 343 | | public DateTimeOffset? SignedUtc { get; } |
| | | 344 | | |
| | | 345 | | /// <summary> |
| | | 346 | | /// Gets the provider-neutral signing metadata assembled from the individual signature properties on this record. |
| | | 347 | | /// </summary> |
| | | 348 | | public SigningMetadataValue SigningMetadata { get; } |
| | | 349 | | |
| | | 350 | | /// <summary> |
| | | 351 | | /// Gets the framework-neutral audit metadata, merged from the source receipt and any additional metadata supplied |
| | | 352 | | /// when the record was created. |
| | | 353 | | /// </summary> |
| | | 354 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Gets a value indicating whether the record carries at least one reason code. |
| | | 358 | | /// </summary> |
| | 6 | 359 | | public bool HasReasonCodes => ReasonCodes.Count > 0; |
| | | 360 | | |
| | | 361 | | /// <summary> |
| | | 362 | | /// Gets a value indicating whether the record carries at least one metadata entry. |
| | | 363 | | /// </summary> |
| | 10 | 364 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 365 | | |
| | | 366 | | /// <summary> |
| | | 367 | | /// Creates a persistence-ready ledger record from a decision receipt, attaching optional handshake, capability, |
| | | 368 | | /// hash-chain, and signature references. |
| | | 369 | | /// </summary> |
| | | 370 | | /// <param name="receipt">The decision receipt to capture.</param> |
| | | 371 | | /// <param name="recordId">The ledger record identifier; a new identifier is generated when omitted or blank.</param |
| | | 372 | | /// <param name="recordedUtc">The capture timestamp; defaults to the current UTC time.</param> |
| | | 373 | | /// <param name="handshakeId">The optional liability handshake identifier to link.</param> |
| | | 374 | | /// <param name="acknowledgmentId">The optional handshake acknowledgment identifier to link.</param> |
| | | 375 | | /// <param name="capabilityTokenId"> |
| | | 376 | | /// The optional capability token or grant identifier to link. When omitted or blank, the receipt's |
| | | 377 | | /// <see cref="IDecisionReceipt.CapabilityGrantId" /> is used. |
| | | 378 | | /// </param> |
| | | 379 | | /// <param name="previousRecordHash">The optional hash of the preceding ledger record.</param> |
| | | 380 | | /// <param name="recordHash">The optional hash of this ledger record.</param> |
| | | 381 | | /// <param name="signatureKeyId">The optional identifier of the signing key.</param> |
| | | 382 | | /// <param name="signatureAlgorithm">The optional signature algorithm.</param> |
| | | 383 | | /// <param name="signatureValue">The optional encoded signature value.</param> |
| | | 384 | | /// <param name="signingHash">The optional hash that was signed.</param> |
| | | 385 | | /// <param name="signatureKeyVersion">The optional version of the signing key.</param> |
| | | 386 | | /// <param name="signatureProvider">The optional name of the signing provider.</param> |
| | | 387 | | /// <param name="signedUtc">The optional UTC timestamp when the record was signed.</param> |
| | | 388 | | /// <param name="metadata">Optional metadata merged over the receipt metadata; entries with the same key win.</param |
| | | 389 | | /// <param name="schemaVersion">The schema version; defaults to the receipt schema version.</param> |
| | | 390 | | /// <returns>A normalized ledger record with optional values trimmed and blank values treated as absent.</returns> |
| | | 391 | | /// <exception cref="ArgumentNullException"><paramref name="receipt" /> is <see langword="null" />.</exception> |
| | | 392 | | /// <exception cref="ArgumentException">A required receipt value, such as the event, actor, operation, or outcome, i |
| | | 393 | | /// <exception cref="ArgumentOutOfRangeException">A receipt latency, constraint count, outbox sequence, or risk scor |
| | | 394 | | public static AuditLedgerRecord FromDecisionReceipt( |
| | | 395 | | IDecisionReceipt receipt, |
| | | 396 | | string? recordId = null, |
| | | 397 | | DateTimeOffset? recordedUtc = null, |
| | | 398 | | string? handshakeId = null, |
| | | 399 | | string? acknowledgmentId = null, |
| | | 400 | | string? capabilityTokenId = null, |
| | | 401 | | string? previousRecordHash = null, |
| | | 402 | | string? recordHash = null, |
| | | 403 | | string? signatureKeyId = null, |
| | | 404 | | string? signatureAlgorithm = null, |
| | | 405 | | string? signatureValue = null, |
| | | 406 | | string? signingHash = null, |
| | | 407 | | string? signatureKeyVersion = null, |
| | | 408 | | string? signatureProvider = null, |
| | | 409 | | DateTimeOffset? signedUtc = null, |
| | | 410 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 411 | | string? schemaVersion = null) |
| | | 412 | | { |
| | 91 | 413 | | ArgumentNullException.ThrowIfNull(receipt); |
| | | 414 | | |
| | 90 | 415 | | return new AuditLedgerRecord( |
| | 90 | 416 | | NormalizeIdentifier(recordId), |
| | 90 | 417 | | schemaVersion ?? receipt.SchemaVersion, |
| | 90 | 418 | | receipt.EventId, |
| | 90 | 419 | | receipt.DecisionReceiptId, |
| | 90 | 420 | | receipt.OccurredUtc, |
| | 90 | 421 | | recordedUtc ?? DateTimeOffset.UtcNow, |
| | 90 | 422 | | receipt.ActorId, |
| | 90 | 423 | | receipt.ActorType, |
| | 90 | 424 | | receipt.ActorDisplayName, |
| | 90 | 425 | | receipt.OperationName, |
| | 90 | 426 | | receipt.Outcome, |
| | 90 | 427 | | NormalizeReasonCodes(receipt.ReasonCodes), |
| | 90 | 428 | | receipt.CorrelationId, |
| | 90 | 429 | | receipt.TraceId, |
| | 90 | 430 | | receipt.SpanId, |
| | 90 | 431 | | receipt.ParentSpanId, |
| | 90 | 432 | | receipt.DecisionLatencyMs, |
| | 90 | 433 | | receipt.ConstraintSetHash, |
| | 90 | 434 | | receipt.ConstraintCount, |
| | 90 | 435 | | receipt.RiskScore, |
| | 90 | 436 | | receipt.PolicyScope, |
| | 90 | 437 | | receipt.TenantHash, |
| | 90 | 438 | | receipt.OrganizationHash, |
| | 90 | 439 | | receipt.EmitterStatus, |
| | 90 | 440 | | receipt.EmitterProvider, |
| | 90 | 441 | | receipt.OutboxSequence, |
| | 90 | 442 | | receipt.GatewayExecutionId, |
| | 90 | 443 | | receipt.DecisionStage, |
| | 90 | 444 | | receipt.PolicyVersion, |
| | 90 | 445 | | receipt.PolicyHash, |
| | 90 | 446 | | handshakeId, |
| | 90 | 447 | | acknowledgmentId, |
| | 90 | 448 | | string.IsNullOrWhiteSpace(capabilityTokenId) ? receipt.CapabilityGrantId : capabilityTokenId, |
| | 90 | 449 | | previousRecordHash, |
| | 90 | 450 | | recordHash, |
| | 90 | 451 | | signatureKeyId, |
| | 90 | 452 | | signatureAlgorithm, |
| | 90 | 453 | | signatureValue, |
| | 90 | 454 | | signingHash, |
| | 90 | 455 | | signatureKeyVersion, |
| | 90 | 456 | | signatureProvider, |
| | 90 | 457 | | signedUtc, |
| | 90 | 458 | | NormalizeMetadata(receipt.Metadata, metadata)); |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | private static string NormalizeIdentifier(string? identifier) |
| | | 462 | | { |
| | 90 | 463 | | return string.IsNullOrWhiteSpace(identifier) |
| | 90 | 464 | | ? Guid.NewGuid().ToString("N") |
| | 90 | 465 | | : identifier.Trim(); |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | private static string? NormalizeOptional(string? value) |
| | | 469 | | { |
| | 2322 | 470 | | return string.IsNullOrWhiteSpace(value) |
| | 2322 | 471 | | ? null |
| | 2322 | 472 | | : value.Trim(); |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | private static long? NormalizeNonNegative(long? value, string parameterName) |
| | | 476 | | { |
| | 172 | 477 | | return value < 0 |
| | 172 | 478 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 172 | 479 | | : value; |
| | | 480 | | } |
| | | 481 | | |
| | | 482 | | private static int? NormalizeNonNegative(int? value, string parameterName) |
| | | 483 | | { |
| | 86 | 484 | | return value < 0 |
| | 86 | 485 | | ? throw new ArgumentOutOfRangeException(parameterName, value, "Value must be greater than or equal to zero." |
| | 86 | 486 | | : value; |
| | | 487 | | } |
| | | 488 | | |
| | | 489 | | private static double? NormalizeRiskScore(double? riskScore) |
| | | 490 | | { |
| | 86 | 491 | | return riskScore is null |
| | 86 | 492 | | ? null |
| | 86 | 493 | | : double.IsNaN(riskScore.Value) || double.IsInfinity(riskScore.Value) || riskScore.Value < 0 |
| | 86 | 494 | | ? throw new ArgumentOutOfRangeException(nameof(riskScore), riskScore, "Risk score must be a finite value gre |
| | 86 | 495 | | : riskScore; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | private static ReadOnlyCollection<string> NormalizeReasonCodes(IEnumerable<string>? reasonCodes) |
| | | 499 | | { |
| | 90 | 500 | | string[] normalizedReasonCodes = reasonCodes? |
| | 90 | 501 | | .Where(reasonCode => !string.IsNullOrWhiteSpace(reasonCode)) |
| | 90 | 502 | | .Select(reasonCode => reasonCode.Trim()) |
| | 90 | 503 | | .ToArray() ?? []; |
| | | 504 | | |
| | 90 | 505 | | return normalizedReasonCodes.Length == 0 |
| | 90 | 506 | | ? EmptyReasonCodes |
| | 90 | 507 | | : Array.AsReadOnly(normalizedReasonCodes); |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 511 | | params IReadOnlyDictionary<string, string>?[] metadataSets) |
| | | 512 | | { |
| | 90 | 513 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 514 | | |
| | 540 | 515 | | foreach (IReadOnlyDictionary<string, string>? metadata in metadataSets) |
| | | 516 | | { |
| | 180 | 517 | | if (metadata is null || metadata.Count == 0) |
| | | 518 | | { |
| | | 519 | | continue; |
| | | 520 | | } |
| | | 521 | | |
| | 228 | 522 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 523 | | { |
| | 70 | 524 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 525 | | { |
| | | 526 | | continue; |
| | | 527 | | } |
| | | 528 | | |
| | 67 | 529 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 530 | | } |
| | | 531 | | } |
| | | 532 | | |
| | 90 | 533 | | return normalizedMetadata.Count == 0 |
| | 90 | 534 | | ? EmptyMetadata |
| | 90 | 535 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 536 | | } |
| | | 537 | | } |