1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Utilize new set type more.

This commit is contained in:
Sascha L. Teichmann 2023-06-15 14:35:51 +02:00
parent 71a3c3a13b
commit c6d0e9a9e2
2 changed files with 21 additions and 20 deletions

View file

@ -24,7 +24,7 @@ type rolieLabelChecker struct {
feedURL string
feedLabel csaf.TLPLabel
advisories map[csaf.TLPLabel]map[string]struct{}
advisories map[csaf.TLPLabel]util.Set[string]
}
// tlpLevel returns an inclusion order of TLP colors.
@ -68,10 +68,10 @@ func (ca *rolieLabelChecker) check(
// Associate advisory label to urls.
advs := ca.advisories[advisoryLabel]
if advs == nil {
advs = make(map[string]struct{})
advs = util.Set[string]{}
ca.advisories[advisoryLabel] = advs
}
advs[advisory] = struct{}{}
advs.Add(advisory)
// If entry shows up in feed of higher tlp level,
// give out info or warning
@ -170,7 +170,7 @@ func (p *processor) processROLIEFeeds(feeds [][]csaf.Feed) error {
p.labelChecker = &rolieLabelChecker{
feedURL: feedURL.String(),
feedLabel: label,
advisories: map[csaf.TLPLabel]map[string]struct{}{},
advisories: map[csaf.TLPLabel]util.Set[string]{},
}
if err := p.integrity(files, feedBase, rolieMask, p.badProviderMetadata.add); err != nil {
@ -183,7 +183,7 @@ func (p *processor) processROLIEFeeds(feeds [][]csaf.Feed) error {
// Phase 3: Check for completeness.
hasSummary := map[csaf.TLPLabel]struct{}{}
hasSummary := util.Set[csaf.TLPLabel]{}
var (
hasUnlabeled = false
@ -222,18 +222,19 @@ func (p *processor) processROLIEFeeds(feeds [][]csaf.Feed) error {
}
reference := p.labelChecker.advisories[label]
advisories := make(map[string]struct{}, len(reference))
advisories := make(util.Set[string], len(reference))
for _, adv := range files {
u, err := url.Parse(adv.URL())
if err != nil {
p.badProviderMetadata.error("Invalid URL %s in feed: %v.", *feed.URL, err)
p.badProviderMetadata.error(
"Invalid URL %s in feed: %v.", *feed.URL, err)
continue
}
advisories[makeAbs(u).String()] = struct{}{}
}
if containsAllKeys(reference, advisories) {
hasSummary[label] = struct{}{}
if advisories.ContainsAll(reference) {
hasSummary.Add(label)
}
}
}
@ -252,7 +253,7 @@ func (p *processor) processROLIEFeeds(feeds [][]csaf.Feed) error {
csaf.TLPLabelAmber,
csaf.TLPLabelRed,
} {
if _, ok := hasSummary[label]; !ok && len(p.labelChecker.advisories[label]) > 0 {
if hasSummary.Contains(label) && len(p.labelChecker.advisories[label]) > 0 {
p.badROLIEFeed.warn(
"ROLIE feed for TLP:%s has no accessible listed feed covering all advisories.",
label)
@ -262,16 +263,6 @@ func (p *processor) processROLIEFeeds(feeds [][]csaf.Feed) error {
return nil
}
// containsAllKeys returns if m2 contains all keys of m1.
func containsAllKeys[K comparable, V any](m1, m2 map[K]V) bool {
for k := range m1 {
if _, ok := m2[k]; !ok {
return false
}
}
return true
}
// categoryCheck checks for the existence of a feeds ROLIE category document and if it does,
// whether the category document contains distinguishing categories
func (p *processor) categoryCheck(folderURL string, label csaf.TLPLabel) error {

View file

@ -41,3 +41,13 @@ func (s Set[K]) Difference(t Set[K]) Set[K] {
}
return d
}
// ContainsAll returns true if all keys of a given set are in this set.
func (s Set[K]) ContainsAll(t Set[K]) bool {
for k := range t {
if !s.Contains(k) {
return false
}
}
return true
}