在Blob连接上使用Azure函数中具有Binder的密钥库密钥



我找到了这个代码示例来添加到Blob存储的连接并向文件中写入一些文本,但连接信息取决于包含连接字符串的环境变量名。我不知道如何提供一个包含连接字符串的密钥库秘密引用,而不必在连接字符串中使用环境变量名。

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host.Bindings.Runtime;
public static async Task Run(string input, Binder binder)
{
var attributes = new Attribute[]
{    
new BlobAttribute("samples-output/path"),
new StorageAccountAttribute("MyStorageAccount")
};
using (var writer = await binder.BindAsync<TextWriter>(attributes))
{
writer.Write("Hello World!");
}
}

我可以用下面的代码从我的密钥库中获取连接字符串,但我不知道如何将其提供给Binder使用。

string StorageConnectionString = GetSecrets(Environment.GetEnvironmentVariable("StorageAccountSecretUrl")).Result.Value;

调用的函数如下:

private static async Task<SecretBundle> GetSecrets(string Url)
{
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
return await keyVaultClient.GetSecretAsync(Url).ConfigureAwait(false);
}

目标是仅使用我的密钥保管库机密来更改连接字符串,并将连接字符串排除在配置文件之外。

看看这个名为";将密钥库引用用于应用服务和Azure功能";(https://learn.microsoft.com/en-us/azure/app-service/app-service-key-vault-references)。

此技术基本上允许您在不实质性更改功能代码的情况下使用密钥保管库。您在函数中保留对应用程序设置变量的引用(供绑定使用(,但该应用程序设置反过来又是指向密钥库机密的指针。该系统负责";翻译";在运行时指向秘密的指针。该过程确实涉及为应用程序服务/功能创建一个托管身份,以获得访问Vault的权限。

最新更新