Skip to content

Commit e2f9f63

Browse files
committed
Add Azure KeyVault integration
- When a KeyVault URI is in the enviroment the vault will be used also - This depends on a Managed Service Idendity +semver: feature
1 parent 6e2fb59 commit e2f9f63

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/EasyConfig.SiteExtension/EasyConfig.SiteExtension.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
16+
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.3.1" />
17+
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.0.0" />
1618
</ItemGroup>
1719

1820
</Project>
Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
namespace EasyConfig.SiteExtension
22
{
33
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Azure.KeyVault;
5+
using Microsoft.Azure.Services.AppAuthentication;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Configuration.AzureKeyVault;
48
using Microsoft.Extensions.Hosting;
59

610
public class Program
@@ -9,6 +13,36 @@ public class Program
913

1014
public static IHostBuilder CreateHostBuilder(string[] args) =>
1115
Host.CreateDefaultBuilder(args)
12-
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
16+
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
17+
.ConfigureAppConfiguration(
18+
(ctx, builder) =>
19+
{
20+
//Build the config from sources we have
21+
var config = builder.Build();
22+
23+
// Get the uri for the Vault from configuration
24+
var keyVaultUri = config["KeyVault:Uri"];
25+
26+
// Add KeyVault only if the uri is not empty
27+
if (!string.IsNullOrWhiteSpace(keyVaultUri))
28+
{
29+
//Create Managed Service Identity token provider
30+
var azureServiceTokenProvider = new AzureServiceTokenProvider();
31+
32+
//Create the Key Vault client
33+
var keyVaultClient = new KeyVaultClient(
34+
new KeyVaultClient.AuthenticationCallback(
35+
azureServiceTokenProvider.KeyVaultTokenCallback)
36+
);
37+
38+
//Add Key Vault to configuration pipeline
39+
_ = builder.AddAzureKeyVault(
40+
keyVaultUri,
41+
keyVaultClient,
42+
new DefaultKeyVaultSecretManager()
43+
);
44+
}
45+
}
46+
);
1347
}
1448
}

0 commit comments

Comments
 (0)