forked from erjosito/azcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vwan_2xshub.azcli
2368 lines (2284 loc) · 132 KB
/
vwan_2xshub.azcli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
############################################################################
# Created by Jose Moreno
# October 2020
#
# The script creates a VWAN lab with
# * 2 secure vhubs
# * 2 branches connecting over VPN (simulated via Cisco CSR)
#
# CLI extensions required:
# * virtual-wan
# * azure-firewall
#
# Tested with zsh
############################################################################
###############
# Variables #
###############
# Control
no_of_hubs=2 # 1 or 2 hubs
no_of_spokes=1 # Number of spokes per hub (without counting the hub with NVA for indirect spokes)
secure_hub=no # Whether firewalls are provisioned in the hubs
public_ip=no # Whether extra VMs are created with public IP addressing
indirect_spokes=no # Whether spokes with Linux NVAs and indirect spokes are created
nva_bgp=no # If using an NVA in last spoke (for example 14/24), whether configuring BGP on it towards VWAN
vnet_ass=default # Route table to associate VNet connections. Can be 'default' or 'vnet'
vnet_prop=default # Route table to propagate from VNet connections. Can be 'default', 'vnet' or 'none'
nva_in_hub1=no # Whether a Linux NVA is deployed in hub1 (note you need to have a valid NVA image in the same subscription!)
nva_in_hub2=no # Whether a Linux NVA is deployed in hub2 (note you need to have a valid NVA image in the same subscription!)
deploy_vpngw=no # Whether VPN GWs should be deployed
deploy_er=yes # Whether ER should be provided (note you need an account with a provider such as Megaport)
route_thru_nva=no # Whether a static pointing to the NVA in the last spoke is configured
er_bow_tie=yes # If creating ER in multiple regions, whether each region should be connected to both virtual hubs
# Generic variables
rg=vwaneastus
vwan=vwan
location1=germanywestcentral
location2=westcentralus
password=Microsoft123!
vwan_hub1_prefix=192.168.0.0/23
vwan_hub2_prefix=192.168.2.0/23
username=$(whoami)
vm_size=Standard_B1s
nva_size=Standard_B2ms
# Branches
publisher=cisco
offer=cisco-csr-1000v
sku=16_12-byol
version=$(az vm image list -p $publisher -f $offer -s $sku --all --query '[0].version' -o tsv)
branch1_prefix=10.201.0.0/24
branch1_prefix_long="10.201.0.0 255.255.255.0"
branch1_subnet=10.201.0.0/26
branch1_vm_subnet=10.201.0.64/26
branch1_gateway=10.201.0.1
branch1_bgp_ip=10.201.0.10
branch1_asn=65501
branch2_prefix=10.202.0.0/24
branch2_prefix_long="10.202.0.0 255.255.255.0"
branch2_vm_subnet=10.202.0.64/26
branch2_subnet=10.202.0.0/26
branch2_gateway=10.202.0.1
branch2_bgp_ip=10.202.0.10
branch2_2ary_bgp_ip=10.202.0.20
branch2_asn=65502
branch3_prefix=10.213.0.0/24
branch3_subnet=10.213.0.0/26
branch3_vm_subnet=10.213.0.64/26
branch3_gateway=10.213.0.1
branch3_bgp_ip=10.213.0.75
branch3_asn=65503
branch3_test_route=3.3.3.0/24
branch4_prefix=10.214.0.0/24
branch4_vm_subnet=10.214.0.64/26
branch4_subnet=10.214.0.0/26
branch4_gateway=10.214.0.1
branch4_bgp_ip=10.214.0.75
branch4_asn=65504
branch4_test_route=4.4.4.0/24
# ER (optional)
er_provider=Megaport
er_circuit_sku=Premium
megaport_script_location="/home/jose/repos/azcli/megaport.sh"
hub1_er_pop=Frankfurt
hub1_er_circuit_name="er1-$hub1_er_pop"
hub2_er_pop=Dallas
hub2_er_circuit_name="er2-$hub2_er_pop"
# gcloud variables to simulate onprem via ER
project_name=onprem
project_id="${project_name}${RANDOM}"
machine_type=e2-micro
gcp_asn=16550
# gcp region 1
region1=europe-west3
zone1=europe-west3-b
gcp_vm1_name=vm1
gcp_vpc1_name=vpc1
gcp_subnet1_name=vm1
gcp_subnet1_prefix='10.225.0.0/24'
attachment1_name=attachment1
router1_name=router1
# gcp region 2
region2=us-west2
zone2=us-west2-b
gcp_vm2_name=vm2
gcp_vpc2_name=vpc2
gcp_subnet2_name=vm2
gcp_subnet2_prefix='10.226.0.0/24'
attachment2_name=attachment2
router2_name=router2
#############
# Functions #
#############
# Wait for resource to be created
function wait_until_finished {
wait_interval=15
resource_id=$1
resource_name=$(echo $resource_id | cut -d/ -f 9)
echo "Waiting for resource $resource_name to finish provisioning..."
start_time=`date +%s`
state=$(az resource show --id $resource_id --query properties.provisioningState -o tsv)
until [[ "$state" == "Succeeded" ]] || [[ "$state" == "Failed" ]] || [[ -z "$state" ]]
do
sleep $wait_interval
state=$(az resource show --id $resource_id --query properties.provisioningState -o tsv)
done
if [[ -z "$state" ]]
then
echo "Something really bad happened..."
else
run_time=$(expr `date +%s` - $start_time)
((minutes=${run_time}/60))
((seconds=${run_time}%60))
echo "Resource $resource_name provisioning state is $state, wait time $minutes minutes and $seconds seconds"
fi
}
# Helper function to calculate the default gateway for a subnet
# Example: default_gw 172.16.1.31 255.255.255.248
function default_gw(){
IP=$1
MASK=$2
DEBUG=$3 # Set to yes for message debugging
IP_HEX=$(printf '%.2X%.2X%.2X%.2X\n' `echo $IP | sed -e 's/\./ /g'`)
MASK_HEX=$(printf '%.2X%.2X%.2X%.2X\n' `echo $MASK | sed -e 's/\./ /g'`)
IP_DEC=$(echo "ibase=16; $IP_HEX" | bc)
MASK_DEC=$(echo "ibase=16; $MASK_HEX" | bc)
SUBNET_DEC=$(( IP_DEC&MASK_DEC ))
GW_DEC=$(( $SUBNET_DEC + 1 ))
GW_HEX=$(printf '%x\n' $GW_DEC)
if [[ "${#GW_HEX}" == "7" ]]; then
GW_HEX="0${GW_HEX}"
fi
GW=$(printf '%d.%d.%d.%d\n' `echo $GW_HEX | sed -r 's/(..)/0x\1 /g'`)
if [[ "$DEBUG" == "yes" ]]
then
echo "Input: ${IP}/${MASK}"
echo "Decimal: ${IP_DEC}/${MASK_DEC}"
echo "Subnet decimal: ${SUBNET_DEC}"
echo "Gateway dec: ${GW_DEC}"
echo "Gateway hex: ${GW_HEX}"
echo "Gateway: ${GW}"
else
echo "$GW"
fi
}
# Create a VNet with a test VM and optionally attach it to a hub
function create_spoke() {
hub=$1
spoke=$2
if [[ -z "$hub" ]] || [[ -z "spoke" ]]
then
echo "You need to provide a hub ID and a spoke ID"
exit
fi
connect_to_hub=$3
if [[ -z "$connect_to_hub" ]]
then
connect_to_hub="yes"
fi
spoke_id="${hub}${spoke}"
hub_name="hub${hub}"
vnet_prefix="10.${hub}.${spoke}.0/24"
subnet_prefix="10.${hub}.${spoke}.0/26"
if [[ "$hub" == "1" ]]
then
location="$location1"
nsg_name="$nsg1_name"
hub_vnet_ass_rt_id="$hub1_vnet_ass_rt_id"
hub_vnet_prop_rt_id="$hub1_vnet_prop_rt_id"
elif [[ "$hub" == "2" ]]
then
location="$location2"
nsg_name="$nsg2_name"
hub_vnet_ass_rt_id="$hub2_vnet_ass_rt_id"
hub_vnet_prop_rt_id="$hub2_vnet_prop_rt_id"
else
echo "ERROR: $hub is not a valid hub ID"
exit
fi
echo "Creating VM spoke${spoke_id}-vm in VNet spoke${spoke_id}-$location..."
az vm create -n spoke${spoke_id}-vm -g $rg -l $location --image ubuntuLTS --admin-username $username --generate-ssh-keys \
--public-ip-address spoke${spoke_id}-pip --public-ip-sku Standard --vnet-name spoke${spoke_id}-$location --nsg $nsg_name --size $vm_size \
--vnet-address-prefix $vnet_prefix --subnet vm --subnet-address-prefix $subnet_prefix --custom-data $cloudinit_file -o none
echo "Installing Network Watcher extension in VM spoke${spoke_id}-vm..."
az vm extension set --vm-name spoke${spoke_id}-vm -g $rg -n NetworkWatcherAgentLinux --publisher Microsoft.Azure.NetworkWatcher --version 1.4 -o none
if [[ "$connect_to_hub" == "yes" ]]
then
echo "Connecting VNet spoke${spoke_id} to $hub_name..."
az network vhub connection create -n spoke${spoke_id} -g $rg --vhub-name $hub_name --remote-vnet spoke${spoke_id}-$location \
--internet-security true --associated-route-table $hub_vnet_ass_rt_id --propagated-route-tables $hub_vnet_prop_rt_id --labels $vnet_prop_label -o none
else
echo "Skipping connection to the hub"
fi
}
# Create a linux VM to simulate a branch to connect to an NVA deployed in the hub
function create_hub_nva_branch() {
hub_id=$1
hub_name="hub${hub_id}"
nva_name="${hub_name}nva"
nva_asn="6510${hub_id}"
# Set variables depending on which hub we are connecting the branch to
if [[ "$hub_id" == "1" ]]
then
onprem_vnet_name=branch3
onprem_vnet_prefix=$branch3_prefix
onprem_nva_subnet_name=onpremnva
onprem_nva_subnet_prefix=$branch3_vm_subnet
onprem_linuxnva_asn=${branch3_asn}
onprem_linuxnva_name=branch3-nva
onprem_linuxnva_pip=${onprem_linuxnva_name}-pip
onprem_linuxnva_ip=$branch3_bgp_ip
onprem_test_route=$branch3_test_route
vhub_space="$vwan_hub1_prefix"
location=$location1
storage_account_name=$storage_account1_name
else
onprem_vnet_name=branch4
onprem_vnet_prefix=$branch4_prefix
onprem_nva_subnet_name=onpremnva
onprem_nva_subnet_prefix=$branch4_vm_subnet
onprem_linuxnva_asn=${branch4_asn}
onprem_linuxnva_name=branch4-nva
onprem_linuxnva_pip=${onprem_linuxnva_name}-pip
onprem_linuxnva_ip=$branch4_bgp_ip
onprem_test_route=$branch4_test_route
vhub_space="$vwan_hub2_prefix"
location=$location2
storage_account_name=$storage_account2_name
fi
# Get RS values from the virtual hub
echo "Getting IP addresses and ASN from ${hub_name}..."
rs_ip1=$(az network vhub show -n $hub_name -g $rg --query 'virtualRouterIps[0]' -o tsv) && echo $rs_ip1
rs_ip2=$(az network vhub show -n $hub_name -g $rg --query 'virtualRouterIps[1]' -o tsv) && echo $rs_ip2
rs_asn=$(az network vhub show -n $hub_name -g $rg --query 'virtualRouterAsn' -o tsv) && echo $rs_asn
# Create VNet
echo "Creating VNet $onprem_vnet_name with $onprem_vnet_prefix..."
az network vnet create -n $onprem_vnet_name -g $rg --address-prefixes $onprem_vnet_prefix --subnet-name $onprem_nva_subnet_name --subnet-prefixes $onprem_nva_subnet_prefix -o none
# Cloudinit file for onprem NVA
linuxnva_cloudinit_file=/tmp/linuxnva_cloudinit.txt
cat <<EOF > $linuxnva_cloudinit_file
#cloud-config
runcmd:
- apt update && apt install -y bird strongswan
- sysctl -w net.ipv4.ip_forward=1
- sysctl -w net.ipv4.conf.all.accept_redirects = 0
- sysctl -w net.ipv4.conf.all.send_redirects = 0
EOF
# NSG for onprem NVA
echo "Creating NSG ${onprem_linuxnva_name}-nsg..."
az network nsg create -n "${onprem_linuxnva_name}-nsg" -g $rg -o none
az network nsg rule create -n SSH --nsg-name "${onprem_linuxnva_name}-nsg" -g $rg --priority 1000 --destination-port-ranges 22 --access Allow --protocol Tcp -o none
az network nsg rule create -n IKE --nsg-name "${onprem_linuxnva_name}-nsg" -g $rg --priority 1010 --destination-port-ranges 4500 --access Allow --protocol Udp -o none
az network nsg rule create -n IPsec --nsg-name "${onprem_linuxnva_name}-nsg" -g $rg --priority 1020 --destination-port-ranges 500 --access Allow --protocol Udp -o none
az network nsg rule create -n ICMP --nsg-name "${onprem_linuxnva_name}-nsg" -g $rg --priority 1030 --destination-port-ranges '*' --access Allow --protocol Icmp -o none
az network watcher flow-log create -l $location -n ${onprem_linuxnva_name}-nsg -g $rg \
--nsg ${onprem_linuxnva_name}-nsg --storage-account $storage_account_name --log-version 2 --retention 7 -o none
# VM for onprem NVA
echo "Creating VM $onprem_linuxnva_name..."
az vm create -n $onprem_linuxnva_name -g $rg -l $location --image ubuntuLTS --generate-ssh-keys \
--public-ip-address $onprem_linuxnva_pip --public-ip-sku Standard --vnet-name $onprem_vnet_name --size $vm_size --subnet $onprem_nva_subnet_name \
--custom-data $linuxnva_cloudinit_file --private-ip-address "$onprem_linuxnva_ip" --nsg "${onprem_linuxnva_name}-nsg" -o none
onprem_linuxnva_nic_id=$(az vm show -n $onprem_linuxnva_name -g "$rg" --query 'networkProfile.networkInterfaces[0].id' -o tsv)
az network nic update --ids $onprem_linuxnva_nic_id --ip-forwarding -o none
echo "Getting information about the created VM..."
onprem_linuxnva_pip_ip=$(az network public-ip show -n $onprem_linuxnva_pip -g $rg --query ipAddress -o tsv) && echo $onprem_linuxnva_pip_ip
onprem_linuxnva_private_ip=$(az network nic show --ids $onprem_linuxnva_nic_id --query 'ipConfigurations[0].privateIpAddress' -o tsv) && echo $onprem_linuxnva_private_ip
onprem_linuxnva_default_gw=$(default_gw "$onprem_linuxnva_ip" "255.255.255.192") && echo $onprem_linuxnva_default_gw
# Public IPs of the external interfaces of the VWAN NVAs
echo "Getting public IPs of the NVA $nva_name deployed in the virtual hub..."
pip1=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[1].publicIpAddress' -o tsv) && echo $pip1
pip2=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[3].publicIpAddress' -o tsv) && echo $pip2
# Private IPs of the external interfaces of the VWAN NVAs
echo "Getting private IPs (external NICs) of the NVA $nva_name deployed in the virtual hub..."
nva_ip21=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[1].privateIpAddress' -o tsv) && echo $nva_ip21
nva_ip22=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[3].privateIpAddress' -o tsv) && echo $nva_ip22
# Private IPs VWAN NVAs
echo "Getting private IPs (internal NICs) of the NVA $nva_name deployed in the virtual hub..."
nva_private_ip11=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[0].privateIpAddress' -o tsv) && echo $nva_private_ip11
nva_private_ip12=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[2].privateIpAddress' -o tsv) && echo $nva_private_ip12
# Gateway for the VWAN NVA (internal NIC)
nva_internal_mask=$(ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$pip1" "ifconfig eth1 | grep netmask | sed -n 's/.* netmask \([^ ]*\).*/\1/p'") && echo "Network mask $nva_internal_mask"
if [[ "${nva_internal_mask:0:11}" != "255.255.255" ]]
then
echo "It looks like $nva_internal_mask is not a correct network mask, defaulting to 255.255.255.240..."
nva_internal_mask="255.255.255.240"
fi
nva_default_gw=$(default_gw "$nva_private_ip11" "$nva_internal_mask") && echo "Internal gateway $nva_default_gw"
# Private/public endpoints (A/B are the redundant hub NVAs, C is onprem)
endpoint_a_public=$pip1
endpoint_a_private=$nva_ip21
endpoint_b_public=$pip2
endpoint_b_private=$nva_ip22
endpoint_c_public=$onprem_linuxnva_pip_ip
endpoint_c_private=$onprem_linuxnva_private_ip
echo "Configuring VPN between A:${endpoint_a_public}/${endpoint_a_private} and C:${endpoint_c_public}/${endpoint_c_private}"
echo "Configuring VPN between B:${endpoint_b_public}/${endpoint_b_private} and C:${endpoint_c_public}/${endpoint_c_private}"
# Initial config: NVA 0
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo ip route add $vhub_space via $nva_default_gw"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo ip tunnel add vti0 local $endpoint_a_private remote $endpoint_c_public mode vti key 11"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo sysctl -w net.ipv4.conf.vti0.disable_policy=1"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo ip link set up dev vti0"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo ip route add ${endpoint_c_private}/32 dev vti0"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo sed -i 's/# install_routes = yes/install_routes = no/' /etc/strongswan.d/charon.conf"
# Initial config: NVA 1
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo ip route add $vhub_space via $nva_default_gw"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo ip tunnel add vti0 local $endpoint_b_private remote $endpoint_c_public mode vti key 11"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo sysctl -w net.ipv4.conf.vti0.disable_policy=1"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo ip link set up dev vti0"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo ip route add ${endpoint_c_private}/32 dev vti0"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo sed -i 's/# install_routes = yes/install_routes = no/' /etc/strongswan.d/charon.conf"
# Initial config: Onprem
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo ip tunnel add vti0 local $endpoint_c_private remote $endpoint_a_public mode vti key 11"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo sysctl -w net.ipv4.conf.vti0.disable_policy=1"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo ip link set up dev vti0"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo ip route add ${nva_ip21}/32 dev vti0"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo ip tunnel add vti1 local $endpoint_c_private remote $endpoint_b_public mode vti key 12"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo sysctl -w net.ipv4.conf.vti1.disable_policy=1"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo ip link set up dev vti1"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo ip route add ${nva_ip22}/32 dev vti1"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo sed -i 's/# install_routes = yes/install_routes = no/' /etc/strongswan.d/charon.conf"
# IPsec Config files
vpn_psk=$(openssl rand -base64 64)
vpn_psk=${vpn_psk//$'\n'/} # Remove line breaks
psk_file_a=/tmp/ipsec.secrets.a
psk_file_b=/tmp/ipsec.secrets.b
psk_file_c=/tmp/ipsec.secrets.c
# PSK VWAN NVA 0
cat <<EOF > $psk_file_a
$endpoint_a_public $endpoint_c_public : PSK "$vpn_psk"
EOF
# PSK VWAN NVA 1
cat <<EOF > $psk_file_b
$endpoint_b_public $endpoint_c_public : PSK "$vpn_psk"
EOF
# PSK onprem
cat <<EOF > $psk_file_c
$endpoint_c_public $endpoint_a_public : PSK "$vpn_psk"
$endpoint_c_public $endpoint_b_public : PSK "$vpn_psk"
EOF
ipsec_file_a=/tmp/ipsec.conf.a
ipsec_file_b=/tmp/ipsec.conf.b
ipsec_file_c=/tmp/ipsec.conf.c
cat <<EOF > $ipsec_file_a
config setup
charondebug="all"
uniqueids=yes
strictcrlpolicy=no
conn to-onprem
authby=secret
leftid=$endpoint_a_public
leftsubnet=0.0.0.0/0
right=$endpoint_c_public
rightsubnet=0.0.0.0/0
ike=aes256-sha2_256-modp1024!
esp=aes256-sha2_256!
keyingtries=0
ikelifetime=1h
lifetime=8h
dpddelay=30
dpdtimeout=120
dpdaction=restart
auto=start
mark=11
EOF
# IPsec VWAN NVA 1
cat <<EOF > $ipsec_file_b
config setup
charondebug="all"
uniqueids=yes
strictcrlpolicy=no
conn to-onprem
authby=secret
leftid=$endpoint_b_public
leftsubnet=0.0.0.0/0
right=$endpoint_c_public
rightsubnet=0.0.0.0/0
ike=aes256-sha2_256-modp1024!
esp=aes256-sha2_256!
keyingtries=0
ikelifetime=1h
lifetime=8h
dpddelay=30
dpdtimeout=120
dpdaction=restart
auto=start
mark=11
EOF
# IPsec onprem
cat <<EOF > $ipsec_file_c
config setup
charondebug="all"
uniqueids=yes
strictcrlpolicy=no
conn to-azure1
authby=secret
leftid=$endpoint_c_public
leftsubnet=0.0.0.0/0
right=$endpoint_a_public
rightsubnet=0.0.0.0/0
ike=aes256-sha2_256-modp1024!
esp=aes256-sha2_256!
keyingtries=0
ikelifetime=1h
lifetime=8h
dpddelay=30
dpdtimeout=120
dpdaction=restart
auto=start
mark=11
conn to-azure2
authby=secret
leftid=$endpoint_c_public
leftsubnet=0.0.0.0/0
right=$endpoint_b_public
rightsubnet=0.0.0.0/0
ike=aes256-sha2_256-modp1024!
esp=aes256-sha2_256!
keyingtries=0
ikelifetime=1h
lifetime=8h
dpddelay=30
dpdtimeout=120
dpdaction=restart
auto=start
mark=12
EOF
username=$(whoami)
# Deploy files to NVA1
scp $psk_file_a $pip1:/home/$username/ipsec.secrets
scp $ipsec_file_a $pip1:/home/$username/ipsec.conf
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo mv ./ipsec.* /etc/"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo systemctl restart ipsec"
# Deploy files to NVA2
scp $psk_file_b $pip2:/home/$username/ipsec.secrets
scp $ipsec_file_b $pip2:/home/$username/ipsec.conf
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo mv ./ipsec.* /etc/"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo systemctl restart ipsec"
# Deploy files to onprem
scp $psk_file_c $onprem_linuxnva_pip_ip:/home/$username/ipsec.secrets
scp $ipsec_file_c $onprem_linuxnva_pip_ip:/home/$username/ipsec.conf
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo mv ./ipsec.* /etc/"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo systemctl restart ipsec"
# Configure BGP with Bird
# =======================
bird_config_file_a=/tmp/bird.conf.a # NVA 1/2
bird_config_file_c=/tmp/bird.conf.c # onprem
# BGP config VWAN NVA (both instances)
cat <<EOF > $bird_config_file_a
log syslog all;
protocol device {
scan time 10;
}
protocol direct {
disabled;
}
protocol kernel {
preference 254;
learn;
merge paths on;
import filter {
if net ~ ${endpoint_c_private}/32 then accept;
else reject;
};
export filter {
if net ~ ${endpoint_c_private}/32 then reject;
if net ~ [ 0.0.0.0/0{0,1} ] then reject;
else accept;
};
}
protocol static {
import all;
route $vhub_space via $nva_default_gw;
}
filter DROP_LONG {
# Drop long prefixes
if ( net ~ [ 0.0.0.0/0{30,32} ] ) then { reject; }
else accept;
}
protocol bgp RS1 {
description "RS1";
multihop;
local as $nva_asn;
neighbor $rs_ip1 as $rs_asn;
import filter {accept;};
# export filter {accept;};
export filter DROP_LONG;
}
protocol bgp RS2 {
description "RS1";
multihop;
local as $nva_asn;
neighbor $rs_ip2 as $rs_asn;
import filter {accept;};
# export filter {accept;};
export filter DROP_LONG;
}
protocol bgp onprem {
description "BGP to Onprem";
multihop;
local as $nva_asn;
neighbor $endpoint_c_private as $onprem_linuxnva_asn;
import filter {accept;};
# export filter {accept;};
export filter DROP_LONG;
}
EOF
# Configure BGP with Bird (onprem)
cat <<EOF > $bird_config_file_c
log syslog all;
router id $endpoint_c_private;
protocol device {
scan time 10;
}
protocol direct {
disabled;
}
protocol kernel {
preference 254;
learn;
merge paths on;
import filter {
if net ~ ${nva_ip21}/32 then accept;
if net ~ ${nva_ip22}/32 then accept;
else reject;
};
export filter {
if net ~ ${nva_ip21}/32 then reject;
if net ~ ${nva_ip22}/32 then reject;
if net ~ [ 0.0.0.0/0{0,1} ] then reject;
else accept;
};
}
protocol static {
import all;
route $onprem_test_route via $onprem_linuxnva_default_gw;
route $onprem_vnet_prefix via $onprem_linuxnva_default_gw;
}
filter DROP_LONG {
# Drop long prefixes
if ( net ~ [ 0.0.0.0/0{30,32} ] ) then { reject; }
else accept;
}
protocol bgp NVA1 {
description "BGP to NVA1";
multihop;
local $endpoint_c_private as $onprem_linuxnva_asn;
neighbor $endpoint_a_private as $nva_asn;
import filter {accept;};
#export filter {accept;};
export filter DROP_LONG;
}
protocol bgp NVA2 {
description "BGP to NVA2";
multihop;
local $endpoint_c_private as $onprem_linuxnva_asn;
neighbor $endpoint_b_private as $nva_asn;
import filter {accept;};
#export filter {accept;};
export filter DROP_LONG;
}
EOF
# Deploy BGP config files
username=$(whoami)
# NVA instance 0
scp $bird_config_file_a "${pip1}:/home/${username}/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo mv /home/${username}/bird.conf /etc/bird/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip1 "sudo systemctl restart bird"
# NVA instance 1
scp $bird_config_file_a "${pip2}:/home/${username}/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo mv /home/${username}/bird.conf /etc/bird/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $pip2 "sudo systemctl restart bird"
# Onprem
scp $bird_config_file_c "${onprem_linuxnva_pip_ip}:/home/${username}/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo mv /home/${username}/bird.conf /etc/bird/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo systemctl restart bird"
# Some diagnostics
echo "IPSec status in onprem NVA:"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo ipsec status"
echo "BGP peerings in onprem NVA:"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $onprem_linuxnva_pip_ip "sudo birdc show protocols"
}
# Create a Linux NVA in the hub
# Most code from nva_vwan.azcli in this repo
function create_hub_nva() {
hub_id=$1
hub_name="hub${hub_id}"
subscription_id=$(az account show --query id -o tsv)
vendor="Jose_generic_test_nva"
version=latest
deploy_mode=rest # arm and rest supported at this time
vpn_endpoint=public # if "private", deploys ER to connect to onprem too. Use "public" if you dont have access to ER
nva_name="${hub_name}nva"
nva_asn="6510${hub_id}"
gnva_cloudinit="/tmp/nva-cloudinit.txt"
username=$(whoami)
public_ssh_key=$(more ~/.ssh/id_rsa.pub)
# Verify subscription ID
if [[ "$subscription_id" != "e7da9914-9b05-4891-893c-546cb7b0422e" ]]
then
"ERROR: $subscription_id is not a valid subscription for NVA $vendor"
exit
fi
# Get IDs
vwan_id=$(az network vwan show -n $vwan -g $rg --query name -o tsv)
hub_arm_id=$(az network vhub show -n $hub_name -g $rg --query id -o tsv)
# Create cloudinit file
cat <<EOF > $gnva_cloudinit
#cloud-config
users:
- default
- name: $username
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
ssh-authorized-keys:
- $public_ssh_key
packages:
- jq
runcmd:
- apt update
- UCF_FORCE_CONFOLD=1 DEBIAN_FRONTEND=noninteractive apt install -y bird strongswan
- sysctl -w net.ipv4.ip_forward=1
- sysctl -w net.ipv4.conf.all.accept_redirects = 0
- sysctl -w net.ipv4.conf.all.send_redirects = 0
EOF
cloudinit_string=$(cat $gnva_cloudinit | python3 -c 'import json, sys; print( json.dumps( sys.stdin.read() ) )')
# REST payload
json_payload='{
"properties": {
"nvaSku": {
"vendor": "'$vendor'",
"bundledScaleUnit": "2",
"marketPlaceVersion": "'$version'"
},
"virtualHub": {
"id": "'$hub_arm_id'"
},
"virtualApplianceAsn": '$nva_asn',
"cloudInitConfiguration": '$cloudinit_string'
},
"location": "'$location'",
"tags": {
"tagexample1": "tagvalue1"
}
}'
uri="/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/NetworkVirtualAppliances/${nva_name}?api-version=2021-02-01"
echo "Sending REST request to $uri..."
az rest --method PUT --uri $uri --body "$json_payload" -o none
nva_id=$(az network virtual-appliance show -n $nva_name -g $rg --query id -o tsv)
wait_until_finished $nva_id
# Diagnostics
echo "Interfaces in the NVA instances..."
nva_pip1=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[1].publicIpAddress' -o tsv) && echo $pip1
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no "$nva_pip1" "ip a"
nva_pip2=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[3].publicIpAddress' -o tsv) && echo $pip2
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no "$nva_pip2" "ip a"
}
# Configure BGP in a hub NVA
function configure_hub_nva_bgp() {
hub_id=$1
hub_name="hub${hub_id}"
nva_name="${hub_name}nva"
echo "Getting public IP addresses of NVA $nva_name..."
nva_pip1=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[1].publicIpAddress' -o tsv) && echo $pip1
nva_pip2=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[3].publicIpAddress' -o tsv) && echo $pip2
# Get RS IPs
echo "Finding information about Route Service in $hub_name..."
rs_ip1=$(az network vhub show -n $hub_name -g $rg --query 'virtualRouterIps[0]' -o tsv) && echo $rs_ip1
rs_ip2=$(az network vhub show -n $hub_name -g $rg --query 'virtualRouterIps[1]' -o tsv) && echo $rs_ip2
rs_asn=$(az network vhub show -n $hub_name -g $rg --query 'virtualRouterAsn' -o tsv) && echo $rs_asn
# Get NVA private IPs
echo "Finding internal private IPs of NVA $nva_name..."
nva_ip1=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[0].privateIpAddress' -o tsv) && echo $nva_ip1
nva_ip2=$(az network virtual-appliance show -n $nva_name -g $rg --query 'virtualApplianceNics[2].privateIpAddress' -o tsv) && echo $nva_ip2
# Find out the gateway for the private ip
nva_internal_mask=$(ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$nva_pip1" "ifconfig eth1 | grep netmask | sed -n 's/.* netmask \([^ ]*\).*/\1/p'") && echo "Network mask $nva_internal_mask"
if [[ "${nva_internal_mask:0:11}" != "255.255.255" ]]
then
echo "It looks like $nva_internal_mask is not a correct network mask, defaulting to 255.255.255.240..."
nva_internal_mask="255.255.255.240"
fi
nva_default_gw=$(default_gw "$nva_ip1" "$nva_internal_mask") && echo "Internal gateway is $nva_default_gw"
# Adding static IP routes
if [[ $hub_id == "1" ]]
then
vhub_space="$vwan_hub1_prefix"
else
vhub_space="$vwan_hub2_prefix"
fi
echo "Adding static routes to NVA instances for the hub prefix $vhub_space..."
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$nva_pip1" "sudo ip route add $vhub_space via $nva_default_gw"
ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes "$nva_pip2" "sudo ip route add $vhub_space via $nva_default_gw"
# Create BGP file
bird_config_file=/tmp/bird.conf
echo "Generating BIRD config file in $bird_config_file..."
cat <<EOF > $bird_config_file
log syslog all;
#router id $nva_ip1;
protocol device {
scan time 10;
}
protocol direct {
disabled;
}
protocol kernel {
preference 254;
learn;
merge paths on;
export all;
#disabled;
}
protocol static {
import all;
# route $vhub_space via $linuxnva_default_gw;
}
filter DROP_LONG {
# Drop long prefixes
if ( net ~ [ 0.0.0.0/0{30,32} ] ) then { reject; }
else accept;
}
protocol bgp RS1 {
description "RS1";
multihop;
#local $nva_ip1 as $nva_asn;
local as $nva_asn;
neighbor $rs_ip1 as $rs_asn;
import filter {accept;};
# export filter {accept;};
export filter DROP_LONG;
}
protocol bgp RS2 {
description "RS1";
multihop;
#local $nva_ip1 as $nva_asn;
local as $nva_asn;
neighbor $rs_ip2 as $rs_asn;
import filter {accept;};
# export filter {accept;};
export filter DROP_LONG;
}
EOF
# Deploy file
echo "Copying BIRD configuration files to both NVA instances..."
username=$(whoami)
scp $bird_config_file "${nva_pip1}:/home/${username}/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip1 "sudo mv /home/${username}/bird.conf /etc/bird/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip1 "sudo systemctl restart bird"
scp $bird_config_file "${nva_pip2}:/home/${username}/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip2 "sudo mv /home/${username}/bird.conf /etc/bird/bird.conf"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip2 "sudo systemctl restart bird"
# Diagnostics
echo "Running some quick BGP diagnostics on both NVA instances..."
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip1 "sudo birdc show prot"
ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no $nva_pip2 "sudo birdc show prot"
}
#############
# Start #
#############
# Start: create RG
az group create -n $rg -l $location1 -o none
# vwan and hubs
echo "Creating Virtual WAN and Virtual Hubs..."
az network vwan create -n $vwan -g $rg -l $location1 --branch-to-branch-traffic true --type Standard -o none
az network vhub create -n hub1 -g $rg --vwan $vwan -l $location1 --address-prefix $vwan_hub1_prefix -o none
if [[ "$no_of_hubs" == "2" ]]
then
az network vhub create -n hub2 -g $rg --vwan $vwan -l $location2 --address-prefix $vwan_hub2_prefix -o none
fi
# Create RT for vnets
echo "Creating custom RT for VNets..."
az network vhub route-table create -n hub1Vnet --vhub-name hub1 -g $rg --labels vnet -o none
if [[ "$no_of_hubs" == "2" ]]
then
az network vhub route-table create -n hub2Vnet --vhub-name hub2 -g $rg --labels vnet -o none
fi
# Add nohub1/nohub2 labels to default RTs
# az network vhub route-table update -n defaultRouteTable --vhub-name hub1 -g $rg --labels default nohub2
# az network vhub route-table update -n defaultRouteTable --vhub-name hub2 -g $rg --labels default nohub1
# Retrieve IDs of RTs. We will need this when creating the connections
hub1_vnet_rt_id=$(az network vhub route-table show --vhub-name hub1 -g $rg -n hub1Vnet --query id -o tsv)
hub1_default_rt_id=$(az network vhub route-table show --vhub-name hub1 -g $rg -n defaultRouteTable --query id -o tsv)
hub1_none_rt_id=$(az network vhub route-table show --vhub-name hub1 -g $rg -n noneRouteTable --query id -o tsv)
if [[ "$no_of_hubs" == "2" ]]
then
hub2_default_rt_id=$(az network vhub route-table show --vhub-name hub2 -g $rg -n defaultRouteTable --query id -o tsv)
hub2_vnet_rt_id=$(az network vhub route-table show --vhub-name hub2 -g $rg -n hub2Vnet --query id -o tsv)
hub2_none_rt_id=$(az network vhub route-table show --vhub-name hub2 -g $rg -n noneRouteTable --query id -o tsv)
fi
# Define which association/propagations we will use
if [[ "$vnet_ass" == "vnet" ]]
then
hub1_vnet_ass_rt_id="$hub1_vnet_rt_id"
hub2_vnet_ass_rt_id="$hub2_vnet_rt_id"
else
hub1_vnet_ass_rt_id="$hub1_default_rt_id"
hub2_vnet_ass_rt_id="$hub2_default_rt_id"
fi
if [[ "$vnet_prop" == "vnet" ]]
then
hub1_vnet_prop_rt_id="$hub1_vnet_rt_id"
hub2_vnet_prop_rt_id="$hub2_vnet_rt_id"
vnet_prop_label='vnet'
elif [[ "$vnet_prop" == "none" ]]
then
hub1_vnet_prop_rt_id="$hub1_none_rt_id"
hub2_vnet_prop_rt_id="$hub2_none_rt_id"
vnet_prop_label='none'
else
hub1_vnet_prop_rt_id="$hub1_default_rt_id"
hub2_vnet_prop_rt_id="$hub2_default_rt_id"
vnet_prop_label="default"
fi
# Create VPN gateways (not using --no-wait to avoid race conditions due to parallelism)
if [[ "$deploy_vpngw" == "yes" ]]
then
echo "Creating VPN Gateways..."
az network vpn-gateway create -n hubvpn1 -g $rg -l $location1 --vhub hub1 --asn 65515 -o none
if [[ "$no_of_hubs" == "2" ]]
then
az network vpn-gateway create -n hubvpn2 -g $rg -l $location2 --vhub hub2 --asn 65515 -o none
fi
fi
# Create NSGs to be used by VMs
echo "Creating NSGs for Virtual Machines..."
nsg1_name=vm-nsg-$location1
az network nsg create -n $nsg1_name -g $rg -l $location1 -o none
az network nsg rule create --nsg-name $nsg1_name -g $rg -n Allow_Inbound_SSH --priority 1000 \
--access Allow --protocol Tcp --source-address-prefixes '*' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges 22 -o none
az network nsg rule create --nsg-name $nsg1_name -g $rg -n Allow_Inbound_HTTP --priority 1010 --direction Inbound \
--access Allow --protocol Tcp --source-address-prefixes '10.0.0.0/8' '172.16.0.0/12' '20.0.0.0/6' '192.168.0.0/16' \
--destination-address-prefixes '*' --destination-port-ranges 9 80 443 -o none
# az network nsg rule create --nsg-name $nsg1_name -g $rg -n Allow_Inbound_HTTP_Return --priority 1015 --direction Inbound \
# --access Allow --protocol Tcp --source-address-prefixes '*' \
# --destination-address-prefixes '10.0.0.0/8' '172.16.0.0/12' '20.0.0.0/6' '192.168.0.0/16' --destination-port-ranges 80 443
az network nsg rule create --nsg-name $nsg1_name -g $rg -n Allow_Inbound_IPsec --priority 1020 \
--access Allow --protocol Udp --source-address-prefixes 'Internet' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges 500 4500 -o none
az network nsg rule create --nsg-name $nsg1_name -g $rg -n Allow_Inbound_NTP --priority 1030 \
--access Allow --protocol Udp --source-address-prefixes '10.0.0.0/8' '172.16.0.0/12' '20.0.0.0/6' '192.168.0.0/16' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges 123 -o none
az network nsg rule create --nsg-name $nsg1_name -g $rg -n Allow_Inbound_Icmp --priority 1040 \
--access Allow --protocol Icmp --source-address-prefixes '*' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges '*' -o none
az network nsg rule create --nsg-name $nsg1_name -g $rg -n Allow_Outbound_All --priority 1000 \
--access Allow --protocol '*' --source-address-prefixes '*' --direction Outbound \
--destination-address-prefixes '*' --destination-port-ranges '*' -o none
# Configure NSG flow logs
storage_account1_name=vwan$RANDOM$location1
az storage account create -n $storage_account1_name -g $rg --sku Standard_LRS --kind StorageV2 -l $location1 -o none
az network watcher flow-log create -l $location1 -n flowlog-$location1 -g $rg \
--nsg $nsg1_name --storage-account $storage_account1_name --log-version 2 --retention 7 -o none
# Hub2
if [[ "$no_of_hubs" == "2" ]]
then
nsg2_name=vm-nsg-$location2
if [[ "$location1" != "$location2" ]]
then
az network nsg create -n $nsg2_name -g $rg -l $location2 -o none
az network nsg rule create --nsg-name $nsg2_name -g $rg -n Allow_Inbound_SSH --priority 1000 \
--access Allow --protocol Tcp --source-address-prefixes '*' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges 22 -o none
az network nsg rule create --nsg-name $nsg2_name -g $rg -n Allow_Inbound_HTTP --priority 1010 \
--access Allow --protocol Tcp --source-address-prefixes '10.0.0.0/8' '172.16.0.0/12' '20.0.0.0/6' '192.168.0.0/16' \
--destination-address-prefixes '*' --destination-port-ranges 9 80 443 -o none
az network nsg rule create --nsg-name $nsg2_name -g $rg -n Allow_Inbound_IPsec --priority 1020 \
--access Allow --protocol Udp --source-address-prefixes 'Internet' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges 500 4500 -o none
az network nsg rule create --nsg-name $nsg2_name -g $rg -n Allow_Inbound_NTP --priority 1030 \
--access Allow --protocol Udp --source-address-prefixes '10.0.0.0/8' '172.16.0.0/12' '20.0.0.0/6' '192.168.0.0/16' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges 123 -o none
az network nsg rule create --nsg-name $nsg2_name -g $rg -n Allow_Inbound_Icmp --priority 1040 \
--access Allow --protocol Icmp --source-address-prefixes '*' --direction Inbound \
--destination-address-prefixes '*' --destination-port-ranges '*' -o none
az network nsg rule create --nsg-name $nsg2_name -g $rg -n Allow_Outbound_All --priority 1000 \
--access Allow --protocol '*' --source-address-prefixes '*' --direction Outbound \
--destination-address-prefixes '*' --destination-port-ranges '*' -o none
fi
if [[ "$location1" != "$location2" ]]
then
storage_account2_name=vwan$RANDOM$location2
else
storage_account2_name=$storage_account1_name
fi
az storage account create -n $storage_account2_name -g $rg --sku Standard_LRS --kind StorageV2 -l $location2 -o none
az network watcher flow-log create -l $location2 -n flowlog-$location2 -g $rg \
--nsg $nsg2_name --storage-account $storage_account2_name --log-version 2 --retention 7 -o none
fi
# Create CSRs only if there are VPN Gateways
# Create CSR to simulate branch1
if [[ "$deploy_vpngw" == "yes" ]]
then
# You might have to accept the CSR marketplace terms to deploy the image
echo "Accepting the Cisco marketplace image for Cisco CSR..."
az vm image terms accept -p $publisher -f $offer --plan $sku -o none
echo "Creating NVAs..."
az vm create -n branch1-nva -g $rg -l $location1 --image ${publisher}:${offer}:${sku}:${version} \
--generate-ssh-keys --admin-username $username --nsg $nsg1_name --size $nva_size \
--public-ip-address branch1-pip --public-ip-address-allocation static --public-ip-sku Standard --private-ip-address $branch1_bgp_ip \
--vnet-name branch1 --vnet-address-prefix $branch1_prefix --subnet nva --subnet-address-prefix $branch1_subnet -o none
branch1_ip=$(az network public-ip show -n branch1-pip -g $rg --query ipAddress -o tsv)
# az network vpn-site create -n branch1 -g $rg -l $location1 --virtual-wan $vwan \
# --asn $branch1_asn --bgp-peering-address $branch1_bgp_ip --ip-address $branch1_ip --address-prefixes ${branch1_ip}/32 --device-vendor cisco --device-model csr --link-speed 100 -o none
# az network vpn-gateway connection create -n branch1 --gateway-name hubvpn1 -g $rg --remote-vpn-site branch1 \
# --enable-bgp true --protocol-type IKEv2 --shared-key "$password" --connection-bandwidth 100 --routing-weight 10 \
# --associated-route-table $hub1_default_rt_id --propagated-route-tables $hub1_default_rt_id --labels default --internet-security true -o none
az network vpn-site create -n branch1 -g $rg -l $location1 --virtual-wan $vwan --address-prefixes ${branch1_ip}/32 --device-vendor cisco --device-model csr --link-speed 100 --ip-address $branch1_ip \
--asn $branch1_asn --bgp-peering-address $branch1_bgp_ip --with-link -o none
# az network vpn-site link add -n branch1 -g $rg --ip-address $branch1_ip --asn $branch1_asn --bgp-peering-address $branch1_bgp_ip --site-name branch1 --link-speed-in-mbps 100 --link-provider-name msft -o none
branch1_link_id=$(az network vpn-site link list --site-name branch1 -g $rg --query '[0].id' -o tsv)
az network vpn-gateway connection create -n branch1 --gateway-name hubvpn1 -g $rg --remote-vpn-site branch1 --with-link true --vpn-site-link $branch1_link_id \
--enable-bgp true --protocol-type IKEv2 --shared-key "$password" --connection-bandwidth 100 --routing-weight 10 \
--associated-route-table $hub1_default_rt_id --propagated-route-tables $hub1_default_rt_id --labels default --internet-security true -o none
if [[ "$no_of_hubs" == "2" ]]
then
# Create CSR to simulate branch2
az vm create -n branch2-nva -g $rg -l $location2 --image ${publisher}:${offer}:${sku}:${version} \
--generate-ssh-keys --admin-username $username --nsg $nsg2_name --size $nva_size \
--public-ip-address branch2-pip --public-ip-address-allocation static --public-ip-sku Standard --private-ip-address $branch2_bgp_ip \
--vnet-name branch2 --vnet-address-prefix $branch2_prefix --subnet nva --subnet-address-prefix $branch2_subnet -o none
branch2_ip=$(az network public-ip show -n branch2-pip -g $rg --query ipAddress -o tsv)
az network vpn-site create -n branch2 -g $rg -l $location2 --virtual-wan $vwan --with-link --device-vendor cisco --device-model csr --link-speed 100 \
--asn $branch2_asn --bgp-peering-address $branch2_bgp_ip --ip-address $branch2_ip --address-prefixes ${branch2_ip}/32 -o none
branch2_link_id=$(az network vpn-site link list --site-name branch1 -g $rg --query '[0].id' -o tsv)
az network vpn-gateway connection create -n branch2 --gateway-name hubvpn2 -g $rg --remote-vpn-site branch2 --with-link true --vpn-site-link $branch2_link_id \
--enable-bgp true --protocol-type IKEv2 --shared-key "$password" --connection-bandwidth 100 --routing-weight 10 \
--associated-route-table $hub2_default_rt_id --propagated-route-tables $hub2_default_rt_id --labels default --internet-security true -o none
fi
fi
# Configure branches (CSRs)
# Get parameters for VPN GW in hub1
if [[ "$deploy_vpngw" == "yes" ]]
then
vpngw1_config=$(az network vpn-gateway show -n hubvpn1 -g $rg)
site=branch1
vpngw1_gw0_pip=$(echo $vpngw1_config | jq -r '.bgpSettings.bgpPeeringAddresses[0].tunnelIpAddresses[0]')
vpngw1_gw1_pip=$(echo $vpngw1_config | jq -r '.bgpSettings.bgpPeeringAddresses[1].tunnelIpAddresses[0]')
vpngw1_gw0_bgp_ip=$(echo $vpngw1_config | jq -r '.bgpSettings.bgpPeeringAddresses[0].defaultBgpIpAddresses[0]')
vpngw1_gw1_bgp_ip=$(echo $vpngw1_config | jq -r '.bgpSettings.bgpPeeringAddresses[1].defaultBgpIpAddresses[0]')
vpngw1_bgp_asn=$(echo $vpngw1_config | jq -r '.bgpSettings.asn') # This is today always 65515
echo "Extracted info for hubvpn1: Gateway0 $vpngw1_gw0_pip, $vpngw1_gw0_bgp_ip. Gateway1 $vpngw1_gw1_pip, $vpngw1_gw0_bgp_ip. ASN $vpngw1_bgp_asn"
if [[ "$no_of_hubs" == "2" ]]
then
# Get parameters for VPN GW in hub2
vpngw2_config=$(az network vpn-gateway show -n hubvpn2 -g $rg)
site=branch2
vpngw2_gw0_pip=$(echo $vpngw2_config | jq -r '.bgpSettings.bgpPeeringAddresses[0].tunnelIpAddresses[0]')
vpngw2_gw1_pip=$(echo $vpngw2_config | jq -r '.bgpSettings.bgpPeeringAddresses[1].tunnelIpAddresses[0]')
vpngw2_gw0_bgp_ip=$(echo $vpngw2_config | jq -r '.bgpSettings.bgpPeeringAddresses[0].defaultBgpIpAddresses[0]')
vpngw2_gw1_bgp_ip=$(echo $vpngw2_config | jq -r '.bgpSettings.bgpPeeringAddresses[1].defaultBgpIpAddresses[0]')
vpngw2_bgp_asn=$(echo $vpngw2_config | jq -r '.bgpSettings.asn') # This is today always 65515
echo "Extracted info for hubvpn2: Gateway0 $vpngw2_gw0_pip, $vpngw2_gw0_bgp_ip. Gateway1 $vpngw2_gw1_pip, $vpngw2_gw0_bgp_ip. ASN $vpngw2_bgp_asn"
fi
# Wait until CSR in branch1 takes SSH connections
wait_interval_csr=30
echo "Waiting until CSR in branch${branch_id} is reachable..."