| 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 time |
| 10 |
|
| 11 |
import testlib |
| 12 |
|
| 13 |
|
| 14 |
def kill_user_admin(machine): |
| 15 |
# logind from systemd 208 is buggy, so we use systemd directly if it fails |
| 16 |
# https://bugs.freedesktop.org/show_bug.cgi?id=71092 |
| 17 |
machine.execute("loginctl terminate-user admin || systemctl kill user-1000.slice") |
| 18 |
|
| 19 |
|
| 20 |
def authorize_user(m, user, public_keys=None): |
| 21 |
if public_keys is None: |
| 22 |
public_keys = ["verify/files/ssh/id_rsa.pub"] |
| 23 |
m.execute(f"mkdir -p /home/{user}/.ssh") |
| 24 |
m.upload(public_keys, f"/home/{user}/.ssh/authorized_keys") |
| 25 |
m.execute(f"chown -R {user}:{user} /home/{user}/.ssh/") |
| 26 |
m.execute(f"chmod 600 /home/{user}/.ssh/authorized_keys") |
| 27 |
m.execute(f"chmod 700 /home/{user}/.ssh") |
| 28 |
|
| 29 |
|
| 30 |
LOAD_KEYS = [ |
| 31 |
"id_rsa", # password: foobar |
| 32 |
"id_ecdsa", # no password |
| 33 |
"id_ed25519", # password: locked |
| 34 |
] |
| 35 |
|
| 36 |
KEY_IDS = [ |
| 37 |
"2048 SHA256:SRvBhCmkCEVnJ6ascVH0AoVEbS3nPbowZkNevJnXtgw /home/admin/.ssh/id_rsa (RSA)", |
| 38 |
"256 SHA256:dyHF4jiKz6RolQqORIATqhbZ4kil5cyiMQWizbQWU8k /home/admin/.ssh/id_ecdsa (ECDSA)", |
| 39 |
"256 SHA256:Wd028KYmG3OVLp7dBmdx0gMR7VcarJVIfaTtKqYCmak /home/admin/.ssh/id_ed25519 (ED25519)" |
| 40 |
] |
| 41 |
|
| 42 |
|
| 43 |
@testlib.skipImage("TODO: ssh key check fails on Arch Linux", "arch") |
| 44 |
@testlib.skipBeiboot("host switching disabled in beiboot mode") |
| 45 |
class TestMultiMachineKeyAuth(testlib.MachineCase): |
| 46 |
provision = { |
| 47 |
"machine1": {"address": "10.111.113.1/20"}, |
| 48 |
"machine2": {"address": "10.111.113.2/20"}, |
| 49 |
} |
| 50 |
|
| 51 |
def load_key(self, name, password): |
| 52 |
self.browser.switch_to_top() |
| 53 |
self.browser.eval_js("loaded = false") |
| 54 |
self.browser.eval_js(f""" |
| 55 |
load = function (user) {{ |
| 56 |
const proc = cockpit.spawn([ 'ssh-add', '{name}' ], {{ pty: true, directory: user.home + '/.ssh' }}); |
| 57 |
proc.stream(data => {{ |
| 58 |
if (data.indexOf('passphrase') !== -1) |
| 59 |
proc.input('{password}\\n', true); |
| 60 |
console.log(data); |
| 61 |
}}) |
| 62 |
.then(() => {{ |
| 63 |
loaded = true; |
| 64 |
}}) |
| 65 |
.catch(ex => {{ |
| 66 |
console.error(JSON.stringify(ex)); |
| 67 |
}}); |
| 68 |
}} |
| 69 |
""") |
| 70 |
self.browser.eval_js("cockpit.user().then(load)") |
| 71 |
self.browser.wait_js_cond('loaded === true') |
| 72 |
|
| 73 |
def check_keys(self, keys): |
| 74 |
def normalize(k): |
| 75 |
return re.sub(r"(/var)?/home/admin/\.ssh/[^ ]*|test@test|ecdsa w/o comment", "", k) |
| 76 |
self.assertEqual(normalize(self.browser.eval_js("cockpit.spawn([ 'ssh-add', '-l' ])")), |
| 77 |
normalize("\n".join(keys) + "\n")) |
| 78 |
|
| 79 |
def setUp(self): |
| 80 |
super().setUp() |
| 81 |
self.machine2 = self.machines['machine2'] |
| 82 |
|
| 83 |
# Add user |
| 84 |
self.machine2.disconnect() |
| 85 |
self.machine2.execute("useradd user -c User", direct=True) |
| 86 |
self.machine2.execute( |
| 87 |
"sed -i 's/.*PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config $(ls /etc/ssh/sshd_config.d/* 2>/dev/null || true)", direct=True) |
| 88 |
self.machine2.execute(self.restart_sshd, direct=True) |
| 89 |
self.machine2.wait_execute() |
| 90 |
|
| 91 |
# Disable preloading on all machines ("machine1" is done in testlib.py) |
| 92 |
# Preloading on machines with debug build can overload the browser and cause slowness and browser crashes |
| 93 |
# In these tests we actually switch between machines in quick succession which can make things even worse |
| 94 |
if self.is_devel_build(): |
| 95 |
self.disable_preload("packagekit", "playground", "systemd", machine=self.machine2) |
| 96 |
else: |
| 97 |
# Disable packagekit for RAM constraint |
| 98 |
self.disable_preload("packagekit", machine=self.machine) |
| 99 |
self.disable_preload("packagekit", machine=self.machine2) |
| 100 |
|
| 101 |
# enable multi-host |
| 102 |
self.enable_multihost(self.machine) |
| 103 |
|
| 104 |
# Possible workaround - ssh as `admin` and just do `m.execute()` |
| 105 |
@testlib.skipBrowser("Firefox cannot do `cockpit.spawn`", "firefox") |
| 106 |
def testBasic(self): |
| 107 |
b = self.browser |
| 108 |
m1 = self.machine |
| 109 |
m2 = self.machine2 |
| 110 |
|
| 111 |
# Load keys |
| 112 |
m1.execute("mkdir -p /home/admin/.ssh") |
| 113 |
|
| 114 |
m1.upload([f"verify/files/ssh/{k}" for k in LOAD_KEYS], |
| 115 |
"/home/admin/.ssh/") |
| 116 |
m1.upload([f"verify/files/ssh/{k}.pub" for k in LOAD_KEYS], |
| 117 |
"/home/admin/.ssh/") |
| 118 |
m1.execute("chmod 400 /home/admin/.ssh/*") |
| 119 |
m1.execute("chown -R admin:admin /home/admin/.ssh") |
| 120 |
|
| 121 |
self.login_and_go("/system") |
| 122 |
|
| 123 |
# pam-ssh-add isn't used with ws container |
| 124 |
if m1.ws_container: |
| 125 |
self.load_key('id_rsa', 'foobar') |
| 126 |
self.check_keys(["2048 SHA256:SRvBhCmkCEVnJ6ascVH0AoVEbS3nPbowZkNevJnXtgw id_rsa (RSA)"]) |
| 127 |
else: |
| 128 |
# Check our keys were loaded. |
| 129 |
self.load_key("id_ed25519", "locked") |
| 130 |
self.check_keys(KEY_IDS) |
| 131 |
|
| 132 |
# Add machine |
| 133 |
b.switch_to_top() |
| 134 |
b.go("/@10.111.113.2") |
| 135 |
b.click('#machine-troubleshoot') |
| 136 |
b.wait_visible('#hosts_setup_server_dialog') |
| 137 |
|
| 138 |
b.wait_text(f'#hosts_setup_server_dialog {self.primary_btn_class}', "Add") |
| 139 |
b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}') |
| 140 |
b.wait_visible('#hosts_connect_server_dialog') |
| 141 |
b.click('#hosts_connect_server_dialog button.pf-m-warning') |
| 142 |
b.wait_in_text('#hosts_setup_server_dialog', "You are connecting to 10.111.113.2 for the first time.") |
| 143 |
b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}') |
| 144 |
b.wait_in_text('#hosts_setup_server_dialog h1', "Log in to") |
| 145 |
b.wait_in_text('#hosts_setup_server_dialog', "accept password login") |
| 146 |
b.click("#hosts_setup_server_dialog button:contains('Cancel')") |
| 147 |
b.wait_not_present('#hosts_setup_server_dialog') |
| 148 |
|
| 149 |
# add key |
| 150 |
authorize_user(m2, "admin") |
| 151 |
|
| 152 |
# Login |
| 153 |
b.click('#machine-troubleshoot') |
| 154 |
b.wait_visible('#hosts_setup_server_dialog') |
| 155 |
b.wait_text(f'#hosts_setup_server_dialog {self.primary_btn_class}', "Add") |
| 156 |
b.click(f'#hosts_setup_server_dialog {self.primary_btn_class}') |
| 157 |
b.wait_not_present('#hosts_setup_server_dialog') |
| 158 |
b.enter_page("/system", host="10.111.113.2") |
| 159 |
|
| 160 |
# Logout |
| 161 |
b.logout() |
| 162 |
b.wait_visible("#login") |
| 163 |
|
| 164 |
self.login_and_go("/system") |
| 165 |
b.switch_to_top() |
| 166 |
# pam-ssh-add isn't used on OSTree |
| 167 |
if m1.ostree_image: |
| 168 |
self.load_key('id_rsa', 'foobar') |
| 169 |
|
| 170 |
b.go("/@10.111.113.2") |
| 171 |
b.wait_visible('#hosts_connect_server_dialog') |
| 172 |
b.click('#hosts_connect_server_dialog button.pf-m-warning') |
| 173 |
b.wait_visible("iframe.container-frame[name='cockpit1:10.111.113.2/system']") |
| 174 |
|
| 175 |
# Change user |
| 176 |
authorize_user(m2, "user") |
| 177 |
m2.execute("rm /home/admin/.ssh/authorized_keys") |
| 178 |
|
| 179 |
b.click("#hosts-sel button") |
| 180 |
# the opening animation takes quite long, don't move elements underneath mouse clicks |
| 181 |
b.wait_js_cond('ph_count_animations("#nav-hosts") == 0') |
| 182 |
time.sleep(0.2) |
| 183 |
b.click("button:contains('Edit hosts')") |
| 184 |
b.click("#nav-hosts .nav-item a[href='/@10.111.113.2'] + span button.nav-action.pf-m-secondary") |
| 185 |
|
| 186 |
b.wait_visible('#hosts_setup_server_dialog') |
| 187 |
b.wait_visible('#add-machine-user') |
| 188 |
self.assertEqual(b.val("#add-machine-user"), "") |
| 189 |
b.set_input_text('#add-machine-user', 'user') |
| 190 |
b.click("#hosts_setup_server_dialog .pf-m-primary") |
| 191 |
b.wait_not_present('#hosts_setup_server_dialog') |
| 192 |
|
| 193 |
# We now expect this iframe to disappear |
| 194 |
b.wait_not_present("iframe.container-frame[name='cockpit1:10.111.113.2/system']") |
| 195 |
|
| 196 |
# And then we expect it to be reloaded after clicking through |
| 197 |
b.wait_visible("a[href='/@10.111.113.2']") |
| 198 |
b.enter_page("/system", host="[email protected]") |
| 199 |
|
| 200 |
self.allow_hostkey_messages() |
| 201 |
# Might happen when killing the bridge. |
| 202 |
self.allow_journal_messages("localhost: dropping message while waiting for child to exit", |
| 203 |
"Received message for unknown channel: .*", |
| 204 |
".*: error reading from ssh", |
| 205 |
".*: bridge program failed: Child process exited with code .*", |
| 206 |
# Since there is not password, |
| 207 |
# reauthorize doesn't work on m2 |
| 208 |
"received authorize command for wrong user: user", |
| 209 |
".*: user admin reauthorization failed", |
| 210 |
"Error executing command as another user: Not authorized", |
| 211 |
"This incident has been reported.", |
| 212 |
"sudo: a password is required") |
| 213 |
|
| 214 |
# Possible workaround - ssh as `admin` and just do `m.execute()` |
| 215 |
def testLockedIdentity(self): |
| 216 |
b = self.browser |
| 217 |
m1 = self.machine |
| 218 |
m2 = self.machine2 |
| 219 |
|
| 220 |
# upload id_ed25519 (password: locked) |
| 221 |
m1.write("/home/admin/.ssh/config", """ |
| 222 |
Host 10.111.113.2 |
| 223 |
User user |
| 224 |
IdentityFile /home/admin/.ssh/id_ed25519 |
| 225 |
""") |
| 226 |
m1.upload(["verify/files/ssh/id_ed25519", "verify/files/ssh/id_ed25519.pub", |
| 227 |
"verify/files/ssh/id_rsa", "verify/files/ssh/id_rsa.pub"], |
| 228 |
"/home/admin/.ssh/") |
| 229 |
m1.execute("chmod 400 /home/admin/.ssh/*") |
| 230 |
m1.execute("chown -R admin:admin /home/admin/.ssh") |
| 231 |
authorize_user(m2, "user", ["verify/files/ssh/id_ed25519.pub"]) |
| 232 |
|
| 233 |
self.login_and_go("/system") |
| 234 |
b.switch_to_top() |
| 235 |
|
| 236 |
self.load_key('id_rsa', 'foobar') |
| 237 |
|
| 238 |
b.click("#hosts-sel button") |
| 239 |
# the opening animation takes quite long, don't move elements underneath mouse clicks |
| 240 |
b.wait_js_cond('ph_count_animations("#nav-hosts") == 0') |
| 241 |
time.sleep(0.2) |
| 242 |
b.click("button:contains('Add new host')") |
| 243 |
b.wait_visible('#hosts_setup_server_dialog') |
| 244 |
b.set_input_text('#add-machine-address', "10.111.113.2") |
| 245 |
b.click("#hosts_setup_server_dialog .pf-m-primary") |
| 246 |
b.wait_visible('#hosts_connect_server_dialog') |
| 247 |
b.click('#hosts_connect_server_dialog button.pf-m-warning') |
| 248 |
b.wait_in_text("#hosts_setup_server_dialog", "You are connecting to 10.111.113.2 for the first time.") |
| 249 |
b.click("#hosts_setup_server_dialog .pf-m-primary") |
| 250 |
b.wait_in_text("#hosts_setup_server_dialog", "/home/admin/.ssh/id_ed25519") |
| 251 |
b.set_input_text("#locked-identity-password", "locked") |
| 252 |
b.click("#hosts_setup_server_dialog .pf-m-primary") |
| 253 |
b.enter_page("/system", "10.111.113.2") |
| 254 |
self.allow_hostkey_messages() |
| 255 |
|
| 256 |
def testLockedDefaultIdentity(self): |
| 257 |
b = self.browser |
| 258 |
m1 = self.machine |
| 259 |
m2 = self.machine2 |
| 260 |
|
| 261 |
# Upload id_rsa and change its password to something else so |
| 262 |
# that it is not automatically loaded into the agent. id_rsa |
| 263 |
# should be tried autoamtically without needing to configure |
| 264 |
# it explicitly in ~/.ssh/config. |
| 265 |
|
| 266 |
m1.execute("mkdir -p /home/admin/.ssh") |
| 267 |
m1.upload(["verify/files/ssh/id_rsa", "verify/files/ssh/id_rsa.pub"], |
| 268 |
"/home/admin/.ssh/") |
| 269 |
m1.execute("chmod 400 /home/admin/.ssh/*") |
| 270 |
m1.execute("chown -R admin:admin /home/admin/.ssh") |
| 271 |
m1.execute("ssh-keygen -p -f /home/admin/.ssh/id_rsa -P foobar -N foobarfoo") |
| 272 |
authorize_user(m2, "admin", ["verify/files/ssh/id_rsa.pub"]) |
| 273 |
|
| 274 |
self.login_and_go("/system") |
| 275 |
b.switch_to_top() |
| 276 |
|
| 277 |
b.click("#hosts-sel button") |
| 278 |
# the opening animation takes quite long, don't move elements underneath mouse clicks |
| 279 |
b.wait_js_cond('ph_count_animations("#nav-hosts") == 0') |
| 280 |
time.sleep(0.2) |
| 281 |
b.click("button:contains('Add new host')") |
| 282 |
b.wait_visible('#hosts_setup_server_dialog') |
| 283 |
b.set_input_text("#add-machine-address", "10.111.113.2") |
| 284 |
b.click("#hosts_setup_server_dialog .pf-m-primary") |
| 285 |
b.wait_visible('#hosts_connect_server_dialog') |
| 286 |
b.click('#hosts_connect_server_dialog button.pf-m-warning') |
| 287 |
b.wait_in_text("#hosts_setup_server_dialog", "You are connecting to 10.111.113.2 for the first time.") |
| 288 |
b.click("#hosts_setup_server_dialog .pf-m-primary") |
| 289 |
b.wait_in_text("#hosts_setup_server_dialog", "/home/admin/.ssh/id_rsa") |
| 290 |
b.set_input_text("#locked-identity-password", "foobarfoo") |
| 291 |
b.click("#hosts_setup_server_dialog .pf-m-primary") |
| 292 |
b.enter_page("/system", "10.111.113.2") |
| 293 |
self.allow_hostkey_messages() |
| 294 |
|
| 295 |
|
| 296 |
if __name__ == '__main__': |
| 297 |
testlib.test_main() |