2025-02-03 22:27:44 +00:00
|
|
|
import React, { useContext, useState } from "react";
|
|
|
|
|
import { useQuery } from "react-query";
|
|
|
|
|
|
2025-05-12 20:51:38 +00:00
|
|
|
import classnames from "classnames";
|
|
|
|
|
|
2025-02-03 22:27:44 +00:00
|
|
|
import { NotificationContext } from "context/notification";
|
2025-02-27 15:53:34 +00:00
|
|
|
import { AppContext } from "context/app";
|
|
|
|
|
import { getPathWithQueryParams } from "utilities/url";
|
2025-02-03 22:27:44 +00:00
|
|
|
import scriptAPI from "services/entities/scripts";
|
|
|
|
|
|
|
|
|
|
import Button from "components/buttons/Button";
|
|
|
|
|
import CustomLink from "components/CustomLink";
|
|
|
|
|
import DataError from "components/DataError";
|
|
|
|
|
import Editor from "components/Editor";
|
|
|
|
|
import Modal from "components/Modal";
|
|
|
|
|
import ModalFooter from "components/ModalFooter";
|
|
|
|
|
import Spinner from "components/Spinner";
|
|
|
|
|
import paths from "router/paths";
|
|
|
|
|
|
|
|
|
|
import { ScriptContent } from "interfaces/script";
|
|
|
|
|
import { DEFAULT_USE_QUERY_OPTIONS } from "utilities/constants";
|
|
|
|
|
import { getErrorMessage } from "../ScriptUploader/helpers";
|
|
|
|
|
|
|
|
|
|
const baseClass = "edit-script-modal";
|
|
|
|
|
|
2025-05-12 20:51:38 +00:00
|
|
|
interface IWarningModal {
|
|
|
|
|
onExit: () => void;
|
|
|
|
|
onSave: () => void;
|
|
|
|
|
scriptName: string;
|
|
|
|
|
isSubmitting: boolean;
|
|
|
|
|
}
|
|
|
|
|
const WarningModal = ({
|
|
|
|
|
onExit,
|
|
|
|
|
onSave,
|
|
|
|
|
scriptName,
|
|
|
|
|
isSubmitting,
|
|
|
|
|
}: IWarningModal) => {
|
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
className={`${baseClass}__warning`}
|
|
|
|
|
title="Save changes?"
|
|
|
|
|
onExit={onExit}
|
|
|
|
|
>
|
|
|
|
|
<>
|
|
|
|
|
<p>
|
|
|
|
|
The changes you are making will cancel any pending script runs for{" "}
|
|
|
|
|
<b>{scriptName}</b>.<br />
|
|
|
|
|
<br />
|
|
|
|
|
If this script is currently running on a host, it will complete, but
|
|
|
|
|
results won't appear in Fleet. <br />
|
|
|
|
|
<br />
|
|
|
|
|
You cannot undo this action.
|
|
|
|
|
</p>
|
|
|
|
|
<div className="modal-cta-wrap">
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={onSave}
|
|
|
|
|
className="save-loading"
|
|
|
|
|
isLoading={isSubmitting}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={onExit} variant="inverse">
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-03 22:27:44 +00:00
|
|
|
interface IEditScriptModal {
|
|
|
|
|
onExit: () => void;
|
|
|
|
|
scriptId: number;
|
|
|
|
|
scriptName: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validate = (scriptContent: string) => {
|
|
|
|
|
if (scriptContent.trim() === "") {
|
|
|
|
|
return "Script cannot be empty";
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const EditScriptModal = ({
|
|
|
|
|
scriptId,
|
|
|
|
|
scriptName,
|
|
|
|
|
onExit,
|
|
|
|
|
}: IEditScriptModal) => {
|
|
|
|
|
const { renderFlash } = useContext(NotificationContext);
|
2025-02-27 15:53:34 +00:00
|
|
|
const { currentTeam } = useContext(AppContext);
|
2025-02-03 22:27:44 +00:00
|
|
|
|
|
|
|
|
// Editable script content
|
|
|
|
|
const [scriptFormData, setScriptFormData] = useState("");
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
|
const [formError, setFormError] = useState<string | null>(null);
|
|
|
|
|
|
2025-05-12 20:51:38 +00:00
|
|
|
const [showConfirmChanges, setShowConfirmChanges] = useState(false);
|
|
|
|
|
|
2025-02-03 22:27:44 +00:00
|
|
|
const {
|
2025-05-12 20:51:38 +00:00
|
|
|
data: curScriptContent,
|
2025-02-03 22:27:44 +00:00
|
|
|
error: isSelectedScriptContentError,
|
|
|
|
|
isLoading: isLoadingSelectedScriptContent,
|
|
|
|
|
} = useQuery<ScriptContent, Error>(
|
|
|
|
|
[scriptId],
|
|
|
|
|
() => scriptAPI.downloadScript(scriptId),
|
|
|
|
|
{
|
|
|
|
|
...DEFAULT_USE_QUERY_OPTIONS,
|
2025-05-12 20:51:38 +00:00
|
|
|
onSuccess: (curScriptContent_) => {
|
|
|
|
|
setScriptFormData(curScriptContent_);
|
2025-02-03 22:27:44 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onChange = (value: string) => {
|
|
|
|
|
setScriptFormData(value);
|
2025-02-04 17:27:52 +00:00
|
|
|
const err = validate(value);
|
|
|
|
|
if (!err && !!formError) {
|
|
|
|
|
setFormError(validate(value));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onBlur = () => {
|
|
|
|
|
setFormError(validate(scriptFormData));
|
2025-02-03 22:27:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSave = async () => {
|
2025-02-04 17:27:52 +00:00
|
|
|
const err = validate(scriptFormData);
|
|
|
|
|
setFormError(err);
|
|
|
|
|
if (err || isSubmitting) {
|
2025-02-03 22:27:44 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2025-05-12 20:51:38 +00:00
|
|
|
// if contents have changed and not already showing the warning modal
|
|
|
|
|
if (curScriptContent !== scriptFormData && !showConfirmChanges) {
|
|
|
|
|
setShowConfirmChanges(true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// show warning modal and abort this call
|
2025-02-03 22:27:44 +00:00
|
|
|
try {
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
|
await scriptAPI.updateScript(scriptId, scriptFormData, scriptName);
|
|
|
|
|
renderFlash("success", "Successfully saved script.");
|
|
|
|
|
onExit();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
renderFlash("error", getErrorMessage(e));
|
|
|
|
|
} finally {
|
|
|
|
|
setIsSubmitting(false);
|
2025-05-12 20:51:38 +00:00
|
|
|
setShowConfirmChanges(false);
|
2025-02-03 22:27:44 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
onSave();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const renderContent = () => {
|
|
|
|
|
if (isLoadingSelectedScriptContent) {
|
|
|
|
|
return <Spinner />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isSelectedScriptContentError) {
|
|
|
|
|
return <DataError description="Close this modal and try again." />;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 15:24:05 +00:00
|
|
|
// Set editing mode based on the file extension.
|
|
|
|
|
const mode = scriptName.match(/\.sh$/) ? "sh" : "powershell";
|
|
|
|
|
|
2025-02-03 22:27:44 +00:00
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<form onSubmit={onSubmit}>
|
|
|
|
|
<Editor
|
2025-04-29 15:24:05 +00:00
|
|
|
mode={mode}
|
2025-02-03 22:27:44 +00:00
|
|
|
error={formError}
|
2025-02-14 14:31:21 +00:00
|
|
|
label="Script"
|
2025-02-04 17:27:52 +00:00
|
|
|
onBlur={onBlur}
|
2025-02-14 14:31:21 +00:00
|
|
|
onChange={onChange}
|
|
|
|
|
value={scriptFormData}
|
2025-02-03 22:27:44 +00:00
|
|
|
/>
|
|
|
|
|
<div className="form-field__help-text">
|
|
|
|
|
To run this script on a host, go to the{" "}
|
2025-02-27 15:53:34 +00:00
|
|
|
<CustomLink
|
|
|
|
|
text="Hosts"
|
|
|
|
|
url={getPathWithQueryParams(paths.MANAGE_HOSTS, {
|
|
|
|
|
team_id: currentTeam?.id,
|
|
|
|
|
})}
|
|
|
|
|
/>{" "}
|
|
|
|
|
page and select a host.
|
2025-02-03 22:27:44 +00:00
|
|
|
<br />
|
|
|
|
|
To run the script across multiple hosts, add a policy automation on
|
2025-02-27 15:53:34 +00:00
|
|
|
the{" "}
|
|
|
|
|
<CustomLink
|
|
|
|
|
text="Policies"
|
|
|
|
|
url={getPathWithQueryParams(paths.MANAGE_POLICIES, {
|
|
|
|
|
team_id: currentTeam?.id,
|
|
|
|
|
})}
|
|
|
|
|
/>{" "}
|
|
|
|
|
page.
|
2025-02-03 22:27:44 +00:00
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
<ModalFooter
|
|
|
|
|
primaryButtons={
|
|
|
|
|
<>
|
|
|
|
|
<Button onClick={onExit} variant="inverse">
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={onSave}
|
|
|
|
|
isLoading={isSubmitting}
|
|
|
|
|
disabled={!!formError}
|
|
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-12 20:51:38 +00:00
|
|
|
const classes = classnames(baseClass, {
|
|
|
|
|
[`${baseClass}__hide-main`]: !!showConfirmChanges,
|
|
|
|
|
});
|
2025-02-03 22:27:44 +00:00
|
|
|
return (
|
2025-05-12 20:51:38 +00:00
|
|
|
<>
|
|
|
|
|
<Modal
|
|
|
|
|
className={classes}
|
|
|
|
|
title={scriptName}
|
|
|
|
|
width="large"
|
|
|
|
|
onExit={onExit}
|
|
|
|
|
>
|
|
|
|
|
{renderContent()}
|
|
|
|
|
</Modal>
|
|
|
|
|
{!!showConfirmChanges && (
|
|
|
|
|
<WarningModal
|
|
|
|
|
onExit={() => setShowConfirmChanges(false)}
|
|
|
|
|
onSave={onSave}
|
|
|
|
|
scriptName={scriptName}
|
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2025-02-03 22:27:44 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default EditScriptModal;
|