Skip to content

Commit

Permalink
internal/worker: refactor row creation to make it simpler
Browse files Browse the repository at this point in the history
Change-Id: I1deb885c40413b76d745290719a2e423658d06bd
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/589077
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Maceo Thompson <[email protected]>
  • Loading branch information
zpavlinovic committed Jun 18, 2024
1 parent 4bc5480 commit 31d77f9
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions internal/worker/govulncheck_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,12 @@ func (s *scanner) ScanModule(ctx context.Context, w http.ResponseWriter, sreq *g
info, err := s.proxyClient.Info(ctx, sreq.Module, sreq.Version)
if err != nil {
log.Infof(ctx, "proxy error: %s@%s %v", sreq.Path(), sreq.Version, err)
// return row with proxy error for each appropriate scan mode
// based on the ecosystem metrics mode.
var scanModes []string
if sreq.Mode == ModeCompare {
scanModes = []string{scanModeCompareBinary, scanModeCompareSource}
} else if sreq.Mode == ModeGovulncheck {
scanModes = []string{scanModeSourceSymbol, scanModeSourcePackage, scanModeSourceModule}
}
var rows []bigquery.Row
for _, sm := range scanModes {
rows := createRows(sreq.Mode, func(sm string) *govulncheck.Result {
row := *baseRow
row.ScanMode = sm
row.AddError(fmt.Errorf("%v: %w", err, derrors.ProxyError))
rows = append(rows, &row)
}
return &row
})
return nil, writeResults(ctx, sreq.Serve, w, s.bqClient, govulncheck.TableName, rows)
}
baseRow.Version = info.Version
Expand Down Expand Up @@ -384,9 +375,7 @@ func (s *scanner) CheckModule(ctx context.Context, w http.ResponseWriter, sreq *
}
}

// create a row for each precision level, error or not
var rows []bigquery.Row
for _, sm := range []string{scanModeSourceSymbol, scanModeSourcePackage, scanModeSourceModule} {
rows := createRows(sreq.Mode, func(sm string) *govulncheck.Result {
row := *baseRow
row.ScanMode = sm

Expand All @@ -405,8 +394,8 @@ func (s *scanner) CheckModule(ctx context.Context, w http.ResponseWriter, sreq *
row.Vulns = vulnsForScanMode(response, sm)
log.Infof(ctx, "scanner.runScanModule returned %d findings for %s with row.Vulns=%d in scan mode=%s", len(response.Findings), sreq.Path(), len(row.Vulns), sm)
}
rows = append(rows, &row)
}
return &row
})

if err := writeResults(ctx, sreq.Serve, w, s.bqClient, govulncheck.TableName, rows); err != nil {
return nil, err
Expand Down Expand Up @@ -450,7 +439,23 @@ func vulnsForScanMode(response *govulncheck.AnalysisResponse, scanMode string) [
return vulns
}

// runScanModule fetches the module version from the proxy, and analyzes its source
// createRows creates a row, using f, for each scanMode associated
// with ecosystem metrics mode.
func createRows(mode string, f func(string) *govulncheck.Result) []bigquery.Row {
var scanModes []string
if mode == ModeCompare {
scanModes = []string{scanModeCompareBinary, scanModeCompareSource}
} else if mode == ModeGovulncheck {
scanModes = []string{scanModeSourceSymbol, scanModeSourcePackage, scanModeSourceModule}
}

var rows []bigquery.Row
for _, sm := range scanModes {
rows = append(rows, f(sm))
}
return rows
}

// code for vulnerabilities. The analysis of binaries is done in CompareModule.
func (s *scanner) runScanModule(ctx context.Context, modulePath, version, mode string) (response *govulncheck.AnalysisResponse, err error) {
err = doScan(ctx, modulePath, version, s.insecure, func() (err error) {
Expand Down

0 comments on commit 31d77f9

Please sign in to comment.