You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Before creating a virtual network, you have to create a resource group for the virtual network, and all other resources created in this article. Create a resource group with [az group create](/cli/azure/group). The following example creates a resource group named *test-rg* in the *westus2* location.
137
+
138
+
```azurecli-interactive
139
+
az group create \
140
+
--name test-rg \
141
+
--location westus2
142
+
```
143
+
144
+
Create a virtual network with one subnet with [az network vnet create](/cli/azure/network/vnet).
145
+
146
+
```azurecli-interactive
147
+
az network vnet create \
148
+
--name vnet-1 \
149
+
--resource-group test-rg \
150
+
--address-prefix 10.0.0.0/16 \
151
+
--subnet-name subnet-1 \
152
+
--subnet-prefix 10.0.0.0/24
153
+
```
154
+
155
+
In this example, a service endpoint for *Microsoft.Storage* is created for the subnet *subnet-1*:
Create a network security group with [az network nsg create](/cli/azure/network/nsg). The following example creates a network security group named *nsg-1*.
324
+
325
+
```azurecli-interactive
326
+
az network nsg create \
327
+
--resource-group test-rg \
328
+
--name nsg-1
329
+
```
330
+
331
+
Associate the network security group to the *subnet-1* subnet with [az network vnet subnet update](/cli/azure/network/vnet/subnet). The following example associates the *nsg-1* network security group to the *subnet-1* subnet:
332
+
333
+
```azurecli-interactive
334
+
az network vnet subnet update \
335
+
--vnet-name vnet-1 \
336
+
--name subnet-1 \
337
+
--resource-group test-rg \
338
+
--network-security-group nsg-1
339
+
```
340
+
341
+
Create security rules with [az network nsg rule create](/cli/azure/network/nsg/rule). The rule that follows allows outbound access to the public IP addresses assigned to the Azure Storage service:
342
+
343
+
```azurecli-interactive
344
+
az network nsg rule create \
345
+
--resource-group test-rg \
346
+
--nsg-name nsg-1 \
347
+
--name Allow-Storage-All \
348
+
--access Allow \
349
+
--protocol "*" \
350
+
--direction Outbound \
351
+
--priority 100 \
352
+
--source-address-prefix "VirtualNetwork" \
353
+
--source-port-range "*" \
354
+
--destination-address-prefix "Storage" \
355
+
--destination-port-range "*"
356
+
```
357
+
358
+
Each network security group contains several [default security rules](./network-security-groups-overview.md#default-security-rules). The rule that follows overrides a default security rule that allows outbound access to all public IP addresses. The `destination-address-prefix "Internet"` option denies outbound access to all public IP addresses. The previous rule overrides this rule, due to its higher priority, which allows access to the public IP addresses of Azure Storage.
359
+
360
+
```azurecli-interactive
361
+
az network nsg rule create \
362
+
--resource-group test-rg \
363
+
--nsg-name nsg-1 \
364
+
--name Deny-Internet-All \
365
+
--access Deny \
366
+
--protocol "*" \
367
+
--direction Outbound \
368
+
--priority 110 \
369
+
--source-address-prefix "VirtualNetwork" \
370
+
--source-port-range "*" \
371
+
--destination-address-prefix "Internet" \
372
+
--destination-port-range "*"
373
+
```
374
+
292
375
---
293
376
294
377
## Restrict network access to Azure Storage accounts
Use [Get-AzStorageAccountKey](/powershell/module/az.storage/get-azstorageaccountkey) to get the storage account key for the allowed storage account. You will use this key in the next step to create a file share in the allowed storage account.
394
501
@@ -413,7 +520,7 @@ Create a file share with [New-AzStorageShare](/powershell/module/az.storage/new-
Use [Get-AzStorageAccountKey](/powershell/module/az.storage/get-azstorageaccountkey) to get the storage account key for the allowed storage account. You will use this key in the next step to create a file share in the denied storage account.
Create a context for your storage account and key with [New-AzStorageContext](/powershell/module/az.storage/new-AzStoragecontext). The context encapsulates the storage account name and account key.
Retrieve the connection string for the storage accounts into a variable with [az storage account show-connection-string](/cli/azure/storage/account). The connection string is used to create a file share in a later step.
Create a file share in the storage account with [az storage share create](/cli/azure/storage/share). In a later step, this file share is mounted to confirm network access to it.
Retrieve the connection string for the storage accounts into a variable with [az storage account show-connection-string](/cli/azure/storage/account). The connection string is used to create a file share in a later step.
Create a file share in the storage account with [az storage share create](/cli/azure/storage/share). In a later step, this file share is mounted to confirm network access to it.
By default, storage accounts accept network connections from clients in any network. To limit access to selected networks, change the default action to *Deny* with [az storage account update](/cli/azure/storage/account). Once network access is denied, the storage account is not accessible from any network.
677
+
678
+
```azurecli-interactive
679
+
az storage account update \
680
+
--name $storageAcctName1 \
681
+
--resource-group test-rg \
682
+
--default-action Deny
683
+
684
+
az storage account update \
685
+
--name $storageAcctName2 \
686
+
--resource-group test-rg \
687
+
--default-action Deny
688
+
```
689
+
690
+
### Enable network access only from the virtual network subnet
691
+
692
+
Allow network access to the storage account from the *subnet-1* subnet with [az storage account network-rule add](/cli/azure/storage/account/network-rule).
693
+
694
+
```azurecli-interactive
695
+
az storage account network-rule add \
696
+
--resource-group test-rg \
697
+
--account-name $storageAcctName1 \
698
+
--vnet-name vnet-1 \
699
+
--subnet subnet-1
700
+
701
+
az storage account network-rule add \
702
+
--resource-group test-rg \
703
+
--account-name $storageAcctName2 \
704
+
--vnet-name vnet-1 \
705
+
--subnet subnet-1
706
+
```
707
+
528
708
---
529
709
530
710
## Apply policy to allow access to valid storage account
Service endpoint policies are applied over service endpoints. We will start by creating a service endpoint policy. We will then create the policy definitions under this policy for Azure Storage accounts to be approved for this subnet
794
+
795
+
Use [az storage account show](/cli/azure/storage/account) to get the resource ID for the storage account that is allowed.
796
+
797
+
```azurecli-interactive
798
+
serviceResourceId=$(az storage account show --name allowedaccount --query id --output tsv)
799
+
```
800
+
801
+
Create a service endpoint policy
802
+
803
+
```azurecli-interactive
804
+
az network service-endpoint policy create \
805
+
--resource-group test-rg \
806
+
--name sepolicy \
807
+
--location eastus
808
+
```
809
+
810
+
Create and add a policy definition for allowing the previous Azure Storage account to the service endpoint policy
811
+
812
+
```azurecli-interactive
813
+
az network service-endpoint policy-definition create \
814
+
--resource-group test-rg \
815
+
--policy-name sepolicy \
816
+
--name policy-definition \
817
+
--service "Microsoft.Storage" \
818
+
--service-resources $serviceResourceId
819
+
```
820
+
613
821
---
614
822
615
823
## Associate a service endpoint policy to a subnet
0 commit comments