| 1 |
#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv) |
| 2 |
|
| 3 |
# |
| 4 |
# Copyright (C) 2022 Red Hat, Inc. |
| 5 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
|
| 7 |
|
| 8 |
import testlib |
| 9 |
|
| 10 |
HOST = "host.containers.internal" |
| 11 |
|
| 12 |
|
| 13 |
@testlib.onlyImage("no cockpit/ws container on this image", "fedora-coreos") |
| 14 |
@testlib.nondestructive |
| 15 |
class TestWsBastionContainer(testlib.MachineCase): |
| 16 |
def setUp(self): |
| 17 |
super().setUp() |
| 18 |
# stop ws container from previous runs |
| 19 |
self.machine.stop_cockpit() |
| 20 |
# undo cockpit/ws install steps |
| 21 |
self.restore_file("/etc/systemd/system/cockpit.service") |
| 22 |
self.addCleanup(self.machine.execute, "podman rm -f --all") |
| 23 |
|
| 24 |
def approve_key(self, b, hostname): |
| 25 |
b.wait_visible("#hostkey-group") |
| 26 |
b.wait_in_text("#hostkey-message-1", f"You are connecting to {hostname} for the first time.") |
| 27 |
b.click("#login-button") |
| 28 |
|
| 29 |
def testPasswordLogin(self): |
| 30 |
m = self.machine |
| 31 |
b = self.browser |
| 32 |
m.execute("podman run -d --name cockpit-bastion -p 9090:9090 localhost/cockpit/ws") |
| 33 |
m.execute("until curl --fail --head -k https://localhost:9090/; do sleep 1; done", timeout=10) |
| 34 |
|
| 35 |
b.open("/", tls=True) |
| 36 |
|
| 37 |
b.wait_visible("#login") |
| 38 |
# should be pre-configured to RequireHost |
| 39 |
b.wait_not_visible("#option-group") |
| 40 |
b.wait_visible("#server-field") |
| 41 |
# LoginTitle from default-bastion.conf |
| 42 |
b.wait_text("#server-name", "Cockpit Bastion") |
| 43 |
# No branding by default |
| 44 |
b.wait_text("#brand", "") |
| 45 |
|
| 46 |
b.set_val("#login-user-input", "admin") |
| 47 |
b.set_val("#login-password-input", "foobar") |
| 48 |
|
| 49 |
# Requires a host |
| 50 |
b.click("#login-button") |
| 51 |
b.wait_text("#login-error-title", "Please specify the host to connect to") |
| 52 |
b.wait_not_visible("#login-error-message") |
| 53 |
# so connect to our own container host |
| 54 |
b.set_val("#server-field", HOST) |
| 55 |
b.set_val("#login-password-input", "foobar") |
| 56 |
# key is unknown |
| 57 |
b.click("#login-button") |
| 58 |
self.approve_key(b, HOST) |
| 59 |
|
| 60 |
b.wait_visible('#content') |
| 61 |
b.wait_text('#current-username', 'admin') |
| 62 |
# Our test VM only pre-installs -system and -networkmanager |
| 63 |
b.wait_in_text("#host-apps", "Overview") |
| 64 |
b.wait_in_text("#host-apps", "Networking") |
| 65 |
self.assertNotIn("Kernel dump", b.text("#host-apps")) |
| 66 |
# runs the ssh target's bridge (no beiboot) |
| 67 |
m.execute("pgrep -f /usr/bin/[c]ockpit-bridge") |
| 68 |
b.logout() |
| 69 |
|
| 70 |
# remembers the last host via URL, server field should be pre-filled |
| 71 |
self.assertEqual(b.eval_js("window.location.pathname"), f"/={HOST}/system") |
| 72 |
# this is only for Cockpit Client |
| 73 |
b.wait_not_visible("#recent-hosts") |
| 74 |
# second time SSH key is known |
| 75 |
b.try_login() |
| 76 |
b.wait_visible('#content') |
| 77 |
b.logout() |
| 78 |
|
| 79 |
# disable target bridge, should use beiboot |
| 80 |
m.execute("mount -o bind /dev/null /usr/bin/cockpit-bridge") |
| 81 |
self.addCleanup(m.execute, "umount /usr/bin/cockpit-bridge") |
| 82 |
b.try_login() |
| 83 |
b.wait_visible('#content') |
| 84 |
m.execute("pgrep -f '[p]ython3 -ic # cockpit-bridge'") |
| 85 |
# beiboot mode has extra included pages |
| 86 |
b.wait_in_text("#host-apps", "Overview") |
| 87 |
b.wait_in_text("#host-apps", "Networking") |
| 88 |
b.wait_in_text("#host-apps", "Kernel dump") |
| 89 |
b.wait_in_text("#host-apps", "SELinux") |
| 90 |
# but Storage and Applications are hidden due to failing manifest conditions |
| 91 |
self.assertNotIn("Storage", b.text("#host-apps")) |
| 92 |
self.assertNotIn("Applications", b.text("#host-apps")) |
| 93 |
b.logout() |
| 94 |
|
| 95 |
def testKnownHosts(self): |
| 96 |
m = self.machine |
| 97 |
b = self.browser |
| 98 |
|
| 99 |
m.execute(f"ssh-keyscan localhost | sed 's/^localhost/{HOST}/' > /root/known_hosts") |
| 100 |
self.addCleanup(m.execute, "rm /root/known_hosts") |
| 101 |
|
| 102 |
def check_login(): |
| 103 |
m.execute("until curl --fail --head -k https://localhost:9090/; do sleep 1; done", timeout=10) |
| 104 |
b.open("/", tls=True) |
| 105 |
b.set_val("#login-user-input", "admin") |
| 106 |
b.set_val("#login-password-input", "foobar") |
| 107 |
b.set_val("#server-field", HOST) |
| 108 |
b.click("#login-button") |
| 109 |
b.wait_visible('#content') |
| 110 |
b.logout() |
| 111 |
m.execute("podman rm -f cockpit-bastion") |
| 112 |
|
| 113 |
# default location |
| 114 |
m.execute("podman run -d --name cockpit-bastion -p 9090:9090 " |
| 115 |
"-v /root/known_hosts:/etc/ssh/ssh_known_hosts:ro,Z " |
| 116 |
"localhost/cockpit/ws") |
| 117 |
check_login() |
| 118 |
|
| 119 |
# custom location |
| 120 |
m.execute("podman run -d --name cockpit-bastion -p 9090:9090 " |
| 121 |
"-v /root/known_hosts:/known_hosts:ro,Z " |
| 122 |
"-e COCKPIT_SSH_KNOWN_HOSTS_FILE=/known_hosts " |
| 123 |
"localhost/cockpit/ws") |
| 124 |
check_login() |
| 125 |
|
| 126 |
def testCustomConf(self): |
| 127 |
m = self.machine |
| 128 |
b = self.browser |
| 129 |
|
| 130 |
# custom cockpit.conf and pretend we are Fedora CoreOS/RHEL |
| 131 |
self.write_file("/root/cockpit.conf", """[WebService] |
| 132 |
LoginTitle = My Walden |
| 133 |
""") |
| 134 |
m.execute("cp /etc/os-release /root; " |
| 135 |
"podman run -d --name cockpit-bastion -p 9090:9090 " |
| 136 |
"-v /root/cockpit.conf:/etc/cockpit/cockpit.conf:ro,Z " |
| 137 |
"-v /root/os-release:/etc/os-release:ro,Z " |
| 138 |
"localhost/cockpit/ws") |
| 139 |
m.execute("until curl --fail --head -k https://localhost:9090/; do sleep 1; done", timeout=10) |
| 140 |
|
| 141 |
b.open("/", tls=True) |
| 142 |
|
| 143 |
b.wait_visible("#login") |
| 144 |
b.wait_text("#server-name", "My Walden") |
| 145 |
# custom conf does not have RequireHost |
| 146 |
b.wait_visible("#option-group") |
| 147 |
# Shows os-release branding |
| 148 |
b.wait_in_text("#brand", "Fedora" if m.image == "fedora-coreos" else "Red Hat Enterprise Linux") |
| 149 |
|
| 150 |
# pre-fill target host |
| 151 |
b.open(f"/={HOST}/", tls=True) |
| 152 |
b.wait_visible("#login") |
| 153 |
b.set_val("#login-user-input", "admin") |
| 154 |
b.set_val("#login-password-input", "foobar") |
| 155 |
|
| 156 |
b.click("#login-button") |
| 157 |
self.approve_key(b, HOST) |
| 158 |
|
| 159 |
b.wait_visible('#content') |
| 160 |
b.wait_text('#current-username', 'admin') |
| 161 |
|
| 162 |
def testKeyLogin(self): |
| 163 |
m = self.machine |
| 164 |
b = self.browser |
| 165 |
|
| 166 |
KEY_PASSWORD = "sshfoobar" |
| 167 |
|
| 168 |
# old RSA/PEM format |
| 169 |
m.execute(f"ssh-keygen -q -f /root/id_bastion -t rsa -m PEM -N {KEY_PASSWORD}") |
| 170 |
m.execute(f"ssh-keyscan localhost | sed 's/^localhost/{HOST}/' > /root/known_hosts") |
| 171 |
self.addCleanup(m.execute, "rm /root/known_hosts /root/id_bastion /root/id_bastion.pub") |
| 172 |
|
| 173 |
def do_test_key_login(ssh_key_env: str): |
| 174 |
m.execute( |
| 175 |
"podman run -d --name cockpit-bastion -p 9090:9090 " |
| 176 |
"-v /root/known_hosts:/etc/ssh/ssh_known_hosts:ro,Z " |
| 177 |
"-v /root/id_bastion:/id_bastion:ro,Z " |
| 178 |
f"-e {ssh_key_env}=/id_bastion " |
| 179 |
"-e COCKPIT_DEBUG=all " |
| 180 |
"localhost/cockpit/ws" |
| 181 |
) |
| 182 |
m.execute("until curl --fail --head -k https://localhost:9090/; do sleep 1; done", timeout=10) |
| 183 |
|
| 184 |
b.open("/", tls=True) |
| 185 |
b.set_val("#login-user-input", "admin") |
| 186 |
b.set_val("#server-field", HOST) |
| 187 |
|
| 188 |
# the account password does not work |
| 189 |
b.set_val("#login-password-input", "foobar") |
| 190 |
b.click("#login-button") |
| 191 |
b.wait_text("#login-error-title", "Authentication failed") |
| 192 |
b.wait_text("#login-error-message", "Wrong user name or password") |
| 193 |
|
| 194 |
# SSH key password, but key is not authorized |
| 195 |
b.set_val("#login-password-input", KEY_PASSWORD) |
| 196 |
b.click("#login-button") |
| 197 |
b.wait_text("#login-error-title", "Authentication failed") |
| 198 |
b.wait_text("#login-error-message", "Wrong user name or password") |
| 199 |
|
| 200 |
# authorize key |
| 201 |
self.restore_file("/home/admin/.ssh/authorized_keys") |
| 202 |
# Do not use authorized_keys.d as that does not work on rhel4edge |
| 203 |
# Do not append but overwrite so we are sure the right key is used |
| 204 |
m.execute("cat /root/id_bastion.pub > /home/admin/.ssh/authorized_keys") |
| 205 |
|
| 206 |
# fails with wrong key password |
| 207 |
b.set_val("#login-password-input", "notthispassword") |
| 208 |
b.click("#login-button") |
| 209 |
b.wait_text("#login-error-title", "Authentication failed") |
| 210 |
b.wait_text("#login-error-message", "Wrong user name or password") |
| 211 |
|
| 212 |
# works with correct key password |
| 213 |
b.set_val("#login-password-input", KEY_PASSWORD) |
| 214 |
b.click("#login-button") |
| 215 |
b.wait_visible('#content') |
| 216 |
# runs the ssh target's bridge (no beiboot) |
| 217 |
m.execute("pgrep -f /usr/bin/[c]ockpit-bridge") |
| 218 |
b.logout() |
| 219 |
|
| 220 |
# now test with current OpenSSH format |
| 221 |
m.execute(f"yes | ssh-keygen -q -f /root/id_bastion -t rsa -N {KEY_PASSWORD}") |
| 222 |
m.execute("cat /root/id_bastion.pub > /home/admin/.ssh/authorized_keys") |
| 223 |
b.set_val("#login-user-input", "admin") |
| 224 |
b.set_val("#server-field", HOST) |
| 225 |
b.set_val("#login-password-input", KEY_PASSWORD) |
| 226 |
b.click("#login-button") |
| 227 |
b.wait_visible('#content') |
| 228 |
b.logout() |
| 229 |
|
| 230 |
# disable target bridge, should use beiboot |
| 231 |
m.execute("mount -o bind /dev/null /usr/bin/cockpit-bridge") |
| 232 |
try: |
| 233 |
b.try_login(password=KEY_PASSWORD) |
| 234 |
b.wait_visible('#content') |
| 235 |
m.execute("pgrep -f '[p]ython3 -ic # cockpit-bridge'") |
| 236 |
finally: |
| 237 |
m.execute("umount /usr/bin/cockpit-bridge") |
| 238 |
b.logout() |
| 239 |
|
| 240 |
m.execute("podman rm -f -t0 cockpit-bastion") |
| 241 |
m.execute("rm /home/admin/.ssh/authorized_keys") |
| 242 |
|
| 243 |
for ssh_key_env in ["COCKPIT_SSH_KEY_PATH", f"COCKPIT_SSH_KEY_PATH_{HOST.upper()}"]: |
| 244 |
do_test_key_login(ssh_key_env=ssh_key_env) |
| 245 |
|
| 246 |
def testExternalAgent(self): |
| 247 |
m = self.machine |
| 248 |
b = self.browser |
| 249 |
|
| 250 |
KEY_PASSWORD = "sshfoobar" |
| 251 |
|
| 252 |
# run the container as user -- as root does not make sense, as we want to |
| 253 |
# share the user's SSH key with it |
| 254 |
m.execute(f"podman save localhost/cockpit/ws -o {self.vm_tmpdir}/cockpit-ws.tar") |
| 255 |
|
| 256 |
# HACK: user podman does not add default route if the host doesn't have any |
| 257 |
m.execute("ip route add default via 172.27.0.1") |
| 258 |
self.addCleanup(m.execute, "ip route del default") |
| 259 |
|
| 260 |
self.restore_dir("/home/admin") |
| 261 |
|
| 262 |
# podman needs logind session (https://bugzilla.redhat.com/show_bug.cgi?id=2375715) |
| 263 |
m.execute("loginctl enable-linger admin") |
| 264 |
self.addCleanup(m.execute, "loginctl disable-linger admin") |
| 265 |
|
| 266 |
m.execute("runuser -u admin -- sh -ex", input=f""" |
| 267 |
# we don't start a real login session here, fake it |
| 268 |
cd $HOME |
| 269 |
export XDG_RUNTIME_DIR=/run/user/$(id -u) |
| 270 |
|
| 271 |
# create new key, set it up for logging into host |
| 272 |
ssh-keygen -q -f ~/.ssh/id_bastion -N {KEY_PASSWORD} |
| 273 |
cat ~/.ssh/id_bastion.pub > /home/admin/.ssh/authorized_keys |
| 274 |
ssh-keyscan localhost | sed 's/^localhost/{HOST}/' > ~/.ssh/known_hosts |
| 275 |
|
| 276 |
# start agent, load key |
| 277 |
eval $(ssh-agent -a $XDG_RUNTIME_DIR/ssh-agent) |
| 278 |
printf '#!/bin/sh\necho {KEY_PASSWORD}\n' > /tmp/pwd |
| 279 |
chmod u+x /tmp/pwd |
| 280 |
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=/tmp/pwd ssh-add ~/.ssh/id_bastion |
| 281 |
rm /tmp/pwd |
| 282 |
ssh-add -l |
| 283 |
|
| 284 |
# run container |
| 285 |
podman load -i {self.vm_tmpdir}/cockpit-ws.tar |
| 286 |
podman run -d --rm --name cockpit-bastion -p 9090:9090 \ |
| 287 |
-v ~/.ssh/known_hosts:/etc/ssh/ssh_known_hosts:ro \ |
| 288 |
-v $XDG_RUNTIME_DIR/ssh-agent:/ssh-agent \ |
| 289 |
-e SSH_AUTH_SOCK=/ssh-agent \ |
| 290 |
--security-opt=label=disable \ |
| 291 |
-e COCKPIT_DEBUG=all localhost/cockpit/ws |
| 292 |
|
| 293 |
until curl --fail --head -k https://localhost:9090/; do sleep 1; done |
| 294 |
""") |
| 295 |
|
| 296 |
# login works without a password, using the external agent |
| 297 |
b.open("/", tls=True) |
| 298 |
b.set_val("#server-field", HOST) |
| 299 |
b.try_login(password="") |
| 300 |
b.wait_visible('#content') |
| 301 |
|
| 302 |
|
| 303 |
@testlib.onlyImage("no cockpit/ws container on this image", "fedora-coreos") |
| 304 |
@testlib.nondestructive |
| 305 |
class TestWsPrivileged(testlib.MachineCase): |
| 306 |
def testService(self): |
| 307 |
# the install script should have created a cockpit.service, but not started it |
| 308 |
m = self.machine |
| 309 |
|
| 310 |
self.assertEqual(m.execute("! systemctl is-enabled cockpit.service").strip(), "disabled") |
| 311 |
self.assertEqual(m.execute("! systemctl is-active cockpit.service").strip(), "inactive") |
| 312 |
# stop ws container from previous test runs |
| 313 |
self.machine.stop_cockpit() |
| 314 |
self.assertEqual(m.execute("podman ps --noheading"), "") |
| 315 |
self.addCleanup(m.execute, "systemctl disable --now cockpit.service") |
| 316 |
|
| 317 |
m.execute("systemctl start cockpit.service") |
| 318 |
self.assertEqual(m.execute("systemctl is-active cockpit.service").strip(), "active") |
| 319 |
firstStartTime = m.execute("podman inspect cockpit-ws| jq '.[0].Created'") |
| 320 |
m.execute("until curl --fail --head -k https://localhost:9090/; do sleep 1; done", timeout=10) |
| 321 |
|
| 322 |
m.execute("systemctl restart cockpit.service") |
| 323 |
self.assertEqual(m.execute("systemctl is-active cockpit.service").strip(), "active") |
| 324 |
secondStartTime = m.execute("podman inspect cockpit-ws| jq '.[0].Created'") |
| 325 |
m.execute("until curl --fail --head -k https://localhost:9090/; do sleep 1; done", timeout=10) |
| 326 |
|
| 327 |
self.assertNotEqual(firstStartTime, secondStartTime) |
| 328 |
|
| 329 |
m.execute("systemctl stop cockpit.service") |
| 330 |
self.assertEqual(m.execute("! systemctl is-enabled cockpit.service").strip(), "disabled") |
| 331 |
self.assertEqual(m.execute("! systemctl is-active cockpit.service").strip(), "inactive") |
| 332 |
self.assertEqual(m.execute("podman ps --noheading"), "") |
| 333 |
|
| 334 |
# current container has `set -x` in its startup script, which ends up in the journal |
| 335 |
self.allow_journal_messages("^[/+'].*") |
| 336 |
|
| 337 |
|
| 338 |
if __name__ == '__main__': |
| 339 |
testlib.test_main() |