Azure Key Vault 能否与 Functions 一起使用来存储队列触发器的连接字符串?

Can Azure Key Vault be used with Functions to store the connection string for queue triggers?

我能够在函数应用程序中使用 Key Vault as described here 但是当我尝试使用 Key Vault 来保存带有队列触发器的函数的连接字符串时,存储帐户出现问题连接字符串。该函数似乎找到了我提供的参数,但要么没有取回秘密,要么在抛出错误 No valid combination of account information found 时不喜欢这些信息。

我的函数定义为:

        [FunctionName("ReadQueueForMessage")]
        public static async Task Run([QueueTrigger("%AzureQueueTrigger%", Connection = "AzureWebJobsStorage")] string myQueueItem,
            Binder binderinputblob,
            ILogger log)

如果我只在我的 local.settings.json 中定义连接字符串,这就可以正常工作。我想要做的不是仅仅将连接字符串放在 json 文件中,而是想使用以下语法将函数指向 Key Vault:

"AzureWebJobsStorage": "@Microsoft.KeyVault(SecretUri=https://myappkeyvault.vault.azure.net/secrets/myapp-AzureWebJobsStorage-Queue/the-guid-of-secret)",

我确实去了 Key Vault 并更新了访问策略以包含函数应用程序,这样它就可以 read/list 秘密。文档 here 显示了在将配置部署到 Azure 后对配置进行更新。我正在尝试先在 Visual Studio 中进行测试。也许这就是问题所在?或者根本不可能以这种方式使用秘密?

I'm trying to test first in Visual Studio.

目前 将 Azure Key Vault 引用与 Azure Functions 结合使用不支持在本地工作,Azure Functions 团队确认了这一点。如果您仍想在本地进行测试,您可以实施一个不完整的本地解决方法,例如 issue.

我在门户网站上测试过,效果很好。您可以参考以下步骤:

1.In VS Function.cs,然后发布到azure:

 public static void Run([QueueTrigger("queue", Connection = "AzureWebJobsStorage")]string myQueueItem, TraceWriter log)
 {
     log.Info($"C# Queue trigger function processed: {myQueueItem}");
     string connectionString = System.Environment.GetEnvironmentVariable("AzureWebJobsStorage");
     log.Info($"The connection string is {connectionString}");
 }

2.Set AzureWebJobsStorageAppsettings setting 门户网站上。

3.Then 它将正常工作。

使用 Nuget 包 Azure.Extensions.AspNetCore.Configuration.SecretsAzure.Identity 您现在可以使用 KeyVault 作为配置提供程序,与 ASP.NET Core 中的方式相同。您需要添加从 FunctionsStartup 派生的 class 以将 KeyVault 添加为配置提供程序,请参阅 Add FunctionsStartup class with the KeyVault as a configuration provider

如果将 AzureWebJobsStorage 连接字符串作为密钥添加到 KeyVault,则可以将其从函数的 Configuration 部分中删除Azure 中的应用程序。确保在 Identity 部分中打开系统分配,并在 KeyVault 中添加一个 Access Policy,并为您的 Function App 添加秘密权限 Get 和 List .

当您 运行 本地 Function App 进行调试时,Azure.Identity 会自动使用您的 Microsoft 帐户访问 KeyVault,前提是它至少具有获取和列出机密的权限。

不幸的是,当您测试本地时,Function App 不会从 configuration/KeyVault 中读取 AzureWebJobsStorage,但需要将其存储在 local.settings.json。为防止在本地计算机上存储密钥,您可以在 local.settings.json.

中将 AzureWebJobsStorage 设置为“UseDevelopmentStorage=true”

有关详细说明,请参阅:Create Azure Function App with Dependency Injection and the Key Vault as Configuration Provider

示例项目:https://github.com/Forestbrook/FunctionWithKeyVaultAndDI