Skip to content

Commit f5b6523

Browse files
authored
Hide overrides key if empty, display comment for flags if empty (#7986)
1 parent 812d3c8 commit f5b6523

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

frontend/pages/admin/AppSettingsPage/cards/Agents/Agents.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState, useEffect } from "react";
22

33
// @ts-ignore
4-
import constructErrorString from "utilities/yaml";
4+
import { constructErrorString, agentOptionsToYaml } from "utilities/yaml";
55
import yaml from "js-yaml";
66
import paths from "router/paths";
77

@@ -26,7 +26,7 @@ const Agents = ({
2626
const { ADMIN_TEAMS } = paths;
2727

2828
const [formData, setFormData] = useState<any>({
29-
agentOptions: yaml.dump(appConfig.agent_options) || {},
29+
agentOptions: agentOptionsToYaml(appConfig.agent_options),
3030
});
3131

3232
const { agentOptions } = formData;

frontend/pages/admin/TeamManagementPage/TeamDetailsWrapper/AgentOptionsPage/AgentOptionsPage.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useContext, useState } from "react";
22
import { useQuery } from "react-query";
33
import { useErrorHandler } from "react-error-boundary";
4-
import yaml from "js-yaml";
4+
import { agentOptionsToYaml } from "utilities/yaml";
55

66
import { NotificationContext } from "context/notification";
77
import { IApiError } from "interfaces/errors";
@@ -45,7 +45,7 @@ const AgentOptionsPage = ({
4545

4646
if (selected) {
4747
setFormData({
48-
osquery_options: yaml.dump(selected.agent_options),
48+
osquery_options: agentOptionsToYaml(selected.agent_options),
4949
});
5050
setTeamName(selected.name);
5151
} else {

frontend/utilities/helpers.ts

-33
Original file line numberDiff line numberDiff line change
@@ -199,38 +199,6 @@ export const formatConfigDataForServer = (config: any): any => {
199199
};
200200
};
201201

202-
// TODO: Finalize interface for config - see frontend\interfaces\config.ts
203-
export const frontendFormattedConfig = (config: IConfig) => {
204-
const {
205-
org_info: orgInfo,
206-
server_settings: serverSettings,
207-
smtp_settings: smtpSettings,
208-
sso_settings: ssoSettings,
209-
host_expiry_settings: hostExpirySettings,
210-
webhook_settings: { host_status_webhook: webhookSettings }, // unnested to frontend
211-
update_interval: updateInterval,
212-
license,
213-
logging,
214-
} = config;
215-
216-
if (config.agent_options) {
217-
config.agent_options = yaml.dump(config.agent_options);
218-
}
219-
220-
return {
221-
...orgInfo,
222-
...serverSettings,
223-
...smtpSettings,
224-
...ssoSettings,
225-
...hostExpirySettings,
226-
...webhookSettings,
227-
...updateInterval,
228-
...license,
229-
...logging,
230-
agent_options: config.agent_options,
231-
};
232-
};
233-
234202
export const formatFloatAsPercentage = (float: number): string => {
235203
const formatter = Intl.NumberFormat("en-US", {
236204
maximumSignificantDigits: 2,
@@ -844,7 +812,6 @@ export default {
844812
secondsToDhms,
845813
labelSlug,
846814
setupData,
847-
frontendFormattedConfig,
848815
syntaxHighlight,
849816
getValidatedTeamId,
850817
normalizeEmptyValues,

frontend/utilities/yaml/index.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
1+
import yaml from "js-yaml";
2+
13
interface IYAMLError {
24
name: string;
35
reason: string;
46
line: string;
57
}
68

7-
const constructErrorString = (yamlError: IYAMLError) => {
9+
export const constructErrorString = (yamlError: IYAMLError) => {
810
return `${yamlError.name}: ${yamlError.reason} at line ${yamlError.line}`;
911
};
1012

13+
export const agentOptionsToYaml = (agentOpts: any) => {
14+
agentOpts ||= {};
15+
16+
// hide the "overrides" key if it is empty
17+
if (!agentOpts.overrides || Object.keys(agentOpts.overrides).length === 0) {
18+
delete agentOpts.overrides;
19+
}
20+
21+
// add a comment besides the "command_line_flags" if it is empty
22+
let addFlagsComment = false;
23+
if (
24+
!agentOpts.command_line_flags ||
25+
Object.keys(agentOpts.command_line_flags).length === 0
26+
) {
27+
// delete it so it does not render, and will add it explicitly after (along with the comment)
28+
delete agentOpts.command_line_flags;
29+
addFlagsComment = true;
30+
}
31+
32+
let yamlString = yaml.dump(agentOpts);
33+
if (addFlagsComment) {
34+
yamlString += "command_line_flags: {} # requires Orbit\n";
35+
}
36+
37+
return yamlString;
38+
};
39+
1140
export default constructErrorString;

0 commit comments

Comments
 (0)