Debian Codegraph @

test/verify/check-shell-multi-machine

raw
1 #!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv)
2
3 #
4 # Copyright (C) 2013 Red Hat, Inc.
5 # SPDX-License-Identifier: LGPL-2.1-or-later
6
7
8 import re
9 import subprocess
10 import time
11
12 import testlib
13
14
15 def break_hostkey(m, address):
16 filename = "/home/admin/.ssh/known_hosts"
17
18 m.execute(f'su admin -c "mkdir -p -m 700 `dirname {filename}`"')
19 key = f"{address} ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJqfgO2FPiix1n2sCJCXbaffwog1Vvi3zRdmcAxG//5T"
20 m.write(filename, key, owner="admin:admin")
21
22
23 def fix_hostkey(m, key=None):
24 filename = "/home/admin/.ssh/known_hosts"
25 m.execute(f'su admin -c "mkdir -p -m 700 `dirname {filename}`"')
26 m.write(filename, key or '', owner="admin:admin")
27
28
29 def break_bridge(m):
30 # we really want to get a "not found" in the shell, not a "permission denied" (which we would get with a
31 # non-executable present file or link)
32 m.execute("""
33 mkdir -p /tmp/overlay /tmp/work
34 mount -t overlay overlay -o lowerdir=/usr/bin,upperdir=/tmp/overlay,workdir=/tmp/work /usr/bin
35 rm /usr/bin/cockpit-bridge
36 """)
37
38
39 def fix_bridge(m):
40 # umount lives in /usr, so needs a little dance for the EBUSY
41 m.execute("""
42 umount -l /usr/bin
43 while mountpoint -q /usr/bin; do sleep 0.5; done
44 rm -rf /tmp/overlay /tmp/work /tmp/umount
45 """)
46
47
48 def check_failed_state(b, expected_title):
49 b.wait_in_text('#hosts_setup_server_dialog h1', expected_title)
50 b.click("#hosts_setup_server_dialog button:contains('Close')")
51 b.wait_not_present('#hosts_setup_server_dialog')
52
53
54 def fail_login(b):
55 b.click('#hosts_setup_server_dialog button:contains("Log in")')
56 b.wait_visible('#hosts_setup_server_dialog button:contains("Log in"):not([disabled])')
57 b.wait_in_text("#hosts_setup_server_dialog .pf-v6-c-alert", "Login failed")
58
59
60 def kill_user_admin(machine):
61 machine.execute("loginctl terminate-user admin")
62
63
64 def change_ssh_port(machine, address, sshd_socket, sshd_service, port=None, timeout_sec=120):
65 try:
66 port = int(port)
67 except (ValueError, TypeError):
68 port = 22
69
70 # Keep in mind that not all operating systems have firewalld
71 machine.execute(f"firewall-cmd --permanent --zone=public --add-port={port}/tcp || true")
72 machine.execute("firewall-cmd --reload || true")
73 if machine.ostree_image: # no semanage
74 machine.execute("setenforce 0")
75 else:
76 machine.execute(f"! selinuxenabled || semanage port -a -t ssh_port_t -p tcp {port}")
77 if machine.image.startswith("ubuntu"): # always socket activated
78 machine.write("/etc/systemd/system/ssh.socket.d/override.conf",
79 f"[Socket]\nListenStream=\nListenStream=127.27.0.15:22\nListenStream={address}:{port}")
80 machine.execute("systemctl daemon-reload")
81 machine.execute("systemctl restart ssh.socket")
82 else:
83 machine.execute("sed -i 's/.*Port .*/#\\0/' /etc/ssh/sshd_config")
84 machine.write("/etc/ssh/sshd_config",
85 f"ListenAddress 127.27.0.15:22\nListenAddress {address}:{port}\n",
86 append=True)
87 # We stop the sshd.socket unit and just go with a regular
88 # daemon. This is more portable and reloading/restarting the
89 # socket doesn't seem to work well.
90 #
91 if sshd_socket:
92 machine.execute(f"systemctl stop {sshd_socket}")
93 machine.execute(f"systemctl restart {sshd_service}")
94
95 start_time = time.time()
96 error = None
97 while (time.time() - start_time) < timeout_sec:
98 try:
99 machine.execute(
100 f"ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no -o PasswordAuthentication=no -p {port} {address} 2>&1 | grep -q 'Permission denied'", quiet=True)
101 return
102 except Exception as e:
103 error = e
104 time.sleep(0.5)
105 raise error
106
107
108 @testlib.skipBeiboot("host switching disabled in beiboot mode")
109 class TestMultiMachineAdd(testlib.MachineCase):
110 provision = {
111 "machine1": {"address": "10.111.113.1/20"},
112 "machine2": {"address": "10.111.113.2/20"},
113 "machine3": {"address": "10.111.113.3/20"},
114 }
115
116 def setUp(self):
117 super().setUp()
118 self.machine2 = self.machines['machine2']
119 self.machine3 = self.machines['machine3']
120
121 # Disable preloading on all machines
122 # Preloading on machines debug build can overload the browser and cause slowness and browser crashes,
123 # and failing to load software updates breaks pixel tests in release builds
124 self.setup_provisioned_hosts(disable_preload=True)
125 self.setup_ssh_auth()
126 self.enable_multihost(self.machine)
127
128 def testBasic(self):
129 b = self.browser
130 m = self.machine
131 m2 = self.machine2
132 m3 = self.machine3
133 m3_host = "10.111.113.3:2222"
134 change_ssh_port(m3, "10.111.113.3", self.sshd_socket, self.sshd_service, port=2222)
135
136 hostname_selector = "#system_information_hostname_text"
137
138 self.login_and_go(None)
139 b.add_machine("10.111.113.2", password=None)
140 b.add_machine(m3_host, password=None, expect_warning=False)
141
142 b.switch_to_top()
143 b.click("#hosts-sel button")
144
145 kill_user_admin(m2)
146 with b.wait_timeout(30):
147 b.wait_visible("#machine2-error")
148
149 kill_user_admin(m3)
150 with b.wait_timeout(30):
151 b.wait_visible("#machine3-error")
152
153 # Navigating reconnects
154 b.click("a[href='/@10.111.113.2']")
155
156 b.wait_js_cond('window.location.pathname == "/@10.111.113.2/system"')
157 b.enter_page("/system", host="10.111.113.2")
158 b.wait_text(hostname_selector, "machine2")
159
160 b.switch_to_top()
161 b.click("#hosts-sel button")
162 b.wait_visible("a[href='/@10.111.113.2']")
163 b.wait_not_present("#machine2-error")
164
165 b.click("a[href='/@10.111.113.3']")
166
167 b.wait_js_cond('window.location.pathname == "/@10.111.113.3/system"')
168 b.enter_page("/system", host=m3_host)
169 b.wait_text(hostname_selector, "machine3")
170
171 b.switch_to_top()
172 b.click("#hosts-sel button")
173 b.wait_visible("a[href='/@10.111.113.3']")
174 b.wait_not_present("#machine3-error")
175
176 self.allow_restart_journal_messages()
177 self.allow_hostkey_messages()
178 # Might happen when killing the bridge.
179 self.allow_journal_messages("localhost: dropping message while waiting for child to exit",
180 "Received message for unknown channel: .*",
181 '.*: Socket error: disconnected',
182 ".*: error reading from ssh",
183 ".*: bridge failed: .*",
184 ".*: bridge program failed: Child process exited with code .*")
185
186 # in a remote Shell secondary session, ssh-connecting to other machines happens from primary machine
187 kill_user_admin(m3)
188 with b.wait_timeout(30):
189 b.wait_visible("#machine3-error")
190
191 b.click("a[href='/@10.111.113.2']")
192 b.wait_js_cond('window.location.pathname == "/@10.111.113.2/system"')
193 m.execute("while pgrep -af [s]sh.*10.111.113.3; do sleep 1; done", stdout=None)
194
195 b.go("/@10.111.113.2/playground/remote")
196 b.enter_page("/playground/remote", host="10.111.113.2")
197 b.set_input_text("#host", m3_host)
198 b.click("button")
199 # this works without dialog, as the SSH key get auto-unlocked with the user password
200 b.wait_text("#output", "machine3")
201
202 # the SSH connection originates from machine1, not 2
203 m.execute("pgrep -af [s]sh.*10.111.113.3", stdout=None)
204 m2.execute("! pgrep -af [s]sh.*10.111.113.3", stdout=None)
205
206 def testGlobalSSHConfig(self):
207 b = self.browser
208 m = self.machine
209 m3 = self.machine3
210
211 change_ssh_port(m3, "10.111.113.3", self.sshd_socket, self.sshd_service, port=2222)
212 m.write("/etc/ssh/ssh_config", "Host m2\n\tHostName 10.111.113.2\n", append=True)
213 m.write("/etc/ssh/ssh_config", "Host m3\n\tHostName 10.111.113.3\n\tPort 2222\n", append=True)
214
215 self.login_and_go(None)
216 b.add_machine("m2", password=None)
217 b.add_machine("m3", password=None, expect_warning=False)
218
219 b.switch_to_top()
220 b.click("#hosts-sel button")
221 b.wait_visible("a[href='/@m2']")
222 b.wait_visible("a[href='/@m3']")
223 b.wait_not_present("#page-sidebar .nav-status")
224
225 self.allow_hostkey_messages()
226
227
228 @testlib.skipBeiboot("host switching disabled in beiboot mode")
229 class TestMultiMachine(testlib.MachineCase):
230 provision = {
231 "machine1": {"address": "10.111.113.1/20"},
232 "machine2": {"address": "10.111.113.2/20"},
233 "machine3": {"address": "10.111.113.3/20"},
234 }
235
236 def setUp(self):
237 super().setUp()
238
239 self.machine2 = self.machines['machine2']
240 self.machine3 = self.machines['machine3']
241 self.allow_journal_messages("sudo: unable to resolve host machine1: .*")
242
243 # HACK: idling too long on the login page in Negotiate in one machine, while tests
244 # do things on the other times out in ws and kills cockpit-session;
245 # this should be fixed properly in ws
246 self.allow_journal_messages('.*session timed out during authentication')
247 self.allow_journal_messages('.*authentication timed out')
248
249 self.setup_provisioned_hosts(disable_preload=True)
250 self.enable_multihost(self.machine)
251
252 def checkDirectLogin(self, root='/', known_host=False):
253 b = self.browser
254 m2 = self.machine2
255 m = self.machine
256
257 hostname_selector = "#system_information_hostname_text"
258
259 # Direct to machine2, new login
260 m2.execute("echo admin:alt-password | chpasswd")
261 b.switch_to_top()
262 b.open(f"{root}=10.111.113.2")
263 b.wait_visible("#login")
264 b.wait_visible("#server-name")
265 b.wait_not_visible("#badge")
266 b.wait_not_visible("#brand")
267 b.wait_in_text("#server-name", "10.111.113.2")
268 b.wait_val("#server-field", "10.111.113.2")
269 b.set_input_text("#login-user-input", "admin")
270 b.set_input_text("#login-password-input", "alt-password")
271 b.click('#login-button')
272 if not known_host:
273 b.wait_in_text("#hostkey-message-1", "10.111.113.2")
274 match = re.match(r'\((?:ssh-)?([^-]*).*\)', b.text("#hostkey-type"))
275 self.assertIsNotNone(match)
276 algo = match.groups()[0]
277 # This assumes that all fingerprints use SHA256.
278 line = m2.execute(f"ssh-keygen -l -E SHA256 -f /etc/ssh/ssh_host_{algo.lower()}_key.pub", quiet=True)
279 fp = line.split(" ")[1]
280 self.assertEqual(b.text('#hostkey-fingerprint'), fp)
281 b.click('#login-button')
282
283 b.enter_page("/system")
284 b.wait_in_text(hostname_selector, "machine2")
285 b.switch_to_top()
286
287 b.wait_js_cond(f'window.location.pathname == "{root}=10.111.113.2/system"')
288 b.wait_in_text("#hosts-sel", "machine2")
289
290 # This is a primary session, we can also ssh onwards to other machines
291
292 b.go("/playground/remote")
293 b.enter_page("/playground/remote")
294 b.set_input_text("#host", "10.111.113.3")
295 b.click("button")
296 b.wait_text("#ssh-unknown-host-dialog .pf-v6-c-modal-box__title-text", "Unknown host: 10.111.113.3")
297 b.click("#ssh-unknown-host-dialog button.pf-m-primary")
298 b.wait_not_present("#ssh-unknown-host-dialog")
299
300 b.wait_text("#ssh-change-auth-dialog .pf-v6-c-modal-box__title-text", "Log in to 10.111.113.3")
301 b.set_input_text("#login-custom-password", "foobar")
302 b.click("#ssh-change-auth-dialog button.pf-m-primary")
303 b.wait_not_present("#ssh-change-auth-dialog")
304 b.wait_text("#output", "machine3")
305
306 # the SSH connection originates from machine2, not 1
307 m2.execute("pgrep -af [s]sh.*10.111.113.3", stdout=None)
308 m.execute("! pgrep -af [s]sh.*10.111.113.3", stdout=None)
309
310 b.logout()
311
312 # Bad host key
313 b.eval_js("""window.localStorage.setItem("known_hosts", '{}')""")
314 m.write("/etc/ssh/ssh_known_hosts", "10.111.113.2 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDgPMmTosSQ4NxMtq+aL2NKLC+W4I9/jbD1e74cnOKTW")
315 b.open(f"{root}=10.111.113.2")
316 b.wait_visible("#login")
317 b.set_input_text("#login-user-input", "admin")
318 b.set_input_text("#login-password-input", "alt-password")
319 b.click('#login-button')
320
321 b.wait_not_visible("#conversation-group")
322 b.wait_visible("#password-group")
323 b.wait_visible("#user-group")
324 b.wait_visible("#option-group")
325 b.wait_visible("#server-group")
326 b.wait_text("#login-error-title", "Refusing to connect")
327 b.wait_text("#login-error-message", "Hostkey does not match")
328
329 # Clear bad host key in /etc and set bad host key in
330 # localStorage.
331 m.write("/etc/ssh/ssh_known_hosts", "")
332 b.eval_js("""window.localStorage.setItem("known_hosts", '{"10.111.113.2":"BAD"}')""")
333 b.set_input_text("#login-user-input", "admin")
334 b.set_input_text("#login-password-input", "alt-password")
335 b.click('#login-button')
336 b.wait_visible("#hostkey-group")
337 b.wait_in_text("#hostkey-title", "10.111.113.2 key changed")
338 b.click('#login-button')
339
340 b.enter_page("/system")
341 b.wait_in_text(hostname_selector, "machine2")
342 b.logout()
343
344 # Clear localStorage and set correct host key in /etc
345 b.eval_js("""window.localStorage.setItem("known_hosts", '{}')""")
346 m.execute("ssh-keyscan 10.111.113.2 > /etc/ssh/ssh_known_hosts")
347 b.set_input_text("#login-user-input", "admin")
348 b.set_input_text("#login-password-input", "alt-password")
349 b.click('#login-button')
350 b.enter_page("/system")
351 b.wait_in_text(hostname_selector, "machine2")
352 b.logout()
353
354 # beiboot mode: same OS → compatible, supported
355 break_bridge(m2)
356 b.try_login(password="alt-password")
357 b.wait_visible('#content')
358 b.logout()
359
360 # beiboot mode: known remote OS; m2 runs TEST_OS, so we cover all supported ones in our testmap
361 # damage local os-release to avoid the "same unknown OS" code path
362 m.execute('''sed -i '/^ID=/ s/=.*/=testux/; /^PLATFORM_ID/d; /^VERSION_ID=/ s/=.*$/="0815"/' '''
363 "/etc/os-release")
364 # rolling OSes don't have a VERSION_ID
365 if m.image in ["arch", "debian-testing"]:
366 m.execute('''echo 'VERSION_ID="0815"' >> /etc/os-release''')
367 b.try_login(password="alt-password")
368 b.wait_visible('#content')
369 b.logout()
370
371 # beiboot mode: different OS version → incompatible, not supported
372 if m2.image in ["arch", "debian-testing"]:
373 # rolling OSes don't have a VERSION_ID
374 m2.execute('''echo 'VERSION_ID="0815"' >> /etc/os-release''')
375 else:
376 m2.execute('''sed -i '/^VERSION_ID/ s/=.*$/="0815"/; /^PLATFORM_ID/d' /etc/os-release''')
377 b.try_login(password="alt-password")
378 b.wait_text('#login-error-title', "Packageless session unavailable")
379 b.wait_in_text('#login-error-message', "Install the cockpit-system package")
380 b.wait_in_text('#login-error-message', "on 10.111.113.2")
381
382 # beiboot mode: local and remote OS are unknown but the same
383 m2.execute('''sed -i '/^ID=/ s/=.*/=testux/' /etc/os-release''')
384 b.try_login(password="alt-password")
385 b.wait_visible('#content')
386 b.logout()
387
388 fix_bridge(m2)
389
390 login_options = '#show-other-login-options'
391
392 # Connect to bad machine
393 b.open(f"{root}other")
394 b.set_input_text("#login-user-input", "admin")
395 b.set_input_text("#login-password-input", "bad-password")
396 b.click(login_options)
397 b.wait_visible("#server-group")
398 b.set_input_text("#server-field", "bad")
399 b.click(login_options)
400 b.wait_not_visible("#server-group")
401 b.click('#login-button')
402 b.wait_visible("#server-group")
403 b.wait_text("#login-error-title", "Refusing to connect")
404 b.wait_text("#login-error-message", "Host is unknown")
405
406 # Might happen when we switch away.
407 self.allow_hostkey_messages()
408 self.allow_journal_messages(".* Failed to resolve hostname bad .*")
409
410 def testDirectLogin(self):
411 self.machine.start_cockpit()
412 self.checkDirectLogin('/')
413
414 # Also check whether AllowMultiHost blocks access already in cockpit-ws.
415
416 b = self.browser
417 m = self.machine
418
419 self.enable_multihost(m)
420 self.setup_ssh_auth()
421
422 # login into cockpit on machine1
423 self.login_and_go("/system")
424 cookie = b.cookie("cockpit")
425
426 def http_code(url):
427 return int(subprocess.check_output(["curl",
428 "--silent",
429 "-b", f"cockpit={cookie['value']}",
430 "-o", "/dev/null", "-w", "%{http_code" + "}",
431 f"http://{b.address}:{b.port}{url}"]))
432
433 # Now we can get resources from machine1 and machine2
434 self.assertEqual(200, http_code("/cockpit/@localhost/manifests.json"))
435 self.assertEqual(200, http_code("/cockpit/@10.111.113.2/manifests.json"))
436
437 # But not when AllowMultiHost is false
438 m.write("/etc/cockpit/cockpit.conf",
439 '[WebService]\nAllowMultiHost=no\n')
440 m.restart_cockpit()
441 b.relogin("/system")
442 cookie = b.cookie("cockpit")
443
444 self.assertEqual(200, http_code("/cockpit/@localhost/manifests.json"))
445 self.assertEqual(403, http_code("/cockpit/@10.111.113.2/manifests.json"))
446
447 def testUrlRoot(self):
448 b = self.browser
449 m = self.machine
450
451 hostname_selector = "#system_information_hostname_text"
452
453 m.write("/etc/cockpit/cockpit.conf", "[WebService]\nUrlRoot = cockpit-new\nAllowMultiHost=yes\n")
454 m.start_cockpit()
455
456 # Make sure normal urls don't work.
457 output = m.execute('curl -s -o /dev/null -w "%{http_code}" http://localhost:9090/cockpit/socket')
458 self.assertIn('404', output)
459
460 output = m.execute('curl -s -o /dev/null -w "%{http_code}" http://localhost:9090/cockpit/socket')
461 self.assertIn('404', output)
462
463 b.open("/cockpit-new/system")
464 b.wait_visible("#login")
465 b.set_input_text("#login-user-input", "admin")
466 b.set_input_text("#login-password-input", "foobar")
467 b.click('#login-button')
468 b.enter_page("/system")
469 b.switch_to_top()
470 b.wait_js_cond('window.location.pathname == "/cockpit-new/system"')
471
472 # Test 2nd machine
473 b.add_machine("10.111.113.2")
474 b.enter_page("/system", host="10.111.113.2")
475 b.wait_text(hostname_selector, "machine2")
476 b.switch_to_top()
477 b.wait_js_cond('window.location.pathname == "/cockpit-new/@10.111.113.2/system"')
478
479 # Test subnav
480 b.click_system_menu("/cockpit-new/@10.111.113.2/users", enter=False)
481 b.enter_page("/users", host="10.111.113.2")
482 b.click('#accounts-list td[data-label="Username"] a[href="#/admin"]')
483 b.wait_text("#account-user-name", "admin")
484 b.switch_to_top()
485 b.wait_js_cond('window.location.pathname == "/cockpit-new/@10.111.113.2/users"')
486 b.wait_js_cond('window.location.hash == "#/admin"')
487
488 b.logout()
489 self.checkDirectLogin('/cockpit-new/')
490 self.allow_hostkey_messages()
491
492 def testUrlRootWithQuery(self):
493 b = self.browser
494 m = self.machine
495
496 m.write("/etc/cockpit/cockpit.conf", "[WebService]\nUrlRoot = cockpit-new")
497 m.start_cockpit()
498
499 b.open("/cockpit-new/system?access_token=XXXX")
500 b.wait_visible("#login")
501 b.set_input_text("#login-user-input", "admin")
502 b.set_input_text("#login-password-input", "foobar")
503 b.click('#login-button')
504 b.enter_page("/system")
505 b.switch_to_top()
506 b.wait_js_cond('window.location.pathname == "/cockpit-new/system"')
507
508 def testExternalPage(self):
509 b = self.browser
510 m1 = self.machine
511 m2 = self.machine2
512
513 # Modify the terminals to be different on the two machines.
514 for machine, name in zip((m1, m2), ('m1', 'm2'), strict=True):
515 # This page may be either compressed or uncompressed
516 machine.execute(f"""
517 FILENAME=/usr/share/cockpit/systemd/terminal.html*
518 cp $FILENAME /tmp
519 test -f /tmp/terminal.html || gzip -d /tmp/terminal.html.gz
520 sed -ie 's|</body>|magic-{name}-token</body>|' /tmp/terminal.html
521 gzip < /tmp/terminal.html > /tmp/terminal.html.gz
522 mount -o bind /tmp/"$(basename $FILENAME)" $FILENAME""")
523
524 self.login_and_go("/system")
525 b.add_machine("10.111.113.2")
526
527 b.leave_page()
528 b.go("/@10.111.113.2/system/terminal")
529 b.enter_page("/system/terminal", host="10.111.113.2")
530 b.wait_in_text("body", "magic-m2-token")
531
532 b.leave_page()
533 b.go("/@localhost/system/terminal")
534 b.enter_page("/system/terminal")
535 b.wait_in_text("body", "magic-m1-token")
536
537 self.allow_hostkey_messages()
538
539 def testFrameNavigation(self):
540 b = self.browser
541 m2 = self.machine2
542
543 m2_path = "/@10.111.113.2/playground/test"
544
545 # Add a machine
546 self.login_and_go(None)
547 b.add_machine("10.111.113.2")
548
549 # Go to the path, remove the image
550 b.go(m2_path)
551 b.enter_page("/playground/test", "10.111.113.2")
552 b.click("img[src='hammer.gif']")
553 b.switch_to_top()
554
555 # kill admin, lock account
556 m2.execute('passwd -l admin')
557 kill_user_admin(m2)
558
559 with b.wait_timeout(30):
560 b.wait_text(".curtains-ct h1", "Not connected to host")
561 b.wait_text("#machine-reconnect", "Reconnect")
562
563 b.click("#hosts-sel button")
564 b.wait_visible("a[href='/@10.111.113.2']")
565 b.wait_visible("#machine2-error")
566 b.go("/system")
567 b.enter_page("/system")
568 b.wait_in_text("#system_information_hostname_text", "machine1")
569 b.switch_to_top()
570
571 # navigating there again will fail
572 b.go(m2_path)
573 with b.wait_timeout(30):
574 b.wait_visible("#hosts_setup_server_dialog")
575 b.start_machine_troubleshoot(expect_curtain=False, password="foobar",
576 expect_closed_dialog=False, expect_warning=False)
577 b.wait_in_text("#hosts_setup_server_dialog .pf-v6-c-alert", "Login failed")
578 b.click("#hosts_setup_server_dialog button:contains('Cancel')")
579
580 # wait for system to load
581 b.go("/system")
582 b.enter_page("/system")
583 b.wait_in_text("#system_information_hostname_text", "machine1")
584 b.switch_to_top()
585
586 # re-enable admin
587 m2.execute('passwd -u admin')
588
589 # path should reconnect at this point
590 b.reload()
591 b.go(m2_path)
592 with b.wait_timeout(30):
593 b.wait_visible("#hosts_setup_server_dialog")
594 b.start_machine_troubleshoot(expect_curtain=False, password="foobar", expect_warning=False)
595
596 b.enter_page("/playground/test", "10.111.113.2", reconnect=True)
597 # image is back because it page was reloaded after disconnection
598 b.wait_visible("img[src='hammer.gif']")
599 b.switch_to_top()
600
601 # Host shows it is up
602 b.click("#hosts-sel button")
603 b.wait_visible("a[href='/@10.111.113.2']")
604 b.wait_not_present("#page-sidebar .nav-status")
605
606 # Bad host also bounces
607 b.go("/@10.0.0.0/playground/test")
608 with b.wait_timeout(30):
609 b.wait_text(".curtains-ct h1", "Not connected to host")
610 self.assertEqual(b.text(".curtains-ct .pf-v6-c-empty-state__body"), "Cannot connect to an unknown host")
611
612 self.allow_hostkey_messages()
613 # Might happen when killing the bridge.
614 self.allow_journal_messages("localhost: dropping message while waiting for child to exit",
615 "Received message for unknown channel: .*",
616 '.*: Socket error: disconnected',
617 ".*: error reading from ssh",
618 ".*: bridge failed: .*",
619 ".*: bridge program failed: Child process exited with code .*",
620 "/playground/test.html: failed to retrieve resource: authentication-failed")
621
622 def testFrameReload(self):
623 b = self.browser
624 m2 = self.machines["machine2"]
625
626 frame = "cockpit1:10.111.113.2/playground/test"
627 m2_path = "/@10.111.113.2/playground/test"
628
629 # Add a machine
630 self.login_and_go(None)
631 b.add_machine("10.111.113.2")
632
633 b.switch_to_top()
634 b.go(m2_path)
635 b.enter_page("/playground/test", "10.111.113.2")
636
637 # Check that channels are closed when a frame is reloaded.
638
639 # Lock a file on m2, that keeps a channel open.
640 m2.execute("touch /tmp/playground-test-lock")
641 b.click(".lock-channel button")
642 b.wait_in_text(".lock-channel span", 'locked')
643 m2.execute("! flock --nonblock /tmp/playground-test-lock true")
644
645 # load the same page on m1
646 b.switch_to_top()
647 b.go("/@localhost/playground/test")
648 b.enter_page("/playground/test")
649 b.wait_text('#file-content', "0")
650
651 # Now reload the m2 frame by kicking its "src" attribute.
652 b.go(m2_path)
653 b.enter_page("/playground/test", "10.111.113.2")
654 b.wait_in_text(".lock-channel span", 'locked')
655 b.switch_to_top()
656 b.eval_js('ph_set_attr("iframe[name=\'%s\']", "src", "../playground/test.html?i=1#/")' % frame)
657
658 # The channel and the lock should have gone away
659 m2.execute("flock --timeout 10 /tmp/playground-test-lock true")
660
661 b.enter_page("/playground/test", "10.111.113.2")
662 b.wait_not_in_text(".lock-channel span", 'locked')
663
664 self.allow_hostkey_messages()
665
666 def testTroubleshooting(self):
667 b = self.browser
668 m1 = self.machine
669 m2 = self.machine2
670
671 # Logging in as root is no longer allowed by default by sshd
672 m2.execute("sed -ri 's/#?PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config")
673 m2.execute(self.restart_sshd)
674
675 machine_path = "/@10.111.113.2"
676
677 self.login_and_go(None)
678
679 # Troubleshoot while adding
680 b.go(machine_path)
681
682 # Bad hostkey
683 break_hostkey(m1, "10.111.113.2")
684 b.start_machine_troubleshoot(new=True, known_host=True, expect_closed_dialog=False)
685 b.wait_in_text('#hosts_setup_server_dialog', "10.111.113.2 key changed")
686 b.click("#hosts_setup_server_dialog button:contains('Cancel')")
687 b.wait_not_present('#hosts_setup_server_dialog')
688 fix_hostkey(m1)
689
690 # Host key path is correct
691 m1.execute("mkdir -p /home/admin/.ssh/")
692 break_hostkey(m1, "10.111.113.2")
693
694 b.start_machine_troubleshoot(new=True, known_host=True, expect_closed_dialog=False, expect_warning=False)
695 b.wait_in_text('#hosts_setup_server_dialog', "10.111.113.2 key changed")
696 b.click("#hosts_setup_server_dialog button:contains('Cancel')")
697 fix_hostkey(m1)
698
699 # Bad cockpit
700 break_bridge(m2)
701 b.start_machine_troubleshoot(new=True, password="foobar", expect_closed_dialog=False, expect_warning=False)
702 check_failed_state(b, "Cockpit is not installed")
703 fix_bridge(m2)
704
705 # Troubleshoot existing
706 # Properly add machine
707 fix_hostkey(m1)
708 b.add_machine("10.111.113.2", expect_warning=False)
709 b.logout()
710 b.wait_visible("#login")
711
712 # Bad cockpit
713 break_bridge(m2)
714 self.login_and_go(None)
715 b.go(machine_path)
716 with b.wait_timeout(20):
717 b.start_machine_troubleshoot(expect_curtain=False, password="foobar", expect_closed_dialog=False)
718
719 check_failed_state(b, "Cockpit is not installed")
720 b.wait_visible("#machine-reconnect")
721 fix_bridge(m2)
722
723 # Clear host key
724 fix_hostkey(m1)
725 b.click("#machine-reconnect")
726 b.start_machine_troubleshoot(expect_curtain=False, expect_closed_dialog=False, expect_warning=False)
727 b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.")
728
729 # show fingerprint validation
730 b.wait_not_visible("#hosts_setup_server_dialog .hostkey-verify-help-cmds")
731 b.click("#hosts_setup_server_dialog .pf-v6-c-expandable-section__toggle button")
732 b.wait_visible("#hosts_setup_server_dialog .hostkey-verify-help-cmds")
733 # run validation command
734 cmd = b.val("#hosts_setup_server_dialog .hostkey-verify-help-cmds input")
735 self.assertIn("ssh-keyscan", cmd)
736 fingerprint = m2.execute(cmd).strip()
737 # there is some additional noise around it, like the OpenSSH version and host name
738 self.assertIn(b.val("#hosts_setup_server_dialog .hostkey-fingerprint input"), fingerprint)
739
740 b.click("#hosts_setup_server_dialog button:contains('Trust and add host')")
741 b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
742 b.set_input_text('#login-custom-password', "foobar")
743 # Submit the dialog with Enter instead of clicking the Login button, for variety
744 b.focus("#login-custom-password")
745 b.key("Enter")
746 b.wait_not_present('#hosts_setup_server_dialog')
747
748 # Reconnect
749 b.wait_not_present(".curtains-ct")
750 b.enter_page('/system', "10.111.113.2")
751
752 b.logout()
753 b.wait_visible("#login")
754
755 # Break auth
756 m2.execute("echo admin:alt-password | chpasswd")
757 self.login_and_go(None)
758 b.go(machine_path)
759
760 with b.wait_timeout(20):
761 b.wait_visible("#hosts_connect_server_dialog")
762 b.start_machine_troubleshoot(expect_curtain=False, expect_closed_dialog=False)
763 b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
764 b.set_input_text("#login-custom-password", "bad")
765 fail_login(b)
766 b.set_input_text("#login-custom-password", "alt-password")
767 b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
768 b.wait_not_present('#hosts_setup_server_dialog')
769
770 # Reconnect
771 b.wait_not_present(".curtains-ct")
772 b.enter_page('/system', "10.111.113.2")
773 b.logout()
774 b.wait_visible("#login")
775
776 change_ssh_port(m2, "10.111.113.2", self.sshd_socket, self.sshd_service, port=2222)
777 m2.disconnect()
778 del self.machines["machine2"] # No more access to m2
779
780 self.login_and_go(None)
781 b.go(machine_path)
782 with b.wait_timeout(20):
783 b.wait_visible("#hosts_connect_server_dialog")
784 b.start_machine_troubleshoot(expect_curtain=False, expect_closed_dialog=False)
785 b.wait_in_text('#hosts_setup_server_dialog', "Unable to contact")
786 b.set_input_text("#add-machine-address", "10.111.113.2:2222")
787 b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
788 # ssh(1) tracks known hosts by name/IP, not by port; so not expecting a host key prompt here
789 b.wait_in_text('#hosts_setup_server_dialog h1', "Log in to")
790 b.set_input_text("#login-custom-password", "alt-password")
791 b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}')
792 b.wait_not_present('#hosts_setup_server_dialog')
793
794 b.wait_not_present(".curtains-ct")
795 b.enter_page('/system', "10.111.113.2:2222")
796 b.logout()
797
798 self.allow_hostkey_messages()
799 self.allow_journal_messages('.* couldn\'t connect: .*',
800 '.* failed to retrieve resource: invalid-hostkey',
801 '.* host key for server has changed to: .*',
802 '.* spawning remote bridge failed .*',
803 '.*: bridge failed: .*',
804 '.*: cockpit-bridge: command not found',
805 '.*: received truncated .*',
806 '.*: Socket error: disconnected',
807 '.*: host key for this server changed key type: .*',
808 '.*: server offered unsupported authentication methods: .*')
809
810 @testlib.skipImage("TODO: Broken on Arch Linux", "arch")
811 def testSshKeySetup(self):
812 b = self.browser
813 m1 = self.machine
814 m2 = self.machine2
815
816 # Let's not use "admin" on the remote machine. Creating a
817 # dedicated user gives us a guaranteed clean slate and also
818 # tests more code paths.
819
820 m2.execute("useradd -m fred")
821 m2.execute("echo fred:foobar | chpasswd")
822
823 self.login_and_go(None)
824 b.go("/@10.111.113.2")
825 b.click('#machine-troubleshoot')
826 b.wait_visible('#hosts_setup_server_dialog')
827 b.wait_in_text('#hosts_setup_server_dialog', "new host")
828 b.set_input_text('#add-machine-user', "fred")
829 b.click('#hosts_setup_server_dialog button:contains("Add")')
830 b.wait_visible('#hosts_connect_server_dialog')
831 b.click("#hosts_connect_server_dialog button.pf-m-warning")
832 b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.")
833 b.click("#hosts_setup_server_dialog button:contains('Trust and add host')")
834 b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
835
836 # There is no key yet. Create and authorize it.
837
838 m1.execute("! test -f /home/admin/.ssh/id_rsa")
839 m2.execute("! test -f /home/fred/.ssh/authorized_keys")
840
841 b.wait_in_text("#hosts_setup_server_dialog", "Create a new SSH key and authorize it")
842 b.set_input_text('#login-custom-password', "foobar")
843 b.set_checked("#login-setup-keys", val=True)
844 # Leave passphrase empty on Coreos, since it can't load keys into the agent
845 if not m1.ostree_image:
846 b.set_input_text('#hosts_setup_server_dialog #login-setup-new-key-password', "foobar")
847 b.set_input_text('#hosts_setup_server_dialog #login-setup-new-key-password2', "foobar")
848 b.click('#hosts_setup_server_dialog button:contains("Log in")')
849 with b.wait_timeout(30):
850 b.wait_not_present('#hosts_setup_server_dialog')
851
852 b.enter_page("/system", host="[email protected]")
853 m1.execute("test -f /home/admin/.ssh/id_rsa; test -f /home/admin/.ssh/id_rsa.pub")
854 self.assertEqual(m1.execute("cat /home/admin/.ssh/id_rsa.pub"),
855 m2.execute("cat /home/fred/.ssh/authorized_keys"))
856
857 # Relogin. This should now work seamlessly (except for the warning).
858 b.relogin(None, wait_remote_session_machine=m1)
859 b.wait_visible('#hosts_connect_server_dialog')
860 b.click("#hosts_connect_server_dialog button.pf-m-warning")
861 b.enter_page("/system", host="[email protected]")
862
863 # De-authorize key and relogin, then re-authorize.
864 m2.execute("rm /home/fred/.ssh/authorized_keys")
865 b.relogin(None, wait_remote_session_machine=m1)
866 b.wait_visible('#hosts_connect_server_dialog')
867 b.click("#hosts_connect_server_dialog button.pf-m-warning")
868 b.wait_visible('#hosts_setup_server_dialog')
869 b.wait_in_text('#hosts_setup_server_dialog', "Unable to log in")
870 b.wait_in_text("#hosts_setup_server_dialog", "Authorize SSH key")
871 b.set_checked("#login-setup-keys", val=True)
872 b.set_input_text('#login-custom-password', "foobar")
873 b.click('#hosts_setup_server_dialog button:contains("Log in")')
874 b.wait_not_present('#hosts_setup_server_dialog')
875 b.enter_page("/system", host="[email protected]")
876 self.assertEqual(m1.execute("cat /home/admin/.ssh/id_rsa.pub"),
877 m2.execute("cat /home/fred/.ssh/authorized_keys"))
878
879 # Put a 'better' passphrase on the key and relogin, then
880 # change the passphrase back to the login password
881 m1.execute("ssh-keygen -q -f /home/admin/.ssh/id_rsa -p -P foobar -N foobarfoo")
882 b.relogin(None, wait_remote_session_machine=m1)
883 b.wait_visible('#hosts_connect_server_dialog')
884 b.click("#hosts_connect_server_dialog button.pf-m-warning")
885 b.wait_visible('#hosts_setup_server_dialog')
886 b.wait_in_text('#hosts_setup_server_dialog', "The SSH key for logging in")
887 b.set_checked('#hosts_setup_server_dialog input[value=key]', val=True)
888 b.set_input_text('#hosts_setup_server_dialog #locked-identity-password', "foobarfoo")
889 b.set_checked("#login-setup-keys", val=True)
890 b.set_input_text('#hosts_setup_server_dialog #login-setup-new-key-password', "foobar")
891 b.set_input_text('#hosts_setup_server_dialog #login-setup-new-key-password2', "foobar")
892 b.click('#hosts_setup_server_dialog button:contains("Log in")')
893 b.wait_not_present('#hosts_setup_server_dialog')
894 b.enter_page("/system", host="[email protected]")
895
896 # Relogin. This should now work seamlessly (except on OSTree images
897 # which don't have pam-ssh-add in its PAM stack.)
898 if not m1.ostree_image:
899 b.relogin(None, wait_remote_session_machine=m1)
900 b.wait_visible('#hosts_connect_server_dialog')
901 b.click("#hosts_connect_server_dialog button.pf-m-warning")
902 b.enter_page("/system", host="[email protected]")
903
904 # The authorized_keys files should still only have a single key
905 self.assertEqual(m1.execute("cat /home/admin/.ssh/id_rsa.pub"),
906 m2.execute("cat /home/fred/.ssh/authorized_keys"))
907
908 self.allow_hostkey_messages()
909
910 def testSshKeySetupCustom(self):
911 b = self.browser
912 m1 = self.machine
913 m2 = self.machine2
914
915 # This tests how the ssh key setup reacts to a already
916 # existing configuration involving a custom key with a
917 # passphrase.
918
919 m1.execute("d=/home/admin/.ssh; mkdir -p $d; chown admin:admin $d; chmod 700 $d")
920 m1.execute("ssh-keygen -f /home/admin/.ssh/id_local -t rsa -N 'foobar'")
921 m1.execute("chown admin:admin /home/admin/.ssh/id_local*")
922 m1.write("/home/admin/.ssh/config", "Host 10.111.113.2\n IdentityFile /home/admin/.ssh/id_local\n")
923 pubkey = self.machine.execute("cat /home/admin/.ssh/id_local.pub")
924
925 m2.execute("d=/home/admin/.ssh; mkdir -p $d; chown admin:admin $d; chmod 700 $d")
926 m2.write("/home/admin/.ssh/authorized_keys", pubkey)
927 m2.execute("chown admin:admin /home/admin/.ssh/authorized_keys")
928
929 self.login_and_go(None)
930 b.go("/@10.111.113.2")
931 b.click('#machine-troubleshoot')
932 b.wait_visible('#hosts_setup_server_dialog')
933 b.click('#hosts_setup_server_dialog button:contains("Add")')
934 b.wait_visible('#hosts_connect_server_dialog')
935 b.click('#hosts_connect_server_dialog button:contains("Connect")')
936 b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.")
937 b.click("#hosts_setup_server_dialog button:contains('Trust and add host')")
938 b.wait_in_text('#hosts_setup_server_dialog', "The SSH key")
939 b.wait_not_present('.password-change-advice')
940 b.wait_not_present('.login-setup-auto')
941
942 self.allow_hostkey_messages()
943
944
945 if __name__ == '__main__':
946 testlib.test_main()