-
Notifications
You must be signed in to change notification settings - Fork 12
/
DiskEncryption.jsx
169 lines (149 loc) · 5.49 KB
/
DiskEncryption.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
/*
* Copyright (C) 2023 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with This program; If not, see <http://www.gnu.org/licenses/>.
*/
import cockpit from "cockpit";
import React, { useContext, useEffect, useMemo, useState } from "react";
import {
Checkbox,
EmptyState,
EmptyStateFooter,
EmptyStateHeader,
EmptyStateIcon,
Form,
Spinner,
Text,
TextContent,
TextVariants,
useWizardFooter,
} from "@patternfly/react-core";
import { applyStorage } from "../../apis/storage_partitioning.js";
import { AnacondaWizardFooter } from "../AnacondaWizardFooter.jsx";
import { RuntimeContext, StorageContext } from "../Common.jsx";
import { PasswordFormFields, ruleLength } from "../Password.jsx";
import "./DiskEncryption.scss";
const _ = cockpit.gettext;
const ruleAscii = {
check: (policy, password) => password.length > 0 && /^[\x20-\x7F]*$/.test(password),
id: "ascii",
isError: false,
text: () => _("The passphrase you have provided contains non-ASCII characters. You may not be able to switch between keyboard layouts when typing it."),
};
const CheckDisksSpinner = (
<EmptyState id="installation-destination-next-spinner">
<EmptyStateHeader titleText={<>{_("Checking storage configuration")}</>} icon={<EmptyStateIcon icon={Spinner} />} headingLevel="h4" />
<EmptyStateFooter>
<TextContent>
<Text component={TextVariants.p}>
{_("This may take a moment")}
</Text>
</TextContent>
</EmptyStateFooter>
</EmptyState>
);
const DiskEncryption = ({
idPrefix,
isInProgress,
setIsFormValid,
setStepNotification,
}) => {
const { partitioning } = useContext(StorageContext);
const request = partitioning?.requests?.[0];
const [confirmPassword, setConfirmPassword] = useState(request?.passphrase || "");
const [isEncrypted, setIsEncrypted] = useState(request?.encrypted);
const [password, setPassword] = useState(request?.passphrase || "");
const luksPolicy = useContext(RuntimeContext).passwordPolicies.luks;
// Display custom footer
const getFooter = useMemo(
() => <CustomFooter encrypt={isEncrypted} encryptPassword={password} setStepNotification={setStepNotification} />,
[isEncrypted, password, setStepNotification]
);
useWizardFooter(getFooter);
const encryptedDevicesCheckbox = content => (
<Checkbox
id={idPrefix + "-encrypt-devices"}
label={_("Encrypt my data")}
isChecked={isEncrypted}
onChange={(_event, isEncrypted) => setIsEncrypted(isEncrypted)}
body={content}
/>
);
const passphraseForm = (
<PasswordFormFields
idPrefix={idPrefix}
policy={luksPolicy}
password={password}
setPassword={setPassword}
passwordLabel={_("Passphrase")}
confirmPassword={confirmPassword}
setConfirmPassword={setConfirmPassword}
confirmPasswordLabel={_("Confirm passphrase")}
rules={[ruleLength, ruleAscii]}
setIsValid={setIsFormValid}
/>
);
useEffect(() => {
setIsFormValid(!isEncrypted);
}, [setIsFormValid, isEncrypted]);
if (isInProgress) {
return CheckDisksSpinner;
}
return (
<>
<TextContent>
<Text component={TextVariants.p}>
{_("Encryption helps secure your data, to prevent others from accessing it.")}
</Text>
<Text component={TextVariants.p}>
{_("Only new partitions will be encrypted. Existing partitions will remain untouched.")}
</Text>
</TextContent>
<Form>
{encryptedDevicesCheckbox(isEncrypted ? passphraseForm : null)}
</Form>
</>
);
};
const CustomFooter = ({ encrypt, encryptPassword, setStepNotification }) => {
const step = usePage({}).id;
const onNext = ({ goToNextStep, setIsFormDisabled }) => {
return applyStorage({
encrypt,
encryptPassword,
onFail: ex => {
setIsFormDisabled(false);
setStepNotification({ step, ...ex });
},
onSuccess: () => {
goToNextStep();
// Reset the state after the onNext call. Otherwise,
// React will try to render the current step again.
setIsFormDisabled(false);
setStepNotification();
},
});
};
return <AnacondaWizardFooter onNext={onNext} />;
};
export const usePage = () => {
const { storageScenarioId } = useContext(StorageContext);
return ({
component: DiskEncryption,
id: "disk-encryption",
isHidden: ["mount-point-mapping", "use-configured-storage"].includes(storageScenarioId),
label: _("Disk encryption"),
title: _("Encrypt the selected devices?")
});
};