Skip to content

Commit a3fe8ce

Browse files
committed
Warn when a top-level cooldown duration can't be parsed
The cooldown library reads a duration it can't parse as 0, so a value such as default: "1w" or "7 days" turned cooldown off without any sign of it. Durations under cooldown.profiles were already rejected at startup, but cooldown.default, cooldown.ecosystems, cooldown.packages and cooldown.package_patterns were never checked. The proxy now logs a warning at startup naming the value. It warns rather than failing to start, so configs that start today keep starting.
1 parent 57428c5 commit a3fe8ce

5 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎docs/configuration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ cooldown:
12751275
| `cooldown.packages` | - | Per-package overrides (keyed by PURL) |
12761276
| `cooldown.package_patterns` | - | Per-package glob overrides (keyed by PURL glob) |
12771277

1278-
Durations support days (`7d`), hours (`48h`), and minutes (`30m`). Set to `0` to disable.
1278+
Durations support days (`7d`), hours (`48h`), and minutes (`30m`). Set to `0` to disable. Other units such as weeks (`1w`) are not supported: a top-level value that can't be parsed applies no cooldown, so the proxy logs a warning at startup naming it. Invalid durations inside `cooldown.profiles` are rejected at startup.
12791279

12801280
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.
12811281

‎internal/config/config.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (c *CooldownConfig) NormalizedPackages() map[string]string {
288288

289289
// Shortest returns the shortest cooldown configured anywhere, by default,
290290
// ecosystem, package or package pattern, or 0 when none is. Unparsable
291-
// durations are left out; Validate reports them.
291+
// durations are left out; Config.Warnings reports them.
292292
func (c *CooldownConfig) Shortest() time.Duration {
293293
if c == nil {
294294
return 0

‎internal/config/cooldown_profiles.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ func (c *CooldownConfig) Policy() (*cooldown.Policy, error) {
1818
}, c.PackagePatterns)
1919
}
2020

21+
// durationWarning describes a top-level cooldown duration that cannot be
22+
// parsed, or returns "" when they all parse.
23+
//
24+
// The cooldown library reads a duration it cannot parse as 0, so a typo such
25+
// as "1w" turns cooldown off without notice. Profiles already reject such
26+
// values; the top level only warns, so configs that start today keep starting.
27+
func (c *CooldownConfig) durationWarning() string {
28+
if err := c.validateProfileDurations(); err != nil {
29+
return fmt.Sprintf("cooldown: %v; no cooldown applies where this value is used", err)
30+
}
31+
return ""
32+
}
33+
2134
// ValidateProfiles checks names and policies before they become routes.
2235
func (c *CooldownConfig) ValidateProfiles() error {
2336
for _, name := range slices.Sorted(maps.Keys(c.Profiles)) {

‎internal/config/cooldown_profiles_test.go‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,61 @@ func TestLoadRejectsInvalidCooldownProfiles(t *testing.T) {
7979
})
8080
}
8181
}
82+
83+
// TestWarnsOnInvalidTopLevelCooldown checks that a top-level duration the
84+
// cooldown library cannot parse, and would read as 0, is reported. It warns
85+
// rather than failing Validate so that configs which start today still start.
86+
func TestWarnsOnInvalidTopLevelCooldown(t *testing.T) {
87+
for _, tc := range []struct {
88+
name string
89+
set func(*CooldownConfig)
90+
want string
91+
}{
92+
{"default weeks", func(c *CooldownConfig) { c.Default = "1w" }, "cooldown: invalid default"},
93+
{"default words", func(c *CooldownConfig) { c.Default = "7 days" }, "cooldown: invalid default"},
94+
{"default negative", func(c *CooldownConfig) { c.Default = "-3d" }, "cooldown: invalid default"},
95+
{"ecosystem", func(c *CooldownConfig) { c.Ecosystems = map[string]string{"npm": "3days"} }, "cooldown: invalid ecosystems.npm"},
96+
{"package", func(c *CooldownConfig) { c.Packages = map[string]string{"pkg:npm/lodash": "soon"} }, "cooldown: invalid packages.pkg:npm/lodash"},
97+
{"pattern", func(c *CooldownConfig) { c.PackagePatterns = map[string]string{"pkg:npm/*": "1w"} }, "cooldown: invalid package_patterns.pkg:npm/*"},
98+
} {
99+
t.Run(tc.name, func(t *testing.T) {
100+
cfg := Default()
101+
tc.set(&cfg.Cooldown)
102+
if err := cfg.Validate(); err != nil {
103+
t.Fatalf("Validate = %v, want nil: invalid top-level durations warn", err)
104+
}
105+
if !hasWarning(cfg.Warnings(), tc.want) {
106+
t.Fatalf("Warnings = %q, want one containing %q", cfg.Warnings(), tc.want)
107+
}
108+
})
109+
}
110+
}
111+
112+
func TestNoWarningForValidTopLevelCooldown(t *testing.T) {
113+
for _, value := range []string{"", "0", "3d", "1.5d", "48h", "90m"} {
114+
cfg := Default()
115+
cfg.Cooldown.Default = value
116+
cfg.Cooldown.Ecosystems = map[string]string{"npm": value}
117+
if hasWarning(cfg.Warnings(), "cooldown") {
118+
t.Errorf("Warnings(%q) = %q, want no cooldown warning", value, cfg.Warnings())
119+
}
120+
}
121+
}
122+
123+
func TestWarnsOnInvalidCooldownFromEnv(t *testing.T) {
124+
t.Setenv("PROXY_COOLDOWN_DEFAULT", "1w")
125+
cfg := Default()
126+
cfg.LoadFromEnv()
127+
if !hasWarning(cfg.Warnings(), "cooldown: invalid default") {
128+
t.Fatalf("Warnings = %q, want invalid cooldown default", cfg.Warnings())
129+
}
130+
}
131+
132+
func hasWarning(warnings []string, substr string) bool {
133+
for _, w := range warnings {
134+
if strings.Contains(w, substr) {
135+
return true
136+
}
137+
}
138+
return false
139+
}

‎internal/config/mirror.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func (c *Config) Warnings() []string {
144144
if c.Mirror.Refresh != nil && !c.CacheMetadata {
145145
warnings = append(warnings, "mirror.refresh without cache_metadata does not refresh Debian, Alpine or RPM packages: their indexes are found through the metadata cache")
146146
}
147+
if w := c.Cooldown.durationWarning(); w != "" {
148+
warnings = append(warnings, w)
149+
}
147150
return warnings
148151
}
149152

0 commit comments

Comments
 (0)