summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_interfaces_openvpn.py
blob: c9376b0326305924c5f922e84c78ec844f70171a (plain)
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
#!/usr/bin/env python3
#
# Copyright (C) 2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import unittest

from glob import glob
from ipaddress import IPv4Network
from netifaces import interfaces

from vyos.configsession import ConfigSession
from vyos.configsession import ConfigSessionError
from vyos.util import cmd
from vyos.util import process_named_running
from vyos.util import read_file
from vyos.template import address_from_cidr
from vyos.template import dec_ip
from vyos.template import inc_ip
from vyos.template import last_host_address
from vyos.template import netmask_from_cidr

PROCESS_NAME = 'openvpn'

base_path = ['interfaces', 'openvpn']
ca_cert  = '/config/auth/ovpn_test_ca.pem'
ssl_cert = '/config/auth/ovpn_test_server.pem'
ssl_key  = '/config/auth/ovpn_test_server.key'
dh_pem   = '/config/auth/ovpn_test_dh.pem'
s2s_key  = '/config/auth/ovpn_test_site2site.key'
auth_key = '/config/auth/ovpn_test_tls_auth.key'

remote_port = '1194'
protocol = 'udp'
path = []
interface = ''
remote_host = ''
vrf_name = 'orange'
dummy_if = 'dum1301'

def get_vrf(interface):
    for upper in glob(f'/sys/class/net/{interface}/upper*'):
        # an upper interface could be named: upper_bond0.1000.1100, thus
        # we need top drop the upper_ prefix
        tmp = os.path.basename(upper)
        tmp = tmp.replace('upper_', '')
        return tmp

class TestInterfacesOpenVPN(unittest.TestCase):
    def setUp(self):
        self.session = ConfigSession(os.getpid())
        self.session.set(['interfaces', 'dummy', dummy_if, 'address', '192.0.2.1/32'])
        self.session.set(['vrf', 'name', vrf_name, 'table', '12345'])

    def tearDown(self):
        self.session.delete(base_path)
        self.session.delete(['interfaces', 'dummy', dummy_if])
        self.session.delete(['vrf'])
        self.session.commit()
        del self.session

    def test_openvpn_client_verify(self):
        # Create OpenVPN client interface and test verify() steps.
        interface = 'vtun2000'
        path = base_path + [interface]
        self.session.set(path + ['mode', 'client'])
        self.session.set(path + ['encryption', 'ncp-ciphers', 'aes192gcm'])

        # check validate() - cannot specify local-port in client mode
        self.session.set(path + ['local-port', '5000'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['local-port'])

        # check validate() - cannot specify local-host in client mode
        self.session.set(path + ['local-host', '127.0.0.1'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['local-host'])

        # check validate() - cannot specify protocol tcp-passive in client mode
        self.session.set(path + ['protocol', 'tcp-passive'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['protocol'])

        # check validate() - remote-host must be set in client mode
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['remote-host', '192.0.9.9'])

        # check validate() - cannot specify "tls dh-file" in client mode
        self.session.set(path + ['tls', 'dh-file', dh_pem])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['tls'])

        # check validate() - must specify one of "shared-secret-key-file" and "tls"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['shared-secret-key-file', s2s_key])

        # check validate() - must specify one of "shared-secret-key-file" and "tls"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['shared-secret-key-file', s2s_key])

        self.session.set(path + ['tls', 'ca-cert-file', ca_cert])
        self.session.set(path + ['tls', 'cert-file', ssl_cert])
        self.session.set(path + ['tls', 'key-file', ssl_key])

        # check validate() - can not have auth username without a password
        self.session.set(path + ['authentication', 'username', 'vyos'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['authentication', 'password', 'vyos'])

        # client commit must pass
        self.session.commit()

        self.assertTrue(process_named_running(PROCESS_NAME))
        self.assertIn(interface, interfaces())


    def test_openvpn_client_interfaces(self):
        # Create OpenVPN client interfaces connecting to different
        # server IP addresses. Validate configuration afterwards.
        num_range = range(10, 15)
        for ii in num_range:
            interface = f'vtun{ii}'
            remote_host = f'192.0.2.{ii}'
            path = base_path + [interface]
            auth_hash = 'sha1'

            self.session.set(path + ['device-type', 'tun'])
            self.session.set(path + ['encryption', 'cipher', 'aes256'])
            self.session.set(path + ['hash', auth_hash])
            self.session.set(path + ['mode', 'client'])
            self.session.set(path + ['persistent-tunnel'])
            self.session.set(path + ['protocol', protocol])
            self.session.set(path + ['remote-host', remote_host])
            self.session.set(path + ['remote-port', remote_port])
            self.session.set(path + ['tls', 'ca-cert-file', ca_cert])
            self.session.set(path + ['tls', 'cert-file', ssl_cert])
            self.session.set(path + ['tls', 'key-file', ssl_key])
            self.session.set(path + ['vrf', vrf_name])
            self.session.set(path + ['authentication', 'username', interface+'user'])
            self.session.set(path + ['authentication', 'password', interface+'secretpw'])

        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            remote_host = f'192.0.2.{ii}'
            config_file = f'/run/openvpn/{interface}.conf'
            pw_file = f'/run/openvpn/{interface}.pw'
            config = read_file(config_file)

            self.assertIn(f'dev {interface}', config)
            self.assertIn(f'dev-type tun', config)
            self.assertIn(f'persist-key', config)
            self.assertIn(f'proto {protocol}', config)
            self.assertIn(f'rport {remote_port}', config)
            self.assertIn(f'remote {remote_host}', config)
            self.assertIn(f'persist-tun', config)
            self.assertIn(f'auth {auth_hash}', config)
            self.assertIn(f'cipher aes-256-cbc', config)

            # TLS options
            self.assertIn(f'ca {ca_cert}', config)
            self.assertIn(f'cert {ssl_cert}', config)
            self.assertIn(f'key {ssl_key}', config)

            self.assertTrue(process_named_running(PROCESS_NAME))
            self.assertEqual(get_vrf(interface), vrf_name)
            self.assertIn(interface, interfaces())

            pw = cmd(f'sudo cat {pw_file}')
            self.assertIn(f'{interface}user', pw)
            self.assertIn(f'{interface}secretpw', pw)

        # check that no interface remained after deleting them
        self.session.delete(base_path)
        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            self.assertNotIn(interface, interfaces())

    def test_openvpn_server_verify(self):
        # Create one OpenVPN server interface and check required verify() stages
        interface = 'vtun5000'
        path = base_path + [interface]

        # check validate() - must speciy operating mode
        self.session.set(path)
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['mode', 'server'])

        # check validate() - cannot specify protocol tcp-active in server mode
        self.session.set(path + ['protocol', 'tcp-active'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['protocol'])

        # check validate() - cannot specify local-port in client mode
        self.session.set(path + ['remote-port', '5000'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['remote-port'])

        # check validate() - cannot specify local-host in client mode
        self.session.set(path + ['remote-host', '127.0.0.1'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['remote-host'])

        # check validate() - must specify "tls dh-file" when not using EC keys
        # in server mode
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['tls', 'dh-file', dh_pem])

        # check validate() - must specify "server subnet" or add interface to
        # bridge in server mode
        with self.assertRaises(ConfigSessionError):
            self.session.commit()

        # check validate() - server client-ip-pool is too large
        # [100.64.0.4 -> 100.127.255.251 = 4194295], maximum is 65536 addresses.
        self.session.set(path + ['server', 'subnet', '100.64.0.0/10'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()

        # check validate() - cannot specify more than 1 IPv4 and 1 IPv6 server subnet
        self.session.set(path + ['server', 'subnet', '100.64.0.0/20'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['server', 'subnet', '100.64.0.0/10'])

        # check validate() - must specify "tls ca-cert-file"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['tls', 'ca-cert-file', ca_cert])

        # check validate() - must specify "tls cert-file"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['tls', 'cert-file', ssl_cert])

        # check validate() - must specify "tls key-file"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['tls', 'key-file', ssl_key])

        # check validate() - cannot specify "tls role" in client-server mode'
        self.session.set(path + ['tls', 'role', 'active'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()

        # check validate() - cannot specify "tls role" in client-server mode'
        self.session.set(path + ['tls', 'auth-file', auth_key])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()

        # check validate() - cannot specify "tcp-passive" when "tls role" is "active"
        self.session.set(path + ['protocol', 'tcp-passive'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['protocol'])

        # check validate() - cannot specify "tls dh-file" when "tls role" is "active"
        self.session.set(path + ['tls', 'dh-file', dh_pem])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['tls', 'dh-file'])

        # Now test the other path with tls role passive
        self.session.set(path + ['tls', 'role', 'passive'])
        # check validate() - cannot specify "tcp-active" when "tls role" is "passive"
        self.session.set(path + ['protocol', 'tcp-active'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['protocol'])


        # check validate() - must specify "tls dh-file" when "tls role" is "passive"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['tls', 'dh-file', dh_pem])

        self.session.commit()

        self.assertTrue(process_named_running(PROCESS_NAME))
        self.assertIn(interface, interfaces())

    def test_openvpn_server_subnet_topology(self):
        # Create OpenVPN server interfaces using different client subnets.
        # Validate configuration afterwards.

        auth_hash = 'sha256'
        num_range = range(20, 25)
        port = ''
        client1_routes = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']
        for ii in num_range:
            interface = f'vtun{ii}'
            subnet = f'192.0.{ii}.0/24'
            client_ip = inc_ip(subnet, '5')
            path = base_path + [interface]
            port = str(2000 + ii)

            self.session.set(path + ['device-type', 'tun'])
            self.session.set(path + ['encryption', 'cipher', 'aes192'])
            self.session.set(path + ['hash', auth_hash])
            self.session.set(path + ['mode', 'server'])
            self.session.set(path + ['local-port', port])
            self.session.set(path + ['server', 'subnet', subnet])
            self.session.set(path + ['server', 'topology', 'subnet'])
            self.session.set(path + ['keep-alive', 'failure-count', '5'])
            self.session.set(path + ['keep-alive', 'interval', '5'])

            # clients
            self.session.set(path + ['server', 'client', 'client1', 'ip', client_ip])
            for route in client1_routes:
                self.session.set(path + ['server', 'client', 'client1', 'subnet', route])

            self.session.set(path + ['replace-default-route'])
            self.session.set(path + ['tls', 'ca-cert-file', ca_cert])
            self.session.set(path + ['tls', 'cert-file', ssl_cert])
            self.session.set(path + ['tls', 'key-file', ssl_key])
            self.session.set(path + ['tls', 'dh-file', dh_pem])
            self.session.set(path + ['vrf', vrf_name])

        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            subnet = f'192.0.{ii}.0/24'

            start_addr = inc_ip(subnet, '2')
            stop_addr = last_host_address(subnet)

            client_ip = inc_ip(subnet, '5')
            client_netmask = netmask_from_cidr(subnet)

            port = str(2000 + ii)

            config_file = f'/run/openvpn/{interface}.conf'
            client_config_file = f'/run/openvpn/ccd/{interface}/client1'
            config = read_file(config_file)

            self.assertIn(f'dev {interface}', config)
            self.assertIn(f'dev-type tun', config)
            self.assertIn(f'persist-key', config)
            self.assertIn(f'proto udp', config) # default protocol
            self.assertIn(f'auth {auth_hash}', config)
            self.assertIn(f'cipher aes-192-cbc', config)
            self.assertIn(f'topology subnet', config)
            self.assertIn(f'lport {port}', config)
            self.assertIn(f'push "redirect-gateway def1"', config)
            self.assertIn(f'keepalive 5 25', config)

            # TLS options
            self.assertIn(f'ca {ca_cert}', config)
            self.assertIn(f'cert {ssl_cert}', config)
            self.assertIn(f'key {ssl_key}', config)
            self.assertIn(f'dh {dh_pem}', config)

            # IP pool configuration
            netmask = IPv4Network(subnet).netmask
            network = IPv4Network(subnet).network_address
            self.assertIn(f'server {network} {netmask} nopool', config)

            # Verify client
            client_config = read_file(client_config_file)

            self.assertIn(f'ifconfig-push {client_ip} {client_netmask}', client_config)
            for route in client1_routes:
                self.assertIn('iroute {} {}'.format(address_from_cidr(route), netmask_from_cidr(route)), client_config)

            self.assertTrue(process_named_running(PROCESS_NAME))
            self.assertEqual(get_vrf(interface), vrf_name)
            self.assertIn(interface, interfaces())

        # check that no interface remained after deleting them
        self.session.delete(base_path)
        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            self.assertNotIn(interface, interfaces())

    def test_openvpn_server_net30_topology(self):
        # Create OpenVPN server interfaces (net30) using different client
        # subnets. Validate configuration afterwards.
        auth_hash = 'sha256'
        num_range = range(20, 25)
        port = ''
        for ii in num_range:
            interface = f'vtun{ii}'
            subnet = f'192.0.{ii}.0/24'
            path = base_path + [interface]
            port = str(2000 + ii)

            self.session.set(path + ['device-type', 'tun'])
            self.session.set(path + ['encryption', 'cipher', 'aes192'])
            self.session.set(path + ['hash', auth_hash])
            self.session.set(path + ['mode', 'server'])
            self.session.set(path + ['local-port', port])
            self.session.set(path + ['server', 'subnet', subnet])
            self.session.set(path + ['server', 'topology', 'net30'])
            self.session.set(path + ['replace-default-route'])
            self.session.set(path + ['keep-alive', 'failure-count', '10'])
            self.session.set(path + ['keep-alive', 'interval', '5'])
            self.session.set(path + ['tls', 'ca-cert-file', ca_cert])
            self.session.set(path + ['tls', 'cert-file', ssl_cert])
            self.session.set(path + ['tls', 'key-file', ssl_key])
            self.session.set(path + ['tls', 'dh-file', dh_pem])
            self.session.set(path + ['vrf', vrf_name])

        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            subnet = f'192.0.{ii}.0/24'
            start_addr = inc_ip(subnet, '4')
            stop_addr = dec_ip(last_host_address(subnet), '1')
            port = str(2000 + ii)

            config_file = f'/run/openvpn/{interface}.conf'
            config = read_file(config_file)

            self.assertIn(f'dev {interface}', config)
            self.assertIn(f'dev-type tun', config)
            self.assertIn(f'persist-key', config)
            self.assertIn(f'proto udp', config) # default protocol
            self.assertIn(f'auth {auth_hash}', config)
            self.assertIn(f'cipher aes-192-cbc', config)
            self.assertIn(f'topology net30', config)
            self.assertIn(f'lport {port}', config)
            self.assertIn(f'push "redirect-gateway def1"', config)
            self.assertIn(f'keepalive 5 50', config)

            # TLS options
            self.assertIn(f'ca {ca_cert}', config)
            self.assertIn(f'cert {ssl_cert}', config)
            self.assertIn(f'key {ssl_key}', config)
            self.assertIn(f'dh {dh_pem}', config)

            # IP pool configuration
            netmask = IPv4Network(subnet).netmask
            network = IPv4Network(subnet).network_address
            self.assertIn(f'server {network} {netmask} nopool', config)
            self.assertIn(f'ifconfig-pool {start_addr} {stop_addr}', config)

            self.assertTrue(process_named_running(PROCESS_NAME))
            self.assertEqual(get_vrf(interface), vrf_name)
            self.assertIn(interface, interfaces())

        # check that no interface remained after deleting them
        self.session.delete(base_path)
        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            self.assertNotIn(interface, interfaces())

    def test_openvpn_site2site_verify(self):
        # Create one OpenVPN site2site interface and check required
        # verify() stages

        interface = 'vtun5000'
        path = base_path + [interface]

        self.session.set(path + ['mode', 'site-to-site'])

        # check validate() - encryption ncp-ciphers cannot be specified in site-to-site mode
        self.session.set(path + ['encryption', 'ncp-ciphers', 'aes192gcm'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['encryption'])

        # check validate() - must specify "local-address" or add interface to bridge
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['local-address', '10.0.0.1'])
        self.session.set(path + ['local-address', '2001:db8:1::1'])

        # check validate() - cannot specify more than 1 IPv4 local-address
        self.session.set(path + ['local-address', '10.0.0.2'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['local-address', '10.0.0.2'])

        # check validate() - cannot specify more than 1 IPv6 local-address
        self.session.set(path + ['local-address', '2001:db8:1::2'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['local-address', '2001:db8:1::2'])

        # check validate() - IPv4 "local-address" requires IPv4 "remote-address"
        # or IPv4 "local-address subnet"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['remote-address', '192.168.0.1'])
        self.session.set(path + ['remote-address', '2001:db8:ffff::1'])

        # check validate() - Cannot specify more than 1 IPv4 "remote-address"
        self.session.set(path + ['remote-address', '192.168.0.2'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['remote-address', '192.168.0.2'])

        # check validate() - Cannot specify more than 1 IPv6 "remote-address"
        self.session.set(path + ['remote-address', '2001:db8:ffff::2'])
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.delete(path + ['remote-address', '2001:db8:ffff::2'])

        # check validate() - Must specify one of "shared-secret-key-file" and "tls"
        with self.assertRaises(ConfigSessionError):
            self.session.commit()
        self.session.set(path + ['shared-secret-key-file', s2s_key])

        self.session.commit()

    def test_openvpn_site2site_interfaces_tun(self):
        # Create two OpenVPN site-to-site interfaces

        num_range = range(30, 35)
        port = ''
        local_address = ''
        remote_address = ''

        for ii in num_range:
            interface = f'vtun{ii}'
            local_address = f'192.0.{ii}.1'
            local_address_subnet = '255.255.255.252'
            remote_address = f'172.16.{ii}.1'
            path = base_path + [interface]
            port = str(3000 + ii)

            self.session.set(path + ['local-address', local_address])

            # even numbers use tun type, odd numbers use tap type
            if ii % 2 == 0:
                self.session.set(path + ['device-type', 'tun'])
            else:
                self.session.set(path + ['device-type', 'tap'])
                self.session.set(path + ['local-address', local_address, 'subnet-mask', local_address_subnet])

            self.session.set(path + ['mode', 'site-to-site'])
            self.session.set(path + ['local-port', port])
            self.session.set(path + ['remote-port', port])
            self.session.set(path + ['shared-secret-key-file', s2s_key])
            self.session.set(path + ['remote-address', remote_address])
            self.session.set(path + ['vrf', vrf_name])

        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            local_address = f'192.0.{ii}.1'
            remote_address = f'172.16.{ii}.1'
            port = str(3000 + ii)

            config_file = f'/run/openvpn/{interface}.conf'
            config = read_file(config_file)

            # even numbers use tun type, odd numbers use tap type
            if ii % 2 == 0:
                self.assertIn(f'dev-type tun', config)
                self.assertIn(f'ifconfig {local_address} {remote_address}', config)
            else:
                self.assertIn(f'dev-type tap', config)
                self.assertIn(f'ifconfig {local_address} {local_address_subnet}', config)

            self.assertIn(f'dev {interface}', config)
            self.assertIn(f'secret {s2s_key}', config)
            self.assertIn(f'lport {port}', config)
            self.assertIn(f'rport {port}', config)


            self.assertTrue(process_named_running(PROCESS_NAME))
            self.assertEqual(get_vrf(interface), vrf_name)
            self.assertIn(interface, interfaces())


        # check that no interface remained after deleting them
        self.session.delete(base_path)
        self.session.commit()

        for ii in num_range:
            interface = f'vtun{ii}'
            self.assertNotIn(interface, interfaces())


if __name__ == '__main__':
    # Our SSL certificates need a subject ...
    subject = '/C=DE/ST=BY/O=VyOS/localityName=Cloud/commonName=vyos/' \
              'organizationalUnitName=VyOS/emailAddress=maintainers@vyos.io/'

    if not (os.path.isfile(ssl_key) and os.path.isfile(ssl_cert)):
        # Generate mandatory SSL certificate
        tmp = f'openssl req -newkey rsa:4096 -new -nodes -x509 -days 3650 '\
              f'-keyout {ssl_key} -out {ssl_cert} -subj {subject}'
        print(cmd(tmp))

    if not os.path.isfile(ca_cert):
        # Generate "CA"
        tmp = f'openssl req -new -x509 -key {ssl_key} -out {ca_cert} -subj {subject}'
        print(cmd(tmp))

    if not os.path.isfile(dh_pem):
        # Generate "DH" key
        tmp = f'openssl dhparam -out {dh_pem} 2048'
        print(cmd(tmp))

    if not os.path.isfile(s2s_key):
        # Generate site-2-site key
        tmp = f'openvpn --genkey --secret {s2s_key}'
        print(cmd(tmp))

    if not os.path.isfile(auth_key):
        # Generate TLS auth key
        tmp = f'openvpn --genkey --secret {auth_key}'
        print(cmd(tmp))

    for file in [ca_cert, ssl_cert, ssl_key, dh_pem, s2s_key, auth_key]:
        cmd(f'sudo chown openvpn:openvpn {file}')

    unittest.main(verbosity=2)