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
|
# This file is part of cloud-init. See LICENSE file for license information.
from collections import namedtuple
from unittest.mock import patch
from cloudinit import ssh_util
from cloudinit.tests import helpers as test_helpers
from cloudinit import util
# https://stackoverflow.com/questions/11351032/
FakePwEnt = namedtuple(
'FakePwEnt',
['pw_dir', 'pw_gecos', 'pw_name', 'pw_passwd', 'pw_shell', 'pwd_uid'])
FakePwEnt.__new__.__defaults__ = tuple(
"UNSET_%s" % n for n in FakePwEnt._fields)
# Do not use these public keys, most of them are fetched from
# the testdata for OpenSSH, and their private keys are available
# https://github.com/openssh/openssh-portable/tree/master/regress/unittests/sshkey/testdata
VALID_CONTENT = {
'dsa': (
"AAAAB3NzaC1kc3MA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N"
"b2liCn9Nn0VHeNMMRLl7uyw4eKlOX2ogom8SLvihYxcJoqlCwtehpLsKsU4iwME"
"PmDteW5GBGf4GbnqPFkpIT5ed1jGhdZt/dpsp+v6QhYH1uX4pPxdkdnuc84/yb9"
"k4SQdKBJ+l3KZkfIxApNWOZqicJfz/eWwS/15hiamRKRuiiUV2zS1V+l8bV7g9O"
"gy5scPBMONxtfFlGEKikZKurFmzboCOGQKRBEUCpsY44IAp443h59pQdVIb0YAS"
"kfp2xKHwYij6ELRNdH5MrlFa3bNTskGO4k5XDR4cl/Sma2SXgBKb5XjTtlNmCQG"
"Gv6lOW7pGXNhs5wfd8K9Ukm6KeLTIlYn1iiKM37YQpa+4JQYljCYhumbqNCkPTZ"
"rNYClh8fQEQ8XuOCDpomMWu58YOTfbZNMDWs/Ou7RfCjX+VNwjPShDK9joMwWKc"
"Jy3QalZbaoWtcyyvXxR2sqhVR9F7Cmasq4="
),
'ssh-xmss@openssh.com': (
"AAAAFHNzaC14bXNzQG9wZW5zc2guY29tAAAAFVhNU1NfU0hBMi0yNTZfVzE2X0g"
"xMAAAAECqptWnK94d+Sj2xcdTu8gz+75lawZoLSZFqC5IhbYuT/Z3oBZCim6yt+"
"HAmk6MKldl3Fg+74v4sR/SII0I0Jv/"
),
}
KEY_TYPES = list(VALID_CONTENT.keys())
TEST_OPTIONS = (
"no-port-forwarding,no-agent-forwarding,no-X11-forwarding,"
'command="echo \'Please login as the user \"ubuntu\" rather than the'
'user \"root\".\';echo;sleep 10"')
class TestAuthKeyLineParser(test_helpers.CiTestCase):
def test_simple_parse(self):
# test key line with common 3 fields (keytype, base64, comment)
parser = ssh_util.AuthKeyLineParser()
for ktype in KEY_TYPES:
content = VALID_CONTENT[ktype]
comment = 'user-%s@host' % ktype
line = ' '.join((ktype, content, comment,))
key = parser.parse(line)
self.assertEqual(key.base64, content)
self.assertFalse(key.options)
self.assertEqual(key.comment, comment)
self.assertEqual(key.keytype, ktype)
def test_parse_no_comment(self):
# test key line with key type and base64 only
parser = ssh_util.AuthKeyLineParser()
for ktype in KEY_TYPES:
content = VALID_CONTENT[ktype]
line = ' '.join((ktype, content,))
key = parser.parse(line)
self.assertEqual(key.base64, content)
self.assertFalse(key.options)
self.assertFalse(key.comment)
self.assertEqual(key.keytype, ktype)
def test_parse_with_keyoptions(self):
# test key line with options in it
parser = ssh_util.AuthKeyLineParser()
options = TEST_OPTIONS
for ktype in KEY_TYPES:
content = VALID_CONTENT[ktype]
comment = 'user-%s@host' % ktype
line = ' '.join((options, ktype, content, comment,))
key = parser.parse(line)
self.assertEqual(key.base64, content)
self.assertEqual(key.options, options)
self.assertEqual(key.comment, comment)
self.assertEqual(key.keytype, ktype)
def test_parse_with_options_passed_in(self):
# test key line with key type and base64 only
parser = ssh_util.AuthKeyLineParser()
baseline = ' '.join(("rsa", VALID_CONTENT['rsa'], "user@host"))
myopts = "no-port-forwarding,no-agent-forwarding"
key = parser.parse("allowedopt" + " " + baseline)
self.assertEqual(key.options, "allowedopt")
key = parser.parse("overridden_opt " + baseline, options=myopts)
self.assertEqual(key.options, myopts)
def test_parse_invalid_keytype(self):
parser = ssh_util.AuthKeyLineParser()
key = parser.parse(' '.join(["badkeytype", VALID_CONTENT['rsa']]))
self.assertFalse(key.valid())
class TestUpdateAuthorizedKeys(test_helpers.CiTestCase):
def test_new_keys_replace(self):
"""new entries with the same base64 should replace old."""
orig_entries = [
' '.join(('rsa', VALID_CONTENT['rsa'], 'orig_comment1')),
' '.join(('dsa', VALID_CONTENT['dsa'], 'orig_comment2'))]
new_entries = [
' '.join(('rsa', VALID_CONTENT['rsa'], 'new_comment1')), ]
expected = '\n'.join([new_entries[0], orig_entries[1]]) + '\n'
parser = ssh_util.AuthKeyLineParser()
found = ssh_util.update_authorized_keys(
[parser.parse(p) for p in orig_entries],
[parser.parse(p) for p in new_entries])
self.assertEqual(expected, found)
def test_new_invalid_keys_are_ignored(self):
"""new entries that are invalid should be skipped."""
orig_entries = [
' '.join(('rsa', VALID_CONTENT['rsa'], 'orig_comment1')),
' '.join(('dsa', VALID_CONTENT['dsa'], 'orig_comment2'))]
new_entries = [
' '.join(('rsa', VALID_CONTENT['rsa'], 'new_comment1')),
'xxx-invalid-thing1',
'xxx-invalid-blob2'
]
expected = '\n'.join([new_entries[0], orig_entries[1]]) + '\n'
parser = ssh_util.AuthKeyLineParser()
found = ssh_util.update_authorized_keys(
[parser.parse(p) for p in orig_entries],
[parser.parse(p) for p in new_entries])
self.assertEqual(expected, found)
class TestParseSSHConfig(test_helpers.CiTestCase):
def setUp(self):
self.load_file_patch = patch('cloudinit.ssh_util.util.load_file')
self.load_file = self.load_file_patch.start()
self.isfile_patch = patch('cloudinit.ssh_util.os.path.isfile')
self.isfile = self.isfile_patch.start()
self.isfile.return_value = True
def tearDown(self):
self.load_file_patch.stop()
self.isfile_patch.stop()
def test_not_a_file(self):
self.isfile.return_value = False
self.load_file.side_effect = IOError
ret = ssh_util.parse_ssh_config('not a real file')
self.assertEqual([], ret)
def test_empty_file(self):
self.load_file.return_value = ''
ret = ssh_util.parse_ssh_config('some real file')
self.assertEqual([], ret)
def test_comment_line(self):
comment_line = '# This is a comment'
self.load_file.return_value = comment_line
ret = ssh_util.parse_ssh_config('some real file')
self.assertEqual(1, len(ret))
self.assertEqual(comment_line, ret[0].line)
def test_blank_lines(self):
lines = ['', '\t', ' ']
self.load_file.return_value = '\n'.join(lines)
ret = ssh_util.parse_ssh_config('some real file')
self.assertEqual(len(lines), len(ret))
for line in ret:
self.assertEqual('', line.line)
def test_lower_case_config(self):
self.load_file.return_value = 'foo bar'
ret = ssh_util.parse_ssh_config('some real file')
self.assertEqual(1, len(ret))
self.assertEqual('foo', ret[0].key)
self.assertEqual('bar', ret[0].value)
def test_upper_case_config(self):
self.load_file.return_value = 'Foo Bar'
ret = ssh_util.parse_ssh_config('some real file')
self.assertEqual(1, len(ret))
self.assertEqual('foo', ret[0].key)
self.assertEqual('Bar', ret[0].value)
def test_lower_case_with_equals(self):
self.load_file.return_value = 'foo=bar'
ret = ssh_util.parse_ssh_config('some real file')
self.assertEqual(1, len(ret))
self.assertEqual('foo', ret[0].key)
self.assertEqual('bar', ret[0].value)
def test_upper_case_with_equals(self):
self.load_file.return_value = 'Foo=bar'
ret = ssh_util.parse_ssh_config('some real file')
self.assertEqual(1, len(ret))
self.assertEqual('foo', ret[0].key)
self.assertEqual('bar', ret[0].value)
class TestUpdateSshConfigLines(test_helpers.CiTestCase):
"""Test the update_ssh_config_lines method."""
exlines = [
"#PasswordAuthentication yes",
"UsePAM yes",
"# Comment line",
"AcceptEnv LANG LC_*",
"X11Forwarding no",
]
pwauth = "PasswordAuthentication"
def check_line(self, line, opt, val):
self.assertEqual(line.key, opt.lower())
self.assertEqual(line.value, val)
self.assertIn(opt, str(line))
self.assertIn(val, str(line))
def test_new_option_added(self):
"""A single update of non-existing option."""
lines = ssh_util.parse_ssh_config_lines(list(self.exlines))
result = ssh_util.update_ssh_config_lines(lines, {'MyKey': 'MyVal'})
self.assertEqual(['MyKey'], result)
self.check_line(lines[-1], "MyKey", "MyVal")
def test_commented_out_not_updated_but_appended(self):
"""Implementation does not un-comment and update lines."""
lines = ssh_util.parse_ssh_config_lines(list(self.exlines))
result = ssh_util.update_ssh_config_lines(lines, {self.pwauth: "no"})
self.assertEqual([self.pwauth], result)
self.check_line(lines[-1], self.pwauth, "no")
def test_single_option_updated(self):
"""A single update should have change made and line updated."""
opt, val = ("UsePAM", "no")
lines = ssh_util.parse_ssh_config_lines(list(self.exlines))
result = ssh_util.update_ssh_config_lines(lines, {opt: val})
self.assertEqual([opt], result)
self.check_line(lines[1], opt, val)
def test_multiple_updates_with_add(self):
"""Verify multiple updates some added some changed, some not."""
updates = {"UsePAM": "no", "X11Forwarding": "no", "NewOpt": "newval",
"AcceptEnv": "LANG ADD LC_*"}
lines = ssh_util.parse_ssh_config_lines(list(self.exlines))
result = ssh_util.update_ssh_config_lines(lines, updates)
self.assertEqual(set(["UsePAM", "NewOpt", "AcceptEnv"]), set(result))
self.check_line(lines[3], "AcceptEnv", updates["AcceptEnv"])
def test_return_empty_if_no_changes(self):
"""If there are no changes, then return should be empty list."""
updates = {"UsePAM": "yes"}
lines = ssh_util.parse_ssh_config_lines(list(self.exlines))
result = ssh_util.update_ssh_config_lines(lines, updates)
self.assertEqual([], result)
self.assertEqual(self.exlines, [str(line) for line in lines])
def test_keycase_not_modified(self):
"""Original case of key should not be changed on update.
This behavior is to keep original config as much intact as can be."""
updates = {"usepam": "no"}
lines = ssh_util.parse_ssh_config_lines(list(self.exlines))
result = ssh_util.update_ssh_config_lines(lines, updates)
self.assertEqual(["usepam"], result)
self.assertEqual("UsePAM no", str(lines[1]))
class TestUpdateSshConfig(test_helpers.CiTestCase):
cfgdata = '\n'.join(["#Option val", "MyKey ORIG_VAL", ""])
def test_modified(self):
mycfg = self.tmp_path("ssh_config_1")
util.write_file(mycfg, self.cfgdata)
ret = ssh_util.update_ssh_config({"MyKey": "NEW_VAL"}, mycfg)
self.assertTrue(ret)
found = util.load_file(mycfg)
self.assertEqual(self.cfgdata.replace("ORIG_VAL", "NEW_VAL"), found)
# assert there is a newline at end of file (LP: #1677205)
self.assertEqual('\n', found[-1])
def test_not_modified(self):
mycfg = self.tmp_path("ssh_config_2")
util.write_file(mycfg, self.cfgdata)
with patch("cloudinit.ssh_util.util.write_file") as m_write_file:
ret = ssh_util.update_ssh_config({"MyKey": "ORIG_VAL"}, mycfg)
self.assertFalse(ret)
self.assertEqual(self.cfgdata, util.load_file(mycfg))
m_write_file.assert_not_called()
class TestBasicAuthorizedKeyParse(test_helpers.CiTestCase):
def test_user(self):
self.assertEqual(
["/opt/bobby/keys"],
ssh_util.render_authorizedkeysfile_paths(
"/opt/%u/keys", "/home/bobby", "bobby"))
def test_multiple(self):
self.assertEqual(
["/keys/path1", "/keys/path2"],
ssh_util.render_authorizedkeysfile_paths(
"/keys/path1 /keys/path2", "/home/bobby", "bobby"))
def test_relative(self):
self.assertEqual(
["/home/bobby/.secret/keys"],
ssh_util.render_authorizedkeysfile_paths(
".secret/keys", "/home/bobby", "bobby"))
def test_home(self):
self.assertEqual(
["/homedirs/bobby/.keys"],
ssh_util.render_authorizedkeysfile_paths(
"%h/.keys", "/homedirs/bobby", "bobby"))
def test_all(self):
self.assertEqual(
["/homedirs/bobby/.keys", "/homedirs/bobby/.secret/keys",
"/keys/path1", "/opt/bobby/keys"],
ssh_util.render_authorizedkeysfile_paths(
"%h/.keys .secret/keys /keys/path1 /opt/%u/keys",
"/homedirs/bobby", "bobby"))
class TestMultipleSshAuthorizedKeysFile(test_helpers.CiTestCase):
@patch("cloudinit.ssh_util.pwd.getpwnam")
def test_multiple_authorizedkeys_file_order1(self, m_getpwnam):
fpw = FakePwEnt(pw_name='bobby', pw_dir='/tmp/home2/bobby')
m_getpwnam.return_value = fpw
user_ssh_folder = "%s/.ssh" % fpw.pw_dir
# /tmp/home2/bobby/.ssh/authorized_keys = rsa
authorized_keys = self.tmp_path('authorized_keys', dir=user_ssh_folder)
util.write_file(authorized_keys, VALID_CONTENT['rsa'])
# /tmp/home2/bobby/.ssh/user_keys = dsa
user_keys = self.tmp_path('user_keys', dir=user_ssh_folder)
util.write_file(user_keys, VALID_CONTENT['dsa'])
# /tmp/sshd_config
sshd_config = self.tmp_path('sshd_config', dir="/tmp")
util.write_file(
sshd_config,
"AuthorizedKeysFile %s %s" % (authorized_keys, user_keys)
)
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual(user_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
@patch("cloudinit.ssh_util.pwd.getpwnam")
def test_multiple_authorizedkeys_file_order2(self, m_getpwnam):
fpw = FakePwEnt(pw_name='suzie', pw_dir='/tmp/home/suzie')
m_getpwnam.return_value = fpw
user_ssh_folder = "%s/.ssh" % fpw.pw_dir
# /tmp/home/suzie/.ssh/authorized_keys = rsa
authorized_keys = self.tmp_path('authorized_keys', dir=user_ssh_folder)
util.write_file(authorized_keys, VALID_CONTENT['rsa'])
# /tmp/home/suzie/.ssh/user_keys = dsa
user_keys = self.tmp_path('user_keys', dir=user_ssh_folder)
util.write_file(user_keys, VALID_CONTENT['dsa'])
# /tmp/sshd_config
sshd_config = self.tmp_path('sshd_config', dir="/tmp")
util.write_file(
sshd_config,
"AuthorizedKeysFile %s %s" % (user_keys, authorized_keys)
)
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual(authorized_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
@patch("cloudinit.ssh_util.pwd.getpwnam")
def test_multiple_authorizedkeys_file_local_global(self, m_getpwnam):
fpw = FakePwEnt(pw_name='bobby', pw_dir='/tmp/home2/bobby')
m_getpwnam.return_value = fpw
user_ssh_folder = "%s/.ssh" % fpw.pw_dir
# /tmp/home2/bobby/.ssh/authorized_keys = rsa
authorized_keys = self.tmp_path('authorized_keys', dir=user_ssh_folder)
util.write_file(authorized_keys, VALID_CONTENT['rsa'])
# /tmp/home2/bobby/.ssh/user_keys = dsa
user_keys = self.tmp_path('user_keys', dir=user_ssh_folder)
util.write_file(user_keys, VALID_CONTENT['dsa'])
# /tmp/etc/ssh/authorized_keys = ecdsa
authorized_keys_global = self.tmp_path('etc/ssh/authorized_keys',
dir="/tmp")
util.write_file(authorized_keys_global, VALID_CONTENT['ecdsa'])
# /tmp/sshd_config
sshd_config = self.tmp_path('sshd_config', dir="/tmp")
util.write_file(
sshd_config,
"AuthorizedKeysFile %s %s %s" % (authorized_keys_global,
user_keys, authorized_keys)
)
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual(authorized_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['ecdsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
@patch("cloudinit.ssh_util.pwd.getpwnam")
def test_multiple_authorizedkeys_file_local_global2(self, m_getpwnam):
fpw = FakePwEnt(pw_name='bobby', pw_dir='/tmp/home2/bobby')
m_getpwnam.return_value = fpw
user_ssh_folder = "%s/.ssh" % fpw.pw_dir
# /tmp/home2/bobby/.ssh/authorized_keys2 = rsa
authorized_keys = self.tmp_path('authorized_keys2',
dir=user_ssh_folder)
util.write_file(authorized_keys, VALID_CONTENT['rsa'])
# /tmp/home2/bobby/.ssh/user_keys3 = dsa
user_keys = self.tmp_path('user_keys3', dir=user_ssh_folder)
util.write_file(user_keys, VALID_CONTENT['dsa'])
# /tmp/etc/ssh/authorized_keys = ecdsa
authorized_keys_global = self.tmp_path('etc/ssh/authorized_keys',
dir="/tmp")
util.write_file(authorized_keys_global, VALID_CONTENT['ecdsa'])
# /tmp/sshd_config
sshd_config = self.tmp_path('sshd_config', dir="/tmp")
util.write_file(
sshd_config,
"AuthorizedKeysFile %s %s %s" % (authorized_keys_global,
authorized_keys, user_keys)
)
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual(user_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['ecdsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
@patch("cloudinit.ssh_util.pwd.getpwnam")
def test_multiple_authorizedkeys_file_global(self, m_getpwnam):
fpw = FakePwEnt(pw_name='bobby', pw_dir='/tmp/home2/bobby')
m_getpwnam.return_value = fpw
# /tmp/etc/ssh/authorized_keys = rsa
authorized_keys_global = self.tmp_path('etc/ssh/authorized_keys',
dir="/tmp")
util.write_file(authorized_keys_global, VALID_CONTENT['rsa'])
# /tmp/sshd_config
sshd_config = self.tmp_path('sshd_config')
util.write_file(
sshd_config,
"AuthorizedKeysFile %s" % (authorized_keys_global)
)
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual("%s/.ssh/authorized_keys" % fpw.pw_dir, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
@patch("cloudinit.ssh_util.pwd.getpwnam")
def test_multiple_authorizedkeys_file_multiuser(self, m_getpwnam):
fpw = FakePwEnt(pw_name='bobby', pw_dir='/tmp/home2/bobby')
m_getpwnam.return_value = fpw
user_ssh_folder = "%s/.ssh" % fpw.pw_dir
# /tmp/home2/bobby/.ssh/authorized_keys2 = rsa
authorized_keys = self.tmp_path('authorized_keys2',
dir=user_ssh_folder)
util.write_file(authorized_keys, VALID_CONTENT['rsa'])
# /tmp/home2/bobby/.ssh/user_keys3 = dsa
user_keys = self.tmp_path('user_keys3', dir=user_ssh_folder)
util.write_file(user_keys, VALID_CONTENT['dsa'])
fpw2 = FakePwEnt(pw_name='suzie', pw_dir='/tmp/home/suzie')
user_ssh_folder = "%s/.ssh" % fpw2.pw_dir
# /tmp/home/suzie/.ssh/authorized_keys2 = ssh-xmss@openssh.com
authorized_keys2 = self.tmp_path('authorized_keys2',
dir=user_ssh_folder)
util.write_file(authorized_keys2,
VALID_CONTENT['ssh-xmss@openssh.com'])
# /tmp/etc/ssh/authorized_keys = ecdsa
authorized_keys_global = self.tmp_path('etc/ssh/authorized_keys2',
dir="/tmp")
util.write_file(authorized_keys_global, VALID_CONTENT['ecdsa'])
# /tmp/sshd_config
sshd_config = self.tmp_path('sshd_config', dir="/tmp")
util.write_file(
sshd_config,
"AuthorizedKeysFile %s %%h/.ssh/authorized_keys2 %s" %
(authorized_keys_global, user_keys)
)
# process first user
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual(user_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['ecdsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
self.assertFalse(VALID_CONTENT['ssh-xmss@openssh.com'] in content)
m_getpwnam.return_value = fpw2
# process second user
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw2.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual(authorized_keys2, auth_key_fn)
self.assertTrue(VALID_CONTENT['ssh-xmss@openssh.com'] in content)
self.assertTrue(VALID_CONTENT['ecdsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
self.assertFalse(VALID_CONTENT['rsa'] in content)
@patch("cloudinit.ssh_util.pwd.getpwnam")
def test_multiple_authorizedkeys_file_multiuser2(self, m_getpwnam):
fpw = FakePwEnt(pw_name='bobby', pw_dir='/tmp/home/bobby')
m_getpwnam.return_value = fpw
user_ssh_folder = "%s/.ssh" % fpw.pw_dir
# /tmp/home/bobby/.ssh/authorized_keys2 = rsa
authorized_keys = self.tmp_path('authorized_keys2',
dir=user_ssh_folder)
util.write_file(authorized_keys, VALID_CONTENT['rsa'])
# /tmp/home/bobby/.ssh/user_keys3 = dsa
user_keys = self.tmp_path('user_keys3', dir=user_ssh_folder)
util.write_file(user_keys, VALID_CONTENT['dsa'])
fpw2 = FakePwEnt(pw_name='badguy', pw_dir='/tmp/home/badguy')
user_ssh_folder = "%s/.ssh" % fpw2.pw_dir
# /tmp/home/badguy/home/bobby = ""
authorized_keys2 = self.tmp_path('home/bobby', dir="/tmp/home/badguy")
# /tmp/etc/ssh/authorized_keys = ecdsa
authorized_keys_global = self.tmp_path('etc/ssh/authorized_keys2',
dir="/tmp")
util.write_file(authorized_keys_global, VALID_CONTENT['ecdsa'])
# /tmp/sshd_config
sshd_config = self.tmp_path('sshd_config', dir="/tmp")
util.write_file(
sshd_config,
"AuthorizedKeysFile %s %%h/.ssh/authorized_keys2 %s %s" %
(authorized_keys_global, user_keys, authorized_keys2)
)
# process first user
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
self.assertEqual(user_keys, auth_key_fn)
self.assertTrue(VALID_CONTENT['rsa'] in content)
self.assertTrue(VALID_CONTENT['ecdsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
m_getpwnam.return_value = fpw2
# process second user
(auth_key_fn, auth_key_entries) = ssh_util.extract_authorized_keys(
fpw2.pw_name, sshd_config)
content = ssh_util.update_authorized_keys(auth_key_entries, [])
# badguy should not take the key from the other user!
self.assertEqual(authorized_keys2, auth_key_fn)
self.assertTrue(VALID_CONTENT['ecdsa'] in content)
self.assertTrue(VALID_CONTENT['dsa'] in content)
self.assertFalse(VALID_CONTENT['rsa'] in content)
# vi: ts=4 expandtab
|