Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace logger with slogger in library lookup and osquery instance #1617

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cmd/launcher/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"strings"

"github.com/kolide/kit/logutil"
"github.com/kolide/launcher/cmd/launcher/internal"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/tuf"
Expand All @@ -34,8 +33,6 @@ func runInteractive(args []string) error {
return err
}

logger := logutil.NewServerLogger(*flDebug)

slogLevel := slog.LevelInfo
if *flDebug {
slogLevel = slog.LevelDebug
Expand All @@ -47,7 +44,7 @@ func runInteractive(args []string) error {

osquerydPath := *flOsquerydPath
if osquerydPath == "" {
latestOsquerydBinary, err := tuf.CheckOutLatestWithoutConfig("osqueryd", logger)
latestOsquerydBinary, err := tuf.CheckOutLatestWithoutConfig("osqueryd", slogger)
if err != nil {
osquerydPath = launcher.FindOsquery()
if osquerydPath == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
osqueryruntime.WithOsquerydBinary(k.OsquerydPath()),
osqueryruntime.WithRootDirectory(k.RootDirectory()),
osqueryruntime.WithOsqueryExtensionPlugins(table.LauncherTables(k)...),
osqueryruntime.WithLogger(logger),
osqueryruntime.WithSlogger(k.Slogger().With("component", "osquery_instance")),
osqueryruntime.WithOsqueryVerbose(k.OsqueryVerbose()),
osqueryruntime.WithOsqueryFlags(k.OsqueryFlags()),
osqueryruntime.WithStdout(kolidelog.NewOsqueryLogAdapter(
Expand Down
52 changes: 31 additions & 21 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"text/tabwriter"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/env"
"github.com/kolide/kit/logutil"
Expand Down Expand Up @@ -44,13 +43,18 @@ func main() {

ctx = ctxlog.NewContext(ctx, logger)

// set up system slogger to write to os logs
systemSlogger := multislogger.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))

// If there's a newer version of launcher on disk, use it.
// Allow a caller to set `LAUNCHER_SKIP_UPDATES` as a way to
// skip exec'ing an update. This helps prevent launcher from
// fork-bombing itself. This is an ENV, because there's no
// good way to pass it through the flags.
if !env.Bool("LAUNCHER_SKIP_UPDATES", false) {
runNewerLauncherIfAvailable(ctx, logger)
runNewerLauncherIfAvailable(ctx, systemSlogger.Logger)
}

// if the launcher is being ran with a positional argument,
Expand All @@ -71,11 +75,6 @@ func main() {
// recreate the logger with the appropriate level.
logger = logutil.NewServerLogger(opts.Debug)

// set up system slogger to write to os logs
systemSlogger := multislogger.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))

// set up slogger for internal launcher logging
slogger := multislogger.New()

Expand Down Expand Up @@ -118,7 +117,7 @@ func main() {
if err := runLauncher(ctx, cancel, slogger, systemSlogger, opts); err != nil {
if tuf.IsLauncherReloadNeededErr(err) {
level.Debug(logger).Log("msg", "runLauncher exited to run newer version of launcher", "err", err.Error())
runNewerLauncherIfAvailable(ctx, logger)
runNewerLauncherIfAvailable(ctx, slogger.Logger)
} else {
level.Debug(logger).Log("msg", "run launcher", "stack", fmt.Sprintf("%+v", err))
logutil.Fatal(logger, err, "run launcher")
Expand Down Expand Up @@ -167,11 +166,11 @@ func runSubcommands() error {

// runNewerLauncherIfAvailable checks the autoupdate library for a newer version
// of launcher than the currently-running one. If found, it will exec that version.
func runNewerLauncherIfAvailable(ctx context.Context, logger log.Logger) {
newerBinary, err := latestLauncherPath(ctx, logger)
func runNewerLauncherIfAvailable(ctx context.Context, slogger *slog.Logger) {
newerBinary, err := latestLauncherPath(ctx, slogger)
if err != nil {
level.Error(logger).Log(
"msg", "could not check out latest launcher, will fall back to old autoupdate library",
slogger.Log(ctx, slog.LevelError,
"could not check out latest launcher, will fall back to old autoupdate library",
"err", err,
)

Expand All @@ -183,35 +182,46 @@ func runNewerLauncherIfAvailable(ctx context.Context, logger log.Logger) {
}

if newerBinary == "" {
level.Info(logger).Log("msg", "nothing newer")
slogger.Log(ctx, slog.LevelInfo,
"nothing newer",
)
return
}

level.Info(logger).Log(
"msg", "preparing to exec new binary",
slogger.Log(ctx, slog.LevelInfo,
"preparing to exec new binary",
"old_version", version.Version().Version,
"new_binary", newerBinary,
)

if err := execwrapper.Exec(ctx, newerBinary, os.Args, os.Environ()); err != nil {
logutil.Fatal(logger, "msg", "error execing newer version of launcher", "new_binary", newerBinary, "err", err)
slogger.Log(ctx, slog.LevelError,
"error execing newer version of launcher",
"new_binary", newerBinary,
"err", err,
)
os.Exit(1)
}

logutil.Fatal(logger, "msg", "execing newer version of launcher exited unexpectedly without error", "new_binary", newerBinary)
slogger.Log(ctx, slog.LevelError,
"execing newer version of launcher exited unexpectedly without error",
"new_binary", newerBinary,
)
os.Exit(1)
}

// latestLauncherPath looks for the latest version of launcher in the new autoupdate library,
// falling back to the old library if necessary.
func latestLauncherPath(ctx context.Context, logger log.Logger) (string, error) {
newerBinary, err := tuf.CheckOutLatestWithoutConfig("launcher", logger)
func latestLauncherPath(ctx context.Context, slogger *slog.Logger) (string, error) {
newerBinary, err := tuf.CheckOutLatestWithoutConfig("launcher", slogger)
if err != nil {
return "", fmt.Errorf("checking out latest launcher: %w", err)
}

currentPath, _ := os.Executable()
if newerBinary.Version != version.Version().Version && newerBinary.Path != currentPath {
level.Info(logger).Log(
"msg", "got new version of launcher to run",
slogger.Log(ctx, slog.LevelInfo,
"got new version of launcher to run",
"old_version", version.Version().Version,
"new_binary_version", newerBinary.Version,
"new_binary_path", newerBinary.Path,
Expand Down
10 changes: 8 additions & 2 deletions ee/agent/knapsack/knapsack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"log/slog"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/ee/agent/storage"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/tuf"
Expand Down Expand Up @@ -43,6 +42,13 @@ type knapsack struct {
}

func New(stores map[storage.Store]types.KVStore, flags types.Flags, db *bbolt.DB, slogger, systemSlogger *multislogger.MultiSlogger) *knapsack {
if slogger == nil {
slogger = multislogger.New()
}
if systemSlogger == nil {
systemSlogger = multislogger.New()
}

k := &knapsack{
db: db,
flags: flags,
Expand Down Expand Up @@ -141,7 +147,7 @@ func (k *knapsack) getKVStore(storeType storage.Store) types.KVStore {
}

func (k *knapsack) LatestOsquerydPath(ctx context.Context) string {
latestBin, err := tuf.CheckOutLatest(ctx, "osqueryd", k.RootDirectory(), k.UpdateDirectory(), k.UpdateChannel(), log.NewNopLogger())
latestBin, err := tuf.CheckOutLatest(ctx, "osqueryd", k.RootDirectory(), k.UpdateDirectory(), k.UpdateChannel(), k.Slogger())
if err != nil {
return autoupdate.FindNewest(ctx, k.OsquerydPath())
}
Expand Down
8 changes: 5 additions & 3 deletions ee/debug/checkups/tuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"path/filepath"
"runtime"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/log/multislogger"
)

type (
Expand Down Expand Up @@ -196,14 +196,16 @@ func (tc *tufCheckup) selectedVersions() map[string]map[string]string {
"osqueryd": make(map[string]string),
}

if launcherVersion, err := tuf.CheckOutLatestWithoutConfig("launcher", log.NewNopLogger()); err != nil {
nopLogger := multislogger.New().Logger

if launcherVersion, err := tuf.CheckOutLatestWithoutConfig("launcher", nopLogger); err != nil {
selectedVersions["launcher"]["path"] = fmt.Sprintf("error checking out latest version: %v", err)
} else {
selectedVersions["launcher"]["path"] = launcherVersion.Path
selectedVersions["launcher"]["version"] = launcherVersion.Version
}

if osquerydVersion, err := tuf.CheckOutLatestWithoutConfig("osqueryd", log.NewNopLogger()); err != nil {
if osquerydVersion, err := tuf.CheckOutLatestWithoutConfig("osqueryd", nopLogger); err != nil {
selectedVersions["osqueryd"]["path"] = fmt.Sprintf("error checking out latest version: %v", err)
} else {
selectedVersions["osqueryd"]["path"] = osquerydVersion.Path
Expand Down
22 changes: 14 additions & 8 deletions ee/tuf/library_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"

"github.com/Masterminds/semver"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/pkg/autoupdate"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/traces"
Expand All @@ -32,8 +31,8 @@ type autoupdateConfig struct {

// CheckOutLatestWithoutConfig returns information about the latest downloaded executable for our binary,
// searching for launcher configuration values in its config file.
func CheckOutLatestWithoutConfig(binary autoupdatableBinary, logger log.Logger) (*BinaryUpdateInfo, error) {
logger = log.With(logger, "component", "tuf_library_lookup")
func CheckOutLatestWithoutConfig(binary autoupdatableBinary, slogger *slog.Logger) (*BinaryUpdateInfo, error) {
slogger = slogger.With("component", "tuf_library_lookup")
cfg, err := getAutoupdateConfig(os.Args[1:])
if err != nil {
return nil, fmt.Errorf("could not get autoupdate config: %w", err)
Expand All @@ -44,7 +43,7 @@ func CheckOutLatestWithoutConfig(binary autoupdatableBinary, logger log.Logger)
return &BinaryUpdateInfo{Path: cfg.localDevelopmentPath}, nil
}

return CheckOutLatest(context.Background(), binary, cfg.rootDirectory, cfg.updateDirectory, cfg.channel, logger)
return CheckOutLatest(context.Background(), binary, cfg.rootDirectory, cfg.updateDirectory, cfg.channel, slogger)
}

// getAutoupdateConfig pulls the configuration values necessary to work with the autoupdate library
Expand Down Expand Up @@ -141,7 +140,7 @@ func getAutoupdateConfigFromFile(configFilePath string) (*autoupdateConfig, erro

// CheckOutLatest returns the path to the latest downloaded executable for our binary, as well
// as its version.
func CheckOutLatest(ctx context.Context, binary autoupdatableBinary, rootDirectory string, updateDirectory string, channel string, logger log.Logger) (*BinaryUpdateInfo, error) {
func CheckOutLatest(ctx context.Context, binary autoupdatableBinary, rootDirectory string, updateDirectory string, channel string, slogger *slog.Logger) (*BinaryUpdateInfo, error) {
ctx, span := traces.StartSpan(ctx, "binary", string(binary))
defer span.End()

Expand All @@ -152,11 +151,18 @@ func CheckOutLatest(ctx context.Context, binary autoupdatableBinary, rootDirecto
update, err := findExecutableFromRelease(ctx, binary, LocalTufDirectory(rootDirectory), channel, updateDirectory)
if err == nil {
span.AddEvent("found_latest_from_release")
level.Info(logger).Log("msg", "found executable matching current release", "executable_path", update.Path, "executable_version", update.Version)
slogger.Log(ctx, slog.LevelInfo,
"found executable matching current release",
"executable_path", update.Path,
"executable_version", update.Version,
)
return update, nil
}

level.Info(logger).Log("msg", "could not find executable matching current release", "err", err)
slogger.Log(ctx, slog.LevelInfo,
"could not find executable matching current release",
"err", err,
)

// If we can't find the specific release version that we should be on, then just return the executable
// with the most recent version in the library
Expand Down
6 changes: 3 additions & 3 deletions ee/tuf/library_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"path/filepath"
"testing"

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

Expand Down Expand Up @@ -45,7 +45,7 @@ func TestCheckOutLatest_withTufRepository(t *testing.T) {
require.NoError(t, os.Chmod(tooRecentPath, 0755))

// Check it
latest, err := CheckOutLatest(context.TODO(), binary, rootDir, "", "nightly", log.NewNopLogger())
latest, err := CheckOutLatest(context.TODO(), binary, rootDir, "", "nightly", multislogger.New().Logger)
require.NoError(t, err, "unexpected error on checking out latest")
require.Equal(t, executablePath, latest.Path)
require.Equal(t, executableVersion, latest.Version)
Expand All @@ -72,7 +72,7 @@ func TestCheckOutLatest_withoutTufRepository(t *testing.T) {
require.NoError(t, err, "did not make test binary")

// Check it
latest, err := CheckOutLatest(context.TODO(), binary, rootDir, "", "nightly", log.NewNopLogger())
latest, err := CheckOutLatest(context.TODO(), binary, rootDir, "", "nightly", multislogger.New().Logger)
require.NoError(t, err, "unexpected error on checking out latest")
require.Equal(t, executablePath, latest.Path)
require.Equal(t, executableVersion, latest.Version)
Expand Down
Loading
Loading