From 14fc9cdc61dae4b63c84a125c5443a7b4106f6f3 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Wed, 29 Jun 2022 18:32:17 +0200 Subject: [PATCH] Make categories configurable in provider --- cmd/csaf_provider/config.go | 60 +++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/cmd/csaf_provider/config.go b/cmd/csaf_provider/config.go index a300b8f..7b0d145 100644 --- a/cmd/csaf_provider/config.go +++ b/cmd/csaf_provider/config.go @@ -56,6 +56,7 @@ type config struct { UploadLimit *int64 `toml:"upload_limit"` Issuer *string `toml:"issuer"` RemoteValidator *csaf.RemoteValidatorOptions `toml:"remote_validator"` + Categories *[]string `toml:"categories"` } func (pmdc *providerMetadataConfig) apply(pmd *csaf.ProviderMetadata) { @@ -145,6 +146,65 @@ func (cfg *config) checkPassword(hash string) bool { bcrypt.CompareHashAndPassword([]byte(hash), []byte(*cfg.Password)) == nil } +// HasCategories tells if categories are configured. +func (cfg *config) HasCategories() bool { + return cfg.Categories != nil +} + +// HasDynamicCategories tells if dynamic categories are configured. +func (cfg *config) HasDynamicCategories() bool { + if !cfg.HasCategories() { + return false + } + for _, cat := range *cfg.Categories { + if strings.HasPrefix(cat, "dynamic:") { + return true + } + } + return false +} + +// HasStaticCategories tells if static categories are configured. +func (cfg *config) HasStaticCategories() bool { + if !cfg.HasCategories() { + return false + } + for _, cat := range *cfg.Categories { + if !strings.HasPrefix(cat, "expr:") { + return true + } + } + return false +} + +// StaticCategories returns a list on the configured static categories. +func (cfg *config) StaticCategories() []string { + if !cfg.HasCategories() { + return nil + } + cats := make([]string, 0, len(*cfg.Categories)) + for _, cat := range *cfg.Categories { + if !strings.HasPrefix(cat, "expr:") { + cats = append(cats, cat) + } + } + return cats +} + +// DynamicCategories returns a list on the configured dynamic categories. +func (cfg *config) DynamicCategories() []string { + if !cfg.HasCategories() { + return nil + } + cats := make([]string, 0, len(*cfg.Categories)) + for _, cat := range *cfg.Categories { + if strings.HasPrefix(cat, "expr:") { + cats = append(cats, cat[len("expr:"):]) + } + } + return cats +} + // loadConfig extracts the config values from the config file. The path to the // file is taken either from environment variable "CSAF_CONFIG" or from the // defined default path in "defaultConfigPath".