From cf6a8a29c094e4d62c600e398536be4829c48dad Mon Sep 17 00:00:00 2001 From: Lucas Manuel Rodriguez Date: Thu, 11 Jun 2026 11:13:08 -0300 Subject: [PATCH] Fix command line flags in agent settings UI (#47414) Resolves #47412. - [X] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. ## Testing - [X] Added/updated automated tests - [X] QA'd all new/changed functionality manually ## Summary by CodeRabbit * **Bug Fixes** * Agent settings editor now renders empty or null command-line flags visibly (instead of hiding them), preserving their clearing behavior for host-level settings. * **Tests** * Added tests to validate YAML output and formatting for command-line flags, missing keys, and related edge cases. --- changes/47412-render-command-line-flags-as-is | 1 + frontend/utilities/yaml/index.ts | 25 +++++---- frontend/utilities/yaml/yaml.tests.ts | 52 +++++++++++++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 changes/47412-render-command-line-flags-as-is create mode 100644 frontend/utilities/yaml/yaml.tests.ts diff --git a/changes/47412-render-command-line-flags-as-is b/changes/47412-render-command-line-flags-as-is new file mode 100644 index 0000000000..769d8bcb7e --- /dev/null +++ b/changes/47412-render-command-line-flags-as-is @@ -0,0 +1 @@ +* Fixed the agent settings YAML editor (global and fleet-level) hiding `command_line_flags` behind a comment when set to `{}` or `null` — those values now render as-is, since they have special semantics (they clear all local osquery flags on hosts). diff --git a/frontend/utilities/yaml/index.ts b/frontend/utilities/yaml/index.ts index 6582c95702..f5a5e5b307 100644 --- a/frontend/utilities/yaml/index.ts +++ b/frontend/utilities/yaml/index.ts @@ -18,21 +18,20 @@ export const agentOptionsToYaml = (agentOpts: any) => { 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; - } + const flagsComment = "# Requires fleetd agent\n"; + // always show the informational comment above "command_line_flags". When + // the key is present — even set to {} or null — render it as-is, since + // those empty values have special semantics; when absent, suggest it with + // a commented-out placeholder. let yamlString = yaml.dump(agentOpts); - if (addFlagsComment) { - yamlString += - "# Requires Fleet's osquery installer\n# command_line_flags: {}\n"; + if ("command_line_flags" in agentOpts) { + yamlString = yamlString.replace( + /^command_line_flags:/m, + `${flagsComment}command_line_flags:` + ); + } else { + yamlString += `${flagsComment}# command_line_flags: {}\n`; } return yamlString; diff --git a/frontend/utilities/yaml/yaml.tests.ts b/frontend/utilities/yaml/yaml.tests.ts new file mode 100644 index 0000000000..9da3591829 --- /dev/null +++ b/frontend/utilities/yaml/yaml.tests.ts @@ -0,0 +1,52 @@ +import { agentOptionsToYaml } from "utilities/yaml"; + +const FLAGS_COMMENT = "# Requires fleetd agent\n"; + +describe("agentOptionsToYaml", () => { + it("adds a commented-out placeholder when the command_line_flags key is absent", () => { + expect(agentOptionsToYaml({ config: {} })).toContain( + `${FLAGS_COMMENT}# command_line_flags: {}\n` + ); + }); + + it("adds a commented-out placeholder when agent options are unset", () => { + expect(agentOptionsToYaml(null)).toContain( + `${FLAGS_COMMENT}# command_line_flags: {}\n` + ); + }); + + it("renders command_line_flags set to an empty object as-is, with the comment above it", () => { + const result = agentOptionsToYaml({ + config: {}, + command_line_flags: {}, + }); + expect(result).toContain(`${FLAGS_COMMENT}command_line_flags: {}`); + expect(result).not.toContain("# command_line_flags: {}"); + }); + + it("renders command_line_flags set to null as-is, with the comment above it", () => { + const result = agentOptionsToYaml({ + config: {}, + command_line_flags: null, + }); + expect(result).toContain(`${FLAGS_COMMENT}command_line_flags: null`); + expect(result).not.toContain("# command_line_flags: {}"); + }); + + it("renders non-empty command_line_flags with the comment above it", () => { + const result = agentOptionsToYaml({ + config: {}, + command_line_flags: { verbose: true }, + }); + expect(result).toContain( + `${FLAGS_COMMENT}command_line_flags:\n verbose: true` + ); + expect(result).not.toContain("# command_line_flags: {}"); + }); + + it("omits an empty overrides key", () => { + expect(agentOptionsToYaml({ config: {}, overrides: {} })).not.toContain( + "overrides" + ); + }); +});