Skip to content

Commit

Permalink
fix: filter integrations in job summary
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed Dec 17, 2024
1 parent fc30def commit 5b82fee
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
12 changes: 11 additions & 1 deletion jobs/compliance-summarizer-job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ func (w *Worker) RunJob(ctx context.Context, j types2.Job) error {
},
}

jobIntegrations := make(map[string]bool)
for _, i := range j.IntegrationIDs {
jobIntegrations[i] = true
}

totalControls := make(map[string]bool)
failedControls := make(map[string]bool)
integrationsMap := make(map[string]bool)
Expand Down Expand Up @@ -136,9 +141,14 @@ func (w *Worker) RunJob(ctx context.Context, j types2.Job) error {
}
w.logger.Info("Before adding resource finding", zap.String("platform_resource_id", f.PlatformResourceID),
zap.Any("resource", resource))
jd.AddComplianceResult(w.logger, j, f, resource)
jd.AddComplianceResult(w.logger, j, f, resource, jobIntegrations)

if f.BenchmarkID == j.BenchmarkID {
if len(jobIntegrations) > 0 {
if _, ok := jobIntegrations[f.IntegrationID]; !ok {
continue
}
}
addJobSummary(controlSummary, controlView, resourceView, f)
integrationsMap[f.IntegrationID] = true
totalControls[f.ControlID] = true
Expand Down
1 change: 1 addition & 0 deletions jobs/compliance-summarizer-job/types/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ type Job struct {
ComplianceJobID uint
RetryCount int
BenchmarkID string
IntegrationIDs []string
CreatedAt time.Time
}
8 changes: 6 additions & 2 deletions jobs/compliance-summarizer-job/types/job_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type JobDocs struct {
}

func (jd *JobDocs) AddComplianceResult(logger *zap.Logger, job Job,
complianceResult types.ComplianceResult, resource *es.LookupResource,
complianceResult types.ComplianceResult, resource *es.LookupResource, jobIntegrations map[string]bool,
) {
if complianceResult.Severity == "" {
complianceResult.Severity = types.ComplianceResultSeverityNone
Expand All @@ -30,7 +30,11 @@ func (jd *JobDocs) AddComplianceResult(logger *zap.Logger, job Job,
}

if job.BenchmarkID == complianceResult.BenchmarkID {
jd.BenchmarkSummary.Integrations.addComplianceResult(complianceResult)
if len(jobIntegrations) == 0 {
jd.BenchmarkSummary.Integrations.addComplianceResult(complianceResult)
} else if _, ok := jobIntegrations[complianceResult.IntegrationID]; ok {
jd.BenchmarkSummary.Integrations.addComplianceResult(complianceResult)
}
}

var platformResourceID, resourceType, resourceName string
Expand Down
5 changes: 3 additions & 2 deletions services/describe/db/model/compliance_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func (cr *ComplianceRunner) SetCallers(callers []runner.Caller) error {
type ComplianceSummarizer struct {
gorm.Model

BenchmarkID string
ParentJobID uint
BenchmarkID string
ParentJobID uint
IntegrationIDs pq.StringArray `gorm:"type:text[]"`

StartedAt time.Time
RetryCount int
Expand Down
14 changes: 8 additions & 6 deletions services/describe/schedulers/compliance/summarizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (s *JobScheduler) runSummarizer(ctx context.Context, manuals bool) error {
}
s.logger.Info("documents are sank, creating summarizer", zap.String("benchmarkId", job.FrameworkID), zap.Int("sankDocCount", sankDocCount), zap.Int("totalDocCount", totalDocCount))

err = s.CreateSummarizer(job.FrameworkID, &job.ID, job.TriggerType)
err = s.CreateSummarizer(job.FrameworkID, job.IntegrationIDs, &job.ID, job.TriggerType)
if err != nil {
s.logger.Error("failed to create summarizer", zap.Error(err), zap.String("benchmarkId", job.FrameworkID))
return err
Expand Down Expand Up @@ -222,13 +222,14 @@ func (s *JobScheduler) finishComplianceJob(job model.ComplianceJob) error {
return s.db.UpdateComplianceJob(job.ID, model.ComplianceJobSucceeded, "")
}

func (s *JobScheduler) CreateSummarizer(benchmarkId string, jobId *uint, triggerType model.ComplianceTriggerType) error {
func (s *JobScheduler) CreateSummarizer(benchmarkId string, integrationIDs []string, jobId *uint, triggerType model.ComplianceTriggerType) error {
// run summarizer
dbModel := model.ComplianceSummarizer{
BenchmarkID: benchmarkId,
StartedAt: time.Now(),
Status: summarizer.ComplianceSummarizerCreated,
TriggerType: triggerType,
BenchmarkID: benchmarkId,
IntegrationIDs: integrationIDs,
StartedAt: time.Now(),
Status: summarizer.ComplianceSummarizerCreated,
TriggerType: triggerType,
}
if jobId != nil {
dbModel.ParentJobID = *jobId
Expand All @@ -247,6 +248,7 @@ func (s *JobScheduler) triggerSummarizer(ctx context.Context, job model.Complian
summarizerJob := types2.Job{
ID: job.ID,
ComplianceJobID: job.ParentJobID,
IntegrationIDs: job.IntegrationIDs,
RetryCount: job.RetryCount,
BenchmarkID: job.BenchmarkID,
CreatedAt: job.CreatedAt,
Expand Down
2 changes: 1 addition & 1 deletion services/describe/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ func (h HttpServer) TriggerConnectionsComplianceJobSummary(ctx echo.Context) err
}

for _, benchmark := range benchmarks {
err = h.Scheduler.complianceScheduler.CreateSummarizer(benchmark.ID, nil, model2.ComplianceTriggerTypeManual)
err = h.Scheduler.complianceScheduler.CreateSummarizer(benchmark.ID, nil, nil, model2.ComplianceTriggerTypeManual)
if err != nil {
return fmt.Errorf("error while creating compliance job summarizer: %v", err)
}
Expand Down

0 comments on commit 5b82fee

Please sign in to comment.