Skip to content

Commit

Permalink
[Flare] Update a couple things in how flares are shipped (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
directionless authored Oct 30, 2023
1 parent e0e1d1a commit 3380fa0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
66 changes: 35 additions & 31 deletions cmd/launcher/flare.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/pkg/agent/flags"
"github.com/kolide/launcher/pkg/agent/knapsack"
Expand All @@ -28,32 +30,16 @@ func runFlare(args []string) error {
launcher.SetDefaultPaths()

var (
flagset = flag.NewFlagSet("flare", flag.ExitOnError)
flSave = flagset.String(
"save",
"local",
"local | upload",
)
flOutputDir = flagset.String(
"output_dir",
".",
"path to directory to save flare output",
)
flUploadRequestURL = flagset.String(
"upload_request_url",
"",
"URL to request a signed upload URL",
)
flagset = flag.NewFlagSet("flare", flag.ExitOnError)
flSave = flagset.String("save", "upload", "local | upload")
flOutputDir = flagset.String("output_dir", ".", "path to directory to save flare output")
flUploadRequestURL = flagset.String("upload_request_url", "https://api.kolide.com/api/agent/flare", "URL to request a signed upload URL")
)

if err := ff.Parse(flagset, args); err != nil {
return fmt.Errorf("parsing flags: %w", err)
}

if *flSave != "local" && *flSave != "upload" {
return fmt.Errorf("invalid save option: %s, expected local or upload", *flSave)
}

// were passing an empty array here just to get the default options
opts, err := launcher.ParseOptions("flareupload", make([]string, 0))
if err != nil {
Expand All @@ -67,23 +53,41 @@ func runFlare(args []string) error {
k := knapsack.New(nil, flagController, nil)
ctx := context.Background()

if *flSave == "upload" {
type flareDestinationTyp interface {
io.WriteCloser
Name() string
}
var flareDest flareDestinationTyp
var successMessage string

switch *flSave {
case "upload":
shipper, err := shipper.New(k, shipper.WithNote(strings.Join(flagset.Args(), " ")), shipper.WithUploadRequestURL(*flUploadRequestURL))
if err != nil {
return err
}
return checkups.RunFlare(ctx, k, shipper, checkups.StandaloneEnviroment)
}
flareDest = shipper
successMessage = "Flare uploaded successfully"
case "local":
reportName := fmt.Sprintf("kolide_agent_flare_report_%s.zip", ulid.New())
reportPath := filepath.Join(*flOutputDir, reportName)

// saving flare locally
reportName := fmt.Sprintf("kolide_agent_flare_report_%s", ulid.New())
reportPath := fmt.Sprintf("%s.zip", filepath.Join(*flOutputDir, reportName))
flareFile, err := os.Create(reportPath)
if err != nil {
return fmt.Errorf("creating flare file (%s): %w", reportPath, err)
}
defer flareFile.Close()
flareDest = flareFile
successMessage = "Flare saved locally"
default:
return fmt.Errorf(`invalid save option: %s, expected "local" or "upload"`, *flSave)

flareFile, err := os.Create(reportPath)
if err != nil {
return fmt.Errorf("creating flare file (%s): %w", reportPath, err)
}
defer flareFile.Close()

return checkups.RunFlare(ctx, k, flareFile, checkups.StandaloneEnviroment)
if err := checkups.RunFlare(ctx, k, flareDest, checkups.StandaloneEnviroment); err != nil {
return err
}

level.Info(logger).Log("msg", successMessage, "file", flareDest.Name())
return nil
}
10 changes: 9 additions & 1 deletion pkg/debug/shipper/shipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type shipper struct {
writer io.WriteCloser
knapsack types.Knapsack

uploadName string
uploadRequestURL string
uploadRequest *http.Request
uploadRequestStarted bool
Expand Down Expand Up @@ -91,6 +92,10 @@ func New(knapsack types.Knapsack, opts ...shipperOption) (*shipper, error) {
return s, nil
}

func (s *shipper) Name() string {
return s.uploadName
}

func (s *shipper) Write(p []byte) (n int, err error) {
if s.uploadRequestStarted {
return s.writer.Write(p)
Expand Down Expand Up @@ -153,6 +158,7 @@ func (s *shipper) signedUrl() (string, error) {
}

signedUrlRequest.Header.Set(control.HeaderApiVersion, control.ApiVersion)
signedUrlRequest.Header.Set("Content-Type", "application/json")

signHttpRequest(signedUrlRequest, body)

Expand All @@ -163,7 +169,8 @@ func (s *shipper) signedUrl() (string, error) {
defer signedUrlResponse.Body.Close()

responseData := struct {
URL string `json:"URL"`
URL string `json:"URL"`
Name string `json:"name"`
}{}

if err := json.NewDecoder(signedUrlResponse.Body).Decode(&responseData); err != nil {
Expand All @@ -174,6 +181,7 @@ func (s *shipper) signedUrl() (string, error) {
return "", fmt.Errorf("got %s status in signed url response", signedUrlResponse.Status)
}

s.uploadName = responseData.Name
return responseData.URL, nil
}

Expand Down

0 comments on commit 3380fa0

Please sign in to comment.