Debian Codegraph @

pkg/storaged/stratis/pool.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8 import client from "../client";
9
10 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
11 import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
12 import { CardHeader, CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
13 import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
14 import { Table, Tbody, Tr, Td } from '@patternfly/react-table';
15 import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
16
17 import { VolumeIcon } from "../icons/gnome-icons.jsx";
18 import { fmt_to_fragments } from "utils.jsx";
19
20 import { StorageButton, StorageUsageBar, StorageLink, StorageOnOff } from "../storage-controls.jsx";
21 import {
22 StorageCard, StorageDescription, ChildrenTable, PageTable, Actions,
23 new_page, new_card, PAGE_CATEGORY_VIRTUAL,
24 get_crossrefs, navigate_away_from_card
25 } from "../pages.jsx";
26 import {
27 get_active_usage, teardown_active_usage, for_each_async,
28 get_available_spaces, prepare_available_spaces,
29 decode_filename, should_ignore,
30 } from "../utils.js";
31
32 import {
33 dialog_open, SelectSpaces, TextInput, PassInput, SelectOne, SizeSlider, CheckBoxes, Group, Message,
34 BlockingMessage, TeardownMessage,
35 init_teardown_usage
36 } from "../dialog.jsx";
37
38 import { validate_url, get_tang_adv } from "../crypto/tang.jsx";
39 import { is_valid_mount_point } from "../filesystem/utils.jsx";
40 import { at_boot_input, update_at_boot_input, mount_options } from "../filesystem/mounting-dialog.jsx";
41
42 import {
43 validate_pool_name, std_reply, with_stored_passphrase,
44 confirm_tang_trust,
45 validate_fs_name, set_mount_options, destroy_filesystem
46 } from "./utils.jsx";
47 import { make_stratis_filesystem_page } from "./filesystem.jsx";
48
49 const _ = cockpit.gettext;
50
51 const fsys_min_size = 512 * 1024 * 1024;
52 const fsys_round_size = 1024 * 1024;
53
54 /* Abstractions over the r6 and r8 API revisions, and V1 and V2 pool
55 * formats.
56 */
57
58 /* Key descriptions are returned as an array with elements of type
59 *
60 * { slot: number | null, keydesc: string }
61 *
62 * A V1 pool will have at most one entry in the array, and it will
63 * have `slot == null`. V2 pools have zero or more entries with
64 * numbered slots.
65 */
66
67 function get_key_descriptions(pool) {
68 const result = [];
69
70 if (!pool.Encrypted)
71 return result;
72
73 if (client.stratis_interface_revision < 8) {
74 const key_desc = pool.KeyDescription;
75 /* The KeyDescription property has type "(b(bs))".
76
77 The first "b" is false when the encryption of the pool is
78 inconsistent (not all block devices have the same LUKS
79 header). We pretend that the pool doesn't have any
80 passphrase in that case.
81
82 The second "b" is true when the pool has a passphrase, and
83 the "s" is the description of the key for it.
84
85 Thus, we detect an object of shape
86
87 [ true, [ true, "..." ] ]
88
89 and fish the string out of it.
90 */
91 if (key_desc[0] && key_desc[1][0])
92 result.push({ slot: null, keydesc: key_desc[1][1] });
93 } else {
94 let key_descs = pool.KeyDescriptions;
95 /* The KeyDescriptions property itself has type "v". For a V1
96 pool this variant carries a value of type "b(bs)" just as
97 with the r6 API (see above). For a V2 pool, it carries a
98 "a(us)" value, an array of "slot number plus key
99 description".
100
101 Since the property itself has type "v", we need to unwrap
102 this variant explicitly.
103 */
104
105 /* HACK - https://github.com/stratis-storage/stratisd/issues/3895
106
107 Introduced in stratisd 3.8.0, fixed in stratisd 3.8.3.
108
109 Change notifications drop the variant wrapping for some
110 reason, so we only do the unwrapping when "key_descs" is
111 indeed a variant.
112 */
113 if ("t" in key_descs)
114 key_descs = key_descs.v;
115 if (pool.MetadataVersion == 1) {
116 if (key_descs[0] && key_descs[1][0])
117 result.push({ slot: null, keydesc: key_descs[1][1] });
118 } else if (pool.MetadataVersion == 2) {
119 for (const key_desc of key_descs)
120 result.push({ slot: key_desc[0], keydesc: key_desc[1] });
121 }
122 }
123
124 return result;
125 }
126
127 /* Clevis information is returned as an array with elements of type
128 *
129 * { slot: number | null, pin: "tang", url: string }
130 * | { slot: number | null, pin: string }
131 *
132 * A V1 pool will have at most one entry in the array, and it will
133 * have `slot == null`. V2 pools have zero or more entries with
134 * numbered slots.
135 */
136
137 function get_clevis_infos(pool) {
138 const result = [];
139
140 if (!pool.Encrypted)
141 return result;
142
143 if (client.stratis_interface_revision < 8) {
144 const clevis_info = pool.ClevisInfo;
145 /* The ClevisInfo property has type "(b(b(ss)))".
146
147 The first "b" is false when the encryption of the pool is
148 inconsistent (not all block devices have the same LUKS
149 header). We pretend that the pool doesn't have any Clevis
150 infos in that case.
151
152 The second "b" is true when the pool has a Clevis info, and
153 the two "s" are the pin and its JSON encoded configuration,
154 respectively.
155
156 Thus, we detect an object of shape
157
158 [ true, [ true, [ "...", "..." ] ] ]
159
160 and fish the strings out of it.
161
162 Cockpit only knows about the "tang" pin in detail, so we
163 detect that as well and only then decode the configuration
164 in order to extract the url from it.
165 */
166 if (clevis_info[0] && clevis_info[1][0]) {
167 if (clevis_info[1][1][0] == "tang") {
168 const config = JSON.parse(clevis_info[1][1][1]);
169 result.push({ slot: null, pin: "tang", url: config.url });
170 } else {
171 result.push({ slot: null, pin: clevis_info[1][1][0] });
172 }
173 }
174 } else {
175 let clevis_infos = pool.ClevisInfos;
176 /* The ClevisInfos property itself has type "v". For a V1 pool
177 this variant carries a value of type "b(b(ss)" just as with
178 the r6 API (see above). For a V2 pool, it carries a "a(u(ss))"
179 value, an array of "slot number plus pin-plus-config".
180
181 Since the property itself has type "v", we need to unwrap
182 this variant explicitly.
183 */
184
185 /* HACK - https://github.com/stratis-storage/stratisd/issues/3895
186
187 Introduced in stratisd 3.8.0, fixed in stratisd 3.8.3.
188
189 Change notifications drop the variant wrapping for some
190 reason, so we only do the unwrapping when "clevis_infos" is
191 indeed a variant.
192 */
193 if ("t" in clevis_infos)
194 clevis_infos = clevis_infos.v;
195 if (pool.MetadataVersion == 1) {
196 if (clevis_infos[0] && clevis_infos[1][0]) {
197 if (clevis_infos[1][1][0] == "tang") {
198 const config = JSON.parse(clevis_infos[1][1][1]);
199 result.push({ slot: null, pin: "tang", url: config.url });
200 } else {
201 result.push({ slot: null, pin: clevis_infos[1][1][0] });
202 }
203 }
204 } else if (pool.MetadataVersion == 2) {
205 for (const clevis_info of clevis_infos) {
206 if (clevis_info[1][0] == "tang") {
207 const config = JSON.parse(clevis_info[1][1]);
208 result.push({ slot: clevis_info[0], pin: "tang", url: config.url });
209 } else {
210 result.push({ slot: clevis_info[0], pin: clevis_info[1][0] });
211 }
212 }
213 }
214 }
215
216 return result;
217 }
218
219 function bind_keyring(pool, keydesc) {
220 if (client.stratis_interface_revision < 8)
221 return pool.BindKeyring(keydesc);
222 else
223 return pool.BindKeyring(keydesc, [false, 0]);
224 }
225
226 function rebind_keyring(pool, keydesc, slot) {
227 if (client.stratis_interface_revision < 8)
228 return pool.RebindKeyring(keydesc);
229 else
230 return pool.RebindKeyring(keydesc, [slot !== null, slot || 0]);
231 }
232
233 function unbind_keyring(pool, slot) {
234 if (client.stratis_interface_revision < 8)
235 return pool.UnbindKeyring();
236 else
237 return pool.UnbindKeyring([slot !== null, slot || 0]);
238 }
239
240 function bind_clevis(pool, pin, config) {
241 if (client.stratis_interface_revision < 8)
242 return pool.BindClevis(pin, config);
243 else
244 return pool.BindClevis(pin, config, [false, 0]);
245 }
246
247 function unbind_clevis(pool, slot) {
248 if (client.stratis_interface_revision < 8)
249 return pool.UnbindClevis();
250 else
251 return pool.UnbindClevis([slot !== null, slot || 0]);
252 }
253
254 /* Utilities for key descriptions and passphrases
255 */
256
257 export async function get_stored_keydescs() {
258 return await client.stratis_manager.ListKeys().catch(() => []);
259 }
260
261 async function get_new_keydesc(pool) {
262 const key_descs = get_key_descriptions(pool);
263 const stored_keydescs = await get_stored_keydescs();
264
265 // Let's arbitrarily stop after 1000 tries to avoid excessive
266 // looping. That shouldn't happen, but...
267 let desc;
268 for (let i = 0; i < 1000; i++) {
269 desc = pool.Name + (i > 0 ? "." + i.toFixed() : "");
270 if (!key_descs.find(kd => kd.keydesc == desc) && !stored_keydescs.includes(desc))
271 break;
272 }
273 return desc;
274 }
275
276 function PoolPassphrase(tag, pool, main, stored_keydescs, force) {
277 const all_key_descs = get_key_descriptions(pool);
278 const clevis_infos = get_clevis_infos(pool);
279
280 const available_key_descs = all_key_descs.filter(kd => !stored_keydescs.includes(kd.keydesc));
281 const can_use_passphrase = available_key_descs.length > 0;
282 const have_stored_passphrase = all_key_descs.length > available_key_descs.length;
283 const need_passphrase = (force || clevis_infos.length == 0) && !have_stored_passphrase;
284 const single_tang_url = (clevis_infos.length == 1 && clevis_infos[0].pin == "tang" && !have_stored_passphrase && clevis_infos[0].url);
285 const only_tang = clevis_infos.every(ci => ci.pin == "tang") && !have_stored_passphrase;
286
287 if (can_use_passphrase) {
288 let extra_explanation;
289 if (need_passphrase) {
290 extra_explanation = _("Please provide an existing pool passphrase.");
291 } else if (single_tang_url) {
292 extra_explanation = cockpit.format(_("If the keyserver at $0 is not reachable, you can provide an existing passphrase."), single_tang_url);
293 } else if (only_tang) {
294 extra_explanation = _("If none of the keyservers is reachable, you can provide an existing passphrase.");
295 } else {
296 /* Clevis other than "tang" and/or passphrases already in the keyring.
297 */
298 extra_explanation = _("If none of the non-interactive unlock methods works, you can provide an existing passphrase.");
299 }
300
301 return PassInput(tag, _("Pool passphrase"), {
302 validate: val => need_passphrase && !val.length && _("Passphrase cannot be empty"),
303 explanation: main + " " + extra_explanation,
304 });
305 } else if (single_tang_url) {
306 return Message(main + " " + cockpit.format(_("The keyserver at $0 must be reachable."), single_tang_url));
307 } else if (only_tang) {
308 return Message(main + " " + _("At least one keyserver must be reachable."));
309 } else {
310 /* Clevis other than "tang" and/or passphrases already in the keyring.
311 */
312 return Message(main + " " + _("At least one of the non-interactive unlock methods must work."));
313 }
314 }
315
316 async function with_pool_passphrase(pool, passphrase, func) {
317 const stored_keydescs = await get_stored_keydescs();
318 const key_descs = get_key_descriptions(pool).filter(kd => !stored_keydescs.includes(kd.key_descs));
319
320 if (!passphrase || key_descs.length == 0)
321 return func();
322
323 let err;
324
325 for (const kd of key_descs) {
326 try {
327 return await with_stored_passphrase(client, kd.keydesc, passphrase, func);
328 } catch (e) {
329 err = e;
330 }
331 }
332
333 throw err;
334 }
335
336 /* Operations
337 */
338
339 function destroy_pool(pool) {
340 return for_each_async(client.stratis_pool_filesystems[pool.path], fsys => destroy_filesystem(fsys))
341 .then(() => client.stratis_manager.DestroyPool(pool.path).then(std_reply));
342 }
343
344 function create_fs(pool) {
345 const filesystems = client.stratis_pool_filesystems[pool.path];
346 const stats = client.stratis_pool_stats[pool.path];
347 const forced_options = ["x-systemd.requires=stratis-fstab-setup@" + pool.Uuid + ".service"];
348
349 let action_variants;
350 if (!client.in_anaconda_mode()) {
351 action_variants = [
352 { tag: null, Title: _("Create and mount") },
353 { tag: "nomount", Title: _("Create only") },
354 ];
355 } else {
356 action_variants = [
357 { tag: "nomount", Title: _("Create") },
358 ];
359 }
360
361 dialog_open({
362 Title: _("Create filesystem"),
363 Fields: [
364 TextInput("name", _("Name"),
365 {
366 validate: name => validate_fs_name(null, name, filesystems)
367 }),
368 Group(_("Stratis filesystem"), [
369 CheckBoxes("set_custom_size", null,
370 {
371 value: {
372 enabled: !pool.Overprovisioning,
373 },
374 fields: [
375 { tag: "enabled", title: _("Set initial size") },
376 ]
377 }),
378 SizeSlider("size", null,
379 {
380 visible: vals => vals.set_custom_size.enabled,
381 min: fsys_min_size,
382 max: pool.Overprovisioning ? stats.pool_total : stats.pool_free,
383 allow_infinite: pool.Overprovisioning,
384 round: fsys_round_size,
385 }),
386 CheckBoxes("set_custom_limit", null,
387 {
388 value: {
389 enabled: false,
390 },
391 fields: [
392 { tag: "enabled", title: _("Limit size") },
393 ]
394 }),
395 SizeSlider("limit", null,
396 {
397 visible: vals => vals.set_custom_limit.enabled,
398 min: fsys_min_size,
399 max: pool.Overprovisioning ? stats.pool_total : stats.pool_free,
400 allow_infinite: true,
401 round: fsys_round_size,
402 }),
403 ]),
404 TextInput("mount_point", _("Mount point"),
405 {
406 validate: (val, values, variant) => {
407 return is_valid_mount_point(client,
408 null,
409 client.add_mount_point_prefix(val),
410 variant == "nomount");
411 }
412 }),
413 mount_options(false, false),
414 at_boot_input(),
415 ],
416 update: update_at_boot_input,
417 Action: {
418 Variants: action_variants,
419 action: async function (vals) {
420 let size_spec = [false, ""]; let limit_spec = [false, ""];
421 if (vals.set_custom_size.enabled)
422 size_spec = [true, vals.size.toString()];
423 if (vals.set_custom_limit.enabled)
424 limit_spec = [true, vals.limit.toString()];
425 const result = await pool.CreateFilesystems([[vals.name, size_spec, limit_spec]]).then(std_reply);
426 if (result[0])
427 await set_mount_options(result[1][0][0], vals, forced_options);
428 }
429 }
430 });
431 }
432
433 function delete_pool(pool, card) {
434 const usage = get_active_usage(client, pool.path, _("delete"));
435
436 if (usage.Blocking) {
437 dialog_open({
438 Title: cockpit.format(_("$0 is in use"),
439 pool.Name),
440 Body: BlockingMessage(usage)
441 });
442 return;
443 }
444
445 dialog_open({
446 Title: cockpit.format(_("Permanently delete $0?"), pool.Name),
447 Teardown: TeardownMessage(usage),
448 Action: {
449 Danger: _("Deleting a Stratis pool will erase all data it contains."),
450 Title: _("Delete"),
451 action: async function () {
452 await teardown_active_usage(client, usage);
453 await destroy_pool(pool);
454 navigate_away_from_card(card);
455 }
456 },
457 Inits: [
458 init_teardown_usage(client, usage)
459 ]
460 });
461 }
462
463 function rename_pool(pool) {
464 dialog_open({
465 Title: _("Rename Stratis pool"),
466 Fields: [
467 TextInput("name", _("Name"),
468 {
469 value: pool.Name,
470 validate: name => validate_pool_name(pool, name)
471 })
472 ],
473 Action: {
474 Title: _("Rename"),
475 action: function (vals) {
476 return pool.SetName(vals.name).then(std_reply);
477 }
478 }
479 });
480 }
481
482 async function add_disks(pool) {
483 const blockdevs = client.stratis_pool_blockdevs[pool.path] || [];
484 const is_v1_pool = client.stratis_interface_revision < 8 || pool.MetadataVersion == 1;
485 const stored_keydescs = await get_stored_keydescs();
486
487 dialog_open({
488 Title: _("Add block devices"),
489 Fields: [
490 SelectOne("tier", _("Tier"), {
491 choices: [
492 { value: "data", title: _("Data") },
493 {
494 value: "cache",
495 title: _("Cache"),
496 }
497 ]
498 }),
499 SelectSpaces("disks", _("Block devices"), {
500 empty_warning: _("No disks are available."),
501 validate: function(disks) {
502 if (disks.length === 0)
503 return _("At least one disk is needed.");
504 },
505 spaces: get_available_spaces()
506 }),
507 ...(is_v1_pool
508 ? [PoolPassphrase("pool_passphrase", pool, _("Adding blockdevices requires unlocking the pool."), stored_keydescs, true)]
509 : []),
510 ],
511 Action: {
512 Title: _("Add"),
513 action: function(vals) {
514 return prepare_available_spaces(client, vals.disks)
515 .then(paths => {
516 const devs = paths.map(p => decode_filename(client.blocks[p].PreferredDevice));
517
518 function add() {
519 if (vals.tier == "data") {
520 return pool.AddDataDevs(devs).then(std_reply);
521 } else if (vals.tier == "cache") {
522 const has_cache = blockdevs.some(bd => bd.Tier == 1);
523 const method = has_cache ? "AddCacheDevs" : "InitCache";
524 return pool[method](devs).then(std_reply);
525 }
526 }
527
528 return with_pool_passphrase(pool, vals.pool_passphrase, add);
529 });
530 }
531 }
532 });
533 }
534
535 function make_stratis_filesystem_pages(parent, pool) {
536 const filesystems = client.stratis_pool_filesystems[pool.path];
537 const stats = client.stratis_pool_stats[pool.path];
538 const forced_options = ["x-systemd.requires=stratis-fstab-setup@" + pool.Uuid + ".service"];
539
540 filesystems.forEach((fs, i) => make_stratis_filesystem_page(parent, pool, fs,
541 stats.fsys_offsets[i],
542 forced_options));
543 }
544
545 export function make_stratis_pool_page(parent, pool) {
546 const degraded_ops = pool.AvailableActions && pool.AvailableActions !== "fully_operational";
547 const blockdevs = client.stratis_pool_blockdevs[pool.path] || [];
548 const can_grow = blockdevs.some(bd => (bd.NewPhysicalSize[0] &&
549 Number(bd.NewPhysicalSize[1]) > Number(bd.TotalPhysicalSize)));
550 const stats = client.stratis_pool_stats[pool.path];
551
552 const use = pool.TotalPhysicalUsed[0] && [Number(pool.TotalPhysicalUsed[1]), Number(pool.TotalPhysicalSize)];
553
554 if (should_ignore(client, pool.path))
555 return;
556
557 const pool_card = new_card({
558 title: pool.Encrypted ? _("Encrypted Stratis pool") : _("Stratis pool"),
559 next: null,
560 page_location: ["pool", pool.Uuid],
561 page_name: pool.Name,
562 page_icon: VolumeIcon,
563 page_category: PAGE_CATEGORY_VIRTUAL,
564 page_size: (use
565 ? <StorageUsageBar key="s" stats={use} short />
566 : Number(pool.TotalPhysicalSize)),
567 component: StratisPoolCard,
568 props: { pool, degraded_ops, can_grow, stats },
569 actions: [
570 {
571 title: _("Add block devices"),
572 action: () => add_disks(pool),
573 },
574 {
575 title: _("Delete pool"),
576 action: () => delete_pool(pool, pool_card),
577 danger: true,
578 },
579 ],
580 });
581
582 let crypto_card = null;
583 if (pool.Encrypted) {
584 crypto_card = new_card({
585 title: _("Encryption tokens"),
586 next: pool_card,
587 component: StratisEncryptionCard,
588 props: { pool },
589 });
590 }
591
592 const fsys_card = new_card({
593 title: _("Stratis filesystems"),
594 next: crypto_card || pool_card,
595 has_warning: degraded_ops || can_grow,
596 component: StratisFilesystemsCard,
597 props: { pool, degraded_ops, can_grow, stats },
598 actions: [
599 {
600 title: _("Create new filesystem"),
601 action: () => create_fs(pool),
602 excuse: ((!pool.Overprovisioning && stats.pool_free < fsys_min_size)
603 ? _("Not enough free space")
604 : null),
605 },
606 ],
607 });
608
609 const p = new_page(parent, fsys_card);
610 make_stratis_filesystem_pages(p, pool);
611 }
612
613 const StratisFilesystemsCard = ({ card, pool, degraded_ops, can_grow, stats }) => {
614 const blockdevs = client.stratis_pool_blockdevs[pool.path] || [];
615
616 function grow_blockdevs() {
617 return for_each_async(blockdevs, bd => pool.GrowPhysicalDevice(bd.Uuid));
618 }
619
620 const alerts = [];
621 if (can_grow) {
622 alerts.push(
623 <Alert isInline key="unused"
624 variant="warning"
625 title={_("This pool does not use all the space on its block devices.")}>
626 {_("Some block devices of this pool have grown in size after the pool was created. The pool can be safely grown to use the newly available space.")}
627 <div className="storage-alert-actions">
628 <StorageButton onClick={grow_blockdevs}>
629 {_("Grow the pool to take all space")}
630 </StorageButton>
631 </div>
632 </Alert>);
633 }
634
635 if (degraded_ops) {
636 const goToStratisLogs = () => cockpit.jump("/system/logs/#/?prio=warn&_SYSTEMD_UNIT=stratisd.service");
637 alerts.push(
638 <Alert isInline key="degraded"
639 variant="warning"
640 title={_("This pool is in a degraded state.")}>
641 <div className="storage-alert-actions">
642 <Button variant="link" isInline onClick={goToStratisLogs}>
643 {_("View logs")}
644 </Button>
645 </div>
646 </Alert>);
647 }
648
649 return (
650 <StorageCard card={card} alerts={alerts}>
651 <ChildrenTable
652 emptyCaption={_("No filesystems")}
653 aria-label={_("Stratis filesystems pool")}
654 page={card.page} />
655 </StorageCard>
656 );
657 };
658
659 const StratisV1TokenDescriptions = ({
660 key_descs,
661 add_passphrase,
662 change_passphrase,
663 remove_passphrase,
664 clevis_infos,
665 add_tang,
666 remove_clevis,
667 }) => {
668 let remove_passphrase_excuse;
669 let remove_tang_excuse;
670
671 if (key_descs.length + clevis_infos.length <= 1) {
672 remove_passphrase_excuse = _("This passphrase is the only way to unlock the pool and can not be removed.");
673 remove_tang_excuse = _("This keyserver is the only way to unlock the pool and can not be removed.");
674 }
675
676 return (
677 <>
678 <StorageDescription title={_("Passphrase")}>
679 <Flex>
680 { key_descs.length == 0
681 ? <FlexItem>
682 <StorageLink
683 onClick={add_passphrase}
684 >
685 {_("Add passphrase")}
686 </StorageLink>
687 </FlexItem>
688 : <>
689 <FlexItem>
690 <StorageLink
691 onClick={() => change_passphrase(key_descs[0])}
692 >
693 {_("Change")}
694 </StorageLink>
695 </FlexItem>
696 <FlexItem>
697 <StorageLink
698 onClick={() => remove_passphrase(key_descs[0])}
699 excuse={remove_passphrase_excuse}
700 >
701 {_("Remove")}
702 </StorageLink>
703 </FlexItem>
704 </>
705 }
706 </Flex>
707 </StorageDescription>
708 { (clevis_infos.length > 0 || !client.in_anaconda_mode()) &&
709 <StorageDescription title={_("Keyserver")}>
710 <Flex>
711 { clevis_infos.length == 0
712 ? <FlexItem>
713 <StorageLink
714 onClick={add_tang}
715 >
716 {_("Add keyserver")}
717 </StorageLink>
718 </FlexItem>
719 : (clevis_infos[0].pin == "tang"
720 ? <>
721 <FlexItem>
722 {clevis_infos[0].url}
723 </FlexItem>
724 <FlexItem>
725 <StorageLink
726 onClick={() => remove_clevis(clevis_infos[0])}
727 excuse={remove_tang_excuse}
728 >
729 {_("Remove")}
730 </StorageLink>
731 </FlexItem>
732 </>
733 : <FlexItem>
734 {cockpit.format(_("Clevis \"$0\""), clevis_infos[0].pin)}
735 </FlexItem>
736 )
737 }
738 </Flex>
739 </StorageDescription>}
740 </>
741 );
742 };
743
744 const StratisV2TokenTable = ({
745 pool,
746 tokens,
747 add_passphrase,
748 change_passphrase,
749 remove_passphrase,
750 add_tang,
751 remove_clevis,
752 }) => {
753 let remove_excuse;
754 if (tokens.length <= 1)
755 remove_excuse = _("Last token can not be removed");
756
757 const free_token_slots = (pool.FreeTokenSlots && pool.FreeTokenSlots[0])
758 ? pool.FreeTokenSlots[1]
759 : 15 - tokens.length;
760
761 let add_excuse;
762 if (free_token_slots <= 0)
763 add_excuse = _("No more space for passphrases or keyservers.");
764
765 function make_row(info) {
766 let test_location;
767 let description;
768 let actions;
769
770 if (info.keydesc) {
771 test_location = "passphrase";
772 description = _("Passphrase");
773 actions = [
774 {
775 title: _("Change"),
776 action: () => change_passphrase(info),
777 },
778 {
779 title: _("Remove"),
780 action: () => remove_passphrase(info),
781 excuse: remove_excuse,
782 danger: true,
783 },
784 ];
785 } else if (info.pin == "tang") {
786 test_location = info.url;
787 description = info.url;
788 actions = [
789 {
790 title: _("Remove"),
791 action: () => remove_clevis(info),
792 excuse: remove_excuse,
793 danger: true,
794 },
795 ];
796 } else {
797 test_location = info.pin;
798 description = cockpit.format(_("Clevis \"$0\""), info.pin);
799 actions = [
800 {
801 title: _("Remove"),
802 action: () => remove_clevis(info),
803 excuse: remove_excuse,
804 danger: true,
805 },
806 ];
807 }
808
809 return (
810 <Tr key={info.slot} data-test-row-location={test_location}>
811 <Td>{cockpit.format(_("Slot $0"), info.slot)}</Td>
812 <Td>{description}</Td>
813 <Td modifier="nowrap" className="pf-v6-c-table__action">
814 <Actions onlyMenu actions={actions} />
815 </Td>
816 </Tr>
817 );
818 }
819
820 const actions = [
821 {
822 title: _("Add passphrase"),
823 action: add_passphrase,
824 excuse: add_excuse
825 },
826 ];
827
828 if (!client.in_anaconda_mode())
829 actions.push({
830 title: _("Add keyserver"),
831 action: add_tang,
832 excuse: add_excuse
833 });
834
835 const v2_table = (
836 <Table variant="compact">
837 <Tbody>
838 { tokens.map(make_row) }
839 </Tbody>
840 </Table>
841 );
842
843 return [v2_table, actions];
844 };
845
846 const StratisEncryptionCard = ({ card, pool }) => {
847 const key_descs = get_key_descriptions(pool);
848 const clevis_infos = get_clevis_infos(pool);
849 const is_v1_pool = client.stratis_interface_revision < 8 || pool.MetadataVersion == 1;
850
851 const tokens = key_descs.concat(clevis_infos).sort((a, b) => a.slot - b.slot);
852
853 async function add_passphrase() {
854 const stored_keydescs = await get_stored_keydescs();
855
856 dialog_open({
857 Title: _("Add passphrase"),
858 Fields: [
859 PassInput("passphrase", _("Passphrase"),
860 { validate: val => !val.length && _("Passphrase cannot be empty") }),
861 PassInput("passphrase2", _("Confirm"),
862 { validate: (val, vals) => vals.passphrase.length && vals.passphrase != val && _("Passphrases do not match") }),
863 PoolPassphrase("pool_passphrase", pool, _("Adding a passphrase requires unlocking the pool."), stored_keydescs),
864 ],
865 Action: {
866 Title: _("Save"),
867 action: async vals => {
868 const key_desc = await get_new_keydesc(pool);
869 return await with_pool_passphrase(pool, vals.pool_passphrase,
870 () => with_stored_passphrase(client, key_desc, vals.passphrase,
871 () => bind_keyring(pool, key_desc).then(std_reply)));
872 }
873 }
874 });
875 }
876
877 async function change_passphrase(info) {
878 const stored_keydescs = await get_stored_keydescs();
879 const keydesc_set = stored_keydescs.includes(info.keydesc);
880
881 dialog_open({
882 Title: _("Change passphrase"),
883 Fields: [
884 PassInput("old_passphrase", _("Old passphrase"),
885 {
886 visible: vals => !keydesc_set,
887 validate: val => !val.length && _("Passphrase cannot be empty")
888 }),
889 PassInput("new_passphrase", _("New passphrase"),
890 { validate: val => !val.length && _("Passphrase cannot be empty") }),
891 PassInput("new_passphrase2", _("Confirm"),
892 { validate: (val, vals) => vals.new_passphrase.length && vals.new_passphrase != val && _("Passphrases do not match") })
893 ],
894 Action: {
895 Title: _("Save"),
896 action: async vals => {
897 const new_keydesc = await get_new_keydesc(pool);
898
899 function rebind() {
900 return with_stored_passphrase(client, new_keydesc, vals.new_passphrase,
901 () => rebind_keyring(pool, new_keydesc, info.slot).then(std_reply));
902 }
903
904 if (vals.old_passphrase) {
905 await with_stored_passphrase(client, info.keydesc, vals.old_passphrase, rebind);
906 } else {
907 await rebind();
908 }
909 }
910 }
911 });
912 }
913
914 function remove_passphrase(info) {
915 dialog_open({
916 Title: _("Remove passphrase?"),
917 Body: <div>
918 <p className="slot-warning">{ fmt_to_fragments(_("Passphrase removal may prevent unlocking $0."), <b>{pool.Name}</b>) }</p>
919 </div>,
920 Action: {
921 DangerButton: true,
922 Title: _("Remove"),
923 action: function (vals) {
924 return unbind_keyring(pool, info.slot).then(std_reply);
925 }
926 }
927 });
928 }
929
930 async function add_tang() {
931 const stored_keydescs = await get_stored_keydescs();
932
933 dialog_open({
934 Title: _("Add Tang keyserver"),
935 Fields: [
936 TextInput("tang_url", _("Keyserver address"),
937 {
938 validate: validate_url,
939 }),
940 PoolPassphrase("pool_passphrase", pool, _("Adding a keyserver requires unlocking the pool."), stored_keydescs),
941 ],
942 Action: {
943 Title: _("Save"),
944 action: function (vals, progress) {
945 return get_tang_adv(vals.tang_url)
946 .then(adv => {
947 function bind() {
948 return bind_clevis(pool, "tang", JSON.stringify({ url: vals.tang_url, adv }))
949 .then(std_reply);
950 }
951 confirm_tang_trust(vals.tang_url, adv,
952 () => with_pool_passphrase(pool, vals.pool_passphrase, bind));
953 });
954 }
955 }
956 });
957 }
958
959 function remove_clevis(info) {
960 dialog_open({
961 Title: info.pin == "tang" ? _("Remove Tang keyserver?") : _("Remove Clevis token?"),
962 Body: <div>
963 <p>{ fmt_to_fragments(_("Remove $0?"), <b>{info.pin == "tang" ? info.url : info.pin}</b>) }</p>
964 <p className="slot-warning">{ fmt_to_fragments(_("Removal may prevent unlocking $0."), <b>{pool.Name}</b>) }</p>
965 </div>,
966 Action: {
967 DangerButton: true,
968 Title: _("Remove"),
969 action: function (vals) {
970 return unbind_clevis(pool, info.slot).then(std_reply);
971 }
972 }
973 });
974 }
975
976 let v1_descriptions;
977 let v2_table;
978 let actions;
979
980 if (is_v1_pool) {
981 v1_descriptions = StratisV1TokenDescriptions({
982 key_descs,
983 add_passphrase,
984 change_passphrase,
985 remove_passphrase,
986 clevis_infos,
987 add_tang,
988 remove_clevis,
989 });
990 } else if (pool.MetadataVersion == 2) {
991 [v2_table, actions] = StratisV2TokenTable({
992 pool,
993 tokens,
994 add_passphrase,
995 change_passphrase,
996 remove_passphrase,
997 add_tang,
998 remove_clevis,
999 });
1000 }
1001
1002 return (
1003 <StorageCard card={card} actions={<Actions actions={actions} />}>
1004 <CardBody>
1005 <DescriptionList className="pf-m-horizontal-on-sm">
1006 <StorageDescription
1007 title={_("Metadata format")}
1008 value={is_v1_pool ? "V1" : "V" + pool.MetadataVersion}
1009 help={is_v1_pool && _("Pools with metadata format V1 are restricted to at most one passphrase and at most one keyserver.")}
1010 />
1011 {v1_descriptions}
1012 </DescriptionList>
1013 </CardBody>
1014 {v2_table}
1015 </StorageCard>
1016 );
1017 };
1018
1019 const StratisPoolCard = ({ card, pool, degraded_ops, can_grow, stats }) => {
1020 const use = pool.TotalPhysicalUsed[0] && [Number(pool.TotalPhysicalUsed[1]), Number(pool.TotalPhysicalSize)];
1021
1022 return (
1023 <StorageCard card={card}>
1024 <CardBody>
1025 <DescriptionList className="pf-m-horizontal-on-sm">
1026 <StorageDescription title={_("Name")}
1027 value={pool.Name}
1028 action={<StorageLink onClick={() => rename_pool(pool)}>
1029 {_("edit")}
1030 </StorageLink>} />
1031 <StorageDescription title={_("UUID")} value={pool.Uuid} />
1032 { use &&
1033 <StorageDescription title={_("Usage")}>
1034 <StorageUsageBar stats={use} critical={0.80} />
1035 </StorageDescription>
1036 }
1037 <StorageDescription title={_("Overprovisioning")}>
1038 <StorageOnOff state={pool.Overprovisioning}
1039 aria-label={_("Allow overprovisioning")}
1040 onChange={() => client.stratis_set_property(pool,
1041 "Overprovisioning",
1042 "b", !pool.Overprovisioning)}
1043 excuse={(pool.Overprovisioning && stats.fsys_total_size > stats.pool_total)
1044 ? _("Virtual filesystem sizes are larger than the pool. Overprovisioning can not be disabled.")
1045 : null} />
1046 </StorageDescription>
1047 { !pool.Overprovisioning &&
1048 <StorageDescription title={_("Allocated")}>
1049 <StorageUsageBar stats={[stats.fsys_total_size, stats.pool_total]} critical={2} />
1050 </StorageDescription>
1051 }
1052 </DescriptionList>
1053 </CardBody>
1054 <CardHeader><strong>{_("Block devices")}</strong></CardHeader>
1055 <PageTable
1056 emptyCaption={_("No block devices found")}
1057 aria-label={_("Stratis block devices")}
1058 crossrefs={get_crossrefs(pool)} />
1059 </StorageCard>
1060 );
1061 };