问题描述
在Azure Function中,如何使用托管身份(Managed Identity) 而不是 AzureWebJobsStorage 来连接函数应用到存储账户?
原因是为了Storage Account更安全,减少Access Key的使用场景。或当Storage Account禁用了Access Key方式后。
问题解答
在函数应用中,通常使用应用设置 AzureWebJobsStorage 来配置连接字符串连接到Storage Account。
当在Storage Account中关闭了“Allow storage account key access”功能后。连接字符串也就无法正常工作。
本文介绍了如何使用 Azure Active Directory 身份而不是秘密或连接字符串来配置函数应用。使用身份可以避免意外泄露敏感秘密,并且可以更好地了解数据的访问方式。
以下是配置步骤
第一步:在函数应用中启用系统分配的身份并保存
第二步:为函数应用授予存储访问权限
在Storaeg Account的添加权限页面,搜索 Storage Blob Data Contributor 并选择它。
如果配置了 blob 触发的函数应用,重复步第二步。添加 Storage Queue Data Contributor 和 Storage Table Data Contributor 角色,这些角色将用于 Funtcion 的触发。
返回访问控制 (IAM),点击角色分配,搜索函数应用名称以确认角色已成功添加。完善的权限列表又如下三个:
- Storage Queue Data Contributor
- Storage Blob Data Contributor
- Storage Table Data Contributor
第三步:配置 AzureWebJobsStorage__blobServiceUri 参数
因为中国区Azure Storage Account使用的终结点后缀与Global Azure不同,所以这是不能配置AzureWebJobsStorage__accountname 并把值指定为 Stroage Account Name。
而是需要指定具体的Blob Service Uri,所以需要使用AzureWebJobsStorage__blobServiceUri 参数,并包含正确的域名,如:https://<your-storage-account-name>.blob.core.chinacloudapi.cn
注意:新设置使用双下划线 (__),这是应用设置中的特殊字符,如果需要使用Table 或 Queue,同理,配置它们的终结点:
- AzureWebJobsStorage__queueServiceUri,值为 https://<your-storage-account-name>.queue.core.chinacloudapi.cn/
- AzureWebJobsStorage__tableServiceUri,值为 https://<your-storage-account-name>.table.core.chinacloudapi.cn/
以上操作完成后,删除旧的配置 AzureWebJobsStorage。
Function App就能通过Managed Identity访问Storage Account。
参考文档
Use managed identity instead of AzureWebJobsStorage to connect a function app to a storage account : https://techcommunity.microsoft.com/blog/appsonazureblog/use-managed-identity-instead-of-azurewebjobsstorage-to-connect-a-function-app-to/3657606
Configure Durable Functions with managed identity :https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-configure-managed-identity
Azure Functions 的应用设置参考 : https://docs.azure.cn/zh-cn/azure-functions/functions-app-settings#azurewebjobsstorage
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!