< Summary - Envilder .NET SDK

Information
Class: Envilder.Infrastructure.Azure.AzureKeyVaultSecretProvider
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Infrastructure/Azure/AzureKeyVaultSecretProvider.cs
Tag: 561_41292037308
Line coverage
77%
Covered lines: 14
Uncovered lines: 4
Coverable lines: 18
Total lines: 66
Line coverage: 77.7%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
GetSecretAsync()50%2285.71%
GetSecret(...)50%2262.5%

File(s)

/home/runner/work/envilder/envilder/src/sdks/dotnet/Infrastructure/Azure/AzureKeyVaultSecretProvider.cs

#LineLine coverage
 1namespace Envilder.Infrastructure.Azure;
 2
 3using global::Azure;
 4using global::Azure.Security.KeyVault.Secrets;
 5using global::Envilder.Domain.Ports;
 6using System;
 7using System.Threading;
 8using 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/// Each reference is mapped to its Key Vault name with the same rule the CLI uses.
 14/// </summary>
 15public class AzureKeyVaultSecretProvider : ISecretProvider
 16{
 17  private readonly SecretClient _secretClient;
 18
 19  /// <summary>
 20  /// Initializes a new instance using the supplied Key Vault client.
 21  /// </summary>
 22  /// <param name="secretClient">A configured <see cref="SecretClient"/> instance.</param>
 123  public AzureKeyVaultSecretProvider(SecretClient secretClient)
 24  {
 125    _secretClient = secretClient ?? throw new ArgumentNullException(nameof(secretClient));
 126  }
 27
 28  /// <inheritdoc />
 29  public async Task<string?> GetSecretAsync(string name, CancellationToken cancellationToken = default)
 30  {
 131    if (string.IsNullOrWhiteSpace(name))
 32    {
 033      throw new ArgumentException("Secret name cannot be null or whitespace.", nameof(name));
 34    }
 35
 36    try
 37    {
 138      var response = await _secretClient.GetSecretAsync(AzureSecretName.From(name), null, cancellationToken);
 139      return response.Value.Value;
 40    }
 141    catch (RequestFailedException ex) when (ex.Status == 404)
 42    {
 143      return null;
 44    }
 145  }
 46
 47  /// <inheritdoc />
 48  public string? GetSecret(string name)
 49  {
 150    if (string.IsNullOrWhiteSpace(name))
 51    {
 052      throw new ArgumentException("Secret name cannot be null or whitespace.", nameof(name));
 53    }
 54
 55    try
 56    {
 157      using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
 158      var response = _secretClient.GetSecret(AzureSecretName.From(name), version: null, cancellationToken: cts.Token);
 159      return response.Value.Value;
 60    }
 061    catch (RequestFailedException ex) when (ex.Status == 404)
 62    {
 063      return null;
 64    }
 165  }
 66}