Debian Codegraph @

test/verify/check-storage-luks

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) 2015 Red Hat, Inc.
5 # SPDX-License-Identifier: LGPL-2.1-or-later
6
7
8 import json
9 import os
10 import subprocess
11
12 import packagelib
13 import storagelib
14 import testlib
15
16
17 def console_screenshot(machine, name):
18 subprocess.run(f"virsh -c qemu:///session screenshot {machine._domain.ID()!s} '{name}'",
19 shell=True)
20 testlib.attach(name, move=True)
21 print("Wrote screenshot to " + name)
22
23
24 @testlib.nondestructive
25 @testlib.skipImage("cryptsetup uses too much memory, OOM on our test VMs", "rhel-8-*")
26 class TestStorageLuks(storagelib.StorageCase):
27
28 def testLuks(self):
29 self.allow_journal_messages("Device is not initialized.*", ".*could not be opened.")
30 m = self.machine
31 b = self.browser
32
33 mount_point_secret = "/run/secret"
34
35 self.login_and_go("/storage")
36
37 # Add a disk and partition it
38 disk = self.add_ram_disk(100)
39 self.click_card_row("Storage", name=disk)
40 self.click_card_dropdown("Solid State Drive", "Create partition table")
41 self.dialog({"type": "gpt"})
42 b.wait_text(self.card_row_col("GPT partitions", 1, 1), "Free space")
43
44 self.assertEqual(m.execute("grep -v ^# /etc/crypttab || true").strip(), "")
45
46 # Format it with luks
47 self.click_dropdown(self.card_row("GPT partitions", 1), "Create partition")
48 self.dialog_wait_open()
49 self.dialog_set_val("size", 60)
50 self.dialog_set_val("type", "ext4")
51 self.dialog_set_val("crypto", self.default_crypto_type)
52 self.dialog_set_val("name", "ENCRYPTED")
53 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja")
54 self.dialog_set_val("passphrase2", "vainu-reku-toma-rolle-kaja")
55 self.dialog_set_val("store_passphrase.on", val=True)
56 self.dialog_set_val("crypto_options", "crypto,options")
57 self.dialog_set_val("mount_point", mount_point_secret)
58 b.assert_pixels("#dialog", "format")
59 self.dialog_apply()
60 self.dialog_wait_close()
61 b.wait_text(self.card_row_col("GPT partitions", 1, 2), "ext4 filesystem (encrypted)")
62 b.wait_text(self.card_row_col("GPT partitions", 1, 3), mount_point_secret)
63
64 dev = "/dev/" + b.text(self.card_row_col("GPT partitions", 1, 1))
65 self.click_card_row("GPT partitions", 1)
66
67 if self.default_crypto_type == "luks1":
68 b.wait_text(self.card_desc("Encryption", "Encryption type"), "LUKS1")
69 elif self.default_crypto_type == "luks2":
70 b.wait_text(self.card_desc("Encryption", "Encryption type"), "LUKS2")
71
72 uuid = m.execute(f"cryptsetup luksUUID {dev}").strip()
73 cleartext_dev = "/dev/mapper/luks-" + uuid
74 passphrase_path = "/etc/luks-keys/luks-" + uuid
75
76 self.assert_in_configuration(dev, "crypttab", "options", "crypto,options")
77 self.assert_not_in_configuration(dev, "crypttab", "options", "noauto")
78 self.assert_in_configuration(dev, "crypttab", "passphrase-path", passphrase_path)
79 self.assertEqual(m.execute(f"cat {passphrase_path}"), "vainu-reku-toma-rolle-kaja")
80
81 self.assert_in_configuration(cleartext_dev, "fstab", "dir", mount_point_secret)
82 self.assert_not_in_configuration(cleartext_dev, "fstab", "opts", "noauto")
83
84 # cut off minutes, to avoid a too wide race condition in the test
85 date_no_mins = b.eval_js("Intl.DateTimeFormat('en', { dateStyle: 'medium', timeStyle: 'short' }).format()").split(':')[0]
86 b.wait_in_text(self.card_desc("Encryption", "Stored passphrase"),
87 f"Last modified: {date_no_mins}:")
88
89 b.assert_pixels(self.card("Encryption"), "card",
90 ignore=["[data-test-desc-title='Stored passphrase']",
91 "[data-test-desc-title='Cleartext device']"])
92
93 # Unmount. This locks it
94 b.click(self.card_button("ext4 filesystem", "Unmount"))
95 self.confirm()
96 self.assert_in_configuration(dev, "crypttab", "options", "noauto")
97 self.assert_in_child_configuration(dev, "fstab", "opts", "noauto")
98 self.wait_not_mounted("Filesystem")
99
100 # Mount, this uses the stored passphrase for unlocking
101 b.click(self.card_button("Filesystem", "Mount"))
102 self.dialog_wait_open()
103 self.dialog_wait_val("mount_point", mount_point_secret)
104 self.dialog_apply()
105 self.dialog_wait_close()
106 self.assert_not_in_configuration(dev, "crypttab", "options", "noauto")
107 self.assert_not_in_configuration(cleartext_dev, "fstab", "opts", "noauto")
108 self.wait_mounted("ext4 filesystem")
109
110 b.click(self.card_desc_action("Encryption", "Options"))
111 self.dialog({"options": "weird,options"})
112 self.assert_in_configuration(dev, "crypttab", "options", "weird,options")
113
114 # Change stored passphrase
115 b.click(self.card_desc_action("Encryption", "Stored passphrase"))
116 self.dialog({"passphrase": "wrong-passphrase"})
117 self.assert_in_configuration(dev, "crypttab", "passphrase-path", passphrase_path)
118 self.assertEqual(m.execute(f"cat {passphrase_path}"), "wrong-passphrase")
119
120 # Unmount it
121 b.click(self.card_button("ext4 filesystem", "Unmount"))
122 self.confirm()
123 self.assert_in_configuration(dev, "crypttab", "options", "noauto")
124 self.assert_in_child_configuration(dev, "fstab", "opts", "noauto")
125 self.wait_not_mounted("Filesystem")
126
127 # Mount, this tries the wrong passphrase but eventually prompts.
128 b.click(self.card_button("Filesystem", "Mount"))
129 self.dialog_wait_open()
130 self.dialog_apply()
131 b.wait_in_text("#dialog", "Failed to activate device:")
132 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja")
133 self.dialog_apply()
134 self.dialog_wait_close()
135 self.assert_not_in_configuration(dev, "crypttab", "options", "noauto")
136 self.assert_not_in_configuration(cleartext_dev, "fstab", "opts", "noauto")
137 self.wait_mounted("ext4 filesystem")
138
139 # Remove passphrase
140 b.click(self.card_desc_action("Encryption", "Stored passphrase"))
141 self.dialog({"passphrase": ""})
142 self.assert_in_configuration(dev, "crypttab", "passphrase-path", "")
143
144 # Unmount it
145 b.click(self.card_button("ext4 filesystem", "Unmount"))
146 self.confirm()
147 self.assert_in_configuration(dev, "crypttab", "options", "noauto")
148 self.assert_in_child_configuration(dev, "fstab", "opts", "noauto")
149 self.wait_not_mounted("Filesystem")
150
151 # Mount it readonly. This asks for a passphrase.
152 b.click(self.card_button("Filesystem", "Mount"))
153 self.dialog({"mount_options.ro": True,
154 "passphrase": "vainu-reku-toma-rolle-kaja"})
155 self.wait_mounted("ext4 filesystem")
156 self.assertIn("ro", m.execute(f"findmnt -s -n -o OPTIONS {mount_point_secret}"))
157 self.assert_in_configuration(dev, "crypttab", "options", "readonly")
158 self.assert_in_configuration(cleartext_dev, "fstab", "opts", "ro")
159
160 # Check that the clear text device is actually readonly
161 self.assertEqual(m.execute(f"lsblk -no RO {cleartext_dev}").strip(), "1")
162
163 # Change "read only" mount option. This should reopen LUKS as
164 # necessary and ask for a passphrase.
165 b.click(self.card_desc("ext4 filesystem", "Mount point") + " button")
166 self.dialog_wait_open()
167 self.dialog_set_val("mount_options.ro", val=False)
168 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja")
169 self.dialog_apply()
170 self.dialog_wait_close()
171
172 self.assertEqual(m.execute(f"lsblk -no RO {cleartext_dev}").strip(), "0")
173
174 # Delete the partition.
175 self.click_card_dropdown("Partition", "Delete")
176 self.confirm()
177 b.wait_text(self.card_row_col("GPT partitions", 1, 1), "Free space")
178 # luksmeta-monitor-hack.py races with the partition deletion
179 self.allow_journal_messages('Unknown device .*: No such file or directory')
180
181 self.assertEqual(m.execute("grep -v ^# /etc/crypttab || true").strip(), "")
182 self.assertEqual(m.execute(f"grep {mount_point_secret} /etc/fstab || true"), "")
183
184 # luksmeta-monitor-hack.py might leave a udevadm process
185 # behind, so let's check that the session goes away cleanly
186 # after a logout.
187
188 b.logout()
189 testlib.wait(lambda: m.execute("(loginctl list-users | grep admin) || true") == "")
190
191 # FIXME: race condition after unmounting; hard to investigate, re-check after storage redesign
192 self.allow_browser_errors("validateDOMNesting.*cannot appear as a child.*<tr> ul")
193
194 def testFormatReadOnly(self):
195 m = self.machine
196 b = self.browser
197
198 self.login_and_go("/storage")
199
200 disk = self.add_loopback_disk(320) # xfs minimum size is ~300MB
201 self.click_card_row("Storage", name=disk)
202
203 # Create a encrypted filesystem and mount it readonly. This
204 # needs to reopen the LUKS layer after formatting.
205
206 self.click_card_dropdown("Unformatted data", "Format")
207 self.dialog({"type": "ext4",
208 "mount_point": f"{self.mnt_dir}/foo",
209 "mount_options.ro": True,
210 "crypto": "luks1",
211 "passphrase": "vainu-reku-toma-rolle-kaja",
212 "passphrase2": "vainu-reku-toma-rolle-kaja"})
213 b.wait_visible(self.card("ext4 filesystem"))
214
215 uuid = m.execute(f"cryptsetup luksUUID {disk}").strip()
216 cleartext_dev = "/dev/mapper/luks-" + uuid
217
218 self.assert_in_configuration(disk, "crypttab", "options", "readonly")
219 self.assert_in_configuration(cleartext_dev, "fstab", "opts", "ro")
220 self.assertEqual(m.execute(f"lsblk -no RO {cleartext_dev}").strip(), "1")
221
222 # Reformat it, while keeping the encryption layer and keeping
223 # it read-only. This needs to reopen LUKS twice, once to make
224 # the cleartext device writable for formatting, and once to
225 # make it read-only again, as requested.
226
227 self.click_card_dropdown("ext4 filesystem", "Format")
228 self.dialog_wait_open()
229 self.dialog_wait_val("mount_point", f"{self.mnt_dir}/foo")
230 self.dialog_wait_val("type", "ext4")
231 self.dialog_wait_val("crypto", " keep")
232 self.dialog_wait_val("crypto_options", "")
233 b.wait_visible(self.dialog_field("mount_options.ro") + ":checked")
234 self.dialog_set_val("type", "xfs")
235 self.dialog_set_val("old_passphrase", "vainu-reku-toma-rolle-kaja")
236 self.dialog_apply()
237 self.dialog_wait_close()
238 b.wait_visible(self.card("xfs filesystem"))
239
240 self.assertEqual(m.execute(f"lsblk -no RO {cleartext_dev}").strip(), "1")
241
242 def testNoFsys(self):
243 m = self.machine
244 b = self.browser
245
246 self.login_and_go("/storage")
247
248 # Add a disk and format it with luks, but without filesystem
249 disk = self.add_ram_disk()
250 self.click_card_row("Storage", name=disk)
251
252 self.click_card_dropdown("Unformatted data", "Format")
253 self.dialog({"type": "empty",
254 "crypto": self.default_crypto_type,
255 "passphrase": "vainu-reku-toma-rolle-kaja",
256 "passphrase2": "vainu-reku-toma-rolle-kaja"})
257 b.wait_visible(self.card("Unformatted data"))
258
259 # Lock it
260 b.click(self.card_button("Unformatted data", "Lock"))
261 self.confirm()
262 b.wait_visible(self.card("Locked data"))
263
264 # Make it readonly
265
266 # The locking above changes crypttab asynchronously, and there
267 # is no indication in the UI when cockpit has caught up with
268 # that. So we retry the dialog when it looks like Cockpit is
269 # out of synch...
270
271 self.dialog_with_error_retry(trigger=lambda: b.click(self.card_desc_action("Encryption", "Options")),
272 values={"options": "readonly"},
273 errors=["Didn't find entry to remove"])
274 self.assertNotEqual(m.execute("grep readonly /etc/crypttab"), "")
275
276 # Unlock it
277 b.click(self.card_button("Locked data", "Unlock"))
278 self.dialog({"passphrase": "vainu-reku-toma-rolle-kaja"})
279
280 # Should be unlocked readonly
281 uuid = m.execute(f"cryptsetup luksUUID {disk}").strip()
282 cleartext_dev = "/dev/mapper/luks-" + uuid
283 self.assertEqual(m.execute(f"lsblk -no RO {cleartext_dev}").strip(), "1")
284
285 # Now create a empty, encrypted partition
286 self.click_card_dropdown("Solid State Drive", "Create partition table")
287 self.dialog({"type": "gpt"})
288 b.wait_text(self.card_row_col("GPT partitions", 1, 1), "Free space")
289 self.click_dropdown(self.card_row("GPT partitions", 1), "Create partition")
290 self.dialog({"type": "empty",
291 "crypto": self.default_crypto_type,
292 "passphrase": "vainu-reku-toma-rolle-kaja",
293 "passphrase2": "vainu-reku-toma-rolle-kaja"})
294 b.wait_text(self.card_row_col("GPT partitions", 1, 2), "Unformatted data (encrypted)")
295
296 def testKeepKeys(self):
297 b = self.browser
298
299 self.login_and_go("/storage")
300
301 # Add a disk and format it with luks and a filesystem
302
303 disk = self.add_ram_disk()
304 self.click_card_row("Storage", name=disk)
305
306 self.click_card_dropdown("Unformatted data", "Format")
307 self.dialog({"type": "ext4",
308 "crypto": self.default_crypto_type,
309 "passphrase": "vainu-reku-toma-rolle-kaja",
310 "passphrase2": "vainu-reku-toma-rolle-kaja",
311 "mount_point": f"{self.mnt_dir}/foo"})
312 b.wait_visible(self.card("ext4 filesystem"))
313
314 # Format it again but keep the keys
315
316 self.click_card_dropdown("ext4 filesystem", "Format")
317 self.dialog({"type": "ext4",
318 "crypto": " keep",
319 "mount_point": f"{self.mnt_dir}/foo"})
320 b.wait_visible(self.card("ext4 filesystem"))
321
322 # Unmount (and lock) it
323
324 b.click(self.card_button("ext4 filesystem", "Unmount"))
325 self.confirm()
326 self.wait_not_mounted("Filesystem")
327
328 # Format it again and keep the keys. Because it's locked, we
329 # need the old passphrase.
330
331 self.click_card_dropdown("Filesystem", "Format")
332 self.dialog({"type": "ext4",
333 "crypto": " keep",
334 "old_passphrase": "vainu-reku-toma-rolle-kaja",
335 "mount_point": f"{self.mnt_dir}/foo"})
336 self.wait_mounted("ext4 filesystem")
337
338
339 @testlib.skipImage('/usr is immutable', 'centos-9-bootc')
340 class TestStorageLuksDestructive(storagelib.StorageCase):
341
342 # on RHEL 8, provide enough RAM for cryptsetup's PBKDF (https://issues.redhat.com/browse/RHEL-8258)
343 # On any newer OS, cryptsetup gets along with less RAM
344 provision = {
345 "0": {"memory_mb": 1400 if os.environ.get("TEST_OS", "").startswith("rhel-8-") else None}
346 }
347
348 def testLuks1Slots(self):
349 self.allow_journal_messages("Device is not initialized.*", ".*could not be opened.")
350 m = self.machine
351 b = self.browser
352
353 # This should work without any of the Clevis stuff.
354 m.execute("rm -f /usr/bin/luksmeta /usr/bin/clevis*")
355
356 mount_point_secret = "/run/secret"
357
358 error_base = "Error unlocking /dev/sda: Failed to activate device: "
359 error_messages = [error_base + "Operation not permitted",
360 error_base + "Incorrect passphrase."]
361
362 self.login_and_go("/storage")
363
364 m.add_disk("50M", serial="MYDISK")
365 dev = "/dev/sda"
366 self.click_card_row("Storage", name=dev)
367 # create volume and passphrase
368 self.click_card_dropdown("Unformatted data", "Format")
369 self.dialog({"type": "ext4",
370 "crypto": "luks1",
371 "name": "ENCRYPTED",
372 "mount_point": mount_point_secret,
373 "passphrase": "vainu-reku-toma-rolle-kaja",
374 "passphrase2": "vainu-reku-toma-rolle-kaja"},
375 secondary=True)
376 self.wait_not_mounted("Filesystem")
377 b.wait_text(self.card_desc("Encryption", "Encryption type"), "LUKS1")
378 b.wait_in_text(self.card_desc("Encryption", "Options"), "nofail")
379
380 uuid = m.execute(f"cryptsetup luksUUID {dev}").strip()
381 cleartext_dev = "/dev/mapper/luks-" + uuid
382
383 # add one more passphrase
384 panel = "#encryption-keys "
385 b.wait_visible(panel)
386 b.click(self.card("Encryption") + " [aria-label='Add']")
387 self.dialog_wait_open()
388 self.dialog_wait_apply_enabled()
389 self.dialog_set_val("new_passphrase", "vainu-reku-toma-rolle-kaja-1")
390 self.dialog_set_val("new_passphrase2", "vainu-reku-toma-rolle-kaja-1")
391 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja")
392 self.dialog_apply()
393 self.dialog_wait_close()
394 # unlock with first passphrase
395 b.click(self.card_button("Filesystem", "Mount"))
396 self.dialog({"passphrase": "vainu-reku-toma-rolle-kaja"})
397 self.wait_mounted("ext4 filesystem")
398 self.assert_not_in_configuration(dev, "crypttab", "options", "noauto")
399 self.assert_not_in_configuration(cleartext_dev, "fstab", "opts", "noauto")
400 # unlock with second passphrase
401 b.click(self.card_button("ext4 filesystem", "Unmount"))
402 self.confirm()
403 self.assert_in_configuration(dev, "crypttab", "options", "noauto")
404 self.assert_in_child_configuration(dev, "fstab", "opts", "noauto")
405 self.wait_not_mounted("Filesystem")
406 b.click(self.card_button("Filesystem", "Mount"))
407 self.dialog({"passphrase": "vainu-reku-toma-rolle-kaja-1"})
408 self.assert_not_in_configuration(dev, "crypttab", "options", "noauto")
409 self.assert_not_in_configuration(cleartext_dev, "fstab", "opts", "noauto")
410 self.wait_mounted("ext4 filesystem")
411 # delete second key slot
412 b.click(panel + "tr:nth-child(2) button[aria-label='Remove']")
413 # do not accept the same passphrase
414 b.set_input_text("#remove-passphrase", "vainu-reku-toma-rolle-kaja-1")
415 b.click("button:contains('Remove')")
416 b.wait_in_text(".pf-v6-c-alert__title", "No key available with this passphrase.")
417 # delete with passphrase from slot 0
418 b.set_input_text("#remove-passphrase", "vainu-reku-toma-rolle-kaja")
419 b.click("button:contains('Remove')")
420 with b.wait_timeout(30):
421 b.wait_not_present("#remove-passphrase")
422 # check that it is not possible to unlock with deleted passphrase
423 b.click(self.card_button("ext4 filesystem", "Unmount"))
424 self.confirm()
425 self.assert_in_configuration(dev, "crypttab", "options", "noauto")
426 self.assert_in_child_configuration(dev, "fstab", "opts", "noauto")
427 self.wait_not_mounted("Filesystem")
428 b.click(self.card_button("Filesystem", "Mount"))
429 self.dialog_wait_open()
430 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja-1")
431 self.dialog_apply()
432 b.wait_visible(".pf-v6-c-alert")
433 self.assertIn(b.text("h4.pf-v6-c-alert__title:not(span)").split("Danger alert:", 1).pop(), error_messages)
434 self.dialog_cancel()
435
436 # add more passphrases, seven exactly, to reach the limit of eight for LUKSv1
437 for i in range(1, 8):
438 b.click(self.card("Encryption") + " [aria-label='Add']")
439 self.dialog_wait_open()
440 self.dialog_wait_apply_enabled()
441 self.dialog_set_val("new_passphrase", f"vainu-reku-toma-rolle-kaja-{i}")
442 self.dialog_set_val("new_passphrase2", f"vainu-reku-toma-rolle-kaja-{i}")
443 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja")
444 self.dialog_apply()
445 self.dialog_wait_close()
446
447 # check if add button is inactive
448 b.wait_visible(self.card("Encryption") + " [aria-label='Add']:disabled")
449 # check if edit button is inactive
450 slots_row = panel + " tr:first-child"
451 b.wait_visible(slots_row + " button:disabled")
452
453 # remove one slot
454 slots_list = panel
455 b.wait_visible(slots_list + " td:contains('Slot 7')")
456 b.click(slots_list + "tr:last-child button[aria-label='Remove']")
457 b.set_input_text("#remove-passphrase", "vainu-reku-toma-rolle-kaja-6")
458 b.click("button:contains('Remove')")
459 with b.wait_timeout(30):
460 b.wait_not_present("#remove-passphrase")
461 # check if buttons have become enabled after removing last slot
462 b.wait_not_present(slots_list + ":disabled")
463 b.wait_not_present(panel + ":disabled")
464 # remove slot 0, with the original passphrase
465 b.click(slots_list + "tr:nth-child(1) button[aria-label='Remove']")
466 b.click("#force-remove-passphrase")
467 b.click("button:contains('Remove')")
468 with b.wait_timeout(30):
469 b.wait_not_present("#remove-passphrase")
470 # check that it is not possible to unlock with deleted passphrase
471 b.click(self.card_button("Filesystem", "Mount"))
472 self.dialog_wait_open()
473 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja")
474 self.dialog_apply()
475 b.wait_visible(".pf-v6-c-alert")
476 self.assertIn(b.text("h4.pf-v6-c-alert__title:not(span)").split("Danger alert:", 1).pop(), error_messages)
477 self.dialog_cancel()
478 # change one of the passphrases
479 b.click(slots_list + "tr:last-child [aria-label='Edit']")
480 self.dialog_wait_open()
481 self.dialog_wait_apply_enabled()
482 self.dialog_set_val("old_passphrase", "vainu-reku-toma-rolle-kaja-6")
483 self.dialog_set_val("new_passphrase", "vainu-reku-toma-rolle-kaja-8")
484 self.dialog_set_val("new_passphrase2", "vainu-reku-toma-rolle-kaja-8")
485 self.dialog_apply()
486 self.dialog_wait_close()
487 # unlock volume with the negwly created passphrase
488 b.click(self.card_button("Filesystem", "Mount"))
489 self.dialog({"passphrase": "vainu-reku-toma-rolle-kaja-8"})
490 self.wait_mounted("ext4 filesystem")
491 self.assert_not_in_configuration(dev, "crypttab", "options", "noauto")
492 self.assert_not_in_configuration(cleartext_dev, "fstab", "opts", "noauto")
493
494 def testReboot(self):
495 m = self.machine
496 b = self.browser
497
498 self.login_and_go("/storage")
499
500 # Add a disk and format it with luks and a filesystem, then
501 # reboot and check that the filesystem is mounted.
502
503 dev = "/dev/sda"
504 m.add_disk("50M", serial="MYDISK")
505 self.click_card_row("Storage", name=dev)
506
507 self.click_card_dropdown("Unformatted data", "Format")
508 self.dialog({"type": "ext4",
509 "crypto": self.default_crypto_type,
510 "passphrase": "vainu-reku-toma-rolle-kaja",
511 "passphrase2": "vainu-reku-toma-rolle-kaja",
512 "mount_point": f"{self.mnt_dir}/foo"})
513 self.wait_mounted("ext4 filesystem")
514
515 self.setup_systemd_password_agent("vainu-reku-toma-rolle-kaja")
516 self.reboot()
517 m.start_cockpit()
518 b.relogin()
519 b.enter_page("/storage")
520
521 self.wait_mounted("ext4 filesystem")
522
523
524 @testlib.skipImage("clevis/tang not installed", "*-bootc")
525 class TestStorageNBDE(storagelib.StorageCase, packagelib.PackageCase):
526 provision = {
527 "0": {"address": "10.111.112.1/20", "memory_mb": 2048},
528 "tang": {"address": "10.111.112.5/20"}
529 }
530
531 def testBasic(self):
532 m = self.machine
533 b = self.browser
534
535 # Only Arch gets it right...
536 need_fixing = (m.image != "arch")
537
538 mount_point_secret = "/run/secret"
539
540 tang_m = self.machines["tang"]
541 tang_m.execute("systemctl start tangd.socket")
542 tang_m.execute("firewall-cmd --add-port 80/tcp")
543
544 if need_fixing:
545 self.addPackageSet("clevis")
546 self.enableRepo()
547
548 self.login_and_go("/storage")
549
550 # Add a disk and format it with luks
551 dev = "/dev/sda"
552 m.add_disk("50M", serial="MYDISK")
553 self.click_card_row("Storage", name=dev)
554
555 self.click_card_dropdown("Unformatted data", "Format")
556 self.dialog({"type": "ext4",
557 "crypto": self.default_crypto_type,
558 "name": "ENCRYPTED",
559 "mount_point": mount_point_secret,
560 "passphrase": "vainu-reku-toma-rolle-kaja",
561 "passphrase2": "vainu-reku-toma-rolle-kaja"},
562 secondary=True)
563 self.wait_not_mounted("Filesystem")
564
565 b.wait_in_text(self.card_desc("Encryption", "Options"), "nofail")
566 panel = "#encryption-keys "
567 b.wait_in_text(panel + "tr:nth-child(1)", "Passphrase")
568
569 # Add a key
570 #
571 b.click(self.card("Encryption") + " [aria-label='Add']")
572 self.dialog_wait_open()
573 self.dialog_wait_apply_enabled()
574 self.dialog_set_val("type", "tang")
575 self.dialog_set_val("tang_url", "10.111.112.5")
576 self.dialog_set_val("passphrase", "wrong-passphrase")
577 self.dialog_apply()
578 b.wait_in_text("#dialog", "Check the key hash with the Tang server")
579 b.wait_in_text("#dialog", tang_m.execute("tang-show-keys").strip())
580 self.dialog_apply()
581 with b.wait_timeout(60):
582 if need_fixing:
583 b.wait_in_text("#dialog", "Add Network Bound Disk Encryption")
584 b.wait_in_text("#dialog", "The clevis-systemd package must be installed")
585 self.dialog_apply()
586 b.wait_in_text("#dialog", "No key available with this passphrase.")
587 self.dialog_set_val("passphrase", "vainu-reku-toma-rolle-kaja")
588 self.dialog_apply()
589 self.dialog_wait_close()
590 b.wait_in_text(panel + "tr:nth-child(2)", "10.111.112.5")
591
592 # Adding the key should add "_netdev" options
593 #
594 b.wait_in_text(self.card_desc("Filesystem", "Mount point"), "after network")
595 b.wait_in_text(self.card_desc("Encryption", "Options"), "_netdev")
596
597 # Add a clevis config that Cockpit doesn't understand, make
598 # sure it can't be edited but can be removed.
599 #
600 thp = tang_m.execute("tang-show-keys").strip()
601 m.execute(f"""echo vainu-reku-toma-rolle-kaja | clevis luks bind -d /dev/sda sss '{{"t":1,"pins":{{"tang":[{{"url":"10.111.112.5","thp":"{thp}"}}]}}}}'""")
602 b.wait_in_text(panel + "tr:nth-child(3)", "Unknown type")
603 b.wait_in_text(panel + "tr:nth-child(3)", "sss")
604 b.wait_visible(panel + "tr:nth-child(3) [aria-label='Edit']:disabled")
605 b.click(panel + 'tr:nth-child(3) button[aria-label="Remove"]')
606 b.wait_in_text("#dialog", "Remove key in slot 2?")
607 self.confirm()
608 b.wait_not_present(panel + "tr:nth-child(3)")
609
610 # Mount it. This should succeed without passphrase.
611 #
612 b.click(self.card_button("Filesystem", "Mount"))
613 self.dialog_wait_open()
614 self.dialog_wait_val("mount_point", mount_point_secret)
615 self.dialog_apply()
616 self.dialog_wait_close()
617 self.wait_mounted("ext4 filesystem")
618
619 # LUKS should be read/write
620 uuid = m.execute(f"cryptsetup luksUUID {dev}").strip()
621 cleartext_dev = "/dev/mapper/luks-" + uuid
622 self.assertEqual(m.execute(f"lsblk -no RO {cleartext_dev}").strip(), "0")
623
624 # Remount "readonly", LUKS should be readonly
625 b.click(self.card_button("ext4 filesystem", "Unmount"))
626 self.confirm()
627 b.click(self.card_button("Filesystem", "Mount"))
628 self.dialog_wait_open()
629 self.dialog_set_val("mount_options.ro", val=True)
630 self.dialog_apply()
631 self.dialog_wait_close()
632 self.wait_mounted("ext4 filesystem")
633
634 # Now LUKS should be readonly
635 self.assertEqual(m.execute(f"lsblk -no RO {cleartext_dev}").strip(), "1")
636
637 # Edit the key, without providing an existing passphrase
638 #
639 b.click(panel + "tr:nth-child(2) [aria-label='Edit']")
640 self.dialog_wait_open()
641 self.dialog_wait_apply_enabled()
642 self.dialog_wait_val("tang_url", "10.111.112.5")
643 self.dialog_set_val("tang_url", "http://10.111.112.5/")
644 self.dialog_apply()
645 b.wait_in_text("#dialog", "Check the key hash with the Tang server")
646 b.wait_in_text("#dialog", tang_m.execute("tang-show-keys").strip())
647 self.dialog_apply()
648 self.dialog_wait_close()
649 b.wait_in_text(panel + "tr:nth-child(2)", "http://10.111.112.5/")
650
651 # Reset the options so that we can check that they get added
652 # also when a second key is added.
653 #
654 b.click(self.card_desc("ext4 filesystem", "Mount point") + " button")
655 self.dialog({"at_boot": "nofail"})
656 b.wait_in_text(self.card_desc("Encryption", "Options"), "nofail")
657
658 if need_fixing:
659 # Break things again, including PackageKit
660 m.execute("if type dnf; then dnf remove -y clevis-systemd; else apt-get purge -y clevis-systemd; fi")
661 m.execute("systemctl mask --now packagekit")
662 if m.image.startswith("fedora-"):
663 m.execute("systemctl mask --now dnf5daemon-server")
664
665 # Add a second key, this should try to fix things again, but
666 # we have to help with the package install
667 #
668 b.click(self.card("Encryption") + " [aria-label='Add']")
669 self.dialog_wait_open()
670 self.dialog_wait_apply_enabled()
671 self.dialog_set_val("type", "tang")
672 self.dialog_set_val("tang_url", "http://10.111.112.5")
673 self.dialog_apply()
674 b.wait_in_text("#dialog", "Check the key hash with the Tang server")
675 b.wait_in_text("#dialog", tang_m.execute("tang-show-keys").strip())
676 self.dialog_apply()
677 with b.wait_timeout(60):
678 if need_fixing:
679 b.wait_in_text("#dialog", "Add Network Bound Disk Encryption")
680 b.wait_in_text("#dialog", "The clevis-systemd package must be installed")
681 self.dialog_apply()
682 b.wait_in_text("#dialog", "PackageKit or dnf5daemon-server is not installed")
683 self.dialog_cancel()
684 self.dialog_wait_close()
685
686 # Manually install the missing package, Cockpit should
687 # be happy with that even when PackageKit is not
688 # available. Also disable the unit, for variety.
689 # Cockpit will enable it without needing explicit
690 # confirmation.
691 #
692 m.execute("if type dnf; then dnf install -y clevis-systemd; else apt-get install -y clevis-systemd; fi")
693 m.execute("systemctl disable --now clevis-luks-askpass.path")
694
695 b.click(self.card("Encryption") + " [aria-label='Add']")
696 self.dialog_wait_open()
697 self.dialog_wait_apply_enabled()
698 self.dialog_set_val("type", "tang")
699 self.dialog_set_val("tang_url", "http://10.111.112.5")
700 self.dialog_apply()
701 b.wait_in_text("#dialog", "Check the key hash with the Tang server")
702 b.wait_in_text("#dialog", tang_m.execute("tang-show-keys").strip())
703 self.dialog_apply()
704
705 self.dialog_wait_close()
706
707 b.wait_in_text(panel + "tr:nth-child(3)", "http://10.111.112.5")
708
709 # This should bring the options back.
710 #
711 b.wait_in_text(self.card_desc("ext4 filesystem", "Mount point"), "after network")
712 b.wait_in_text(self.card_desc("Encryption", "Options"), "_netdev")
713
714 # Reboot
715 #
716 self.reboot()
717 m.start_cockpit()
718 b.relogin()
719 b.enter_page("/storage")
720
721 self.wait_mounted("ext4 filesystem")
722
723 # Remove one key on client
724 #
725 b.click(panel + 'tr[data-row-slot="1"] button[aria-label="Remove"]')
726 self.confirm()
727 b.wait_not_present(panel + 'tr[data-row-slot="1"]')
728
729 @testlib.skipImage("TODO: don't know how to encrypt the rootfs", "debian-*", "ubuntu-*", "arch")
730 # this doesn't work in ostree either, but we don't run storage tests there
731 @testlib.skipWsContainer("newly built root doesn't contain ws container")
732 @testlib.timeout(1200)
733 def testRootReboot(self):
734 m = self.machine
735 b = self.browser
736
737 tang_m = self.machines["tang"]
738 tang_m.execute("systemctl start tangd.socket")
739 tang_m.execute("firewall-cmd --add-port 80/tcp")
740
741 try:
742 self.modify_rootfs(passphrase="einszweidrei")
743 except Exception:
744 console_screenshot(m, "failed-encrypt.ppm")
745 raise
746
747 self.assertIn("crypt", m.execute("lsblk -snlo TYPE $(findmnt -no SOURCE /)"))
748
749 self.addPackageSet("clevis")
750 self.enableRepo()
751
752 self.login_and_go("/storage")
753
754 # While we are here, let's quickly test that Cockpit warns
755 # about renaming the volume group that contains the root
756 # filesystem.
757
758 b.go("#/vg/root")
759 b.click(self.card_desc_action("LVM2 volume group", "Name"))
760 self.dialog_wait_open()
761 b.wait_in_text("#dialog", "This volume group contains the root filesystem.")
762 b.wait_visible("#dialog button.apply.pf-m-danger")
763 self.dialog_cancel()
764 self.dialog_wait_close()
765 b.go("#/")
766
767 # Add a clevis key and then reboot.
768 #
769 # We also remove the original passphrase in order to be sure
770 # that it was in fact clevis that has unlocked the rootfs, and
771 # not the magic provided by "modify_rootfs"
772
773 self.click_card_row("Storage", location="root")
774
775 panel = "#encryption-keys "
776 b.wait_in_text(panel + "tr:nth-child(1)", "Passphrase")
777
778 with b.wait_timeout(360):
779 b.click(self.card("Encryption") + " [aria-label='Add']")
780 self.dialog_wait_open()
781 self.dialog_wait_apply_enabled()
782 self.dialog_set_val("type", "tang")
783 self.dialog_set_val("tang_url", "10.111.112.5")
784 self.dialog_set_val("passphrase", "einszweidrei")
785 self.dialog_apply()
786 b.wait_in_text("#dialog", "Check the key hash with the Tang server")
787 b.wait_in_text("#dialog", tang_m.execute("tang-show-keys").strip())
788 self.dialog_apply()
789 b.wait_in_text("#dialog", "Add Network Bound Disk Encryption")
790 b.wait_in_text("#dialog", "The clevis-dracut package must be installed")
791 b.wait_in_text("#dialog", "The initrd must be regenerated")
792 self.dialog_apply()
793 self.dialog_wait_close()
794
795 b.click(panel + "tr:nth-child(1) button[aria-label='Remove']")
796 b.click("#force-remove-passphrase")
797 b.click("button:contains('Remove')")
798 b.wait_in_text(panel + "tr:nth-child(1)", "Keyserver")
799
800 # Tell the initrd to configure our special inter-machine
801 # network that has the "tang" machine.
802 routes = json.loads(m.execute("ip --json route show 10.111.112.0/20"))
803 iface = routes[0]["dev"]
804 m.execute(f"grubby --update-kernel=ALL --args='ip=10.111.112.1::10.111.112.1:255.255.255.0::{iface}:off'")
805
806 try:
807 self.reboot(timeout_sec=300)
808 except Exception:
809 console_screenshot(m, "failed-reboot.ppm")
810 raise
811
812 m.start_cockpit()
813 b.relogin()
814 b.enter_page("/storage")
815
816 self.assertIn("crypt", m.execute("lsblk -snlo TYPE $(findmnt -no SOURCE /)"))
817
818
819 if __name__ == '__main__':
820 testlib.test_main()