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

Add abilities to aggregator to mirror and list

* Aggregator now checks every provider on whether its mirrored or listed. 
*Add the option to the docs.
* Clean up the example  toml file to still contain two mirrors and one example-lister.

Co-authored-by: Jan Höfelmeyer <Jan Höfelmeyer jhoefelmeyer@intevation.de>
Co-authored-by: Sascha L. Teichmann <sascha.teichmann@intevation.de>
Co-authored-by: Bernhard Reiter <bernhard@intevation.de>
This commit is contained in:
JanHoefelmeyer 2022-07-21 17:59:58 +02:00 committed by GitHub
parent d1855a9c30
commit 3769f1d338
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 21 deletions

View file

@ -36,12 +36,13 @@ 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"` WriteIndices *bool `toml:"write_indices"`
Categories *[]string `toml:"categories"`
// ServiceDocument incidates if we should create a service.json document. // ServiceDocument incidates if we should create a service.json document.
ServiceDocument *bool `toml:"create_service_document"` ServiceDocument *bool `toml:"create_service_document"`
WriteIndices *bool `toml:"write_indices"` AggregatoryCategory *csaf.AggregatorCategory `toml:"category"`
} }
type config struct { type config struct {
@ -101,6 +102,26 @@ func (p *provider) writeIndices(c *config) bool {
return c.WriteIndices return c.WriteIndices
} }
func (p *provider) runAsMirror(c *config) bool {
if p.AggregatoryCategory != nil {
return *p.AggregatoryCategory == csaf.AggregatorAggregator
}
return c.runAsMirror()
}
// atLeastNMirrors checks if there are at least n mirrors configured.
func (c *config) atLeastNMirrors(n int) bool {
var mirrors int
for _, p := range c.Providers {
if p.runAsMirror(c) {
if mirrors++; mirrors >= n {
return true
}
}
}
return false
}
// runAsMirror determines if the aggregator should run in mirror mode. // runAsMirror determines if the aggregator should run in mirror mode.
func (c *config) runAsMirror() bool { func (c *config) runAsMirror() bool {
return c.Aggregator.Category != nil && return c.Aggregator.Category != nil &&
@ -184,6 +205,20 @@ func (c *config) checkProviders() error {
return nil return nil
} }
func (c *config) checkMirror() error {
if c.runAsMirror() {
if !c.AllowSingleProvider && !c.atLeastNMirrors(2) {
return errors.New("at least 2 providers need to be mirrored")
} else if c.AllowSingleProvider && !c.atLeastNMirrors(1) {
return errors.New("at least one provider must be mirrored")
}
} else if !c.AllowSingleProvider && c.atLeastNMirrors(1) {
return errors.New("found mirrors in a lister aggregator")
}
return nil
}
func (c *config) setDefaults() { func (c *config) setDefaults() {
if c.Folder == "" { if c.Folder == "" {
c.Folder = defaultFolder c.Folder = defaultFolder
@ -219,7 +254,11 @@ func (c *config) check() error {
return err return err
} }
return c.checkProviders() if err := c.checkProviders(); err != nil {
return err
}
return c.checkMirror()
} }
func loadConfig(path string) (*config, error) { func loadConfig(path string) (*config, error) {

View file

@ -24,6 +24,7 @@ import (
type fullJob struct { type fullJob struct {
provider *provider provider *provider
aggregatorProvider *csaf.AggregatorCSAFProvider aggregatorProvider *csaf.AggregatorCSAFProvider
work fullWorkFunc
err error err error
} }
@ -61,11 +62,7 @@ func (w *worker) setupProviderFull(provider *provider) error {
type fullWorkFunc func(*worker) (*csaf.AggregatorCSAFProvider, error) type fullWorkFunc func(*worker) (*csaf.AggregatorCSAFProvider, error)
// fullWork handles the treatment of providers concurrently. // fullWork handles the treatment of providers concurrently.
func (w *worker) fullWork( func (w *worker) fullWork(wg *sync.WaitGroup, jobs <-chan *fullJob) {
wg *sync.WaitGroup,
doWork fullWorkFunc,
jobs <-chan *fullJob,
) {
defer wg.Done() defer wg.Done()
for j := range jobs { for j := range jobs {
@ -73,16 +70,15 @@ func (w *worker) fullWork(
j.err = err j.err = err
continue continue
} }
j.aggregatorProvider, j.err = doWork(w) j.aggregatorProvider, j.err = j.work(w)
} }
} }
// full performs the complete lister/download // full performs the complete lister/download
func (p *processor) full() error { func (p *processor) full() error {
var doWork fullWorkFunc
if p.cfg.runAsMirror() { if p.cfg.runAsMirror() {
log.Println("Running in aggregator mode")
// check if we need to setup a remote validator // check if we need to setup a remote validator
if p.cfg.RemoteValidatorOptions != nil { if p.cfg.RemoteValidatorOptions != nil {
@ -98,11 +94,7 @@ func (p *processor) full() error {
p.remoteValidator = nil p.remoteValidator = nil
}() }()
} }
doWork = (*worker).mirror
log.Println("Running in aggregator mode")
} else { } else {
doWork = (*worker).lister
log.Println("Running in lister mode") log.Println("Running in lister mode")
} }
@ -113,13 +105,22 @@ func (p *processor) full() error {
for i := 1; i <= p.cfg.Workers; i++ { for i := 1; i <= p.cfg.Workers; i++ {
wg.Add(1) wg.Add(1)
w := newWorker(i, p) w := newWorker(i, p)
go w.fullWork(&wg, doWork, queue) go w.fullWork(&wg, queue)
} }
jobs := make([]fullJob, len(p.cfg.Providers)) jobs := make([]fullJob, len(p.cfg.Providers))
for i, p := range p.cfg.Providers { for i, provider := range p.cfg.Providers {
jobs[i] = fullJob{provider: p} var work fullWorkFunc
if provider.runAsMirror(p.cfg) {
work = (*worker).mirror
} else {
work = (*worker).lister
}
jobs[i] = fullJob{
provider: provider,
work: work,
}
queue <- &jobs[i] queue <- &jobs[i]
} }
close(queue) close(queue)

View file

@ -101,8 +101,14 @@ domain
rate rate
insecure insecure
write_indices write_indices
category
``` ```
If you want an entry to be listed instead of mirrored
in a `aggregator.category == "aggregator"` instance,
set `category` to `lister` in the entry.
Otherwise it is recommended to not set `category` for entries.
#### Example config file #### Example config file
<!-- MARKDOWN-AUTO-DOCS:START (CODE:src=../docs/examples/aggregator.toml) --> <!-- MARKDOWN-AUTO-DOCS:START (CODE:src=../docs/examples/aggregator.toml) -->
<!-- The below code snippet is automatically added from ../docs/examples/aggregator.toml --> <!-- The below code snippet is automatically added from ../docs/examples/aggregator.toml -->
@ -136,6 +142,7 @@ insecure = true
create_service_document = true create_service_document = true
# rate = 1.5 # rate = 1.5
# insecure = true # insecure = true
category = "lister"
[[providers]] [[providers]]
name = "local-dev-provider2" name = "local-dev-provider2"
@ -143,5 +150,14 @@ insecure = true
# rate = 1.2 # rate = 1.2
# insecure = true # insecure = true
write_indices = true write_indices = true
category = "aggregator"
[[providers]]
name = "local-dev-provider3"
domain = "localhost"
# rate = 1.8
# insecure = true
write_indices = true
category = "aggregator"
``` ```
<!-- MARKDOWN-AUTO-DOCS:END --> <!-- MARKDOWN-AUTO-DOCS:END -->

View file

@ -14,6 +14,8 @@ insecure = true
# allow_single_provider = true # allow_single_provider = true
[aggregator] [aggregator]
# Set if this instance shall be a mirror (aka `aggregator`) or a `lister`.
# This determines the default value for the entries in [[provider]].
category = "aggregator" category = "aggregator"
name = "Example Development CSAF Aggregator" name = "Example Development CSAF Aggregator"
contact_details = "some @ somewhere" contact_details = "some @ somewhere"
@ -34,3 +36,13 @@ insecure = true
# rate = 1.2 # rate = 1.2
# insecure = true # insecure = true
write_indices = true write_indices = true
[[providers]]
name = "local-dev-provider3"
domain = "localhost"
# rate = 1.8
# insecure = true
write_indices = true
# If aggregator.category == "aggreator", set for an entry that should
# be listed in addition:
category = "lister"