fleetctl preview downloads standard query library and populates console (#1450)

* fleetctl preview downloads standard query library and populates console
* add change log file, use specGroupFromBytes to parse standard query library file
This commit is contained in:
Benjamin Edwards 2021-07-28 11:05:03 -04:00 committed by GitHub
parent a188d03015
commit 1fc4b7649c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 4 deletions

View file

@ -0,0 +1 @@
* Preview command now downloads the standard query library from "https://raw.githubusercontent.com/fleetdm/fleet/main/docs/1-Using-Fleet/standard-query-library/standard-query-library.yml" and loads it into the console via fleet api

View file

@ -21,8 +21,9 @@ import (
)
const (
downloadUrl = "https://github.com/fleetdm/osquery-in-a-box/archive/master.zip"
licenseKeyFlagName = "license-key"
downloadUrl = "https://github.com/fleetdm/osquery-in-a-box/archive/master.zip"
standardQueryLibraryUrl = "https://raw.githubusercontent.com/fleetdm/fleet/main/docs/1-Using-Fleet/standard-query-library/standard-query-library.yml"
licenseKeyFlagName = "license-key"
)
func previewCommand() *cli.Command {
@ -110,12 +111,12 @@ Use the stop and reset subcommands to manage the server and dependencies once st
password = "admin123#"
)
fleet, err := service.NewClient(address, true, "", "")
fleetClient, err := service.NewClient(address, true, "", "")
if err != nil {
return errors.Wrap(err, "Error creating Fleet API client handler")
}
token, err := fleet.Setup(email, "Admin", password, "Fleet Preview")
token, err := fleetClient.Setup(email, "Admin", password, "Fleet Preview")
if err != nil {
switch errors.Cause(err).(type) {
case service.SetupAlreadyErr:
@ -193,6 +194,23 @@ Use the stop and reset subcommands to manage the server and dependencies once st
return errors.Errorf("Failed to run docker-compose")
}
fmt.Println("Downloading standard query library")
buf, err := downloadStandardQueryLibrary()
if err != nil {
return errors.Wrap(err, "failed to download standard query library")
}
specGroup, err := specGroupFromBytes(buf)
if err != nil {
return errors.Wrap(err, "failed to parse standard query library")
}
fmt.Println("Applying standard query library")
err = client.ApplyQueries(specGroup.Queries)
if err != nil {
return errors.Wrap(err, "failed to apply standard query library")
}
fmt.Println("Preview environment complete. Enjoy using Fleet!")
return nil
@ -237,6 +255,21 @@ func downloadFiles() error {
return nil
}
func downloadStandardQueryLibrary() ([]byte, error) {
resp, err := http.Get(standardQueryLibraryUrl)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("status: %d", resp.StatusCode)
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "read response body")
}
return buf, nil
}
// Adapted from https://stackoverflow.com/a/24792688/491710
func unzip(r *zip.Reader) error {
previewDir := previewDirectory()