简体   繁体   English

访问 Azure Function 中的 Azure Key Vault 机密

[英]Access Azure Key Vault secret in Azure Function

I'm building Azure Function in python triggered with Event Grid events, which should be able to gather secret from Kay Vault.我正在构建 Azure Function 中的 python 由事件网格事件触发,应该能够从 Kay Vault 收集秘密。

I added system-assigned managed identity to my Function App, and then I was able to pick my App in Key Vault access policies.我将系统分配的托管标识添加到我的 Function 应用程序中,然后我能够在 Key Vault 访问策略中选择我的应用程序。 I gave it permissions like below:我给了它如下权限:

函数应用的权限

(I was trying different combinations at this one) (我在这一次尝试了不同的组合)

Also I provided new app setting with reference to mentioned key vault.我还参考提到的密钥库提供了新的应用程序设置。

密钥库参考

Unfortunetly when i try to check this value from code I'm unable to get this.不幸的是,当我尝试从代码中检查这个值时,我无法得到这个。

logging.info(os.environ)

When I add another app setting, just with plaintext it works great.当我添加另一个应用程序设置时,只需使用纯文本就可以了。 I will be grateful for any ideas what else can be done.我将不胜感激任何其他可以做的想法。

You can use the following helper to get the values您可以使用以下助手来获取值

namespace AccessKeyVault
{
    public static class GetKeyVaultValues
    {
        [FunctionName("GetKeyVaultValues")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            string linkKeyVaultUrl = $"https://keyVaultname.vault.azure.net/secrets/";
            string keyvaultKey = $"KeyVaultKey";
            var secretURL = linkKeyVaultUrl + keyvaultKey;

            //Get token from managed service principal
            var azureServiceTokenProvider = new AzureServiceTokenProvider();
            var kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
            try
            {
                var clientIdRecord = await kvClient.GetSecretAsync(secretURL).ConfigureAwait(false);

                string KeyvaultValue = clientIdRecord.Value;

                return req.CreateErrorResponse(HttpStatusCode.OK, "Key vault secret value is  :  " + KeyvaultValue);
            }
            catch (System.Exception ex)
            {

               return req.CreateResponse(HttpStatusCode.BadRequest, "Key vault value request is not successfull");
            }

        }
    }
}

I couldn't figure out what you want to get with os.environ .我不知道你想用os.environ得到什么。 I test with function It could work for me.我用 function 测试它可以为我工作。

In function to retrieve key-valut if you already set it in appsettings, you could use Environment.GetEnvironmentVariable("secrest name", EnvironmentVariableTarget.Process) to implement it.在 function 中检索键值,如果你已经在 appsettings 中设置了它,你可以使用Environment.GetEnvironmentVariable("secrest name", EnvironmentVariableTarget.Process)来实现它。

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM