< 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: 299_25910610327
Line coverage
77%
Covered lines: 14
Uncovered lines: 4
Coverable lines: 18
Total lines: 65
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 Envilder.Domain.Ports;
 4using global::Azure;
 5using global::Azure.Security.KeyVault.Secrets;
 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/// </summary>
 14public 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>
 122  public AzureKeyVaultSecretProvider(SecretClient secretClient)
 23  {
 124    _secretClient = secretClient ?? throw new ArgumentNullException(nameof(secretClient));
 125  }
 26
 27  /// <inheritdoc />
 28  public async Task<string?> GetSecretAsync(string name, CancellationToken cancellationToken = default)
 29  {
 130    if (string.IsNullOrWhiteSpace(name))
 31    {
 032      throw new ArgumentException("Secret name cannot be null or whitespace.", nameof(name));
 33    }
 34
 35    try
 36    {
 137      var response = await _secretClient.GetSecretAsync(name, null, cancellationToken);
 138      return response.Value.Value;
 39    }
 140    catch (RequestFailedException ex) when (ex.Status == 404)
 41    {
 142      return null;
 43    }
 144  }
 45
 46  /// <inheritdoc />
 47  public string? GetSecret(string name)
 48  {
 149    if (string.IsNullOrWhiteSpace(name))
 50    {
 051      throw new ArgumentException("Secret name cannot be null or whitespace.", nameof(name));
 52    }
 53
 54    try
 55    {
 156      using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
 157      var response = _secretClient.GetSecret(name, version: null, cancellationToken: cts.Token);
 158      return response.Value.Value;
 59    }
 060    catch (RequestFailedException ex) when (ex.Status == 404)
 61    {
 062      return null;
 63    }
 164  }
 65}