Skip to content

Commit

Permalink
dont use desktop runner singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Pickett committed Sep 23, 2024
1 parent c87a128 commit 47fbb32
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
1 change: 1 addition & 0 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
ls, err := localserver.New(
ctx,
k,
runner,
)

if err != nil {
Expand Down
48 changes: 22 additions & 26 deletions ee/desktop/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,32 +88,6 @@ func InstanceDesktopProcessRecords() map[string]processRecord {
return instance.uidProcs
}

func InstanceDetectPresence(reason string, interval time.Duration) (bool, error) {
if instance == nil {
return false, errors.New("no instance of DesktopUsersProcessesRunner")
}

if instance.uidProcs == nil || len(instance.uidProcs) == 0 {
return false, errors.New("no desktop processes running")
}

var lastErr error
for _, proc := range instance.uidProcs {
client := client.New(instance.userServerAuthToken, proc.socketPath)
success, err := client.DetectPresence(reason, interval)

// not sure how to handle the possiblity of multiple users
// so just return the first success
if success {
return success, err
}

lastErr = err
}

return false, fmt.Errorf("no desktop processes detected presence, last error: %w", lastErr)
}

// DesktopUsersProcessesRunner creates a launcher desktop process each time it detects
// a new console (GUI) user. If the current console user's desktop process dies, it
// will create a new one.
Expand Down Expand Up @@ -308,6 +282,28 @@ func (r *DesktopUsersProcessesRunner) Interrupt(_ error) {
)
}

func (r *DesktopUsersProcessesRunner) DetectPresence(reason string, interval time.Duration) (bool, error) {
if r.uidProcs == nil || len(r.uidProcs) == 0 {
return false, errors.New("no desktop processes running")
}

var lastErr error
for _, proc := range r.uidProcs {
client := client.New(r.userServerAuthToken, proc.socketPath)
success, err := client.DetectPresence(reason, interval)

// not sure how to handle the possiblity of multiple users
// so just return the first success
if success {
return success, err
}

lastErr = err
}

return false, fmt.Errorf("no desktop processes detected presence, last error: %w", lastErr)
}

// killDesktopProcesses kills any existing desktop processes
func (r *DesktopUsersProcessesRunner) killDesktopProcesses(ctx context.Context) {
wgDone := make(chan struct{})
Expand Down
2 changes: 1 addition & 1 deletion ee/localserver/request-id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Test_localServer_requestIdHandler(t *testing.T) {
func testServer(t *testing.T, k types.Knapsack) *localServer {
require.NoError(t, osquery.SetupLauncherKeys(k.ConfigStore()))

server, err := New(context.TODO(), k)
server, err := New(context.TODO(), k, nil)
require.NoError(t, err)
return server
}
12 changes: 9 additions & 3 deletions ee/localserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/kolide/krypto/pkg/echelper"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/ee/desktop/runner"
"github.com/kolide/launcher/pkg/osquery"
"github.com/kolide/launcher/pkg/traces"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
Expand Down Expand Up @@ -57,14 +56,20 @@ type localServer struct {

serverKey *rsa.PublicKey
serverEcKey *ecdsa.PublicKey

presenceDetector presenceDetector
}

const (
defaultRateLimit = 5
defaultRateBurst = 10
)

func New(ctx context.Context, k types.Knapsack) (*localServer, error) {
type presenceDetector interface {
DetectPresence(reason string, interval time.Duration) (bool, error)
}

func New(ctx context.Context, k types.Knapsack, presenceDetector presenceDetector) (*localServer, error) {
_, span := traces.StartSpan(ctx)
defer span.End()

Expand All @@ -75,6 +80,7 @@ func New(ctx context.Context, k types.Knapsack) (*localServer, error) {
kolideServer: k.KolideServerURL(),
myLocalDbSigner: agent.LocalDbKeys(),
myLocalHardwareSigner: agent.HardwareKeys(),
presenceDetector: presenceDetector,
}

// TODO: As there may be things that adjust the keys during runtime, we need to persist that across
Expand Down Expand Up @@ -427,7 +433,7 @@ func (ls *localServer) presenceDetectionHandler(next http.Handler) http.Handler
reason = reasonHeader
}

success, err := runner.InstanceDetectPresence(reason, detectionIntervalDuration)
success, err := ls.presenceDetector.DetectPresence(reason, detectionIntervalDuration)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
Expand Down
2 changes: 1 addition & 1 deletion ee/localserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestInterrupt_Multiple(t *testing.T) {
recalculateInterval = 100 * time.Millisecond

// Create the localserver
ls, err := New(context.TODO(), k)
ls, err := New(context.TODO(), k, nil)
require.NoError(t, err)

// Set the querier
Expand Down

0 comments on commit 47fbb32

Please sign in to comment.