Apply starter library during for fleetctl preview server (#30519)

This commit is contained in:
Luke Heath
2025-07-16 08:12:32 -06:00
committed by GitHub
parent d740c50db5
commit e52a8a2ecf
5 changed files with 253 additions and 84 deletions
+15 -51
View File
@@ -24,10 +24,10 @@ import (
"github.com/fleetdm/fleet/v4/orbit/pkg/update"
"github.com/fleetdm/fleet/v4/pkg/fleethttp"
"github.com/fleetdm/fleet/v4/pkg/open"
"github.com/fleetdm/fleet/v4/pkg/spec"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/service"
kitlog "github.com/go-kit/log"
"github.com/google/go-github/v37/github"
"github.com/mitchellh/go-ps"
"github.com/urfave/cli/v2"
@@ -36,7 +36,6 @@ import (
type dockerComposeVersion int
const (
standardQueryLibraryUrl = "https://raw.githubusercontent.com/fleetdm/fleet/main/docs/01-Using-Fleet/standard-query-library/standard-query-library.yml"
licenseKeyFlagName = "license-key"
tagFlagName = "tag"
previewConfigFlagName = "preview-config"
@@ -45,7 +44,7 @@ const (
osquerydChannel = "osqueryd-channel"
updateURL = "update-url"
updateRootKeys = "update-roots"
stdQueryLibFilePath = "std-query-lib-file-path"
starterLibraryFilePath = "starter-library-file-path"
previewConfigPathFlagName = "preview-config-path"
disableOpenBrowser = "disable-open-browser"
@@ -145,8 +144,8 @@ Use the stop and reset subcommands to manage the server and dependencies once st
Value: "",
},
&cli.StringFlag{
Name: stdQueryLibFilePath,
Usage: "Use custom standard query library yml file (used for development/testing)",
Name: starterLibraryFilePath,
Usage: "Use custom starter library yml file (used for development/testing)",
Value: "",
},
&cli.StringFlag{
@@ -370,37 +369,17 @@ Use the stop and reset subcommands to manage the server and dependencies once st
}
client.SetToken(token)
fmt.Println("Loading standard query library...")
var buf []byte
if fp := c.String(stdQueryLibFilePath); fp != "" {
var err error
buf, err = os.ReadFile(fp)
if err != nil {
return fmt.Errorf("failed to read standard query library file %q: %w", fp, err)
}
} else {
var err error
buf, err = downloadStandardQueryLibrary()
if err != nil {
return fmt.Errorf("failed to download standard query library: %w", err)
}
}
specs, err := spec.GroupFromBytes(buf)
if err != nil {
return err
}
logf := func(format string, a ...interface{}) {
fmt.Fprintf(c.App.Writer, format, a...)
}
// this only applies standard queries, the base directory is not used,
// so pass in the current working directory.
teamsSoftwareInstallers := make(map[string][]fleet.SoftwarePackageResponse)
teamsScripts := make(map[string][]fleet.ScriptResponse)
teamsVPPApps := make(map[string][]fleet.VPPAppResponse)
_, _, _, _, err = client.ApplyGroup(c.Context, false, specs, ".", logf, nil, fleet.ApplyClientSpecOptions{}, teamsSoftwareInstallers, teamsVPPApps, teamsScripts)
if err != nil {
return err
fmt.Println("Loading starter library...")
if err := service.ApplyStarterLibrary(
c.Context,
address,
token,
kitlog.NewLogfmtLogger(os.Stderr),
fleethttp.NewClient,
service.NewClient,
nil, // No mock ApplyGroup for production code
); err != nil {
return fmt.Errorf("failed to apply starter library: %w", err)
}
// disable analytics collection and enable software inventory for preview
@@ -574,21 +553,6 @@ func downloadFromFleetRepo(
return nil
}
func downloadStandardQueryLibrary() ([]byte, error) {
resp, err := http.Get(standardQueryLibraryUrl)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d", resp.StatusCode)
}
buf, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response body: %w", err)
}
return buf, nil
}
func waitStartup() error {
retryStrategy := backoff.NewExponentialBackOff()
retryStrategy.MaxInterval = 1 * time.Second
@@ -1,10 +1,8 @@
package preview
import (
"bytes"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
@@ -30,10 +28,8 @@ func TestIntegrationsPreview(t *testing.T) {
require.Equal(t, "", fleetctl.RunAppForTest(t, []string{"preview", "--config", configPath, "stop"}))
})
var output *bytes.Buffer
require.NoError(t, nettest.RunWithNetRetry(t, func() error {
var err error
output, err = fleetctl.RunAppNoChecks([]string{
_, err := fleetctl.RunAppNoChecks([]string{
"preview",
"--config", configPath,
"--preview-config-path", filepath.Join(gitRootPath(t), "tools", "osquery", "in-a-box"),
@@ -43,17 +39,12 @@ func TestIntegrationsPreview(t *testing.T) {
return err
}))
queriesRe := regexp.MustCompile(`applied ([0-9]+) queries`)
policiesRe := regexp.MustCompile(`applied ([0-9]+) policies`)
require.True(t, queriesRe.MatchString(output.String()))
require.True(t, policiesRe.MatchString(output.String()))
// run some sanity checks on the preview environment
// standard queries must have been loaded
// starter library queries must have been loaded
queries := fleetctl.RunAppForTest(t, []string{"get", "queries", "--config", configPath, "--json"})
n := strings.Count(queries, `"kind":"query"`)
require.Greater(t, n, 10)
require.Greater(t, n, 0)
// app configuration must disable analytics
appConf := fleetctl.RunAppForTest(t, []string{"get", "config", "--include-server-config", "--config", configPath, "--yaml"})