Azure Function App 通过应用程序设置使用最新版本的 Key Vault Secret

Azure Function App use latest version of Key Vault Secret via Application Settings

我在消费计划上有一个 Linux 功能应用程序 运行,它使用应用程序设置中的 Key Vault 引用来检索和使用存储在 Azure Key Vault 中的秘密。

到目前为止一切正常。

但是,我们必须每天更改该机密(即在 Key Vault 中创建该机密的新版本并为该机密设置激活日期),并希望 Function App 自动检索和使用新版本一旦激活,无需手动更改 Kev Vault 对新版本秘密的引用。

目前这是否可能,如何实现?

编辑:现在可以了,看上面c0lby的回答。


带有一些附加信息的原始答案:

目前无法执行此操作。

https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references

Versions are currently required. When rotating secrets, you will need to update the version in your application configuration.

重新启动您的函数对您没有任何帮助,因为轮换秘密意味着您还创建了新版本的秘密。这可能也是目前不支持它的原因。当有新版本可用时,AppService 不会收到通知,并且您可能不希望 AppService 在更新 KeyVault 中的机密时自动重启。

您需要在函数代码中手动获取最新的活动机密,或者通过其他方法更新引用。我可能更喜欢第一种方法,因为它无需重新启动您的 AppService 即可工作。

https://docs.microsoft.com/en-us/samples/azure-samples/app-service-msi-keyvault-dotnet/keyvault-msi-appservice-sample/


    AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();

    try
    {
        var keyVaultClient = new KeyVaultClient(
            new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

        var secret = await keyVaultClient.GetSecretAsync("https://keyvaultname.vault.azure.net/secrets/secret")
            .ConfigureAwait(false);

        ViewBag.Secret = $"Secret: {secret.Value}";
        
    }
    //...
}

现已支持。

https://docs.microsoft.com/en-us/azure/app-service/app-service-key-vault-references#rotation

If a version is not specified in the reference, then the app will use the latest version that exists in Key Vault. When newer versions become available, such as with a rotation event, the app will automatically update and begin using the latest version within one day. Any configuration changes made to the app will cause an immediate update to the latest versions of all referenced secrets.