Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Bugs fixes (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishkerez authored Mar 7, 2023
1 parent ba18852 commit e6e685c
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 36 deletions.
16 changes: 15 additions & 1 deletion backend/pkg/database/gorm/dbtorest.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func ConvertToRestScanResult(scanResult ScanResult) (models.TargetScanResult, er
return ret, fmt.Errorf("failed to unmarshal json: %w", err)
}
}
if scanResult.Summary != nil {
ret.Summary = &models.TargetScanResultSummary{}
if err := json.Unmarshal(scanResult.Summary, ret.Summary); err != nil {
return ret, fmt.Errorf("failed to unmarshal json: %w", err)
}
}
ret.Id = utils.StringPtr(scanResult.ID.String())
ret.ScanId = scanResult.ScanID
ret.TargetId = scanResult.TargetID
Expand Down Expand Up @@ -164,11 +170,19 @@ func ConvertToRestScan(scan Scan) (models.Scan, error) {
return ret, fmt.Errorf("failed to unmarshal json: %w", err)
}
}
if scan.Summary != nil {
ret.Summary = &models.ScanSummary{}
if err := json.Unmarshal(scan.Summary, ret.Summary); err != nil {
return ret, fmt.Errorf("failed to unmarshal json: %w", err)
}
}

ret.Id = utils.StringPtr(scan.ID.String())
ret.StartTime = scan.ScanStartTime
ret.EndTime = scan.ScanEndTime
ret.ScanConfig = &models.ScanConfigRelationship{Id: *scan.ScanConfigID}
if scan.ScanConfigID != nil {
ret.ScanConfig = &models.ScanConfigRelationship{Id: *scan.ScanConfigID}
}

return ret, nil
}
Expand Down
14 changes: 14 additions & 0 deletions backend/pkg/database/gorm/resttodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ func ConvertToDBScanResult(result models.TargetScanResult) (ScanResult, error) {
}
}

if result.Summary != nil {
ret.Summary, err = json.Marshal(result.Summary)
if err != nil {
return ret, fmt.Errorf("failed to marshal json: %w", err)
}
}

ret.Base = Base{ID: scanResultUUID}

return ret, nil
Expand Down Expand Up @@ -166,6 +173,13 @@ func ConvertToDBScan(scan models.Scan) (Scan, error) {
}
}

if scan.Summary != nil {
ret.Summary, err = json.Marshal(scan.Summary)
if err != nil {
return ret, fmt.Errorf("failed to marshal json: %w", err)
}
}

ret.Base = Base{ID: scanUUID}

return ret, nil
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/database/gorm/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Scan struct {

// TargetIDs List of target IDs that are targeted for scanning as part of this scan
TargetIDs []byte `json:"target_ids,omitempty" gorm:"column:target_ids"`
Summary []byte `json:"summary,omitempty" gorm:"column:summary"`
}

type GetScansParams struct {
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/database/gorm/scan_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ScanResult struct {
Sboms []byte `json:"sboms,omitempty" gorm:"column:sboms"`
Secrets []byte `json:"secrets,omitempty" gorm:"column:secrets"`
Status []byte `json:"status,omitempty" gorm:"column:status"`
Summary []byte `json:"summary,omitempty" gorm:"column:summary"`
Vulnerabilities []byte `json:"vulnerabilities,omitempty" gorm:"column:vulnerabilities"`
}

Expand Down
55 changes: 21 additions & 34 deletions runtime_scan/pkg/orchestrator/configwatcher/scan_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,7 @@ func (scw *ScanConfigWatcher) initNewScan(ctx context.Context, scanConfig *model
},
StartTime: &now,
State: utils.PointerTo[models.ScanState](models.Pending),
Summary: &models.ScanSummary{
JobsCompleted: utils.PointerTo[int](0),
JobsLeftToRun: utils.PointerTo[int](0),
TotalExploits: utils.PointerTo[int](0),
TotalMalware: utils.PointerTo[int](0),
TotalMisconfigurations: utils.PointerTo[int](0),
TotalPackages: utils.PointerTo[int](0),
TotalRootkits: utils.PointerTo[int](0),
TotalSecrets: utils.PointerTo[int](0),
TotalVulnerabilities: &models.VulnerabilityScanSummary{
TotalCriticalVulnerabilities: utils.PointerTo[int](0),
TotalHighVulnerabilities: utils.PointerTo[int](0),
TotalLowVulnerabilities: utils.PointerTo[int](0),
TotalMediumVulnerabilities: utils.PointerTo[int](0),
TotalNegligibleVulnerabilities: utils.PointerTo[int](0),
},
},
Summary: createInitScanSummary(),
}
scanID, err := scw.createScan(ctx, scan)
if err != nil {
Expand All @@ -106,23 +90,6 @@ func (scw *ScanConfigWatcher) initNewScan(ctx context.Context, scanConfig *model
TargetIDs: targetIds,
State: utils.PointerTo[models.ScanState](models.Discovered),
StateMessage: utils.PointerTo[string]("Targets for scan successfully discovered"),
Summary: &models.ScanSummary{
JobsCompleted: utils.PointerTo[int](0),
JobsLeftToRun: utils.PointerTo[int](0),
TotalExploits: utils.PointerTo[int](0),
TotalMalware: utils.PointerTo[int](0),
TotalMisconfigurations: utils.PointerTo[int](0),
TotalPackages: utils.PointerTo[int](0),
TotalRootkits: utils.PointerTo[int](0),
TotalSecrets: utils.PointerTo[int](0),
TotalVulnerabilities: &models.VulnerabilityScanSummary{
TotalCriticalVulnerabilities: utils.PointerTo[int](0),
TotalHighVulnerabilities: utils.PointerTo[int](0),
TotalMediumVulnerabilities: utils.PointerTo[int](0),
TotalLowVulnerabilities: utils.PointerTo[int](0),
TotalNegligibleVulnerabilities: utils.PointerTo[int](0),
},
},
}
scanID, err = scw.patchScan(ctx, scanID, scan)
if err != nil {
Expand All @@ -132,6 +99,26 @@ func (scw *ScanConfigWatcher) initNewScan(ctx context.Context, scanConfig *model
return targetInstances, scanID, nil
}

func createInitScanSummary() *models.ScanSummary {
return &models.ScanSummary{
JobsCompleted: utils.PointerTo(0),
JobsLeftToRun: utils.PointerTo(0),
TotalExploits: utils.PointerTo(0),
TotalMalware: utils.PointerTo(0),
TotalMisconfigurations: utils.PointerTo(0),
TotalPackages: utils.PointerTo(0),
TotalRootkits: utils.PointerTo(0),
TotalSecrets: utils.PointerTo(0),
TotalVulnerabilities: &models.VulnerabilityScanSummary{
TotalCriticalVulnerabilities: utils.PointerTo(0),
TotalHighVulnerabilities: utils.PointerTo(0),
TotalLowVulnerabilities: utils.PointerTo(0),
TotalMediumVulnerabilities: utils.PointerTo(0),
TotalNegligibleVulnerabilities: utils.PointerTo(0),
},
}
}

func getTargetIDs(targetInstances []*types.TargetInstance) *[]string {
ret := make([]string, len(targetInstances))
for i, targetInstance := range targetInstances {
Expand Down
22 changes: 21 additions & 1 deletion runtime_scan/pkg/scanner/job_managment.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s *Scanner) jobBatchManagement(ctx context.Context) {
scanComplete := false
for !scanComplete {
var scan *models.Scan
var err error
select {
case targetID := <-done:
numberOfCompletedJobs := numberOfCompletedJobs + 1
Expand All @@ -139,7 +140,7 @@ func (s *Scanner) jobBatchManagement(ctx context.Context) {
anyJobsFailed = true
}

scan, err := s.createScanWithUpdatedSummary(ctx, *data)
scan, err = s.createScanWithUpdatedSummary(ctx, *data)
if err != nil {
log.WithFields(s.logFields).Errorf("Failed to create a scan with updated summary: %v", err)
scan = &models.Scan{}
Expand Down Expand Up @@ -567,6 +568,7 @@ func (s *Scanner) createInitTargetScanStatus(ctx context.Context, scanID, target
ScanId: scanID,
Status: initScanStatus,
TargetId: targetID,
Summary: createInitScanResultSummary(),
}
resp, err := s.backendClient.PostScanResultsWithResponse(ctx, scanResult)
if err != nil {
Expand Down Expand Up @@ -598,6 +600,24 @@ func (s *Scanner) createInitTargetScanStatus(ctx context.Context, scanID, target
}
}

func createInitScanResultSummary() *models.TargetScanResultSummary {
return &models.TargetScanResultSummary{
TotalExploits: runtimeScanUtils.PointerTo[int](0),
TotalMalware: runtimeScanUtils.PointerTo[int](0),
TotalMisconfigurations: runtimeScanUtils.PointerTo[int](0),
TotalPackages: runtimeScanUtils.PointerTo[int](0),
TotalRootkits: runtimeScanUtils.PointerTo[int](0),
TotalSecrets: runtimeScanUtils.PointerTo[int](0),
TotalVulnerabilities: &models.VulnerabilityScanSummary{
TotalCriticalVulnerabilities: runtimeScanUtils.PointerTo[int](0),
TotalHighVulnerabilities: runtimeScanUtils.PointerTo[int](0),
TotalMediumVulnerabilities: runtimeScanUtils.PointerTo[int](0),
TotalLowVulnerabilities: runtimeScanUtils.PointerTo[int](0),
TotalNegligibleVulnerabilities: runtimeScanUtils.PointerTo[int](0),
},
}
}

func getInitScanStatusVulnerabilitiesStateFromEnabled(config *models.VulnerabilitiesConfig) *models.TargetScanStateState {
if config == nil || config.Enabled == nil || !*config.Enabled {
return stateToPointer(models.NOTSCANNED)
Expand Down

0 comments on commit e6e685c

Please sign in to comment.