fleet/frontend/components/AddHostsModal/PlatformWrapper/PlatformWrapper.tsx

601 lines
19 KiB
TypeScript
Raw Normal View History

2022-02-16 21:21:42 +00:00
import React, { useContext, useState } from "react";
import { Tab, Tabs, TabList, TabPanel } from "react-tabs";
import FileSaver from "file-saver";
import { NotificationContext } from "context/notification";
// @ts-ignore
2022-02-16 21:21:42 +00:00
import { stringToClipboard } from "utilities/copy_text";
import { IConfig } from "interfaces/config";
2022-02-16 21:21:42 +00:00
import Button from "components/buttons/Button";
import Icon from "components/Icon/Icon";
import RevealButton from "components/buttons/RevealButton";
// @ts-ignore
2022-02-16 21:21:42 +00:00
import InputField from "components/forms/fields/InputField";
import Checkbox from "components/forms/fields/Checkbox";
import TooltipWrapper from "components/TooltipWrapper";
2022-02-16 21:21:42 +00:00
import TabsWrapper from "components/TabsWrapper";
2023-05-30 23:17:14 +00:00
import InfoBanner from "components/InfoBanner/InfoBanner";
import CustomLink from "components/CustomLink/CustomLink";
2022-02-16 21:21:42 +00:00
import { isValidPemCertificate } from "../../../pages/hosts/ManageHostsPage/helpers";
interface IPlatformSubNav {
name: string;
type: string;
}
const platformSubNav: IPlatformSubNav[] = [
{
name: "macOS",
type: "pkg",
},
{
name: "Windows",
type: "msi",
},
{
name: "Linux (RPM)",
type: "rpm",
},
{
name: "Linux (deb)",
2022-02-16 21:21:42 +00:00
type: "deb",
},
2023-05-30 23:17:14 +00:00
{
name: "ChromeOS",
type: "chromeos",
},
2022-02-16 21:21:42 +00:00
{
name: "Advanced",
type: "advanced",
},
];
interface IPlatformWrapperProps {
enrollSecret: string;
2022-02-16 21:21:42 +00:00
onCancel: () => void;
certificate: any;
isFetchingCertificate: boolean;
fetchCertificateError: any;
config: IConfig | null;
2022-02-16 21:21:42 +00:00
}
const baseClass = "platform-wrapper";
const PlatformWrapper = ({
enrollSecret,
2022-02-16 21:21:42 +00:00
onCancel,
certificate,
isFetchingCertificate,
fetchCertificateError,
config,
}: IPlatformWrapperProps): JSX.Element => {
const { renderFlash } = useContext(NotificationContext);
2023-05-30 23:17:14 +00:00
const [copyMessage, setCopyMessage] = useState<Record<string, string>>({});
const [includeFleetDesktop, setIncludeFleetDesktop] = useState(true);
const [showPlainOsquery, setShowPlainOsquery] = useState(false);
2023-05-30 23:17:14 +00:00
const [selectedTabIndex, setSelectedTabIndex] = useState(0); // External link requires control in state
2022-02-16 21:21:42 +00:00
let tlsHostname = config?.server_settings.server_url || "";
2022-02-16 21:21:42 +00:00
try {
const serverUrl = new URL(config?.server_settings.server_url || "");
2022-02-16 21:21:42 +00:00
tlsHostname = serverUrl.hostname;
if (serverUrl.port) {
tlsHostname += `:${serverUrl.port}`;
}
} catch (e) {
if (!(e instanceof TypeError)) {
throw e;
}
}
const flagfileContent = `# Server
--tls_hostname=${tlsHostname}
--tls_server_certs=fleet.pem
# Enrollment
--host_identifier=instance
--enroll_secret_path=secret.txt
--enroll_tls_endpoint=/api/osquery/enroll
2022-02-16 21:21:42 +00:00
# Configuration
--config_plugin=tls
--config_tls_endpoint=/api/v1/osquery/config
2022-02-16 21:21:42 +00:00
--config_refresh=10
# Live query
--disable_distributed=false
--distributed_plugin=tls
--distributed_interval=10
--distributed_tls_max_attempts=3
--distributed_tls_read_endpoint=/api/v1/osquery/distributed/read
--distributed_tls_write_endpoint=/api/v1/osquery/distributed/write
2022-02-16 21:21:42 +00:00
# Logging
--logger_plugin=tls
--logger_tls_endpoint=/api/v1/osquery/log
2022-02-16 21:21:42 +00:00
--logger_tls_period=10
# File carving
--disable_carver=false
--carver_start_endpoint=/api/v1/osquery/carve/begin
--carver_continue_endpoint=/api/v1/osquery/carve/block
--carver_block_size=8000000`;
2022-02-16 21:21:42 +00:00
const onDownloadEnrollSecret = (evt: React.MouseEvent) => {
2022-02-16 21:21:42 +00:00
evt.preventDefault();
const filename = "secret.txt";
const file = new global.window.File([enrollSecret], filename);
FileSaver.saveAs(file);
return false;
};
const onDownloadFlagfile = (evt: React.MouseEvent) => {
2022-02-16 21:21:42 +00:00
evt.preventDefault();
const filename = "flagfile.txt";
const file = new global.window.File([flagfileContent], filename);
FileSaver.saveAs(file);
return false;
};
const onDownloadCertificate = (evt: React.MouseEvent) => {
evt.preventDefault();
if (certificate && isValidPemCertificate(certificate)) {
const filename = "fleet.pem";
const file = new global.window.File([certificate], filename, {
type: "application/x-pem-file",
});
FileSaver.saveAs(file);
} else {
renderFlash(
"error",
"Your certificate could not be downloaded. Please check your Fleet configuration."
2022-02-16 21:21:42 +00:00
);
}
return false;
};
const renderFleetCertificateBlock = (type: "plain" | "tooltip") => {
return (
<div className={`${baseClass}__advanced--fleet-certificate`}>
{type === "plain" ? (
<div className={`${baseClass}__advanced--heading`}>
Download your Fleet certificate
</div>
) : (
<div
className={`${baseClass}__advanced--heading download-certificate--tooltip`}
>
Download your{" "}
<TooltipWrapper tipContent="A Fleet certificate is required if Fleet is running with a self signed or otherwise untrusted certificate.">
Fleet certificate:
</TooltipWrapper>
</div>
)}
{isFetchingCertificate && (
<p className={`${baseClass}__certificate-loading`}>
Loading your certificate
</p>
)}
{!isFetchingCertificate &&
(certificate ? (
<p>
{type === "plain" && (
<>
Prove the TLS certificate used by the Fleet server to enable
secure connections from osquery:
<br />
</>
)}
<Button
variant="text-icon"
className={`${baseClass}__fleet-certificate-download`}
onClick={onDownloadCertificate}
>
Download
<Icon name="download" color="core-fleet-blue" size="small" />
</Button>
</p>
) : (
<p className={`${baseClass}__certificate-error`}>
<em>Fleet failed to load your certificate.</em>
<span>
If you&apos;re able to access Fleet at a private or secure
(HTTPS) IP address, please log into Fleet at this address to
load your certificate.
</span>
</p>
))}
</div>
);
};
const renderInstallerString = (packageType: string) => {
return packageType === "advanced"
2022-10-18 16:53:43 +00:00
? `fleetctl package --type=YOUR_TYPE --fleet-url=${config?.server_settings.server_url}
--enroll-secret=${enrollSecret}
--fleet-certificate=PATH_TO_YOUR_CERTIFICATE/fleet.pem`
: `fleetctl package --type=${packageType} ${
includeFleetDesktop ? "--fleet-desktop " : ""
}--fleet-url=${
config?.server_settings.server_url
} --enroll-secret=${enrollSecret}`;
2022-02-16 21:21:42 +00:00
};
const renderLabel = (packageType: string, installerString: string) => {
2022-02-16 21:21:42 +00:00
const onCopyInstaller = (evt: React.MouseEvent) => {
evt.preventDefault();
stringToClipboard(installerString)
.then(() =>
setCopyMessage((prev) => ({ ...prev, [packageType]: "Copied!" }))
)
.catch(() =>
setCopyMessage((prev) => ({ ...prev, [packageType]: "Copy failed" }))
);
2022-02-16 21:21:42 +00:00
// Clear message after 1 second
setTimeout(
() => setCopyMessage((prev) => ({ ...prev, [packageType]: "" })),
1000
);
2022-02-16 21:21:42 +00:00
return false;
};
return (
<>
{packageType === "plain-osquery" ? (
<>
<p className={`${baseClass}__advanced--heading`}>
With{" "}
<a
href="https://www.osquery.io/downloads"
target="_blank"
rel="noopener noreferrer"
>
osquery
</a>{" "}
installed:
</p>
<p className={`${baseClass}__advanced--text`}>
Run osquery from the directory containing the above files (may
require sudo or Run as Administrator privileges):
</p>
</>
) : (
2022-02-16 21:21:42 +00:00
<span className={`${baseClass}__cta`}>
Run this command with the{" "}
2022-02-16 21:21:42 +00:00
<a
className={`${baseClass}__command-line-tool`}
2022-09-08 15:27:26 +00:00
href="https://fleetdm.com/docs/using-fleet/fleetctl-cli"
2022-02-16 21:21:42 +00:00
target="_blank"
rel="noopener noreferrer"
>
Fleet command-line tool
</a>{" "}
installed:
</span>
)}{" "}
<span className={`${baseClass}__name`}>
<span className="buttons">
<Button
variant="unstyled"
className={`${baseClass}__installer-copy-icon`}
onClick={onCopyInstaller}
>
<Icon name="copy" />
2022-02-16 21:21:42 +00:00
</Button>
{copyMessage[packageType] && (
<span
className={`${baseClass}__copy-message`}
>{`${copyMessage[packageType]} `}</span>
)}
2022-02-16 21:21:42 +00:00
</span>
</span>
</>
);
};
2023-05-30 23:17:14 +00:00
const renderChromeOSLabel = (label: string, value: string) => {
const onCopyChromeOSLabel = (evt: React.MouseEvent) => {
evt.preventDefault();
stringToClipboard(value)
.then(() => setCopyMessage((prev) => ({ ...prev, [label]: "Copied!" })))
.catch(() =>
setCopyMessage((prev) => ({
...prev,
[label]: "Copy failed",
}))
);
// Clear message after 1 second
setTimeout(
() => setCopyMessage((prev) => ({ ...prev, [label]: "" })),
1000
);
return false;
};
return (
<>
{label}
<span className="buttons">
<Button
variant="unstyled"
className={`${baseClass}__chromeos-copy-icon`}
onClick={onCopyChromeOSLabel}
>
<Icon name="copy" />
</Button>
{copyMessage[label] && (
<span className={`${baseClass}__copy-message`}>Copied!</span>
)}
</span>
</>
);
};
const renderTab = (packageType: string) => {
const CHROME_OS_INFO = {
extensionId: "fleeedmmihkfkeemmipgmhhjemlljidg",
installationUrl: "https://chrome.fleetdm.com/updates.xml",
policyForExtension: `{
"fleet_url": {
"Value": "${config?.server_settings.server_url}"
},
"enroll_secret": {
"Value": "${enrollSecret}"
}
}`,
};
2023-05-30 23:17:14 +00:00
if (packageType === "chromeos") {
return (
<div className={baseClass}>
<div className={`${baseClass}__chromeos`}>
<div className={`${baseClass}__chromeos--add-extension`}>
<p className={`${baseClass}__chromeos--heading`}>
In Google Admin:
</p>
<p>
Add the extension for the relevant users & browsers using the
information below.
</p>
<InfoBanner className={`${baseClass}__chromeos--instructions`}>
For a step-by-step guide, see the documentation page for{" "}
<CustomLink
url="https://fleetdm.com/docs/using-fleet/adding-hosts#enroll-chromebooks"
2023-05-30 23:17:14 +00:00
text="adding hosts"
newTab
multiline
/>
</InfoBanner>
<div className={`${baseClass}__chromeos--installer`}>
<InputField
disabled
inputWrapperClass={`${baseClass}__installer-input ${baseClass}__chromeos-extension-id`}
name="Extension ID"
label={renderChromeOSLabel(
"Extension ID",
CHROME_OS_INFO.extensionId
)}
value={CHROME_OS_INFO.extensionId}
/>
<InputField
disabled
inputWrapperClass={`${baseClass}__installer-input ${baseClass}__chromeos-url`}
name="Installation URL"
label={renderChromeOSLabel(
"Installation URL",
CHROME_OS_INFO.installationUrl
)}
value={CHROME_OS_INFO.installationUrl}
2023-05-30 23:17:14 +00:00
/>
<InputField
disabled
inputWrapperClass={`${baseClass}__installer-input ${baseClass}__chromeos-policy-for-extension`}
name="Policy for extension"
label={renderChromeOSLabel(
"Policy for extension",
CHROME_OS_INFO.policyForExtension
)}
type="textarea"
value={CHROME_OS_INFO.policyForExtension}
/>
</div>
</div>
</div>
</div>
);
}
if (packageType === "advanced") {
2022-02-16 21:21:42 +00:00
return (
<div className={baseClass}>
<div className={`${baseClass}__advanced`}>
{renderFleetCertificateBlock("tooltip")}
<div className={`${baseClass}__advanced--installer`}>
<InputField
disabled
inputWrapperClass={`${baseClass}__installer-input ${baseClass}__installer-input-${packageType}`}
name="installer"
label={renderLabel(
packageType,
renderInstallerString(packageType)
)}
2023-05-30 23:17:14 +00:00
type="textarea"
value={renderInstallerString(packageType)}
/>
2023-05-30 23:17:14 +00:00
<p className={`${baseClass}__advanced--instructions`}>
Distribute your package to add hosts to Fleet.
</p>
<InfoBanner className={`${baseClass}__chrome--instructions`}>
This works for macOS, Windows, and Linux hosts. To add
Chromebooks,{" "}
<Button
variant="text-link"
onClick={() => setSelectedTabIndex(4)}
>
click here
</Button>
.
</InfoBanner>
2022-02-16 21:21:42 +00:00
</div>
<RevealButton
className={baseClass}
isShowing={showPlainOsquery}
hideText={"Plain osquery"}
showText={"Plain osquery"}
caretPosition={"after"}
onClick={() => setShowPlainOsquery((prev) => !prev)}
/>
{showPlainOsquery && (
<>
<div className={`${baseClass}__advanced--enroll-secrets`}>
<p className={`${baseClass}__advanced--heading`}>
Download your enroll secret:
</p>
<p>
Osquery uses an enroll secret to authenticate with the Fleet
server.
<br />
<Button
variant="text-icon"
onClick={onDownloadEnrollSecret}
2022-02-16 21:21:42 +00:00
>
Download
<Icon
name="download"
color="core-fleet-blue"
size="small"
/>
</Button>
</p>
</div>
{renderFleetCertificateBlock("plain")}
<div className={`${baseClass}__advanced--flagfile`}>
<p className={`${baseClass}__advanced--heading`}>
Download your flagfile:
</p>
<p>
If using the enroll secret and server certificate downloaded
above, use the generated flagfile. In some configurations,
modifications may need to be made.
<br />
{fetchCertificateError ? (
<span className={`${baseClass}__error`}>
{fetchCertificateError}
</span>
) : (
<Button variant="text-icon" onClick={onDownloadFlagfile}>
Download
<Icon
name="download"
color="core-fleet-blue"
size="small"
/>
</Button>
)}
</p>
</div>
<div className={`${baseClass}__advanced--osqueryd`}>
<InputField
disabled
inputWrapperClass={`${baseClass}__run-osquery-input`}
name="run-osquery"
label={renderLabel(
"plain-osquery",
"osqueryd --flagfile=flagfile.txt --verbose"
)}
type={"text"}
value={"osqueryd --flagfile=flagfile.txt --verbose"}
/>
</div>
</>
)}
2022-02-16 21:21:42 +00:00
</div>
</div>
);
}
2022-02-16 21:21:42 +00:00
return (
<>
{packageType !== "pkg" && (
<Checkbox
name="include-fleet-desktop"
onChange={(value: boolean) => setIncludeFleetDesktop(value)}
value={includeFleetDesktop}
>
<>
Include&nbsp;
<TooltipWrapper
tipContent={
2023-11-07 21:15:49 +00:00
"Include Fleet Desktop if you're adding workstations."
}
>
Fleet Desktop
</TooltipWrapper>
</>
</Checkbox>
)}
2022-02-16 21:21:42 +00:00
<InputField
disabled
inputWrapperClass={`${baseClass}__installer-input ${baseClass}__installer-input-${packageType}`}
2022-02-16 21:21:42 +00:00
name="installer"
label={renderLabel(packageType, renderInstallerString(packageType))}
2022-02-16 21:21:42 +00:00
type={"textarea"}
value={renderInstallerString(packageType)}
2022-02-16 21:21:42 +00:00
/>
<span>Distribute your package to add hosts to Fleet.</span>
2022-02-16 21:21:42 +00:00
</>
);
};
return (
<div className={baseClass}>
<TabsWrapper>
2023-05-30 23:17:14 +00:00
<Tabs
onSelect={(index) => setSelectedTabIndex(index)}
selectedIndex={selectedTabIndex}
>
2022-02-16 21:21:42 +00:00
<TabList>
{platformSubNav.map((navItem) => {
// Bolding text when the tab is active causes a layout shift
// so we add a hidden pseudo element with the same text string
return (
<Tab key={navItem.name} data-text={navItem.name}>
{navItem.name}
</Tab>
);
})}
</TabList>
{platformSubNav.map((navItem) => {
// Bolding text when the tab is active causes a layout shift
// so we add a hidden pseudo element with the same text string
return (
<TabPanel className={`${baseClass}__info`} key={navItem.type}>
{renderTab(navItem.type)}
</TabPanel>
);
})}
</Tabs>
</TabsWrapper>
<div className="modal-cta-wrap">
<Button onClick={onCancel} variant="brand">
2022-02-16 21:21:42 +00:00
Done
</Button>
</div>
</div>
);
};
export default PlatformWrapper;