| | | 1 | | using AsiBackbone.AspNetCore.Actors; |
| | | 2 | | using AsiBackbone.AspNetCore.Correlation; |
| | | 3 | | using AsiBackbone.AspNetCore.Handshakes; |
| | | 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 DefaultAsiBackboneEndpointGovernanceService : IAsiBackboneEndpointGovernanceService |
| | | 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 IAsiBackboneHttpActorContextResolver actorContextResolver; |
| | | 29 | | private readonly IAsiBackboneHttpRequestCorrelationResolver requestCorrelationResolver; |
| | | 30 | | private readonly IAsiBackboneAcknowledgmentChallengeService acknowledgmentChallengeService; |
| | | 31 | | private readonly AsiBackboneEndpointGovernanceOptions endpointOptions; |
| | | 32 | | private readonly AsiBackboneHttpResultMappingOptions resultOptions; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="DefaultAsiBackboneEndpointGovernanceService" /> class. |
| | | 36 | | /// </summary> |
| | 23 | 37 | | public DefaultAsiBackboneEndpointGovernanceService( |
| | 23 | 38 | | IServiceProvider serviceProvider, |
| | 23 | 39 | | IAsiBackboneHttpActorContextResolver actorContextResolver, |
| | 23 | 40 | | IAsiBackboneHttpRequestCorrelationResolver requestCorrelationResolver, |
| | 23 | 41 | | IAsiBackboneAcknowledgmentChallengeService acknowledgmentChallengeService, |
| | 23 | 42 | | IOptions<AsiBackboneEndpointGovernanceOptions> endpointOptions, |
| | 23 | 43 | | IOptions<AsiBackboneHttpResultMappingOptions> resultOptions) |
| | | 44 | | { |
| | 23 | 45 | | this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); |
| | 23 | 46 | | this.actorContextResolver = actorContextResolver ?? throw new ArgumentNullException(nameof(actorContextResolver) |
| | 23 | 47 | | this.requestCorrelationResolver = requestCorrelationResolver ?? throw new ArgumentNullException(nameof(requestCo |
| | 23 | 48 | | this.acknowledgmentChallengeService = acknowledgmentChallengeService ?? throw new ArgumentNullException(nameof(a |
| | 23 | 49 | | this.endpointOptions = endpointOptions?.Value ?? throw new ArgumentNullException(nameof(endpointOptions)); |
| | 23 | 50 | | this.resultOptions = resultOptions?.Value ?? throw new ArgumentNullException(nameof(resultOptions)); |
| | 23 | 51 | | this.endpointOptions.Validate(); |
| | 23 | 52 | | this.resultOptions.Validate(); |
| | 23 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public async ValueTask<AsiBackboneEndpointGovernanceResult> EvaluateAsync( |
| | | 57 | | HttpContext httpContext, |
| | | 58 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 59 | | CancellationToken cancellationToken = default) |
| | | 60 | | { |
| | 21 | 61 | | ArgumentNullException.ThrowIfNull(httpContext); |
| | 21 | 62 | | ArgumentNullException.ThrowIfNull(descriptor); |
| | 21 | 63 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 64 | | |
| | 20 | 65 | | if (!descriptor.HasGovernanceMetadata) |
| | | 66 | | { |
| | 1 | 67 | | return AsiBackboneEndpointGovernanceResult.Allow(); |
| | | 68 | | } |
| | | 69 | | |
| | 19 | 70 | | var optionalServices = new EndpointGovernanceOptionalServiceResolver( |
| | 19 | 71 | | httpContext.RequestServices, |
| | 19 | 72 | | serviceProvider); |
| | 19 | 73 | | AsiBackboneHttpRequestCorrelation correlation = requestCorrelationResolver.ResolveRequestCorrelation(); |
| | 19 | 74 | | IReadOnlyDictionary<string, string> endpointMetadata = descriptor.ToMetadata(endpointOptions.MetadataMode); |
| | | 75 | | |
| | 19 | 76 | | MetadataStageResult metadataStage = await SanitizeMetadataAsync( |
| | 19 | 77 | | optionalServices, |
| | 19 | 78 | | correlation, |
| | 19 | 79 | | endpointMetadata, |
| | 19 | 80 | | cancellationToken).ConfigureAwait(false); |
| | | 81 | | |
| | 19 | 82 | | if (metadataStage.TerminalResult is not null) |
| | | 83 | | { |
| | 1 | 84 | | return metadataStage.TerminalResult; |
| | | 85 | | } |
| | | 86 | | |
| | 18 | 87 | | endpointMetadata = metadataStage.Metadata; |
| | 18 | 88 | | AsiBackboneConstraintEvaluationContext evaluationContext = correlation.ToEvaluationContext( |
| | 18 | 89 | | endpointOptions.PolicyVersion, |
| | 18 | 90 | | endpointOptions.PolicyHash, |
| | 18 | 91 | | endpointMetadata); |
| | | 92 | | |
| | 18 | 93 | | DecisionStageResult policyStage = await EvaluatePolicyAsync( |
| | 18 | 94 | | httpContext, |
| | 18 | 95 | | descriptor, |
| | 18 | 96 | | optionalServices, |
| | 18 | 97 | | correlation, |
| | 18 | 98 | | endpointMetadata, |
| | 18 | 99 | | evaluationContext, |
| | 18 | 100 | | cancellationToken).ConfigureAwait(false); |
| | | 101 | | |
| | 17 | 102 | | if (policyStage.TerminalResult is not null) |
| | | 103 | | { |
| | 2 | 104 | | return policyStage.TerminalResult; |
| | | 105 | | } |
| | | 106 | | |
| | 15 | 107 | | DecisionStageResult capabilityStage = await ValidateCapabilityAsync( |
| | 15 | 108 | | httpContext, |
| | 15 | 109 | | descriptor, |
| | 15 | 110 | | optionalServices, |
| | 15 | 111 | | endpointMetadata, |
| | 15 | 112 | | policyStage.Decision, |
| | 15 | 113 | | cancellationToken).ConfigureAwait(false); |
| | | 114 | | |
| | 15 | 115 | | if (capabilityStage.TerminalResult is not null) |
| | | 116 | | { |
| | 3 | 117 | | return capabilityStage.TerminalResult; |
| | | 118 | | } |
| | | 119 | | |
| | 12 | 120 | | AuditStageResult auditStage = await EmitAuditAsync( |
| | 12 | 121 | | httpContext, |
| | 12 | 122 | | descriptor, |
| | 12 | 123 | | optionalServices, |
| | 12 | 124 | | endpointMetadata, |
| | 12 | 125 | | capabilityStage.Decision, |
| | 12 | 126 | | cancellationToken).ConfigureAwait(false); |
| | | 127 | | |
| | 12 | 128 | | return auditStage.TerminalResult is not null |
| | 12 | 129 | | ? auditStage.TerminalResult |
| | 12 | 130 | | : CreateCompletedResult( |
| | 12 | 131 | | descriptor, |
| | 12 | 132 | | endpointMetadata, |
| | 12 | 133 | | capabilityStage.Decision, |
| | 12 | 134 | | auditStage.Actor); |
| | 19 | 135 | | } |
| | | 136 | | |
| | | 137 | | private async ValueTask<MetadataStageResult> SanitizeMetadataAsync( |
| | | 138 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 139 | | AsiBackboneHttpRequestCorrelation correlation, |
| | | 140 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 141 | | CancellationToken cancellationToken) |
| | | 142 | | { |
| | 19 | 143 | | IGovernanceMetadataSanitizer? metadataSanitizer = optionalServices.GetMetadataSanitizer(); |
| | 19 | 144 | | if (metadataSanitizer is null) |
| | | 145 | | { |
| | 18 | 146 | | return new MetadataStageResult(endpointMetadata, TerminalResult: null); |
| | | 147 | | } |
| | | 148 | | |
| | 1 | 149 | | GovernanceMetadataSanitizationResult sanitizationResult = await metadataSanitizer |
| | 1 | 150 | | .SanitizeAsync(endpointMetadata, cancellationToken) |
| | 1 | 151 | | .ConfigureAwait(false); |
| | | 152 | | |
| | 1 | 153 | | return sanitizationResult.CanProceed |
| | 1 | 154 | | ? new MetadataStageResult(sanitizationResult.SanitizedMetadata, TerminalResult: null) |
| | 1 | 155 | | : new MetadataStageResult( |
| | 1 | 156 | | endpointMetadata, |
| | 1 | 157 | | CreateMetadataSanitizationFailure(correlation, sanitizationResult)); |
| | 19 | 158 | | } |
| | | 159 | | |
| | | 160 | | private async ValueTask<DecisionStageResult> EvaluatePolicyAsync( |
| | | 161 | | HttpContext httpContext, |
| | | 162 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 163 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 164 | | AsiBackboneHttpRequestCorrelation correlation, |
| | | 165 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 166 | | AsiBackboneConstraintEvaluationContext evaluationContext, |
| | | 167 | | CancellationToken cancellationToken) |
| | | 168 | | { |
| | 18 | 169 | | GovernanceDecision allowedDecision = CreateAllowDecision(evaluationContext, correlation.TraceId); |
| | 18 | 170 | | if (descriptor.PolicyTypes.Count == 0) |
| | | 171 | | { |
| | 4 | 172 | | return new DecisionStageResult(allowedDecision, TerminalResult: null); |
| | | 173 | | } |
| | | 174 | | |
| | 14 | 175 | | IAsiBackbonePolicyEvaluator<AsiBackboneConstraintEvaluationContext>? evaluator = |
| | 14 | 176 | | optionalServices.GetPolicyEvaluator(); |
| | 14 | 177 | | if (evaluator is null) |
| | | 178 | | { |
| | 2 | 179 | | AsiBackboneEndpointGovernanceResult terminalResult = endpointOptions.FailClosedWhenPolicyEvaluatorMissing |
| | 2 | 180 | | ? CreateConfigurationFailure( |
| | 2 | 181 | | httpContext, |
| | 2 | 182 | | descriptor, |
| | 2 | 183 | | endpointMetadata, |
| | 2 | 184 | | "endpoint.policy_evaluator.missing", |
| | 2 | 185 | | "Endpoint governance policy metadata was present, but no AsiBackbone policy evaluator was registered |
| | 2 | 186 | | allowedDecision, |
| | 2 | 187 | | "aspnetcore.endpoint.governance.configuration.policy_evaluator") |
| | 2 | 188 | | : AsiBackboneEndpointGovernanceResult.Allow(allowedDecision); |
| | | 189 | | |
| | 2 | 190 | | return new DecisionStageResult(allowedDecision, terminalResult); |
| | | 191 | | } |
| | | 192 | | |
| | 12 | 193 | | GovernanceDecision decision = await evaluator |
| | 12 | 194 | | .EvaluateAsync(evaluationContext, cancellationToken) |
| | 12 | 195 | | .ConfigureAwait(false); |
| | | 196 | | |
| | 11 | 197 | | return new DecisionStageResult(decision, TerminalResult: null); |
| | 17 | 198 | | } |
| | | 199 | | |
| | | 200 | | private async ValueTask<DecisionStageResult> ValidateCapabilityAsync( |
| | | 201 | | HttpContext httpContext, |
| | | 202 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 203 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 204 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 205 | | GovernanceDecision decision, |
| | | 206 | | CancellationToken cancellationToken) |
| | | 207 | | { |
| | 15 | 208 | | if (!decision.CanProceed || descriptor.CapabilityScopes.Count == 0) |
| | | 209 | | { |
| | 8 | 210 | | return new DecisionStageResult(decision, TerminalResult: null); |
| | | 211 | | } |
| | | 212 | | |
| | 7 | 213 | | IAsiBackboneEndpointCapabilityGrantValidator? capabilityValidator = |
| | 7 | 214 | | optionalServices.GetCapabilityValidator(); |
| | 7 | 215 | | if (capabilityValidator is null) |
| | | 216 | | { |
| | 3 | 217 | | AsiBackboneEndpointGovernanceResult terminalResult = endpointOptions.FailClosedWhenCapabilityValidatorMissin |
| | 3 | 218 | | ? CreateCapabilityFailure( |
| | 3 | 219 | | httpContext, |
| | 3 | 220 | | descriptor, |
| | 3 | 221 | | endpointMetadata, |
| | 3 | 222 | | "endpoint.capability_validator.missing", |
| | 3 | 223 | | "Endpoint capability metadata was present, but no host-owned endpoint capability validator was regis |
| | 3 | 224 | | decision, |
| | 3 | 225 | | "aspnetcore.endpoint.governance.capability.configuration") |
| | 3 | 226 | | : AsiBackboneEndpointGovernanceResult.Allow(decision); |
| | | 227 | | |
| | 3 | 228 | | return new DecisionStageResult(decision, terminalResult); |
| | | 229 | | } |
| | | 230 | | |
| | 4 | 231 | | GovernanceDecision validatedDecision = await capabilityValidator |
| | 4 | 232 | | .ValidateAsync(httpContext, descriptor, decision, cancellationToken) |
| | 4 | 233 | | .ConfigureAwait(false); |
| | | 234 | | |
| | 4 | 235 | | return new DecisionStageResult(validatedDecision, TerminalResult: null); |
| | 15 | 236 | | } |
| | | 237 | | |
| | | 238 | | private async ValueTask<AuditStageResult> EmitAuditAsync( |
| | | 239 | | HttpContext httpContext, |
| | | 240 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 241 | | EndpointGovernanceOptionalServiceResolver optionalServices, |
| | | 242 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 243 | | GovernanceDecision decision, |
| | | 244 | | CancellationToken cancellationToken) |
| | | 245 | | { |
| | 12 | 246 | | if (!descriptor.EmitGovernanceAudit) |
| | | 247 | | { |
| | 9 | 248 | | return new AuditStageResult(Actor: null, TerminalResult: null); |
| | | 249 | | } |
| | | 250 | | |
| | 3 | 251 | | IAsiBackboneAuditSink? auditSink = optionalServices.GetAuditSink(); |
| | 3 | 252 | | if (auditSink is null) |
| | | 253 | | { |
| | 1 | 254 | | AsiBackboneEndpointGovernanceResult terminalResult = endpointOptions.FailClosedWhenAuditSinkMissing |
| | 1 | 255 | | ? CreateConfigurationFailure( |
| | 1 | 256 | | httpContext, |
| | 1 | 257 | | descriptor, |
| | 1 | 258 | | endpointMetadata, |
| | 1 | 259 | | "endpoint.audit_sink.missing", |
| | 1 | 260 | | "Endpoint governance audit emission was requested, but no host-owned audit sink was registered.", |
| | 1 | 261 | | decision, |
| | 1 | 262 | | "aspnetcore.endpoint.governance.configuration.audit_sink") |
| | 1 | 263 | | : AsiBackboneEndpointGovernanceResult.Allow(decision); |
| | | 264 | | |
| | 1 | 265 | | return new AuditStageResult(Actor: null, terminalResult); |
| | | 266 | | } |
| | | 267 | | |
| | 2 | 268 | | IAsiBackboneActorContext actor = actorContextResolver.ResolveActorContext(); |
| | 2 | 269 | | var residue = AuditResidue.FromDecision( |
| | 2 | 270 | | actor, |
| | 2 | 271 | | descriptor.OperationName, |
| | 2 | 272 | | decision, |
| | 2 | 273 | | metadata: endpointMetadata, |
| | 2 | 274 | | decisionStage: "aspnetcore.endpoint.governance"); |
| | | 275 | | |
| | 2 | 276 | | await auditSink.WriteAsync(residue, cancellationToken).ConfigureAwait(false); |
| | 2 | 277 | | return new AuditStageResult(actor, TerminalResult: null); |
| | 12 | 278 | | } |
| | | 279 | | |
| | | 280 | | private AsiBackboneEndpointGovernanceResult CreateCompletedResult( |
| | | 281 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 282 | | IReadOnlyDictionary<string, string> endpointMetadata, |
| | | 283 | | GovernanceDecision decision, |
| | | 284 | | IAsiBackboneActorContext? actor) |
| | | 285 | | { |
| | 11 | 286 | | if (decision.RequiresAcknowledgment && descriptor.RequiresLiabilityHandshake) |
| | | 287 | | { |
| | 1 | 288 | | actor ??= actorContextResolver.ResolveActorContext(); |
| | 1 | 289 | | AsiBackboneAcknowledgmentChallenge challenge = acknowledgmentChallengeService.CreateChallenge( |
| | 1 | 290 | | actor, |
| | 1 | 291 | | descriptor.OperationName, |
| | 1 | 292 | | decision, |
| | 1 | 293 | | endpointMetadata); |
| | | 294 | | |
| | 1 | 295 | | IResult challengeResult = Microsoft.AspNetCore.Http.Results.Json( |
| | 1 | 296 | | challenge, |
| | 1 | 297 | | statusCode: endpointOptions.AcknowledgmentChallengeStatusCode); |
| | | 298 | | |
| | 1 | 299 | | return AsiBackboneEndpointGovernanceResult.Challenge(challenge, challengeResult, decision); |
| | | 300 | | } |
| | | 301 | | |
| | 10 | 302 | | return decision.CanProceed |
| | 10 | 303 | | ? AsiBackboneEndpointGovernanceResult.Allow(decision) |
| | 10 | 304 | | : CreateBlockedDecisionResult(decision); |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | private AsiBackboneEndpointGovernanceResult CreateMetadataSanitizationFailure( |
| | | 308 | | AsiBackboneHttpRequestCorrelation correlation, |
| | | 309 | | GovernanceMetadataSanitizationResult sanitizationResult) |
| | | 310 | | { |
| | 1 | 311 | | var reasons = new List<OperationReason>(sanitizationResult.Reasons.Count + 1) |
| | 1 | 312 | | { |
| | 1 | 313 | | OperationReason.Create( |
| | 1 | 314 | | MetadataSanitizationDeniedReasonCode, |
| | 1 | 315 | | MetadataSanitizationDeniedReasonMessage) |
| | 1 | 316 | | }; |
| | 1 | 317 | | reasons.AddRange(sanitizationResult.Reasons); |
| | | 318 | | |
| | 1 | 319 | | var decision = GovernanceDecision.Deny( |
| | 1 | 320 | | reasons, |
| | 1 | 321 | | correlationId: correlation.CorrelationId, |
| | 1 | 322 | | traceId: correlation.TraceId, |
| | 1 | 323 | | policyVersion: endpointOptions.PolicyVersion, |
| | 1 | 324 | | policyHash: endpointOptions.PolicyHash); |
| | | 325 | | |
| | 1 | 326 | | return CreateBlockedDecisionResult(decision); |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | private static GovernanceDecision CreateAllowDecision( |
| | | 330 | | AsiBackboneConstraintEvaluationContext evaluationContext, |
| | | 331 | | string? traceId) |
| | | 332 | | { |
| | 18 | 333 | | return GovernanceDecision.Allow( |
| | 18 | 334 | | correlationId: evaluationContext.CorrelationId, |
| | 18 | 335 | | traceId: traceId, |
| | 18 | 336 | | policyVersion: evaluationContext.PolicyVersion, |
| | 18 | 337 | | policyHash: evaluationContext.PolicyHash); |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private readonly record struct MetadataStageResult( |
| | 18 | 341 | | IReadOnlyDictionary<string, string> Metadata, |
| | 20 | 342 | | AsiBackboneEndpointGovernanceResult? TerminalResult); |
| | | 343 | | |
| | | 344 | | private readonly record struct DecisionStageResult( |
| | 38 | 345 | | GovernanceDecision Decision, |
| | 37 | 346 | | AsiBackboneEndpointGovernanceResult? TerminalResult); |
| | | 347 | | |
| | | 348 | | private readonly record struct AuditStageResult( |
| | 11 | 349 | | IAsiBackboneActorContext? Actor, |
| | 13 | 350 | | AsiBackboneEndpointGovernanceResult? TerminalResult); |
| | | 351 | | |
| | 19 | 352 | | private sealed class EndpointGovernanceOptionalServiceResolver( |
| | 19 | 353 | | IServiceProvider requestServices, |
| | 19 | 354 | | IServiceProvider governanceServices) |
| | | 355 | | { |
| | | 356 | | private IAsiBackbonePolicyEvaluator<AsiBackboneConstraintEvaluationContext>? policyEvaluator; |
| | | 357 | | private IAsiBackboneEndpointCapabilityGrantValidator? capabilityValidator; |
| | | 358 | | private IAsiBackboneAuditSink? auditSink; |
| | | 359 | | private IGovernanceMetadataSanitizer? metadataSanitizer; |
| | | 360 | | private bool policyEvaluatorResolved; |
| | | 361 | | private bool capabilityValidatorResolved; |
| | | 362 | | private bool auditSinkResolved; |
| | | 363 | | private bool metadataSanitizerResolved; |
| | | 364 | | |
| | | 365 | | public IAsiBackbonePolicyEvaluator<AsiBackboneConstraintEvaluationContext>? GetPolicyEvaluator() |
| | | 366 | | { |
| | 14 | 367 | | if (!policyEvaluatorResolved) |
| | | 368 | | { |
| | 14 | 369 | | policyEvaluator = requestServices |
| | 14 | 370 | | .GetService<IAsiBackbonePolicyEvaluator<AsiBackboneConstraintEvaluationContext>>(); |
| | 14 | 371 | | policyEvaluatorResolved = true; |
| | | 372 | | } |
| | | 373 | | |
| | 14 | 374 | | return policyEvaluator; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | public IAsiBackboneEndpointCapabilityGrantValidator? GetCapabilityValidator() |
| | | 378 | | { |
| | 7 | 379 | | if (!capabilityValidatorResolved) |
| | | 380 | | { |
| | 7 | 381 | | capabilityValidator = requestServices.GetService<IAsiBackboneEndpointCapabilityGrantValidator>(); |
| | 7 | 382 | | capabilityValidatorResolved = true; |
| | | 383 | | } |
| | | 384 | | |
| | 7 | 385 | | return capabilityValidator; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | public IAsiBackboneAuditSink? GetAuditSink() |
| | | 389 | | { |
| | 3 | 390 | | if (!auditSinkResolved) |
| | | 391 | | { |
| | 3 | 392 | | auditSink = governanceServices.GetService<IAsiBackboneAuditSink>(); |
| | 3 | 393 | | auditSinkResolved = true; |
| | | 394 | | } |
| | | 395 | | |
| | 3 | 396 | | return auditSink; |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | public IGovernanceMetadataSanitizer? GetMetadataSanitizer() |
| | | 400 | | { |
| | 19 | 401 | | if (!metadataSanitizerResolved) |
| | | 402 | | { |
| | 19 | 403 | | metadataSanitizer = requestServices.GetService<IGovernanceMetadataSanitizer>(); |
| | 19 | 404 | | metadataSanitizerResolved = true; |
| | | 405 | | } |
| | | 406 | | |
| | 19 | 407 | | return metadataSanitizer; |
| | | 408 | | } |
| | | 409 | | } |
| | | 410 | | |
| | | 411 | | private AsiBackboneEndpointGovernanceResult CreateBlockedDecisionResult(GovernanceDecision decision) |
| | | 412 | | { |
| | 7 | 413 | | return decision.IsDenied && resultOptions.DeniedStatusCode == StatusCodes.Status403Forbidden |
| | 7 | 414 | | ? AsiBackboneEndpointGovernanceResult.BlockWithDefaultFailure(decision) |
| | 7 | 415 | | : AsiBackboneEndpointGovernanceResult.Block(decision.ToHttpResult(resultOptions), decision); |
| | | 416 | | } |
| | | 417 | | |
| | | 418 | | private AsiBackboneEndpointGovernanceResult CreateConfigurationFailure( |
| | | 419 | | HttpContext httpContext, |
| | | 420 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 421 | | IReadOnlyDictionary<string, string> metadata, |
| | | 422 | | string code, |
| | | 423 | | string message, |
| | | 424 | | GovernanceDecision currentDecision, |
| | | 425 | | string decisionStage) |
| | | 426 | | { |
| | 2 | 427 | | var decision = GovernanceDecision.Deny( |
| | 2 | 428 | | code, |
| | 2 | 429 | | message, |
| | 2 | 430 | | correlationId: currentDecision.CorrelationId, |
| | 2 | 431 | | traceId: currentDecision.TraceId, |
| | 2 | 432 | | policyVersion: currentDecision.PolicyVersion, |
| | 2 | 433 | | policyHash: currentDecision.PolicyHash); |
| | | 434 | | |
| | 2 | 435 | | return AsiBackboneEndpointGovernanceDevelopmentDiagnostics.IsEnabled(httpContext, endpointOptions) |
| | 2 | 436 | | ? AsiBackboneEndpointGovernanceResult.Block( |
| | 2 | 437 | | AsiBackboneEndpointGovernanceDevelopmentDiagnostics.CreateProblem( |
| | 2 | 438 | | httpContext, |
| | 2 | 439 | | endpointOptions, |
| | 2 | 440 | | descriptor, |
| | 2 | 441 | | decision, |
| | 2 | 442 | | decisionStage, |
| | 2 | 443 | | title: "Endpoint governance configuration is incomplete.", |
| | 2 | 444 | | detail: message, |
| | 2 | 445 | | statusCode: endpointOptions.ConfigurationFailureStatusCode, |
| | 2 | 446 | | metadata: metadata), |
| | 2 | 447 | | decision) |
| | 2 | 448 | | : AsiBackboneEndpointGovernanceResult.Block( |
| | 2 | 449 | | Microsoft.AspNetCore.Http.Results.Problem( |
| | 2 | 450 | | title: "Endpoint governance configuration is incomplete.", |
| | 2 | 451 | | detail: message, |
| | 2 | 452 | | statusCode: endpointOptions.ConfigurationFailureStatusCode, |
| | 2 | 453 | | extensions: new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 2 | 454 | | { |
| | 2 | 455 | | ["reasonCodes"] = decision.ReasonCodes, |
| | 2 | 456 | | ["outcome"] = decision.Outcome.ToString() |
| | 2 | 457 | | }), |
| | 2 | 458 | | decision); |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | private AsiBackboneEndpointGovernanceResult CreateCapabilityFailure( |
| | | 462 | | HttpContext httpContext, |
| | | 463 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 464 | | IReadOnlyDictionary<string, string> metadata, |
| | | 465 | | string code, |
| | | 466 | | string message, |
| | | 467 | | GovernanceDecision currentDecision, |
| | | 468 | | string decisionStage) |
| | | 469 | | { |
| | 3 | 470 | | var decision = GovernanceDecision.Deny( |
| | 3 | 471 | | code, |
| | 3 | 472 | | message, |
| | 3 | 473 | | correlationId: currentDecision.CorrelationId, |
| | 3 | 474 | | traceId: currentDecision.TraceId, |
| | 3 | 475 | | policyVersion: currentDecision.PolicyVersion, |
| | 3 | 476 | | policyHash: currentDecision.PolicyHash); |
| | | 477 | | |
| | 3 | 478 | | return AsiBackboneEndpointGovernanceDevelopmentDiagnostics.IsEnabled(httpContext, endpointOptions) |
| | 3 | 479 | | ? AsiBackboneEndpointGovernanceResult.Block( |
| | 3 | 480 | | AsiBackboneEndpointGovernanceDevelopmentDiagnostics.CreateProblem( |
| | 3 | 481 | | httpContext, |
| | 3 | 482 | | endpointOptions, |
| | 3 | 483 | | descriptor, |
| | 3 | 484 | | decision, |
| | 3 | 485 | | decisionStage, |
| | 3 | 486 | | title: "Endpoint capability grant validation failed.", |
| | 3 | 487 | | detail: message, |
| | 3 | 488 | | statusCode: endpointOptions.CapabilityFailureStatusCode, |
| | 3 | 489 | | metadata: metadata), |
| | 3 | 490 | | decision) |
| | 3 | 491 | | : AsiBackboneEndpointGovernanceResult.Block( |
| | 3 | 492 | | Microsoft.AspNetCore.Http.Results.Problem( |
| | 3 | 493 | | title: "Endpoint capability grant validation failed.", |
| | 3 | 494 | | detail: message, |
| | 3 | 495 | | statusCode: endpointOptions.CapabilityFailureStatusCode, |
| | 3 | 496 | | extensions: new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 3 | 497 | | { |
| | 3 | 498 | | ["reasonCodes"] = decision.ReasonCodes, |
| | 3 | 499 | | ["outcome"] = decision.Outcome.ToString() |
| | 3 | 500 | | }), |
| | 3 | 501 | | decision); |
| | | 502 | | } |
| | | 503 | | } |