< Summary

Information
Class: AsiBackbone.AspNetCore.Endpoints.EndpointGovernanceDescriptor
Assembly: AsiBackbone.AspNetCore
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Endpoints/EndpointGovernanceDescriptor.cs
Line coverage
93%
Covered lines: 110
Uncovered lines: 8
Coverable lines: 118
Total lines: 300
Line coverage: 93.2%
Branch coverage
88%
Covered branches: 69
Total branches: 78
Branch coverage: 88.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor(...)100%11100%
get_HasGovernanceMetadata()100%88100%
FromEndpoint(...)100%3232100%
None(...)100%11100%
ToMetadata()100%11100%
ToMetadata(...)75%4483.33%
ContainsPolicyType(...)83.33%7671.43%
ContainsScope(...)16.67%19628.57%
CreateFullMetadata()100%1212100%
CreateReducedMetadata(...)100%44100%
JoinPolicyTypeNames(...)75%44100%
ResolveOperationName(...)50%22100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/Endpoints/EndpointGovernanceDescriptor.cs

#LineLine coverage
 1using System.Collections.ObjectModel;
 2using Microsoft.AspNetCore.Http;
 3
 4namespace AsiBackbone.AspNetCore.Endpoints;
 5
 6/// <summary>
 7/// Represents normalized AsiBackbone governance metadata resolved from an ASP.NET Core endpoint.
 8/// </summary>
 9public sealed class EndpointGovernanceDescriptor
 10{
 211    private static readonly ReadOnlyCollection<Type> EmptyPolicyTypes = Array.AsReadOnly(Array.Empty<Type>());
 212    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
 12617    private EndpointGovernanceDescriptor(
 12618        string operationName,
 12619        IReadOnlyList<Type> policyTypes,
 12620        bool? shortCircuitOnFirstDenial,
 12621        bool requiresAcknowledgment,
 12622        IReadOnlyList<string> capabilityScopes,
 12623        bool emitGovernanceAudit)
 24    {
 12625        ArgumentException.ThrowIfNullOrWhiteSpace(operationName);
 26
 12627        OperationName = operationName.Trim();
 12628        PolicyTypes = policyTypes;
 12629        ShortCircuitOnFirstDenial = shortCircuitOnFirstDenial;
 12630        RequiresAcknowledgment = requiresAcknowledgment;
 12631        CapabilityScopes = capabilityScopes;
 12632        EmitGovernanceAudit = emitGovernanceAudit;
 12633        reducedMetadata = CreateReducedMetadata(OperationName, PolicyTypes);
 12634        fullMetadata = CreateFullMetadata();
 12635    }
 36
 37    /// <summary>
 38    /// Gets the operation name used for decision receipt and acknowledgment challenge construction.
 39    /// </summary>
 40    public string OperationName { get; }
 41
 42    /// <summary>
 43    /// Gets the policy marker or resolver types attached to the endpoint.
 44    /// </summary>
 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>
 50    public bool? ShortCircuitOnFirstDenial { get; }
 51
 52    /// <summary>
 53    /// Gets a value indicating whether liability-handshake support is requested.
 54    /// </summary>
 55    public bool RequiresAcknowledgment { get; }
 56
 57    /// <summary>
 58    /// Gets the required capability-grant scopes attached to the endpoint.
 59    /// </summary>
 60    public IReadOnlyList<string> CapabilityScopes { get; }
 61
 62    /// <summary>
 63    /// Gets a value indicating whether governance audit emission is requested.
 64    /// </summary>
 65    public bool EmitGovernanceAudit { get; }
 66
 67    /// <summary>
 68    /// Gets a value indicating whether the endpoint contains any AsiBackbone governance metadata.
 69    /// </summary>
 6770    public bool HasGovernanceMetadata => PolicyTypes.Count > 0
 6771        || ShortCircuitOnFirstDenial.HasValue
 6772        || RequiresAcknowledgment
 6773        || CapabilityScopes.Count > 0
 6774        || 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 EndpointGovernanceDescriptor FromEndpoint(Endpoint? endpoint)
 82    {
 12183        if (endpoint is null)
 84        {
 485            return None("unresolved-endpoint");
 86        }
 87
 11788        List<Type>? policyTypes = null;
 42689        foreach (IEndpointGovernancePolicyMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IEndpointGovernanceP
 90        {
 9691            if (metadata.PolicyType is not Type policyType || ContainsPolicyType(policyTypes, policyType))
 92            {
 93                continue;
 94            }
 95
 9696            policyTypes ??= [];
 9697            policyTypes.Add(policyType);
 98        }
 99
 117100        List<string>? capabilityScopes = null;
 394101        foreach (IEndpointCapabilityGrantMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IEndpointCapabilityGr
 102        {
 80103            if (string.IsNullOrWhiteSpace(metadata.Scope))
 104            {
 105                continue;
 106            }
 107
 80108            string scope = metadata.Scope.Trim();
 80109            if (ContainsScope(capabilityScopes, scope))
 110            {
 111                continue;
 112            }
 113
 80114            capabilityScopes ??= [];
 80115            capabilityScopes.Add(scope);
 116        }
 117
 117118        bool? shortCircuitOnFirstDenial = null;
 240119        foreach (IEndpointPolicyEvaluationOptionsMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IEndpointPoli
 120        {
 3121            shortCircuitOnFirstDenial = metadata.ShortCircuitOnFirstDenial;
 122        }
 123
 117124        bool requiresAcknowledgment = false;
 248125        foreach (IEndpointAcknowledgmentMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IEndpointAcknowledgmen
 126        {
 14127            if (metadata.RequiresAcknowledgment)
 128            {
 14129                requiresAcknowledgment = true;
 14130                break;
 131            }
 132        }
 133
 117134        bool emitGovernanceAudit = false;
 296135        foreach (IEndpointAuditEmissionMetadata metadata in endpoint.Metadata.GetOrderedMetadata<IEndpointAuditEmissionM
 136        {
 62137            if (metadata.EmitGovernanceAudit)
 138            {
 62139                emitGovernanceAudit = true;
 62140                break;
 141            }
 142        }
 143
 117144        return new EndpointGovernanceDescriptor(
 117145            ResolveOperationName(endpoint),
 117146            policyTypes is null ? EmptyPolicyTypes : Array.AsReadOnly(policyTypes.ToArray()),
 117147            shortCircuitOnFirstDenial,
 117148            requiresAcknowledgment,
 117149            capabilityScopes is null ? EmptyScopes : Array.AsReadOnly(capabilityScopes.ToArray()),
 117150            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 EndpointGovernanceDescriptor None(string operationName)
 159    {
 9160        return new EndpointGovernanceDescriptor(
 9161            operationName,
 9162            EmptyPolicyTypes,
 9163            shortCircuitOnFirstDenial: null,
 9164            requiresAcknowledgment: false,
 9165            EmptyScopes,
 9166            emitGovernanceAudit: false);
 167    }
 168
 169    /// <summary>
 170    /// Converts descriptor values into safe metadata for framework-neutral governance evaluation and decision receipt.
 171    /// </summary>
 172    /// <returns>A normalized metadata dictionary.</returns>
 173    public IReadOnlyDictionary<string, string> ToMetadata()
 174    {
 7175        return ToMetadata(EndpointGovernanceMetadataMode.Full);
 176    }
 177
 178    /// <summary>
 179    /// Converts descriptor values into safe metadata for framework-neutral governance evaluation and decision receipt.
 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(EndpointGovernanceMetadataMode metadataMode)
 184    {
 63185        return metadataMode switch
 63186        {
 56187            EndpointGovernanceMetadataMode.Full => fullMetadata,
 7188            EndpointGovernanceMetadataMode.Reduced => reducedMetadata,
 0189            _ => throw new ArgumentOutOfRangeException(nameof(metadataMode), metadataMode, "Endpoint governance metadata
 63190        };
 191    }
 192
 193    private static bool ContainsPolicyType(List<Type>? policyTypes, Type policyType)
 194    {
 96195        if (policyTypes is null)
 196        {
 95197            return false;
 198        }
 199
 4200        foreach (Type currentPolicyType in policyTypes)
 201        {
 1202            if (currentPolicyType == policyType)
 203            {
 0204                return true;
 205            }
 206        }
 207
 1208        return false;
 0209    }
 210
 211    private static bool ContainsScope(List<string>? capabilityScopes, string scope)
 212    {
 80213        if (capabilityScopes is null)
 214        {
 80215            return false;
 216        }
 217
 0218        foreach (string currentScope in capabilityScopes)
 219        {
 0220            if (string.Equals(currentScope, scope, StringComparison.Ordinal))
 221            {
 0222                return true;
 223            }
 224        }
 225
 0226        return false;
 0227    }
 228
 229    private ReadOnlyDictionary<string, string> CreateFullMetadata()
 230    {
 126231        int metadataCapacity = 3
 126232            + (PolicyTypes.Count > 0 ? 1 : 0)
 126233            + (ShortCircuitOnFirstDenial.HasValue ? 1 : 0)
 126234            + (CapabilityScopes.Count > 0 ? 1 : 0);
 235
 126236        var metadata = new Dictionary<string, string>(metadataCapacity, StringComparer.Ordinal)
 126237        {
 126238            ["endpoint.operation_name"] = OperationName,
 126239            ["endpoint.requires_liability_handshake"] = RequiresAcknowledgment ? "true" : "false",
 126240            ["endpoint.emit_governance_audit"] = EmitGovernanceAudit ? "true" : "false"
 126241        };
 242
 126243        if (PolicyTypes.Count > 0)
 244        {
 95245            metadata["endpoint.policy_types"] = JoinPolicyTypeNames(PolicyTypes);
 246        }
 247
 126248        if (ShortCircuitOnFirstDenial.HasValue)
 249        {
 2250            metadata["endpoint.short_circuit_on_first_denial"] = ShortCircuitOnFirstDenial.Value ? "true" : "false";
 251        }
 252
 126253        if (CapabilityScopes.Count > 0)
 254        {
 80255            metadata["endpoint.capability_scopes"] = string.Join(",", CapabilityScopes);
 256        }
 257
 126258        return new ReadOnlyDictionary<string, string>(metadata);
 259    }
 260
 261    private static ReadOnlyDictionary<string, string> CreateReducedMetadata(
 262        string operationName,
 263        IReadOnlyList<Type> policyTypes)
 264    {
 265        // Policy types are retained in Reduced mode. A host decision policy that varies its outcome by
 266        // endpoint.policy_types would otherwise stop seeing the marker under Reduced and silently fall back to
 267        // uniform behavior, which is a permissive change made by a metadata setting rather than a policy one.
 126268        var metadata = new Dictionary<string, string>(policyTypes.Count > 0 ? 2 : 1, StringComparer.Ordinal)
 126269        {
 126270            ["endpoint.operation_name"] = operationName
 126271        };
 272
 126273        if (policyTypes.Count > 0)
 274        {
 95275            metadata["endpoint.policy_types"] = JoinPolicyTypeNames(policyTypes);
 276        }
 277
 126278        return new ReadOnlyDictionary<string, string>(metadata);
 279    }
 280
 281    private static string JoinPolicyTypeNames(IReadOnlyList<Type> policyTypes)
 282    {
 190283        string[] policyTypeNames = new string[policyTypes.Count];
 284
 764285        for (int index = 0; index < policyTypeNames.Length; index++)
 286        {
 192287            Type policyType = policyTypes[index];
 192288            policyTypeNames[index] = policyType.FullName ?? policyType.Name;
 289        }
 290
 190291        return string.Join(",", policyTypeNames);
 292    }
 293
 294    private static string ResolveOperationName(Endpoint endpoint)
 295    {
 117296        return string.IsNullOrWhiteSpace(endpoint.DisplayName)
 117297            ? "aspnetcore.endpoint"
 117298            : endpoint.DisplayName.Trim();
 299    }
 300}