2022-02-05 00:48:35 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
|
|
|
|
|
|
import Modal from "components/Modal";
|
|
|
|
|
import Button from "components/buttons/Button";
|
2022-03-11 17:56:14 +00:00
|
|
|
import Slider from "components/forms/fields/Slider";
|
2022-02-05 00:48:35 +00:00
|
|
|
// @ts-ignore
|
|
|
|
|
import InputField from "components/forms/fields/InputField";
|
|
|
|
|
|
|
|
|
|
import { IWebhookSoftwareVulnerabilities } from "interfaces/webhook";
|
|
|
|
|
import { useDeepEffect } from "utilities/hooks";
|
|
|
|
|
import { size } from "lodash";
|
|
|
|
|
|
|
|
|
|
import PreviewPayloadModal from "../PreviewPayloadModal";
|
|
|
|
|
|
|
|
|
|
interface IManageAutomationsModalProps {
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
onCreateWebhookSubmit: (formData: IWebhookSoftwareVulnerabilities) => void;
|
|
|
|
|
togglePreviewPayloadModal: () => void;
|
|
|
|
|
showPreviewPayloadModal: boolean;
|
|
|
|
|
softwareVulnerabilityWebhookEnabled?: boolean;
|
|
|
|
|
currentDestinationUrl?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validateWebhookURL = (url: string) => {
|
|
|
|
|
const errors: { [key: string]: string } = {};
|
|
|
|
|
|
2022-03-03 17:20:49 +00:00
|
|
|
if (url === "") {
|
|
|
|
|
errors.url = "Please add a destination URL";
|
2022-02-05 00:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const valid = !size(errors);
|
|
|
|
|
return { valid, errors };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const baseClass = "manage-automations-modal";
|
|
|
|
|
|
|
|
|
|
const ManageAutomationsModal = ({
|
|
|
|
|
onCancel: onReturnToApp,
|
|
|
|
|
onCreateWebhookSubmit,
|
|
|
|
|
togglePreviewPayloadModal,
|
|
|
|
|
showPreviewPayloadModal,
|
|
|
|
|
softwareVulnerabilityWebhookEnabled,
|
|
|
|
|
currentDestinationUrl,
|
|
|
|
|
}: IManageAutomationsModalProps): JSX.Element => {
|
|
|
|
|
const [destination_url, setDestinationUrl] = useState<string>(
|
|
|
|
|
currentDestinationUrl || ""
|
|
|
|
|
);
|
|
|
|
|
const [errors, setErrors] = useState<{ [key: string]: string }>({});
|
|
|
|
|
|
2022-03-11 17:56:14 +00:00
|
|
|
const [
|
|
|
|
|
softwareAutomationsEnabled,
|
|
|
|
|
setSoftwareAutomationsEnabled,
|
|
|
|
|
] = useState<boolean>(softwareVulnerabilityWebhookEnabled || false);
|
2022-02-05 00:48:35 +00:00
|
|
|
|
|
|
|
|
useDeepEffect(() => {
|
|
|
|
|
if (destination_url) {
|
|
|
|
|
setErrors({});
|
|
|
|
|
}
|
|
|
|
|
}, [destination_url]);
|
|
|
|
|
|
|
|
|
|
const onURLChange = (value: string) => {
|
|
|
|
|
setDestinationUrl(value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleSaveAutomation = (evt: React.MouseEvent<HTMLFormElement>) => {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
|
|
const { valid, errors: newErrors } = validateWebhookURL(destination_url);
|
|
|
|
|
setErrors({
|
|
|
|
|
...errors,
|
|
|
|
|
...newErrors,
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-03 17:20:49 +00:00
|
|
|
// URL validation only needed if software automation is checked
|
2022-03-11 17:56:14 +00:00
|
|
|
if (valid || !softwareAutomationsEnabled) {
|
2022-02-05 00:48:35 +00:00
|
|
|
onCreateWebhookSubmit({
|
|
|
|
|
destination_url,
|
2022-03-11 17:56:14 +00:00
|
|
|
enable_vulnerabilities_webhook: softwareAutomationsEnabled,
|
2022-02-05 00:48:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onReturnToApp();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (showPreviewPayloadModal) {
|
|
|
|
|
return <PreviewPayloadModal onCancel={togglePreviewPayloadModal} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
onExit={onReturnToApp}
|
|
|
|
|
title={"Manage automations"}
|
|
|
|
|
className={baseClass}
|
|
|
|
|
>
|
|
|
|
|
<div className={baseClass}>
|
|
|
|
|
<div className={`${baseClass}__software-select-items`}>
|
2022-03-11 17:56:14 +00:00
|
|
|
<Slider
|
|
|
|
|
value={softwareAutomationsEnabled}
|
|
|
|
|
onChange={() =>
|
|
|
|
|
setSoftwareAutomationsEnabled(!softwareAutomationsEnabled)
|
2022-02-05 00:48:35 +00:00
|
|
|
}
|
2022-03-11 17:56:14 +00:00
|
|
|
inactiveText={"Vulnerability automations disabled"}
|
|
|
|
|
activeText={"Vulnerability automations enabled"}
|
2022-02-05 00:48:35 +00:00
|
|
|
/>
|
|
|
|
|
</div>
|
2022-03-11 17:56:14 +00:00
|
|
|
<div className={`${baseClass}__overlay-container`}>
|
|
|
|
|
<div className={`${baseClass}__software-automation-enabled`}>
|
|
|
|
|
<div className={`${baseClass}__software-automation-description`}>
|
|
|
|
|
<p>
|
|
|
|
|
A request will be sent to your configured <b>Destination URL</b>{" "}
|
2022-04-06 14:42:02 +00:00
|
|
|
if a detected vulnerability (CVE) was published in the last 30
|
2022-03-11 17:56:14 +00:00
|
|
|
days.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2022-03-16 06:38:19 +00:00
|
|
|
<InputField
|
|
|
|
|
inputWrapperClass={`${baseClass}__url-input`}
|
|
|
|
|
name="webhook-url"
|
|
|
|
|
label={"Destination URL"}
|
|
|
|
|
type={"text"}
|
|
|
|
|
value={destination_url}
|
|
|
|
|
onChange={onURLChange}
|
|
|
|
|
error={errors.url}
|
|
|
|
|
hint={
|
|
|
|
|
"For each new vulnerability detected, Fleet will send a JSON payload to this URL with a list of the affected hosts."
|
|
|
|
|
}
|
|
|
|
|
placeholder={"https://server.com/example"}
|
|
|
|
|
tooltip="Provide a URL to deliver a webhook request to."
|
|
|
|
|
/>
|
2022-03-11 17:56:14 +00:00
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="text-link"
|
|
|
|
|
onClick={togglePreviewPayloadModal}
|
|
|
|
|
>
|
|
|
|
|
Preview payload
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
{!softwareAutomationsEnabled && (
|
|
|
|
|
<div className={`${baseClass}__overlay`} />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2022-02-05 00:48:35 +00:00
|
|
|
<div className={`${baseClass}__button-wrap`}>
|
|
|
|
|
<Button
|
|
|
|
|
className={`${baseClass}__btn`}
|
|
|
|
|
onClick={onReturnToApp}
|
|
|
|
|
variant="inverse"
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
className={`${baseClass}__btn`}
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="brand"
|
|
|
|
|
onClick={handleSaveAutomation}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default ManageAutomationsModal;
|