@@ -48,9 +48,11 @@ import (
48
48
constants2 "github.com/devtron-labs/devtron/pkg/pipeline/constants"
49
49
"github.com/devtron-labs/devtron/pkg/pipeline/executors"
50
50
repository2 "github.com/devtron-labs/devtron/pkg/plugin/repository"
51
+ security2 "github.com/devtron-labs/devtron/pkg/security"
51
52
"github.com/devtron-labs/devtron/pkg/sql"
52
53
"github.com/devtron-labs/devtron/pkg/workflow/cd"
53
54
bean4 "github.com/devtron-labs/devtron/pkg/workflow/cd/bean"
55
+ "github.com/devtron-labs/devtron/pkg/workflow/dag/adaptor"
54
56
bean2 "github.com/devtron-labs/devtron/pkg/workflow/dag/bean"
55
57
"github.com/devtron-labs/devtron/pkg/workflow/dag/helper"
56
58
error2 "github.com/devtron-labs/devtron/util/error"
@@ -128,6 +130,7 @@ type WorkflowDagExecutorImpl struct {
128
130
deploymentConfigService common2.DeploymentConfigService
129
131
asyncRunnable * async.Runnable
130
132
scanHistoryRepository security.ImageScanHistoryRepository
133
+ imageScanService security2.ImageScanService
131
134
}
132
135
133
136
func NewWorkflowDagExecutorImpl (Logger * zap.SugaredLogger , pipelineRepository pipelineConfig.PipelineRepository ,
@@ -152,7 +155,9 @@ func NewWorkflowDagExecutorImpl(Logger *zap.SugaredLogger, pipelineRepository pi
152
155
commonArtifactService artifacts.CommonArtifactService ,
153
156
deploymentConfigService common2.DeploymentConfigService ,
154
157
asyncRunnable * async.Runnable ,
155
- scanHistoryRepository security.ImageScanHistoryRepository ) * WorkflowDagExecutorImpl {
158
+ scanHistoryRepository security.ImageScanHistoryRepository ,
159
+ imageScanService security2.ImageScanService ,
160
+ ) * WorkflowDagExecutorImpl {
156
161
wde := & WorkflowDagExecutorImpl {logger : Logger ,
157
162
pipelineRepository : pipelineRepository ,
158
163
cdWorkflowRepository : cdWorkflowRepository ,
@@ -177,6 +182,7 @@ func NewWorkflowDagExecutorImpl(Logger *zap.SugaredLogger, pipelineRepository pi
177
182
deploymentConfigService : deploymentConfigService ,
178
183
asyncRunnable : asyncRunnable ,
179
184
scanHistoryRepository : scanHistoryRepository ,
185
+ imageScanService : imageScanService ,
180
186
}
181
187
config , err := types .GetCdConfig ()
182
188
if err != nil {
@@ -565,14 +571,20 @@ func (impl *WorkflowDagExecutorImpl) HandlePreStageSuccessEvent(triggerContext t
565
571
return err
566
572
}
567
573
scanEnabled , scanned := ciArtifact .ScanEnabled , ciArtifact .Scanned
568
- err = impl .handleScanningEventForArtifact (ciArtifact , cdStageCompleteEvent .CdPipelineId , repository4 .PIPELINE_STAGE_TYPE_PRE_CD )
574
+ isScanPluginConfigured , isScanningDoneViaPlugin , err : = impl .isArtifactScannedByPluginForPipeline (ciArtifact , cdStageCompleteEvent .CdPipelineId , repository4 .PIPELINE_STAGE_TYPE_PRE_CD , bean2 . ImageScanningPluginToCheckInPipelineStageStep )
569
575
if err != nil {
570
576
impl .logger .Errorw ("error in handling scanning event for ci artifact" , "ciArtifact" , ciArtifact , "err" , err )
571
577
return err
572
578
}
579
+ if isScanPluginConfigured {
580
+ ciArtifact .ScanEnabled = true
581
+ }
582
+ if isScanningDoneViaPlugin {
583
+ ciArtifact .Scanned = true
584
+ }
573
585
// if ciArtifact scanEnabled and scanned state changed from above func then update ciArtifact
574
586
if scanEnabled != ciArtifact .ScanEnabled || scanned != ciArtifact .Scanned {
575
- ciArtifact , err = impl .ciArtifactRepository .Update (ciArtifact )
587
+ err = impl .ciArtifactRepository .Update (ciArtifact )
576
588
if err != nil {
577
589
impl .logger .Errorw ("error in updating ci artifact after handling scan event for this artifact" , "ciArtifact" , ciArtifact , "err" , err )
578
590
return err
@@ -672,14 +684,20 @@ func (impl *WorkflowDagExecutorImpl) HandlePostStageSuccessEvent(triggerContext
672
684
return err
673
685
}
674
686
scanEnabled , scanned := ciArtifact .ScanEnabled , ciArtifact .Scanned
675
- err = impl .handleScanningEventForArtifact (ciArtifact , cdPipelineId , repository4 .PIPELINE_STAGE_TYPE_POST_CD )
687
+ isScanPluginConfigured , isScanningDoneViaPlugin , err : = impl .isArtifactScannedByPluginForPipeline (ciArtifact , cdPipelineId , repository4 .PIPELINE_STAGE_TYPE_POST_CD , bean2 . ImageScanningPluginToCheckInPipelineStageStep )
676
688
if err != nil {
677
689
impl .logger .Errorw ("error in handling scanning event for ci artifact" , "ciArtifact" , ciArtifact , "err" , err )
678
690
return err
679
691
}
692
+ if isScanPluginConfigured {
693
+ ciArtifact .ScanEnabled = true
694
+ }
695
+ if isScanningDoneViaPlugin {
696
+ ciArtifact .Scanned = true
697
+ }
680
698
// if ciArtifact scanEnabled and scanned state changed from above func then update ciArtifact
681
699
if scanEnabled != ciArtifact .ScanEnabled || scanned != ciArtifact .Scanned {
682
- ciArtifact , err = impl .ciArtifactRepository .Update (ciArtifact )
700
+ err = impl .ciArtifactRepository .Update (ciArtifact )
683
701
if err != nil {
684
702
impl .logger .Errorw ("error in updating ci artifact after handling scan event for this artifact" , "ciArtifact" , ciArtifact , "err" , err )
685
703
return err
@@ -744,47 +762,20 @@ func (impl *WorkflowDagExecutorImpl) UpdateCiWorkflowForCiSuccess(request *bean2
744
762
return nil
745
763
}
746
764
747
- func (impl * WorkflowDagExecutorImpl ) isScanPluginConfiguredAtPipelineStage (pipelineId int , pipelineStage repository4.PipelineStageType ) (bool , error ) {
748
- plugin , err := impl .globalPluginRepository .GetPluginByName (bean2 .ImageScanningPluginToCheckInPipelineStageStep )
749
- if err != nil {
750
- impl .logger .Errorw ("error in getting image scanning plugin, Vulnerability Scanning" , "pipelineId" , pipelineId , "pipelineStage" , pipelineStage , "err" , err )
751
- return false , err
752
- }
753
- if len (plugin ) == 0 {
754
- return false , nil
755
- }
756
- isScanPluginConfigured , err := impl .pipelineStageRepository .CheckIfPluginExistsInPipelineStage (pipelineId , pipelineStage , plugin [0 ].Id )
757
- if err != nil && ! util .IsErrNoRows (err ) {
758
- impl .logger .Errorw ("error in getting ci pipeline plugin" , "err" , err , "pipelineId" , pipelineId , "pluginId" , plugin [0 ].Id )
759
- return false , err
760
- }
761
- return isScanPluginConfigured , nil
762
- }
765
+ func (impl * WorkflowDagExecutorImpl ) isArtifactScannedByPluginForPipeline (ciArtifact * repository.CiArtifact , pipelineId int ,
766
+ pipelineStage repository4.PipelineStageType , pluginName string ) (bool , bool , error ) {
763
767
764
- func (impl * WorkflowDagExecutorImpl ) handleScanningEventForArtifact (ciArtifact * repository.CiArtifact , pipelineId int ,
765
- pipelineStage repository4.PipelineStageType ) error {
766
-
767
- isScanPluginConfigured , err := impl .isScanPluginConfiguredAtPipelineStage (pipelineId , pipelineStage )
768
+ isScanPluginConfigured , err := impl .pipelineStageService .IsScanPluginConfiguredAtPipelineStage (pipelineId , pipelineStage , pluginName )
768
769
if err != nil {
769
770
impl .logger .Errorw ("error in fetching if a scan plugin is configured or not in a pipeline" , "pipelineStage" , pipelineStage , "ciArtifact" , ciArtifact )
770
- return err
771
+ return false , false , err
771
772
}
772
- if isScanPluginConfigured {
773
- ciArtifact .ScanEnabled = true
774
- // if scan history is present for this artifact, then this image has been scanned
775
- // else there was some issue with the scanning plugin completing its job.
776
- _ , err := impl .scanHistoryRepository .FindByImageAndDigest (ciArtifact .ImageDigest , ciArtifact .Image )
777
- if err != nil && ! util .IsErrNoRows (err ) {
778
- impl .logger .Errorw ("error while fetching latest image scan execution history for image and image digest" , "image" , ciArtifact .Image , "imageDigest" , ciArtifact .ImageDigest , "err" , err )
779
- return err
780
- } else if util .IsErrNoRows (err ) {
781
- //scan history not found for image and digest hence marking scanned as false
782
- ciArtifact .Scanned = false
783
- } else {
784
- ciArtifact .Scanned = true
785
- }
773
+ isScanningDone , err := impl .imageScanService .IsImageScanExecutionCompleted (ciArtifact .Image , ciArtifact .ImageDigest )
774
+ if err != nil {
775
+ impl .logger .Errorw ("error in checking if image scanning is completed or not" , "image" , ciArtifact .Image , "imageDigest" , ciArtifact .ImageDigest )
776
+ return false , false , err
786
777
}
787
- return nil
778
+ return isScanPluginConfigured , isScanningDone , nil
788
779
}
789
780
790
781
func (impl * WorkflowDagExecutorImpl ) HandleCiSuccessEvent (triggerContext triggerBean.TriggerContext , ciPipelineId int , request * bean2.CiArtifactWebhookRequest , imagePushedAt time.Time ) (id int , err error ) {
@@ -807,21 +798,31 @@ func (impl *WorkflowDagExecutorImpl) HandleCiSuccessEvent(triggerContext trigger
807
798
if ! imagePushedAt .IsZero () {
808
799
createdOn = imagePushedAt
809
800
}
810
- buildArtifact := helper .GetBuildArtifact (request , pipelineModal .Id , materialJson , createdOn , updatedOn )
801
+ buildArtifact := adaptor .GetBuildArtifact (request , pipelineModal .Id , materialJson , createdOn , updatedOn )
811
802
803
+ // image scanning plugin can only be applied in Post-ci, scanning in pre-ci doesn't make sense
812
804
pipelineStage := repository4 .PIPELINE_STAGE_TYPE_POST_CI
813
805
if pipelineModal .PipelineType == constants2 .CI_JOB .ToString () {
814
806
pipelineStage = repository4 .PIPELINE_STAGE_TYPE_PRE_CI
815
807
}
816
- err = impl .handleScanningEventForArtifact (buildArtifact , pipelineModal .Id , pipelineStage )
817
- if err != nil {
818
- impl .logger .Errorw ("error in handling scanning event for this ci artifact" , "ciArtifact" , buildArtifact , "err" , err )
819
- return 0 , err
820
- }
808
+ // this flag comes from ci-runner when scanning is enabled from ciPipeline modal
821
809
if request .IsScanEnabled {
822
810
buildArtifact .Scanned = true
823
811
buildArtifact .ScanEnabled = true
812
+ } else {
813
+ isScanPluginConfigured , isScanningDoneViaPlugin , err := impl .isArtifactScannedByPluginForPipeline (buildArtifact , pipelineModal .Id , pipelineStage , bean2 .ImageScanningPluginToCheckInPipelineStageStep )
814
+ if err != nil {
815
+ impl .logger .Errorw ("error in handling scanning event for this ci artifact" , "ciArtifact" , buildArtifact , "err" , err )
816
+ return 0 , err
817
+ }
818
+ if isScanPluginConfigured {
819
+ buildArtifact .ScanEnabled = true
820
+ }
821
+ if isScanningDoneViaPlugin {
822
+ buildArtifact .Scanned = true
823
+ }
824
824
}
825
+
825
826
if err = impl .ciArtifactRepository .Save (buildArtifact ); err != nil {
826
827
impl .logger .Errorw ("error in saving material" , "err" , err )
827
828
return 0 , err
0 commit comments