From e0d2136ee69e55651af4284a7f19e640ddc76a82 Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Tue, 5 Aug 2025 02:09:37 +0000 Subject: [PATCH] feat(config): add support for log filters Signed-off-by: Xe Iaso --- lib/policy/config/config.go | 15 +++++++++++ lib/policy/config/logging.go | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 lib/policy/config/logging.go diff --git a/lib/policy/config/config.go b/lib/policy/config/config.go index 6b5946ae..bc39f05d 100644 --- a/lib/policy/config/config.go +++ b/lib/policy/config/config.go @@ -326,6 +326,7 @@ func (sc StatusCodes) Valid() error { type fileConfig struct { Bots []BotOrImport `json:"bots"` DNSBL bool `json:"dnsbl"` + Logging *Logging `json:"logging"` OpenGraph openGraphFileConfig `json:"openGraph,omitempty"` Impressum *Impressum `json:"impressum,omitempty"` StatusCodes StatusCodes `json:"status_codes"` @@ -368,6 +369,12 @@ func (c *fileConfig) Valid() error { } } + if c.Logging != nil { + if err := c.Logging.Valid(); err != nil { + errs = append(errs, err) + } + } + if len(errs) != 0 { return fmt.Errorf("config is not valid:\n%w", errors.Join(errs...)) } @@ -401,6 +408,7 @@ func Load(fin io.Reader, fname string) (*Config, error) { ConsiderHost: c.OpenGraph.ConsiderHost, Override: c.OpenGraph.Override, }, + Logging: c.Logging, StatusCodes: c.StatusCodes, Store: c.Store, } @@ -441,6 +449,12 @@ func Load(fin io.Reader, fname string) (*Config, error) { result.Impressum = c.Impressum } + if c.Logging != nil { + if err := c.Logging.Valid(); err != nil { + validationErrs = append(validationErrs, err) + } + } + if len(c.Thresholds) == 0 { c.Thresholds = DefaultThresholds } @@ -465,6 +479,7 @@ type Config struct { Bots []BotConfig Thresholds []Threshold DNSBL bool + Logging *Logging Impressum *Impressum OpenGraph OpenGraph StatusCodes StatusCodes diff --git a/lib/policy/config/logging.go b/lib/policy/config/logging.go new file mode 100644 index 00000000..f710b07a --- /dev/null +++ b/lib/policy/config/logging.go @@ -0,0 +1,49 @@ +package config + +import ( + "errors" + "fmt" +) + +type Logging struct { + Filters []LogFilter `json:"filters,omitempty" yaml:"filters,omitempty"` +} + +func (l *Logging) Valid() error { + var errs []error + + for _, lf := range l.Filters { + if err := lf.Valid(); err != nil { + errs = append(errs, err) + } + } + + if len(errs) != 0 { + return errors.Join(errs...) + } + + return nil +} + +type LogFilter struct { + Name string `json:"name" yaml:"name"` + Expression ExpressionOrList `json:"expression" yaml:"expression"` +} + +func (lf LogFilter) Valid() error { + var errs []error + + if lf.Name == "" { + errs = append(errs, fmt.Errorf("%w: log filter has no name", ErrMissingValue)) + } + + if err := lf.Expression.Valid(); err != nil { + errs = append(errs, err) + } + + if len(errs) != 0 { + return fmt.Errorf("log filter %q is not valid: %w", lf.Name, errors.Join(errs...)) + } + + return nil +}