Skip to content

Configuration provider implementation that decorates configuration supporting Azure Key Vault references

License

Notifications You must be signed in to change notification settings

skarllot/azure-keyvault-reference

Repository files navigation

Azure Key Vault Reference

Build status OpenSSF Scorecard Code coverage Mutation testing badge GitHub license

The Raiqub Azure Key Vault Reference NuGet packages simplifies the integration of Azure Key Vault with your .NET applications by providing support for Azure Key Vault references in the IConfiguration system.

🏃 Quickstart   |   📗 Guide   |   📦 NuGet


Features

  • Seamless integration of Azure Key Vault references with IConfiguration
  • Easy retrieval of secrets from configuration using Azure Key Vault references
  • Support for parsing Azure Key Vault references from strings

NuGet Packages

  • NuGet Raiqub.AzureKeyVaultReference: provides support for parsing Azure Key Vault references
  • NuGet Raiqub.AzureKeyVaultReference.Configuration: provides support for integrating Azure Key Vault references with IConfiguration

Prerequisites

Before you begin, you'll need the following:

  • .NET Standard 2.0 or .NET Core 6.0 installed on your machine
  • An IDE such as Visual Studio, Visual Studio Code, or JetBrains Rider

Quickstart

To use the library, you can install the desired NuGet package(s) in your Web project and add the configuration provider. Here's an example of how to add the configuration provider:

var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureAzureKeyVaultReference();

or using WebApplication

var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureAzureKeyVaultReference();

Guide

To use the Azure Key Vault Configuration Provider, follow these steps:

  1. Set up Azure Key Vault: Ensure you have an Azure Key Vault instance created and the necessary permissions to access it.
  2. Install and configure the package: Install the NuGet package and add the necessary configuration to your application.
  3. Configure Azure Key Vault references: In your `appsettings.json` file or any other configuration source, add Azure Key Vault references using the `@Microsoft.KeyVault` syntax. For example:
{
  "MySecret": "@Microsoft.KeyVault(SecretUri=https://your-keyvault.vault.azure.net/secrets/MySecret)",
  "OtherSecret": "@Microsoft.KeyVault(VaultName=your-keyvault;SecretName=OtherSecret)"
}
  1. Retrieve configuration values: Access the configuration values as usual using the `IConfiguration` interface. The Azure Key Vault Configuration Provider will automatically fetch the secrets from Azure Key Vault and replace the references with the corresponding values.
using System.IO;
using Microsoft.Extensions.Configuration;
using Raiqub.AzureKeyVaultReference.Configuration;

var configuration = new ConfigurationManager()
    .AddAzureKeyVaultReference(builder =>
        builder
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json"))
    .Build();

var mySecretValue = configuration["MySecret"];

Parsing Azure Key Vault references

If you need to parse Azure Key Vault references from strings programmatically, you can use the `KeyVaultSecretReference` class provided by this package.

using Raiqub.AzureKeyVaultReference;

var reference = "@Microsoft.KeyVault(SecretUri=https://your-keyvault.vault.azure.net/secrets/MySecret)";

var parsedReference = KeyVaultSecretReference.Parse(reference);
// ParsedReference.VaultUri: "https://your-keyvault.vault.azure.net"
// ParsedReference.Name: "MySecret"
// ParsedReference.Version: null

Default Azure Key Vault

This library supports defining a default Key Vault to use when one is not defined on Azure Key Vault reference.

var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureAzureKeyVaultReference(
    options => options.GetDefaultVaultNameOrUri = () => Environment.GetEnvironmentVariable("KEYVAULTURI"));

or using WebApplication

builder.Host.ConfigureAzureKeyVaultReference(
    options => options.GetDefaultVaultNameOrUri = () => Environment.GetEnvironmentVariable("KEYVAULTNAME"));

Doing so the Azure Key Vault reference do not need to specify the Key Vault Name

{
  "MySecret": "@Microsoft.KeyVault(SecretName=MySecret)"
}

Contributing

If something is not working for you or if you think that the source file should change, feel free to create an issue or Pull Request. I will be happy to discuss and potentially integrate your ideas!

License

This library is licensed under the MIT License.

About

Configuration provider implementation that decorates configuration supporting Azure Key Vault references

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •  

Languages