Skip to content

Hide overrides key if empty, display comment for flags if empty #7986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState, useEffect } from "react";

// @ts-ignore
import constructErrorString from "utilities/yaml";
import { constructErrorString, agentOptionsToYaml } from "utilities/yaml";
import yaml from "js-yaml";
import paths from "router/paths";

Expand All @@ -26,7 +26,7 @@ const Agents = ({
const { ADMIN_TEAMS } = paths;

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

const { agentOptions } = formData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useContext, useState } from "react";
import { useQuery } from "react-query";
import { useErrorHandler } from "react-error-boundary";
import yaml from "js-yaml";
import { agentOptionsToYaml } from "utilities/yaml";

import { NotificationContext } from "context/notification";
import { IApiError } from "interfaces/errors";
Expand Down Expand Up @@ -45,7 +45,7 @@ const AgentOptionsPage = ({

if (selected) {
setFormData({
osquery_options: yaml.dump(selected.agent_options),
osquery_options: agentOptionsToYaml(selected.agent_options),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have the same || {} (presumably inside the parens) here too?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if yaml.dump gracefully handles falsey values, I'd be inclined to just leave it like this. Otherwise yes, I vote to add the safety || {}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up moving the check inside agentOptionsToYaml instead.

});
setTeamName(selected.name);
} else {
Expand Down
33 changes: 0 additions & 33 deletions frontend/utilities/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,38 +199,6 @@ export const formatConfigDataForServer = (config: any): any => {
};
};

// TODO: Finalize interface for config - see frontend\interfaces\config.ts
export const frontendFormattedConfig = (config: IConfig) => {
const {
org_info: orgInfo,
server_settings: serverSettings,
smtp_settings: smtpSettings,
sso_settings: ssoSettings,
host_expiry_settings: hostExpirySettings,
webhook_settings: { host_status_webhook: webhookSettings }, // unnested to frontend
update_interval: updateInterval,
license,
logging,
} = config;

if (config.agent_options) {
config.agent_options = yaml.dump(config.agent_options);
}

return {
...orgInfo,
...serverSettings,
...smtpSettings,
...ssoSettings,
...hostExpirySettings,
...webhookSettings,
...updateInterval,
...license,
...logging,
agent_options: config.agent_options,
};
};

export const formatFloatAsPercentage = (float: number): string => {
const formatter = Intl.NumberFormat("en-US", {
maximumSignificantDigits: 2,
Expand Down Expand Up @@ -844,7 +812,6 @@ export default {
secondsToDhms,
labelSlug,
setupData,
frontendFormattedConfig,
syntaxHighlight,
getValidatedTeamId,
normalizeEmptyValues,
Expand Down
31 changes: 30 additions & 1 deletion frontend/utilities/yaml/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
import yaml from "js-yaml";

interface IYAMLError {
name: string;
reason: string;
line: string;
}

const constructErrorString = (yamlError: IYAMLError) => {
export const constructErrorString = (yamlError: IYAMLError) => {
return `${yamlError.name}: ${yamlError.reason} at line ${yamlError.line}`;
};

export const agentOptionsToYaml = (agentOpts: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any chance of agentOpts being undefined or null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this was done (or meant to be done, I guess) at the callsite with the || {} but you're right, maybe it should be done here inside this function instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we dont have the best typing in some parts of the app atm. I'd look at where this code is used to determine if that's a possiblity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the check inside the func. Regarding the type, agent_options is not strictly defined even on the backend, it can have arbitrary keys as those are passed to the osquery agent ultimately, so I left it as any.

agentOpts ||= {};

// hide the "overrides" key if it is empty
if (!agentOpts.overrides || Object.keys(agentOpts.overrides).length === 0) {
delete agentOpts.overrides;
}

// add a comment besides the "command_line_flags" if it is empty
let addFlagsComment = false;
if (
!agentOpts.command_line_flags ||
Object.keys(agentOpts.command_line_flags).length === 0
) {
// delete it so it does not render, and will add it explicitly after (along with the comment)
delete agentOpts.command_line_flags;
addFlagsComment = true;
}

let yamlString = yaml.dump(agentOpts);
if (addFlagsComment) {
yamlString += "command_line_flags: {} # requires Orbit\n";
}

return yamlString;
};

export default constructErrorString;