From cee776b3830e60d3ec8ccd6703363e9c1f2ae56d Mon Sep 17 00:00:00 2001 From: Hank Donnay Date: Wed, 17 May 2023 15:44:53 -0500 Subject: [PATCH] config: add newtype for Durations This was an oversight that prevented JSON serialization working. The change is a breaking code change, but keeps the YAML serialization (currently the only one) the same. Signed-off-by: Hank Donnay --- config/config.go | 19 +++++++++++++++++++ config/matcher.go | 9 ++++----- config/notifier.go | 16 ++++++++-------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 15243e8044..02638cb747 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "fmt" "net" + "time" ) // Config is the configuration object for the commands in @@ -75,3 +76,21 @@ func (c *Config) lint() (ws []Warning, err error) { } return ws, nil } + +// Duration is a serializeable [time.Duration]. +type Duration time.Duration + +// UnmarshalText implements [encoding.TextUnmarshaler]. +func (d *Duration) UnmarshalText(b []byte) error { + dur, err := time.ParseDuration(string(b)) + if err != nil { + return err + } + *d = Duration(dur) + return nil +} + +// MarshalText implements [encoding.TextMarshaler]. +func (d *Duration) MarshalText() ([]byte, error) { + return []byte(time.Duration(*d).String()), nil +} diff --git a/config/matcher.go b/config/matcher.go index bd913a7cd3..a6321d30fa 100644 --- a/config/matcher.go +++ b/config/matcher.go @@ -3,7 +3,6 @@ package config import ( "fmt" "net/url" - "time" ) // Matcher is the configuration for the matcher service. @@ -23,7 +22,7 @@ type Matcher struct { // Period controls how often updaters are run. // // The default is 30 minutes. - Period time.Duration `yaml:"period,omitempty" json:"period,omitempty"` + Period Duration `yaml:"period,omitempty" json:"period,omitempty"` // UpdateRetention controls the number of updates to retain between // garbage collection periods. // @@ -48,7 +47,7 @@ type Matcher struct { // // If empty, the duration set in "Period" will be used. This means client // may cache "stale" results for 2(Period) - 1 seconds. - CacheAge time.Duration `yaml:"cache_age,omitempty" json:"cache_age,omitempty"` + CacheAge Duration `yaml:"cache_age,omitempty" json:"cache_age,omitempty"` // A "true" or "false" value // // Whether Matcher nodes handle migrations to their databases. @@ -65,7 +64,7 @@ func (m *Matcher) validate(mode Mode) ([]Warning, error) { return nil, nil } if m.Period == 0 { - m.Period = DefaultMatcherPeriod + m.Period = Duration(DefaultMatcherPeriod) } switch { case m.UpdateRetention < 0: @@ -103,7 +102,7 @@ func (m *Matcher) lint() (ws []Warning, err error) { ws[i].path = ".connstring" } - if m.Period < DefaultMatcherPeriod { + if m.Period < Duration(DefaultMatcherPeriod) { ws = append(ws, Warning{ path: ".period", msg: "updater period is very aggressive: most sources are updated daily", diff --git a/config/notifier.go b/config/notifier.go index 0b392a5296..5d2ba419a4 100644 --- a/config/notifier.go +++ b/config/notifier.go @@ -42,14 +42,14 @@ type Notifier struct { // The frequency at which the notifier will query at Matcher for Update Operations. // If a value smaller then 1 second is provided it will be replaced with the // default 5 second poll interval. - PollInterval time.Duration `yaml:"poll_interval,omitempty" json:"poll_interval,omitempty"` + PollInterval Duration `yaml:"poll_interval,omitempty" json:"poll_interval,omitempty"` // A time.ParseDuration parsable string // // The frequency at which the notifier attempt delivery of created or previously failed // notifications // If a value smaller then 1 second is provided it will be replaced with the // default 5 second delivery interval. - DeliveryInterval time.Duration `yaml:"delivery_interval,omitempty" json:"delivery_interval,omitempty"` + DeliveryInterval Duration `yaml:"delivery_interval,omitempty" json:"delivery_interval,omitempty"` // DisableSummary disables summarizing vulnerabilities per-manifest. // // The default is to summarize any new vulnerabilities to the most severe @@ -70,11 +70,11 @@ func (n *Notifier) validate(mode Mode) ([]Warning, error) { if mode != ComboMode && mode != NotifierMode { return nil, nil } - if n.PollInterval < 1*time.Second { - n.PollInterval = DefaultNotifierPollInterval + if n.PollInterval < Duration(1*time.Second) { + n.PollInterval = Duration(DefaultNotifierPollInterval) } - if n.DeliveryInterval < 1*time.Second { - n.DeliveryInterval = DefaultNotifierDeliveryInterval + if n.DeliveryInterval < Duration(1*time.Second) { + n.DeliveryInterval = Duration(DefaultNotifierDeliveryInterval) } switch mode { case ComboMode: @@ -120,13 +120,13 @@ func (n *Notifier) lint() (ws []Warning, err error) { }) } - if n.PollInterval < DefaultNotifierPollInterval { + if n.PollInterval < Duration(DefaultNotifierPollInterval) { ws = append(ws, Warning{ path: ".poll_interval", msg: "interval is very fast: may result in increased workload", }) } - if n.DeliveryInterval < DefaultNotifierDeliveryInterval { + if n.DeliveryInterval < Duration(DefaultNotifierDeliveryInterval) { ws = append(ws, Warning{ path: ".delivery_interval", msg: "interval is very fast: may result in increased workload",