Skip to content

Commit c1d9855

Browse files
committed
added cli commands
1 parent dbc41a2 commit c1d9855

File tree

1 file changed

+244
-4
lines changed

1 file changed

+244
-4
lines changed

articles/virtual-network/virtual-network-service-endpoint-policies.md

Lines changed: 244 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,37 @@ $virtualNetwork | Set-AzVirtualNetwork @subnetpriv
133133

134134
### [CLI](#tab/cli)
135135

136+
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*:
156+
157+
```azurecli-interactive
158+
az network vnet subnet create \
159+
--vnet-name vnet-1 \
160+
--resource-group test-rg \
161+
--name subnet-1 \
162+
--address-prefix 10.0.0.0/24 \
163+
--service-endpoints Microsoft.Storage
164+
```
165+
166+
136167
---
137168

138169
## Restrict network access for the subnet
@@ -289,6 +320,58 @@ $virtualNetwork | Set-AzVirtualNetwork
289320

290321
### [CLI](#tab/cli)
291322

323+
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+
292375
---
293376

294377
## Restrict network access to Azure Storage accounts
@@ -360,6 +443,30 @@ New-AzStorageAccount @storageAcctParams
360443

361444
### [CLI](#tab/cli)
362445

446+
Create two Azure storage accounts with [az storage account create](/cli/azure/storage/account).
447+
448+
```azurecli-interactive
449+
storageAcctName1="allowedaccount"
450+
451+
az storage account create \
452+
--name $storageAcctName1 \
453+
--resource-group test-rg \
454+
--sku Standard_LRS \
455+
--kind StorageV2
456+
```
457+
458+
Use the same command to create the denied Azure storage account, but change the name to *deniedaccount*.
459+
460+
```azurecli-interactive
461+
storageAcctName2="deniedaccount"
462+
463+
az storage account create \
464+
--name $storageAcctName2 \
465+
--resource-group test-rg \
466+
--sku Standard_LRS \
467+
--kind StorageV2
468+
```
469+
363470
---
364471

365472
### Create file shares
@@ -388,7 +495,7 @@ New-AzStorageAccount @storageAcctParams
388495

389496
### [PowerShell](#tab/powershell)
390497

391-
### Create allowed storage account
498+
### Create allowed storage account file share
392499

393500
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.
394501

@@ -413,7 +520,7 @@ Create a file share with [New-AzStorageShare](/powershell/module/az.storage/new-
413520
$share1 = New-AzStorageShare file-share -Context $storageContext1
414521
```
415522

416-
### Create denied storage account
523+
### Create denied storage account file share
417524

418525
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.
419526

@@ -429,7 +536,7 @@ $storageAcctKey2 = (Get-AzStorageAccountKey @storageAcctParams1).Value[0]
429536
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.
430537

431538
```azurepowershell-interactive
432-
$storageContext12= New-AzStorageContext $storageAcctName2 $storageAcctKey2
539+
$storageContext2= New-AzStorageContext $storageAcctName2 $storageAcctKey2
433540
``
434541
435542
Create a file share with [New-AzStorageShare](/powershell/module/az.storage/new-azstorageshare).
@@ -440,6 +547,48 @@ $share2 = New-AzStorageShare file-share -Context $storageContext2
440547

441548
### [CLI](#tab/cli)
442549

550+
### Create allowed storage account file share
551+
552+
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.
553+
554+
```azurecli-interactive
555+
saConnectionString1=$(az storage account show-connection-string \
556+
--name $storageAcctName1 \
557+
--resource-group test-rg \
558+
--query 'connectionString' \
559+
--out tsv)
560+
```
561+
562+
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.
563+
564+
```azurecli-interactive
565+
az storage share create \
566+
--name file-share \
567+
--quota 2048 \
568+
--connection-string $saConnectionString1 > /dev/null
569+
```
570+
571+
### Create denied storage account file share
572+
573+
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.
574+
575+
```azurecli-interactive
576+
saConnectionString2=$(az storage account show-connection-string \
577+
--name $storageAcctName2 \
578+
--resource-group test-rg \
579+
--query 'connectionString' \
580+
--out tsv)
581+
```
582+
583+
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.
584+
585+
```azurecli-interactive
586+
az storage share create \
587+
--name file-share \
588+
--quota 2048 \
589+
--connection-string $saConnectionString2 > /dev/null
590+
```
591+
443592
---
444593

445594
### Deny all network access to storage accounts
@@ -522,9 +671,40 @@ $networkRuleParams2 = @{
522671
Add-AzStorageAccountNetworkRule @networkRuleParams2
523672
```
524673

525-
526674
### [CLI](#tab/cli)
527675

676+
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+
528708
---
529709

530710
## Apply policy to allow access to valid storage account
@@ -610,6 +790,34 @@ $sepolicy = New-AzServiceEndpointPolicy @sepolicyParams
610790

611791
### [CLI](#tab/cli)
612792

793+
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+
613821
---
614822

615823
## Associate a service endpoint policy to a subnet
@@ -650,6 +858,17 @@ $virtualNetwork | Set-AzVirtualNetwork
650858

651859
### [CLI](#tab/cli)
652860

861+
Use [az network vnet subnet update](/cli/azure/network/vnet/subnet) to associate the service endpoint policy to the subnet.
862+
863+
```azurecli-interactive
864+
az network vnet subnet update \
865+
--vnet-name vnet-1 \
866+
--resource-group test-rg \
867+
--name subnet-1 \
868+
--service-endpoints Microsoft.Storage \
869+
--service-endpoint-policy sepolicy
870+
```
871+
653872
---
654873

655874
>[!WARNING]
@@ -724,6 +943,18 @@ Wait for the virtual machine to finish deploying before continuing on to the nex
724943

725944
### [CLI](#tab/cli)
726945

946+
Create a VM in the *subnet-1* subnet with [az vm create](/cli/azure/vm).
947+
948+
```azurecli-interactive
949+
az vm create \
950+
--resource-group test-rg \
951+
--name vm-1 \
952+
--image Win2022Datacenter \
953+
--admin-username azureuser \
954+
--vnet-name vnet-1 \
955+
--subnet subnet-1
956+
```
957+
727958
---
728959

729960
### Confirm access to the *allowed* storage account
@@ -830,6 +1061,15 @@ Remove-AzResourceGroup @params
8301061

8311062
### [CLI](#tab/cli)
8321063

1064+
When no longer needed, use [az group delete](/cli/azure/group) to remove the resource group and all of the resources it contains.
1065+
1066+
```azurecli-interactive
1067+
az group delete \
1068+
--name test-rg \
1069+
--yes \
1070+
--no-wait
1071+
```
1072+
8331073
---
8341074

8351075
## Next steps

0 commit comments

Comments
 (0)