| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using Microsoft.AspNetCore.Http; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents normalized AsiBackbone governance metadata resolved from an ASP.NET Core endpoint. |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class AsiBackboneEndpointGovernanceDescriptor |
| | | 10 | | { |
| | 3 | 11 | | private static readonly ReadOnlyCollection<Type> EmptyPolicyTypes = Array.AsReadOnly(Array.Empty<Type>()); |
| | 3 | 12 | | private static readonly ReadOnlyCollection<string> EmptyScopes = Array.AsReadOnly(Array.Empty<string>()); |
| | | 13 | | |
| | | 14 | | private readonly IReadOnlyDictionary<string, string> fullMetadata; |
| | | 15 | | private readonly IReadOnlyDictionary<string, string> reducedMetadata; |
| | | 16 | | |
| | 50 | 17 | | private AsiBackboneEndpointGovernanceDescriptor( |
| | 50 | 18 | | string operationName, |
| | 50 | 19 | | IReadOnlyList<Type> policyTypes, |
| | 50 | 20 | | bool? shortCircuitOnFirstDenial, |
| | 50 | 21 | | bool requiresLiabilityHandshake, |
| | 50 | 22 | | IReadOnlyList<string> capabilityScopes, |
| | 50 | 23 | | bool emitGovernanceAudit) |
| | | 24 | | { |
| | 50 | 25 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | | 26 | | |
| | 50 | 27 | | OperationName = operationName.Trim(); |
| | 50 | 28 | | PolicyTypes = policyTypes; |
| | 50 | 29 | | ShortCircuitOnFirstDenial = shortCircuitOnFirstDenial; |
| | 50 | 30 | | RequiresLiabilityHandshake = requiresLiabilityHandshake; |
| | 50 | 31 | | CapabilityScopes = capabilityScopes; |
| | 50 | 32 | | EmitGovernanceAudit = emitGovernanceAudit; |
| | 50 | 33 | | reducedMetadata = CreateReducedMetadata(OperationName); |
| | 50 | 34 | | fullMetadata = CreateFullMetadata(); |
| | 50 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the operation name used for audit residue and acknowledgment challenge construction. |
| | | 39 | | /// </summary> |
| | 107 | 40 | | public string OperationName { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the policy marker or resolver types attached to the endpoint. |
| | | 44 | | /// </summary> |
| | 182 | 45 | | public IReadOnlyList<Type> PolicyTypes { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets an endpoint-scoped first-denial short-circuit preference, when endpoint metadata supplied one. |
| | | 49 | | /// </summary> |
| | 120 | 50 | | public bool? ShortCircuitOnFirstDenial { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a value indicating whether liability-handshake support is requested. |
| | | 54 | | /// </summary> |
| | 65 | 55 | | public bool RequiresLiabilityHandshake { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the required capability-grant scopes attached to the endpoint. |
| | | 59 | | /// </summary> |
| | 155 | 60 | | public IReadOnlyList<string> CapabilityScopes { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets a value indicating whether governance audit emission is requested. |
| | | 64 | | /// </summary> |
| | 69 | 65 | | public bool EmitGovernanceAudit { get; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets a value indicating whether the endpoint contains any AsiBackbone governance metadata. |
| | | 69 | | /// </summary> |
| | 36 | 70 | | public bool HasGovernanceMetadata => PolicyTypes.Count > 0 |
| | 36 | 71 | | || ShortCircuitOnFirstDenial.HasValue |
| | 36 | 72 | | || RequiresLiabilityHandshake |
| | 36 | 73 | | || CapabilityScopes.Count > 0 |
| | 36 | 74 | | || EmitGovernanceAudit; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Creates a descriptor from the selected ASP.NET Core endpoint. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="endpoint">The selected endpoint.</param> |
| | | 80 | | /// <returns>A normalized descriptor.</returns> |
| | | 81 | | public static AsiBackboneEndpointGovernanceDescriptor FromEndpoint(Endpoint? endpoint) |
| | | 82 | | { |
| | 49 | 83 | | if (endpoint is null) |
| | | 84 | | { |
| | 1 | 85 | | return None("unresolved-endpoint"); |
| | | 86 | | } |
| | | 87 | | |
| | 48 | 88 | | List<Type>? policyTypes = null; |
| | 144 | 89 | | foreach (IAsiBackboneEndpointGovernancePolicyMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IAsiBackb |
| | | 90 | | { |
| | 24 | 91 | | if (metadata.PolicyType is not Type policyType || ContainsPolicyType(policyTypes, policyType)) |
| | | 92 | | { |
| | | 93 | | continue; |
| | | 94 | | } |
| | | 95 | | |
| | 24 | 96 | | policyTypes ??= []; |
| | 24 | 97 | | policyTypes.Add(policyType); |
| | | 98 | | } |
| | | 99 | | |
| | 48 | 100 | | List<string>? capabilityScopes = null; |
| | 152 | 101 | | foreach (IAsiBackboneEndpointCapabilityGrantMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IAsiBackbo |
| | | 102 | | { |
| | 28 | 103 | | if (string.IsNullOrWhiteSpace(metadata.Scope)) |
| | | 104 | | { |
| | | 105 | | continue; |
| | | 106 | | } |
| | | 107 | | |
| | 28 | 108 | | string scope = metadata.Scope.Trim(); |
| | 28 | 109 | | if (ContainsScope(capabilityScopes, scope)) |
| | | 110 | | { |
| | | 111 | | continue; |
| | | 112 | | } |
| | | 113 | | |
| | 28 | 114 | | capabilityScopes ??= []; |
| | 28 | 115 | | capabilityScopes.Add(scope); |
| | | 116 | | } |
| | | 117 | | |
| | 48 | 118 | | bool? shortCircuitOnFirstDenial = null; |
| | 102 | 119 | | foreach (IAsiBackboneEndpointPolicyEvaluationOptionsMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IA |
| | | 120 | | { |
| | 3 | 121 | | shortCircuitOnFirstDenial = metadata.ShortCircuitOnFirstDenial; |
| | | 122 | | } |
| | | 123 | | |
| | 48 | 124 | | bool requiresLiabilityHandshake = false; |
| | 102 | 125 | | foreach (IAsiBackboneEndpointLiabilityHandshakeMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IAsiBac |
| | | 126 | | { |
| | 6 | 127 | | if (metadata.RequiresLiabilityHandshake) |
| | | 128 | | { |
| | 6 | 129 | | requiresLiabilityHandshake = true; |
| | 6 | 130 | | break; |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | 48 | 134 | | bool emitGovernanceAudit = false; |
| | 103 | 135 | | foreach (IAsiBackboneEndpointAuditEmissionMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IAsiBackbone |
| | | 136 | | { |
| | 7 | 137 | | if (metadata.EmitGovernanceAudit) |
| | | 138 | | { |
| | 7 | 139 | | emitGovernanceAudit = true; |
| | 7 | 140 | | break; |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | 48 | 144 | | return new AsiBackboneEndpointGovernanceDescriptor( |
| | 48 | 145 | | ResolveOperationName(endpoint), |
| | 48 | 146 | | policyTypes is null ? EmptyPolicyTypes : Array.AsReadOnly(policyTypes.ToArray()), |
| | 48 | 147 | | shortCircuitOnFirstDenial, |
| | 48 | 148 | | requiresLiabilityHandshake, |
| | 48 | 149 | | capabilityScopes is null ? EmptyScopes : Array.AsReadOnly(capabilityScopes.ToArray()), |
| | 48 | 150 | | emitGovernanceAudit); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Creates a descriptor that does not request governance handling. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <param name="operationName">The operation name to associate with the descriptor.</param> |
| | | 157 | | /// <returns>A descriptor with no governance metadata.</returns> |
| | | 158 | | public static AsiBackboneEndpointGovernanceDescriptor None(string operationName) |
| | | 159 | | { |
| | 2 | 160 | | return new AsiBackboneEndpointGovernanceDescriptor( |
| | 2 | 161 | | operationName, |
| | 2 | 162 | | EmptyPolicyTypes, |
| | 2 | 163 | | shortCircuitOnFirstDenial: null, |
| | 2 | 164 | | requiresLiabilityHandshake: false, |
| | 2 | 165 | | EmptyScopes, |
| | 2 | 166 | | emitGovernanceAudit: false); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Converts descriptor values into safe metadata for framework-neutral governance evaluation and audit residue. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <returns>A normalized metadata dictionary.</returns> |
| | | 173 | | public IReadOnlyDictionary<string, string> ToMetadata() |
| | | 174 | | { |
| | 6 | 175 | | return ToMetadata(AsiBackboneEndpointGovernanceMetadataMode.Full); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Converts descriptor values into safe metadata for framework-neutral governance evaluation and audit residue. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <param name="metadataMode">The endpoint metadata mode to apply.</param> |
| | | 182 | | /// <returns>A normalized metadata dictionary.</returns> |
| | | 183 | | public IReadOnlyDictionary<string, string> ToMetadata(AsiBackboneEndpointGovernanceMetadataMode metadataMode) |
| | | 184 | | { |
| | 29 | 185 | | return metadataMode switch |
| | 29 | 186 | | { |
| | 24 | 187 | | AsiBackboneEndpointGovernanceMetadataMode.Full => fullMetadata, |
| | 5 | 188 | | AsiBackboneEndpointGovernanceMetadataMode.Reduced => reducedMetadata, |
| | 0 | 189 | | _ => throw new ArgumentOutOfRangeException(nameof(metadataMode), metadataMode, "Endpoint governance metadata |
| | 29 | 190 | | }; |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | private static bool ContainsPolicyType(List<Type>? policyTypes, Type policyType) |
| | | 194 | | { |
| | 24 | 195 | | if (policyTypes is null) |
| | | 196 | | { |
| | 24 | 197 | | return false; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | foreach (Type currentPolicyType in policyTypes) |
| | | 201 | | { |
| | 0 | 202 | | if (currentPolicyType == policyType) |
| | | 203 | | { |
| | 0 | 204 | | return true; |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | return false; |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | private static bool ContainsScope(List<string>? capabilityScopes, string scope) |
| | | 212 | | { |
| | 28 | 213 | | if (capabilityScopes is null) |
| | | 214 | | { |
| | 28 | 215 | | return false; |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | foreach (string currentScope in capabilityScopes) |
| | | 219 | | { |
| | 0 | 220 | | if (string.Equals(currentScope, scope, StringComparison.Ordinal)) |
| | | 221 | | { |
| | 0 | 222 | | return true; |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | return false; |
| | 0 | 227 | | } |
| | | 228 | | |
| | | 229 | | private ReadOnlyDictionary<string, string> CreateFullMetadata() |
| | | 230 | | { |
| | 50 | 231 | | int metadataCapacity = 3 |
| | 50 | 232 | | + (PolicyTypes.Count > 0 ? 1 : 0) |
| | 50 | 233 | | + (ShortCircuitOnFirstDenial.HasValue ? 1 : 0) |
| | 50 | 234 | | + (CapabilityScopes.Count > 0 ? 1 : 0); |
| | | 235 | | |
| | 50 | 236 | | var metadata = new Dictionary<string, string>(metadataCapacity, StringComparer.Ordinal) |
| | 50 | 237 | | { |
| | 50 | 238 | | ["endpoint.operation_name"] = OperationName, |
| | 50 | 239 | | ["endpoint.requires_liability_handshake"] = RequiresLiabilityHandshake ? "true" : "false", |
| | 50 | 240 | | ["endpoint.emit_governance_audit"] = EmitGovernanceAudit ? "true" : "false" |
| | 50 | 241 | | }; |
| | | 242 | | |
| | 50 | 243 | | if (PolicyTypes.Count > 0) |
| | | 244 | | { |
| | 24 | 245 | | metadata["endpoint.policy_types"] = JoinPolicyTypeNames(PolicyTypes); |
| | | 246 | | } |
| | | 247 | | |
| | 50 | 248 | | if (ShortCircuitOnFirstDenial.HasValue) |
| | | 249 | | { |
| | 2 | 250 | | metadata["endpoint.short_circuit_on_first_denial"] = ShortCircuitOnFirstDenial.Value ? "true" : "false"; |
| | | 251 | | } |
| | | 252 | | |
| | 50 | 253 | | if (CapabilityScopes.Count > 0) |
| | | 254 | | { |
| | 28 | 255 | | metadata["endpoint.capability_scopes"] = string.Join(",", CapabilityScopes); |
| | | 256 | | } |
| | | 257 | | |
| | 50 | 258 | | return new ReadOnlyDictionary<string, string>(metadata); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | private static ReadOnlyDictionary<string, string> CreateReducedMetadata(string operationName) |
| | | 262 | | { |
| | 50 | 263 | | return new ReadOnlyDictionary<string, string>( |
| | 50 | 264 | | new Dictionary<string, string>(1, StringComparer.Ordinal) |
| | 50 | 265 | | { |
| | 50 | 266 | | ["endpoint.operation_name"] = operationName |
| | 50 | 267 | | }); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | private static string JoinPolicyTypeNames(IReadOnlyList<Type> policyTypes) |
| | | 271 | | { |
| | 24 | 272 | | string[] policyTypeNames = new string[policyTypes.Count]; |
| | | 273 | | |
| | 96 | 274 | | for (int index = 0; index < policyTypeNames.Length; index++) |
| | | 275 | | { |
| | 24 | 276 | | Type policyType = policyTypes[index]; |
| | 24 | 277 | | policyTypeNames[index] = policyType.FullName ?? policyType.Name; |
| | | 278 | | } |
| | | 279 | | |
| | 24 | 280 | | return string.Join(",", policyTypeNames); |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | private static string ResolveOperationName(Endpoint endpoint) |
| | | 284 | | { |
| | 48 | 285 | | return string.IsNullOrWhiteSpace(endpoint.DisplayName) |
| | 48 | 286 | | ? "aspnetcore.endpoint" |
| | 48 | 287 | | : endpoint.DisplayName.Trim(); |
| | | 288 | | } |
| | | 289 | | } |