| | | 1 | | using System.Globalization; |
| | | 2 | | using AsiBackbone.Core.Audit; |
| | | 3 | | using AsiBackbone.Core.Emissions; |
| | | 4 | | using AsiBackbone.Core.Outbox; |
| | | 5 | | using AsiBackbone.Core.Serialization; |
| | | 6 | | |
| | | 7 | | namespace AsiBackbone.Core.Signing; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Builds deterministic, provider-neutral signing payloads for AsiBackbone governance artifacts. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class CanonicalPayloadBuilder |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Builds a canonical payload for audit residue. |
| | | 16 | | /// </summary> |
| | | 17 | | public static CanonicalPayload ForAuditResidue(IAsiBackboneAuditResidue residue, CanonicalPayloadOptions? options = |
| | | 18 | | { |
| | 2 | 19 | | ArgumentNullException.ThrowIfNull(residue); |
| | 2 | 20 | | CanonicalPayloadOptions effectiveOptions = options ?? CanonicalPayloadOptions.Default; |
| | 2 | 21 | | string auditResidueId = GetAuditResidueId(residue); |
| | | 22 | | |
| | 2 | 23 | | return CanonicalPayload.Create( |
| | 2 | 24 | | CanonicalArtifactTypes.AuditResidue, |
| | 2 | 25 | | auditResidueId, |
| | 2 | 26 | | residue.SchemaVersion, |
| | 2 | 27 | | effectiveOptions.CanonicalizationVersion, |
| | 2 | 28 | | BuildAuditResidueContent(residue, effectiveOptions, auditResidueId)); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Builds a canonical payload for a persistence-ready audit ledger record. |
| | | 33 | | /// </summary> |
| | | 34 | | public static CanonicalPayload ForAuditLedgerRecord(AuditLedgerRecord record, CanonicalPayloadOptions? options = nul |
| | | 35 | | { |
| | 11 | 36 | | ArgumentNullException.ThrowIfNull(record); |
| | 11 | 37 | | CanonicalPayloadOptions effectiveOptions = options ?? CanonicalPayloadOptions.Default; |
| | | 38 | | |
| | 11 | 39 | | SortedDictionary<string, object?> content = BuildAuditResidueContent(record, effectiveOptions, record.AuditResid |
| | 11 | 40 | | content["acknowledgmentId"] = record.AcknowledgmentId; |
| | 11 | 41 | | content["capabilityGrantId"] = record.CapabilityTokenId; |
| | 11 | 42 | | content["handshakeId"] = record.HandshakeId; |
| | 11 | 43 | | content["previousRecordHash"] = record.PreviousRecordHash; |
| | 11 | 44 | | content["recordedUtc"] = FormatUtc(record.RecordedUtc); |
| | 11 | 45 | | content["recordId"] = record.RecordId; |
| | | 46 | | |
| | 11 | 47 | | return CanonicalPayload.Create( |
| | 11 | 48 | | CanonicalArtifactTypes.AuditLedgerRecord, |
| | 11 | 49 | | record.RecordId, |
| | 11 | 50 | | record.SchemaVersion, |
| | 11 | 51 | | effectiveOptions.CanonicalizationVersion, |
| | 11 | 52 | | content); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Builds a canonical payload for an audit residue lifecycle event. |
| | | 57 | | /// </summary> |
| | | 58 | | public static CanonicalPayload ForAuditResidueLifecycleEvent(AuditResidueLifecycleEvent lifecycleEvent, CanonicalPay |
| | | 59 | | { |
| | 2 | 60 | | ArgumentNullException.ThrowIfNull(lifecycleEvent); |
| | 2 | 61 | | CanonicalPayloadOptions effectiveOptions = options ?? CanonicalPayloadOptions.Default; |
| | | 62 | | |
| | 2 | 63 | | SortedDictionary<string, object?> content = new(StringComparer.Ordinal) |
| | 2 | 64 | | { |
| | 2 | 65 | | ["auditResidueId"] = lifecycleEvent.AuditResidueId, |
| | 2 | 66 | | ["correlationId"] = lifecycleEvent.CorrelationId, |
| | 2 | 67 | | ["eventId"] = lifecycleEvent.EventId, |
| | 2 | 68 | | ["metadata"] = FilterMetadata(lifecycleEvent.Metadata, effectiveOptions), |
| | 2 | 69 | | ["occurredUtc"] = FormatUtc(lifecycleEvent.OccurredUtc), |
| | 2 | 70 | | ["operationName"] = lifecycleEvent.OperationName, |
| | 2 | 71 | | ["outcome"] = lifecycleEvent.Outcome, |
| | 2 | 72 | | ["stage"] = lifecycleEvent.Stage.ToString(), |
| | 2 | 73 | | ["stageSequence"] = lifecycleEvent.StageSequence, |
| | 2 | 74 | | ["traceId"] = lifecycleEvent.TraceId |
| | 2 | 75 | | }; |
| | | 76 | | |
| | 2 | 77 | | return CanonicalPayload.Create( |
| | 2 | 78 | | CanonicalArtifactTypes.AuditResidueLifecycleEvent, |
| | 2 | 79 | | lifecycleEvent.EventId, |
| | 2 | 80 | | AsiBackboneSchemaVersions.StableArtifactsV1, |
| | 2 | 81 | | effectiveOptions.CanonicalizationVersion, |
| | 2 | 82 | | content); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Builds a canonical payload for a governance emission envelope. |
| | | 87 | | /// </summary> |
| | | 88 | | public static CanonicalPayload ForGovernanceEmissionEnvelope(GovernanceEmissionEnvelope envelope, CanonicalPayloadOp |
| | | 89 | | { |
| | 7 | 90 | | ArgumentNullException.ThrowIfNull(envelope); |
| | 7 | 91 | | CanonicalPayloadOptions effectiveOptions = options ?? CanonicalPayloadOptions.Default; |
| | | 92 | | |
| | 7 | 93 | | return CanonicalPayload.Create( |
| | 7 | 94 | | CanonicalArtifactTypes.GovernanceEmissionEnvelope, |
| | 7 | 95 | | envelope.EnvelopeId, |
| | 7 | 96 | | envelope.SchemaVersion, |
| | 7 | 97 | | effectiveOptions.CanonicalizationVersion, |
| | 7 | 98 | | BuildGovernanceEmissionEnvelopeContent(envelope, effectiveOptions)); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Builds a canonical payload for a durable governance outbox entry. |
| | | 103 | | /// </summary> |
| | | 104 | | public static CanonicalPayload ForGovernanceOutboxEntry(GovernanceOutboxEntry entry, CanonicalPayloadOptions? option |
| | | 105 | | { |
| | 7 | 106 | | ArgumentNullException.ThrowIfNull(entry); |
| | 7 | 107 | | CanonicalPayloadOptions effectiveOptions = options ?? CanonicalPayloadOptions.Default; |
| | | 108 | | |
| | 7 | 109 | | SortedDictionary<string, object?> content = new(StringComparer.Ordinal) |
| | 7 | 110 | | { |
| | 7 | 111 | | ["createdUtc"] = FormatUtc(entry.CreatedUtc), |
| | 7 | 112 | | ["deadLetterReason"] = entry.DeadLetterReason, |
| | 7 | 113 | | ["envelope"] = BuildGovernanceEmissionEnvelopeContent(entry.Envelope, effectiveOptions), |
| | 7 | 114 | | ["lastError"] = BuildGovernanceEmissionErrorContent(entry.LastError), |
| | 7 | 115 | | ["maxRetryCount"] = entry.MaxRetryCount, |
| | 7 | 116 | | ["metadata"] = FilterMetadata(entry.Metadata, effectiveOptions), |
| | 7 | 117 | | ["nextRetryUtc"] = FormatUtc(entry.NextRetryUtc), |
| | 7 | 118 | | ["outboxEntryId"] = entry.OutboxEntryId, |
| | 7 | 119 | | ["providerName"] = entry.ProviderName, |
| | 7 | 120 | | ["providerRecordId"] = entry.ProviderRecordId, |
| | 7 | 121 | | ["retryCount"] = entry.RetryCount, |
| | 7 | 122 | | ["status"] = entry.Status.ToString(), |
| | 7 | 123 | | ["updatedUtc"] = FormatUtc(entry.UpdatedUtc) |
| | 7 | 124 | | }; |
| | | 125 | | |
| | 7 | 126 | | return CanonicalPayload.Create( |
| | 7 | 127 | | CanonicalArtifactTypes.GovernanceOutboxEntry, |
| | 7 | 128 | | entry.OutboxEntryId, |
| | 7 | 129 | | entry.Envelope.SchemaVersion, |
| | 7 | 130 | | effectiveOptions.CanonicalizationVersion, |
| | 7 | 131 | | content); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private static SortedDictionary<string, object?> BuildAuditResidueContent( |
| | | 135 | | IAsiBackboneAuditResidue residue, |
| | | 136 | | CanonicalPayloadOptions options, |
| | | 137 | | string auditResidueId) |
| | | 138 | | { |
| | 13 | 139 | | return new SortedDictionary<string, object?>(StringComparer.Ordinal) |
| | 13 | 140 | | { |
| | 13 | 141 | | ["actorDisplayName"] = residue.ActorDisplayName, |
| | 13 | 142 | | ["actorId"] = residue.ActorId, |
| | 13 | 143 | | ["actorType"] = residue.ActorType.ToString(), |
| | 13 | 144 | | ["auditResidueId"] = auditResidueId, |
| | 13 | 145 | | ["constraintCount"] = residue.ConstraintCount, |
| | 13 | 146 | | ["constraintSetHash"] = residue.ConstraintSetHash, |
| | 13 | 147 | | ["correlationId"] = residue.CorrelationId, |
| | 13 | 148 | | ["decisionLatencyMs"] = residue.DecisionLatencyMs, |
| | 13 | 149 | | ["decisionStage"] = residue.DecisionStage, |
| | 13 | 150 | | ["emitterProvider"] = residue.EmitterProvider, |
| | 13 | 151 | | ["emitterStatus"] = residue.EmitterStatus, |
| | 13 | 152 | | ["eventId"] = residue.EventId, |
| | 13 | 153 | | ["gatewayExecutionId"] = residue.GatewayExecutionId, |
| | 13 | 154 | | ["metadata"] = FilterMetadata(residue.Metadata, options), |
| | 13 | 155 | | ["occurredUtc"] = FormatUtc(residue.OccurredUtc), |
| | 13 | 156 | | ["operationName"] = residue.OperationName, |
| | 13 | 157 | | ["organizationHash"] = residue.OrganizationHash, |
| | 13 | 158 | | ["outboxSequence"] = residue.OutboxSequence, |
| | 13 | 159 | | ["outcome"] = residue.Outcome, |
| | 13 | 160 | | ["parentSpanId"] = residue.ParentSpanId, |
| | 13 | 161 | | ["policyHash"] = residue.PolicyHash, |
| | 13 | 162 | | ["policyScope"] = residue.PolicyScope, |
| | 13 | 163 | | ["policyVersion"] = residue.PolicyVersion, |
| | 13 | 164 | | ["reasonCodes"] = NormalizeStringSet(residue.ReasonCodes), |
| | 13 | 165 | | ["riskScore"] = residue.RiskScore, |
| | 13 | 166 | | ["schemaVersion"] = residue.SchemaVersion, |
| | 13 | 167 | | ["spanId"] = residue.SpanId, |
| | 13 | 168 | | ["tenantHash"] = residue.TenantHash, |
| | 13 | 169 | | ["traceId"] = residue.TraceId |
| | 13 | 170 | | }; |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | private static SortedDictionary<string, object?> BuildGovernanceEmissionEnvelopeContent(GovernanceEmissionEnvelope e |
| | | 174 | | { |
| | 14 | 175 | | return new SortedDictionary<string, object?>(StringComparer.Ordinal) |
| | 14 | 176 | | { |
| | 14 | 177 | | ["actorId"] = envelope.ActorId, |
| | 14 | 178 | | ["auditResidueId"] = envelope.AuditResidueId, |
| | 14 | 179 | | ["correlationId"] = envelope.CorrelationId, |
| | 14 | 180 | | ["createdUtc"] = FormatUtc(envelope.CreatedUtc), |
| | 14 | 181 | | ["decisionStage"] = envelope.DecisionStage, |
| | 14 | 182 | | ["emitterProvider"] = envelope.EmitterProvider, |
| | 14 | 183 | | ["emitterStatus"] = envelope.EmitterStatus, |
| | 14 | 184 | | ["envelopeId"] = envelope.EnvelopeId, |
| | 14 | 185 | | ["eventId"] = envelope.EventId, |
| | 14 | 186 | | ["eventType"] = envelope.EventType.ToString(), |
| | 14 | 187 | | ["gatewayExecutionId"] = envelope.GatewayExecutionId, |
| | 14 | 188 | | ["lifecycleStage"] = envelope.LifecycleStage?.ToString(), |
| | 14 | 189 | | ["lifecycleStageSequence"] = envelope.LifecycleStageSequence, |
| | 14 | 190 | | ["metadata"] = FilterMetadata(envelope.Metadata, options), |
| | 14 | 191 | | ["occurredUtc"] = FormatUtc(envelope.OccurredUtc), |
| | 14 | 192 | | ["operationName"] = envelope.OperationName, |
| | 14 | 193 | | ["outboxSequence"] = envelope.OutboxSequence, |
| | 14 | 194 | | ["outcome"] = envelope.Outcome, |
| | 14 | 195 | | ["parentSpanId"] = envelope.ParentSpanId, |
| | 14 | 196 | | ["payload"] = BuildGovernanceEmissionPayloadContent(envelope.Payload, options), |
| | 14 | 197 | | ["policyHash"] = envelope.PolicyHash, |
| | 14 | 198 | | ["policyVersion"] = envelope.PolicyVersion, |
| | 14 | 199 | | ["schemaVersion"] = envelope.SchemaVersion, |
| | 14 | 200 | | ["spanId"] = envelope.SpanId, |
| | 14 | 201 | | ["traceId"] = envelope.TraceId |
| | 14 | 202 | | }; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | private static SortedDictionary<string, object?>? BuildGovernanceEmissionPayloadContent(GovernanceEmissionPayload? p |
| | | 206 | | { |
| | 14 | 207 | | return payload is null |
| | 14 | 208 | | ? null |
| | 14 | 209 | | : new SortedDictionary<string, object?>(StringComparer.Ordinal) |
| | 14 | 210 | | { |
| | 14 | 211 | | ["contentHash"] = payload.ContentHash, |
| | 14 | 212 | | ["contentType"] = payload.ContentType, |
| | 14 | 213 | | ["metadata"] = FilterMetadata(payload.Metadata, options), |
| | 14 | 214 | | ["payloadType"] = payload.PayloadType, |
| | 14 | 215 | | ["schemaVersion"] = payload.SchemaVersion, |
| | 14 | 216 | | ["sizeBytes"] = payload.SizeBytes |
| | 14 | 217 | | }; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | private static SortedDictionary<string, object?>? BuildGovernanceEmissionErrorContent(GovernanceEmissionError? error |
| | | 221 | | { |
| | 7 | 222 | | return error is null |
| | 7 | 223 | | ? null |
| | 7 | 224 | | : new SortedDictionary<string, object?>(StringComparer.Ordinal) |
| | 7 | 225 | | { |
| | 7 | 226 | | ["code"] = error.Code, |
| | 7 | 227 | | ["isRetryable"] = error.IsRetryable, |
| | 7 | 228 | | ["message"] = error.Message, |
| | 7 | 229 | | ["providerErrorCode"] = error.ProviderErrorCode, |
| | 7 | 230 | | ["providerName"] = error.ProviderName |
| | 7 | 231 | | }; |
| | | 232 | | } |
| | | 233 | | |
| | | 234 | | private static SortedDictionary<string, object?> FilterMetadata(IReadOnlyDictionary<string, string>? metadata, Canon |
| | | 235 | | { |
| | 46 | 236 | | SortedDictionary<string, object?> filteredMetadata = new(StringComparer.Ordinal); |
| | | 237 | | |
| | 46 | 238 | | if (metadata is null || metadata.Count == 0) |
| | | 239 | | { |
| | 24 | 240 | | return filteredMetadata; |
| | | 241 | | } |
| | | 242 | | |
| | 124 | 243 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 244 | | { |
| | 40 | 245 | | if (!options.AllowsMetadataKey(item.Key)) |
| | | 246 | | { |
| | | 247 | | continue; |
| | | 248 | | } |
| | | 249 | | |
| | 23 | 250 | | filteredMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 251 | | } |
| | | 252 | | |
| | 22 | 253 | | return filteredMetadata; |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | private static string[] NormalizeStringSet(IEnumerable<string> values) |
| | | 257 | | { |
| | 13 | 258 | | return [.. values |
| | 22 | 259 | | .Where(value => !string.IsNullOrWhiteSpace(value)) |
| | 21 | 260 | | .Select(value => value.Trim()) |
| | 13 | 261 | | .Distinct(StringComparer.Ordinal) |
| | 23 | 262 | | .OrderBy(value => value, StringComparer.Ordinal)]; |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | private static string? FormatUtc(DateTimeOffset? timestamp) |
| | | 266 | | { |
| | 7 | 267 | | return timestamp.HasValue ? FormatUtc(timestamp.Value) : null; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | private static string FormatUtc(DateTimeOffset timestamp) |
| | | 271 | | { |
| | 70 | 272 | | return timestamp.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'", CultureInfo.InvariantCulture); |
| | | 273 | | } |
| | | 274 | | |
| | | 275 | | private static string GetAuditResidueId(IAsiBackboneAuditResidue residue) |
| | | 276 | | { |
| | 2 | 277 | | return string.IsNullOrWhiteSpace(residue.AuditResidueId) |
| | 2 | 278 | | ? residue.EventId |
| | 2 | 279 | | : residue.AuditResidueId; |
| | | 280 | | } |
| | | 281 | | } |