< Summary - Envilder .NET SDK

Information
Class: Envilder.EnvilderBuilder
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Application/EnvilderBuilder.cs
Tag: 352_26963168797
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 120
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;
 2
 3using global::Envilder.Infrastructure;
 4using System.Collections.Generic;
 5using System.IO;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9/// <summary>
 10/// Fluent builder for configuring and resolving secrets from a map file.
 11/// Obtain an instance via <see cref="Env.FromMapFile(string)"/>.
 12/// </summary>
 13/// <example>
 14/// <code>
 15/// var secrets = Env.FromMapFile("envilder.json")
 16///     .WithProvider(SecretProviderType.Azure)
 17///     .WithVaultUrl("https://my-vault.vault.azure.net")
 18///     .Resolve();
 19/// </code>
 20/// </example>
 21public class EnvilderBuilder
 22{
 23  private readonly string _filePath;
 124  private readonly EnvilderOptions _options = new();
 25
 126  internal EnvilderBuilder(string filePath)
 27  {
 128    _filePath = filePath;
 129  }
 30
 31  /// <summary>
 32  /// Overrides the secret provider type specified in the map file's <c>$config</c>.
 33  /// </summary>
 34  /// <param name="provider">The provider to use (AWS or Azure).</param>
 35  /// <returns>This builder for chaining.</returns>
 36  public EnvilderBuilder WithProvider(SecretProviderType provider)
 37  {
 138    _options.Provider = provider;
 139    return this;
 40  }
 41
 42  /// <summary>
 43  /// Overrides the AWS named profile used for credential resolution.
 44  /// Only applicable when the provider is AWS.
 45  /// </summary>
 46  /// <param name="profile">The AWS profile name.</param>
 47  /// <returns>This builder for chaining.</returns>
 48  public EnvilderBuilder WithProfile(string profile)
 49  {
 150    _options.Profile = profile;
 151    return this;
 52  }
 53
 54  /// <summary>
 55  /// Overrides the Azure Key Vault URL used for secret retrieval.
 56  /// Only applicable when the provider is Azure.
 57  /// </summary>
 58  /// <param name="vaultUrl">The Key Vault URL (e.g. <c>https://my-vault.vault.azure.net</c>).</param>
 59  /// <returns>This builder for chaining.</returns>
 60  public EnvilderBuilder WithVaultUrl(string vaultUrl)
 61  {
 162    _options.VaultUrl = vaultUrl;
 163    return this;
 64  }
 65
 66  /// <summary>
 67  /// Resolves secrets from the map file using the configured overrides and
 68  /// returns them as a dictionary, without injecting into the environment.
 69  /// </summary>
 70  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 71  public IReadOnlyDictionary<string, string> Resolve()
 72  {
 173    Env.ValidateFileExists(_filePath);
 174    var json = File.ReadAllText(_filePath);
 175    var mapFile = new MapFileParser().Parse(json);
 176    var provider = SecretProviderFactory.Create(mapFile.Config, _options);
 177    return new Dictionary<string, string>(new EnvilderClient(provider).ResolveSecrets(mapFile));
 78  }
 79
 80  /// <summary>
 81  /// Asynchronously resolves secrets from the map file using the configured overrides.
 82  /// </summary>
 83  /// <param name="cancellationToken">Optional cancellation token.</param>
 84  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 85  public async Task<IReadOnlyDictionary<string, string>> ResolveAsync(
 86    CancellationToken cancellationToken = default)
 87  {
 188    Env.ValidateFileExists(_filePath);
 189    var json = await Env.ReadFileAsync(_filePath, cancellationToken).ConfigureAwait(false);
 190    var mapFile = new MapFileParser().Parse(json);
 191    var provider = SecretProviderFactory.Create(mapFile.Config, _options);
 192    var secrets = await new EnvilderClient(provider).ResolveSecretsAsync(mapFile, cancellationToken).ConfigureAwait(fals
 193    return new Dictionary<string, string>(secrets);
 194  }
 95
 96  /// <summary>
 97  /// Resolves secrets and injects them as process-level environment variables
 98  /// via <see cref="System.Environment.SetEnvironmentVariable(string, string)"/>.
 99  /// </summary>
 100  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 101  public IReadOnlyDictionary<string, string> Inject()
 102  {
 1103    var secrets = Resolve();
 1104    EnvilderClient.InjectIntoEnvironment(secrets);
 1105    return secrets;
 106  }
 107
 108  /// <summary>
 109  /// Asynchronously resolves secrets and injects them as process-level environment variables.
 110  /// </summary>
 111  /// <param name="cancellationToken">Optional cancellation token.</param>
 112  /// <returns>Resolved secrets keyed by environment variable name.</returns>
 113  public async Task<IReadOnlyDictionary<string, string>> InjectAsync(
 114    CancellationToken cancellationToken = default)
 115  {
 1116    var secrets = await ResolveAsync(cancellationToken).ConfigureAwait(false);
 1117    EnvilderClient.InjectIntoEnvironment(secrets);
 1118    return secrets;
 1119  }
 120}