Share via


使用 Azure CLI 搭配虛擬網路服務端點原則來管理對 Azure 儲存體帳戶的資料外流

虛擬網路服務端點原則可讓您從虛擬網路內透過服務端點,將存取控制套用至 Azure 儲存體帳戶。 這是保護工作負載安全的關鍵,可管理允許的儲存體帳戶,以及允許資料外流的位置。 在本文中,您將學會如何:

  • 建立虛擬網路並新增子網路。
  • 針對 Azure 儲存體啟用服務端點。
  • 建立兩個 Azure 儲存體帳戶,並允許從上述建立的子網路對其進行網路存取。
  • 建立服務端點原則,只允許存取其中一個儲存體帳戶。
  • 將虛擬機器 (VM) 部署至子網路。
  • 確認可以從子網路存取允許的儲存體帳戶。
  • 確認從子網路存取非允許的儲存體帳戶時會遭到拒絕。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本文需要 2.0.28 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

建立虛擬網路

建立虛擬網路之前,您必須為虛擬網路以及在本文中建立的所有其他資源,建立資源群組。 使用 az group create 來建立資源群組。 下列範例會在 eastus 位置建立名為 myResourceGroup 的資源群組。

az group create \
  --name myResourceGroup \
  --location eastus

使用 az network vnet create 建立具有一個子網路的虛擬網路。

az network vnet create \
  --name myVirtualNetwork \
  --resource-group myResourceGroup \
  --address-prefix 10.0.0.0/16 \
  --subnet-name Private \
  --subnet-prefix 10.0.0.0/24

啟用服務端點

在此範例中,會針對 Private 子網路,建立 Microsoft.Storage 的服務端點:

az network vnet subnet create \
  --vnet-name myVirtualNetwork \
  --resource-group myResourceGroup \
  --name Private \
  --address-prefix 10.0.0.0/24 \
  --service-endpoints Microsoft.Storage

限制子網路的網路存取

使用 az network nsg create 建立網路安全性群組。 下列範例會建立名為 myNsgPrivate 的網路安全性群組。

az network nsg create \
  --resource-group myResourceGroup \
  --name myNsgPrivate

請使用 az network vnet subnet update 將網路安全性群組與「私人」子網路建立關聯。 下列範例會將 myNsgPrivate 網路安全性群組與「私人」子網路建立關聯:

az network vnet subnet update \
  --vnet-name myVirtualNetwork \
  --name Private \
  --resource-group myResourceGroup \
  --network-security-group myNsgPrivate

使用 az network nsg rule create 來建立安全性規則。 下列規則允許對指派給 Azure 儲存體服務之公用 IP 位址的輸出存取:

az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNsgPrivate \
  --name Allow-Storage-All \
  --access Allow \
  --protocol "*" \
  --direction Outbound \
  --priority 100 \
  --source-address-prefix "VirtualNetwork" \
  --source-port-range "*" \
  --destination-address-prefix "Storage" \
  --destination-port-range "*"

每個網路安全性群組均包含數個預設的安全性規則。 隨後的規則會覆寫允許對所有公用 IP 位址進行輸出存取的預設安全性規則。 destination-address-prefix "Internet" 選項會拒絕對所有公用 IP 位址的輸出存取。 上一個規則會因其具有較高優先順序而覆寫這項規則,從而允許對 Azure 儲存體之公用 IP 位址的存取。

az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNsgPrivate \
  --name Deny-Internet-All \
  --access Deny \
  --protocol "*" \
  --direction Outbound \
  --priority 110 \
  --source-address-prefix "VirtualNetwork" \
  --source-port-range "*" \
  --destination-address-prefix "Internet" \
  --destination-port-range "*"

下列規則允許 SSH 流量從任何地方輸入子網路。 此規則會覆寫會拒絕來自網際網路之所有連入流量的預設安全性規則。 允許透過 SSH 連線至子網路,以便在稍後步驟中測試連線能力。

az network nsg rule create \
  --resource-group myResourceGroup \
  --nsg-name myNsgPrivate \
  --name Allow-SSH-All \
  --access Allow \
  --protocol Tcp \
  --direction Inbound \
  --priority 120 \
  --source-address-prefix "*" \
  --source-port-range "*" \
  --destination-address-prefix "VirtualNetwork" \
  --destination-port-range "22"

限制對 Azure 儲存體帳戶的網路存取

此節列出的步驟可讓您從虛擬網路中的指定子網路,透過服務端點來限制對 Azure 儲存體帳戶的網路存取。

建立儲存體帳戶

使用 az storage account create 建立兩個 Azure 儲存體帳戶。

storageAcctName1="allowedstorageacc"

az storage account create \
  --name $storageAcctName1 \
  --resource-group myResourceGroup \
  --sku Standard_LRS \
  --kind StorageV2

storageAcctName2="notallowedstorageacc"

az storage account create \
  --name $storageAcctName2 \
  --resource-group myResourceGroup \
  --sku Standard_LRS \
  --kind StorageV2

建立儲存體帳戶之後,請使用 az storage account show-connection-string,將儲存體帳戶的連接字串擷取到變數中。 在稍後步驟中,會使用連接字串來建立檔案共用。

saConnectionString1=$(az storage account show-connection-string \
  --name $storageAcctName1 \
  --resource-group myResourceGroup \
  --query 'connectionString' \
  --out tsv)

saConnectionString2=$(az storage account show-connection-string \
  --name $storageAcctName2 \
  --resource-group myResourceGroup \
  --query 'connectionString' \
  --out tsv)

檢視變數的內容,並記下輸出中傳回的 AccountKey 值,因為後續步驟中會用到此值。

echo $saConnectionString1

echo $saConnectionString2

在儲存體帳戶中建立檔案共用

使用 az storage share create 在儲存體帳戶中建立檔案共用。 在稍後步驟中,會裝載此檔案共用,以確認其網路存取。

az storage share create \
  --name my-file-share \
  --quota 2048 \
  --connection-string $saConnectionString1 > /dev/null

az storage share create \
  --name my-file-share \
  --quota 2048 \
  --connection-string $saConnectionString2 > /dev/null

拒絕所有對儲存體帳戶的網路存取

根據預設,儲存體帳戶會接受來自任何網路用戶端的網路連線。 若要限制對選取網路的存取,請使用 az storage account update 將預設動作變更為「拒絕」。 一旦網路存取遭到拒絕後,就無法從任何網路存取儲存體帳戶。

az storage account update \
  --name $storageAcctName1 \
  --resource-group myResourceGroup \
  --default-action Deny

az storage account update \
  --name $storageAcctName2 \
  --resource-group myResourceGroup \
  --default-action Deny

允許從虛擬網路子網路進行網路存取

使用 az storage account network-rule add 允許從「私人」子網路對儲存體帳戶的網路存取。

az storage account network-rule add \
  --resource-group myResourceGroup \
  --account-name $storageAcctName1 \
  --vnet-name myVirtualNetwork \
  --subnet Private

az storage account network-rule add \
  --resource-group myResourceGroup \
  --account-name $storageAcctName2 \
  --vnet-name myVirtualNetwork \
  --subnet Private

套用原則以允許存取有效的儲存體帳戶

Azure 服務端點原則僅適用於 Azure 儲存體。 因此,我們將針對此範例設定,在此子網路上為 Microsoft.Storage 啟用服務端點。

服務端點原則會套用至所有服務端點。 我們將從建立服務端點原則開始。 接著,將在此原則下方建立原則定義,以針對此子網路核准 Azure 儲存體帳戶

建立服務端點原則

az network service-endpoint policy create \
  --resource-group myResourceGroup \
  --name mysepolicy \
  --location eastus

針對允許的儲存體帳戶,將資源 URI 儲存於變數中。 執行下列命令之前,請先將 <your-subscription-id> 取代為您訂用帳戶識別碼的實際值。

$serviceResourceId="/subscriptions/<your-subscription-id>/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/allowedstorageacc"

建立並新增原則定義,以針對服務端點原則允許上述 Azure 儲存體帳戶

az network service-endpoint policy-definition create \
  --resource-group myResourceGroup \
  --policy-name mysepolicy \
  --name mypolicydefinition \
  --service "Microsoft.Storage" \
  --service-resources $serviceResourceId

然後,更新虛擬網路子網路,以將其與在上一個步驟中建立的服務端點原則產生關聯

az network vnet subnet update \
  --vnet-name myVirtualNetwork \
  --resource-group myResourceGroup \
  --name Private \
  --service-endpoints Microsoft.Storage \
  --service-endpoint-policy mysepolicy

驗證對 Azure 儲存體帳戶的存取限制

建立虛擬機器

若要測試對儲存體帳戶的網路存取,請將 VM 部署至子網路。

使用 az vm create,在 Private 子網路中建立 VM。 如果預設金鑰位置中還沒有 SSH 金鑰,此命令將會建立這些金鑰。 若要使用一組特定金鑰,請使用 --ssh-key-value 選項。

az vm create \
  --resource-group myResourceGroup \
  --name myVmPrivate \
  --image <SKU linux image> \
  --vnet-name myVirtualNetwork \
  --subnet Private \
  --generate-ssh-keys

建立 VM 需要幾分鐘的時間。 在建立之後,請記下傳回之輸出中的 publicIpAddress。 在稍後的步驟中,這個位址可用來從網際網路存取虛擬機器。

確認對儲存體帳戶的存取

使用 SSH 連線到 myVmPrivate VM。 將 <publicIpAddress> 取代為您 myVmPrivate VM 的公用 IP 位址。

ssh <publicIpAddress>

建立裝載點的資料夾:

sudo mkdir /mnt/MyAzureFileShare1

將 Azure 檔案共用裝載至您所建立的目錄。 執行下列命令之前,請先將 <storage-account-key> 取代為 $saConnectionString1AccountKey 值。

sudo mount --types cifs //allowedstorageacc.file.core.windows.net/my-file-share /mnt/MyAzureFileShare1 --options vers=3.0,username=allowedstorageacc,password=<storage-account-key>,dir_mode=0777,file_mode=0777,serverino

您會收到 user@myVmPrivate:~$ 提示字元。 Azure 檔案共用已成功裝載到 /mnt/MyAzureFileShare

確認已拒絕存取儲存體帳戶

從相同的 VM myVmPrivate,建立掛接點的目錄:

sudo mkdir /mnt/MyAzureFileShare2

嘗試從 notallowedstorageacc 儲存體帳戶,將 Azure 檔案共用掛接到您建立的目錄。 本文假設您已部署最新的 Linux 發行版本。 如果您是使用先前的 Linux 發行版本,請參閱 Linux 上的裝載,以取得有關裝載檔案共用的其他指示。

執行下列命令之前,請先將 <storage-account-key> 取代為 $saConnectionString2AccountKey 值。

sudo mount --types cifs //notallowedstorageacc.file.core.windows.net/my-file-share /mnt/MyAzureFileShare2 --options vers=3.0,username=notallowedstorageacc,password=<storage-account-key>,dir_mode=0777,file_mode=0777,serverino

存取會遭到拒絕,而且您會收到 mount error(13): Permission denied 錯誤,因為此儲存體帳戶不在我們套用至子網路之服務端點原則的允許清單中。

結束對 myVmPublic VM 的 SSH 工作階段。

清除資源

請使用 az group delete 來移除不再需要的資源群組以及其所包含的所有資源。

az group delete --name myResourceGroup --yes

下一步

在此文章中,您已透過 Azure 虛擬網路服務端點,將服務端點原則套用至 Azure 儲存體。 您已建立 Azure 儲存體帳戶,並限制只能從虛擬網路子網路對特定儲存體帳戶進行網路存取 (因此也拒絕存取其他儲存體帳戶)。 若要深入了解服務端點原則,請參閱服務端點原則概觀