-
Notifications
You must be signed in to change notification settings - Fork 540
/
match_exclusion_provider.go
62 lines (49 loc) · 1.65 KB
/
match_exclusion_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package db
import (
"fmt"
grypeDB "github.com/anchore/grype/grype/db/v5"
"github.com/anchore/grype/grype/match"
)
var _ match.ExclusionProvider = (*MatchExclusionProvider)(nil)
type MatchExclusionProvider struct {
reader grypeDB.VulnerabilityMatchExclusionStoreReader
}
func NewMatchExclusionProvider(reader grypeDB.VulnerabilityMatchExclusionStoreReader) *MatchExclusionProvider {
return &MatchExclusionProvider{
reader: reader,
}
}
func buildIgnoreRulesFromMatchExclusion(e grypeDB.VulnerabilityMatchExclusion) []match.IgnoreRule {
var ignoreRules []match.IgnoreRule
if len(e.Constraints) == 0 {
ignoreRules = append(ignoreRules, match.IgnoreRule{Vulnerability: e.ID})
return ignoreRules
}
for _, c := range e.Constraints {
ignoreRules = append(ignoreRules, match.IgnoreRule{
Vulnerability: e.ID,
Namespace: c.Vulnerability.Namespace,
FixState: string(c.Vulnerability.FixState),
Package: match.IgnoreRulePackage{
Name: c.Package.Name,
Language: c.Package.Language,
Type: c.Package.Type,
Location: c.Package.Location,
Version: c.Package.Version,
},
})
}
return ignoreRules
}
func (pr *MatchExclusionProvider) GetRules(vulnerabilityID string) ([]match.IgnoreRule, error) {
matchExclusions, err := pr.reader.GetVulnerabilityMatchExclusion(vulnerabilityID)
if err != nil {
return nil, fmt.Errorf("match exclusion provider failed to fetch records for vulnerability id='%s': %w", vulnerabilityID, err)
}
var ignoreRules []match.IgnoreRule
for _, e := range matchExclusions {
rules := buildIgnoreRulesFromMatchExclusion(e)
ignoreRules = append(ignoreRules, rules...)
}
return ignoreRules, nil
}