Skip to content

Commit

Permalink
Remove WithOsquerydBinary option since we now retrieve it from the kn…
Browse files Browse the repository at this point in the history
…apsack
  • Loading branch information
RebeccaMahany committed Apr 11, 2024
1 parent 0aeed24 commit 8b366ba
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 71 deletions.
1 change: 0 additions & 1 deletion cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
osqueryRunner := osqueryruntime.New(
k,
osqueryruntime.WithKnapsack(k),
osqueryruntime.WithOsquerydBinary(k.OsquerydPath()),
osqueryruntime.WithRootDirectory(k.RootDirectory()),
osqueryruntime.WithOsqueryExtensionPlugins(table.LauncherTables(k)...),
osqueryruntime.WithSlogger(k.Slogger().With("component", "osquery_instance")),
Expand Down
12 changes: 0 additions & 12 deletions pkg/osquery/runtime/osqueryinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ func WithOsqueryExtensionPlugins(plugins ...osquery.OsqueryPlugin) OsqueryInstan
}
}

// WithOsquerydBinary is a functional option which allows the user to define the
// path of the osqueryd binary which will be launched. This should only be called
// once as only one binary will be executed. Defining the path to the osqueryd
// binary is optional. If it is not explicitly defined by the caller, an osqueryd
// binary will be looked for in the current $PATH.
func WithOsquerydBinary(path string) OsqueryInstanceOption {
return func(i *OsqueryInstance) {
i.opts.binaryPath = path
}
}

// WithRootDirectory is a functional option which allows the user to define the
// path where filesystem artifacts will be stored. This may include pidfiles,
// RocksDB database files, etc. If this is not defined, a temporary directory
Expand Down Expand Up @@ -305,7 +294,6 @@ type osqueryOptions struct {
// the following are options which may or may not be set by the functional
// options included by the caller of LaunchOsqueryInstance
augeasLensFunc func(dir string) error
binaryPath string
configPluginFlag string
distributedPluginFlag string
extensionPlugins []osquery.OsqueryPlugin
Expand Down
43 changes: 3 additions & 40 deletions pkg/osquery/runtime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import (
"fmt"
"log/slog"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"time"

"github.com/kolide/launcher/ee/agent/flags/keys"
"github.com/kolide/launcher/ee/agent/types"

"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/backoff"
"github.com/kolide/launcher/pkg/osquery/runtime/history"
"github.com/kolide/launcher/pkg/osquery/table"
Expand Down Expand Up @@ -216,23 +213,6 @@ func (r *Runner) launchOsqueryInstance() error {

o := r.instance

// What binary name to look for
lookFor := "osqueryd"
if runtime.GOOS == "windows" {
lookFor = lookFor + ".exe"
}

// If the path of the osqueryd binary wasn't explicitly defined by the caller,
// try to find it in the path.
if o.opts.binaryPath == "" {
path, err := exec.LookPath(lookFor)
if err != nil {
traces.SetError(span, fmt.Errorf("osqueryd not supplied and not found: %w", err))
return fmt.Errorf("osqueryd not supplied and not found: %w", err)
}
o.opts.binaryPath = path
}

// If the caller did not define the directory which all of the osquery file
// artifacts should be stored in, use a temporary directory.
if o.opts.rootDirectory == "" {
Expand Down Expand Up @@ -301,26 +281,9 @@ func (r *Runner) launchOsqueryInstance() error {
o.opts.distributedPluginFlag = "internal_noop"
}

// If we're on windows, ensure that we're looking for the .exe
if runtime.GOOS == "windows" && !strings.HasSuffix(o.opts.binaryPath, ".exe") {
o.opts.binaryPath = o.opts.binaryPath + ".exe"
}

// before we start osqueryd, check with the update system to
// see if we have the newest version. Do this every time. If
// this proves undesirable, we can expose a function to set
// o.opts.binaryPath in the finalizer to call.
var currentOsquerydBinaryPath string
currentOsquerydBinary, err := tuf.CheckOutLatest(ctx, "osqueryd", o.opts.rootDirectory, o.opts.updateDirectory, o.knapsack.PinnedOsquerydVersion(), o.opts.updateChannel, r.slogger)
if err != nil {
r.slogger.Log(ctx, slog.LevelDebug,
"could not get latest version of osqueryd from autoupdate library",
"err", err,
)
return fmt.Errorf("getting osqueryd path: %w", err)
} else {
currentOsquerydBinaryPath = currentOsquerydBinary.Path
}
// The knapsack will retrieve the correct version of osqueryd from the download library if available.
// If not available, it will fall back to the configured installed version of osqueryd.
currentOsquerydBinaryPath := o.knapsack.LatestOsquerydPath(ctx)
span.AddEvent("got_osqueryd_binary_path", trace.WithAttributes(attribute.String("path", currentOsquerydBinaryPath)))

// Now that we have accepted options from the caller and/or determined what
Expand Down
6 changes: 2 additions & 4 deletions pkg/osquery/runtime/runtime_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ func TestOsquerySlowStart(t *testing.T) {
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
slogger := multislogger.New(slog.NewJSONHandler(&logBytes, &slog.HandlerOptions{Level: slog.LevelDebug}))
k.On("Slogger").Return(slogger.Logger)
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
WithSlogger(slogger.Logger),
WithStartFunc(func(cmd *exec.Cmd) error {
err := cmd.Start()
Expand Down Expand Up @@ -95,15 +94,14 @@ func TestExtensionSocketPath(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

extensionSocketPath := filepath.Join(rootDirectory, "sock")
runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithExtensionSocketPath(extensionSocketPath),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
go runner.Run()

Expand Down
21 changes: 7 additions & 14 deletions pkg/osquery/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,12 @@ func TestBadBinaryPath(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return("")

runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary("/foobar"),
)
assert.Error(t, runner.Run())

Expand All @@ -336,13 +335,12 @@ func TestWithOsqueryFlags(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
WithOsqueryFlags([]string{"verbose=false"}),
)
go runner.Run()
Expand All @@ -369,14 +367,13 @@ func TestFlagsChanged(t *testing.T) {
k.On("WatchdogDelaySec").Return(120)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

// Start the runner
runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
WithOsqueryFlags([]string{"verbose=false"}),
)
go runner.Run()
Expand Down Expand Up @@ -463,13 +460,12 @@ func TestSimplePath(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
go runner.Run()

Expand All @@ -492,13 +488,12 @@ func TestMultipleShutdowns(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
go runner.Run()

Expand All @@ -520,13 +515,12 @@ func TestOsqueryDies(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

runner := New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
go runner.Run()
require.NoError(t, err)
Expand Down Expand Up @@ -615,13 +609,12 @@ func setupOsqueryInstanceForTests(t *testing.T) (runner *Runner, teardown func()
k.On("WatchdogDelaySec").Return(120)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Maybe()
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("PinnedOsquerydVersion").Return("")
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)

runner = New(
k,
WithKnapsack(k),
WithRootDirectory(rootDirectory),
WithOsquerydBinary(testOsqueryBinaryDirectory),
)
go runner.Run()
waitHealthy(t, runner)
Expand Down

0 comments on commit 8b366ba

Please sign in to comment.