| | | 1 | | namespace Envilder.Infrastructure.Configuration; |
| | | 2 | | |
| | | 3 | | using Envilder.Application; |
| | | 4 | | using Envilder.Domain; |
| | | 5 | | using Microsoft.Extensions.Configuration; |
| | | 6 | | using System; |
| | | 7 | | |
| | | 8 | | public class EnvilderConfigurationProvider : ConfigurationProvider |
| | | 9 | | { |
| | | 10 | | private readonly EnvilderClient _client; |
| | | 11 | | private readonly ParsedMapFile _mapFile; |
| | | 12 | | |
| | 1 | 13 | | public EnvilderConfigurationProvider(EnvilderClient client, ParsedMapFile mapFile) |
| | | 14 | | { |
| | 1 | 15 | | _client = client ?? throw new ArgumentNullException(nameof(client)); |
| | 1 | 16 | | _mapFile = mapFile ?? throw new ArgumentNullException(nameof(mapFile)); |
| | 1 | 17 | | } |
| | | 18 | | |
| | | 19 | | // ConfigurationProvider.Load() is synchronous by design. |
| | | 20 | | // ResolveSecretsAsync uses ConfigureAwait(false) throughout to avoid deadlocks. |
| | | 21 | | public override void Load() |
| | | 22 | | { |
| | 1 | 23 | | var secrets = _client.ResolveSecretsAsync(_mapFile) |
| | 1 | 24 | | .ConfigureAwait(false) |
| | 1 | 25 | | .GetAwaiter() |
| | 1 | 26 | | .GetResult(); |
| | | 27 | | |
| | 1 | 28 | | Data.Clear(); |
| | | 29 | | |
| | 1 | 30 | | foreach (var kvp in secrets) |
| | | 31 | | { |
| | 1 | 32 | | var key = NormalizeKey(kvp.Key); |
| | 1 | 33 | | Data[key] = kvp.Value; |
| | | 34 | | } |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static string NormalizeKey(string key) |
| | | 38 | | { |
| | 1 | 39 | | return key.Replace("/", ConfigurationPath.KeyDelimiter); |
| | | 40 | | } |
| | | 41 | | } |