問(wèn)題描述
在Azure的同一數(shù)據(jù)中心,API Management訪問(wèn)啟用了防火墻的Storage Account,并且把APIM的公網(wǎng)IP地址設(shè)置在白名單。但訪問(wèn)依舊是403
原因是:
存儲(chǔ)帳戶部署在同一區(qū)域中的服務(wù)使用專用的 Azure IP 地址進(jìn)行通信。 因此,不能基于特定的 Azure 服務(wù)的公共出站 IP 地址范圍來(lái)限制對(duì)其的訪問(wèn)。
在Storage Account的網(wǎng)絡(luò)設(shè)置頁(yè)面,有一個(gè)功能可以通過(guò)管理標(biāo)識(shí)(Managed Identity)的方式訪問(wèn)Storage Account。
Specify resource instances that will have access to your storage account based on their system-assigned managed identity.
根據(jù)系統(tǒng)分配的托管標(biāo)識(shí)指定有權(quán)訪問(wèn)存儲(chǔ)帳戶的資源實(shí)例。
image.png
所以,如上圖所示,可以通過(guò)管理標(biāo)識(shí)來(lái)指定APIM服務(wù)的實(shí)例來(lái)訪問(wèn)Storage Account中的文件。本文就介紹 [在API Management服務(wù)中使用MI(管理標(biāo)識(shí) Managed Identity)訪問(wèn)啟用防火墻的Storage Account]
實(shí)現(xiàn)步驟
第一步:?jiǎn)⒂肁PIM服務(wù)的MI,并添加Storage Account 的RBAC訪問(wèn)權(quán)限
注意:不是 開(kāi)發(fā)者門(mén)戶部分的Identity,而是APIM 安全部分的** Managed identities**

以下權(quán)限均可以訪問(wèn)Storage Account:
- Storage Account Data Owner
- Storage Blob Data Contributor
- Storage Blob Data Reader
第二步:在Storage Account的Network中,添加APIM 服務(wù)訪問(wèn)實(shí)例,以及選擇正確的MI
- 在Resource type中選擇 Microsoft.ApiManagement/service
-
在Instance name中選擇APIM服務(wù)名稱
image.png
第三步:為APIM中的接口添加 authentication-managed-identity Policy
- 在API的Inbound策略中,添加 <authentication-managed-identity resource="https://storage.azure.com/" />, resource內(nèi)容不變。即使在中國(guó)區(qū),也是使用 storage.azure.com域名
-
訪問(wèn)Storage Account,必須攜帶 x-ms-version header,為了避免每次手動(dòng)輸入,所以在此處添加 set-header 策略來(lái)設(shè)置 x-ms-version 的值
image.png
完整的Polciy內(nèi)容:
<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>
測(cè)試訪問(wèn),成功!

參考資料
Storage Account允許從 Internet IP 范圍進(jìn)行訪問(wèn) : 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
當(dāng)在復(fù)雜的環(huán)境中面臨問(wèn)題,格物之道需:濁而靜之徐清,安以動(dòng)之徐生。 云中,恰是如此!


