| | | 1 | | namespace Envilder.Application; |
| | | 2 | | |
| | | 3 | | using global::Envilder.Domain; |
| | | 4 | | using global::Envilder.Infrastructure; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Threading; |
| | | 8 | | using 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> |
| | | 22 | | public class EnvilderBuilder |
| | | 23 | | { |
| | | 24 | | private readonly string _filePath; |
| | 1 | 25 | | private readonly EnvilderOptions _options = new(); |
| | | 26 | | |
| | 1 | 27 | | internal EnvilderBuilder(string filePath) |
| | | 28 | | { |
| | 1 | 29 | | _filePath = filePath; |
| | 1 | 30 | | } |
| | | 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 | | { |
| | 1 | 39 | | _options.Provider = provider; |
| | 1 | 40 | | 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 | | { |
| | 1 | 51 | | _options.Profile = profile; |
| | 1 | 52 | | 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 | | { |
| | 1 | 63 | | _options.VaultUrl = vaultUrl; |
| | 1 | 64 | | 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 | | { |
| | 1 | 74 | | Envilder.ValidateFileExists(_filePath); |
| | 1 | 75 | | var json = File.ReadAllText(_filePath); |
| | 1 | 76 | | var mapFile = new MapFileParser().Parse(json); |
| | 1 | 77 | | var provider = SecretProviderFactory.Create(mapFile.Config, _options); |
| | 1 | 78 | | 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 | | { |
| | 1 | 89 | | Envilder.ValidateFileExists(_filePath); |
| | 1 | 90 | | var json = await Envilder.ReadFileAsync(_filePath, cancellationToken).ConfigureAwait(false); |
| | 1 | 91 | | var mapFile = new MapFileParser().Parse(json); |
| | 1 | 92 | | var provider = SecretProviderFactory.Create(mapFile.Config, _options); |
| | 1 | 93 | | var secrets = await new EnvilderClient(provider).ResolveSecretsAsync(mapFile, cancellationToken).ConfigureAwait(fals |
| | 1 | 94 | | return new Dictionary<string, string>(secrets); |
| | 1 | 95 | | } |
| | | 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 | | { |
| | 1 | 104 | | var secrets = Resolve(); |
| | 1 | 105 | | EnvilderClient.InjectIntoEnvironment(secrets); |
| | 1 | 106 | | 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 | | { |
| | 1 | 117 | | var secrets = await ResolveAsync(cancellationToken).ConfigureAwait(false); |
| | 1 | 118 | | EnvilderClient.InjectIntoEnvironment(secrets); |
| | 1 | 119 | | return secrets; |
| | 1 | 120 | | } |
| | | 121 | | } |