github.com/quay/claircore@v1.5.28/affectedmanifests.go (about) 1 package claircore 2 3 import ( 4 "sort" 5 "sync" 6 ) 7 8 // AffectedManifests describes a set of manifests affected by 9 // a set of Vulnerabilities. 10 type AffectedManifests struct { 11 mu sync.Mutex 12 // map of vulnerabilities keyed by the vulnerability's ID 13 Vulnerabilities map[string]*Vulnerability `json:"vulnerabilities"` 14 // map associating a list of vulnerability ids keyed by the 15 // manifest hash they affect. 16 VulnerableManifests map[string][]string `json:"vulnerable_manifests"` 17 } 18 19 // NewAffectedManifests initializes a new AffectedManifests struct. 20 func NewAffectedManifests() AffectedManifests { 21 return AffectedManifests{ 22 Vulnerabilities: make(map[string]*Vulnerability), 23 VulnerableManifests: make(map[string][]string), 24 } 25 } 26 27 // Add will add the provided Vulnerability and Manifest digest 28 // to the necessary maps. 29 // 30 // Add is safe to use by multiple goroutines. 31 func (a *AffectedManifests) Add(v *Vulnerability, digests ...Digest) { 32 a.mu.Lock() 33 a.Vulnerabilities[v.ID] = v 34 for _, d := range digests { 35 hash := d.String() 36 a.VulnerableManifests[hash] = append(a.VulnerableManifests[hash], v.ID) 37 } 38 a.mu.Unlock() 39 } 40 41 // Sort will sort each array in the VulnerableManifests map 42 // by Vulnerability.NormalizedSeverity in Desc order. 43 // 44 // Sort is safe to use by multiple goroutines. 45 func (a *AffectedManifests) Sort() { 46 a.mu.Lock() 47 for _, ids := range a.VulnerableManifests { 48 sort.Slice(ids, func(i, j int) bool { 49 id1, id2 := ids[i], ids[j] 50 v1, v2 := a.Vulnerabilities[id1], a.Vulnerabilities[id2] 51 // reverse this since we want descending sort 52 return v1.NormalizedSeverity > v2.NormalizedSeverity 53 }) 54 } 55 a.mu.Unlock() 56 }