Debian Codegraph @

test/verify/check-ssh-api

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) 2024 Red Hat, Inc.
5 # SPDX-License-Identifier: LGPL-2.1-or-later
6
7
8 import testlib
9
10 KEY_PASSWORD = "ssh#geheim"
11 BAD_FINGERPRINT = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJqfgO2FPiix1n2sCJCXbaffwog1Vvi3zRdmcAxG//5T"
12
13
14 @testlib.nondestructive
15 class TestSshDialog(testlib.MachineCase):
16 def setUp(self):
17 super().setUp()
18 # test with separate users so that we can just wipe their home dirs
19 self.password = "userpwd"
20 self.machine.execute(f"useradd --create-home user; echo user:{self.password} | chpasswd")
21 self.restore_dir("/home/user")
22
23 self.other_password = "otherpwd"
24 self.machine.execute(f"useradd --create-home otheruser; echo otheruser:{self.other_password} | chpasswd")
25 self.restore_dir("/home/otheruser")
26
27 def disconnect(self):
28 # HACK: there's no UI way to disconnect; take care to not kill the main
29 # session connection with the ws container
30 self.machine.execute("pkill -ef [s]sh.*ferny.*127.0.0.1")
31
32 @testlib.skipImage("FIXME: Assertion failed: UnknownHostDialog needs a host-key and host-fingerprint in error", "rhel-8-10")
33 @testlib.skipImage("TODO: fix for OpenSUSE", "opensuse-tumbleweed")
34 def testPassword(self):
35 m = self.machine
36 b = self.browser
37
38 self.login_and_go("/playground/remote", user="user", password=self.password, superuser=False)
39
40 # nonexisting host/port
41 b.set_input_text("#host", "localhost:987")
42 b.click("button")
43 b.wait_in_text("#error", "localhost port 987")
44 b.wait_in_text("#error", '"problem":"no-host"')
45
46 # previously unknown host
47 b.set_input_text("#host", "[email protected]")
48 b.click("button")
49 b.wait_text("#ssh-unknown-host-dialog .pf-v6-c-modal-box__title-text", "Unknown host: 127.0.0.1")
50
51 # cancel dialog with footer button
52 b.click("#ssh-unknown-host-dialog button.btn-cancel")
53 b.wait_not_present("#ssh-unknown-host-dialog")
54 b.wait_text("#error", '"cancel"')
55
56 # cancel dialog with X button
57 b.click("button")
58 b.click("#ssh-unknown-host-dialog button[aria-label=Close]")
59 b.wait_not_present("#ssh-unknown-host-dialog")
60 b.wait_text("#error", '"cancel"')
61
62 # try again
63 b.click("button")
64 b.wait_in_text("#ssh-unknown-host-dialog", "You are connecting to 127.0.0.1 for the first time.")
65 b.click("#ssh-unknown-host-dialog .pf-v6-c-expandable-section__toggle button")
66 b.wait_visible("#ssh-unknown-host-dialog .hostkey-verify-help-cmds input")
67 b.assert_pixels("#ssh-unknown-host-dialog", "unknown-host", ignore=[".hostkey-fingerprint"])
68 command = b.val("#ssh-unknown-host-dialog .hostkey-verify-help-cmds input")
69 self.assertIn("ssh-keyscan", command)
70 # run the command to get the fingerprint
71 real_fp = m.execute(command).split()[1]
72 shown_fp = b.val("#ssh-unknown-host-dialog .hostkey-fingerprint input")
73 self.assertEqual(real_fp, shown_fp)
74 # Trust and add host
75 b.click("#ssh-unknown-host-dialog button.pf-m-primary")
76 b.wait_not_present("#ssh-unknown-host-dialog")
77
78 self.assertIn("127.0.0.1", m.execute("cat /home/user/.ssh/known_hosts"))
79
80 # user has no SSH key, so only offers password
81 b.wait_text("#ssh-change-auth-dialog .pf-v6-c-modal-box__title-text", "Log in to 127.0.0.1")
82 b.wait_in_text("#ssh-change-auth-dialog", "Unable to log in to [email protected] using SSH key authentication")
83 b.assert_pixels("#ssh-change-auth-dialog", "change-auth")
84 # empty password
85 b.click("#ssh-change-auth-dialog button.pf-m-primary")
86 b.wait_in_text("#ssh-change-auth-dialog", "The password can not be empty")
87 b.wait_attr("#login-custom-password", "aria-invalid", "true")
88
89 # wrong password
90 b.set_input_text("#login-custom-password", "wrong")
91 b.click("#ssh-change-auth-dialog button.pf-m-primary")
92 b.wait_in_text("#ssh-change-auth-dialog .pf-v6-c-alert", "Login failed")
93 # right password
94 b.set_input_text("#login-custom-password", self.password)
95 b.click("#ssh-change-auth-dialog button.pf-m-primary")
96 b.wait_not_present("#ssh-change-auth-dialog")
97
98 # `hostname` is the default command
99 b.wait_text("#output", m.execute("hostname").strip())
100 b.wait_text("#error", "")
101
102 # now that the connection is made, further channels just work
103 b.set_input_text("#command", "cat /etc/os-release")
104 b.click("button")
105 b.wait_text("#output", m.execute("cat /etc/os-release").strip())
106 b.wait_text("#error", "")
107
108 # changing the port counts as a new channel
109 b.set_input_text("#command", "echo hello")
110 # also leave out the user@, it should default to the currently logged in user
111 b.set_input_text("#host", "127.0.0.1:22")
112 b.click("button")
113 # known host, directly to password login
114 b.wait_in_text("#ssh-change-auth-dialog", "Unable to log in to 127.0.0.1:22 using SSH key authentication")
115 # use Enter key to close the dialog instead of the button
116 b.set_input_text("#login-custom-password", self.password, blur=False)
117 b.key("Enter")
118 b.wait_not_present("#ssh-change-auth-dialog")
119
120 b.wait_text("#output", "hello")
121 b.wait_text("#error", "")
122
123 self.disconnect()
124
125 # change the host key
126 m.write("/home/user/.ssh/known_hosts", f"127.0.0.1 {BAD_FINGERPRINT}")
127 b.set_input_text("#command", "echo newkey")
128 b.click("button")
129
130 b.wait_text("#ssh-unknown-host-dialog .pf-v6-c-modal-box__title-text", "127.0.0.1 key changed")
131 b.wait_val("#ssh-unknown-host-dialog .hostkey-fingerprint input", shown_fp)
132 b.click("#ssh-unknown-host-dialog button.pf-m-primary")
133 b.wait_not_present("#ssh-unknown-host-dialog")
134
135 b.set_input_text("#login-custom-password", self.password)
136 b.click("#ssh-change-auth-dialog button.pf-m-primary")
137 b.wait_not_present("#ssh-change-auth-dialog")
138
139 b.wait_text("#output", "newkey")
140 b.wait_text("#error", "")
141
142 # explicit user field
143 b.set_input_text("#host", "127.0.0.1")
144 b.set_input_text("#user", "otheruser")
145 b.set_input_text("#command", "whoami")
146 b.click("button")
147 b.set_input_text("#login-custom-password", self.other_password)
148 b.click("#ssh-change-auth-dialog button.pf-m-primary")
149 b.wait_not_present("#ssh-change-auth-dialog")
150 b.wait_text("#output", "otheruser")
151 b.wait_text("#error", "")
152 self.disconnect()
153
154 # explicit user field overrides the one from host
155 b.set_input_text("#host", "[email protected]")
156 b.set_input_text("#user", "otheruser")
157 b.set_input_text("#command", "whoami")
158 b.click("button")
159 b.wait_in_text("#ssh-change-auth-dialog", "Unable to log in to [email protected] using SSH key authentication")
160 b.set_input_text("#login-custom-password", self.other_password)
161 b.click("#ssh-change-auth-dialog button.pf-m-primary")
162 b.wait_not_present("#ssh-change-auth-dialog")
163 b.wait_text("#output", "otheruser")
164 b.wait_text("#error", "")
165 self.disconnect()
166
167 # no-cockpit
168 b.set_input_text("#host", "127.0.0.1")
169 b.set_input_text("#user", "")
170 if not m.ostree_image:
171 try:
172 # awkward, but neither /dev/null bind mout nor overlayfs work here
173 m.execute("mv /usr/bin/cockpit-bridge /usr/bin/cockpit-bridge.disabled")
174
175 # test not-supported dialog with both the Close and the X button
176 for button in ["button.btn-cancel", "button[aria-label='Close']"]:
177 b.click("button")
178 b.set_input_text("#login-custom-password", self.password)
179 b.click("#ssh-change-auth-dialog button.pf-m-primary")
180 b.wait_not_present("#ssh-change-auth-dialog")
181 b.wait_visible("#ssh-not-supported-dialog")
182 if "cancel" in button:
183 b.assert_pixels("#ssh-not-supported-dialog", "not-supported")
184 b.click(f"#ssh-not-supported-dialog {button}")
185 b.wait_not_present("#ssh-not-supported-dialog")
186 b.wait_text("#output", "")
187 b.wait_in_text("#error", '"problem":"no-cockpit"')
188 self.disconnect()
189 self.allow_journal_messages(".*cockpit-bridge:.* not found")
190 finally:
191 m.execute("mv /usr/bin/cockpit-bridge.disabled /usr/bin/cockpit-bridge")
192
193 # last test: disabled password login
194 self.write_file("/etc/ssh/sshd_config.d/01-no-password.conf", "PasswordAuthentication no",
195 post_restore_action=self.restart_sshd)
196 m.execute(self.restart_sshd)
197 b.click("button")
198 b.wait_in_text("#ssh-change-auth-dialog", "host does not accept password login")
199 b.wait_visible("#ssh-change-auth-dialog button.pf-m-primary:disabled")
200 b.assert_pixels("#ssh-change-auth-dialog", "not-accepted")
201 b.click("#ssh-change-auth-dialog button.btn-cancel")
202 b.wait_not_present("#ssh-change-auth-dialog")
203
204 def unload_key(self):
205 b = self.browser
206 b.switch_to_top()
207 b.open_session_menu()
208 b.click("#sshkeys")
209 b.wait_visible("#credentials-modal")
210 b.wait_in_text("#credential-keys tr[data-name='id_rsa']", "id_rsa")
211 b.wait_visible("#credential-keys tr[data-name='id_rsa'] input[type=checkbox]:checked")
212 b.set_checked("#credential-keys tr[data-name='id_rsa'] input[type=checkbox]", val=False)
213 b.wait_visible("#credential-keys tr[data-name='id_rsa'] input[type=checkbox]:not(:checked)")
214 b.click("#credentials-modal button[aria-label='Close']")
215 b.wait_not_present("#credentials-modal")
216 b.switch_to_frame("cockpit1:localhost/playground/remote")
217
218 @testlib.skipImage("FIXME: Assertion failed: UnknownHostDialog needs a host-key and host-fingerprint in error", "rhel-8-10")
219 def testKey(self):
220 m = self.machine
221 b = self.browser
222
223 self.login_and_go("/playground/remote", user="user", password=self.password, superuser=False)
224 # not too noisy, and useful to debug failures
225 b.eval_js('window.debugging = "connect-ssh"')
226 b.set_input_text("#host", "127.0.0.1")
227 b.click("button")
228
229 # unknown host
230 b.wait_text("#ssh-unknown-host-dialog .pf-v6-c-modal-box__title-text", "Unknown host: 127.0.0.1")
231 b.click("#ssh-unknown-host-dialog button.pf-m-primary")
232 b.wait_not_present("#ssh-unknown-host-dialog")
233
234 # create an SSH key with password
235
236 b.wait_text("#ssh-change-auth-dialog .pf-v6-c-modal-box__title-text", "Log in to 127.0.0.1")
237 # no SSH key present yet
238 b.wait_not_present("#auth-key")
239 b.click("#login-setup-keys")
240 # this shouldn't yet apply as long as key passphrase validation fails
241 b.set_input_text("#login-custom-password", "wrong")
242 key_password = "s3kr1t"
243 b.set_input_text("#login-setup-new-key-password", key_password)
244 # non-matching passphrase
245 b.set_input_text("#login-setup-new-key-password2", "shhht")
246 b.click("#ssh-change-auth-dialog button.pf-m-primary")
247 b.wait_in_text("#ssh-change-auth-dialog", "The key passwords do not match")
248 b.wait_attr("#login-setup-new-key-password2", "aria-invalid", "true")
249 # fix passphrase confirmation
250 b.set_input_text("#login-setup-new-key-password2", key_password)
251 b.set_input_text("#login-custom-password", self.password)
252 b.assert_pixels("#ssh-change-auth-dialog", "new-key")
253 b.click("#ssh-change-auth-dialog button.pf-m-primary")
254 b.wait_not_present("#ssh-change-auth-dialog")
255
256 # `hostname` is the default command
257 hostname = m.execute("hostname").strip()
258 b.wait_text("#output", hostname)
259 b.wait_text("#error", "")
260 self.disconnect()
261
262 self.assertIn(f"user@{hostname}", m.execute("cat /home/user/.ssh/authorized_keys"))
263 self.assertIn(f"user@{hostname}", m.execute("cat /home/user/.ssh/id_rsa.pub"))
264
265 # now log in with SSH key; host is known now
266 b.set_input_text("#command", "echo createkey")
267 b.click("button")
268 b.wait_visible("#ssh-change-auth-dialog")
269 b.set_checked("#auth-key", val=True)
270 b.click("#ssh-change-auth-dialog button.pf-m-primary")
271 b.wait_attr("#locked-identity-password", "aria-invalid", "true")
272 b.wait_in_text("#ssh-change-auth-dialog", "The key password can not be empty")
273
274 b.set_input_text("#locked-identity-password", "wrong")
275 b.click("#ssh-change-auth-dialog button.pf-m-primary")
276 b.wait_attr("#locked-identity-password", "aria-invalid", "false")
277 b.wait_in_text("#ssh-change-auth-dialog .pf-v6-c-alert", "Password not accepted")
278
279 b.set_input_text("#locked-identity-password", key_password)
280 b.assert_pixels("#ssh-change-auth-dialog", "add-key")
281 b.click("#ssh-change-auth-dialog button.pf-m-primary")
282 b.wait_not_present("#ssh-change-auth-dialog")
283 b.wait_text("#output", "createkey")
284 b.wait_text("#error", "")
285 self.disconnect()
286
287 # the key is in the agent now
288 b.switch_to_top()
289 b.open_session_menu()
290 b.click("#sshkeys")
291 b.wait_visible("#credentials-modal")
292 b.wait_in_text("#credential-keys tr[data-name='id_rsa']", "id_rsa")
293 b.wait_visible("#credential-keys tr[data-name='id_rsa'] input[type=checkbox]:checked")
294 b.click("#credentials-modal button[aria-label='Close']")
295 b.wait_not_present("#credentials-modal")
296 b.switch_to_frame("cockpit1:localhost/playground/remote")
297
298 # now that the key is loaded we can connect without any dialogs/passwords
299 b.set_input_text("#command", "echo loadedkey")
300 b.click("button")
301 b.wait_text("#output", "loadedkey")
302 b.wait_text("#error", "")
303 self.disconnect()
304
305 # different target user
306 b.set_input_text("#user", "otheruser")
307 b.set_input_text("#command", "whoami")
308 b.click("button")
309 b.set_input_text("#login-custom-password", self.other_password)
310 b.click("#login-setup-keys")
311 b.click("#ssh-change-auth-dialog button.pf-m-primary")
312 b.wait_not_present("#ssh-change-auth-dialog")
313 b.wait_text("#output", "otheruser")
314 b.wait_text("#error", "")
315 b.set_input_text("#user", "")
316 self.assertIn(f"user@{hostname}", m.execute("cat /home/otheruser/.ssh/authorized_keys"))
317 self.disconnect()
318 self.unload_key()
319
320 # disable password login in SSH to reconfigure the dialog
321 self.write_file("/etc/ssh/sshd_config.d/01-no-password.conf", "PasswordAuthentication no",
322 post_restore_action=self.restart_sshd)
323 m.execute(self.restart_sshd)
324
325 # after unloading the key we need to enter the key password again
326 b.set_input_text("#command", "echo onlykey")
327 b.click("button")
328 b.wait_visible("#ssh-change-auth-dialog")
329 b.wait_in_text("#ssh-change-auth-dialog",
330 "SSH key for logging in to 127.0.0.1 is protected by a password")
331 b.wait_in_text("#ssh-change-auth-dialog", "host does not allow logging in with a password")
332 b.set_input_text("#locked-identity-password", key_password)
333 b.assert_pixels("#ssh-change-auth-dialog", "only-key")
334 b.click("#ssh-change-auth-dialog button.pf-m-primary")
335 b.wait_not_present("#ssh-change-auth-dialog")
336 b.wait_text("#output", "onlykey")
337 b.wait_text("#error", "")
338 self.disconnect()
339 self.unload_key()
340
341 # re-enable password login in SSH
342 m.execute("rm /etc/ssh/sshd_config.d/01-no-password.conf")
343 m.execute(self.restart_sshd)
344
345 # chose password login with key present
346 b.set_input_text("#command", "echo choosepwd")
347 b.click("button")
348 b.wait_visible("#ssh-change-auth-dialog")
349 b.set_checked("#auth-password", val=True)
350 b.set_input_text("#login-custom-password", self.password)
351 b.assert_pixels("#ssh-change-auth-dialog", "choose")
352 b.click("#ssh-change-auth-dialog button.pf-m-primary")
353 b.wait_not_present("#ssh-change-auth-dialog")
354 b.wait_text("#output", "choosepwd")
355 b.wait_text("#error", "")
356 self.disconnect()
357
358 # delete key
359 m.execute("rm /home/user/.ssh/id_* /home/user/.ssh/authorized_keys")
360
361 # create passwordless key
362 b.set_input_text("#command", "echo create-nopwd-key")
363 b.click("button")
364 b.wait_text("#ssh-change-auth-dialog .pf-v6-c-modal-box__title-text", "Log in to 127.0.0.1")
365 # no SSH key present yet
366 b.wait_not_present("#auth-key")
367 b.click("#login-setup-keys")
368 b.set_input_text("#login-custom-password", self.password)
369 b.click("#ssh-change-auth-dialog button.pf-m-primary")
370 b.wait_not_present("#ssh-change-auth-dialog")
371
372 b.wait_text("#output", "create-nopwd-key")
373 b.wait_text("#error", "")
374 self.disconnect()
375
376 # can reconnect without password
377 b.set_input_text("#command", "echo no-password")
378 b.click("button")
379 b.wait_text("#output", "no-password")
380 b.wait_text("#error", "")
381 self.disconnect()
382
383
384 if __name__ == '__main__':
385 testlib.test_main()