Noctusoft.EzAzureLocalKeyVault
1.0.1
dotnet add package Noctusoft.EzAzureLocalKeyVault --version 1.0.1
NuGet\Install-Package Noctusoft.EzAzureLocalKeyVault -Version 1.0.1
<PackageReference Include="Noctusoft.EzAzureLocalKeyVault" Version="1.0.1" />
<PackageVersion Include="Noctusoft.EzAzureLocalKeyVault" Version="1.0.1" />
<PackageReference Include="Noctusoft.EzAzureLocalKeyVault" />
paket add Noctusoft.EzAzureLocalKeyVault --version 1.0.1
#r "nuget: Noctusoft.EzAzureLocalKeyVault, 1.0.1"
#addin nuget:?package=Noctusoft.EzAzureLocalKeyVault&version=1.0.1
#tool nuget:?package=Noctusoft.EzAzureLocalKeyVault&version=1.0.1
EzLocalKeyVault
A utility library that simplifies using Azure Key Vault and local key vault for .NET applications.
Quick Start - Hybrid Key Vault Integration
The easiest way to get started is with the hybrid key vault integration, which automatically switches between Azure Key Vault and local key vault based on your environment.
1. Install the NuGet Package
dotnet add package Noctusoft.EzAzureLocalKeyVault --version 1.0.1
2. Add to Your Application (Single Line Usage)
// In Program.cs
using Noctusoft.EzAzureLocalKeyVault.Extensions;
var builder = WebApplication.CreateBuilder(args);
// One line to add hybrid key vault to your application
builder.UseEzAzureLocalKeyVault((context, options) =>
{
options.AzureKeyVaultUri = "https://your-key-vault.vault.azure.net/";
});
var app = builder.Build();
// ...
For even simpler usage, you can configure the Azure Key Vault URI in your appsettings.json:
// In Program.cs
using Noctusoft.EzAzureLocalKeyVault.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Simplest one-line usage (gets settings from appsettings.json)
builder.UseEzAzureLocalKeyVault();
var app = builder.Build();
// ...
3. Configure (Optional)
Add configuration to your appsettings.json
:
{
"EzAzureLocalKeyVault": {
"AzureKeyVaultUri": "https://your-key-vault.vault.azure.net/",
"LocalKeyVaultEnvironments": ["Development", "LocalDevelopment"],
"FallbackToLocalKeyVault": true
}
}
4. Use in Your Code
// Inject the service
public class MyService
{
private readonly IHybridKeyVault _keyVault;
public MyService(IHybridKeyVault keyVault)
{
_keyVault = keyVault;
}
public void DoSomething()
{
// Get a secret
var secret = _keyVault.GetSecret("my-secret");
}
}
5. Using Typed Configuration
One of the most powerful features is the ability to bind configuration to strongly-typed classes after key vault substitutions have been applied:
// Define your configuration classes
public class AppSettings
{
public string ApiKey { get; set; } = string.Empty;
public ConnectionStrings ConnectionStrings { get; set; } = new();
}
public class ConnectionStrings
{
public string DefaultConnection { get; set; } = string.Empty;
}
// In Program.cs
using Noctusoft.EzAzureLocalKeyVault.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.UseEzAzureLocalKeyVault();
// Get typed configuration after key vault substitutions
var appSettings = builder.Configuration.GetTypedSection<AppSettings>();
// Or register with dependency injection
// Empty string means "use the root configuration"
builder.Services.AddTypedConfiguration<AppSettings>(builder.Configuration, "");
// If your settings are in a specific section:
builder.Services.AddTypedConfiguration<AppSettings>(builder.Configuration, "AppSettings");
// Or do both at once
var settings = builder.Services.AddAndGetTypedConfiguration<AppSettings>(builder.Configuration, "");
var app = builder.Build();
// ...
Then use the typed configuration in your services:
public class MyService
{
private readonly AppSettings _settings;
public MyService(IOptions<AppSettings> options)
{
_settings = options.Value;
}
public void DoSomething()
{
// Access your configuration with full IntelliSense support
var apiKey = _settings.ApiKey;
var connectionString = _settings.ConnectionStrings.DefaultConnection;
}
}
That's it! The library will automatically:
- Use local key vault in development environments
- Use Azure Key Vault in production environments
- Fall back to local key vault if Azure Key Vault is unavailable
- Fall back to Azure Key Vault if local key vault file is missing
- Cache Azure Key Vault secrets for better performance
Features
- Environment Detection: Automatically use local key vault in development, Azure Key Vault in production
- Intelligent Fallback: Gracefully handle missing files or unavailable services
- Secret Caching: Improve performance by caching Azure Key Vault secrets
- Typed Configuration: Bind configuration to strongly-typed classes
- Variable Substitution: Replace placeholders in configuration with secrets
- CLI Tool: Process templates from the command line
Original EzLocalKeyVault Library
If you only need local key vault functionality without Azure Key Vault integration, you can use the original library:
dotnet add package Noctusoft.EzLocalKeyVault --version 1.0.1
Basic Usage
// In Program.cs
using Noctusoft.EzLocalKeyVault.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add the local key vault to the configuration
builder.Configuration.AddEzLocalKeyVault();
// Register the local key vault service
builder.Services.AddEzLocalKeyVault(builder.Configuration);
var app = builder.Build();
// ...
Motivation
When developing applications that use Azure Key Vault in production, it can be challenging to work with configuration values locally. This package provides a simple solution by allowing you to:
- Use a local JSON file as a key vault
- Support variable substitution in your configuration files using the
$(VARIABLE-NAME)
syntax - Override configuration values directly using JSON property paths
- Automatically reload when the vault file changes
- Securely handle sensitive information by redacting secrets in logs
Installation
# For local key vault only
dotnet add package Noctusoft.EzLocalKeyVault --version 1.0.1
# For hybrid Azure/Local key vault
dotnet add package Noctusoft.EzAzureLocalKeyVault --version 1.0.1
# For configuration validation and typed configuration support
dotnet add package Noctusoft.EzLocalKeyVault.Configuration --version 1.0.1
Configuration Reference
You can customize the behavior of the local key vault by adding an EzLocalKeyVault
section to your appsettings.json
:
{
"EzLocalKeyVault": {
"VaultFilePath": ".local-vault.json",
"ReloadOnChange": true,
"LogSubstitutions": true,
"RedactionPatterns": ["password", "secret", "key", "token"]
}
}
Option | Description | Default |
---|---|---|
VaultFilePath |
Path to the local vault file | .local-vault.json |
ReloadOnChange |
Whether to reload the configuration when the vault file changes | true |
LogSubstitutions |
Whether to log substitutions | true |
RedactionPatterns |
Patterns used to identify values that should be redacted in logs | ["password", "secret", "key", "token"] |
Advanced Usage
Programmatic Configuration
You can also configure the local key vault programmatically:
// Specify a custom vault file path
builder.Configuration.AddEzLocalKeyVault("custom-path/my-vault.json");
// Configure options
builder.Services.AddEzLocalKeyVault(options => {
options.VaultFilePath = "custom-path/my-vault.json";
options.ReloadOnChange = false;
options.LogSubstitutions = false;
options.RedactionPatterns = new[] { "custom-pattern" };
});
Accessing the Key Vault Directly
You can inject the ILocalKeyVault
interface to access the key vault directly:
public class MyService
{
private readonly ILocalKeyVault _keyVault;
public MyService(ILocalKeyVault keyVault)
{
_keyVault = keyVault;
}
public void DoSomething()
{
// Get a specific secret
var secret = _keyVault.GetSecret("my-secret");
// Get all secrets
var allSecrets = _keyVault.GetAll();
}
}
Using Diagnostic Utilities
EzLocalKeyVault includes diagnostic utilities to help troubleshoot configuration issues:
// Generate a diagnostic report
var report = configuration.GenerateKeyVaultDiagnosticReport();
Console.WriteLine(report);
// Analyze substitution issues
var issues = serviceProvider.AnalyzeKeyVaultSubstitutionIssues();
foreach (var issue in issues)
{
Console.WriteLine(issue);
}
// Export configuration to JSON
configuration.ExportConfigurationToJson("config-export.json");
// For ASP.NET Core applications, add diagnostic endpoints
app.UseEzLocalKeyVaultDiagnostics();
// Access at /_diagnostics/keyvault/report, /_diagnostics/keyvault/issues, and /_diagnostics/keyvault/export
For more details, see the Diagnostics documentation.
Security Notice
This package is designed for local development environments only and should not be used in production. The local vault file is stored in plain text and does not provide the same level of security as Azure Key Vault.
While the package includes features to redact sensitive information in logs, it's essential to be careful when handling secrets locally:
- Never commit your
.local-vault.json
file to source control - Add
.local-vault.json
to your.gitignore
file - Be cautious when sharing configuration files that contain variable references
Documentation
For more detailed information, check out these documentation guides:
- Best Practices - Recommended patterns and practices for using EzLocalKeyVault
- Command Line Interface - Using the ez-keyvault CLI tool
- Azure Key Vault Integration - How to integrate with Azure Key Vault
- Diagnostics - Tools and utilities for troubleshooting configuration issues
- Sample Configurations - Example appsettings.json and .local-vault.json files for different environments
- Hybrid Key Vault Integration - Using the EzAzureLocalKeyVault library to seamlessly switch between Azure Key Vault and local key vault
- Packaging and Publishing - Instructions for creating NuGet packages and publishing to NuGet.org
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
v1.0.0 (2025-05-01)
Added
- Initial release of
Noctusoft.EzLocalKeyVault
library- Local key vault implementation for development environments
- Configuration substitution with variable references
- Diagnostic utilities for troubleshooting
- Command-line interface for processing templates
- New
Noctusoft.EzAzureLocalKeyVault
library- Hybrid key vault that automatically switches between Azure Key Vault and local key vault
- Environment-based detection with intelligent fallback logic
- Caching of Azure Key Vault secrets for improved performance
- Direct Azure Key Vault references with prefix support
- Strongly-typed configuration binding
- Enhanced fallback behavior
- Automatic fallback to Azure Key Vault if local key vault file is missing
- Detailed logging for troubleshooting
- Clear error messages when no fallback options are available
- Comprehensive documentation
- Installation and usage guides
- Best practices
- Sample applications
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- Azure.Identity (>= 1.10.4)
- Azure.Security.KeyVault.Secrets (>= 4.7.0)
- Microsoft.Extensions.Configuration (>= 9.0.4)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Configuration.AzureKeyVault (>= 3.1.24)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.4)
- Microsoft.Extensions.Configuration.CommandLine (>= 9.0.4)
- Microsoft.Extensions.Configuration.EnvironmentVariables (>= 9.0.4)
- Microsoft.Extensions.Configuration.FileExtensions (>= 9.0.4)
- Microsoft.Extensions.Configuration.Json (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection (>= 9.0.4)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.4)
- Microsoft.Extensions.FileProviders.Abstractions (>= 9.0.4)
- Microsoft.Extensions.FileProviders.Physical (>= 9.0.4)
- Microsoft.Extensions.FileSystemGlobbing (>= 9.0.4)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.4)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.4)
- Microsoft.Extensions.Primitives (>= 9.0.4)
- Newtonsoft.Json (>= 13.0.3)
- Noctusoft.EzLocalKeyVault (>= 1.0.1)
- System.Text.Json (>= 9.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.