【Azure API Management】实现在API Management服务中使用MI(管理标识 Managed Identity)访问启用防火墙的Storage Account

发布时间 2023-07-17 20:23:30作者: 路边两盏灯

问题描述

在Azure的同一数据中心,API Management访问启用了防火墙的Storage Account,并且把APIM的公网IP地址设置在白名单。但访问依旧是403

原因是:

存储帐户部署在同一区域中的服务使用专用的 Azure IP 地址进行通信。 因此,不能基于特定的 Azure 服务的公共出站 IP 地址范围来限制对其的访问。

在Storage Account的网络设置页面,有一个功能可以通过管理标识(Managed Identity)的方式访问Storage Account。

Specify resource instances that will have access to your storage account based on their system-assigned managed identity.

根据系统分配的托管标识指定有权访问存储帐户的资源实例。

所以,如上图所示,可以通过管理标识来指定APIM服务的实例来访问Storage Account中的文件。本文就介绍 [在API Management服务中使用MI(管理标识 Managed Identity)访问启用防火墙的Storage Account] 

 

实现步骤

第一步:启用APIM服务的MI,并添加Storage Account 的RBAC访问权限

注意:不是 开发者门户部分的Identity,而是APIM 安全部分的 Managed identities

以下权限均可以访问Storage Account:

  • Storage Account Data Owner
  • Storage Blob Data Contributor
  • Storage Blob Data Reader

 

第二步:在Storage Account的Network中,添加APIM 服务访问实例,以及选择正确的MI

  • 在Resource type中选择 Microsoft.ApiManagement/service
  • 在Instance name中选择APIM服务名称

 

 

第三步:为APIM中的接口添加 authentication-managed-identity Policy 

  • 在API的Inbound策略中,添加 <authentication-managed-identity resource="https://storage.azure.com/" />, resource内容不变。即使在中国区,也是使用 storage.azure.com域名 
  • 访问Storage Account,必须携带 x-ms-version header,为了避免每次手动输入,所以在此处添加 set-header 策略来设置 x-ms-version 的值

 完整的Polciy内容:

<policies>
    <inbound>
        <base />
        <authentication-managed-identity resource="https://storage.azure.com/" />
        <set-header name="X-Ms-Version" exists-action="override">
            <value>2022-11-02</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

 

测试访问,成功!

 

参考资料

Storage Account允许从 Internet IP 范围进行访问 : https://docs.azure.cn/zh-cn/storage/common/storage-network-security?tabs=azure-portal#grant-access-from-an-internet-ip-range

Authenticate with managed identity : https://learn.microsoft.com/en-us/azure/api-management/authentication-managed-identity-policy#examples