Debian Codegraph @

pkg/storaged/stratis/create-dialog.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 client from "../client.js";
8
9 import { dialog_open, TextInput, CheckBoxes, PassInput, SelectSpaces } from "../dialog.jsx";
10 import { decode_filename, get_available_spaces, prepare_available_spaces } from "../utils.js";
11
12 import { validate_pool_name, std_reply, get_unused_keydesc, with_stored_passphrase, confirm_tang_trust } from "./utils.jsx";
13 import { remember_passphrase } from "../anaconda.jsx";
14 import { validate_url, get_tang_adv } from "../crypto/tang.jsx";
15
16 const _ = cockpit.gettext;
17
18 function manager_create_pool(name, devs, key_desc, clevis_info) {
19 // "clevis_info" is either falsy or an array with two elements:
20 //
21 // [ pin, pin_config ]
22 //
23 // This array can be used directly with the (b(ss)) signature of
24 // the r6 CreatePool call, but needs to be taken apart for the
25 // a((bu)ss) signature of the r8 CreatePool call.
26
27 if (client.stratis_interface_revision < 8) {
28 let key_desc_arg = [false, ""];
29 if (key_desc)
30 key_desc_arg = [true, key_desc];
31 let clevis_info_arg = [false, ["", ""]];
32 if (clevis_info)
33 clevis_info_arg = [true, clevis_info];
34 return client.stratis_manager.CreatePool(name,
35 devs,
36 key_desc_arg,
37 clevis_info_arg);
38 } else {
39 const any_slot = [false, 0];
40 const key_descs_arg = [];
41 if (key_desc)
42 key_descs_arg.push([any_slot, key_desc]);
43 const clevis_infos_arg = [];
44 if (clevis_info)
45 clevis_infos_arg.push([any_slot, clevis_info[0], clevis_info[1]]);
46 return client.stratis_manager.CreatePool(name,
47 devs,
48 key_descs_arg,
49 clevis_infos_arg,
50 [false, 0], // journal_size
51 [false, ""], // tag_spec
52 [false, false]); // allocate_superblock
53 }
54 }
55
56 export function create_stratis_pool() {
57 function find_pool(name) {
58 for (const p in client.stratis_pools) {
59 if (client.stratis_pools[p].Name == name)
60 return client.stratis_pools[p];
61 }
62 return null;
63 }
64
65 let name;
66 for (let i = 0; i < 1000; i++) {
67 name = "pool" + i.toFixed();
68 if (!find_pool(name))
69 break;
70 }
71
72 dialog_open({
73 Title: _("Create Stratis pool"),
74 Fields: [
75 TextInput("name", _("Name"),
76 {
77 value: name,
78 validate: name => validate_pool_name(null, name)
79 }),
80 SelectSpaces("disks", _("Block devices"),
81 {
82 empty_warning: _("No block devices are available."),
83 validate: function (disks) {
84 if (disks.length === 0)
85 return _("At least one block device is needed.");
86 },
87 spaces: get_available_spaces()
88 }),
89 CheckBoxes("encrypt_pass", _("Options"),
90 {
91 fields: [
92 {
93 tag: "on",
94 title: _("Encrypt data with a passphrase"),
95 }
96 ],
97 nested_fields: [
98 PassInput("passphrase", _("Passphrase"),
99 {
100 validate: function (phrase) {
101 if (phrase === "")
102 return _("Passphrase cannot be empty");
103 },
104 visible: vals => vals.encrypt_pass.on,
105 new_password: true
106 }),
107 PassInput("passphrase2", _("Confirm"),
108 {
109 validate: function (phrase2, vals) {
110 if (phrase2 != vals.passphrase)
111 return _("Passphrases do not match");
112 },
113 visible: vals => vals.encrypt_pass.on,
114 new_password: true
115 })
116 ]
117 }),
118 CheckBoxes("encrypt_tang", "",
119 {
120 visible: () => !client.in_anaconda_mode(),
121 fields: [
122 { tag: "on", title: _("Encrypt data with a Tang keyserver") }
123 ],
124 nested_fields: [
125 TextInput("tang_url", _("Keyserver address"),
126 {
127 validate: validate_url,
128 visible: vals => vals.encrypt_tang && vals.encrypt_tang.on
129 }),
130 ]
131 }),
132 CheckBoxes("overprov", "",
133 {
134 value: { on: true },
135 fields: [
136 {
137 tag: "on",
138 title: _("Overprovisioning"),
139 }
140 ]
141 })
142 ],
143 Action: {
144 Title: _("Create"),
145 action: function (vals) {
146 return prepare_available_spaces(client, vals.disks).then(function (paths) {
147 const devs = paths.map(p => decode_filename(client.blocks[p].PreferredDevice));
148
149 function create(key_desc, adv) {
150 let clevis_info = null;
151 if (adv)
152 clevis_info = ["tang", JSON.stringify({ url: vals.tang_url, adv })];
153 return manager_create_pool(vals.name, devs, key_desc, clevis_info)
154 .then(std_reply)
155 .then(result => {
156 if (key_desc && vals.passphrase) {
157 for (const p of paths) {
158 const block = client.blocks[p];
159 if (block)
160 remember_passphrase(block, vals.passphrase);
161 }
162 }
163 if (vals.overprov && !vals.overprov.on && result[0]) {
164 const path = result[1][0];
165 return client.wait_for(() => client.stratis_pools[path])
166 .then(pool => {
167 return client.stratis_set_property(pool,
168 "Overprovisioning",
169 "b", false);
170 });
171 }
172 });
173 }
174
175 function create2(adv) {
176 if (vals.encrypt_pass.on) {
177 return get_unused_keydesc(client, vals.name)
178 .then(keydesc => with_stored_passphrase(client, keydesc, vals.passphrase,
179 () => create(keydesc, adv)));
180 } else {
181 return create(false, adv);
182 }
183 }
184
185 if (vals.encrypt_tang && vals.encrypt_tang.on) {
186 return get_tang_adv(vals.tang_url)
187 .then(adv => confirm_tang_trust(vals.tang_url, adv, () => create2(adv)));
188 } else {
189 return create2(false);
190 }
191 });
192 }
193 }
194 });
195 }