fleet/cmd/fleetctl
Scott Gress d716265641
Add "generate-gitops" command (#28555)
For #27476

# Checklist for submitter

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/Committing-Changes.md#changes-files)
for more information.
- [X] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)

# Details

This PR adds a new command `generate-gitops` to the `fleetctl` tool. The
purpose of this command is to output GitOps-ready files that can then be
used with `fleetctl-gitops`.

The general usage of the command is:

```
fleectl generate-gitops --dir /path/to/dir/to/add/files/to
```

By default, the outputted files will not contain sensitive data, but
will instead add comments where the data needs to be replaced by a user.
In cases where sensitive data is redacted, the tool outputs warnings to
the user indicating which keys need to be updated.

The tool uses existing APIs to gather data for use in generating
configuration files. In some cases new API client methods needed to be
added to support the tool:

* ListConfigurationProfiles
* GetProfileContents
* GetScriptContents
* GetSoftwareTitleByID

Additionally, the response for the /api/latest/fleet/software/batch
endpoint was updated slightly to return `HashSHA256` for the software
installers. This allows policies that automatically install software to
refer to that software by hash.

Other options that we may or may not choose to document at this time:

* `--insecure`: outputs sensitive data in plaintext instead of leaving
comments
* `--print`: prints the output to stdout instead of writing files
* `--key`: outputs the value at a keypath to stdout, e.g. `--key
agent_options.config`
* `--team`: only generates config for the specified team name
* `--force`: overwrites files in the given directory (defaults to false,
which errors if the dir is not empty)

# Technical notes

The command is implemented using a `GenerateGitopsCommand` type which
holds some state (like a list of software and scripts encountered) as
well as a Fleet client instance (which may be a mock instance for tests)
and the CLI context (containing things like flags and output writers).
The actual "action" of the CLI command calls the `Run()` method of the
`GenerateGitopsCommand` var, which delegates most of the work to other
methods like `generateOrgSettings()`, `generateControls()`, etc.

Wherever possible, the subroutines use reflection to translate Go struct
fields into JSON property names. This guarantees that the correct keys
are written to config files, and protects against the unlikely event of
keys changing.

When sensitive data is encountered, the subroutines call `AddComment()`
to get a new token to add to the config files. These tokens are replaced
with comments like `# TODO - Add your enrollment secrets here` in the
final output.

# Known issues / TODOs:

* The `macos_setup` configuration is not output by this tool yet. More
planning is required for this. In the meantime, if the tool detects that
`macos_setup` is configured on the server, it outputs a key with an
invalid value and prints a warning to the user that they'll need to
configure it themselves.
* `yara_rules` are not output yet. The tool adds a warning that if you
have Yara rules (which you can only upload via GitOps right now) that
you'll have to migrate them manually. Supporting this will require a new
API that we'll have to discuss the authz for, so punting on it for now.
* Fleet maintained apps are not supported by GitOps yet (coming in
https://github.com/fleetdm/fleet/issues/24469). In the meantime, this
tool will output a `fleet_maintained_apps` key and trigger a warning,
and GitOps will fail if that key is present.

---------

Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
Co-authored-by: Noah Talerman <47070608+noahtalerman@users.noreply.github.com>
2025-05-06 15:25:44 -05:00
..
testdata Add "generate-gitops" command (#28555) 2025-05-06 15:25:44 -05:00
api.go add CLI for the new MDM cert flow (#19240) 2024-05-27 11:14:37 -03:00
api_test.go Initial fleetctl api command works with GETS and fields (#17971) 2024-05-07 11:22:05 -05:00
apply.go Add VPP install automation in GitOps (#25400) 2025-01-14 12:52:39 -06:00
apply_test.go IdP vars: add integration test, address last PR comments (#28794) 2025-05-05 11:46:10 -04:00
config.go chore: remove refs to deprecated io/ioutil (#14485) 2023-10-27 15:28:54 -03:00
config_test.go Enable errcheck linter for golangci-lint (#8899) 2022-12-05 16:50:49 -06:00
convert.go chore: remove refs to deprecated io/ioutil (#14485) 2023-10-27 15:28:54 -03:00
convert_test.go Enable staticcheck Go linter. (#23487) 2024-11-05 11:16:24 -06:00
debug.go Updating golangci-lint to 1.61.0 (#22973) 2024-10-18 12:38:26 -05:00
debug_test.go Updating golangci-lint to 1.61.0 (#22973) 2024-10-18 12:38:26 -05:00
delete.go Updating golangci-lint to 1.61.0 (#22973) 2024-10-18 12:38:26 -05:00
delete_test.go Activity feed webhooks backend (#19261) 2024-05-24 11:25:27 -05:00
flags.go Get MDM Command Filters (#20154) 2024-07-09 11:06:06 -06:00
fleetctl.go Add "generate-gitops" command (#28555) 2025-05-06 15:25:44 -05:00
generate.go Minor fixes from analyzing test results (#23260) 2024-10-25 15:57:51 -05:00
generate_gitops.go Add "generate-gitops" command (#28555) 2025-05-06 15:25:44 -05:00
generate_gitops_test.go Add "generate-gitops" command (#28555) 2025-05-06 15:25:44 -05:00
generate_test.go fix fleetctl generate mdm-apple-bm (#19507) 2024-06-04 18:19:09 -03:00
get.go Add wait until fleet is responding (#28241) 2025-04-16 13:11:03 -03:00
get_test.go IdP vars: add integration test, address last PR comments (#28794) 2025-05-05 11:46:10 -04:00
gitops.go Fixed fleetctl gitops issue where creating a new team containing VPP apps caused an error. (#28624) 2025-04-29 16:28:25 -05:00
gitops_enterprise_integration_test.go Added macos_setup.manual_agent_install to global/team config (#28419) 2025-04-24 15:18:01 -05:00
gitops_integration_test.go Adding secret support to profiles via gitops. (#24675) 2024-12-13 15:41:23 -06:00
gitops_test.go IdP vars: add integration test, address last PR comments (#28794) 2025-05-05 11:46:10 -04:00
goquery.go fleetctl now runs saved queries (#15667) 2023-12-15 12:55:39 -06:00
hosts.go fleetctl, API, copy updates around host identifiers (#20220) 2024-07-09 10:25:01 -07:00
hosts_test.go Profiles batch activity (#21604) 2024-08-30 16:00:35 -05:00
kill_process.go Download and launch orbit for the current device when starting preview (#2437) 2021-10-15 18:23:06 -03:00
kill_process_windows.go Fix Windows lint issues and enable linting on Windows (#28704) 2025-05-02 16:11:26 -04:00
login.go Allow opting in users to email verification on login (#24273) 2024-12-05 08:37:10 -06:00
logout.go Use new error handling approach in other packages (#2954) 2021-11-22 09:13:26 -05:00
logout_test.go Simplify fleetctl implementation and improve testing (#3830) 2022-01-24 16:40:51 -03:00
mdm.go Allow software uninstalls, script-based lock/unlock/wipe, while scripts are globally disabled (#24815) 2024-12-30 08:32:48 -06:00
mdm_test.go Allow software uninstalls, script-based lock/unlock/wipe, while scripts are globally disabled (#24815) 2024-12-30 08:32:48 -06:00
package.go Orbit for Windows ARM64 (#27882) 2025-04-11 10:18:28 -04:00
package_test.go Add mTLS support to fleetd (#11319) 2023-04-27 08:44:39 -03:00
preview.go Add VPP install automation in GitOps (#25400) 2025-01-14 12:52:39 -06:00
preview_test.go Validate license key if supplied before running fleetctl preview (#24627) 2024-12-11 07:57:10 -06:00
query.go Better documentation in fleetctl query command for label flag (#24556) 2024-12-09 12:47:52 -06:00
query_output.go add host_display_name to live query rows (#8251) 2022-10-17 15:52:25 +02:00
query_test.go 21855 – Paginate and filter Queries on the server, update platform filtering from compatible to targeted platforms (#24446) 2024-12-11 10:50:28 -08:00
scripts.go Bugfix: Orbit run-script copy update (#20473) 2024-07-15 12:59:32 -06:00
scripts_test.go Allow use of bash as a script interpreter (#25449) 2025-02-04 12:42:40 -05:00
session_test.go 21855 – Paginate and filter Queries on the server, update platform filtering from compatible to targeted platforms (#24446) 2024-12-11 10:50:28 -08:00
setup.go Updating golangci-lint to 1.61.0 (#22973) 2024-10-18 12:38:26 -05:00
testing_utils.go SCIM + host integration (#27880) 2025-04-08 09:35:06 -05:00
trigger.go Add gosimple linter (#23250) 2024-10-29 14:17:51 -05:00
trigger_test.go 🧹 friday cleanup party: substitute deprecated import of go-kit (#19774) 2024-06-17 10:27:31 -03:00
upgrade_packs.go fleetctl now runs saved queries (#15667) 2023-12-15 12:55:39 -06:00
upgrade_packs_test.go Add labels to queries using gitops (#27259) 2025-03-20 15:32:52 -05:00
user.go Allow opting in users to email verification on login (#24273) 2024-12-05 08:37:10 -06:00
users_test.go SCIM integration tests (#27750) 2025-04-04 11:00:46 -05:00
vulnerability_data_stream.go Fix backoff in vuln stream logging (#19148) 2024-05-28 08:13:46 -06:00
vulnerability_data_stream_test.go Improvements to go tests in CI (#21545) 2024-08-26 08:55:53 -05:00