diff --git a/ee/app/services/security/store_scans_service.rb b/ee/app/services/security/store_scans_service.rb index 7d578b42cdad3fc96acf0073c1d74de42639dfa6..a7439782af9426136f4017e22d2f701a48f79102 100644 --- a/ee/app/services/security/store_scans_service.rb +++ b/ee/app/services/security/store_scans_service.rb @@ -17,6 +17,9 @@ def execute results = store_security_scans + store_sbom_scans + remove_dangling_dependency_scans + set_dependency_scanning_reports_to_ready + # StoreGroupedScansService returns true only when it creates a `security_scans` record. # To avoid resource wastage we are skipping the reports ingestion when there are no new scans. # @@ -46,14 +49,9 @@ def store_security_scans def store_sbom_scans return [] if sbom_report_artifacts.blank? - results = sbom_report_artifacts.map do |file_type, artifacts| + sbom_report_artifacts.map do |file_type, artifacts| StoreGroupedSbomScansService.execute(artifacts, pipeline, file_type) end - - remove_dangling_dependency_scans - set_dependency_scanning_reports_to_ready - - results end def already_purged? diff --git a/ee/spec/services/security/store_scans_service_spec.rb b/ee/spec/services/security/store_scans_service_spec.rb index e0618f0e64e97081bfbadae5c64d7213e78b9ae6..f09c8381b17d6101d4592e603038768bb543ff90 100644 --- a/ee/spec/services/security/store_scans_service_spec.rb +++ b/ee/spec/services/security/store_scans_service_spec.rb @@ -52,10 +52,8 @@ let_it_be(:sast_build) { create(:ee_ci_build, pipeline: pipeline) } let_it_be(:dast_build) { create(:ee_ci_build, pipeline: pipeline) } - let_it_be(:cyclonedx_build) { create(:ee_ci_build, :success, pipeline: pipeline) } let_it_be(:sast_artifact) { create(:ee_ci_job_artifact, :sast, job: sast_build) } let_it_be(:dast_artifact) { create(:ee_ci_job_artifact, :dast, job: dast_build) } - let_it_be(:cyclonedx_artifact) { create(:ee_ci_job_artifact, :cyclonedx, job: cyclonedx_build) } subject(:store_group_of_artifacts) { service_object.execute } @@ -87,8 +85,6 @@ expect(Security::StoreGroupedScansService).to have_received(:execute).with([sast_artifact], pipeline, 'sast') expect(Security::StoreGroupedScansService).to have_received(:execute).with([dast_artifact], pipeline, 'dast') - expect(Security::StoreGroupedSbomScansService).to have_received(:execute) - .with([cyclonedx_artifact], pipeline, 'dependency_scanning') end it 'does not schedule sbom ingestion when there are scans stored' do @@ -109,9 +105,41 @@ .with([sast_artifact], pipeline, 'sast') expect(Security::StoreGroupedScansService).not_to have_received(:execute) .with([dast_artifact], pipeline, 'dast') - expect(Security::StoreGroupedSbomScansService).not_to have_received(:execute) + end + end + + context 'when there is a dependency scanning SBoM' do + let_it_be(:cyclonedx_build) { create(:ee_ci_build, :success, pipeline: pipeline) } + let_it_be(:cyclonedx_artifact) { create(:ee_ci_job_artifact, :cyclonedx, job: cyclonedx_build) } + + it 'stores the sbom scans' do + store_group_of_artifacts + + expect(Security::StoreGroupedSbomScansService).to have_received(:execute) .with([cyclonedx_artifact], pipeline, 'dependency_scanning') end + + it 'marks dependency_scanning sbom reports as ready' do + expect(::Vulnerabilities::CompareSecurityReportsService).to receive(:set_security_report_type_to_ready) + .with( + pipeline_id: pipeline.id, + report_type: 'dependency_scanning' + ) + + store_group_of_artifacts + end + + context 'when there is a created dependency scan' do + let_it_be(:dependency_scan) do + create(:security_scan, build: cyclonedx_build, scan_type: :dependency_scanning, status: :created) + end + + it 'deletes the scan' do + expect { store_group_of_artifacts }.to change { + Security::Scan.exists?(dependency_scan.id) + }.from(true).to(false) + end + end end context 'with cyclonedx from a container scanning job' do @@ -125,8 +153,6 @@ expect(Security::StoreGroupedSbomScansService).not_to have_received(:execute) .with([cyclonedx_cs_artifact], pipeline, 'container_scanning') - expect(Security::StoreGroupedSbomScansService).to have_received(:execute) - .with([cyclonedx_artifact], pipeline, 'dependency_scanning') end it 'marks dependency_scanning sbom reports as ready' do