| | | 1 | | namespace Envilder.Infrastructure.Azure; |
| | | 2 | | |
| | | 3 | | using Envilder.Domain.Ports; |
| | | 4 | | using global::Azure; |
| | | 5 | | using global::Azure.Security.KeyVault.Secrets; |
| | | 6 | | using System; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// <see cref="ISecretProvider"/> backed by Azure Key Vault. |
| | | 12 | | /// Secrets that return HTTP 404 are treated as missing and yield <see langword="null"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | public class AzureKeyVaultSecretProvider : ISecretProvider |
| | | 15 | | { |
| | | 16 | | private readonly SecretClient _secretClient; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance using the supplied Key Vault client. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="secretClient">A configured <see cref="SecretClient"/> instance.</param> |
| | 1 | 22 | | public AzureKeyVaultSecretProvider(SecretClient secretClient) |
| | | 23 | | { |
| | 1 | 24 | | _secretClient = secretClient ?? throw new ArgumentNullException(nameof(secretClient)); |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public async Task<string?> GetSecretAsync(string name, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | | 30 | | try |
| | | 31 | | { |
| | 1 | 32 | | var response = await _secretClient.GetSecretAsync(name, null, cancellationToken); |
| | 1 | 33 | | return response.Value.Value; |
| | | 34 | | } |
| | 1 | 35 | | catch (RequestFailedException ex) when (ex.Status == 404) |
| | | 36 | | { |
| | 1 | 37 | | return null; |
| | | 38 | | } |
| | 1 | 39 | | } |
| | | 40 | | } |