-
Notifications
You must be signed in to change notification settings - Fork 845
Expand file tree
/
Copy pathSaveNewPolicyModal.tsx
More file actions
359 lines (335 loc) · 11.3 KB
/
SaveNewPolicyModal.tsx
File metadata and controls
359 lines (335 loc) · 11.3 KB
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
import React, { useState, useContext, useEffect, useCallback } from "react";
import { size } from "lodash";
import classNames from "classnames";
import CUSTOM_TARGET_OPTIONS from "pages/policies/helpers";
import { AppContext } from "context/app";
import { PolicyContext } from "context/policy";
import { IPlatformSelector } from "hooks/usePlatformSelector";
import { ILabelSummary } from "interfaces/label";
import { IPolicyFormData } from "interfaces/policy";
import { CommaSeparatedPlatformString } from "interfaces/platform";
import useDeepEffect from "hooks/useDeepEffect";
import InputField from "components/forms/fields/InputField";
import Checkbox from "components/forms/fields/Checkbox";
import TooltipWrapper from "components/TooltipWrapper";
import Button from "components/buttons/Button";
import Modal from "components/Modal";
import TargetLabelSelector from "components/TargetLabelSelector";
import Icon from "components/Icon";
export interface ISaveNewPolicyModalProps {
baseClass: string;
queryValue: string;
onCreatePolicy: (formData: IPolicyFormData) => void;
setIsSaveNewPolicyModalOpen: (isOpen: boolean) => void;
backendValidators: { [key: string]: string };
platformSelector: IPlatformSelector;
isUpdatingPolicy: boolean;
aiFeaturesDisabled?: boolean;
isFetchingAutofillDescription: boolean;
isFetchingAutofillResolution: boolean;
onClickAutofillDescription: () => Promise<void>;
onClickAutofillResolution: () => Promise<void>;
labels: ILabelSummary[];
}
const validatePolicyName = (name: string) => {
const errors: { [key: string]: string } = {};
if (!name) {
errors.name = "Policy name must be present";
}
const valid = !size(errors);
return { valid, errors };
};
const SaveNewPolicyModal = ({
baseClass,
queryValue,
onCreatePolicy,
setIsSaveNewPolicyModalOpen,
backendValidators,
platformSelector,
isUpdatingPolicy,
aiFeaturesDisabled,
isFetchingAutofillDescription,
isFetchingAutofillResolution,
onClickAutofillDescription,
onClickAutofillResolution,
labels,
}: ISaveNewPolicyModalProps): JSX.Element => {
const { isPremiumTier } = useContext(AppContext);
const {
lastEditedQueryName,
lastEditedQueryDescription,
lastEditedQueryResolution,
lastEditedQueryCritical,
setLastEditedQueryName,
setLastEditedQueryPlatform,
// TODO: Keep last edited query platform from resetting when cancelling out of modal and clicking save again
setLastEditedQueryDescription,
setLastEditedQueryResolution,
setLastEditedQueryCritical,
} = useContext(PolicyContext);
const [errors, setErrors] = useState<{ [key: string]: string }>(
backendValidators
);
const [selectedTargetType, setSelectedTargetType] = useState("All hosts");
const [selectedCustomTarget, setSelectedCustomTarget] = useState(
"labelsIncludeAny"
);
const [selectedLabels, setSelectedLabels] = useState({});
const onSelectLabel = ({
name: labelName,
value,
}: {
name: string;
value: boolean;
}) => {
setSelectedLabels({
...selectedLabels,
[labelName]: value,
});
};
const disableForm =
isFetchingAutofillDescription || isFetchingAutofillResolution;
const disableSave =
!platformSelector.isAnyPlatformSelected ||
disableForm ||
(selectedTargetType === "Custom" &&
!Object.entries(selectedLabels).some(([, value]) => {
return value;
}));
useDeepEffect(() => {
if (lastEditedQueryName) {
setErrors({});
}
}, [lastEditedQueryName]);
useEffect(() => {
setErrors(backendValidators);
}, [backendValidators]);
const handleSavePolicy = (evt: React.MouseEvent<HTMLFormElement>) => {
evt.preventDefault();
const newPlatformString = platformSelector
.getSelectedPlatforms()
.join(",") as CommaSeparatedPlatformString;
setLastEditedQueryPlatform(newPlatformString);
const { valid: validName, errors: newErrors } = validatePolicyName(
lastEditedQueryName
);
setErrors({
...errors,
...newErrors,
});
if (!disableSave && validName) {
onCreatePolicy({
description: lastEditedQueryDescription,
name: lastEditedQueryName,
query: queryValue,
resolution: lastEditedQueryResolution,
platform: newPlatformString,
critical: lastEditedQueryCritical,
labels_include_any:
selectedTargetType === "Custom" &&
selectedCustomTarget === "labelsIncludeAny"
? Object.entries(selectedLabels)
.filter(([, selected]) => selected)
.map(([labelName]) => labelName)
: [],
labels_exclude_any:
selectedTargetType === "Custom" &&
selectedCustomTarget === "labelsExcludeAny"
? Object.entries(selectedLabels)
.filter(([, selected]) => selected)
.map(([labelName]) => labelName)
: [],
});
}
};
const renderAutofillButton = useCallback(
(labelName: "Description" | "Resolution") => {
const isFetchingButton =
(labelName === "Description" && isFetchingAutofillDescription) ||
(labelName === "Resolution" && isFetchingAutofillResolution);
return (
<TooltipWrapper
tipContent={
aiFeaturesDisabled ? (
"AI features are disabled in organization settings"
) : (
<>
Policy queries (SQL) will be sent to a <br />
large language model (LLM). Fleet <br />
doesn't use this data to train models.
</>
)
}
position="top"
disableTooltip={disableForm}
underline={false}
>
<div className="autofill-tooltip-wrapper">
<Button
variant="inverse"
disabled={aiFeaturesDisabled || disableForm}
onClick={
labelName === "Description"
? onClickAutofillDescription
: onClickAutofillResolution
}
size="small"
>
{isFetchingButton ? (
"Thinking..."
) : (
<>
<Icon name="sparkles" /> Autofill
</>
)}
</Button>
</div>
</TooltipWrapper>
);
},
[isFetchingAutofillDescription, isFetchingAutofillResolution, disableForm]
);
const renderAutofillLabel = useCallback(
(labelName: "Description" | "Resolution") => {
const labelClassName = classNames(`${baseClass}__autofill-label`, {
[`${baseClass}__label--${labelName}`]: !!labelName,
});
return (
<div className={labelClassName}>
{labelName}
{renderAutofillButton(labelName)}
</div>
);
},
[renderAutofillButton]
);
return (
<Modal
title="Save policy"
onExit={() => setIsSaveNewPolicyModalOpen(false)}
>
<>
<form
onSubmit={handleSavePolicy}
className={`${baseClass}__save-modal-form`}
autoComplete="off"
>
<InputField
name="name"
onChange={(value: string) => setLastEditedQueryName(value)}
value={lastEditedQueryName}
error={errors.name}
inputClassName={`${baseClass}__policy-save-modal-name`}
label="Name"
autofocus
ignore1password
disabled={disableForm}
/>
<InputField
name="description"
onChange={(value: string) => setLastEditedQueryDescription(value)}
value={lastEditedQueryDescription}
inputClassName={`${baseClass}__policy-save-modal-description`}
label={renderAutofillLabel("Description")}
helpText="How does this policy's failure put the organization at risk?"
type="textarea"
disabled={disableForm}
/>
<InputField
name="resolution"
onChange={(value: string) => setLastEditedQueryResolution(value)}
value={lastEditedQueryResolution}
inputClassName={`${baseClass}__policy-save-modal-resolution`}
label={renderAutofillLabel("Resolution")}
type="textarea"
helpText="If this policy fails, what should the end user expect?"
disabled={disableForm}
/>
{platformSelector.render()}
{isPremiumTier && (
<TargetLabelSelector
selectedTargetType={selectedTargetType}
selectedCustomTarget={selectedCustomTarget}
customTargetOptions={CUSTOM_TARGET_OPTIONS}
onSelectCustomTarget={setSelectedCustomTarget}
selectedLabels={selectedLabels}
className={`${baseClass}__target`}
onSelectTargetType={setSelectedTargetType}
onSelectLabel={onSelectLabel}
labels={labels || []}
customHelpText={
<span className="form-field__help-text">
Policy will target hosts on selected platforms that{" "}
<b>have any</b> of these labels:
</span>
}
suppressTitle
disableOptions={disableForm}
/>
)}
{isPremiumTier && (
<div className="critical-checkbox-wrapper">
<Checkbox
name="critical-policy"
onChange={(value: boolean) => setLastEditedQueryCritical(value)}
value={lastEditedQueryCritical}
disabled={disableForm}
>
<TooltipWrapper
tipContent={
<p>
If automations are turned on, this information is
included. If Okta conditional access is configured, end
users can never bypass critical policies.
</p>
}
>
Critical
</TooltipWrapper>
</Checkbox>
</div>
)}
<div className="modal-cta-wrap">
<TooltipWrapper
tipContent={
<>
Select the platforms this
<br />
policy will be checked on
<br />
to save the policy.
</>
}
tooltipClass={`${baseClass}__button--modal-save-tooltip`}
position="top"
disableTooltip={!disableSave}
underline={false}
showArrow
tipOffset={8}
>
<span className={`${baseClass}__button-wrap--modal-save`}>
<Button
type="submit"
onClick={handleSavePolicy}
disabled={disableSave}
className="save-policy-loading"
isLoading={isUpdatingPolicy}
>
Save
</Button>
</span>
</TooltipWrapper>
<Button
className={`${baseClass}__button--modal-cancel`}
onClick={() => setIsSaveNewPolicyModalOpen(false)}
variant="inverse"
>
Cancel
</Button>
</div>
</form>
</>
</Modal>
);
};
export default SaveNewPolicyModal;