azure - 如何自动从 Key Vault 映射 Azure Functions secret

标签 azure azure-functions azure-keyvault

我想知道是否可以初始化队列触发器,甚至从 AzureVault 读取的连接字符串初始化 Blob 触发器。

现在,我们必须通过 Blade 属性的环境设置来设置这些数据连接。但是,我只想使用服务主体来检索 Azure Key Vault 的 token ,以获取所有这些连接字符串。

我正在尝试弄清楚如何在 java 中使其工作。

谢谢, 德里克

最佳答案

此功能已在此处跟踪并正在进行中:

编辑 2018 年 11 月 28 日:目前处于预览阶段

之前的回答 07/10/2018 此解决方案不适用于使用消耗计划的触发器。

同时,我对您的问题进行了一些研究,如果您使用 Azure Function v2,则可以从 key 保管库读取配置。

我已从 Visual Studio 创建了 Azure Functions v2(.NET Standard)。

它使用:

  • NETStandard.Library v2.0.3
  • Microsoft.NET.Sdk.Functions v1.0.22
  • Microsoft.Azure.WebJobs v3.0.0
  • Microsoft.Azure.WebJobs.Extensions.Storage v3.0.0

由于 Azure Functions v2 使用 ASP.NET core,因此我可以引用此链接来配置我的函数应用以使用 Azure Key Vault:

Azure Key Vault configuration provider in ASP.NET Core

  • 我添加了这个 nuget 包:
  • 我已将我的应用程序配置为使用此 nuget 包:

    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using System.Linq;
    
    [assembly: WebJobsStartup(typeof(FunctionApp1.WebJobsExtensionStartup), "A Web Jobs Extension Sample")]
    namespace FunctionApp1
    {
        public class WebJobsExtensionStartup : IWebJobsStartup
        {
            public void Configure(IWebJobsBuilder builder)
            {
                // Get the existing configuration
                var serviceProvider = builder.Services.BuildServiceProvider();
                var existingConfig = serviceProvider.GetRequiredService<IConfiguration>();
    
                // Create a new config based on the existing one and add kv
                var configuration = new ConfigurationBuilder()
                    .AddConfiguration(existingConfig)
                    .AddAzureKeyVault($"https://{existingConfig["keyVaultName"]}.vault.azure.net/")
                    .Build();
            
                // replace the existing configuration
                builder.Services
                    .Replace(ServiceDescriptor.Singleton(typeof(IConfiguration), configuration));
            }
        }
    }
    

    我的 Azure 函数使用 MSI:

    Azure Functions - Managed Service Identity

    我已向 key 保管库上的函数应用授予读取/列出 secret 权限:

    我有一个小的队列触发功能:

    public static class Function2
    {
        [FunctionName("Function2")]
        public static void Run([QueueTrigger("%queueName%", Connection = "queueConnectionString")]string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
    

    queueNamelocal.settings.json 文件中定义(应用程序设置 Blade 部署后):

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "keyVaultName": "thomastestkv",
        "queueName": "myqueue"
      }
    }
    

    queueConnectionString 在我的 keyvault 中配置:

    Azure Key Vault - Secrets

    关于azure - 如何自动从 Key Vault 映射 Azure Functions secret ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52655284/

    相关文章:

    azure - 为什么我的 Azure Function 找不到 Microsoft.Crm.Sdk.Proxy 程序集依赖项?

    asp.net - 如何通过 URL 访问 Azure SQL 数据库服务器管理 UI

    用于编排其他函数应用程序中的函数的 Azure 函数

    azure - 如何使用azure JavaScript函数应用程序在cosmos DB中插入数据?

    Azure Python 函数服务总线出站属性

    黑客入侵时的 Azure Key Vault 安全性

    c# - 如何使用 DotNetZip 即时提取 ZIP 文件?

    Git 发布具有大量 NuGet 依赖项的 Azure 网站

    azure - 如何将 Excel 文件中的 key 导入到 Azure key 保管库

    c# - 我们应该如何在 Azure Function 中将 Azure Key Vault 与 IOptions<T> 结合使用?