πŸ—οΈ KeyVaultManager Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Class Structure
    4.1 Interface: IKeyVaultManager
    4.2 SecretClient Field
    4.3 Constructor
    4.4 Method: GetSecret
  5. Error Handling & Return Types
  6. Configuration & Dependency Injection
  7. Usage Examples
  8. UML Class Diagram
  9. Conclusion

1. Overview & Purpose

πŸ”’ KeyVaultManager is a simple, focused service in the IrbyImaging.BusinessLogic.KeyVault namespace that abstracts the retrieval of secrets from Azure Key Vault.

  • Primary Goal: Provide a single method (GetSecret) to fetch the value of a secret by name.
  • Why Use It?
    • Centralizes all Key Vault interactions in one place.
    • Makes unit‑testing easier by depending on an interface (IKeyVaultManager).
    • Leverages Azure’s SecretClient for robustness and security.

2. Dependencies & Technologies

Dependency Version/Source Purpose
Azure.Security.KeyVault.Secrets NuGet (Azure SDK v4+) Communicating with Azure Key Vault.
System.Threading.Tasks .NET Built-in Enable asynchronous method support.
  • πŸ“¦ SecretClient (from Azure SDK)
  • πŸ”— IKeyVaultManager (custom interface for decoupling)

3. Key Functional Areas

  • Initialization: Accepts an injected SecretClient.
  • Secret Retrieval: Exposes GetSecret(secretName) to asynchronously fetch a secret.
  • Error Propagation: Catches exceptions thrown by the Azure SDK and rethrows them, allowing upstream layers to handle them.

4. Class Structure

namespace IrbyImaging.BusinessLogic.KeyVault
{
    public class KeyVaultManager : IKeyVaultManager
    {
        private readonly SecretClient _secretClient;

        public KeyVaultManager(SecretClient secretClient)
        {
            _secretClient = secretClient;
        }

        public async Task<string> GetSecret(string secretName)
        {
            try
            {
                KeyVaultSecret keyValueSecret = await _secretClient.GetSecretAsync(secretName);
                return keyValueSecret.Value;
            }
            catch
            {
                throw;
            }
        }
    }
}

4.1 Interface: IKeyVaultManager

public interface IKeyVaultManager
{
    Task<string> GetSecret(string secretName);
}
  • Contract for any KeyVault-based secret provider.

4.2 SecretClient Field

  • Declaration:
    private readonly SecretClient _secretClient;
  • Responsibility:
    Underlying client that interacts with Azure Key Vault.

4.3 Constructor

public KeyVaultManager(SecretClient secretClient)
{
    _secretClient = secretClient;
}
  • Parameter: SecretClient secretClient (injected via DI).
  • Action: Stores the client for use in secret operations.

4.4 Method: GetSecret

public async Task<string> GetSecret(string secretName)
{
    try
    {
        KeyVaultSecret keyValueSecret = await _secretClient.GetSecretAsync(secretName);
        return keyValueSecret.Value;
    }
    catch
    {
        throw; // Propagate any Azure.RequestFailedException or others
    }
}
Parameter Type Description
secretName string The name/key of the secret in Key Vault
Return Type Description
Task Asynchronous string value of the requested secret

5. Error Handling & Return Types

  • Exceptions:
    • Azure.RequestFailedException if the secret is not found, unauthorized, or network issues occur.
    • Other runtime exceptions bubble up.
  • Rethrow Strategy:
    Catches exceptions only to rethrow them, leaving actual error handling to consumers.

6. Configuration & Dependency Injection

// ASP.NET Core (Startup.cs or Program.cs)
services.AddSingleton<IKeyVaultManager>(sp =>
{
    var config = sp.GetRequiredService<IConfiguration>();
    var vaultUri = new Uri(config["KeyVault:Uri"]);
    var credential = new DefaultAzureCredential();
    var secretClient = new SecretClient(vaultUri, credential);
    return new KeyVaultManager(secretClient);
});
  • KeyVault:Uri in appsettings.json:
    {
      "KeyVault": {
        "Uri": "https://your-vault-name.vault.azure.net/"
      }
    }
    

7. Usage Examples

public class SettingsService
{
    private readonly IKeyVaultManager _keyVaultManager;

    public SettingsService(IKeyVaultManager keyVaultManager)
    {
        _keyVaultManager = keyVaultManager;
    }

    public async Task InitializeAsync()
    {
        // Fetch a secret named "DatabasePassword" from Key Vault
        string dbPassword = await _keyVaultManager.GetSecret("DatabasePassword");
        Console.WriteLine($"Retrieved DB Password: {dbPassword}");
    }
}

πŸš€ Tip: Wrap calls in try/catch to handle RequestFailedException for scenarios like missing secrets or permission issues.


8. UML Class Diagram

classDiagram
    class IKeyVaultManager {
        <<interface>>
        + Task<string> GetSecret(string secretName)
    }
    class KeyVaultManager {
        - SecretClient _secretClient
        + KeyVaultManager(SecretClient secretClient)
        + Task<string> GetSecret(string secretName)
    }
    KeyVaultManager ..|> IKeyVaultManager : implements

9. Conclusion

The KeyVaultManager class offers a clean, testable, and reusable way to retrieve secrets from Azure Key Vault. By abstracting away the SecretClient details, your application components remain decoupled from Azure-specific code.

βœ… Next Steps & Enhancements

  • Add caching for frequently accessed secrets.
  • Implement retry policies (e.g., using Polly).
  • Integrate logging to capture success/failure events.
  • Extend the interface for secret creation, deletion, or listing if needed.

Happy coding! πŸ”