Skip to content

Commit f26b927

Browse files
committed
Add option to use multiple KeyVaults
There now can be multiple KeyVault Uris. for example ``` { "KeyVault":{ "Uri": [ "sample1.vault.azure.net/", "sample2.vault.azure.net/" ] } } ``` +semver: feature
1 parent e2f9f63 commit f26b927

File tree

3 files changed

+79
-12
lines changed

3 files changed

+79
-12
lines changed

.vscode/launch.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"env": {
2323
"ASPNETCORE_ENVIRONMENT": "Development",
2424
"EASYCONFIG__apiUrl": "https://codez.one/api",
25-
"EASYCONFIG__test__value": "blubb"
25+
"EASYCONFIG__test__value": "blubb",
26+
//"KeyVault__Uri": "peng"
27+
//"KeyVault__Uri__0": "peng",
28+
//"KeyVault__Uri__1": "puff"
2629
},
2730
"sourceFileMap": {
2831
"/Views": "${workspaceFolder}/Views"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace EasyConfig.SiteExtension
2+
{
3+
using Microsoft.Azure.KeyVault.Models;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.Configuration.AzureKeyVault;
6+
7+
public class PrefixKeyVaultSecretManager : IKeyVaultSecretManager
8+
{
9+
private readonly string prefix;
10+
private readonly bool removePrefix;
11+
12+
public PrefixKeyVaultSecretManager(
13+
string prefix = "EASYCONFIG",
14+
bool removePrefix = false
15+
)
16+
{
17+
this.prefix = $"{prefix}--";
18+
this.removePrefix = removePrefix;
19+
}
20+
21+
public bool Load(SecretItem secret) => secret.Identifier.Name.StartsWith(this.prefix);
22+
23+
public string GetKey(SecretBundle secret)
24+
{
25+
var secretIdentifier = secret.SecretIdentifier.Name;
26+
27+
if (this.removePrefix)
28+
{
29+
secretIdentifier = secretIdentifier.Substring(this.prefix.Length);
30+
}
31+
return secretIdentifier.Replace("--", ConfigurationPath.KeyDelimiter);
32+
}
33+
}
34+
}

src/EasyConfig.SiteExtension/Program.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace EasyConfig.SiteExtension
22
{
3+
using System.Collections.Generic;
34
using Microsoft.AspNetCore.Hosting;
45
using Microsoft.Azure.KeyVault;
56
using Microsoft.Azure.Services.AppAuthentication;
67
using Microsoft.Extensions.Configuration;
7-
using Microsoft.Extensions.Configuration.AzureKeyVault;
88
using Microsoft.Extensions.Hosting;
99

1010
public class Program
@@ -20,27 +20,57 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
2020
//Build the config from sources we have
2121
var config = builder.Build();
2222

23+
var uriList = new List<string>();
24+
2325
// Get the uri for the Vault from configuration
24-
var keyVaultUri = config["KeyVault:Uri"];
26+
// Try to get a string from configutation
27+
// This will happen when the config looks like:
28+
// {
29+
// "KeyVault":{
30+
// "Uri": "sample.vault.azure.net/"
31+
// }
32+
// }
33+
var uriString = config["KeyVault:Uri"];
34+
if (!string.IsNullOrWhiteSpace(uriString))
35+
{
36+
uriList.Add(uriString);
37+
}
38+
// This will happen when the config looks like:
39+
// {
40+
// "KeyVault":{
41+
// "Uri": [
42+
// "sample1.vault.azure.net/",
43+
// "sample2.vault.azure.net/"
44+
// ]
45+
// }
46+
// }
47+
else
48+
{
49+
uriList = config.GetSection("KeyVault:Uri").Get<List<string>>();
50+
}
51+
2552

2653
// Add KeyVault only if the uri is not empty
27-
if (!string.IsNullOrWhiteSpace(keyVaultUri))
54+
if (uriList?.Count > 0)
2855
{
29-
//Create Managed Service Identity token provider
56+
// Create Managed Service Identity token provider
3057
var azureServiceTokenProvider = new AzureServiceTokenProvider();
3158

32-
//Create the Key Vault client
59+
// Create the Key Vault client
3360
var keyVaultClient = new KeyVaultClient(
3461
new KeyVaultClient.AuthenticationCallback(
3562
azureServiceTokenProvider.KeyVaultTokenCallback)
3663
);
3764

38-
//Add Key Vault to configuration pipeline
39-
_ = builder.AddAzureKeyVault(
40-
keyVaultUri,
41-
keyVaultClient,
42-
new DefaultKeyVaultSecretManager()
43-
);
65+
foreach (var uri in uriList)
66+
{
67+
// Add Key Vault to configuration pipeline
68+
_ = builder.AddAzureKeyVault(
69+
uri,
70+
keyVaultClient,
71+
new PrefixKeyVaultSecretManager()
72+
);
73+
}
4474
}
4575
}
4676
);

0 commit comments

Comments
 (0)