-
Notifications
You must be signed in to change notification settings - Fork 558
/
Copy pathAgentOptionsPage.tsx
122 lines (110 loc) · 3.9 KB
/
AgentOptionsPage.tsx
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
import React, { useContext, useState } from "react";
import { useQuery } from "react-query";
import { useErrorHandler } from "react-error-boundary";
import { agentOptionsToYaml } from "utilities/yaml";
import { NotificationContext } from "context/notification";
import { IApiError } from "interfaces/errors";
import { ITeam } from "interfaces/team";
import endpoints from "utilities/endpoints";
import teamsAPI, { ILoadTeamsResponse } from "services/entities/teams";
import osqueryOptionsAPI from "services/entities/osquery_options";
// @ts-ignore
import validateYaml from "components/forms/validators/validate_yaml";
// @ts-ignore
import OsqueryOptionsForm from "components/forms/admin/OsqueryOptionsForm";
import InfoBanner from "components/InfoBanner/InfoBanner";
import ExternalLinkIcon from "../../../../../../assets/images/[email protected]";
const baseClass = "agent-options";
interface IAgentOptionsPageProps {
params: {
team_id: string;
};
}
const AgentOptionsPage = ({
params: { team_id },
}: IAgentOptionsPageProps): JSX.Element => {
const teamIdFromURL = parseInt(team_id, 10);
const { renderFlash } = useContext(NotificationContext);
const [teamName, setTeamName] = useState("");
const [formData, setFormData] = useState<{ osquery_options?: string }>({});
const handlePageError = useErrorHandler();
useQuery<ILoadTeamsResponse, Error, ITeam[]>(
["teams"],
() => teamsAPI.loadAll(),
{
select: (data: ILoadTeamsResponse) => data.teams,
onSuccess: (data) => {
const selected = data.find((team) => team.id === teamIdFromURL);
if (selected) {
setFormData({
osquery_options: agentOptionsToYaml(selected.agent_options),
});
setTeamName(selected.name);
} else {
handlePageError({ status: 404 });
}
},
onError: (error) => handlePageError(error),
}
);
const onSaveOsqueryOptionsFormSubmit = async (updatedForm: {
osquery_options: string;
}) => {
const { TEAMS_AGENT_OPTIONS } = endpoints;
const { error } = validateYaml(updatedForm.osquery_options);
if (error) {
return renderFlash("error", error.reason);
}
osqueryOptionsAPI
.update(updatedForm, TEAMS_AGENT_OPTIONS(teamIdFromURL))
.then(() => {
renderFlash("success", "Successfully saved agent options");
})
.catch((response: { data: IApiError }) => {
console.error(response);
return renderFlash(
"error",
`Could not update ${teamName} team agent options. ${response.data.errors[0].reason}`
);
});
};
return (
<div className={`${baseClass}`}>
<p className={`${baseClass}__page-description`}>
Agent options configure the osquery agent. When you update agent
options, they will be applied the next time a host checks in to Fleet.
<br />
<a
href="https://fleetdm.com/docs/using-fleet/fleet-ui#configuring-agent-options"
target="_blank"
rel="noopener noreferrer"
>
Learn more about agent{" "}
<span className="no-wrap">
options
<img alt="Open external link" src={ExternalLinkIcon} />
</span>
</a>
</p>
<InfoBanner className={`${baseClass}__config-docs`}>
See Fleet documentation for an example file that includes the overrides
option.{" "}
<a
href="https://fleetdm.com/docs/using-fleet/configuration-files#overrides-option"
target="_blank"
rel="noopener noreferrer"
>
Go to Fleet docs
<img alt="Open external link" src={ExternalLinkIcon} />
</a>
</InfoBanner>
<div className={`${baseClass}__form-wrapper`}>
<OsqueryOptionsForm
formData={formData}
handleSubmit={onSaveOsqueryOptionsFormSubmit}
/>
</div>
</div>
);
};
export default AgentOptionsPage;