Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Pickett committed Oct 10, 2023
1 parent d540887 commit faff0ac
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
23 changes: 9 additions & 14 deletions cmd/launcher/flare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/go-kit/kit/log"
"github.com/kolide/kit/env"
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/pkg/agent/flags"
"github.com/kolide/launcher/pkg/agent/knapsack"
"github.com/kolide/launcher/pkg/agent/storage/inmemory"
"github.com/kolide/launcher/pkg/debug/checkups"
"github.com/kolide/launcher/pkg/debug/shipper"
"github.com/kolide/launcher/pkg/launcher"
"github.com/peterbourgon/ff/v3"
)

// sudo /usr/local/kolide-k2/bin/launcher flareupload "note" --debug_upload_request_url="https://example.com"
// runFlare is a command that runs the flare checkup and saves the results locally or uploads them to a server.
func runFlare(args []string) error {
// Flare assumes a launcher installation (at least partially) exists
// Overriding some of the default values allows options to be parsed making this assumption
Expand All @@ -34,10 +33,10 @@ func runFlare(args []string) error {
"local",
"local | upload",
)
flNote = flagset.String(
"note",
"",
"note used in URL upload request",
flOutputDir = flagset.String(
"output_dir",
".",
"path to directory to save flare output",
)
flUploadRequestURL = flagset.String(
"upload_request_url",
Expand All @@ -46,7 +45,7 @@ func runFlare(args []string) error {
)
)

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

Expand All @@ -68,20 +67,16 @@ func runFlare(args []string) error {
ctx := context.Background()

if *flSave == "upload" {
shipper, err := shipper.New(k, shipper.WithNote(*flNote), shipper.WithUploadRequestURL(*flUploadRequestURL))
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)
}

// saving flare locally
var (
dirPath = env.String("KOLIDE_AGENT_FLARE_ZIP_DIR_PATH", "")
)

reportName := fmt.Sprintf("kolide_agent_flare_report_%s", ulid.New())
reportPath := fmt.Sprintf("%s.zip", filepath.Join(dirPath, reportName))
reportPath := fmt.Sprintf("%s.zip", filepath.Join(*flOutputDir, reportName))

flareFile, err := os.Create(reportPath)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func runLauncher(ctx context.Context, cancel func(), opts *launcher.Options) err
actionsQueue.RegisterActor(acceleratecontrolconsumer.AccelerateControlSubsystem, acceleratecontrolconsumer.New(k))

// register flare consumer
actionsQueue.RegisterActor(flareconsumer.FlareSubsystem, flareconsumer.New(k))
actionsQueue.RegisterActor(flareconsumer.FlareSubsystem, flareconsumer.New(logger, k))

// create notification consumer
notificationConsumer, err := notificationconsumer.NewNotifyConsumer(
Expand Down
23 changes: 19 additions & 4 deletions ee/control/consumers/flareconsumer/flareconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@ import (
"io"
"time"

"github.com/go-kit/kit/log"

"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/pkg/agent/types"
"github.com/kolide/launcher/pkg/debug/checkups"
"github.com/kolide/launcher/pkg/debug/shipper"
)

const (
// Identifier for this consumer.
FlareSubsystem = "flare"
FlareSubsystem = "flare"
minFlareInterval = 5 * time.Minute
)

type FlareConsumer struct {
lastFlareTime time.Time
flarer flarer
knapsack types.Knapsack
logger log.Logger
// newFlareStream is assigned to a field so it can be mocked in tests
newFlareStream func(note, uploadRequestURL string) (io.WriteCloser, error)
}
Expand All @@ -36,7 +41,7 @@ func (f *FlareRunner) RunFlare(ctx context.Context, k types.Knapsack, flareStrea
return checkups.RunFlare(ctx, k, flareStream, checkups.InSituEnvironment)
}

func New(knapsack types.Knapsack) *FlareConsumer {
func New(logger log.Logger, knapsack types.Knapsack) *FlareConsumer {
return &FlareConsumer{
flarer: &FlareRunner{},
knapsack: knapsack,
Expand All @@ -47,10 +52,20 @@ func New(knapsack types.Knapsack) *FlareConsumer {
}

func (fc *FlareConsumer) Do(data io.Reader) error {
if time.Since(fc.lastFlareTime) < 5*time.Minute {
timeSinceLastFlare := time.Since(fc.lastFlareTime)

if timeSinceLastFlare < minFlareInterval {
level.Info(fc.logger).Log(
"msg", "skipping flare, run too recently",
"min_flare_interval", minFlareInterval,
"time_since_last_flare", timeSinceLastFlare,
)
return nil
}
fc.lastFlareTime = time.Now()

defer func() {
fc.lastFlareTime = time.Now()
}()

if fc.flarer == nil {
return errors.New("flarer is nil")
Expand Down
3 changes: 2 additions & 1 deletion ee/control/consumers/flareconsumer/flareconsumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"testing"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/ee/control/consumers/flareconsumer/mocks"
knapsackMock "github.com/kolide/launcher/pkg/agent/types/mocks"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -35,7 +36,7 @@ func TestFlareConsumer(t *testing.T) {
t.Parallel()

mockSack := knapsackMock.NewKnapsack(t)
f := New(mockSack)
f := New(log.NewNopLogger(), mockSack)
f.flarer = tt.flarer(t)
f.newFlareStream = func(note, uploadRequestURL string) (io.WriteCloser, error) {
// whatever, it implements write closer
Expand Down
23 changes: 12 additions & 11 deletions pkg/debug/shipper/shipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -203,16 +204,16 @@ func launcherData(k types.Knapsack, note string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

currentUser := "unknown"
consoleUsers, err := consoleuser.CurrentUsers(ctx)

switch {
case err != nil:
currentUser = fmt.Sprintf("error getting current users: %s", err)
case len(consoleUsers) > 0:
currentUser = consoleUsers[0].Username
default: // no console users
currentUser = "no console users"
usernames := "unknown"

Check failure

Code scanning / CodeQL

Hard-coded credentials Critical

Hard-coded
id
.
foundConsoleUsers, err := consoleuser.CurrentUsers(ctx)
if err != nil {
usernames = fmt.Sprintf("error getting current users: %s", err)
} else {
currentUserNames := make([]string, len(foundConsoleUsers))
for i, u := range foundConsoleUsers {
currentUserNames[i] = u.Username
}
usernames = strings.Join(currentUserNames, ", ")
}

hostname, err := os.Hostname()
Expand All @@ -222,7 +223,7 @@ func launcherData(k types.Knapsack, note string) ([]byte, error) {

b, err := json.Marshal(map[string]string{
"enroll_secret": enrollSecret(k),
"username": currentUser,
"usernames": usernames,
"hostname": hostname,
"note": note,
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/debug/shipper/shipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestShip(t *testing.T) { //nolint:paralleltest

require.Equal(t, tt.expectSecret, len(data["enroll_secret"]) > 0)
require.NotEmpty(t, data["hostname"])
require.NotEmpty(t, data["username"])
require.NotEmpty(t, data["usernames"])
require.NotEmpty(t, data["note"])
urlData := struct {
URL string
Expand Down

0 comments on commit faff0ac

Please sign in to comment.