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

One log adapter per osquery instance; set registration ID and instance run ID on log adapter #1985

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
17 changes: 0 additions & 17 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"github.com/kolide/launcher/ee/debug/checkups"
desktopRunner "github.com/kolide/launcher/ee/desktop/runner"
"github.com/kolide/launcher/ee/localserver"
kolidelog "github.com/kolide/launcher/ee/log/osquerylogs"
"github.com/kolide/launcher/ee/powereventwatcher"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/ee/watchdog"
Expand Down Expand Up @@ -371,22 +370,6 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
osqueryRunner := osqueryruntime.New(
k,
client,
osqueryruntime.WithStdout(kolidelog.NewOsqueryLogAdapter(
k.Slogger().With(
"component", "osquery",
"osqlevel", "stdout",
),
k.RootDirectory(),
kolidelog.WithLevel(slog.LevelDebug),
)),
osqueryruntime.WithStderr(kolidelog.NewOsqueryLogAdapter(
k.Slogger().With(
"component", "osquery",
"osqlevel", "stderr",
),
k.RootDirectory(),
kolidelog.WithLevel(slog.LevelInfo),
)),
osqueryruntime.WithAugeasLensFunction(augeas.InstallLenses),
)
runGroup.Add("osqueryRunner", osqueryRunner.Run, osqueryRunner.Interrupt)
Expand Down
47 changes: 21 additions & 26 deletions pkg/osquery/runtime/osqueryinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/gowrapper"
kolidelog "github.com/kolide/launcher/ee/log/osquerylogs"
"github.com/kolide/launcher/pkg/backoff"
launcherosq "github.com/kolide/launcher/pkg/osquery"
"github.com/kolide/launcher/pkg/osquery/runtime/history"
Expand Down Expand Up @@ -71,24 +72,6 @@ func WithExtensionSocketPath(path string) OsqueryInstanceOption {
}
}

// WithStdout is a functional option which allows the user to define where the
// stdout of the osquery process should be directed. By default, the output will
// be discarded. This should only be configured once.
func WithStdout(w io.Writer) OsqueryInstanceOption {
return func(i *OsqueryInstance) {
i.opts.stdout = w
}
}

// WithStderr is a functional option which allows the user to define where the
// stderr of the osquery process should be directed. By default, the output will
// be discarded. This should only be configured once.
func WithStderr(w io.Writer) OsqueryInstanceOption {
return func(i *OsqueryInstance) {
i.opts.stderr = w
}
}

// WithAugeasLensFunction defines a callback function. This can be
// used during setup to populate the augeas lenses directory.
func WithAugeasLensFunction(f func(dir string) error) OsqueryInstanceOption {
Expand Down Expand Up @@ -185,8 +168,6 @@ type osqueryOptions struct {
// options included by the caller of LaunchOsqueryInstance
augeasLensFunc func(dir string) error
extensionSocketPath string
stderr io.Writer
stdout io.Writer
}

func newInstance(registrationId string, knapsack types.Knapsack, serviceClient service.KolideService, opts ...OsqueryInstanceOption) *OsqueryInstance {
Expand Down Expand Up @@ -767,12 +748,26 @@ func (i *OsqueryInstance) createOsquerydCommand(osquerydBinary string, paths *os
}

cmd.Args = append(cmd.Args, platformArgs()...)
if i.opts.stdout != nil {
cmd.Stdout = i.opts.stdout
}
if i.opts.stderr != nil {
cmd.Stderr = i.opts.stderr
}
cmd.Stdout = kolidelog.NewOsqueryLogAdapter(
i.knapsack.Slogger().With(
"component", "osquery",
"osqlevel", "stdout",
"registration_id", i.registrationId,
"instance_run_id", i.runId,
),
i.knapsack.RootDirectory(),
kolidelog.WithLevel(slog.LevelDebug),
)
cmd.Stderr = kolidelog.NewOsqueryLogAdapter(
i.knapsack.Slogger().With(
"component", "osquery",
"osqlevel", "stderr",
"registration_id", i.registrationId,
"instance_run_id", i.runId,
),
i.knapsack.RootDirectory(),
kolidelog.WithLevel(slog.LevelInfo),
)

// Apply user-provided flags last so that they can override other flags set
// by Launcher (besides the flags below)
Expand Down
11 changes: 6 additions & 5 deletions pkg/osquery/runtime/osqueryinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package runtime
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -61,13 +60,12 @@ func TestCreateOsqueryCommand(t *testing.T) {
k.On("OsqueryVerbose").Return(true)
k.On("OsqueryFlags").Return([]string{})
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("RootDirectory").Return("")

i := newInstance(types.DefaultRegistrationID, k, mockServiceClient(), WithStdout(os.Stdout), WithStderr(os.Stderr))
i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

cmd, err := i.createOsquerydCommand(osquerydPath, paths)
_, err := i.createOsquerydCommand(osquerydPath, paths)
require.NoError(t, err)
require.Equal(t, os.Stderr, cmd.Stderr)
require.Equal(t, os.Stdout, cmd.Stdout)

k.AssertExpectations(t)
}
Expand All @@ -83,6 +81,7 @@ func TestCreateOsqueryCommandWithFlags(t *testing.T) {
k.On("OsqueryFlags").Return([]string{"verbose=false", "windows_event_channels=foo,bar"})
k.On("OsqueryVerbose").Return(true)
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("RootDirectory").Return("")

i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

Expand Down Expand Up @@ -116,6 +115,7 @@ func TestCreateOsqueryCommand_SetsEnabledWatchdogSettingsAppropriately(t *testin
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("OsqueryVerbose").Return(true)
k.On("OsqueryFlags").Return([]string{})
k.On("RootDirectory").Return("")

i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

Expand Down Expand Up @@ -165,6 +165,7 @@ func TestCreateOsqueryCommand_SetsDisabledWatchdogSettingsAppropriately(t *testi
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("OsqueryVerbose").Return(true)
k.On("OsqueryFlags").Return([]string{})
k.On("RootDirectory").Return("")

i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

Expand Down
1 change: 1 addition & 0 deletions pkg/osquery/runtime/osqueryinstance_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestCreateOsqueryCommandEnvVars(t *testing.T) {
k.On("OsqueryVerbose").Return(true)
k.On("OsqueryFlags").Return([]string{})
k.On("Slogger").Return(multislogger.NewNopLogger())
k.On("RootDirectory").Return("")

i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

Expand Down
11 changes: 4 additions & 7 deletions pkg/osquery/runtime/runtime_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestOsquerySlowStart(t *testing.T) {

rootDirectory := testRootDirectory(t)

logBytes, slogger, opts := setUpTestSlogger(rootDirectory)
logBytes, slogger := setUpTestSlogger()

k := typesMocks.NewKnapsack(t)
k.On("RegistrationIDs").Return([]string{types.DefaultRegistrationID})
Expand All @@ -57,7 +57,7 @@ func TestOsquerySlowStart(t *testing.T) {
k.On("ReadEnrollSecret").Return("", nil).Maybe()
setUpMockStores(t, k)

opts = append(opts, WithStartFunc(func(cmd *exec.Cmd) error {
runner := New(k, mockServiceClient(), WithStartFunc(func(cmd *exec.Cmd) error {
err := cmd.Start()
if err != nil {
return fmt.Errorf("unexpected error starting command: %w", err)
Expand All @@ -71,8 +71,6 @@ func TestOsquerySlowStart(t *testing.T) {
}()
return nil
}))

runner := New(k, mockServiceClient(), opts...)
go runner.Run()
waitHealthy(t, runner, logBytes)

Expand All @@ -88,7 +86,7 @@ func TestExtensionSocketPath(t *testing.T) {

rootDirectory := testRootDirectory(t)

logBytes, slogger, opts := setUpTestSlogger(rootDirectory)
logBytes, slogger := setUpTestSlogger()

k := typesMocks.NewKnapsack(t)
k.On("RegistrationIDs").Return([]string{types.DefaultRegistrationID})
Expand All @@ -107,9 +105,8 @@ func TestExtensionSocketPath(t *testing.T) {
setUpMockStores(t, k)

extensionSocketPath := filepath.Join(rootDirectory, "sock")
opts = append(opts, WithExtensionSocketPath(extensionSocketPath))

runner := New(k, mockServiceClient(), opts...)
runner := New(k, mockServiceClient(), WithExtensionSocketPath(extensionSocketPath))
go runner.Run()

waitHealthy(t, runner, logBytes)
Expand Down
Loading
Loading