< Summary

Information
Class: AsiBackbone.AspNetCore.DependencyInjection.AsiBackboneStrictGovernanceServiceCollectionExtensions
Assembly: AsiBackbone.AspNetCore
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/DependencyInjection/AsiBackboneStrictGovernanceServiceCollectionExtensions.cs
Line coverage
88%
Covered lines: 30
Uncovered lines: 4
Coverable lines: 34
Total lines: 101
Line coverage: 88.2%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddAsiBackboneStrictGovernance(...)100%11100%
UseStrictGovernanceProfile(...)100%11100%
ApplyStrictPolicyEvaluatorProfile(...)100%11100%
ApplyStrictEndpointGovernanceProfile(...)100%11100%
ValidateOptions(...)100%1160%
ValidateOptions(...)100%1160%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.AspNetCore/DependencyInjection/AsiBackboneStrictGovernanceServiceCollectionExtensions.cs

#LineLine coverage
 1using AsiBackbone.AspNetCore.Endpoints;
 2using AsiBackbone.Core.Evaluation;
 3using AsiBackbone.DependencyInjection;
 4using Microsoft.Extensions.DependencyInjection;
 5
 6namespace AsiBackbone.AspNetCore.DependencyInjection;
 7
 8/// <summary>
 9/// Provides strict governance registration helpers for hosts that prefer fail-closed defaults.
 10/// </summary>
 11public static class AsiBackboneStrictGovernanceServiceCollectionExtensions
 12{
 13    /// <summary>
 14    /// Applies the strict governance profile for policy evaluation and ASP.NET Core endpoint governance options.
 15    /// </summary>
 16    /// <param name="services">The service collection to configure.</param>
 17    /// <returns>The same service collection so calls can be chained.</returns>
 18    /// <exception cref="ArgumentNullException">
 19    /// Thrown when <paramref name="services" /> is <see langword="null" />.
 20    /// </exception>
 21    /// <remarks>
 22    /// This helper keeps strict posture explicit in the current 3.x line while giving production hosts a single opt-in 
 23    /// fail-closed evaluation posture. It configures options only; hosts still own authentication, authorization,
 24    /// constraint registration, endpoint metadata, persistence, and execution enforcement.
 25    /// </remarks>
 26    public static IServiceCollection AddAsiBackboneStrictGovernance(this IServiceCollection services)
 27    {
 828        ArgumentNullException.ThrowIfNull(services);
 29
 830        _ = services.AddOptions<AsiBackbonePolicyEvaluatorOptions>()
 831            .Configure(ApplyStrictPolicyEvaluatorProfile)
 532            .Validate(static options => ValidateOptions(options), "Policy evaluator options must be valid.")
 833            .ValidateOnStart();
 34
 835        _ = services.AddOptions<AsiBackboneEndpointGovernanceOptions>()
 836            .Configure(ApplyStrictEndpointGovernanceProfile)
 637            .Validate(static options => ValidateOptions(options), "Endpoint governance options must be valid.")
 838            .ValidateOnStart();
 39
 840        return services;
 41    }
 42
 43    /// <summary>
 44    /// Applies the strict governance profile through the explicit <c>AddAsiBackbone</c> builder facade.
 45    /// </summary>
 46    /// <param name="builder">The AsiBackbone builder facade being configured.</param>
 47    /// <returns>The same builder so calls can be chained.</returns>
 48    /// <exception cref="ArgumentNullException">
 49    /// Thrown when <paramref name="builder" /> is <see langword="null" />.
 50    /// </exception>
 51    public static IAsiBackboneBuilder UseStrictGovernanceProfile(this IAsiBackboneBuilder builder)
 52    {
 153        ArgumentNullException.ThrowIfNull(builder);
 54
 155        _ = builder.Services.AddAsiBackboneStrictGovernance();
 156        return builder;
 57    }
 58
 59    private static void ApplyStrictPolicyEvaluatorProfile(AsiBackbonePolicyEvaluatorOptions options)
 60    {
 561        options.DenyWhenNoConstraints = true;
 562        options.TreatConstraintExceptionAsDenial = true;
 563        options.TreatThreatContributorExceptionAsDenial = true;
 564        options.PreventThreatAssessmentAllowDowngrade = true;
 565    }
 66
 67    private static void ApplyStrictEndpointGovernanceProfile(AsiBackboneEndpointGovernanceOptions options)
 68    {
 669        options.FailClosedWhenPolicyEvaluatorMissing = true;
 670        options.FailClosedWhenCapabilityValidatorMissing = true;
 671        options.FailClosedWhenAuditSinkMissing = true;
 672        options.RequireGovernanceMetadata = true;
 673        options.IncludeDevelopmentDiagnosticsMetadataValues = false;
 674    }
 75
 76    private static bool ValidateOptions(AsiBackbonePolicyEvaluatorOptions options)
 77    {
 78        try
 79        {
 580            options.Validate();
 581            return true;
 82        }
 083        catch (InvalidOperationException)
 84        {
 085            return false;
 86        }
 587    }
 88
 89    private static bool ValidateOptions(AsiBackboneEndpointGovernanceOptions options)
 90    {
 91        try
 92        {
 693            options.Validate();
 694            return true;
 95        }
 096        catch (InvalidOperationException)
 97        {
 098            return false;
 99        }
 6100    }
 101}