2024-11-13 14:32:59 +00:00
|
|
|
import React, { useState, useEffect, useCallback, useContext } from "react";
|
2023-07-17 21:09:59 +00:00
|
|
|
import { pull, size } from "lodash";
|
|
|
|
|
|
2024-11-13 14:32:59 +00:00
|
|
|
import { AppContext } from "context/app";
|
|
|
|
|
|
2023-07-17 21:09:59 +00:00
|
|
|
import useDeepEffect from "hooks/useDeepEffect";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
FREQUENCY_DROPDOWN_OPTIONS,
|
|
|
|
|
LOGGING_TYPE_OPTIONS,
|
|
|
|
|
MIN_OSQUERY_VERSION_OPTIONS,
|
|
|
|
|
SCHEDULE_PLATFORM_DROPDOWN_OPTIONS,
|
|
|
|
|
} from "utilities/constants";
|
2024-12-05 23:19:56 +00:00
|
|
|
|
2023-07-17 21:19:35 +00:00
|
|
|
import { SelectedPlatformString } from "interfaces/platform";
|
2023-07-17 21:09:59 +00:00
|
|
|
import {
|
|
|
|
|
ICreateQueryRequestBody,
|
|
|
|
|
ISchedulableQuery,
|
|
|
|
|
QueryLoggingOption,
|
|
|
|
|
} from "interfaces/schedulable_query";
|
2024-12-05 23:19:56 +00:00
|
|
|
|
|
|
|
|
import Checkbox from "components/forms/fields/Checkbox";
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
import InputField from "components/forms/fields/InputField";
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
import Dropdown from "components/forms/fields/Dropdown";
|
|
|
|
|
import Slider from "components/forms/fields/Slider";
|
|
|
|
|
import TooltipWrapper from "components/TooltipWrapper";
|
|
|
|
|
import Icon from "components/Icon";
|
|
|
|
|
import Button from "components/buttons/Button";
|
|
|
|
|
import Modal from "components/Modal";
|
|
|
|
|
import RevealButton from "components/buttons/RevealButton";
|
|
|
|
|
import LogDestinationIndicator from "components/LogDestinationIndicator";
|
|
|
|
|
|
2023-10-09 18:31:31 +00:00
|
|
|
import DiscardDataOption from "../DiscardDataOption";
|
2023-07-17 21:09:59 +00:00
|
|
|
|
|
|
|
|
const baseClass = "save-query-modal";
|
|
|
|
|
export interface ISaveQueryModalProps {
|
|
|
|
|
queryValue: string;
|
2023-07-20 00:50:27 +00:00
|
|
|
apiTeamIdForQuery?: number; // query will be global if omitted
|
2023-07-17 21:09:59 +00:00
|
|
|
isLoading: boolean;
|
|
|
|
|
saveQuery: (formData: ICreateQueryRequestBody) => void;
|
|
|
|
|
toggleSaveQueryModal: () => void;
|
|
|
|
|
backendValidators: { [key: string]: string };
|
|
|
|
|
existingQuery?: ISchedulableQuery;
|
2023-10-09 18:31:31 +00:00
|
|
|
queryReportsDisabled?: boolean;
|
2023-07-17 21:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validateQueryName = (name: string) => {
|
|
|
|
|
const errors: { [key: string]: string } = {};
|
|
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
|
errors.name = "Query name must be present";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const valid = !size(errors);
|
|
|
|
|
return { valid, errors };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SaveQueryModal = ({
|
|
|
|
|
queryValue,
|
2023-07-20 00:50:27 +00:00
|
|
|
apiTeamIdForQuery,
|
2023-07-17 21:09:59 +00:00
|
|
|
isLoading,
|
|
|
|
|
saveQuery,
|
|
|
|
|
toggleSaveQueryModal,
|
|
|
|
|
backendValidators,
|
|
|
|
|
existingQuery,
|
2023-10-09 18:31:31 +00:00
|
|
|
queryReportsDisabled,
|
2023-07-17 21:09:59 +00:00
|
|
|
}: ISaveQueryModalProps): JSX.Element => {
|
2024-11-13 14:32:59 +00:00
|
|
|
const { config } = useContext(AppContext);
|
|
|
|
|
|
2023-07-17 21:09:59 +00:00
|
|
|
const [name, setName] = useState("");
|
|
|
|
|
const [description, setDescription] = useState("");
|
|
|
|
|
const [selectedFrequency, setSelectedFrequency] = useState(
|
|
|
|
|
existingQuery?.interval ?? 3600
|
|
|
|
|
);
|
|
|
|
|
const [
|
|
|
|
|
selectedPlatformOptions,
|
|
|
|
|
setSelectedPlatformOptions,
|
2023-07-17 21:19:35 +00:00
|
|
|
] = useState<SelectedPlatformString>(existingQuery?.platform ?? "");
|
2023-07-17 21:09:59 +00:00
|
|
|
const [
|
|
|
|
|
selectedMinOsqueryVersionOptions,
|
|
|
|
|
setSelectedMinOsqueryVersionOptions,
|
|
|
|
|
] = useState(existingQuery?.min_osquery_version ?? "");
|
|
|
|
|
const [
|
|
|
|
|
selectedLoggingType,
|
|
|
|
|
setSelectedLoggingType,
|
|
|
|
|
] = useState<QueryLoggingOption>(existingQuery?.logging ?? "snapshot");
|
|
|
|
|
const [observerCanRun, setObserverCanRun] = useState(false);
|
2024-11-13 14:32:59 +00:00
|
|
|
const [automationsEnabled, setAutomationsEnabled] = useState(false);
|
2023-10-04 22:19:26 +00:00
|
|
|
const [discardData, setDiscardData] = useState(false);
|
2023-07-17 21:09:59 +00:00
|
|
|
const [errors, setErrors] = useState<{ [key: string]: string }>(
|
|
|
|
|
backendValidators
|
|
|
|
|
);
|
|
|
|
|
const [showAdvancedOptions, setShowAdvancedOptions] = useState(false);
|
|
|
|
|
|
|
|
|
|
const toggleAdvancedOptions = () => {
|
|
|
|
|
setShowAdvancedOptions(!showAdvancedOptions);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useDeepEffect(() => {
|
|
|
|
|
if (name) {
|
|
|
|
|
setErrors({});
|
|
|
|
|
}
|
|
|
|
|
}, [name]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setErrors(backendValidators);
|
|
|
|
|
}, [backendValidators]);
|
|
|
|
|
|
|
|
|
|
const onClickSaveQuery = (evt: React.MouseEvent<HTMLFormElement>) => {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
2024-10-03 22:59:10 +00:00
|
|
|
const trimmedName = name.trim();
|
|
|
|
|
|
|
|
|
|
const { valid, errors: newErrors } = validateQueryName(trimmedName);
|
2023-07-17 21:09:59 +00:00
|
|
|
setErrors({
|
|
|
|
|
...errors,
|
|
|
|
|
...newErrors,
|
|
|
|
|
});
|
2024-10-03 22:59:10 +00:00
|
|
|
setName(trimmedName);
|
2023-07-17 21:09:59 +00:00
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
|
saveQuery({
|
|
|
|
|
// from modal fields
|
2024-10-03 22:59:10 +00:00
|
|
|
name: trimmedName,
|
2023-07-17 21:09:59 +00:00
|
|
|
description,
|
|
|
|
|
interval: selectedFrequency,
|
|
|
|
|
observer_can_run: observerCanRun,
|
2024-11-13 14:32:59 +00:00
|
|
|
automations_enabled: automationsEnabled,
|
2023-10-04 22:19:26 +00:00
|
|
|
discard_data: discardData,
|
2023-07-17 21:09:59 +00:00
|
|
|
platform: selectedPlatformOptions,
|
|
|
|
|
min_osquery_version: selectedMinOsqueryVersionOptions,
|
|
|
|
|
logging: selectedLoggingType,
|
|
|
|
|
// from previous New query page
|
|
|
|
|
query: queryValue,
|
|
|
|
|
// from doubly previous ManageQueriesPage
|
2023-07-20 00:50:27 +00:00
|
|
|
team_id: apiTeamIdForQuery,
|
2023-07-17 21:09:59 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onChangeSelectPlatformOptions = useCallback(
|
|
|
|
|
(values: string) => {
|
|
|
|
|
const valArray = values.split(",");
|
|
|
|
|
|
|
|
|
|
// Remove All if another OS is chosen
|
|
|
|
|
// else if Remove OS if All is chosen
|
|
|
|
|
if (valArray.indexOf("") === 0 && valArray.length > 1) {
|
|
|
|
|
// TODO - inmprove type safety of all 3 options
|
|
|
|
|
setSelectedPlatformOptions(
|
2023-07-17 21:19:35 +00:00
|
|
|
pull(valArray, "").join(",") as SelectedPlatformString
|
2023-07-17 21:09:59 +00:00
|
|
|
);
|
|
|
|
|
} else if (valArray.length > 1 && valArray.indexOf("") > -1) {
|
|
|
|
|
setSelectedPlatformOptions("");
|
|
|
|
|
} else {
|
2023-07-17 21:19:35 +00:00
|
|
|
setSelectedPlatformOptions(values as SelectedPlatformString);
|
2023-07-17 21:09:59 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[setSelectedPlatformOptions]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
2024-02-23 14:57:18 +00:00
|
|
|
<Modal title="Save query" onExit={toggleSaveQueryModal}>
|
2023-10-02 19:26:53 +00:00
|
|
|
<form
|
|
|
|
|
onSubmit={onClickSaveQuery}
|
|
|
|
|
className={baseClass}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
>
|
|
|
|
|
<InputField
|
|
|
|
|
name="name"
|
|
|
|
|
onChange={(value: string) => setName(value)}
|
2024-10-03 22:59:10 +00:00
|
|
|
onBlur={() => {
|
|
|
|
|
setName(name.trim());
|
|
|
|
|
}}
|
2023-10-02 19:26:53 +00:00
|
|
|
value={name}
|
|
|
|
|
error={errors.name}
|
|
|
|
|
inputClassName={`${baseClass}__name`}
|
|
|
|
|
label="Name"
|
|
|
|
|
autofocus
|
|
|
|
|
ignore1password
|
|
|
|
|
/>
|
|
|
|
|
<InputField
|
|
|
|
|
name="description"
|
|
|
|
|
onChange={(value: string) => setDescription(value)}
|
|
|
|
|
value={description}
|
|
|
|
|
inputClassName={`${baseClass}__description`}
|
|
|
|
|
label="Description"
|
|
|
|
|
type="textarea"
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText="What information does your query reveal? (optional)"
|
2023-10-02 19:26:53 +00:00
|
|
|
/>
|
|
|
|
|
<Dropdown
|
|
|
|
|
searchable={false}
|
|
|
|
|
options={FREQUENCY_DROPDOWN_OPTIONS}
|
|
|
|
|
onChange={(value: number) => {
|
|
|
|
|
setSelectedFrequency(value);
|
|
|
|
|
}}
|
2024-02-23 14:57:18 +00:00
|
|
|
placeholder="Every hour"
|
2023-10-02 19:26:53 +00:00
|
|
|
value={selectedFrequency}
|
|
|
|
|
label="Frequency"
|
2023-10-09 18:31:31 +00:00
|
|
|
wrapperClassName={`${baseClass}__form-field form-field--frequency`}
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText="This is how often your query collects data."
|
2023-10-02 19:26:53 +00:00
|
|
|
/>
|
|
|
|
|
<Checkbox
|
|
|
|
|
name="observerCanRun"
|
|
|
|
|
onChange={setObserverCanRun}
|
|
|
|
|
value={observerCanRun}
|
2024-02-23 14:57:18 +00:00
|
|
|
wrapperClassName="observer-can-run-wrapper"
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText="Users with the Observer role will be able to run this query as a live query."
|
2023-07-17 21:09:59 +00:00
|
|
|
>
|
2023-10-02 19:26:53 +00:00
|
|
|
Observers can run
|
|
|
|
|
</Checkbox>
|
2024-11-13 14:32:59 +00:00
|
|
|
<Slider
|
|
|
|
|
onChange={() => setAutomationsEnabled(!automationsEnabled)}
|
|
|
|
|
value={automationsEnabled}
|
|
|
|
|
activeText={
|
|
|
|
|
<>
|
|
|
|
|
Automations on
|
|
|
|
|
{selectedFrequency === 0 && (
|
|
|
|
|
<TooltipWrapper
|
|
|
|
|
tipContent={
|
|
|
|
|
<>
|
|
|
|
|
Automations and reporting will be paused <br />
|
|
|
|
|
for this query until a frequency is set.
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
position="right"
|
|
|
|
|
tipOffset={9}
|
|
|
|
|
showArrow
|
|
|
|
|
underline={false}
|
|
|
|
|
>
|
|
|
|
|
<Icon name="warning" />
|
|
|
|
|
</TooltipWrapper>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
inactiveText="Automations off"
|
|
|
|
|
helpText={
|
|
|
|
|
<>
|
|
|
|
|
Historical results will {!automationsEnabled ? "not " : ""}be sent
|
2024-12-05 23:19:56 +00:00
|
|
|
to your log destination:{" "}
|
|
|
|
|
<b>
|
|
|
|
|
<LogDestinationIndicator
|
|
|
|
|
logDestination={config?.logging.result.plugin || ""}
|
|
|
|
|
excludeTooltip
|
|
|
|
|
/>
|
|
|
|
|
</b>
|
|
|
|
|
.
|
2024-11-13 14:32:59 +00:00
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2023-10-02 19:26:53 +00:00
|
|
|
<RevealButton
|
|
|
|
|
isShowing={showAdvancedOptions}
|
2024-02-23 14:57:18 +00:00
|
|
|
className="advanced-options-toggle"
|
|
|
|
|
hideText="Hide advanced options"
|
|
|
|
|
showText="Show advanced options"
|
|
|
|
|
caretPosition="after"
|
2023-10-02 19:26:53 +00:00
|
|
|
onClick={toggleAdvancedOptions}
|
|
|
|
|
/>
|
|
|
|
|
{showAdvancedOptions && (
|
|
|
|
|
<>
|
|
|
|
|
<Dropdown
|
|
|
|
|
options={SCHEDULE_PLATFORM_DROPDOWN_OPTIONS}
|
|
|
|
|
placeholder="Select"
|
|
|
|
|
label="Platforms"
|
|
|
|
|
onChange={onChangeSelectPlatformOptions}
|
|
|
|
|
value={selectedPlatformOptions}
|
|
|
|
|
multi
|
2023-10-09 18:31:31 +00:00
|
|
|
wrapperClassName={`${baseClass}__form-field form-field--platform`}
|
2024-01-18 15:48:44 +00:00
|
|
|
helpText="By default, your query collects data on all compatible platforms."
|
2023-10-02 19:26:53 +00:00
|
|
|
/>
|
|
|
|
|
<Dropdown
|
|
|
|
|
options={MIN_OSQUERY_VERSION_OPTIONS}
|
|
|
|
|
onChange={setSelectedMinOsqueryVersionOptions}
|
|
|
|
|
placeholder="Select"
|
|
|
|
|
value={selectedMinOsqueryVersionOptions}
|
|
|
|
|
label="Minimum osquery version"
|
|
|
|
|
wrapperClassName={`${baseClass}__form-field ${baseClass}__form-field--osquer-vers`}
|
|
|
|
|
/>
|
|
|
|
|
<Dropdown
|
|
|
|
|
options={LOGGING_TYPE_OPTIONS}
|
|
|
|
|
onChange={setSelectedLoggingType}
|
|
|
|
|
placeholder="Select"
|
|
|
|
|
value={selectedLoggingType}
|
|
|
|
|
label="Logging"
|
|
|
|
|
wrapperClassName={`${baseClass}__form-field ${baseClass}__form-field--logging`}
|
|
|
|
|
/>
|
2023-10-09 18:31:31 +00:00
|
|
|
{queryReportsDisabled !== undefined && (
|
|
|
|
|
<DiscardDataOption
|
|
|
|
|
{...{
|
|
|
|
|
queryReportsDisabled,
|
|
|
|
|
selectedLoggingType,
|
|
|
|
|
discardData,
|
|
|
|
|
setDiscardData,
|
|
|
|
|
breakHelpText: true,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2023-10-02 19:26:53 +00:00
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
<div className="modal-cta-wrap">
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="brand"
|
|
|
|
|
className="save-query-loading"
|
|
|
|
|
isLoading={isLoading}
|
2023-07-17 21:09:59 +00:00
|
|
|
>
|
2023-10-02 19:26:53 +00:00
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={toggleSaveQueryModal} variant="inverse">
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
2023-07-17 21:09:59 +00:00
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SaveQueryModal;
|