Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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.

Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 21 additions & 5 deletions internal/config/cooldown_profiles.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"maps"
"regexp"
Expand All @@ -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)) {
Expand All @@ -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,
Expand All @@ -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
}
64 changes: 64 additions & 0 deletions internal/config/cooldown_profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading