| | | 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 Microsoft.AspNetCore.Http; |
| | | 11 | | using Microsoft.Extensions.DependencyInjection; |
| | | 12 | | using Microsoft.Extensions.Options; |
| | | 13 | | |
| | | 14 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Default host-adapter implementation for ergonomic ASP.NET Core endpoint governance. |
| | | 18 | | /// </summary> |
| | | 19 | | public sealed class DefaultAsiBackboneEndpointGovernanceService : IAsiBackboneEndpointGovernanceService |
| | | 20 | | { |
| | | 21 | | private readonly IServiceProvider serviceProvider; |
| | | 22 | | private readonly IAsiBackboneHttpActorContextResolver actorContextResolver; |
| | | 23 | | private readonly IAsiBackboneHttpRequestCorrelationResolver requestCorrelationResolver; |
| | | 24 | | private readonly IAsiBackboneAcknowledgmentChallengeService acknowledgmentChallengeService; |
| | | 25 | | private readonly AsiBackboneEndpointGovernanceOptions endpointOptions; |
| | | 26 | | private readonly AsiBackboneHttpResultMappingOptions resultOptions; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="DefaultAsiBackboneEndpointGovernanceService" /> class. |
| | | 30 | | /// </summary> |
| | 9 | 31 | | public DefaultAsiBackboneEndpointGovernanceService( |
| | 9 | 32 | | IServiceProvider serviceProvider, |
| | 9 | 33 | | IAsiBackboneHttpActorContextResolver actorContextResolver, |
| | 9 | 34 | | IAsiBackboneHttpRequestCorrelationResolver requestCorrelationResolver, |
| | 9 | 35 | | IAsiBackboneAcknowledgmentChallengeService acknowledgmentChallengeService, |
| | 9 | 36 | | IOptions<AsiBackboneEndpointGovernanceOptions> endpointOptions, |
| | 9 | 37 | | IOptions<AsiBackboneHttpResultMappingOptions> resultOptions) |
| | | 38 | | { |
| | 9 | 39 | | this.serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); |
| | 9 | 40 | | this.actorContextResolver = actorContextResolver ?? throw new ArgumentNullException(nameof(actorContextResolver) |
| | 9 | 41 | | this.requestCorrelationResolver = requestCorrelationResolver ?? throw new ArgumentNullException(nameof(requestCo |
| | 9 | 42 | | this.acknowledgmentChallengeService = acknowledgmentChallengeService ?? throw new ArgumentNullException(nameof(a |
| | 9 | 43 | | this.endpointOptions = endpointOptions?.Value ?? throw new ArgumentNullException(nameof(endpointOptions)); |
| | 9 | 44 | | this.resultOptions = resultOptions?.Value ?? throw new ArgumentNullException(nameof(resultOptions)); |
| | 9 | 45 | | this.endpointOptions.Validate(); |
| | 9 | 46 | | this.resultOptions.Validate(); |
| | 9 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public async ValueTask<AsiBackboneEndpointGovernanceResult> EvaluateAsync( |
| | | 51 | | HttpContext httpContext, |
| | | 52 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 53 | | CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | 9 | 55 | | ArgumentNullException.ThrowIfNull(httpContext); |
| | 9 | 56 | | ArgumentNullException.ThrowIfNull(descriptor); |
| | 9 | 57 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 58 | | |
| | 9 | 59 | | if (!descriptor.HasGovernanceMetadata) |
| | | 60 | | { |
| | 0 | 61 | | return AsiBackboneEndpointGovernanceResult.Allow(); |
| | | 62 | | } |
| | | 63 | | |
| | 9 | 64 | | AsiBackboneHttpRequestCorrelation correlation = requestCorrelationResolver.ResolveRequestCorrelation(); |
| | 9 | 65 | | IReadOnlyDictionary<string, string> endpointMetadata = descriptor.ToMetadata(); |
| | 9 | 66 | | AsiBackboneConstraintEvaluationContext evaluationContext = correlation.ToEvaluationContext( |
| | 9 | 67 | | endpointOptions.PolicyVersion, |
| | 9 | 68 | | endpointOptions.PolicyHash, |
| | 9 | 69 | | endpointMetadata); |
| | | 70 | | |
| | 9 | 71 | | var decision = GovernanceDecision.Allow( |
| | 9 | 72 | | correlationId: evaluationContext.CorrelationId, |
| | 9 | 73 | | traceId: correlation.TraceId, |
| | 9 | 74 | | policyVersion: evaluationContext.PolicyVersion, |
| | 9 | 75 | | policyHash: evaluationContext.PolicyHash); |
| | | 76 | | |
| | 9 | 77 | | if (descriptor.PolicyTypes.Count > 0) |
| | | 78 | | { |
| | 5 | 79 | | IAsiBackbonePolicyEvaluator<AsiBackboneConstraintEvaluationContext>? evaluator = |
| | 5 | 80 | | httpContext.RequestServices.GetService<IAsiBackbonePolicyEvaluator<AsiBackboneConstraintEvaluationContex |
| | | 81 | | |
| | 5 | 82 | | if (evaluator is null) |
| | | 83 | | { |
| | 0 | 84 | | return endpointOptions.FailClosedWhenPolicyEvaluatorMissing |
| | 0 | 85 | | ? CreateConfigurationFailure( |
| | 0 | 86 | | httpContext, |
| | 0 | 87 | | descriptor, |
| | 0 | 88 | | endpointMetadata, |
| | 0 | 89 | | "endpoint.policy_evaluator.missing", |
| | 0 | 90 | | "Endpoint governance policy metadata was present, but no AsiBackbone policy evaluator was regist |
| | 0 | 91 | | decision, |
| | 0 | 92 | | "aspnetcore.endpoint.governance.configuration.policy_evaluator") |
| | 0 | 93 | | : AsiBackboneEndpointGovernanceResult.Allow(decision); |
| | | 94 | | } |
| | | 95 | | |
| | 5 | 96 | | decision = await evaluator |
| | 5 | 97 | | .EvaluateAsync(evaluationContext, cancellationToken) |
| | 5 | 98 | | .ConfigureAwait(false); |
| | | 99 | | } |
| | | 100 | | |
| | 9 | 101 | | if (decision.CanProceed && descriptor.CapabilityScopes.Count > 0) |
| | | 102 | | { |
| | 6 | 103 | | IAsiBackboneEndpointCapabilityGrantValidator? capabilityValidator = httpContext.RequestServices.GetService<I |
| | | 104 | | |
| | 6 | 105 | | if (capabilityValidator is null) |
| | | 106 | | { |
| | 4 | 107 | | return endpointOptions.FailClosedWhenCapabilityValidatorMissing |
| | 4 | 108 | | ? CreateCapabilityFailure( |
| | 4 | 109 | | httpContext, |
| | 4 | 110 | | descriptor, |
| | 4 | 111 | | endpointMetadata, |
| | 4 | 112 | | "endpoint.capability_validator.missing", |
| | 4 | 113 | | "Endpoint capability metadata was present, but no host-owned endpoint capability validator was r |
| | 4 | 114 | | decision, |
| | 4 | 115 | | "aspnetcore.endpoint.governance.capability.configuration") |
| | 4 | 116 | | : AsiBackboneEndpointGovernanceResult.Allow(decision); |
| | | 117 | | } |
| | | 118 | | |
| | 2 | 119 | | decision = await capabilityValidator |
| | 2 | 120 | | .ValidateAsync(httpContext, descriptor, decision, cancellationToken) |
| | 2 | 121 | | .ConfigureAwait(false); |
| | | 122 | | } |
| | | 123 | | |
| | 5 | 124 | | IAsiBackboneActorContext actor = actorContextResolver.ResolveActorContext(); |
| | | 125 | | |
| | 5 | 126 | | if (descriptor.EmitGovernanceAudit) |
| | | 127 | | { |
| | 1 | 128 | | IAsiBackboneAuditSink? auditSink = serviceProvider.GetService<IAsiBackboneAuditSink>(); |
| | 1 | 129 | | if (auditSink is null) |
| | | 130 | | { |
| | 0 | 131 | | return endpointOptions.FailClosedWhenAuditSinkMissing |
| | 0 | 132 | | ? CreateConfigurationFailure( |
| | 0 | 133 | | httpContext, |
| | 0 | 134 | | descriptor, |
| | 0 | 135 | | endpointMetadata, |
| | 0 | 136 | | "endpoint.audit_sink.missing", |
| | 0 | 137 | | "Endpoint governance audit emission was requested, but no host-owned audit sink was registered." |
| | 0 | 138 | | decision, |
| | 0 | 139 | | "aspnetcore.endpoint.governance.configuration.audit_sink") |
| | 0 | 140 | | : AsiBackboneEndpointGovernanceResult.Allow(decision); |
| | | 141 | | } |
| | | 142 | | |
| | 1 | 143 | | var residue = AuditResidue.FromDecision( |
| | 1 | 144 | | actor, |
| | 1 | 145 | | descriptor.OperationName, |
| | 1 | 146 | | decision, |
| | 1 | 147 | | metadata: endpointMetadata, |
| | 1 | 148 | | decisionStage: "aspnetcore.endpoint.governance"); |
| | | 149 | | |
| | 1 | 150 | | await auditSink.WriteAsync(residue, cancellationToken).ConfigureAwait(false); |
| | | 151 | | } |
| | | 152 | | |
| | 5 | 153 | | if (decision.RequiresAcknowledgment && descriptor.RequiresLiabilityHandshake) |
| | | 154 | | { |
| | 0 | 155 | | AsiBackboneAcknowledgmentChallenge challenge = acknowledgmentChallengeService.CreateChallenge( |
| | 0 | 156 | | actor, |
| | 0 | 157 | | descriptor.OperationName, |
| | 0 | 158 | | decision, |
| | 0 | 159 | | endpointMetadata); |
| | | 160 | | |
| | 0 | 161 | | IResult challengeResult = Microsoft.AspNetCore.Http.Results.Json( |
| | 0 | 162 | | challenge, |
| | 0 | 163 | | statusCode: endpointOptions.AcknowledgmentChallengeStatusCode); |
| | | 164 | | |
| | 0 | 165 | | return AsiBackboneEndpointGovernanceResult.Challenge(challenge, challengeResult, decision); |
| | | 166 | | } |
| | | 167 | | |
| | 5 | 168 | | return decision.CanProceed |
| | 5 | 169 | | ? AsiBackboneEndpointGovernanceResult.Allow(decision) |
| | 5 | 170 | | : CreateBlockedDecisionResult(decision); |
| | 9 | 171 | | } |
| | | 172 | | |
| | | 173 | | private AsiBackboneEndpointGovernanceResult CreateBlockedDecisionResult(GovernanceDecision decision) |
| | | 174 | | { |
| | 4 | 175 | | return decision.IsDenied && resultOptions.DeniedStatusCode == StatusCodes.Status403Forbidden |
| | 4 | 176 | | ? AsiBackboneEndpointGovernanceResult.BlockWithDefaultFailure(decision) |
| | 4 | 177 | | : AsiBackboneEndpointGovernanceResult.Block(decision.ToHttpResult(resultOptions), decision); |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private AsiBackboneEndpointGovernanceResult CreateConfigurationFailure( |
| | | 181 | | HttpContext httpContext, |
| | | 182 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 183 | | IReadOnlyDictionary<string, string> metadata, |
| | | 184 | | string code, |
| | | 185 | | string message, |
| | | 186 | | GovernanceDecision currentDecision, |
| | | 187 | | string decisionStage) |
| | | 188 | | { |
| | 0 | 189 | | var decision = GovernanceDecision.Deny( |
| | 0 | 190 | | code, |
| | 0 | 191 | | message, |
| | 0 | 192 | | correlationId: currentDecision.CorrelationId, |
| | 0 | 193 | | traceId: currentDecision.TraceId, |
| | 0 | 194 | | policyVersion: currentDecision.PolicyVersion, |
| | 0 | 195 | | policyHash: currentDecision.PolicyHash); |
| | | 196 | | |
| | 0 | 197 | | return AsiBackboneEndpointGovernanceDevelopmentDiagnostics.IsEnabled(httpContext, endpointOptions) |
| | 0 | 198 | | ? AsiBackboneEndpointGovernanceResult.Block( |
| | 0 | 199 | | AsiBackboneEndpointGovernanceDevelopmentDiagnostics.CreateProblem( |
| | 0 | 200 | | httpContext, |
| | 0 | 201 | | endpointOptions, |
| | 0 | 202 | | descriptor, |
| | 0 | 203 | | decision, |
| | 0 | 204 | | decisionStage, |
| | 0 | 205 | | title: "Endpoint governance configuration is incomplete.", |
| | 0 | 206 | | detail: message, |
| | 0 | 207 | | statusCode: endpointOptions.ConfigurationFailureStatusCode, |
| | 0 | 208 | | metadata: metadata), |
| | 0 | 209 | | decision) |
| | 0 | 210 | | : AsiBackboneEndpointGovernanceResult.Block( |
| | 0 | 211 | | Microsoft.AspNetCore.Http.Results.Problem( |
| | 0 | 212 | | title: "Endpoint governance configuration is incomplete.", |
| | 0 | 213 | | detail: message, |
| | 0 | 214 | | statusCode: endpointOptions.ConfigurationFailureStatusCode, |
| | 0 | 215 | | extensions: new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 0 | 216 | | { |
| | 0 | 217 | | ["reasonCodes"] = decision.ReasonCodes, |
| | 0 | 218 | | ["outcome"] = decision.Outcome.ToString() |
| | 0 | 219 | | }), |
| | 0 | 220 | | decision); |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | private AsiBackboneEndpointGovernanceResult CreateCapabilityFailure( |
| | | 224 | | HttpContext httpContext, |
| | | 225 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 226 | | IReadOnlyDictionary<string, string> metadata, |
| | | 227 | | string code, |
| | | 228 | | string message, |
| | | 229 | | GovernanceDecision currentDecision, |
| | | 230 | | string decisionStage) |
| | | 231 | | { |
| | 4 | 232 | | var decision = GovernanceDecision.Deny( |
| | 4 | 233 | | code, |
| | 4 | 234 | | message, |
| | 4 | 235 | | correlationId: currentDecision.CorrelationId, |
| | 4 | 236 | | traceId: currentDecision.TraceId, |
| | 4 | 237 | | policyVersion: currentDecision.PolicyVersion, |
| | 4 | 238 | | policyHash: currentDecision.PolicyHash); |
| | | 239 | | |
| | 4 | 240 | | return AsiBackboneEndpointGovernanceDevelopmentDiagnostics.IsEnabled(httpContext, endpointOptions) |
| | 4 | 241 | | ? AsiBackboneEndpointGovernanceResult.Block( |
| | 4 | 242 | | AsiBackboneEndpointGovernanceDevelopmentDiagnostics.CreateProblem( |
| | 4 | 243 | | httpContext, |
| | 4 | 244 | | endpointOptions, |
| | 4 | 245 | | descriptor, |
| | 4 | 246 | | decision, |
| | 4 | 247 | | decisionStage, |
| | 4 | 248 | | title: "Endpoint capability grant validation failed.", |
| | 4 | 249 | | detail: message, |
| | 4 | 250 | | statusCode: endpointOptions.CapabilityFailureStatusCode, |
| | 4 | 251 | | metadata: metadata), |
| | 4 | 252 | | decision) |
| | 4 | 253 | | : AsiBackboneEndpointGovernanceResult.Block( |
| | 4 | 254 | | Microsoft.AspNetCore.Http.Results.Problem( |
| | 4 | 255 | | title: "Endpoint capability grant validation failed.", |
| | 4 | 256 | | detail: message, |
| | 4 | 257 | | statusCode: endpointOptions.CapabilityFailureStatusCode, |
| | 4 | 258 | | extensions: new Dictionary<string, object?>(StringComparer.Ordinal) |
| | 4 | 259 | | { |
| | 4 | 260 | | ["reasonCodes"] = decision.ReasonCodes, |
| | 4 | 261 | | ["outcome"] = decision.Outcome.ToString() |
| | 4 | 262 | | }), |
| | 4 | 263 | | decision); |
| | | 264 | | } |
| | | 265 | | } |