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

fix: properly synchronize job initiation #53

Merged
merged 4 commits into from
Oct 11, 2023
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1

FROM golang:1.18 as build
FROM golang:1.20 as build
WORKDIR /app

ARG VERSION=v0.0.0
Expand Down
24 changes: 13 additions & 11 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
"os/signal"
"runtime"
"sort"
"sync"
"sync/atomic"
"time"

"github.com/bloodhoundad/azurehound/v2/client/rest"
Expand Down Expand Up @@ -97,19 +99,21 @@ func start(ctx context.Context) {
defer ticker.Stop()

var (
currentJob *models.ClientJob
jobQueued sync.Mutex
currentJobID atomic.Int64
)

for {
select {
case <-ticker.C:
if currentJob != nil {
log.V(1).Info("collection in progress...", "jobId", currentJob.ID)
if jobID := currentJobID.Load(); jobID != 0 {
log.V(1).Info("collection in progress...", "jobId", jobID)
if err := checkin(ctx, *bheInstance, bheClient); err != nil {
log.Error(err, "bloodhound enterprise service checkin failed")
}
} else {
} else if jobQueued.TryLock() {
go func() {
defer jobQueued.Unlock()
log.V(2).Info("checking for available collection jobs")
if jobs, err := getAvailableJobs(ctx, *bheInstance, bheClient, updatedClient.ID); err != nil {
log.Error(err, "unable to fetch available jobs for azurehound")
Expand All @@ -132,12 +136,12 @@ func start(ctx context.Context) {
if len(executableJobs) == 0 {
log.V(2).Info("there are no jobs for azurehound to complete at this time")
} else {

defer currentJobID.Store(0)
queuedJobID := executableJobs[0].ID
currentJobID.Store(int64(queuedJobID))
// Notify BHE instance of job start
currentJob = &executableJobs[0]
if err := startJob(ctx, *bheInstance, bheClient, currentJob.ID); err != nil {
if err := startJob(ctx, *bheInstance, bheClient, queuedJobID); err != nil {
log.Error(err, "failed to start job, will retry on next heartbeat")
currentJob = nil
return
}

Expand All @@ -159,10 +163,8 @@ func start(ctx context.Context) {
if err := endJob(ctx, *bheInstance, bheClient, models.JobStatusComplete, message); err != nil {
log.Error(err, "failed to end job")
} else {
log.Info(message, "id", currentJob.ID, "duration", duration.String())
log.Info(message, "id", queuedJobID, "duration", duration.String())
}

currentJob = nil
}
}
}()
Expand Down
Loading