Skip to content

Commit 8bbbebd

Browse files
Merge pull request #68 from devtron-labs/fix-cve-logs
fix: corrected the logger format
2 parents d17123a + fbb4daf commit 8bbbebd

File tree

1 file changed

+28
-31
lines changed

1 file changed

+28
-31
lines changed

pkg/security/ImageScanService.go

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (impl *ImageScanServiceImpl) GetActiveTool() (*repository.ScanToolMetadata,
118118
//get active tool
119119
tool, err := impl.ScanToolMetadataRepository.FindActiveToolByScanTarget(repository.ImageScanTargetType)
120120
if err != nil {
121-
impl.Logger.Errorw("error in getting active tool by scan target", "err", err, "scanTarget", repository.ImageScanTargetType)
121+
impl.Logger.Errorw("error in getting active tool by scan target", "scanTarget", repository.ImageScanTargetType, "err", err)
122122
return nil, err
123123
}
124124
return tool, nil
@@ -168,7 +168,7 @@ func (impl *ImageScanServiceImpl) ScanImage(scanEvent *common.ImageScanEvent, to
168168
//checking if image is already scanned or not
169169
isImageScanned, err := impl.IsImageScanned(scanEvent.Image)
170170
if err != nil && err != pg.ErrNoRows {
171-
impl.Logger.Errorw("error in fetching scan history ", "err", err, "image", scanEvent.Image)
171+
impl.Logger.Errorw("error in fetching scan history ", "image", scanEvent.Image, "err", err)
172172
return err
173173
}
174174
if isImageScanned {
@@ -179,14 +179,14 @@ func (impl *ImageScanServiceImpl) ScanImage(scanEvent *common.ImageScanEvent, to
179179
if scanEvent.DockerConnection == common.SECUREWITHCERT {
180180
caCertFilePath, err = impl.createCaCertFile(scanEvent.DockerCert)
181181
if err != nil {
182-
impl.Logger.Errorw("error in creating cert file", "err", err, "image", scanEvent.Image)
182+
impl.Logger.Errorw("error in creating cert file", "image", scanEvent.Image, "err", err)
183183
return err
184184
}
185185
defer os.Remove(caCertFilePath)
186186
}
187187
imageScanRenderDto, err := impl.GetImageScanRenderDto(scanEvent.DockerRegistryId, scanEvent)
188188
if err != nil {
189-
impl.Logger.Errorw("service error, GetImageScanRenderDto", "err", err, "dockerRegistryId", scanEvent.DockerRegistryId)
189+
impl.Logger.Errorw("service error, GetImageScanRenderDto", "dockerRegistryId", scanEvent.DockerRegistryId, "err", err)
190190
return err
191191
}
192192
imageScanRenderDto.CaCertFilePath = caCertFilePath
@@ -195,7 +195,7 @@ func (impl *ImageScanServiceImpl) ScanImage(scanEvent *common.ImageScanEvent, to
195195
// TODO: if multiple processes are to be done in parallel, then error propagation should have to be done via channels
196196
err = impl.ScanImageForTool(tool, executionHistory.Id, executionHistoryDirPath, wg, int32(scanEvent.UserId), ctx, imageScanRenderDto)
197197
if err != nil {
198-
impl.Logger.Errorw("err in scanning image", "err", err, "tool", tool, "executionHistory.Id", executionHistory.Id, "executionHistoryDirPath", executionHistoryDirPath, "scanEvent.UserId", scanEvent.UserId)
198+
impl.Logger.Errorw("err in scanning image", "tool", tool, "executionHistory.Id", executionHistory.Id, "executionHistoryDirPath", executionHistoryDirPath, "scanEvent.UserId", scanEvent.UserId, "err", err)
199199
return err
200200
}
201201
wg.Wait()
@@ -205,7 +205,7 @@ func (impl *ImageScanServiceImpl) ScanImage(scanEvent *common.ImageScanEvent, to
205205
func (impl *ImageScanServiceImpl) GetImageScanRenderDto(registryId string, scanEvent *common.ImageScanEvent) (*common.ImageScanRenderDto, error) {
206206
dockerRegistry, err := impl.DockerArtifactStoreRepository.FindById(registryId)
207207
if err != nil {
208-
impl.Logger.Errorw("error in getting docker registry by id", "err", err, "id", registryId)
208+
impl.Logger.Errorw("error in getting docker registry by id", "id", registryId, "err", err)
209209
return nil, err
210210
}
211211
imageScanRenderDto := &common.ImageScanRenderDto{
@@ -226,7 +226,7 @@ func (impl *ImageScanServiceImpl) ScanImageForTool(tool *repository.ScanToolMeta
226226
var processedState bean.ScanExecutionProcessState
227227
err := impl.ProcessScanForTool(toolCopy, executionHistoryDirPathCopy, executionHistoryId, userId, ctx, imageScanRenderDto)
228228
if err != nil {
229-
impl.Logger.Errorw("error in processing scan for tool:", toolCopy.Name, "err", err)
229+
impl.Logger.Errorw("error in processing scan for tool:", "toolCopy Name", toolCopy.Name, "err", err)
230230
processedState = bean.ScanExecutionProcessStateFailed
231231
} else {
232232
processedState = bean.ScanExecutionProcessStateCompleted
@@ -252,7 +252,7 @@ func (impl *ImageScanServiceImpl) RegisterScanExecutionHistoryAndState(scanEvent
252252
executionTimeStart := time.Now()
253253
scanEventJson, err := json.Marshal(scanEvent)
254254
if err != nil {
255-
impl.Logger.Errorw("error in marshalling scanEvent", "err", err, "event", scanEvent)
255+
impl.Logger.Errorw("error in marshalling scanEvent", "event", scanEvent, "err", err)
256256
return nil, "", err
257257
}
258258
executionHistoryModel := &repository.ImageScanExecutionHistory{
@@ -271,7 +271,7 @@ func (impl *ImageScanServiceImpl) RegisterScanExecutionHistoryAndState(scanEvent
271271
defer tx.Rollback()
272272
err = impl.ScanHistoryRepository.Save(tx, executionHistoryModel)
273273
if err != nil {
274-
impl.Logger.Errorw("Failed to save executionHistory", "err", err, "model", executionHistoryModel)
274+
impl.Logger.Errorw("Failed to save executionHistory", "model", executionHistoryModel, "err", err)
275275
return nil, executionHistoryDirPath, err
276276
}
277277

@@ -284,15 +284,15 @@ func (impl *ImageScanServiceImpl) RegisterScanExecutionHistoryAndState(scanEvent
284284
if !isExist {
285285
err = os.Mkdir(bean.ScanOutputDirectory, commonUtil.DefaultFileCreatePermission)
286286
if err != nil && !os.IsExist(err) {
287-
impl.Logger.Errorw("error in creating Output directory", "err", err, "toolId", tool.Id, "executionHistoryDir", executionHistoryDirPath)
287+
impl.Logger.Errorw("error in creating Output directory", "toolId", tool.Id, "executionHistoryDir", executionHistoryDirPath, "err", err)
288288
return nil, executionHistoryDirPath, err
289289
}
290290
}
291291
// creating folder for storing output data for this execution history data
292292
executionHistoryDirPath = impl.CreateFolderForOutputData(executionHistoryModel.Id)
293293
err = os.Mkdir(executionHistoryDirPath, commonUtil.DefaultFileCreatePermission)
294294
if err != nil && !os.IsExist(err) {
295-
impl.Logger.Errorw("error in creating executionHistory directory", "err", err, "executionHistoryId", executionHistoryModel.Id)
295+
impl.Logger.Errorw("error in creating executionHistory directory", "executionHistoryId", executionHistoryModel.Id, "err", err)
296296
return nil, executionHistoryDirPath, err
297297
}
298298
executionHistoryMappingModel := &repository.ScanToolExecutionHistoryMapping{
@@ -334,13 +334,13 @@ func (impl *ImageScanServiceImpl) ProcessScanForTool(tool repository.ScanToolMet
334334
toolOutputDirPath := path.Join(executionHistoryDirPath, toolIdStr)
335335
err = os.Mkdir(toolOutputDirPath, commonUtil.DefaultFileCreatePermission)
336336
if err != nil && !os.IsExist(err) {
337-
impl.Logger.Errorw("error in creating toolOutput directory", "err", err, "toolId", tool.Id, "executionHistoryDir", executionHistoryDirPath)
337+
impl.Logger.Errorw("error in creating toolOutput directory", "toolId", tool.Id, "executionHistoryDir", executionHistoryDirPath, "err", err)
338338
return err
339339
}
340340
//getting all steps for this tool
341341
steps, err := impl.ScanToolStepRepository.FindAllByScanToolId(tool.Id)
342342
if err != nil {
343-
impl.Logger.Errorw("error in getting steps by scan tool id", "err", err, "toolId", tool.Id)
343+
impl.Logger.Errorw("error in getting steps by scan tool id", "toolId", tool.Id, "err", err)
344344
return err
345345
}
346346
//sorting steps on the basis of index
@@ -352,7 +352,7 @@ func (impl *ImageScanServiceImpl) ProcessScanForTool(tool repository.ScanToolMet
352352
// Getting and Setting the starting index based of first step for processing starting point on registry type and tool
353353
registryIndexMappingModel, err := impl.RegistryIndexMappingRepository.GetStartingIndexForARegistryAndATool(tool.Id, imageScanRenderDto.RegistryType)
354354
if err != nil {
355-
impl.Logger.Errorw("error in getting registry index mapping", "err", err, "RegistryType", imageScanRenderDto.RegistryType, "toolId", tool.Id)
355+
impl.Logger.Errorw("error in getting registry index mapping", "RegistryType", imageScanRenderDto.RegistryType, "toolId", tool.Id, "err", err)
356356
return err
357357
}
358358
stepProcessIndex = registryIndexMappingModel.Index
@@ -376,22 +376,22 @@ func (impl *ImageScanServiceImpl) ProcessScanForTool(tool repository.ScanToolMet
376376
if step.StepExecutionSync {
377377
output, err := impl.ProcessScanStep(step, tool, toolOutputDirPath, ctx, imageScanRenderDto)
378378
if err != nil {
379-
impl.Logger.Errorw("error in processing scan step sync", "err", err, "stepId", step.Id)
379+
impl.Logger.Errorw("error in processing scan step sync", "stepId", step.Id, "err", err)
380380
return err
381381
}
382382
if step.StepExecutionType == bean.ScanExecutionTypeCli && step.CliOutputType == cliUtil.CliOutPutTypeStream {
383383
// read output here for further processing, to update this logic when cli stream processing is made async
384384
outputFileName := path.Join(toolOutputDirPath, fmt.Sprintf("%d%s", step.Index, bean.JsonOutputFileNameSuffix))
385385
output, err = commonUtil.ReadFile(outputFileName)
386386
if err != nil {
387-
impl.Logger.Errorw("error in getting reading output of step", "err", err, "stepOutputFileName", outputFileName)
387+
impl.Logger.Errorw("error in getting reading output of step", "stepOutputFileName", outputFileName, "err", err)
388388
return err
389389
}
390390
}
391391

392392
isPassed, err := impl.CheckConditionsForAStep(step, output)
393393
if err != nil {
394-
impl.Logger.Errorw("error in checking conditions for step", "err", err, "stepId", step.Id)
394+
impl.Logger.Errorw("error in checking conditions for step", "stepId", step.Id, "err", err)
395395
return err
396396
}
397397
if !isPassed {
@@ -419,7 +419,7 @@ func (impl *ImageScanServiceImpl) ProcessScanForTool(tool repository.ScanToolMet
419419
//will not check if step is passed or failed
420420
_, err := impl.ProcessScanStep(step, tool, toolOutputDirPath, cxtx, nil)
421421
if err != nil {
422-
impl.Logger.Errorw("error in processing scan step async", "err", err, "stepId", step.Id)
422+
impl.Logger.Errorw("error in processing scan step async", "stepId", step.Id, "err", err)
423423
return
424424
}
425425
}()
@@ -465,17 +465,16 @@ func (impl *ImageScanServiceImpl) ProcessScanStep(step repository.ScanToolStep,
465465
func (impl *ImageScanServiceImpl) ConvertEndStepOutputAndSaveVulnerabilities(stepOutput []byte, executionHistoryId int, tool repository.ScanToolMetadata, step repository.ScanToolStep, userId int32) error {
466466
var vulnerabilities []*bean.ImageScanOutputObject
467467
var err error
468-
impl.Logger.Debugw("ConvertEndStepOutputAndSaveVulnerabilities", "stepOutput", string(stepOutput), "resultDescriptorTemplate", tool.ResultDescriptorTemplate)
469468
if isV1Template(tool.ResultDescriptorTemplate) { // result descriptor template is go template, go with v1 logic
470469
vulnerabilities, err = impl.getImageScanOutputObjectsV1(stepOutput, tool.ResultDescriptorTemplate)
471470
if err != nil {
472-
impl.Logger.Errorw("error, getImageScanOutputObjectsV1", "err", err, "stepOutput", stepOutput, "resultDescriptorTemplate", tool.ResultDescriptorTemplate)
471+
impl.Logger.Errorw("error, getImageScanOutputObjectsV1", "stepOutput", stepOutput, "resultDescriptorTemplate", tool.ResultDescriptorTemplate, "err", err)
473472
return err
474473
}
475474
} else { //not go template, go with v2 logic
476475
vulnerabilities, err = impl.getImageScanOutputObjectsV2(stepOutput, tool.ResultDescriptorTemplate)
477476
if err != nil {
478-
impl.Logger.Errorw("error, getImageScanOutputObjectsV2", "err", err, "stepOutput", stepOutput, "resultDescriptorTemplate", tool.ResultDescriptorTemplate)
477+
impl.Logger.Errorw("error, getImageScanOutputObjectsV2", "stepOutput", stepOutput, "resultDescriptorTemplate", tool.ResultDescriptorTemplate, "err", err)
479478
return err
480479
}
481480
}
@@ -597,7 +596,7 @@ func (impl *ImageScanServiceImpl) getImageScanOutputObjectsV2(stepOutput []byte,
597596
var mappings []map[string]interface{}
598597
err := json.Unmarshal([]byte(resultDescriptorTemplate), &mappings)
599598
if err != nil {
600-
impl.Logger.Errorw("error in un-marshaling result descriptor template", "err", err, "resultDescriptorTemplate", resultDescriptorTemplate)
599+
impl.Logger.Errorw("error in un-marshaling result descriptor template", "resultDescriptorTemplate", resultDescriptorTemplate, "err", err)
601600
return nil, err
602601
}
603602
var processArray func(mapping map[string]interface{}, value gjson.Result)
@@ -639,7 +638,7 @@ func (impl *ImageScanServiceImpl) getImageScanOutputObjectsV2(stepOutput []byte,
639638
}
640639
processArray(mapping, result)
641640
}
642-
impl.Logger.Debugw("received vulnerabilities", "vulnerabilites", vulnerabilities)
641+
643642
return vulnerabilities, nil
644643
}
645644

@@ -689,14 +688,14 @@ func (impl *ImageScanServiceImpl) RenderInputDataForAStep(inputPayloadTmpl strin
689688
metaDataMap := map[string]interface{}{}
690689
err := json.Unmarshal([]byte(toolMetaData), &metaDataMap)
691690
if err != nil {
692-
impl.Logger.Errorw("error in unmarshalling meta data ", "err", err, "toolMetaData", toolMetaData)
691+
impl.Logger.Errorw("error in unmarshalling meta data ", "toolMetaData", toolMetaData, "err", err)
693692
return nil, err
694693
}
695694
if outputStepIndex != bean.NullProcessIndex {
696695
outputFileName := path.Join(toolExecutionDirectoryPath, fmt.Sprintf("%d%s", outputStepIndex, bean.JsonOutputFileNameSuffix))
697696
outputFromStep, err := commonUtil.ReadFile(outputFileName)
698697
if err != nil {
699-
impl.Logger.Errorw("error in getting reading output of step", "err", err, "stepOutputFileName", outputFromStep)
698+
impl.Logger.Errorw("error in getting reading output of step", "stepOutputFileName", outputFromStep, "err", err)
700699
return nil, err
701700
}
702701
err = json.Unmarshal(outputFromStep, &jsonMap)
@@ -737,7 +736,6 @@ func (impl *ImageScanServiceImpl) CreateScanExecutionRegistryForClairV4(vs []*cl
737736
cvesToBeSaved := make([]*repository.CveStore, 0, len(vs))
738737
userId := int32(event.UserId)
739738
for _, item := range vs {
740-
impl.Logger.Debugw("vulnerability data", "vs", item)
741739
cveStore, err := impl.CveStoreRepository.FindByName(item.Name)
742740
if err != nil && err != pg.ErrNoRows {
743741
impl.Logger.Errorw("Failed to fetch cve", "err", err)
@@ -800,7 +798,6 @@ func (impl *ImageScanServiceImpl) CreateScanExecutionRegistryForClairV2(vs []*cl
800798
cvesToBeSaved := make([]*repository.CveStore, 0, len(vs))
801799
userId := int32(event.UserId)
802800
for _, item := range vs {
803-
impl.Logger.Debugw("vulnerability data", "vs", item)
804801
cveStore, err := impl.CveStoreRepository.FindByName(item.Name)
805802
if err != nil && err != pg.ErrNoRows {
806803
impl.Logger.Errorw("Failed to fetch cve", "err", err)
@@ -886,13 +883,13 @@ func (impl *ImageScanServiceImpl) CheckConditionsForAStep(step repository.ScanTo
886883
//get all conditions for a step
887884
conditions, err := impl.ScanStepConditionRepository.FindAllByToolStepId(step.Id)
888885
if err != nil {
889-
impl.Logger.Errorw("error in getting all conditions by step id", "err", err, "stepId", step.Id)
886+
impl.Logger.Errorw("error in getting all conditions by step id", "stepId", step.Id, "err", err)
890887
return false, err
891888
}
892889
for _, condition := range conditions {
893890
isPassedForCondition, err := impl.EvaluateCondition(*condition, stepOutput)
894891
if err != nil {
895-
impl.Logger.Errorw("error in evaluating condition", "err", err, "condition", condition)
892+
impl.Logger.Errorw("error in evaluating condition", "condition", condition, "err", err)
896893
return false, err
897894
}
898895
if !isPassedForCondition { //condition failed, will not check further
@@ -985,7 +982,7 @@ func (impl *ImageScanServiceImpl) HandleProgressingScans() {
985982
}
986983
imageScanRenderDto, err := impl.GetImageScanRenderDto(scanEvent.DockerRegistryId, &scanEvent)
987984
if err != nil {
988-
impl.Logger.Errorw("service error, GetImageScanRenderDto", "err", err, "dockerRegistryId", scanEvent.DockerRegistryId)
985+
impl.Logger.Errorw("service error, GetImageScanRenderDto", "dockerRegistryId", scanEvent.DockerRegistryId, "err", err)
989986
return
990987
}
991988
err = impl.ScanImageForTool(scanTool, scanHistory.ImageScanExecutionHistoryId, executionHistoryDirPath, wg, 1, context.Background(), imageScanRenderDto)
@@ -1000,7 +997,7 @@ func (impl *ImageScanServiceImpl) HandleProgressingScans() {
1000997
if flagForDeleting {
1001998
err = os.Remove(executionHistoryDirPath)
1002999
if err != nil {
1003-
impl.Logger.Errorw("error in deleting executionHistoryDirectory", "err", err, "executionHistoryDirPath", executionHistoryDirPath)
1000+
impl.Logger.Errorw("error in deleting executionHistoryDirectory", "executionHistoryDirPath", executionHistoryDirPath, "err", err)
10041001
}
10051002
}
10061003

0 commit comments

Comments
 (0)