From 5b82fee00062495049467441b0be06b27faa3898 Mon Sep 17 00:00:00 2001 From: artaasadi Date: Tue, 17 Dec 2024 22:15:29 +0100 Subject: [PATCH] fix: filter integrations in job summary --- jobs/compliance-summarizer-job/job.go | 12 +++++++++++- jobs/compliance-summarizer-job/types/job.go | 1 + jobs/compliance-summarizer-job/types/job_docs.go | 8 ++++++-- services/describe/db/model/compliance_job.go | 5 +++-- .../describe/schedulers/compliance/summarizer.go | 14 ++++++++------ services/describe/server.go | 2 +- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/jobs/compliance-summarizer-job/job.go b/jobs/compliance-summarizer-job/job.go index 60c48b727..42f63fb53 100644 --- a/jobs/compliance-summarizer-job/job.go +++ b/jobs/compliance-summarizer-job/job.go @@ -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) @@ -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 diff --git a/jobs/compliance-summarizer-job/types/job.go b/jobs/compliance-summarizer-job/types/job.go index ad487c436..0ac8fe354 100644 --- a/jobs/compliance-summarizer-job/types/job.go +++ b/jobs/compliance-summarizer-job/types/job.go @@ -7,5 +7,6 @@ type Job struct { ComplianceJobID uint RetryCount int BenchmarkID string + IntegrationIDs []string CreatedAt time.Time } diff --git a/jobs/compliance-summarizer-job/types/job_docs.go b/jobs/compliance-summarizer-job/types/job_docs.go index e2d4c1ea0..a0af66364 100644 --- a/jobs/compliance-summarizer-job/types/job_docs.go +++ b/jobs/compliance-summarizer-job/types/job_docs.go @@ -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 @@ -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 diff --git a/services/describe/db/model/compliance_job.go b/services/describe/db/model/compliance_job.go index 72c801fc9..08b4ebb9d 100644 --- a/services/describe/db/model/compliance_job.go +++ b/services/describe/db/model/compliance_job.go @@ -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 diff --git a/services/describe/schedulers/compliance/summarizer.go b/services/describe/schedulers/compliance/summarizer.go index 48883d123..67f4fa16b 100644 --- a/services/describe/schedulers/compliance/summarizer.go +++ b/services/describe/schedulers/compliance/summarizer.go @@ -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 @@ -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 @@ -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, diff --git a/services/describe/server.go b/services/describe/server.go index 147350007..597fc374c 100644 --- a/services/describe/server.go +++ b/services/describe/server.go @@ -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) }