< Summary - Envilder .NET SDK

Information
Class: Envilder.ContractValidation
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Application/ContractValidation.cs
Tag: 605_44106794472
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 91
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
InvalidMapFile(...)100%11100%
CheckRemoteKeysAsync()100%44100%
NotCheckable(...)100%11100%
ProfileNotFound(...)100%11100%
EveryKey(...)100%11100%
ToKeyCheck(...)100%22100%

File(s)

/home/runner/work/envilder/envilder/src/sdks/dotnet/Application/ContractValidation.cs

#LineLine coverage
 1namespace Envilder;
 2
 3using global::Envilder.Domain.Ports;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9internal static class ContractValidation
 10{
 11  private const string NoResultReason = "The provider returned no result for this secret.";
 12
 13  /// <summary>The result for a map file that failed validation: no provider was called.</summary>
 14  public static ContractValidationResult InvalidMapFile(IReadOnlyList<MapFileError> mapFileErrors)
 15  {
 116    return new(mapFileErrors, []);
 17  }
 18
 19  /// <summary>
 20  /// Checks that every secret path declared in a valid map file exists,
 21  /// without reading any value. A path shared by several variables is checked
 22  /// once. Returns one check per variable, in map-file order.
 23  /// </summary>
 24  public static async Task<ContractValidationResult> CheckRemoteKeysAsync(
 25    ParsedMapFile mapFile,
 26    ISecretExistenceChecker checker,
 27    SecretProviderType provider,
 28    CancellationToken cancellationToken)
 29  {
 130    var mappings = mapFile.Mappings.ToList();
 131    var existence = mappings.Count == 0
 132      ? new Dictionary<string, SecretExistence>()
 133      : await checker.CheckSecretsExistAsync(
 134        mappings.Select(mapping => mapping.Value).Distinct().ToList(),
 135        cancellationToken).ConfigureAwait(false);
 36
 137    var keys = mappings
 138      .Select(mapping => ToKeyCheck(mapping.Key, mapping.Value, provider, existence))
 139      .ToList();
 140    return new([], keys);
 141  }
 42
 43  /// <summary>
 44  /// The result for a provider that cannot check existence (a custom one
 45  /// that does not implement <see cref="ISecretExistenceChecker"/>): every
 46  /// key is a provider error.
 47  /// </summary>
 48  public static ContractValidationResult NotCheckable(ParsedMapFile mapFile, SecretProviderType provider)
 49  {
 150    return EveryKey(
 151      mapFile,
 152      provider,
 153      SecretExistenceStatus.ProviderError,
 154      "The provider cannot check whether a secret exists.");
 55  }
 56
 57  /// <summary>
 58  /// The result when the provider cannot be built because the AWS profile is
 59  /// not in the credential store: no key can be checked with those credentials.
 60  /// </summary>
 61  public static ContractValidationResult ProfileNotFound(
 62    ParsedMapFile mapFile,
 63    SecretProviderType provider,
 64    string reason)
 65  {
 166    return EveryKey(mapFile, provider, SecretExistenceStatus.AuthenticationFailed, reason);
 67  }
 68
 69  private static ContractValidationResult EveryKey(
 70    ParsedMapFile mapFile,
 71    SecretProviderType provider,
 72    SecretExistenceStatus status,
 73    string reason)
 74  {
 175    var keys = mapFile.Mappings
 176      .Select(mapping => new KeyCheck(mapping.Key, mapping.Value, provider, status, reason))
 177      .ToList();
 178    return new([], keys);
 79  }
 80
 81  private static KeyCheck ToKeyCheck(
 82    string variable,
 83    string path,
 84    SecretProviderType provider,
 85    IReadOnlyDictionary<string, SecretExistence> existence)
 86  {
 187    return existence.TryGetValue(path, out var found)
 188      ? new(variable, path, provider, found.Status, found.Reason)
 189      : new(variable, path, provider, SecretExistenceStatus.ProviderError, NoResultReason);
 90  }
 91}