pvNugsSecretManagerNc9Azure 9.0.0
dotnet add package pvNugsSecretManagerNc9Azure --version 9.0.0
NuGet\Install-Package pvNugsSecretManagerNc9Azure -Version 9.0.0
<PackageReference Include="pvNugsSecretManagerNc9Azure" Version="9.0.0" />
<PackageVersion Include="pvNugsSecretManagerNc9Azure" Version="9.0.0" />
<PackageReference Include="pvNugsSecretManagerNc9Azure" />
paket add pvNugsSecretManagerNc9Azure --version 9.0.0
#r "nuget: pvNugsSecretManagerNc9Azure, 9.0.0"
#:package pvNugsSecretManagerNc9Azure@9.0.0
#addin nuget:?package=pvNugsSecretManagerNc9Azure&version=9.0.0
#tool nuget:?package=pvNugsSecretManagerNc9Azure&version=9.0.0
I'll help you create a comprehensive README.md file for your Azure Key Vault secret manager NuGet package. Let me generate a professional, well-structured README that covers all the essential aspects.
PvNugs Azure Key Vault Secret Manager
A robust, production-ready Azure Key Vault integration library for .NET applications that provides secure secret management with built-in caching, comprehensive logging, and support for both Managed Identity and Service Principal authentication.
โจ Features
- ๐ Secure Secret Retrieval: Direct integration with Azure Key Vault for secure secret storage and retrieval
- ๐ Performance Optimized: Built-in caching layer to minimize API calls and improve response times
- ๐ Flexible Authentication: Support for both Azure Managed Identity (recommended) and Service Principal authentication
- ๐ Comprehensive Logging: Detailed logging integration with customizable log levels
- โก Async/Await Support: Fully asynchronous operations with cancellation token support
- ๐ก๏ธ Exception Handling: Robust error handling with detailed exception information
- ๐๏ธ Dependency Injection: Seamless integration with .NET's built-in dependency injection container
- ๐ง Configuration Driven: Flexible configuration through .NET's IConfiguration system
- ๐งช Thread Safe: Designed for concurrent access in multi-threaded applications
๐ฆ Installation
Install the package via NuGet Package Manager:
bash
dotnet add package pvNugsSecretManagerNc9Azure
Or via Package Manager Console:
powershell
Install-Package pvNugsSecretManagerNc9Azure
๐ง Dependencies
This package requires the following companion packages:
bash
# Cache provider (required)
dotnet add package pvNugsCacheNc9Local
# Logger service (required)
dotnet add package pvNugsLoggerNc9Seri
๐ Quick Start
1. Configuration Setup
Using Managed Identity (Recommended for Azure environments)
appsettings.json:
json
{
"PvNugsAzureSecretManagerConfig": {
"KeyVaultUrl": "https://your-keyvault.vault.azure.net/"
}
}
Using Service Principal (for local development or non-Azure environments)
appsettings.json:
json
{
"PvNugsAzureSecretManagerConfig": {
"KeyVaultUrl": "https://your-keyvault.vault.azure.net/",
"Credential": {
"TenantId": "12345678-1234-1234-1234-123456789012",
"ClientId": "87654321-4321-4321-4321-210987654321",
"ClientSecret": "your-client-secret-here"
}
}
}
2. Service Registration
Program.cs:
csharp
using pvNugsSecretManagerNc9Azure;
using pvNugsCacheNc9Local;
using pvNugsLoggerNc9Seri;
var builder = WebApplication.CreateBuilder(args);
// Register dependencies
builder.Services.TryAddPvNugsCacheNc9Local(builder.Configuration);
builder.Services.TryAddPvNugsLoggerSeriService(builder.Configuration);
// Register Azure Key Vault secret manager
builder.Services.TryAddPvNugsAzureStaticSecretManager(builder.Configuration);
var app = builder.Build();
3. Usage in Your Application
csharp
using pvNugsSecretManagerNc9Abstractions;
public class DatabaseService
{
private readonly IPvNugsStaticSecretManager _secretManager;
public DatabaseService(IPvNugsStaticSecretManager secretManager)
{
_secretManager = secretManager;
}
public async Task<string> GetConnectionStringAsync()
{
var password = await _secretManager.GetStaticSecretAsync("database-password");
if (password == null)
throw new InvalidOperationException("Database password not found");
return $"Server=myserver;Database=mydb;Password={password};";
}
}
๐ Authentication Methods
Managed Identity (Recommended)
For applications running in Azure (App Service, Functions, VMs, etc.), use Managed Identity for secure, keyless authentication:
json
{
"PvNugsAzureSecretManagerConfig": {
"KeyVaultUrl": "https://your-keyvault.vault.azure.net/",
"Credential": null
}
}
Benefits:
- No credentials to manage or rotate
- Automatic credential management by Azure
- Enhanced security posture
- Simplified deployment process
Service Principal
For local development, testing, or non-Azure environments:
json
{
"PvNugsAzureSecretManagerConfig": {
"KeyVaultUrl": "https://your-keyvault.vault.azure.net/",
"Credential": {
"TenantId": "your-tenant-id",
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret"
}
}
}
๐ฏ Advanced Usage Examples
Error Handling with Retry Logic
csharp
public async Task<string> GetSecretWithRetryAsync(string secretName, int maxRetries = 3)
{
for (int attempt = 1; attempt <= maxRetries; attempt++)
{
try
{
var secret = await _secretManager.GetStaticSecretAsync(secretName);
return secret ?? throw new SecretNotFoundException($"Secret '{secretName}' not found");
}
catch (PvNugsStaticSecretManagerException ex) when (IsTransientError(ex) && attempt < maxRetries)
{
var delay = TimeSpan.FromSeconds(Math.Pow(2, attempt)); // Exponential backoff
_logger.LogWarning(ex, "Transient error on attempt {Attempt}, retrying in {Delay}ms",
attempt, delay.TotalMilliseconds);
await Task.Delay(delay);
}
}
throw new InvalidOperationException($"Failed to retrieve secret after {maxRetries} attempts");
}
private static bool IsTransientError(PvNugsStaticSecretManagerException ex)
{
return ex.InnerException is HttpRequestException or TimeoutException;
}
Batch Secret Retrieval
csharp
public async Task<Dictionary<string, string>> GetMultipleSecretsAsync(
string[] secretNames,
CancellationToken cancellationToken = default)
{
var tasks = secretNames.Select(async name => new
{
Name = name,
Value = await _secretManager.GetStaticSecretAsync(name, cancellationToken)
});
var results = await Task.WhenAll(tasks);
return results
.Where(r => r.Value != null)
.ToDictionary(r => r.Name, r => r.Value!);
}
Environment-Specific Configuration
// Program.cs - Different configuration per environment
if (builder.Environment.IsDevelopment())
{
// Local development with service principal
builder.Services.Configure<PvNugsAzureSecretManagerConfig>(options =>
{
options.KeyVaultUrl = "https://dev-keyvault.vault.azure.net/";
options.Credential = new PvNugsAzureServicePrincipalCredential
{
TenantId = builder.Configuration["AzureAd:TenantId"]!,
ClientId = builder.Configuration["AzureAd:ClientId"]!,
ClientSecret = builder.Configuration["AzureAd:ClientSecret"]!
};
});
}
else
{
// Production with managed identity
builder.Services.Configure<PvNugsAzureSecretManagerConfig>(options =>
{
options.KeyVaultUrl = builder.Configuration["KeyVault:ProductionUrl"]!;
options.Credential = null; // Use managed identity
});
}
builder.Services.TryAddPvNugsAzureStaticSecretManager(builder.Configuration);
๐ง Configuration Options
Property | Type | Required | Description |
---|---|---|---|
KeyVaultUrl |
string | Yes | The HTTPS URL of your Azure Key Vault (format: https://vault-name.vault.azure.net/ ) |
Credential |
object | No | Service principal credentials (when null, uses Managed Identity) |
Credential.TenantId |
string | Conditional* | Azure AD tenant ID (required when using service principal) |
Credential.ClientId |
string | Conditional* | Application (client) ID (required when using service principal) |
Credential.ClientSecret |
string | Conditional* | Client secret (required when using service principal) |
*Required only when using Service Principal authentication
๐ก๏ธ Security Best Practices
- Use Managed Identity in Production: Always prefer Managed Identity over Service Principal in Azure environments
- Secure Secret Storage: Store service principal credentials securely (Key Vault, environment variables, etc.)
- Regular Credential Rotation: Implement regular rotation for service principal secrets
- Principle of Least Privilege: Grant minimal required permissions to Key Vault
- Network Security: Use Key Vault firewall and private endpoints when possible
- Audit Logging: Enable Key Vault audit logging for compliance and monitoring
๐ Performance Characteristics
- Cache Hit Response Time: < 1ms (in-memory cache)
- Cache Miss Response Time: 100-500ms (depends on network latency to Azure)
- Concurrent Request Support: Unlimited (thread-safe singleton pattern)
- Memory Footprint: Minimal (lazy-loaded client, efficient caching)
๐ Troubleshooting
Common Issues and Solutions
Authentication Errors (401/403)
PvNugsStaticSecretManagerException: Unauthorized
Solutions:
- Verify Key Vault access policies or RBAC permissions
- Check service principal credentials if using Service Principal auth
- Ensure Managed Identity is enabled and properly configured
Secret Not Found (404)
PvNugsStaticSecretManagerException: Secret not found
Solutions:
- Verify the secret exists in the specified Key Vault
- Check secret name spelling and casing
- Ensure the secret is not disabled or expired
Network Connectivity Issues
PvNugsStaticSecretManagerException: Network error
Solutions:
- Check network connectivity to Azure
- Verify Key Vault URL is correct and accessible
- Check firewall and network security group rules
๐ API Reference
IPvNugsStaticSecretManager
public interface IPvNugsStaticSecretManager
{
Task<string?> GetStaticSecretAsync(string secretName, CancellationToken cancellationToken = default);
}
PvNugsStaticSecretManagerException
Custom exception thrown for all secret management errors, wrapping underlying Azure SDK exceptions while preserving original error details.
๐ค Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Related Packages
- pvNugsCacheNc9Local - Local caching provider
- pvNugsLoggerNc9Seri - Serilog-based logging provider
- pvNugsSecretManagerNc9Abstractions - Core abstractions and interfaces
๐ Support
- ๐ Documentation: Wiki
- ๐ Bug Reports: Issues
- ๐ก Feature Requests: Discussions
- ๐ง Email Support: Contact Us
Made with โค๏ธ by the PvNugs Team
Secure your secrets with confidence! ๐
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Azure.Identity (>= 1.15.0)
- Azure.Security.KeyVault.Secrets (>= 4.8.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.8)
- pvNugsCacheNc9Abstractions (>= 9.0.0)
- pvNugsLoggerNc9Abstractions (>= 9.0.1)
- pvNugsSecretManagerNc9Abstractions (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
9.0.0 | 181 | 8/29/2025 |
Initial