-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrefixKeyVaultSecretManager.cs
35 lines (31 loc) · 1.19 KB
/
PrefixKeyVaultSecretManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Microsoft.Azure.KeyVault.Models;
namespace Microsoft.Extensions.Configuration.AzureKeyVault
{
/// <summary>
/// Custom Azure Vault Implementation for the prefix dev2--Key--Value syntax.
/// </summary>
public class PrefixKeyVaultSecretManager : IKeyVaultSecretManager
{
private readonly string _prefix;
public PrefixKeyVaultSecretManager(string prefix)
{
_prefix = $"{prefix}--";
}
public bool Load(SecretItem secret)
{
// Load a vault secret when its secret name starts with the
// prefix. Other secrets won't be loaded.
return secret.Identifier.Name.StartsWith(_prefix);
}
public string GetKey(SecretBundle secret)
{
// Remove the prefix from the secret name and replace two
// dashes in any name with the KeyDelimiter, which is the
// delimiter used in configuration (usually a colon). Azure
// Key Vault doesn't allow a colon in secret names.
return secret.SecretIdentifier.Name
.Substring(_prefix.Length)
.Replace("--", ConfigurationPath.KeyDelimiter);
}
}
}