Skip to content

Commit

Permalink
Merge branch 'main' into becca/env-naiferror
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Feb 23, 2024
2 parents e0f86e8 + e0003f2 commit f30ded4
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 136 deletions.
17 changes: 13 additions & 4 deletions cmd/launcher/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"context"
"log/slog"
"os"

"github.com/go-kit/kit/log"
"github.com/kolide/kit/logutil"
"github.com/kolide/launcher/ee/agent/flags"
"github.com/kolide/launcher/ee/agent/knapsack"
"github.com/kolide/launcher/ee/debug/checkups"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
)

func runDoctor(args []string) error {
Expand All @@ -27,8 +27,17 @@ func runDoctor(args []string) error {
}

fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)}
logger := log.With(logutil.NewCLILogger(true), "caller", log.DefaultCaller)
flagController := flags.NewFlagController(logger, nil, fcOpts...)

slogLevel := slog.LevelInfo
if opts.Debug {
slogLevel = slog.LevelDebug
}

slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slogLevel,
})).Logger

flagController := flags.NewFlagController(slogger, nil, fcOpts...)
k := knapsack.New(nil, flagController, nil, nil, nil)

w := os.Stdout //tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', tabwriter.AlignRight)
Expand Down
28 changes: 20 additions & 8 deletions cmd/launcher/flare.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (
"flag"
"fmt"
"io"
"log/slog"
"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/ee/agent/flags"
"github.com/kolide/launcher/ee/agent/knapsack"
"github.com/kolide/launcher/ee/agent/storage/inmemory"
"github.com/kolide/launcher/ee/debug/checkups"
"github.com/kolide/launcher/ee/debug/shipper"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/peterbourgon/ff/v3"
)

Expand Down Expand Up @@ -49,9 +49,17 @@ func runFlare(args []string) error {
return err
}

logger := log.NewLogfmtLogger(os.Stdout)
slogLevel := slog.LevelInfo
if opts.Debug {
slogLevel = slog.LevelDebug
}

slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slogLevel,
})).Logger

fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)}
flagController := flags.NewFlagController(logger, inmemory.NewStore(), fcOpts...)
flagController := flags.NewFlagController(slogger, inmemory.NewStore(), fcOpts...)

k := knapsack.New(nil, flagController, nil, nil, nil)
ctx := context.Background()
Expand All @@ -70,7 +78,7 @@ func runFlare(args []string) error {
return err
}
flareDest = shipper
successMessage = "Flare uploaded successfully"
successMessage = "flare uploaded successfully"
case "local":
reportName := fmt.Sprintf("kolide_agent_flare_report_%s.zip", ulid.New())
reportPath := filepath.Join(*flOutputDir, reportName)
Expand All @@ -81,16 +89,20 @@ func runFlare(args []string) error {
}
defer flareFile.Close()
flareDest = flareFile
successMessage = "Flare saved locally"
successMessage = "flare saved locally"
default:
return fmt.Errorf(`invalid save option: %s, expected "local" or "upload"`, *flSave)

}

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

level.Info(logger).Log("msg", successMessage, "file", flareDest.Name())
slogger.Log(ctx, slog.LevelInfo,
"flare creation complete",
"status", successMessage,
"file", flareDest.Name(),
)

return nil
}
35 changes: 21 additions & 14 deletions cmd/launcher/internal/record_metadata.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package internal

import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/groob/plist"
"github.com/kolide/kit/version"
"github.com/kolide/launcher/ee/agent/types"
Expand All @@ -21,8 +20,8 @@ type (
// subsystem. whenever new data is received, it will rewrite the metadata.json
// and metadata.plist files to our root install directory
metadataWriter struct {
logger log.Logger
k types.Knapsack
slogger *slog.Logger
k types.Knapsack
}
metadata struct {
DeviceId string `json:"device_id" plist:"device_id"`
Expand All @@ -33,21 +32,27 @@ type (
}
)

func NewMetadataWriter(logger log.Logger, k types.Knapsack) *metadataWriter {
func NewMetadataWriter(slogger *slog.Logger, k types.Knapsack) *metadataWriter {
return &metadataWriter{
logger: logger,
k: k,
slogger: slogger.With("component", "metadata_writer"),
k: k,
}
}

func (mw *metadataWriter) Ping() {
metadata := newMetadataTemplate()
if err := mw.populateLatestServerData(metadata); err != nil {
level.Debug(mw.logger).Log("msg", "unable to collect latest server data, metadata files will be incomplete", "err", err)
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"unable to collect latest server data, metadata files will be incomplete",
"err", err,
)
}

if err := mw.recordMetadata(metadata); err != nil {
level.Debug(mw.logger).Log("msg", "unable to write out metadata files", "err", err)
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"unable to write out metadata files",
"err", err,
)
}
}

Expand Down Expand Up @@ -106,17 +111,19 @@ func (mw *metadataWriter) recordMetadata(metadata *metadata) error {
func (mw *metadataWriter) getServerDataValue(store types.GetterSetterDeleterIteratorUpdater, key string) string {
val, err := store.Get([]byte(key))
if err != nil {
level.Debug(mw.logger).Log(
"msg", fmt.Sprintf("unable to collect value for %s from server_data, will re-attempt on next update", key),
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"unable to collect value for key from server_data, will re-attempt on next update",
"key", key,
"err", err,
)

return ""
}

if string(val) == "" {
level.Debug(mw.logger).Log(
"msg", fmt.Sprintf("server_data was missing value for %s, will re-attempt on next update", key),
mw.slogger.Log(context.TODO(), slog.LevelDebug,
"server_data was missing value for key, will re-attempt on next update",
"key", key,
"err", err,
)

Expand Down
8 changes: 4 additions & 4 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
}

fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)}
flagController := flags.NewFlagController(logger, stores[storage.AgentFlagsStore], fcOpts...)
flagController := flags.NewFlagController(slogger, stores[storage.AgentFlagsStore], fcOpts...)
k := knapsack.New(stores, flagController, db, multiSlogger, systemMultiSlogger)

go runOsqueryVersionCheck(ctx, slogger, k.LatestOsquerydPath(ctx))
Expand Down Expand Up @@ -267,15 +267,15 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl

// Add the log checkpoints to the rungroup, and run it once early, to try to get data into the logs.
// The checkpointer can take up to 5 seconds to run, so do this in the background.
checkpointer := checkups.NewCheckupLogger(logger, k)
checkpointer := checkups.NewCheckupLogger(slogger, k)
go checkpointer.Once(ctx)
runGroup.Add("logcheckpoint", checkpointer.Run, checkpointer.Interrupt)

// Create a channel for signals
sigChannel := make(chan os.Signal, 1)

// Add a rungroup to catch things on the sigChannel
signalListener := newSignalListener(sigChannel, cancel, logger)
signalListener := newSignalListener(sigChannel, cancel, slogger)
runGroup.Add("sigChannel", signalListener.Execute, signalListener.Interrupt)

// For now, remediation is not performed -- we only log the hardware change.
Expand Down Expand Up @@ -450,7 +450,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
controlService.RegisterSubscriber(authTokensSubsystemName, traceExporter)
}

if metadataWriter := internal.NewMetadataWriter(logger, k); metadataWriter == nil {
if metadataWriter := internal.NewMetadataWriter(slogger, k); metadataWriter == nil {
slogger.Log(ctx, slog.LevelDebug,
"unable to set up metadata writer",
"err", err,
Expand Down
9 changes: 6 additions & 3 deletions cmd/launcher/run_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package main
import (
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/go-kit/kit/log"
"github.com/kolide/kit/env"
"github.com/kolide/kit/fsutil"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/agent/flags"
"github.com/kolide/launcher/ee/agent/knapsack"
"github.com/kolide/launcher/ee/agent/storage/inmemory"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/kolide/launcher/pkg/osquery/runtime"
"github.com/kolide/launcher/pkg/osquery/table"
)
Expand Down Expand Up @@ -58,9 +59,11 @@ func runSocket(args []string) error {
if err != nil {
return err
}
logger := log.NewLogfmtLogger(os.Stdout)
slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})).Logger
fcOpts := []flags.Option{flags.WithCmdLineOpts(cmdlineopts)}
flagController := flags.NewFlagController(logger, inmemory.NewStore(), fcOpts...)
flagController := flags.NewFlagController(slogger, inmemory.NewStore(), fcOpts...)
k := knapsack.New(nil, flagController, nil, nil, nil)
runner := runtime.New(k, opts...)
go runner.Run()
Expand Down
15 changes: 8 additions & 7 deletions cmd/launcher/signal_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,35 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"

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

// listens for interrupts
type signalListener struct {
sigChannel chan os.Signal
cancel context.CancelFunc
logger log.Logger
slogger *slog.Logger
interrupted bool
}

func newSignalListener(sigChannel chan os.Signal, cancel context.CancelFunc, logger log.Logger) *signalListener {
func newSignalListener(sigChannel chan os.Signal, cancel context.CancelFunc, slogger *slog.Logger) *signalListener {
return &signalListener{
sigChannel: sigChannel,
cancel: cancel,
logger: log.With(logger, "component", "signal_listener"),
slogger: slogger.With("component", "signal_listener"),
}
}

func (s *signalListener) Execute() error {
signal.Notify(s.sigChannel, os.Interrupt, syscall.SIGTERM)
sig := <-s.sigChannel
level.Info(s.logger).Log("msg", "beginning shutdown via signal", "signal_received", sig)
s.slogger.Log(context.TODO(), slog.LevelInfo,
"beginning shutdown via signal",
"signal_received", sig,
)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/launcher/signal_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,7 +16,7 @@ func TestInterrupt_Multiple(t *testing.T) {

sigChannel := make(chan os.Signal, 1)
_, cancel := context.WithCancel(context.TODO())
sigListener := newSignalListener(sigChannel, cancel, log.NewNopLogger())
sigListener := newSignalListener(sigChannel, cancel, multislogger.New().Logger)

// Let the signal listener run for a bit
go sigListener.Execute()
Expand Down
Loading

0 comments on commit f30ded4

Please sign in to comment.