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

Add category support for aggregator

This commit is contained in:
Sascha L. Teichmann 2022-06-30 13:54:51 +02:00
parent 198e5b8897
commit 7bafb210cf
4 changed files with 83 additions and 8 deletions

View file

@ -36,8 +36,9 @@ type provider struct {
Name string `toml:"name"` Name string `toml:"name"`
Domain string `toml:"domain"` Domain string `toml:"domain"`
// Rate gives the provider specific rate limiting (see overall Rate). // Rate gives the provider specific rate limiting (see overall Rate).
Rate *float64 `toml:"rate"` Rate *float64 `toml:"rate"`
Insecure *bool `toml:"insecure"` Insecure *bool `toml:"insecure"`
Categories *[]string `toml:"categories"`
} }
type config struct { type config struct {
@ -50,6 +51,7 @@ type config struct {
// Rate gives the average upper limit of https operations per second. // Rate gives the average upper limit of https operations per second.
Rate *float64 `toml:"rate"` Rate *float64 `toml:"rate"`
Insecure *bool `toml:"insecure"` Insecure *bool `toml:"insecure"`
Categories *[]string `toml:"categories"`
Aggregator csaf.AggregatorInfo `toml:"aggregator"` Aggregator csaf.AggregatorInfo `toml:"aggregator"`
Providers []*provider `toml:"providers"` Providers []*provider `toml:"providers"`
OpenPGPPrivateKey string `toml:"openpgp_private_key"` OpenPGPPrivateKey string `toml:"openpgp_private_key"`

View file

@ -209,6 +209,25 @@ func (w *worker) writeROLIE(label string, summaries []summary) error {
return util.WriteToFile(path, rolie) return util.WriteToFile(path, rolie)
} }
func (w *worker) writeCategories(label string) error {
categories := w.categories[label]
if len(categories) == 0 {
return nil
}
cats := make([]string, len(categories))
var i int
for cat := range categories {
cats[i] = cat
i++
}
rcd := csaf.NewROLIECategoryDocument(cats...)
labelFolder := strings.ToLower(label)
fname := "category-" + labelFolder + ".json"
path := filepath.Join(w.dir, labelFolder, fname)
return util.WriteToFile(path, rcd)
}
func (w *worker) writeIndices() error { func (w *worker) writeIndices() error {
if len(w.summaries) == 0 || w.dir == "" { if len(w.summaries) == 0 || w.dir == "" {
@ -229,6 +248,9 @@ func (w *worker) writeIndices() error {
if err := w.writeROLIE(label, summaries); err != nil { if err := w.writeROLIE(label, summaries); err != nil {
return err return err
} }
if err := w.writeCategories(label); err != nil {
return err
}
} }
return nil return nil

View file

@ -64,6 +64,9 @@ func (w *worker) mirrorInternal() (*csaf.AggregatorCSAFProvider, error) {
// Collecting the summaries of the advisories. // Collecting the summaries of the advisories.
w.summaries = make(map[string][]summary) w.summaries = make(map[string][]summary)
// Collecting the categories per label.
w.categories = make(map[string]map[string]bool)
base, err := url.Parse(w.loc) base, err := url.Parse(w.loc)
if err != nil { if err != nil {
return nil, err return nil, err
@ -413,6 +416,47 @@ func (w *worker) sign(data []byte) (string, error) {
sig.Data, constants.PGPSignatureHeader, "", "") sig.Data, constants.PGPSignatureHeader, "", "")
} }
func (w *worker) extractCategories(label string, advisory interface{}) error {
// use provider or global categories
var categories []string
if w.provider.Categories != nil {
categories = *w.provider.Categories
} else if w.processor.cfg.Categories != nil {
categories = *w.processor.cfg.Categories
}
// Nothing to do.
if len(categories) == 0 {
return nil
}
cats := w.categories[label]
if cats == nil {
cats = make(map[string]bool)
w.categories[label] = cats
}
var result string
matcher := util.StringMatcher(&result)
const exprPrefix = "expr:"
for _, cat := range categories {
if strings.HasPrefix(cat, exprPrefix) {
expr := cat[len(exprPrefix):]
if err := w.expr.Extract(expr, matcher, true, advisory); err != nil {
return err
}
cats[result] = true
} else { // Normal
cats[cat] = true
}
}
return nil
}
func (w *worker) mirrorFiles(tlpLabel csaf.TLPLabel, files []csaf.AdvisoryFile) error { func (w *worker) mirrorFiles(tlpLabel csaf.TLPLabel, files []csaf.AdvisoryFile) error {
label := strings.ToLower(string(tlpLabel)) label := strings.ToLower(string(tlpLabel))
@ -489,6 +533,12 @@ func (w *worker) mirrorFiles(tlpLabel csaf.TLPLabel, files []csaf.AdvisoryFile)
log.Printf("error: %s: %v\n", file, err) log.Printf("error: %s: %v\n", file, err)
continue continue
} }
if err := w.extractCategories(label, advisory); err != nil {
log.Printf("error: %s: %v\n", file, err)
continue
}
summaries = append(summaries, summary{ summaries = append(summaries, summary{
filename: filename, filename: filename,
summary: sum, summary: sum,

View file

@ -40,12 +40,13 @@ type worker struct {
expr *util.PathEval expr *util.PathEval
signRing *crypto.KeyRing signRing *crypto.KeyRing
client util.Client // client per provider client util.Client // client per provider
provider *provider // current provider provider *provider // current provider
metadataProvider interface{} // current metadata provider metadataProvider interface{} // current metadata provider
loc string // URL of current provider-metadata.json loc string // URL of current provider-metadata.json
dir string // Directory to store data to. dir string // Directory to store data to.
summaries map[string][]summary // the summaries of the advisories. summaries map[string][]summary // the summaries of the advisories.
categories map[string]map[string]bool // the categories per label.
} }
func newWorker(num int, processor *processor) *worker { func newWorker(num int, processor *processor) *worker {