diff --git a/docs/configuration.md b/docs/configuration.md index e7305c40..18eeea72 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1313,7 +1313,7 @@ cooldown: | `cooldown.packages` | - | Per-package overrides (keyed by PURL) | | `cooldown.package_patterns` | - | Per-package glob overrides (keyed by PURL glob) | -Durations support days (`7d`), hours (`48h`), and minutes (`30m`). Set to `0` to disable. +Durations support days (`7d`), hours (`48h`), and minutes (`30m`). Set to `0` to disable. Other units such as weeks (`1w`) are not supported. Invalid durations are rejected at startup, wherever they appear. Package PURL keys are normalized to canonical form before matching, so `pkg:npm/@babel/core` and `pkg:npm/%40babel/core` are equivalent, as are `pkg:pypi/Django` and `pkg:pypi/django`. If both forms configure the same package, the canonical entry wins. diff --git a/internal/config/config.go b/internal/config/config.go index e3ee9853..3c7ffb02 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1746,6 +1746,9 @@ func (c *Config) validateCacheArtifacts() error { } func (c *Config) validateComponents() error { + if err := c.Cooldown.validateTopLevelDurations(); err != nil { + return err + } if err := c.Cooldown.ValidateProfiles(); err != nil { return err } diff --git a/internal/config/cooldown_profiles.go b/internal/config/cooldown_profiles.go index 225dd201..f9e2d5f4 100644 --- a/internal/config/cooldown_profiles.go +++ b/internal/config/cooldown_profiles.go @@ -1,6 +1,7 @@ package config import ( + "errors" "fmt" "maps" "regexp" @@ -18,6 +19,14 @@ func (c *CooldownConfig) Policy() (*cooldown.Policy, error) { }, c.PackagePatterns) } +// The cooldown library reads an unparsable duration as 0, which disables cooldown. +func (c *CooldownConfig) validateTopLevelDurations() error { + if err := errors.Join(c.durationErrors()...); err != nil { + return fmt.Errorf("cooldown: %w", err) + } + return nil +} + // ValidateProfiles checks names and policies before they become routes. func (c *CooldownConfig) ValidateProfiles() error { for _, name := range slices.Sorted(maps.Keys(c.Profiles)) { @@ -39,6 +48,14 @@ func (c *CooldownConfig) ValidateProfiles() error { } func (c *CooldownConfig) validateProfileDurations() error { + if errs := c.durationErrors(); len(errs) > 0 { + return errs[0] + } + return nil +} + +func (c *CooldownConfig) durationErrors() []error { + var errs []error values := map[string]string{"default": c.Default} for group, overrides := range map[string]map[string]string{ "ecosystems": c.Ecosystems, "packages": c.Packages, "package_patterns": c.PackagePatterns, @@ -54,11 +71,10 @@ func (c *CooldownConfig) validateProfileDurations() error { } duration, err := cooldown.ParseDuration(value) if err != nil { - return fmt.Errorf("invalid %s: %w", key, err) - } - if duration < 0 { - return fmt.Errorf("invalid %s: duration must be non-negative", key) + errs = append(errs, fmt.Errorf("invalid %s: %w", key, err)) + } else if duration < 0 { + errs = append(errs, fmt.Errorf("invalid %s: duration must be non-negative", key)) } } - return nil + return errs } diff --git a/internal/config/cooldown_profiles_test.go b/internal/config/cooldown_profiles_test.go index 6f10362d..a6894f2d 100644 --- a/internal/config/cooldown_profiles_test.go +++ b/internal/config/cooldown_profiles_test.go @@ -79,3 +79,67 @@ func TestLoadRejectsInvalidCooldownProfiles(t *testing.T) { }) } } + +func TestRejectsInvalidTopLevelCooldown(t *testing.T) { + for _, tc := range []struct { + name string + set func(*CooldownConfig) + want string + }{ + {"default weeks", func(c *CooldownConfig) { c.Default = "1w" }, "cooldown: invalid default"}, + {"default words", func(c *CooldownConfig) { c.Default = "7 days" }, "cooldown: invalid default"}, + {"default negative", func(c *CooldownConfig) { c.Default = "-3d" }, "cooldown: invalid default"}, + {"ecosystem", func(c *CooldownConfig) { c.Ecosystems = map[string]string{"npm": "3days"} }, "cooldown: invalid ecosystems.npm"}, + {"package", func(c *CooldownConfig) { c.Packages = map[string]string{"pkg:npm/lodash": "soon"} }, "cooldown: invalid packages.pkg:npm/lodash"}, + {"pattern weeks", func(c *CooldownConfig) { c.PackagePatterns = map[string]string{"pkg:npm/*": "1w"} }, "cooldown: invalid package_patterns.pkg:npm/*"}, + {"pattern negative", func(c *CooldownConfig) { c.PackagePatterns = map[string]string{"pkg:npm/*": "-1d"} }, "cooldown: invalid package_patterns.pkg:npm/*"}, + } { + t.Run(tc.name, func(t *testing.T) { + cfg := Default() + tc.set(&cfg.Cooldown) + err := cfg.Validate() + if err == nil || !strings.Contains(err.Error(), tc.want) { + t.Fatalf("Validate = %v, want an error containing %q", err, tc.want) + } + }) + } +} + +func TestRejectsEveryInvalidTopLevelCooldown(t *testing.T) { + cfg := Default() + cfg.Cooldown.Default = "1w" + cfg.Cooldown.Ecosystems = map[string]string{"npm": "3days", "pypi": "2d"} + cfg.Cooldown.Packages = map[string]string{"pkg:npm/lodash": "soon"} + err := cfg.Validate() + if err == nil { + t.Fatal("Validate = nil, want the invalid durations rejected") + } + for _, want := range []string{"invalid default", "invalid ecosystems.npm", "invalid packages.pkg:npm/lodash"} { + if !strings.Contains(err.Error(), want) { + t.Errorf("Validate = %q, want it to contain %q", err, want) + } + } + if strings.Contains(err.Error(), "ecosystems.pypi") { + t.Errorf("Validate = %q, want the valid pypi value left out", err) + } +} + +func TestAcceptsValidTopLevelCooldown(t *testing.T) { + for _, value := range []string{"", "0", "3d", "1.5d", "48h", "90m"} { + cfg := Default() + cfg.Cooldown.Default = value + cfg.Cooldown.Ecosystems = map[string]string{"npm": value} + if err := cfg.Validate(); err != nil { + t.Errorf("Validate(%q) = %v, want nil", value, err) + } + } +} + +func TestRejectsInvalidCooldownFromEnv(t *testing.T) { + t.Setenv("PROXY_COOLDOWN_DEFAULT", "1w") + cfg := Default() + cfg.LoadFromEnv() + if err := cfg.Validate(); err == nil || !strings.Contains(err.Error(), "cooldown: invalid default") { + t.Fatalf("Validate = %v, want invalid cooldown default", err) + } +}