< Summary - Envilder .NET SDK

Information
Class: Envilder.Application.EnvilderBuilder
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Application/EnvilderBuilder.cs
Tag: 299_25910610327
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 121
Line coverage: 100%
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
.ctor(...)100%11100%
WithProvider(...)100%11100%
WithProfile(...)100%11100%
WithVaultUrl(...)100%11100%
Resolve()100%11100%
ResolveAsync()100%11100%
Inject()100%11100%
InjectAsync()100%11100%

File(s)

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

#LineLine coverage
 1namespace Envilder.Application;
 2
 3using global::Envilder.Domain;
 4using global::Envilder.Infrastructure;
 5using System.Collections.Generic;
 6using System.IO;
 7using System.Threading;
 8using System.Threading.Tasks;
 9
 10/// <summary>
 11/// Fluent builder for configuring and resolving secrets from a map file.
 12/// Obtain an instance via <see cref="Envilder.FromMapFile(string)"/>.
 13/// </summary>
 14/// <example>
 15/// <code>
 16/// var secrets = Envilder.FromMapFile("envilder.json")
 17///     .WithProvider(SecretProviderType.Azure)
 18///     .WithVaultUrl("https://my-vault.vault.azure.net")
 19///     .Resolve();
 20/// </code>
 21/// </example>
 22public class EnvilderBuilder
 23{
 24  private readonly string _filePath;
 125  private readonly EnvilderOptions _options = new();
 26
 127  internal EnvilderBuilder(string filePath)
 28  {
 129    _filePath = filePath;
 130  }
 31
 32  /// <summary>
 33  /// Overrides the secret provider type specified in the map file's <c>$config</c>.
 34  /// </summary>
 35  /// <param name="provider">The provider to use (AWS or Azure).</param>
 36  /// <returns>This builder for chaining.</returns>
 37  public EnvilderBuilder WithProvider(SecretProviderType provider)
 38  {
 139    _options.Provider = provider;
 140    return this;
 41  }
 42
 43  /// <summary>
 44  /// Overrides the AWS named profile used for credential resolution.
 45  /// Only applicable when the provider is AWS.
 46  /// </summary>
 47  /// <param name="profile">The AWS profile name.</param>
 48  /// <returns>This builder for chaining.</returns>
 49  public EnvilderBuilder WithProfile(string profile)
 50  {
 151    _options.Profile = profile;
 152    return this;
 53  }
 54
 55  /// <summary>
 56  /// Overrides the Azure Key Vault URL used for secret retrieval.
 57  /// Only applicable when the provider is Azure.
 58  /// </summary>
 59  /// <param name="vaultUrl">The Key Vault URL (e.g. <c>https://my-vault.vault.azure.net</c>).</param>
 60  /// <returns>This builder for chaining.</returns>
 61  public EnvilderBuilder WithVaultUrl(string vaultUrl)
 62  {
 163    _options.VaultUrl = vaultUrl;
 164    return this;
 65  }
 66
 67  /// <summary>
 68  /// Resolves secrets from the map file using the configured overrides and
 69  /// returns them as a dictionary, without injecting into the environment.
 70  /// </summary>
 71  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 72  public IReadOnlyDictionary<string, string> Resolve()
 73  {
 174    Envilder.ValidateFileExists(_filePath);
 175    var json = File.ReadAllText(_filePath);
 176    var mapFile = new MapFileParser().Parse(json);
 177    var provider = SecretProviderFactory.Create(mapFile.Config, _options);
 178    return new Dictionary<string, string>(new EnvilderClient(provider).ResolveSecrets(mapFile));
 79  }
 80
 81  /// <summary>
 82  /// Asynchronously resolves secrets from the map file using the configured overrides.
 83  /// </summary>
 84  /// <param name="cancellationToken">Optional cancellation token.</param>
 85  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 86  public async Task<IReadOnlyDictionary<string, string>> ResolveAsync(
 87    CancellationToken cancellationToken = default)
 88  {
 189    Envilder.ValidateFileExists(_filePath);
 190    var json = await Envilder.ReadFileAsync(_filePath, cancellationToken).ConfigureAwait(false);
 191    var mapFile = new MapFileParser().Parse(json);
 192    var provider = SecretProviderFactory.Create(mapFile.Config, _options);
 193    var secrets = await new EnvilderClient(provider).ResolveSecretsAsync(mapFile, cancellationToken).ConfigureAwait(fals
 194    return new Dictionary<string, string>(secrets);
 195  }
 96
 97  /// <summary>
 98  /// Resolves secrets and injects them as process-level environment variables
 99  /// via <see cref="System.Environment.SetEnvironmentVariable(string, string)"/>.
 100  /// </summary>
 101  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 102  public IReadOnlyDictionary<string, string> Inject()
 103  {
 1104    var secrets = Resolve();
 1105    EnvilderClient.InjectIntoEnvironment(secrets);
 1106    return secrets;
 107  }
 108
 109  /// <summary>
 110  /// Asynchronously resolves secrets and injects them as process-level environment variables.
 111  /// </summary>
 112  /// <param name="cancellationToken">Optional cancellation token.</param>
 113  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 114  public async Task<IReadOnlyDictionary<string, string>> InjectAsync(
 115    CancellationToken cancellationToken = default)
 116  {
 1117    var secrets = await ResolveAsync(cancellationToken).ConfigureAwait(false);
 1118    EnvilderClient.InjectIntoEnvironment(secrets);
 1119    return secrets;
 1120  }
 121}