Debian Codegraph @

test/verify/check-client

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) 2023 Red Hat, Inc.
5 # SPDX-License-Identifier: LGPL-2.1-or-later
6
7
8 import testlib
9
10
11 # enable this once our cockpit/ws container can beiboot
12 @testlib.skipWsContainer("client setup does not work with ws container")
13 @testlib.skipImage("TODO: fix for OpenSUSE", "opensuse-tumbleweed")
14 class TestClient(testlib.MachineCase):
15
16 provision = {
17 "client": {"address": "10.111.113.1/24"},
18 "target": {"address": "10.111.113.2/24"},
19 }
20
21 def setUp(self):
22 super().setUp()
23 self.m_target = self.machines["target"]
24 self.m_client = self.machines["client"]
25 self.m_target.execute("hostnamectl set-hostname target")
26 # validate on-demand install: this does not work on arch, non-split package
27 if self.m_target.image.startswith("debian") or self.m_target.image.startswith("ubuntu"):
28 self.m_target.execute("dpkg --purge libpcp-pmda-perl pcp python3-pcp pcp-dstat")
29 elif self.m_target.image == 'arch':
30 self.m_target.execute("pacman -Rdd --noconfirm pcp")
31 else:
32 self.m_target.execute("""
33 rpm --erase --verbose pcp python3-pcp
34 systemctl daemon-reload
35 """)
36
37 # replicate the plumbing bits of src/client/cockpit-client to set up cockpit-beiboot
38 self.m_client.write("/etc/cockpit/cockpit.conf", """
39 [WebService]
40 X-For-CockpitClient = true
41 LoginTo = true
42
43 [Ssh-Login]
44 Command = /usr/bin/env python3 -m cockpit.beiboot
45 """)
46 # use the connect_from_flatpak() code path of beiboot.py
47 self.m_client.write("/.flatpak-info", "test mock")
48 self.m_client.write("/usr/local/bin/flatpak-spawn", """#!/bin/sh -eux
49 if [ "$1" = "--host" ]; then shift; continue; fi
50 while [ "${1#--env=}" != "$1" ]; do
51 export "${1#--env=}"
52 shift
53 continue
54 done
55 exec "$@"
56 """, perm="0755")
57
58 def check_login(self, expected_user: str) -> None:
59 b = self.browser
60 b.wait_visible('#content')
61 b.wait_in_text("#hosts-sel", expected_user)
62 b.enter_page("/system")
63 if self.is_devel_build():
64 b.wait_visible("#system_information_hostname_text")
65 # devel disables preloads
66 else:
67 b.wait_visible("#page_status_notification_updates")
68 b.switch_to_top()
69
70 def logout(self, check_last_host: str | None = None) -> None:
71 b = self.browser
72
73 b.logout()
74 # FIXME: This is broken, nothing appears
75 # b.wait_text("#brand", "Connect to:")
76 if check_last_host:
77 b.wait_val("#server-field", check_last_host)
78
79 # no leaked processes
80 self.m_client.execute('''
81 while [ -n "$(pgrep -au admin | grep -Ev 'cockpit-ws' >&2)" ]; do sleep 1; done
82 ''', timeout=10)
83 self.m_target.execute("while pgrep -af '([c]ockpit|[s]sh-agent)' >&2; do sleep 1; done",
84 timeout=30)
85
86 def testBeibootNoBridge(self):
87 # set up target machine: no cockpit
88 self.m_target.execute("rm /usr/bin/cockpit-bridge; rm -r /usr/share/cockpit")
89 self.checkLoginScenarios()
90
91 def testBeibootWithBridge(self):
92 self.checkLoginScenarios()
93
94 def checkLoginScenarios(self):
95 self.m_client.spawn(f"runuser -u admin -- {self.libexecdir}/cockpit-ws --no-tls", "ws.log")
96 self.m_client.wait_for_cockpit_running()
97
98 b = self.browser
99 b.open("/")
100
101 # same username + password login, unknown host key
102 b.wait_text("#brand", "Connect to:")
103 b.wait_not_visible("#recent-hosts-list")
104 b.set_val("#server-field", "10.111.113.2")
105 b.click("#login-button")
106 # accept unknown host key
107 b.wait_in_text("#hostkey-message-1", "10.111.113.2")
108 b.wait_in_text("#hostkey-fingerprint", "SHA256:")
109 b.click("#login-button")
110 b.wait_text("#conversation-prompt", "[email protected]'s password: ")
111 b.set_val("#conversation-input", "foobar")
112 b.click("#login-button")
113 self.check_login("admin@target")
114 b.wait_in_text("#host-apps", "Services")
115 b.wait_in_text("#host-apps", "Terminal")
116 b.become_superuser()
117 b.drop_superuser()
118 self.logout(check_last_host="10.111.113.2")
119
120 # remembers the last host it connected to
121 b.wait_in_text("#recent-hosts-list", "10.111.113.2")
122
123 # same username + password login, now host is known
124 b.click("#login-button")
125 b.wait_text("#conversation-prompt", "[email protected]'s password: ")
126 b.set_val("#conversation-input", "foobar")
127 b.click("#login-button")
128 self.check_login("admin@target")
129 b.become_superuser()
130
131 # python3-pcp is installed on client, but not on target; recognize that
132 b.go("/metrics")
133 b.enter_page("/metrics")
134 b.wait_in_text(".pf-v6-c-empty-state", "PCP is missing")
135 # on-demand install is allowed
136 b.wait_in_text(".pf-v6-c-empty-state button.pf-m-primary", "Install PCP support")
137 b.click(".pf-v6-c-empty-state button.pf-m-primary")
138 b.wait_in_text(".pf-v6-c-modal-box", "pcp will be installed")
139 b.click(".pf-v6-c-modal-box button.cancel")
140
141 b.drop_superuser()
142 self.logout()
143
144 # wrong password, SSH gives you three attempts
145 b.click("#login-button")
146 for _ in range(3):
147 b.wait_text("#conversation-prompt", "[email protected]'s password: ")
148 b.set_val("#conversation-input", "wrong")
149 b.click("#login-button")
150 b.wait_in_text("#login-error-title", "Authentication failed")
151 b.wait_val("#server-field", "10.111.113.2")
152
153 # connect to most recent host
154 b.open("/") # reset URL from /metrics and last remote =host
155 b.click("#recent-hosts-list .host-line button.host-name")
156 b.wait_text("#conversation-prompt", "[email protected]'s password: ")
157 b.set_val("#conversation-input", "foobar")
158 b.click("#login-button")
159 self.check_login("admin@target")
160 self.logout()
161
162 # different user name + password
163 self.m_target.execute("useradd -s /bin/bash user; echo user:barfoo | chpasswd")
164 b.set_val("#server-field", "[email protected]")
165 b.click("#login-button")
166 b.wait_text("#conversation-prompt", "[email protected]'s password: ")
167 b.set_val("#conversation-input", "barfoo")
168 b.click("#login-button")
169 self.check_login("user@target")
170
171 # not a sudoer
172 b.open_superuser_dialog()
173 if not self.has_sudo_rs:
174 # sudo-ws always asks for password, sudo-rs rejects immediately
175 b.wait_text(".pf-v6-c-modal-box__title-text", "Switch to administrative access")
176 b.set_input_text(".pf-v6-c-modal-box input", "barfoo")
177 b.click(".pf-v6-c-modal-box button:contains('Authenticate')")
178 b.wait_text(".pf-v6-c-modal-box__title-text", "Problem becoming administrator")
179 b.click(".pf-v6-c-modal-box button.btn-cancel")
180 b.wait_not_present(".pf-v6-c-modal-box")
181 b.check_superuser_indicator("Limited access")
182
183 self.logout()
184 b.wait_in_text("#recent-hosts-list", "[email protected]")
185
186 # explicit port
187 b.set_val("#server-field", "10.111.113.2:22")
188 b.click("#login-button")
189 b.wait_text("#conversation-prompt", "[email protected]'s password: ")
190 b.set_val("#conversation-input", "foobar")
191 b.click("#login-button")
192 self.check_login("admin@target")
193 self.logout()
194 b.wait_in_text("#recent-hosts-list", "10.111.113.2:22")
195
196 # different user name + explicit port
197 b.set_val("#server-field", "[email protected]:22")
198 b.click("#login-button")
199 b.wait_text("#conversation-prompt", "[email protected]'s password: ")
200 b.set_val("#conversation-input", "barfoo")
201 b.click("#login-button")
202 self.check_login("user@target")
203 self.logout()
204 b.wait_in_text("#recent-hosts-list", "[email protected]:22")
205
206 # remove that recent hosts entry, this also avoids ambiguous selectors further down
207 b.click("#recent-hosts-list .host-line[data-host-id='[email protected]:22'] button.host-remove")
208 b.wait_not_in_text("#recent-hosts-list", "[email protected]:22")
209
210 # unreachable host
211 b.set_val("#server-field", "unknownhost")
212 b.click("#login-button")
213 b.wait_text("#login-error-title", "Refusing to connect")
214 b.wait_text("#login-error-message", "Host is unknown")
215 b.wait_val("#server-field", "unknownhost")
216 # does not appear in recent hosts
217 b.wait_in_text("#recent-hosts-list", "10.111.113.2")
218 self.assertNotIn("unknownhost", b.text("#recent-hosts-list"))
219
220 # wrong port
221 b.set_val("#server-field", "10.111.113.2:222")
222 b.click("#login-button")
223 b.wait_in_text("#login-error-message", "Host is unknown")
224
225 # unencrypted SSH key login
226 self.m_client.execute("runuser -u admin -- ssh-keygen -t rsa -N '' -f ~admin/.ssh/id_rsa")
227 pubkey = self.m_client.execute("cat ~admin/.ssh/id_rsa.pub")
228 self.m_target.write("/home/user/.ssh/authorized_keys", pubkey, owner="user:user", perm="600")
229 b.click("#recent-hosts-list .host-line[data-host-id='[email protected]'] button.host-name")
230 self.check_login("user@target")
231 self.logout()
232
233 # encrypted SSH key login
234 self.m_client.execute("runuser -u admin -- ssh-keygen -f ~admin/.ssh/id_rsa -p -P '' -N foobarfoo")
235 b.click("#login-button")
236 b.wait_text("#conversation-prompt", "Enter passphrase for key '/home/admin/.ssh/id_rsa': ")
237 b.set_val("#conversation-input", "wrong")
238 b.click("#login-button")
239 b.wait_val("#conversation-input", "")
240 b.wait_text("#conversation-prompt", "Enter passphrase for key '/home/admin/.ssh/id_rsa': ")
241 b.set_val("#conversation-input", "foobarfoo")
242 b.click("#login-button")
243 self.check_login("user@target")
244 self.logout()
245
246
247 if __name__ == '__main__':
248 testlib.test_main()