|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package detectorrunner provides a Run function to help with running detectors |
| 16 | +package detectorrunner |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/google/osv-scalibr/detector" |
| 25 | + scalibrfs "github.com/google/osv-scalibr/fs" |
| 26 | + "github.com/google/osv-scalibr/packageindex" |
| 27 | + "github.com/google/osv-scalibr/plugin" |
| 28 | + "github.com/google/osv-scalibr/stats" |
| 29 | +) |
| 30 | + |
| 31 | +// Run runs the specified detectors and returns their findings, |
| 32 | +// as well as info about whether the plugin runs completed successfully. |
| 33 | +func Run(ctx context.Context, c stats.Collector, detectors []detector.Detector, scanRoot *scalibrfs.ScanRoot, index *packageindex.PackageIndex) ([]*detector.Finding, []*plugin.Status, error) { |
| 34 | + findings := []*detector.Finding{} |
| 35 | + status := []*plugin.Status{} |
| 36 | + for _, d := range detectors { |
| 37 | + if ctx.Err() != nil { |
| 38 | + return nil, nil, ctx.Err() |
| 39 | + } |
| 40 | + start := time.Now() |
| 41 | + results, err := d.Scan(ctx, scanRoot, index) |
| 42 | + c.AfterDetectorRun(d.Name(), time.Since(start), err) |
| 43 | + for _, f := range results { |
| 44 | + f.Detectors = []string{d.Name()} |
| 45 | + } |
| 46 | + findings = append(findings, results...) |
| 47 | + status = append(status, plugin.StatusFromErr(d, false, err)) |
| 48 | + } |
| 49 | + if err := validateAdvisories(findings); err != nil { |
| 50 | + return []*detector.Finding{}, status, err |
| 51 | + } |
| 52 | + return findings, status, nil |
| 53 | +} |
| 54 | + |
| 55 | +func validateAdvisories(findings []*detector.Finding) error { |
| 56 | + // Check that findings with the same advisory ID have identical advisories. |
| 57 | + ids := make(map[detector.AdvisoryID]detector.Advisory) |
| 58 | + for _, f := range findings { |
| 59 | + if f.Adv == nil { |
| 60 | + return fmt.Errorf("finding has no advisory set: %v", f) |
| 61 | + } |
| 62 | + if f.Adv.ID == nil { |
| 63 | + return fmt.Errorf("finding has no advisory ID set: %v", f) |
| 64 | + } |
| 65 | + if adv, ok := ids[*f.Adv.ID]; ok { |
| 66 | + if !reflect.DeepEqual(adv, *f.Adv) { |
| 67 | + return fmt.Errorf("multiple non-identical advisories with ID %v", f.Adv.ID) |
| 68 | + } |
| 69 | + } |
| 70 | + ids[*f.Adv.ID] = *f.Adv |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
0 commit comments