summaryrefslogtreecommitdiff
path: root/tests/generator/test_auth.py
blob: e8fd023a01a225275f4ffb79f11ebcf7a8665382 (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
#
# Tests for network authentication config generated via netplan
#
# Copyright (C) 2018 Canonical, Ltd.
# Author: Mathieu Trudel-Lapierre <mathieu.trudel.lapierre@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
#
# 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 stat

from .base import TestBase, ND_DHCP4, ND_WIFI_DHCP4, SD_WPA, NM_MANAGED, NM_UNMANAGED


class TestNetworkd(TestBase):

    def test_auth_wifi_detailed(self):
        self.generate('''network:
  version: 2
  wifis:
    wl0:
      access-points:
        "Joe's Home":
          password: "s0s3kr1t"
        "Luke's Home":
          auth:
            key-management: psk
            password: "4lsos3kr1t"
        "BobsHome":
          password: "e03ce667c87bc81ca968d9120ca37f89eb09aec3c55b80386e5d772efd6b926e"
        "BillsHome":
          auth:
            key-management: psk
            password: "db3b0acf5653aeaddd5fe034fb9f07175b2864f847b005aaa2f09182d9411b04"
        workplace:
          auth:
            key-management: eap
            method: ttls
            anonymous-identity: "@internal.example.com"
            identity: "joe@internal.example.com"
            password: "v3ryS3kr1t"
        workplace2:
          auth:
            key-management: eap
            method: peap
            identity: "joe@internal.example.com"
            password: "v3ryS3kr1t"
            ca-certificate: /etc/ssl/work2-cacrt.pem
        workplacehashed:
          auth:
            key-management: eap
            method: ttls
            anonymous-identity: "@internal.example.com"
            identity: "joe@internal.example.com"
            password: hash:9db1636cedc5948537e7bee0cc1e9590
        customernet:
          auth:
            key-management: eap
            method: tls
            anonymous-identity: "@cust.example.com"
            identity: "cert-joe@cust.example.com"
            ca-certificate: /etc/ssl/cust-cacrt.pem
            client-certificate: /etc/ssl/cust-crt.pem
            client-key: /etc/ssl/cust-key.pem
            client-key-password: "d3cryptPr1v4t3K3y"
        opennet:
          auth:
            key-management: none
        peer2peer:
          mode: adhoc
          auth: {}
      dhcp4: yes
      ''')

        self.assert_networkd({'wl0.network': ND_WIFI_DHCP4 % 'wl0'})
        self.assert_nm(None)
        self.assert_nm_udev(NM_UNMANAGED % 'wl0')

        # generates wpa config and enables wpasupplicant unit
        with open(os.path.join(self.workdir.name, 'run/netplan/wpa-wl0.conf')) as f:
            new_config = f.read()
            self.assertIn('ctrl_interface=/run/wpa_supplicant', new_config)
            self.assertIn('''
network={
  ssid="peer2peer"
  mode=1
  key_mgmt=NONE
}
''', new_config)
            self.assertIn('''
network={
  ssid="Luke's Home"
  key_mgmt=WPA-PSK
  psk="4lsos3kr1t"
}
''', new_config)
            self.assertIn('''
network={
  ssid="BobsHome"
  key_mgmt=WPA-PSK
  psk=e03ce667c87bc81ca968d9120ca37f89eb09aec3c55b80386e5d772efd6b926e
}
''', new_config)
            self.assertIn('''
network={
  ssid="BillsHome"
  key_mgmt=WPA-PSK
  psk=db3b0acf5653aeaddd5fe034fb9f07175b2864f847b005aaa2f09182d9411b04
}
''', new_config)
            self.assertIn('''
network={
  ssid="workplace2"
  key_mgmt=WPA-EAP
  eap=PEAP
  identity="joe@internal.example.com"
  password="v3ryS3kr1t"
  ca_cert="/etc/ssl/work2-cacrt.pem"
}
''', new_config)
            self.assertIn('''
network={
  ssid="workplace"
  key_mgmt=WPA-EAP
  eap=TTLS
  identity="joe@internal.example.com"
  anonymous_identity="@internal.example.com"
  password="v3ryS3kr1t"
}
''', new_config)
            self.assertIn('''
network={
  ssid="workplacehashed"
  key_mgmt=WPA-EAP
  eap=TTLS
  identity="joe@internal.example.com"
  anonymous_identity="@internal.example.com"
  password=hash:9db1636cedc5948537e7bee0cc1e9590
}
''', new_config)
            self.assertIn('''
network={
  ssid="customernet"
  key_mgmt=WPA-EAP
  eap=TLS
  identity="cert-joe@cust.example.com"
  anonymous_identity="@cust.example.com"
  ca_cert="/etc/ssl/cust-cacrt.pem"
  client_cert="/etc/ssl/cust-crt.pem"
  private_key="/etc/ssl/cust-key.pem"
  private_key_passwd="d3cryptPr1v4t3K3y"
}
''', new_config)
            self.assertIn('''
network={
  ssid="opennet"
  key_mgmt=NONE
}
''', new_config)
            self.assertIn('''
network={
  ssid="Joe's Home"
  key_mgmt=WPA-PSK
  psk="s0s3kr1t"
}
''', new_config)
            self.assertEqual(stat.S_IMODE(os.fstat(f.fileno()).st_mode), 0o600)
        self.assertTrue(os.path.isfile(os.path.join(
            self.workdir.name, 'run/systemd/system/netplan-wpa-wl0.service')))
        self.assertTrue(os.path.islink(os.path.join(
            self.workdir.name, 'run/systemd/system/systemd-networkd.service.wants/netplan-wpa-wl0.service')))

    def test_auth_wired(self):
        self.generate('''network:
  version: 2
  ethernets:
    eth0:
      auth:
        key-management: 802.1x
        method: tls
        anonymous-identity: "@cust.example.com"
        identity: "cert-joe@cust.example.com"
        ca-certificate: /etc/ssl/cust-cacrt.pem
        client-certificate: /etc/ssl/cust-crt.pem
        client-key: /etc/ssl/cust-key.pem
        client-key-password: "d3cryptPr1v4t3K3y"
        phase2-auth: MSCHAPV2
      dhcp4: yes
      ''')

        self.assert_networkd({'eth0.network': ND_DHCP4 % 'eth0'})
        self.assert_nm(None)
        self.assert_nm_udev(NM_UNMANAGED % 'eth0')

        # generates wpa config and enables wpasupplicant unit
        with open(os.path.join(self.workdir.name, 'run/netplan/wpa-eth0.conf')) as f:
            self.assertEqual(f.read(), '''ctrl_interface=/run/wpa_supplicant

network={
  key_mgmt=IEEE8021X
  eap=TLS
  identity="cert-joe@cust.example.com"
  anonymous_identity="@cust.example.com"
  ca_cert="/etc/ssl/cust-cacrt.pem"
  client_cert="/etc/ssl/cust-crt.pem"
  private_key="/etc/ssl/cust-key.pem"
  private_key_passwd="d3cryptPr1v4t3K3y"
  phase2="auth=MSCHAPV2"
}
''')
            self.assertEqual(stat.S_IMODE(os.fstat(f.fileno()).st_mode), 0o600)
        self.assertTrue(os.path.isfile(os.path.join(
            self.workdir.name, 'run/systemd/system/netplan-wpa-eth0.service')))

        with open(os.path.join(self.workdir.name, 'run/systemd/system/netplan-wpa-eth0.service')) as f:
            self.assertEqual(f.read(), SD_WPA % {'iface': 'eth0', 'drivers': 'wired'})
            self.assertEqual(stat.S_IMODE(os.fstat(f.fileno()).st_mode), 0o644)
        self.assertTrue(os.path.islink(os.path.join(
            self.workdir.name, 'run/systemd/system/systemd-networkd.service.wants/netplan-wpa-eth0.service')))


class TestNetworkManager(TestBase):

    def test_auth_wifi_detailed(self):
        self.generate('''network:
  version: 2
  renderer: NetworkManager
  wifis:
    wl0:
      access-points:
        "Joe's Home":
          password: "s0s3kr1t"
        "Luke's Home":
          auth:
            key-management: psk
            password: "4lsos3kr1t"
        workplace:
          auth:
            key-management: eap
            method: ttls
            anonymous-identity: "@internal.example.com"
            identity: "joe@internal.example.com"
            password: "v3ryS3kr1t"
        workplace2:
          auth:
            key-management: eap
            method: peap
            identity: "joe@internal.example.com"
            password: "v3ryS3kr1t"
            ca-certificate: /etc/ssl/work2-cacrt.pem
        workplacehashed:
          auth:
            key-management: eap
            method: ttls
            anonymous-identity: "@internal.example.com"
            identity: "joe@internal.example.com"
            password: hash:9db1636cedc5948537e7bee0cc1e9590
        customernet:
          auth:
            key-management: 802.1x
            method: tls
            anonymous-identity: "@cust.example.com"
            identity: "cert-joe@cust.example.com"
            ca-certificate: /etc/ssl/cust-cacrt.pem
            client-certificate: /etc/ssl/cust-crt.pem
            client-key: /etc/ssl/cust-key.pem
            client-key-password: "d3cryptPr1v4t3K3y"
            phase2-auth: MSCHAPV2
        opennet:
          auth:
            key-management: none
        peer2peer:
          mode: adhoc
          auth: {}
      dhcp4: yes
      ''')

        self.assert_networkd({})
        self.assert_nm({'wl0-Joe%27s%20Home': '''[connection]
id=netplan-wl0-Joe's Home
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=Joe's Home
mode=infrastructure

[wifi-security]
key-mgmt=wpa-psk
psk=s0s3kr1t
''',
                        'wl0-Luke%27s%20Home': '''[connection]
id=netplan-wl0-Luke's Home
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=Luke's Home
mode=infrastructure

[wifi-security]
key-mgmt=wpa-psk
psk=4lsos3kr1t
''',
                        'wl0-workplace': '''[connection]
id=netplan-wl0-workplace
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=workplace
mode=infrastructure

[wifi-security]
key-mgmt=wpa-eap

[802-1x]
eap=ttls
identity=joe@internal.example.com
anonymous-identity=@internal.example.com
password=v3ryS3kr1t
''',
                        'wl0-workplace2': '''[connection]
id=netplan-wl0-workplace2
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=workplace2
mode=infrastructure

[wifi-security]
key-mgmt=wpa-eap

[802-1x]
eap=peap
identity=joe@internal.example.com
password=v3ryS3kr1t
ca-cert=/etc/ssl/work2-cacrt.pem
''',
                        'wl0-workplacehashed': '''[connection]
id=netplan-wl0-workplacehashed
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=workplacehashed
mode=infrastructure

[wifi-security]
key-mgmt=wpa-eap

[802-1x]
eap=ttls
identity=joe@internal.example.com
anonymous-identity=@internal.example.com
password=hash:9db1636cedc5948537e7bee0cc1e9590
''',
                        'wl0-customernet': '''[connection]
id=netplan-wl0-customernet
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=customernet
mode=infrastructure

[wifi-security]
key-mgmt=ieee8021x

[802-1x]
eap=tls
identity=cert-joe@cust.example.com
anonymous-identity=@cust.example.com
ca-cert=/etc/ssl/cust-cacrt.pem
client-cert=/etc/ssl/cust-crt.pem
private-key=/etc/ssl/cust-key.pem
private-key-password=d3cryptPr1v4t3K3y
phase2-auth=MSCHAPV2
''',
                        'wl0-opennet': '''[connection]
id=netplan-wl0-opennet
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=opennet
mode=infrastructure
''',
                        'wl0-peer2peer': '''[connection]
id=netplan-wl0-peer2peer
type=wifi
interface-name=wl0

[ipv4]
method=auto

[ipv6]
method=ignore

[wifi]
ssid=peer2peer
mode=adhoc
'''})
        self.assert_nm_udev(NM_MANAGED % 'wl0')

    def test_auth_wired(self):
        self.generate('''network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eth0:
      auth:
        key-management: 802.1x
        method: tls
        anonymous-identity: "@cust.example.com"
        identity: "cert-joe@cust.example.com"
        ca-certificate: /etc/ssl/cust-cacrt.pem
        client-certificate: /etc/ssl/cust-crt.pem
        client-key: /etc/ssl/cust-key.pem
        client-key-password: "d3cryptPr1v4t3K3y"
      dhcp4: yes
      ''')

        self.assert_nm({'eth0': '''[connection]
id=netplan-eth0
type=ethernet
interface-name=eth0

[ethernet]
wake-on-lan=0

[ipv4]
method=auto

[ipv6]
method=ignore

[802-1x]
eap=tls
identity=cert-joe@cust.example.com
anonymous-identity=@cust.example.com
ca-cert=/etc/ssl/cust-cacrt.pem
client-cert=/etc/ssl/cust-crt.pem
private-key=/etc/ssl/cust-key.pem
private-key-password=d3cryptPr1v4t3K3y
'''})
        self.assert_networkd({})
        self.assert_nm_udev(NM_MANAGED % 'eth0')


class TestConfigErrors(TestBase):

    def test_auth_invalid_key_mgmt(self):
        err = self.generate('''network:
  version: 2
  ethernets:
    eth0:
      auth:
        key-management: bogus''', expect_fail=True)
        self.assertIn("unknown key management type 'bogus'", err)

    def test_auth_invalid_eap_method(self):
        err = self.generate('''network:
  version: 2
  ethernets:
    eth0:
      auth:
        method: bogus''', expect_fail=True)
        self.assertIn("unknown EAP method 'bogus'", err)

    def test_auth_networkd_wifi_psk_too_big(self):
        err = self.generate('''network:
  version: 2
  wifis:
    wl0:
      access-points:
        "Joe's Home":
          password: "LoremipsumdolorsitametconsecteturadipiscingelitCrastemporvelitnunc"
      dhcp4: yes''', expect_fail=True)
        self.assertIn("ASCII passphrase must be between 8 and 63 characters (inclusive)", err)

    def test_auth_networkd_wifi_psk_too_small(self):
        err = self.generate('''network:
  version: 2
  wifis:
    wl0:
      access-points:
        "Joe's Home":
          password: "p4ss"
      dhcp4: yes''', expect_fail=True)
        self.assertIn("ASCII passphrase must be between 8 and 63 characters (inclusive)", err)

    def test_auth_networkd_wifi_psk_64_non_hexdigit(self):
        err = self.generate('''network:
  version: 2
  wifis:
    wl0:
      access-points:
        "Joe's Home":
          password: "LoremipsumdolorsitametconsecteturadipiscingelitCrastemporvelitnu"
      dhcp4: yes''', expect_fail=True)
        self.assertIn("PSK length of 64 is only supported for hex-digit representation", err)

    def test_auth_networkd_wire_psk_64_non_hexdigit(self):
        err = self.generate('''network:
  version: 2
  ethernets:
    eth0:
      auth:
        key-management: psk
        password: "LoremipsumdolorsitametconsecteturadipiscingelitCrastemporvelitnu"
      dhcp4: yes''', expect_fail=True)
        self.assertIn("PSK length of 64 is only supported for hex-digit representation", err)