Skip to content

Commit

Permalink
drop reqs when detection locked (#2008)
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Pickett authored Dec 19, 2024
1 parent fa6e3ac commit f6a17fe
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
7 changes: 3 additions & 4 deletions ee/desktop/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,20 @@ func (r *DesktopUsersProcessesRunner) DetectPresence(reason string, interval tim
}

var lastErr error
var lastDurationSinceLastDetection time.Duration

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

durationSinceLastDetection, err := client.DetectPresence(reason, interval)
if err != nil {
lastErr = err
continue
}

return lastDurationSinceLastDetection, nil
return durationSinceLastDetection, nil
}

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

// killDesktopProcesses kills any existing desktop processes
Expand Down
31 changes: 31 additions & 0 deletions ee/desktop/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/kolide/launcher/ee/agent/flags/keys"
"github.com/kolide/launcher/ee/agent/types/mocks"
"github.com/kolide/launcher/ee/desktop/user/notify"
"github.com/kolide/launcher/ee/presencedetection"
"github.com/kolide/launcher/pkg/backoff"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/kolide/launcher/pkg/threadsafebuffer"
Expand Down Expand Up @@ -493,3 +494,33 @@ func countFilesWithPrefix(folderPath, prefix string) (int, error) {

return count, nil
}

func TestDesktopUsersProcessesRunner_DetectPresence(t *testing.T) {
t.Parallel()

t.Run("no user procs", func(t *testing.T) {
t.Parallel()

runner := DesktopUsersProcessesRunner{}
d, err := runner.DetectPresence("whatevs", time.Second)
require.Error(t, err)
require.Equal(t, presencedetection.DetectionFailedDurationValue, d)
})

t.Run("cant connect to user server", func(t *testing.T) {
t.Parallel()

u, err := user.Current()
require.NoError(t, err)

runner := DesktopUsersProcessesRunner{
uidProcs: map[string]processRecord{
u.Uid: {},
},
}

d, err := runner.DetectPresence("whatevs", time.Second)
require.Error(t, err)
require.Equal(t, presencedetection.DetectionFailedDurationValue, d)
})
}
1 change: 0 additions & 1 deletion ee/localserver/request-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (ls *localServer) requestIdHandlerFunc(w http.ResponseWriter, r *http.Reque
Origin: r.Header.Get("Origin"),
Status: status{
EnrollmentStatus: string(enrollmentStatus),
InstanceStatuses: ls.knapsack.InstanceStatuses(),
},
}
response.identifiers = ls.identifiers
Expand Down
1 change: 0 additions & 1 deletion ee/localserver/request-id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func Test_localServer_requestIdHandler(t *testing.T) {
mockKnapsack.On("ConfigStore").Return(storageci.NewStore(t, multislogger.NewNopLogger(), storage.ConfigStore.String()))
mockKnapsack.On("KolideServerURL").Return("localhost")
mockKnapsack.On("CurrentEnrollmentStatus").Return(types.Enrolled, nil)
mockKnapsack.On("InstanceStatuses").Return(map[string]types.InstanceStatus{"default": types.InstanceStatusHealthy})

var logBytes bytes.Buffer
slogger := slog.New(slog.NewJSONHandler(&logBytes, &slog.HandlerOptions{
Expand Down
10 changes: 6 additions & 4 deletions ee/localserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,6 @@ func (ls *localServer) rateLimitHandler(next http.Handler) http.Handler {

func (ls *localServer) presenceDetectionHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ensure we only prompt for 1 presence detection at a time
ls.presenceDetectionMutex.Lock()
defer ls.presenceDetectionMutex.Unlock()

// can test this by adding an unauthed endpoint to the mux and running, for example:
// curl -i -H "X-Kolide-Presence-Detection-Interval: 10s" -H "X-Kolide-Presence-Detection-Reason: my reason" localhost:12519/id
detectionIntervalStr := r.Header.Get(kolidePresenceDetectionIntervalHeaderKey)
Expand Down Expand Up @@ -461,6 +457,12 @@ func (ls *localServer) presenceDetectionHandler(next http.Handler) http.Handler
)
}

if !ls.presenceDetectionMutex.TryLock() {
http.Error(w, "presence detection already in progress", http.StatusTooManyRequests)
return
}
defer ls.presenceDetectionMutex.Unlock()

durationSinceLastDetection, err := ls.presenceDetector.DetectPresence(reason, detectionIntervalDuration)

if err != nil {
Expand Down

0 comments on commit f6a17fe

Please sign in to comment.