简体   繁体   English

从 Function 应用程序读取 Azure Key Vault Secret

[英]Read Azure Key Vault Secret from Function App

This Python script is deployed to run from Azure Function App on Linux Consumption plan, This script is expected to read secrets from Azure Key Vault. This Python script is deployed to run from Azure Function App on Linux Consumption plan, This script is expected to read secrets from Azure Key Vault.

Apart from code deployment, following configurations are made除代码部署外,还进行以下配置

  1. System Assigned Managed Access Enabled for Azure Function App为 Azure Function 应用启用系统分配的托管访问
  2. Azure Key Vault's Role Assignments Reference this Function App with Reader role. Azure Key Vault 的角色分配参考此 Function 应用程序与Reader角色。

Here is the script from __init.py__这是来自__init.py__的脚本

import azure.functions as func
import os
from azure.identity import ManagedIdentityCredential
from azure.keyvault.secrets import SecretClient

def main(mytimer: func.TimerRequest) -> None:
    identity = ManagedIdentityCredential()
    secretClient = SecretClient(vault_url="https://vault-name.vault.azure.net/", credential=identity)
    secret = secretClient.get_secret('secret-name').
    print (secret.value)

This function app requires following libraries and defined in requirements.txt file此 function 应用程序需要以下库并在requirements.txt文件中定义

azure-functions
azure-keyvault-secrets
azure-identity

This function runs and ends up following exception.此 function 运行并以异常结束。

Result: Failure Exception: HttpResponseError: (Forbidden) The user, group or application 'appid=6fb8XXXX-bba6-4fa7-8a76-a193XXXXd8d7;oid=e4c1XXXX-602d-44b7-a2e6-f646XXXXe360;iss=https://sts.windows.net/320bXXXX-7580-46ef-a61a-7f3fXXXXbe8f/' does not have secrets get permission on key vault 'vault-name;location=northcentralus'. 
For help resolving this issue, please see https://go.microsoft.com/fwlink/?linkid=2125287 Stack: 

File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", 
line 315, in _handle__invocation_request self.__run_sync_func, invocation_id, fi.func, args) File "/usr/local/lib/python3.7/concurrent/futures/thread.py", 
line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", 
line 434, in __run_sync_func return func(**params) File "/home/site/wwwroot/FunctionAppName/__init__.py", 
line 14, in main secret = secretClient.get_secret('secret-name') File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/core/tracing/decorator.py", 
line 83, in wrapper_use_tracer return func(*args, **kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/keyvault/secrets/_client.py", 
line 71, in get_secret **kwargs File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/keyvault/secrets/_shared/_generated/v7_0/operations/_key_vault_client_operations.py", 
line 1625, in get_secret map_error(status_code=response.status_code, response=response, error_map=error_map) File "/home/site/wwwroot/.python_packages/lib/site-packages/azure/core/exceptions.py", 
line 102, in map_error raise error

This error describes that the Application does not have Secrets get permission on Key Vault, But as mentioned above Role assignments are made to Function App with Reader role on Key Vault.此错误描述应用程序在 Key Vault 上没有 Secrets 获得权限,但如上所述,角色分配是对 Function App 进行的,在 Key Vault 上具有 Reader 角色。

What will be the possible issue with configuration and how to mitigate?配置可能出现什么问题以及如何缓解?

In order to read secrets you actually need to create an access policy and add the managed identity related to your Azure Function:为了读取机密,您实际上需要创建访问策略并添加与您的 Azure Function 相关的托管身份:

在此处输入图像描述

You can also update Key Vault access policies through the Azure SDK, using the azure-mgmt-keyvault library.您还可以使用azure-mgmt-keyvault库通过 Azure SDK 更新 Key Vault 访问策略。 Here's an example of how you could give your app permission to get secrets, which would resolve your issue:这是一个示例,说明如何授予应用获取机密的权限,这将解决您的问题:

from azure.identity import ManagedIdentityCredential
from azure.mgmt.keyvault import KeyVaultManagementClient
from azure.mgmt.keyvault.v2019_09_01.models import AccessPolicyEntry, AccessPolicyUpdateKind, SecretPermissions, Permissions, VaultAccessPolicyParameters, VaultAccessPolicyProperties

subscription_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
tenant_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
object_id = "object ID of your app"
client_id = "client ID of your app"

credential = ManagedIdentityCredential()
client = KeyVaultManagementClient(credential, subscription_id)

permissions = Permissions(secrets=[SecretPermissions.get])
access_policy = AccessPolicyEntry(
    tenant_id=tenant_id,
    object_id=object_id,
    application_id=client_id,
    permissions=permissions
)
access_policy_property = VaultAccessPolicyProperties(access_policies=[access_policy])
access_policy_params = VaultAccessPolicyParameters(properties=access_policy_property)

client.vaults.update_access_policy(
    resource_group_name="resource-group",
    vault_name="vault-name",
    operation_kind=AccessPolicyUpdateKind.add,
    parameters=access_policy_params
)

(I work on the Azure SDK in Python) (我在 Python 中研究 Azure SDK)

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

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