Created
January 22, 2020 02:05
-
-
Save maxnorth/521e750a2023a7e70d1794432fac44db to your computer and use it in GitHub Desktop.
Partial key rotation implementation using the Key Vault config provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.Configuration; | |
namespace Example | |
{ | |
public interface IKeyProvider | |
{ | |
string GetKey(); | |
void SwitchKey(); | |
} | |
public class KeyProvider | |
{ | |
public KeyProvider(IConfigurationRoot config) | |
{ | |
_config = config; | |
} | |
private IConfigurationRoot _config; | |
private bool usePrimaryKey = true; | |
string GetKey() | |
{ | |
return usePrimaryKey ? _config["ServiceKey1"] : _config["ServiceKey2"]; | |
} | |
void SwitchKey() | |
{ | |
usePrimaryKey = !usePrimaryKey; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNetCore; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Configuration.AzureKeyVault; | |
namespace Example | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
CreateWebHostBuilder(args).Build().Run(); | |
} | |
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => | |
WebHost.CreateDefaultBuilder(args) | |
.UseStartup<Startup>() | |
.ConfigureAppConfiguration((context, configBuilder) => | |
{ | |
var config = configBuilder.Build(); | |
configBuilder.AddAzureKeyVault(new AzureKeyVaultConfigurationOptions | |
{ | |
Vault = config["KeyVaultUrl"], | |
ReloadInterval = TimeSpan.FromMinutes(10) | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment