2023-09-05 19:14:09 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
2024-10-23 18:51:02 +00:00
|
|
|
"bytes"
|
|
|
|
|
"context"
|
2023-09-05 19:14:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2024-10-23 18:51:02 +00:00
|
|
|
"mime/multipart"
|
2023-09-05 19:14:09 +00:00
|
|
|
"net/http"
|
2024-10-23 18:51:02 +00:00
|
|
|
"os"
|
2024-01-08 21:28:45 +00:00
|
|
|
"strings"
|
2024-07-11 21:03:36 +00:00
|
|
|
"time"
|
2023-09-05 19:14:09 +00:00
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-11 21:03:36 +00:00
|
|
|
const pollWaitTime = 5 * time.Second
|
|
|
|
|
|
2024-03-05 14:53:17 +00:00
|
|
|
func (c *Client) RunHostScriptSync(hostID uint, scriptContents []byte, scriptName string, teamID uint) (*fleet.HostScriptResult, error) {
|
2024-07-11 21:03:36 +00:00
|
|
|
verb, path := "POST", "/api/latest/fleet/scripts/run"
|
|
|
|
|
res, err := c.runHostScript(verb, path, hostID, scriptContents, scriptName, teamID, http.StatusAccepted)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if res.ExecutionID == "" {
|
|
|
|
|
return nil, errors.New("missing execution id in response")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return c.pollForResult(res.ExecutionID)
|
2024-05-07 15:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) RunHostScriptAsync(hostID uint, scriptContents []byte, scriptName string, teamID uint) (*fleet.HostScriptResult, error) {
|
|
|
|
|
verb, path := "POST", "/api/latest/fleet/scripts/run"
|
|
|
|
|
return c.runHostScript(verb, path, hostID, scriptContents, scriptName, teamID, http.StatusAccepted)
|
|
|
|
|
}
|
2023-09-05 19:14:09 +00:00
|
|
|
|
2024-05-07 15:10:22 +00:00
|
|
|
func (c *Client) runHostScript(verb, path string, hostID uint, scriptContents []byte, scriptName string, teamID uint, successStatusCode int) (*fleet.HostScriptResult, error) {
|
2023-09-05 19:14:09 +00:00
|
|
|
req := fleet.HostScriptRequestPayload{
|
2024-03-05 14:53:17 +00:00
|
|
|
HostID: hostID,
|
|
|
|
|
ScriptName: scriptName,
|
|
|
|
|
TeamID: teamID,
|
|
|
|
|
}
|
|
|
|
|
if len(scriptContents) > 0 {
|
|
|
|
|
req.ScriptContents = string(scriptContents)
|
2023-09-05 19:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result fleet.HostScriptResult
|
|
|
|
|
|
|
|
|
|
res, err := c.AuthenticatedDo(verb, path, "", &req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
|
|
switch res.StatusCode {
|
2024-05-07 15:10:22 +00:00
|
|
|
case successStatusCode:
|
2023-09-05 19:14:09 +00:00
|
|
|
b, err := io.ReadAll(res.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("reading %s %s response: %w", verb, path, err)
|
|
|
|
|
}
|
|
|
|
|
if err := json.Unmarshal(b, &result); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("decoding %s %s response: %w, body: %s", verb, path, err, b)
|
|
|
|
|
}
|
|
|
|
|
case http.StatusForbidden:
|
2024-01-08 21:28:45 +00:00
|
|
|
errMsg, err := extractServerErrMsg(verb, path, res)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(errMsg, fleet.RunScriptScriptsDisabledGloballyErrMsg) {
|
|
|
|
|
return nil, errors.New(fleet.RunScriptScriptsDisabledGloballyErrMsg)
|
|
|
|
|
}
|
2023-09-05 19:14:09 +00:00
|
|
|
return nil, errors.New(fleet.RunScriptForbiddenErrMsg)
|
2024-06-14 16:07:08 +00:00
|
|
|
// It's possible we get a GatewayTimeout error message from nginx or another
|
|
|
|
|
// proxy server, so we want to return a more helpful error message in that
|
|
|
|
|
// case.
|
|
|
|
|
case http.StatusGatewayTimeout:
|
|
|
|
|
return nil, errors.New(fleet.RunScriptGatewayTimeoutErrMsg)
|
2024-03-05 14:53:17 +00:00
|
|
|
case http.StatusPaymentRequired:
|
|
|
|
|
if teamID > 0 {
|
|
|
|
|
return nil, errors.New("Team id parameter requires Fleet Premium license.")
|
|
|
|
|
}
|
|
|
|
|
fallthrough // if no team id, fall through to default error message
|
2023-09-05 19:14:09 +00:00
|
|
|
default:
|
|
|
|
|
msg, err := extractServerErrMsg(verb, path, res)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if msg == "" {
|
|
|
|
|
msg = fmt.Sprintf("decoding %d response is missing expected message.", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
|
}
|
2023-10-10 22:00:45 +00:00
|
|
|
|
2024-07-11 21:03:36 +00:00
|
|
|
func (c *Client) pollForResult(id string) (*fleet.HostScriptResult, error) {
|
|
|
|
|
verb, path := "GET", fmt.Sprintf("/api/latest/fleet/scripts/results/%s", id)
|
|
|
|
|
var result *fleet.HostScriptResult
|
|
|
|
|
for {
|
|
|
|
|
res, err := c.AuthenticatedDo(verb, path, "", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("polling for result: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusNotFound {
|
|
|
|
|
|
|
|
|
|
msg, err := extractServerErrMsg(verb, path, res)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("extracting error message: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if msg == "" {
|
|
|
|
|
msg = fmt.Sprintf("decoding %d response is missing expected message.", res.StatusCode)
|
|
|
|
|
}
|
|
|
|
|
return nil, errors.New(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("decoding response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result.ExitCode != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
time.Sleep(pollWaitTime)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-10 22:00:45 +00:00
|
|
|
// ApplyNoTeamScripts sends the list of scripts to be applied for the hosts in
|
|
|
|
|
// no team.
|
2024-10-04 01:03:40 +00:00
|
|
|
func (c *Client) ApplyNoTeamScripts(scripts []fleet.ScriptPayload, opts fleet.ApplySpecOptions) ([]fleet.ScriptResponse, error) {
|
2023-10-10 22:00:45 +00:00
|
|
|
verb, path := "POST", "/api/latest/fleet/scripts/batch"
|
2024-10-04 01:03:40 +00:00
|
|
|
var resp batchSetScriptsResponse
|
|
|
|
|
err := c.authenticatedRequestWithQuery(map[string]interface{}{"scripts": scripts}, verb, path, &resp, opts.RawQuery())
|
|
|
|
|
|
|
|
|
|
return resp.Scripts, err
|
2023-10-10 22:00:45 +00:00
|
|
|
}
|
2024-10-23 18:51:02 +00:00
|
|
|
|
|
|
|
|
func (c *Client) validateMacOSSetupScript(fileName string) ([]byte, error) {
|
|
|
|
|
if err := c.CheckAppleMDMEnabled(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, err := os.ReadFile(fileName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return b, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) deleteMacOSSetupScript(teamID *uint) error {
|
|
|
|
|
var query string
|
|
|
|
|
if teamID != nil {
|
|
|
|
|
query = fmt.Sprintf("team_id=%d", *teamID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verb, path := "DELETE", "/api/latest/fleet/setup_experience/script"
|
|
|
|
|
var delResp deleteSetupExperienceScriptResponse
|
|
|
|
|
return c.authenticatedRequestWithQuery(nil, verb, path, &delResp, query)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *Client) uploadMacOSSetupScript(filename string, data []byte, teamID *uint) error {
|
|
|
|
|
// there is no "replace setup experience script" endpoint, and none was
|
|
|
|
|
// planned, so to avoid delaying the feature I'm doing DELETE then SET, but
|
|
|
|
|
// that's not ideal (will always re-create the script when apply/gitops is
|
|
|
|
|
// run with the same yaml). Note though that we also redo software installers
|
|
|
|
|
// downloads on each run, so the churn of this one is minor in comparison.
|
|
|
|
|
if err := c.deleteMacOSSetupScript(teamID); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
verb, path := "POST", "/api/latest/fleet/setup_experience/script"
|
|
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := multipart.NewWriter(&b)
|
|
|
|
|
|
|
|
|
|
fw, err := w.CreateFormFile("script", filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if _, err := io.Copy(fw, bytes.NewBuffer(data)); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add the team_id field
|
|
|
|
|
if teamID != nil {
|
|
|
|
|
if err := w.WriteField("team_id", fmt.Sprint(*teamID)); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
|
|
response, err := c.doContextWithBodyAndHeaders(context.Background(), verb, path, "",
|
|
|
|
|
b.Bytes(),
|
|
|
|
|
map[string]string{
|
|
|
|
|
"Content-Type": w.FormDataContentType(),
|
|
|
|
|
"Accept": "application/json",
|
|
|
|
|
"Authorization": fmt.Sprintf("Bearer %s", c.token),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("do multipart request: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
|
|
var resp setSetupExperienceScriptResponse
|
|
|
|
|
if err := c.parseResponse(verb, path, response, &resp); err != nil {
|
|
|
|
|
return fmt.Errorf("parse response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-02-11 17:46:53 +00:00
|
|
|
|
|
|
|
|
// ListScripts retrieves the saved scripts.
|
|
|
|
|
func (c *Client) ListScripts(query string) ([]*fleet.Script, error) {
|
|
|
|
|
verb, path := "GET", "/api/latest/fleet/scripts"
|
|
|
|
|
var responseBody listScriptsResponse
|
|
|
|
|
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return responseBody.Scripts, nil
|
|
|
|
|
}
|
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 20:25:44 +00:00
|
|
|
|
|
|
|
|
// Get the contents of a saved script.
|
|
|
|
|
func (c *Client) GetScriptContents(scriptID uint) ([]byte, error) {
|
|
|
|
|
verb, path := "GET", "/api/latest/fleet/scripts/"+fmt.Sprint(scriptID)
|
|
|
|
|
response, err := c.AuthenticatedDo(verb, path, "alt=media", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("%s %s: %w", verb, path, err)
|
|
|
|
|
}
|
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
err = c.parseResponse(verb, path, response, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("parsing script response: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if response.StatusCode != http.StatusNoContent {
|
|
|
|
|
b, err := io.ReadAll(response.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("reading response body: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return b, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
Fix macos_setup not always being exported correctly by generate-gitops (#30504)
for #30502
# Details
This PR fixes an issue where `fleetctl generate-gitops` would not always
add a `macos_setup` setting to a .yml file even if the team had a setup
experience configured. This was due to relying on the `MacOSSetup`
config returned by app/team config APIs to have this data populated,
which turned out to be an incorrect assumption. Instead, we now utilize
various APIs to check for the presence of setup software, scripts,
bootstrap packages and profiles.
Note that for now, `generate-gitops` will only output a `TODO` line if
setup experience is detected;
https://github.com/fleetdm/fleet/issues/30210 is open to flesh this out.
In the meantime `fleetctl gitops` will fail if this TODO is inserted, so
that the user must go and fix it manually.
# Checklist for submitter
If some of the following don't apply, delete the relevant line.
<!-- Note that API documentation changes are now addressed by the
product design team. -->
- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
- [X] Added/updated automated tests
- [X] Manual QA for all new/changed functionality
# Testing
I set up MDM on a local instance and tried the following both on No Team
and a regular team:
* Turned "End user authentication on", verified that `fleetctl
generate-gitops` output a `macos_setup` setting for the team. Turned it
back off and verified that `macos_setup` was no longer exported by
`fleetctl generate-gitops`.
* Did the same for bootstrap package.
* Did the same for install software, and additionally verified that
having software available but _not_ selected did not cause `macos_setup`
to be exported. Same for teams with no software available at all.
* Did the same for setup assistant.
I also tested that changes to No Team didn't affect the output when
exporting a regular team.
---------
Co-authored-by: Lucas Rodriguez <lucas@fleetdm.com>
2025-07-02 12:07:58 +00:00
|
|
|
|
|
|
|
|
// GetSetupExperienceScript retrieves the setup script for the given team, if any.
|
|
|
|
|
func (c *Client) GetSetupExperienceScript(teamID uint) (*fleet.Script, error) {
|
|
|
|
|
verb, path := "GET", "/api/latest/fleet/setup_experience/script"
|
|
|
|
|
var query string
|
|
|
|
|
if teamID != 0 {
|
|
|
|
|
query = fmt.Sprintf("team_id=%d", teamID)
|
|
|
|
|
}
|
|
|
|
|
var responseBody getSetupExperienceScriptResponse
|
|
|
|
|
err := c.authenticatedRequestWithQuery(nil, verb, path, &responseBody, query)
|
|
|
|
|
if err != nil {
|
|
|
|
|
var notFoundErr notFoundErr
|
|
|
|
|
if errors.As(err, ¬FoundErr) {
|
|
|
|
|
// If the script is not found, we return nil instead of an error.
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return responseBody.Script, nil
|
|
|
|
|
}
|