Skip to content

Commit

Permalink
Merge pull request #406 from leancodepl/feature/kv-manager
Browse files Browse the repository at this point in the history
Allow passing KeyVaultSecretManager to KV config
  • Loading branch information
jakubfijalkowski authored Jul 13, 2022
2 parents eead01f + 44a8762 commit 74cf716
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Core/LeanCode.Components.Startup/IHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Azure.Core;
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using LeanCode.AzureIdentity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Expand All @@ -25,24 +26,26 @@ public static class IHostBuilderExtensions
public static IHostBuilder AddAppConfigurationFromAzureKeyVault(
this IHostBuilder builder,
TokenCredential? credential = null,
string? keyVaultKeyOverride = null)
string? keyVaultKeyOverride = null,
KeyVaultSecretManager? manager = null)
{
return builder.ConfigureAppConfiguration((context, builder) =>
{
ConfigureAzureKeyVault(builder, credential, keyVaultKeyOverride);
ConfigureAzureKeyVault(builder, credential, keyVaultKeyOverride, manager);
});
}

public static IHostBuilder AddAppConfigurationFromAzureKeyVaultOnNonDevelopmentEnvironment(
this IHostBuilder builder,
TokenCredential? credential = null,
string? keyVaultKeyOverride = null)
string? keyVaultKeyOverride = null,
KeyVaultSecretManager? manager = null)
{
return builder.ConfigureAppConfiguration((context, builder) =>
{
if (!context.HostingEnvironment.IsDevelopment())
{
ConfigureAzureKeyVault(builder, credential, keyVaultKeyOverride);
ConfigureAzureKeyVault(builder, credential, keyVaultKeyOverride, manager);
}
});
}
Expand Down Expand Up @@ -118,7 +121,8 @@ public static IHostBuilder ConfigureDefaultLogging(
private static void ConfigureAzureKeyVault(
IConfigurationBuilder builder,
TokenCredential? credential,
string? keyVaultUrlKeyOverride)
string? keyVaultUrlKeyOverride,
KeyVaultSecretManager? manager)
{
var configuration = builder.Build();

Expand All @@ -127,7 +131,14 @@ private static void ConfigureAzureKeyVault(
{
var vaultUrl = new Uri(vault);
credential ??= DefaultLeanCodeCredential.Create(configuration);
builder.AddAzureKeyVault(vaultUrl, credential);
if (manager is not null)
{
builder.AddAzureKeyVault(vaultUrl, credential, manager);
}
else
{
builder.AddAzureKeyVault(vaultUrl, credential);
}
}
else
{
Expand Down

0 comments on commit 74cf716

Please sign in to comment.