| | | 1 | | using AsiBackbone.AspNetCore.Acknowledgments; |
| | | 2 | | using AsiBackbone.AspNetCore.Actors; |
| | | 3 | | using AsiBackbone.AspNetCore.Correlation; |
| | | 4 | | using AsiBackbone.AspNetCore.Results; |
| | | 5 | | using AsiBackbone.Core.Actors; |
| | | 6 | | using AsiBackbone.Core.Audit; |
| | | 7 | | using AsiBackbone.Core.Constraints; |
| | | 8 | | using AsiBackbone.Core.Decisions; |
| | | 9 | | using AsiBackbone.Core.Evaluation; |
| | | 10 | | using AsiBackbone.Core.Metadata; |
| | | 11 | | using AsiBackbone.Core.Results; |
| | | 12 | | using Microsoft.AspNetCore.Http; |
| | | 13 | | using Microsoft.Extensions.DependencyInjection; |
| | | 14 | | using Microsoft.Extensions.Options; |
| | | 15 | | |
| | | 16 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Default host-adapter implementation for ergonomic ASP.NET Core endpoint governance. |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class DefaultEndpointGovernanceService : IEndpointGovernanceService |
| | | 22 | | { |
| | | 23 | | private const string MetadataSanitizationDeniedReasonCode = "endpoint.metadata_sanitization.denied"; |
| | | 24 | | private const string MetadataSanitizationDeniedReasonMessage = |
| | | 25 | | "Endpoint governance metadata was denied by the configured sanitation policy."; |
| | | 26 | | |
| | | 27 | | private readonly IServiceProvider serviceProvider; |
| | | 28 | | private readonly IHttpGovernanceActorContextResolver actorContextResolver; |
| | | 29 | | private readonly IHttpGovernanceRequestCorrelationResolver requestCorrelationResolver; |
| | | 30 | | private readonly IAcknowledgmentChallengeService acknowledgmentChallengeService; |
| | | 31 | | private readonly EndpointGovernanceOptions endpointOptions; |
| | | 32 | | private readonly GovernanceHttpResultMappingOptions resultOptions; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="DefaultEndpointGovernanceService" /> class. |
| | | 36 | | /// </summary> |
| | 58 | 37 | | public DefaultEndpointGovernanceService( |
| | 58 | 38 | | IServiceProvider serviceProvider, |
| | 58 | 39 | | IHttpGovernanceActorContextResolver actorContextResolver, |
| | 58 | 40 | | IHttpGovernanceRequestCorrelationResolver requestCorrelationResolver, |
| | 58 | 41 | | IAcknowledgmentChallengeService acknowledgmentChallengeService, |
| | 58 | 42 | | IOptions<EndpointGovernanceOptions> endpointOptions, |
| | 58 | 43 | | IOptions<GovernanceHttpResultMappingOptions> resultOptions) |
| | | 44 | | { |
| | 58 | 45 | | this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); |
| | 57 | 46 | | this.actorContextResolver = actorContextResolver ?? throw new ArgumentNullException(nameof(actorContextResolver) |
| | 56 | 47 | | this.requestCorrelationResolver = requestCorrelationResolver ?? throw new ArgumentNullException(nameof(requestCo |
| | 55 | 48 | | this.acknowledgmentChallengeService = acknowledgmentChallengeService ?? throw new ArgumentNullException(nameof(a |
| | 54 | 49 | | this.endpointOptions = endpointOptions?.Value ?? throw new ArgumentNullException(nameof(endpointOptions)); |
| | 52 | 50 | | this.resultOptions = resultOptions?.Value ?? throw new ArgumentNullException(nameof(resultOptions)); |
| | 50 | 51 | | this.endpointOptions.Validate(); |
| | 50 | 52 | | this.resultOptions.Validate(); |
| | 50 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public async ValueTask<EndpointGovernanceResult> EvaluateAsync( |
| | | 57 | | HttpContext httpContext, |
| | | 58 | | EndpointGovernanceDescriptor descriptor, |
| | | 59 | | CancellationToken cancellationToken = default) |
| | | 60 | | { |
| | 48 | 61 | | ArgumentNullException.ThrowIfNull(httpContext); |
| | 48 | 62 | | ArgumentNullException.ThrowIfNull(descriptor); |
| | 48 | 63 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 64 | | |
| | 47 | 65 | | if (!descriptor.HasGovernanceMetadata) |
| | | 66 | | { |
| | 1 | 67 | | return EndpointGovernanceResult.Allow(); |
| | | 68 | | } |
| | | 69 | | |
| | 46 | 70 | | var optionalServices = new EndpointGovernanceOptionalServiceResolver( |
| | 46 | 71 | | httpContext.RequestServices, |
| | 46 | 72 | | serviceProvider); |
| | 46 | 73 | | GovernanceHttpRequestCorrelation correlation = requestCorrelationResolver.ResolveRequestCorrelation(); |
| | 46 | 74 | | IReadOnlyDictionary<string, string> endpointMetadata = descriptor.ToMetadata(endpointOptions.MetadataMode); |
| | | 75 | | |
| | 46 | 76 | | IReadOnlyDictionary<string, string> mergedMetadata = correlation.MergeMetadata(endpointMetadata); |
| | 46 | 77 | | MetadataStageResult metadataStage = await SanitizeMetadataAsync( |
| | 46 | 78 | | optionalServices, |
| | 46 | 79 | | correlation, |
| | 46 | 80 | | mergedMetadata, |
| | 46 | 81 | | cancellationToken).ConfigureAwait(false); |
| | | 82 | | |
| | 46 | 83 | | if (metadataStage.TerminalResult is not null) |
| | | 84 | | { |
| | 1 | 85 | | return metadataStage.TerminalResult; |
| | | 86 | | } |
| | | 87 | | |
| | 45 | 88 | | endpointMetadata = metadataStage.Metadata; |
| | | 89 | | |
| | | 90 | | // The sanitized dictionary already contains request metadata, so it is authoritative. Merging request |
| | | 91 | | // metadata underneath it again would reintroduce any entry the sanitizer dropped: a dropped key is simply |
| | | 92 | | // absent from the sanitized result, so there would be nothing to override the raw value. |
| | 45 | 93 | | GovernanceEvaluationContext evaluationContext = correlation.ToEvaluationContext( |
| | 45 | 94 | | endpointOptions.PolicyVersion, |
| | 45 | 95 | | endpointOptions.PolicyHash, |
| | 45 | 96 | | endpointMetadata, |
| | 45 | 97 | | mergeRequestMetadata: false); |
| | | 98 | | |
| | 45 | 99 | | DecisionStageResult policyStage = await EvaluatePolicyAsync( |
| | 45 | 100 | | httpContext, |
| | 45 | 101 | | descriptor, |
| | 45 | 102 | | optionalServices, |
| | 45 | 103 | | correlation, |
| | 45 | 104 | | endpointMetadata, |
| | 45 | 105 | | evaluationContext, |
| | 45 | 106 | | cancellationToken).ConfigureAwait(false); |
| | | 107 | | |
| | 44 | 108 | | if (policyStage.TerminalResult is not null) |
| | | 109 | | { |
| | 2 | 110 | | return policyStage.TerminalResult; |
| | | 111 | | } |
| | | 112 | | |
| | 42 | 113 | | DecisionStageResult capabilityStage = await ValidateCapabilityAsync( |
| | 42 | 114 | | httpContext, |
| | 42 | 115 | | descriptor, |
| | 42 | 116 | | optionalServices, |
| | 42 | 117 | | endpointMetadata, |
| | 42 | 118 | | policyStage.Decision, |
| | 42 | 119 | | cancellationToken).ConfigureAwait(false); |
| | | 120 | | |
| | 42 | 121 | | if (capabilityStage.TerminalResult is not null) |
| | | 122 | | { |
| | 5 | 123 | | return capabilityStage.TerminalResult; |
| | | 124 | | } |
| | | 125 | | |
| | 37 | 126 | | AuditStageResult auditStage = await EmitAuditAsync( |
| | 37 | 127 | | httpContext, |
| | 37 | 128 | | descriptor, |
| | 37 | 129 | | optionalServices, |
| | 37 | 130 | | endpointMetadata, |
| | 37 | 131 | | capabilityStage.Decision, |
| | 37 | 132 | | cancellationToken).ConfigureAwait(false); |
| | | 133 | | |
| | 36 | 134 | | return auditStage.TerminalResult is not null |
| | 36 | 135 | | ? auditStage.TerminalResult |
| | 36 | 136 | | : CreateCompletedResult( |
| | 36 | 137 | | descriptor, |
| | 36 | 138 | | endpointMetadata, |
| | 36 | 139 | | capabilityStage.Decision, |
| | 36 | 140 | | auditStage.Actor); |
| | 45 | 141 | | } |
| | | 142 | | |
| | | 143 | | private async ValueTask<MetadataStageResult> SanitizeMetadataAsync( |
| | | 144 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 145 | | GovernanceHttpRequestCorrelation correlation, |
| | | 146 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 147 | | CancellationToken cancellationToken) |
| | | 148 | | { |
| | 46 | 149 | | IGovernanceMetadataSanitizer? metadataSanitizer = optionalServices.GetMetadataSanitizer(); |
| | 46 | 150 | | if (metadataSanitizer is null) |
| | | 151 | | { |
| | 41 | 152 | | return new MetadataStageResult(endpointMetadata, TerminalResult: null); |
| | | 153 | | } |
| | | 154 | | |
| | 5 | 155 | | GovernanceMetadataSanitizationResult sanitizationResult = await metadataSanitizer |
| | 5 | 156 | | .SanitizeAsync(endpointMetadata, cancellationToken) |
| | 5 | 157 | | .ConfigureAwait(false); |
| | | 158 | | |
| | 5 | 159 | | return sanitizationResult.CanProceed |
| | 5 | 160 | | ? new MetadataStageResult(sanitizationResult.SanitizedMetadata, TerminalResult: null) |
| | 5 | 161 | | : new MetadataStageResult( |
| | 5 | 162 | | endpointMetadata, |
| | 5 | 163 | | CreateMetadataSanitizationFailure(correlation, sanitizationResult)); |
| | 46 | 164 | | } |
| | | 165 | | |
| | | 166 | | private async ValueTask<DecisionStageResult> EvaluatePolicyAsync( |
| | | 167 | | HttpContext httpContext, |
| | | 168 | | EndpointGovernanceDescriptor descriptor, |
| | | 169 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 170 | | GovernanceHttpRequestCorrelation correlation, |
| | | 171 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 172 | | GovernanceEvaluationContext evaluationContext, |
| | | 173 | | CancellationToken cancellationToken) |
| | | 174 | | { |
| | 45 | 175 | | GovernanceDecision allowedDecision = CreateAllowDecision(evaluationContext, correlation.TraceId); |
| | 45 | 176 | | if (descriptor.PolicyTypes.Count == 0) |
| | | 177 | | { |
| | 6 | 178 | | return new DecisionStageResult(allowedDecision, TerminalResult: null); |
| | | 179 | | } |
| | | 180 | | |
| | 39 | 181 | | IGovernancePolicyEvaluator<GovernanceEvaluationContext>? evaluator = |
| | 39 | 182 | | optionalServices.GetPolicyEvaluator(); |
| | 39 | 183 | | if (evaluator is null) |
| | | 184 | | { |
| | 2 | 185 | | EndpointGovernanceResult terminalResult = endpointOptions.FailClosedWhenPolicyEvaluatorMissing |
| | 2 | 186 | | ? CreateConfigurationFailure( |
| | 2 | 187 | | httpContext, |
| | 2 | 188 | | descriptor, |
| | 2 | 189 | | endpointMetadata, |
| | 2 | 190 | | "endpoint.policy_evaluator.missing", |
| | 2 | 191 | | "Endpoint governance policy metadata was present, but no AsiBackbone policy evaluator was registered |
| | 2 | 192 | | allowedDecision, |
| | 2 | 193 | | "aspnetcore.endpoint.governance.configuration.policy_evaluator") |
| | 2 | 194 | | : EndpointGovernanceResult.Allow(allowedDecision); |
| | | 195 | | |
| | 2 | 196 | | return new DecisionStageResult(allowedDecision, terminalResult); |
| | | 197 | | } |
| | | 198 | | |
| | 37 | 199 | | GovernanceDecision decision = await evaluator |
| | 37 | 200 | | .EvaluateAsync(evaluationContext, cancellationToken) |
| | 37 | 201 | | .ConfigureAwait(false); |
| | | 202 | | |
| | 36 | 203 | | decision = BackfillTraceId(decision, correlation.TraceId); |
| | | 204 | | |
| | 36 | 205 | | return new DecisionStageResult(decision, TerminalResult: null); |
| | 44 | 206 | | } |
| | | 207 | | |
| | | 208 | | private async ValueTask<DecisionStageResult> ValidateCapabilityAsync( |
| | | 209 | | HttpContext httpContext, |
| | | 210 | | EndpointGovernanceDescriptor descriptor, |
| | | 211 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 212 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 213 | | GovernanceDecision decision, |
| | | 214 | | CancellationToken cancellationToken) |
| | | 215 | | { |
| | | 216 | | // Capability requirements apply to every decision that is not already denied, including warning, |
| | | 217 | | // acknowledgment-required, deferred, and escalation-recommended outcomes. A missing grant must take precedence |
| | | 218 | | // over any later host flow that could otherwise approve the request without re-checking the grant. |
| | 42 | 219 | | if (decision.IsDenied || descriptor.CapabilityScopes.Count == 0) |
| | | 220 | | { |
| | 22 | 221 | | return new DecisionStageResult(decision, TerminalResult: null); |
| | | 222 | | } |
| | | 223 | | |
| | 20 | 224 | | IEndpointCapabilityGrantValidator? capabilityValidator = |
| | 20 | 225 | | optionalServices.GetCapabilityValidator(); |
| | 20 | 226 | | if (capabilityValidator is null) |
| | | 227 | | { |
| | 7 | 228 | | EndpointGovernanceResult? terminalResult = endpointOptions.FailClosedWhenCapabilityValidatorMissing |
| | 7 | 229 | | ? CreateCapabilityFailure( |
| | 7 | 230 | | httpContext, |
| | 7 | 231 | | descriptor, |
| | 7 | 232 | | endpointMetadata, |
| | 7 | 233 | | "endpoint.capability_validator.missing", |
| | 7 | 234 | | "Endpoint capability metadata was present, but no host-owned endpoint capability validator was regis |
| | 7 | 235 | | decision, |
| | 7 | 236 | | "aspnetcore.endpoint.governance.capability.configuration") |
| | 7 | 237 | | : null; |
| | | 238 | | |
| | 7 | 239 | | return new DecisionStageResult(decision, terminalResult); |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | // The feature reports the grant accepted by this evaluation only. Clearing it first keeps an identifier left by |
| | | 243 | | // earlier request processing from reaching the receipt when the validator accepts nothing. |
| | 13 | 244 | | httpContext.Features.Set<IEndpointCapabilityGrantFeature>(null); |
| | | 245 | | |
| | 13 | 246 | | GovernanceDecision validatedDecision = await capabilityValidator |
| | 13 | 247 | | .ValidateAsync(httpContext, descriptor, decision, cancellationToken) |
| | 13 | 248 | | .ConfigureAwait(false); |
| | | 249 | | |
| | 13 | 250 | | validatedDecision = BackfillTraceId(validatedDecision, decision.TraceId); |
| | | 251 | | |
| | 13 | 252 | | return new DecisionStageResult(validatedDecision, TerminalResult: null); |
| | 42 | 253 | | } |
| | | 254 | | |
| | | 255 | | private async ValueTask<AuditStageResult> EmitAuditAsync( |
| | | 256 | | HttpContext httpContext, |
| | | 257 | | EndpointGovernanceDescriptor descriptor, |
| | | 258 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 259 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 260 | | GovernanceDecision decision, |
| | | 261 | | CancellationToken cancellationToken) |
| | | 262 | | { |
| | 37 | 263 | | if (!descriptor.EmitGovernanceAudit) |
| | | 264 | | { |
| | 25 | 265 | | return new AuditStageResult(Actor: null, TerminalResult: null); |
| | | 266 | | } |
| | | 267 | | |
| | 12 | 268 | | IDecisionReceiptSink? auditSink = optionalServices.GetAuditSink(); |
| | 12 | 269 | | if (auditSink is null) |
| | | 270 | | { |
| | 3 | 271 | | EndpointGovernanceResult terminalResult = endpointOptions.FailClosedWhenAuditSinkMissing |
| | 3 | 272 | | ? CreateConfigurationFailure( |
| | 3 | 273 | | httpContext, |
| | 3 | 274 | | descriptor, |
| | 3 | 275 | | endpointMetadata, |
| | 3 | 276 | | "endpoint.audit_sink.missing", |
| | 3 | 277 | | "Endpoint governance audit emission was requested, but no host-owned audit sink was registered.", |
| | 3 | 278 | | decision, |
| | 3 | 279 | | "aspnetcore.endpoint.governance.configuration.audit_sink") |
| | 3 | 280 | | : EndpointGovernanceResult.Allow(decision); |
| | | 281 | | |
| | 3 | 282 | | return new AuditStageResult(Actor: null, terminalResult); |
| | | 283 | | } |
| | | 284 | | |
| | 9 | 285 | | IGovernanceActorContext actor = actorContextResolver.ResolveActorContext(); |
| | 9 | 286 | | var receipt = DecisionReceipt.FromDecision( |
| | 9 | 287 | | actor, |
| | 9 | 288 | | descriptor.OperationName, |
| | 9 | 289 | | decision, |
| | 9 | 290 | | metadata: endpointMetadata, |
| | 9 | 291 | | decisionStage: "aspnetcore.endpoint.governance"); |
| | | 292 | | |
| | | 293 | | // Only a grant accepted for a decision that was not then denied is recorded, so a receipt never names a grant a |
| | | 294 | | // the authority for an operation that did not go ahead on it. |
| | 9 | 295 | | string? capabilityGrantId = descriptor.CapabilityScopes.Count > 0 && !decision.IsDenied |
| | 9 | 296 | | ? httpContext.Features.Get<IEndpointCapabilityGrantFeature>()?.GrantId |
| | 9 | 297 | | : null; |
| | | 298 | | |
| | 9 | 299 | | if (!string.IsNullOrWhiteSpace(capabilityGrantId)) |
| | | 300 | | { |
| | 2 | 301 | | receipt = receipt.WithCapabilityGrantId(capabilityGrantId); |
| | | 302 | | } |
| | | 303 | | |
| | 9 | 304 | | await auditSink.WriteAsync(receipt, cancellationToken).ConfigureAwait(false); |
| | 8 | 305 | | return new AuditStageResult(actor, TerminalResult: null); |
| | 36 | 306 | | } |
| | | 307 | | |
| | | 308 | | private EndpointGovernanceResult CreateCompletedResult( |
| | | 309 | | EndpointGovernanceDescriptor descriptor, |
| | | 310 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 311 | | GovernanceDecision decision, |
| | | 312 | | IGovernanceActorContext? actor) |
| | | 313 | | { |
| | 33 | 314 | | if (decision.RequiresAcknowledgment && descriptor.RequiresAcknowledgment) |
| | | 315 | | { |
| | 6 | 316 | | actor ??= actorContextResolver.ResolveActorContext(); |
| | | 317 | | |
| | 6 | 318 | | if (!AcknowledgmentActorBinding.IsSufficient(actor)) |
| | | 319 | | { |
| | 1 | 320 | | return EndpointGovernanceResult.Block( |
| | 1 | 321 | | Microsoft.AspNetCore.Http.Results.Problem( |
| | 1 | 322 | | title: "Acknowledgment challenge actor binding failed.", |
| | 1 | 323 | | detail: AcknowledgmentActorBinding.FailureMessage, |
| | 1 | 324 | | statusCode: StatusCodes.Status403Forbidden, |
| | 1 | 325 | | extensions: new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 1 | 326 | | { |
| | 1 | 327 | | ["reasonCodes"] = new[] { AcknowledgmentActorBinding.FailureCode }, |
| | 1 | 328 | | ["outcome"] = decision.Outcome.ToString() |
| | 1 | 329 | | }), |
| | 1 | 330 | | decision); |
| | | 331 | | } |
| | | 332 | | |
| | 5 | 333 | | AcknowledgmentChallenge challenge = acknowledgmentChallengeService.CreateChallenge( |
| | 5 | 334 | | actor, |
| | 5 | 335 | | descriptor.OperationName, |
| | 5 | 336 | | decision, |
| | 5 | 337 | | endpointMetadata); |
| | | 338 | | |
| | 5 | 339 | | IResult challengeResult = Microsoft.AspNetCore.Http.Results.Json( |
| | 5 | 340 | | challenge, |
| | 5 | 341 | | statusCode: endpointOptions.AcknowledgmentChallengeStatusCode); |
| | | 342 | | |
| | 5 | 343 | | return EndpointGovernanceResult.Challenge(challenge, challengeResult, decision); |
| | | 344 | | } |
| | | 345 | | |
| | 27 | 346 | | return decision.CanProceed |
| | 27 | 347 | | ? EndpointGovernanceResult.Allow(decision) |
| | 27 | 348 | | : CreateBlockedDecisionResult(decision); |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | private EndpointGovernanceResult CreateMetadataSanitizationFailure( |
| | | 352 | | GovernanceHttpRequestCorrelation correlation, |
| | | 353 | | GovernanceMetadataSanitizationResult sanitizationResult) |
| | | 354 | | { |
| | 1 | 355 | | var reasons = new List<OperationReason>(sanitizationResult.Reasons.Count + 1) |
| | 1 | 356 | | { |
| | 1 | 357 | | OperationReason.Create( |
| | 1 | 358 | | MetadataSanitizationDeniedReasonCode, |
| | 1 | 359 | | MetadataSanitizationDeniedReasonMessage) |
| | 1 | 360 | | }; |
| | 1 | 361 | | reasons.AddRange(sanitizationResult.Reasons); |
| | | 362 | | |
| | 1 | 363 | | var decision = GovernanceDecision.Deny( |
| | 1 | 364 | | reasons, |
| | 1 | 365 | | correlationId: correlation.CorrelationId, |
| | 1 | 366 | | traceId: correlation.TraceId, |
| | 1 | 367 | | policyVersion: endpointOptions.PolicyVersion, |
| | 1 | 368 | | policyHash: endpointOptions.PolicyHash); |
| | | 369 | | |
| | 1 | 370 | | return CreateBlockedDecisionResult(decision); |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | private static GovernanceDecision CreateAllowDecision( |
| | | 374 | | GovernanceEvaluationContext evaluationContext, |
| | | 375 | | string? traceId) |
| | | 376 | | { |
| | 45 | 377 | | return GovernanceDecision.Allow( |
| | 45 | 378 | | correlationId: evaluationContext.CorrelationId, |
| | 45 | 379 | | traceId: traceId, |
| | 45 | 380 | | policyVersion: evaluationContext.PolicyVersion, |
| | 45 | 381 | | policyHash: evaluationContext.PolicyHash); |
| | | 382 | | } |
| | | 383 | | |
| | | 384 | | private static GovernanceDecision BackfillTraceId(GovernanceDecision decision, string? traceId) |
| | | 385 | | { |
| | 49 | 386 | | return decision.TraceId is null && traceId is not null |
| | 49 | 387 | | ? decision.WithTraceId(traceId) |
| | 49 | 388 | | : decision; |
| | | 389 | | } |
| | | 390 | | |
| | | 391 | | private readonly record struct MetadataStageResult( |
| | | 392 | | IReadOnlyDictionary<string, string> Metadata, |
| | | 393 | | EndpointGovernanceResult? TerminalResult); |
| | | 394 | | |
| | | 395 | | private readonly record struct DecisionStageResult( |
| | | 396 | | GovernanceDecision Decision, |
| | | 397 | | EndpointGovernanceResult? TerminalResult); |
| | | 398 | | |
| | | 399 | | private readonly record struct AuditStageResult( |
| | | 400 | | IGovernanceActorContext? Actor, |
| | | 401 | | EndpointGovernanceResult? TerminalResult); |
| | | 402 | | |
| | 46 | 403 | | private sealed class EndpointGovernanceOptionalServiceResolver( |
| | 46 | 404 | | IServiceProvider requestServices, |
| | 46 | 405 | | IServiceProvider governanceServices) |
| | | 406 | | { |
| | | 407 | | private IGovernancePolicyEvaluator<GovernanceEvaluationContext>? policyEvaluator; |
| | | 408 | | private IEndpointCapabilityGrantValidator? capabilityValidator; |
| | | 409 | | private IDecisionReceiptSink? auditSink; |
| | | 410 | | private IGovernanceMetadataSanitizer? metadataSanitizer; |
| | | 411 | | private bool policyEvaluatorResolved; |
| | | 412 | | private bool capabilityValidatorResolved; |
| | | 413 | | private bool auditSinkResolved; |
| | | 414 | | private bool metadataSanitizerResolved; |
| | | 415 | | |
| | | 416 | | public IGovernancePolicyEvaluator<GovernanceEvaluationContext>? GetPolicyEvaluator() |
| | | 417 | | { |
| | 39 | 418 | | if (!policyEvaluatorResolved) |
| | | 419 | | { |
| | 39 | 420 | | policyEvaluator = requestServices |
| | 39 | 421 | | .GetService<IGovernancePolicyEvaluator<GovernanceEvaluationContext>>(); |
| | 39 | 422 | | policyEvaluatorResolved = true; |
| | | 423 | | } |
| | | 424 | | |
| | 39 | 425 | | return policyEvaluator; |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | public IEndpointCapabilityGrantValidator? GetCapabilityValidator() |
| | | 429 | | { |
| | 20 | 430 | | if (!capabilityValidatorResolved) |
| | | 431 | | { |
| | 20 | 432 | | capabilityValidator = requestServices.GetService<IEndpointCapabilityGrantValidator>(); |
| | 20 | 433 | | capabilityValidatorResolved = true; |
| | | 434 | | } |
| | | 435 | | |
| | 20 | 436 | | return capabilityValidator; |
| | | 437 | | } |
| | | 438 | | |
| | | 439 | | public IDecisionReceiptSink? GetAuditSink() |
| | | 440 | | { |
| | 12 | 441 | | if (!auditSinkResolved) |
| | | 442 | | { |
| | 12 | 443 | | auditSink = governanceServices.GetService<IDecisionReceiptSink>(); |
| | 12 | 444 | | auditSinkResolved = true; |
| | | 445 | | } |
| | | 446 | | |
| | 12 | 447 | | return auditSink; |
| | | 448 | | } |
| | | 449 | | |
| | | 450 | | public IGovernanceMetadataSanitizer? GetMetadataSanitizer() |
| | | 451 | | { |
| | 46 | 452 | | if (!metadataSanitizerResolved) |
| | | 453 | | { |
| | 46 | 454 | | metadataSanitizer = requestServices.GetService<IGovernanceMetadataSanitizer>(); |
| | 46 | 455 | | metadataSanitizerResolved = true; |
| | | 456 | | } |
| | | 457 | | |
| | 46 | 458 | | return metadataSanitizer; |
| | | 459 | | } |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | private EndpointGovernanceResult CreateBlockedDecisionResult(GovernanceDecision decision) |
| | | 463 | | { |
| | 16 | 464 | | return decision.IsDenied && resultOptions.DeniedStatusCode == StatusCodes.Status403Forbidden |
| | 16 | 465 | | ? EndpointGovernanceResult.BlockWithDefaultFailure(decision) |
| | 16 | 466 | | : EndpointGovernanceResult.Block(decision.ToHttpResult(resultOptions), decision); |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | private EndpointGovernanceResult CreateConfigurationFailure( |
| | | 470 | | HttpContext httpContext, |
| | | 471 | | EndpointGovernanceDescriptor descriptor, |
| | | 472 | | IReadOnlyDictionary<string, string> metadata, |
| | | 473 | | string code, |
| | | 474 | | string message, |
| | | 475 | | GovernanceDecision currentDecision, |
| | | 476 | | string decisionStage) |
| | | 477 | | { |
| | 3 | 478 | | var decision = GovernanceDecision.Deny( |
| | 3 | 479 | | code, |
| | 3 | 480 | | message, |
| | 3 | 481 | | correlationId: currentDecision.CorrelationId, |
| | 3 | 482 | | traceId: currentDecision.TraceId, |
| | 3 | 483 | | policyVersion: currentDecision.PolicyVersion, |
| | 3 | 484 | | policyHash: currentDecision.PolicyHash); |
| | | 485 | | |
| | 3 | 486 | | return AsiBackboneEndpointGovernanceDevelopmentDiagnostics.IsEnabled(httpContext, endpointOptions) |
| | 3 | 487 | | ? EndpointGovernanceResult.Block( |
| | 3 | 488 | | AsiBackboneEndpointGovernanceDevelopmentDiagnostics.CreateProblem( |
| | 3 | 489 | | httpContext, |
| | 3 | 490 | | endpointOptions, |
| | 3 | 491 | | descriptor, |
| | 3 | 492 | | decision, |
| | 3 | 493 | | decisionStage, |
| | 3 | 494 | | title: "Endpoint governance configuration is incomplete.", |
| | 3 | 495 | | detail: message, |
| | 3 | 496 | | statusCode: endpointOptions.ConfigurationFailureStatusCode, |
| | 3 | 497 | | metadata: metadata), |
| | 3 | 498 | | decision) |
| | 3 | 499 | | : EndpointGovernanceResult.Block( |
| | 3 | 500 | | Microsoft.AspNetCore.Http.Results.Problem( |
| | 3 | 501 | | title: "Endpoint governance configuration is incomplete.", |
| | 3 | 502 | | detail: message, |
| | 3 | 503 | | statusCode: endpointOptions.ConfigurationFailureStatusCode, |
| | 3 | 504 | | extensions: new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 3 | 505 | | { |
| | 3 | 506 | | ["reasonCodes"] = decision.ReasonCodes, |
| | 3 | 507 | | ["outcome"] = decision.Outcome.ToString() |
| | 3 | 508 | | }), |
| | 3 | 509 | | decision); |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | private EndpointGovernanceResult CreateCapabilityFailure( |
| | | 513 | | HttpContext httpContext, |
| | | 514 | | EndpointGovernanceDescriptor descriptor, |
| | | 515 | | IReadOnlyDictionary<string, string> metadata, |
| | | 516 | | string code, |
| | | 517 | | string message, |
| | | 518 | | GovernanceDecision currentDecision, |
| | | 519 | | string decisionStage) |
| | | 520 | | { |
| | 5 | 521 | | var decision = GovernanceDecision.Deny( |
| | 5 | 522 | | code, |
| | 5 | 523 | | message, |
| | 5 | 524 | | correlationId: currentDecision.CorrelationId, |
| | 5 | 525 | | traceId: currentDecision.TraceId, |
| | 5 | 526 | | policyVersion: currentDecision.PolicyVersion, |
| | 5 | 527 | | policyHash: currentDecision.PolicyHash); |
| | | 528 | | |
| | 5 | 529 | | return AsiBackboneEndpointGovernanceDevelopmentDiagnostics.IsEnabled(httpContext, endpointOptions) |
| | 5 | 530 | | ? EndpointGovernanceResult.Block( |
| | 5 | 531 | | AsiBackboneEndpointGovernanceDevelopmentDiagnostics.CreateProblem( |
| | 5 | 532 | | httpContext, |
| | 5 | 533 | | endpointOptions, |
| | 5 | 534 | | descriptor, |
| | 5 | 535 | | decision, |
| | 5 | 536 | | decisionStage, |
| | 5 | 537 | | title: "Endpoint capability grant validation failed.", |
| | 5 | 538 | | detail: message, |
| | 5 | 539 | | statusCode: endpointOptions.CapabilityFailureStatusCode, |
| | 5 | 540 | | metadata: metadata), |
| | 5 | 541 | | decision) |
| | 5 | 542 | | : EndpointGovernanceResult.Block( |
| | 5 | 543 | | Microsoft.AspNetCore.Http.Results.Problem( |
| | 5 | 544 | | title: "Endpoint capability grant validation failed.", |
| | 5 | 545 | | detail: message, |
| | 5 | 546 | | statusCode: endpointOptions.CapabilityFailureStatusCode, |
| | 5 | 547 | | extensions: new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 5 | 548 | | { |
| | 5 | 549 | | ["reasonCodes"] = decision.ReasonCodes, |
| | 5 | 550 | | ["outcome"] = decision.Outcome.ToString() |
| | 5 | 551 | | }), |
| | 5 | 552 | | decision); |
| | | 553 | | } |
| | | 554 | | } |