< Summary

Information
Class: AsiBackbone.Signing.LocalDevelopment.LocalDevelopmentSigningBuilderExtensions
Assembly: AsiBackbone.Signing.LocalDevelopment
File(s): /home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Signing.LocalDevelopment/LocalDevelopmentSigningBuilderExtensions.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 82
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UseLocalDevelopmentSigning(...)100%11100%
UseLocalDevelopmentSigning(...)100%11100%
ThrowIfProduction(...)87.5%88100%

File(s)

/home/runner/work/AsiBackbone/AsiBackbone/src/AsiBackbone.Signing.LocalDevelopment/LocalDevelopmentSigningBuilderExtensions.cs

#LineLine coverage
 1using AsiBackbone.Core.Signing;
 2using AsiBackbone.DependencyInjection;
 3using Microsoft.Extensions.DependencyInjection;
 4
 5namespace AsiBackbone.Signing.LocalDevelopment;
 6
 7/// <summary>
 8/// Provides explicit builder facade extension methods for local-development signing.
 9/// </summary>
 10public static class LocalDevelopmentSigningBuilderExtensions
 11{
 12    /// <summary>
 13    /// Adds local-development signing and verification through the AsiBackbone builder facade using default options.
 14    /// </summary>
 15    public static IAsiBackboneBuilder UseLocalDevelopmentSigning(this IAsiBackboneBuilder builder)
 16    {
 217        return builder.UseLocalDevelopmentSigning(LocalDevelopmentSigningOptions.Create());
 18    }
 19
 20    /// <summary>
 21    /// Adds local-development signing and verification through the AsiBackbone builder facade using configured options.
 22    /// </summary>
 23    /// <exception cref="InvalidOperationException">
 24    /// Thrown when the configured local-development signing options are invalid.
 25    /// </exception>
 26    public static IAsiBackboneBuilder UseLocalDevelopmentSigning(
 27        this IAsiBackboneBuilder builder,
 28        LocalDevelopmentSigningOptions options)
 29    {
 1430        ArgumentNullException.ThrowIfNull(builder);
 1231        ArgumentNullException.ThrowIfNull(options);
 32
 33        // Register a snapshot, and run the validation and production guard against that snapshot. Registering the
 34        // caller's instance let an assignment made after this call, such as setting AllowInProduction once the guard
 35        // had passed, change what the registered provider did.
 1136        LocalDevelopmentSigningOptions snapshot = options.Snapshot();
 1137        snapshot.Validate();
 1038        ThrowIfProduction(snapshot);
 39
 40        // The registered options instance is resolvable and mutable, so the service gets its own snapshot. Closing over
 41        // the registered instance let a change made to the resolved options before the service was first resolved
 42        // reach the provider.
 843        LocalDevelopmentSigningOptions serviceSnapshot = snapshot.Snapshot();
 44
 845        _ = builder.Services.AddSingleton(snapshot);
 846        _ = builder.Services.AddSingleton(serviceProvider => new LocalDevelopmentSigningService(
 847            serviceSnapshot,
 848            serviceProvider.GetService<TimeProvider>() ?? TimeProvider.System));
 849        _ = builder.Services.AddSingleton<IGovernanceSigningService>(serviceProvider =>
 850            serviceProvider.GetRequiredService<LocalDevelopmentSigningService>());
 851        _ = builder.Services.AddSingleton<IGovernanceSignatureVerificationService>(serviceProvider =>
 852            serviceProvider.GetRequiredService<LocalDevelopmentSigningService>());
 53
 854        return builder;
 55    }
 56
 57    /// <summary>
 58    /// Rejects registration of the local-development signing provider in a production environment.
 59    /// </summary>
 60    /// <remarks>
 61    /// The provider generates its key per process, so artifacts it signs stop verifying after a restart. An analyzer ca
 62    /// only see a call it can read; this guard covers the unconditional registration that reaches production at runtime
 63    /// </remarks>
 64    /// <exception cref="InvalidOperationException">The environment is production and the options do not allow it.</exce
 65    private static void ThrowIfProduction(LocalDevelopmentSigningOptions options)
 66    {
 1067        if (options.AllowInProduction)
 68        {
 169            return;
 70        }
 71
 972        string? environmentName = options.EnvironmentName
 973            ?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")
 974            ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
 75
 976        if (string.Equals(environmentName, "Production", StringComparison.OrdinalIgnoreCase))
 77        {
 278            throw new InvalidOperationException(
 279                "Local-development signing cannot be registered in the Production environment. Its key is generated per 
 80        }
 781    }
 82}