-
Notifications
You must be signed in to change notification settings - Fork 44
/
ProposalSettingsSection.jsx
360 lines (321 loc) · 10.5 KB
/
ProposalSettingsSection.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright (c) [2022-2024] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact SUSE LLC.
*
* To contact SUSE LLC about this file by physical or electronic mail, you may
* find current contact information at www.suse.com.
*/
import React, { useEffect, useState } from "react";
import { Checkbox, Form, Skeleton, Switch, Tooltip } from "@patternfly/react-core";
import { _ } from "~/i18n";
import { If, PasswordAndConfirmationInput, Section, Popup } from "~/components/core";
import { ProposalVolumes, ProposalSpacePolicyField } from "~/components/storage";
import { Icon } from "~/components/layout";
import { noop } from "~/utils";
import { hasFS } from "~/components/storage/utils";
/**
* @typedef {import ("~/client/storage").ProposalManager.ProposalSettings} ProposalSettings
* @typedef {import ("~/client/storage").DevicesManager.StorageDevice} StorageDevice
* @typedef {import ("~/client/storage").ProposalManager.Volume} Volume
*/
/**
* Form for configuring the encryption password.
* @component
*
* @param {object} props
* @param {string} props.id - Form ID.
* @param {string} props.password - Password for encryption.
* @param {onSubmitFn} [props.onSubmit=noop] - On submit callback.
* @param {onValidateFn} [props.onValidate=noop] - On validate callback.
*
* @callback onSubmitFn
* @param {string} password
*
* @callback onValidateFn
* @param {boolean} valid
*/
const EncryptionSettingsForm = ({
id,
password: passwordProp,
method: methodProp,
methods,
onSubmit = noop,
onValidate = noop
}) => {
const [password, setPassword] = useState(passwordProp || "");
const [method, setMethod] = useState(methodProp);
const tpmId = "tpm_fde";
const luks2Id = "luks2";
useEffect(() => {
if (password.length === 0) onValidate(false);
}, [password, onValidate]);
const changePassword = (_, v) => setPassword(v);
const changeMethod = (_, value) => {
value ? setMethod(tpmId) : setMethod(luks2Id);
};
const submitForm = (e) => {
e.preventDefault();
onSubmit(password, method);
};
const Description = () => (
<span
dangerouslySetInnerHTML={{
// TRANSLATORS: The word 'directly' is key here. For example, booting to the installer media and then choosing
// 'Boot from Hard Disk' from there will not work. Keep it sort (this is a hint in a form) but keep it clear.
// Do not translate 'abbr' and 'title', they are part of the HTML markup.
__html: _("The password will not be needed to boot and access the data if the <abbr title='Trusted Platform Module'>TPM</abbr> can verify the integrity of the system. TPM sealing requires the new system to be booted directly on its first run.")
}}
/>
);
return (
<Form id={id} onSubmit={submitForm}>
<PasswordAndConfirmationInput
id="encryptionPasswordInput"
value={password}
onChange={changePassword}
onValidation={onValidate}
/>
<If
condition={methods.includes(tpmId)}
then={
<Checkbox
id="encryption_method"
label={_("Use the TPM to decrypt automatically on each boot")}
description={<Description />}
isChecked={method === tpmId}
onChange={changeMethod}
/>
}
/>
</Form>
);
};
/**
* Allows to define snapshots enablement
* @component
*
* @param {object} props
* @param {ProposalSettings} props.settings - Settings used for calculating a proposal.
* @param {onChangeFn} [props.onChange=noop] - On change callback
*
* @callback onChangeFn
* @param {object} settings
*/
const SnapshotsField = ({
settings,
onChange = noop
}) => {
const rootVolume = (settings.volumes || []).find((i) => i.mountPath === "/");
const initialChecked = rootVolume !== undefined && hasFS(rootVolume, "Btrfs") && rootVolume.snapshots;
const [isChecked, setIsChecked] = useState(initialChecked);
// no root volume is probably some error or still loading
if (rootVolume === undefined) {
return <Skeleton width="25%" />;
}
const switchState = (_, checked) => {
setIsChecked(checked);
onChange({ active: checked, settings });
};
if (!rootVolume.outline.snapshotsConfigurable) return;
const explanation = _("Uses Btrfs for the root file system allowing to boot to a previous \
version of the system after configuration changes or software upgrades.");
return (
<div>
<Switch
id="snapshots"
label={_("Use Btrfs Snapshots")}
isReversed
isChecked={isChecked}
onChange={switchState}
/>
<div>
{explanation}
</div>
</div>
);
};
/**
* Allows to define encryption
* @component
*
* @param {object} props
* @param {string} [props.password=""] - Password for encryption
* @param {string} [props.method=""] - Encryption method
* @param {boolean} [props.isChecked=false] - Whether encryption is selected
* @param {boolean} [props.isLoading=false] - Whether to show the selector as loading
* @param {onChangeFn} [props.onChange=noop] - On change callback
*
* @callback onChangeFn
* @param {object} settings
*/
const EncryptionField = ({
password: passwordProp = "",
method: methodProp = "",
methods,
isChecked: isCheckedProp = false,
isLoading = false,
onChange = noop
}) => {
const [isChecked, setIsChecked] = useState(isCheckedProp);
const [password, setPassword] = useState(passwordProp);
const [method, setMethod] = useState(methodProp);
const [isFormOpen, setIsFormOpen] = useState(false);
const [isFormValid, setIsFormValid] = useState(true);
const openForm = () => setIsFormOpen(true);
const closeForm = () => setIsFormOpen(false);
const acceptForm = (newPassword, newMethod) => {
closeForm();
setPassword(newPassword);
setMethod(newMethod);
onChange({ isChecked, password: newPassword, method: newMethod });
};
const cancelForm = () => {
closeForm();
if (password.length === 0) setIsChecked(false);
};
const validateForm = (valid) => setIsFormValid(valid);
const changeSelected = (_, value) => {
setIsChecked(value);
if (value && password.length === 0) openForm();
if (!value) {
setPassword("");
onChange({ isChecked: false, password: "" });
}
};
const ChangeSettingsButton = () => {
return (
<Tooltip
content={_("Change encryption settings")}
entryDelay={400}
exitDelay={50}
position="right"
>
<button aria-label={_("Encryption settings")} className="plain-control" onClick={openForm}>
<Icon name="tune" size="s" />
</button>
</Tooltip>
);
};
if (isLoading) return <Skeleton width="25%" />;
return (
<>
<div className="split">
<Switch
id="encryption"
label={_("Use encryption")}
isReversed
isChecked={isChecked}
onChange={changeSelected}
/>
{ isChecked && <ChangeSettingsButton /> }
</div>
<Popup aria-label={_("Encryption settings")} title={_("Encryption settings")} isOpen={isFormOpen}>
<EncryptionSettingsForm
id="encryptionSettingsForm"
password={password}
method={method}
methods={methods}
onSubmit={acceptForm}
onValidate={validateForm}
/>
<Popup.Actions>
<Popup.Confirm form="encryptionSettingsForm" type="submit" isDisabled={!isFormValid}>{_("Accept")}</Popup.Confirm>
<Popup.Cancel onClick={cancelForm} />
</Popup.Actions>
</Popup>
</>
);
};
/**
* Section for editing the proposal settings
* @component
*
* @param {object} props
* @param {ProposalSettings} props.settings
* @param {String[]} [props.encryptionMethods=[]]
* @param {onChangeFn} [props.onChange=noop]
*
* @callback onChangeFn
* @param {object} settings
*/
export default function ProposalSettingsSection({
settings,
encryptionMethods = [],
volumeTemplates = [],
isLoading = false,
onChange = noop
}) {
const changeEncryption = ({ password, method }) => {
onChange({ encryptionPassword: password, encryptionMethod: method });
};
const changeBtrfsSnapshots = ({ active, settings }) => {
const rootVolume = settings.volumes.find((i) => i.mountPath === "/");
if (active) {
rootVolume.fsType = "Btrfs";
rootVolume.snapshots = true;
} else {
rootVolume.snapshots = false;
}
onChange({ volumes: settings.volumes });
};
const changeVolumes = (volumes) => {
onChange({ volumes });
};
const changeSpacePolicy = (policy, actions) => {
onChange({ spacePolicy: policy, spaceActions: actions });
};
const encryption = settings.encryptionPassword !== undefined && settings.encryptionPassword.length > 0;
const { volumes = [] } = settings;
// Templates for already existing mount points are filtered out
const usefulTemplates = () => {
const mountPaths = volumes.map(v => v.mountPath);
return volumeTemplates.filter(t => (
t.mountPath.length > 0 && !mountPaths.includes(t.mountPath)
));
};
return (
<>
<Section title={_("Settings")}>
<SnapshotsField
settings={settings}
onChange={changeBtrfsSnapshots}
/>
<EncryptionField
password={settings.encryptionPassword || ""}
method={settings.encryptionMethod}
methods={encryptionMethods}
isChecked={encryption}
isLoading={settings.encryptionPassword === undefined}
onChange={changeEncryption}
/>
<ProposalVolumes
volumes={volumes}
templates={usefulTemplates()}
options={{ lvm: settings.lvm, encryption }}
isLoading={isLoading && settings.volumes === undefined}
onChange={changeVolumes}
/>
<ProposalSpacePolicyField
policy={settings.spacePolicy}
actions={settings.spaceActions}
devices={settings.installationDevices}
isLoading={isLoading}
onChange={changeSpacePolicy}
/>
</Section>
</>
);
}