Skip to content

Commit

Permalink
Merge branch 'main' into becca/remove-notary-1
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored Feb 8, 2024
2 parents b2c4a5b + 18ea1da commit 8aa4f65
Show file tree
Hide file tree
Showing 25 changed files with 643 additions and 166 deletions.
82 changes: 43 additions & 39 deletions cmd/launcher/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"os/user"
"runtime"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/logutil"
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/agent"
runnerserver "github.com/kolide/launcher/ee/desktop/runner/server"
Expand Down Expand Up @@ -75,61 +73,67 @@ func runDesktop(args []string) error {
return fmt.Errorf("parsing flags: %w", err)
}

// set up logging
logger := logutil.NewServerLogger(*fldebug)
logger = log.With(logger,
logLevel := slog.LevelInfo
if *fldebug {
logLevel = slog.LevelDebug
}

slogger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
Level: logLevel,
})).With(
"subprocess", "desktop",
"session_pid", os.Getpid(),
)

// Try to get the current user, so we can use the UID for logging. Not a fatal error if we can't, though
user, err := user.Current()
if err != nil {
level.Debug(logger).Log(
"msg", "error getting current user",
slogger.Log(context.TODO(), slog.LevelDebug,
"error getting current user",
"err", err,
)
} else {
logger = log.With(logger,
"uid", user.Uid,
)
slogger = slogger.With("uid", user.Uid)
}

level.Info(logger).Log("msg", "starting")
slogger.Log(context.TODO(), slog.LevelInfo,
"starting",
)

if *flUserServerSocketPath == "" {
*flUserServerSocketPath = defaultUserServerSocketPath()
level.Info(logger).Log(
"msg", "using default socket path since none was provided",
slogger.Log(context.TODO(), slog.LevelInfo,
"using default socket path since none was provided",
"socket_path", *flUserServerSocketPath,
)
}

runGroup := rungroup.NewRunGroup(logger)
runGroup := rungroup.NewRunGroup(slogger)

// listen for signals
runGroup.Add("desktopSignalListener", func() error {
listenSignals(logger)
listenSignals(slogger)
return nil
}, func(error) {})

// Set up notification sending and listening
notifier := notify.NewDesktopNotifier(logger, *flIconPath)
notifier := notify.NewDesktopNotifier(slogger, *flIconPath)
runGroup.Add("desktopNotifier", notifier.Listen, notifier.Interrupt)

// monitor parent
runGroup.Add("desktopMonitorParentProcess", func() error {
monitorParentProcess(logger, *flRunnerServerUrl, *flRunnerServerAuthToken, 2*time.Second)
monitorParentProcess(slogger, *flRunnerServerUrl, *flRunnerServerAuthToken, 2*time.Second)
return nil
}, func(error) {})

shutdownChan := make(chan struct{})
server, err := userserver.New(logger, *flUserServerAuthToken, *flUserServerSocketPath, shutdownChan, notifier)
server, err := userserver.New(slogger, *flUserServerAuthToken, *flUserServerSocketPath, shutdownChan, notifier)
if err != nil {
return err
}

m := menu.New(logger, *flhostname, *flmenupath)
m := menu.New(slogger, *flhostname, *flmenupath)
refreshMenu := func() {
m.Build()
}
Expand All @@ -140,8 +144,8 @@ func runDesktop(args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
level.Error(logger).Log(
"msg", "shutting down server",
slogger.Log(context.TODO(), slog.LevelError,
"shutting down server",
"err", err,
)
}
Expand All @@ -157,16 +161,16 @@ func runDesktop(args []string) error {

// notify runner server when menu opened
runGroup.Add("desktopMenuOpenedListener", func() error {
notifyRunnerServerMenuOpened(logger, *flRunnerServerUrl, *flRunnerServerAuthToken)
notifyRunnerServerMenuOpened(slogger, *flRunnerServerUrl, *flRunnerServerAuthToken)
return nil
}, func(err error) {})

// run run group
go func() {
// have to run this in a goroutine because menu needs the main thread
if err := runGroup.Run(); err != nil {
level.Error(logger).Log(
"msg", "running run group",
slogger.Log(context.TODO(), slog.LevelError,
"running run group",
"err", err,
)
}
Expand All @@ -178,20 +182,20 @@ func runDesktop(args []string) error {
return nil
}

func listenSignals(logger log.Logger) {
func listenSignals(slogger *slog.Logger) {
signalsToHandle := []os.Signal{os.Interrupt, os.Kill}
signals := make(chan os.Signal, len(signalsToHandle))
signal.Notify(signals, signalsToHandle...)

sig := <-signals

level.Debug(logger).Log(
"msg", "received signal",
slogger.Log(context.TODO(), slog.LevelDebug,
"received signal",
"signal", sig,
)
}

func notifyRunnerServerMenuOpened(logger log.Logger, rootServerUrl, authToken string) {
func notifyRunnerServerMenuOpened(slogger *slog.Logger, rootServerUrl, authToken string) {
client := authedclient.New(authToken, 2*time.Second)
menuOpendUrl := fmt.Sprintf("%s%s", rootServerUrl, runnerserver.MenuOpenedEndpoint)

Expand All @@ -200,8 +204,8 @@ func notifyRunnerServerMenuOpened(logger log.Logger, rootServerUrl, authToken st

response, err := client.Get(menuOpendUrl)
if err != nil {
level.Error(logger).Log(
"msg", "sending menu opened request to root server",
slogger.Log(context.TODO(), slog.LevelError,
"sending menu opened request to root server",
"err", err,
)
}
Expand All @@ -213,7 +217,7 @@ func notifyRunnerServerMenuOpened(logger log.Logger, rootServerUrl, authToken st
}

// monitorParentProcess continuously checks to see if parent is a live and sends on provided channel if it is not
func monitorParentProcess(logger log.Logger, runnerServerUrl, runnerServerAuthToken string, interval time.Duration) {
func monitorParentProcess(slogger *slog.Logger, runnerServerUrl, runnerServerAuthToken string, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

Expand All @@ -227,8 +231,8 @@ func monitorParentProcess(logger log.Logger, runnerServerUrl, runnerServerAuthTo
for ; true; <-ticker.C {
// check to to ensure that the ppid is still legit
if os.Getppid() < 2 {
level.Debug(logger).Log(
"msg", "ppid is 0 or 1, exiting",
slogger.Log(context.TODO(), slog.LevelDebug,
"ppid is 0 or 1, exiting",
)
break
}
Expand All @@ -255,8 +259,8 @@ func monitorParentProcess(logger log.Logger, runnerServerUrl, runnerServerAuthTo

// retry
if errCount < maxErrCount {
level.Debug(logger).Log(
"msg", "could not connect to parent, will retry",
slogger.Log(context.TODO(), slog.LevelDebug,
"could not connect to parent, will retry",
"err", err,
"attempts", errCount,
"max_attempts", maxErrCount,
Expand All @@ -265,9 +269,9 @@ func monitorParentProcess(logger log.Logger, runnerServerUrl, runnerServerAuthTo
continue
}

// errCount => maxErrCount, exit
level.Debug(logger).Log(
"msg", "could not connect to parent, max attempts reached, exiting",
// errCount >= maxErrCount, exit
slogger.Log(context.TODO(), slog.LevelDebug,
"could not connect to parent, max attempts reached, exiting",
"err", err,
"attempts", errCount,
"max_attempts", maxErrCount,
Expand Down
9 changes: 7 additions & 2 deletions cmd/launcher/desktop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"context"
"io"
"log/slog"
"net/http"
"testing"
"time"

"github.com/go-kit/kit/log"
runnerserver "github.com/kolide/launcher/ee/desktop/runner/server"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/kolide/launcher/pkg/threadsafebuffer"
Expand All @@ -24,8 +24,13 @@ func Test_desktopMonitorParentProcess(t *testing.T) { //nolint:paralleltest
monitorInterval := 250 * time.Millisecond
var logBytes threadsafebuffer.ThreadSafeBuffer

slogger := slog.New(slog.NewTextHandler(&logBytes, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}))

go func() {
monitorParentProcess(log.NewLogfmtLogger(&logBytes), runnerServer.Url(), token, monitorInterval)
monitorParentProcess(slogger, runnerServer.Url(), token, monitorInterval)
}()

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

// create a rungroup for all the actors we create to allow for easy start/stop
runGroup := rungroup.NewRunGroup(logger)
runGroup := rungroup.NewRunGroup(slogger)

// Need to set up the log shipper so that we can get the logger early
// and pass it to the various systems.
Expand Down Expand Up @@ -328,6 +328,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl

// Create the control service and services that depend on it
var runner *desktopRunner.DesktopUsersProcessesRunner
var actionsQueue *actionqueue.ActionQueue
if k.ControlServerURL() == "" {
slogger.Log(ctx, slog.LevelDebug,
"control server URL not set, will not create control service",
Expand Down Expand Up @@ -358,7 +359,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
controlService.RegisterConsumer(desktopMenuSubsystemName, runner)

// create an action queue for all other action style commands
actionsQueue := actionqueue.New(
actionsQueue = actionqueue.New(
k,
actionqueue.WithContext(ctx),
actionqueue.WithStore(k.ControlServerActionsStore()),
Expand Down Expand Up @@ -459,6 +460,9 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
}

runGroup.Add("tufAutoupdater", tufAutoupdater.Execute, tufAutoupdater.Interrupt)
if actionsQueue != nil {
actionsQueue.RegisterActor(tuf.AutoupdateSubsystemName, tufAutoupdater)
}
}

startupSpan.End()
Expand Down
Loading

0 comments on commit 8aa4f65

Please sign in to comment.