問題描述
在Azure的同一數(shù)據(jù)中心,API Management訪問啟用了防火墻的Storage Account典蜕,并且把APIM的公網(wǎng)IP地址設(shè)置在白名單。但訪問依舊是403
原因是:
存儲帳戶部署在同一區(qū)域中的服務使用專用的 Azure IP 地址進行通信。 因此寨昙,不能基于特定的 Azure 服務的公共出站 IP 地址范圍來限制對其的訪問桨踪。
在Storage Account的網(wǎng)絡(luò)設(shè)置頁面老翘,有一個功能可以通過管理標識(Managed Identity)的方式訪問Storage Account。
Specify resource instances that will have access to your storage account based on their system-assigned managed identity.
根據(jù)系統(tǒng)分配的托管標識指定有權(quán)訪問存儲帳戶的資源實例锻离。
image.png
所以铺峭,如上圖所示,可以通過管理標識來指定APIM服務的實例來訪問Storage Account中的文件汽纠。本文就介紹 [在API Management服務中使用MI(管理標識 Managed Identity)訪問啟用防火墻的Storage Account]
實現(xiàn)步驟
第一步:啟用APIM服務的MI卫键,并添加Storage Account 的RBAC訪問權(quán)限
注意:不是 開發(fā)者門戶部分的Identity,而是APIM 安全部分的** Managed identities**
以下權(quán)限均可以訪問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服務名稱
image.png
第三步:為APIM中的接口添加 authentication-managed-identity Policy
- 在API的Inbound策略中,添加 <authentication-managed-identity resource="https://storage.azure.com/" />碴犬, resource內(nèi)容不變絮宁。即使在中國區(qū),也是使用 storage.azure.com域名
-
訪問Storage Account翅敌,必須攜帶 x-ms-version header羞福,為了避免每次手動輸入,所以在此處添加 set-header 策略來設(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>
測試訪問蚯涮,成功治专!
參考資料
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
當在復雜的環(huán)境中面臨問題,格物之道需:濁而靜之徐清遭顶,安以動之徐生张峰。 云中,恰是如此!