Cap sync_interval at 30 days
This commit is contained in:
@@ -57,7 +57,7 @@ func TestMutableTTLOmittedVsZero(t *testing.T) {
|
||||
}
|
||||
|
||||
// sync_interval round-trips through the API and DB on github_* remotes and is
|
||||
// rejected when negative or on a type without a background syncer.
|
||||
// rejected when negative, over the maximum, or on a type without a background syncer.
|
||||
func TestSyncIntervalAPI(t *testing.T) {
|
||||
if testDSN == "" {
|
||||
t.Skip("Docker unavailable")
|
||||
@@ -93,6 +93,8 @@ func TestSyncIntervalAPI(t *testing.T) {
|
||||
|
||||
for _, body := range []string{
|
||||
`{"name":"si-neg",` + gh + `,"sync_interval":-1}`,
|
||||
`{"name":"si-max",` + gh + `,"sync_interval":2592001}`,
|
||||
`{"name":"si-int32",` + gh + `,"sync_interval":2147483648}`,
|
||||
`{"name":"si-rpm","package_type":"rpm","base_url":"https://example.com/repo","sync_interval":60}`,
|
||||
} {
|
||||
if c := do(t, h, "POST", "/", body); c != 400 {
|
||||
|
||||
@@ -185,11 +185,14 @@ func syncPhase(name string, period time.Duration) time.Duration {
|
||||
return time.Duration(h.Sum64() % uint64(period))
|
||||
}
|
||||
|
||||
// ValidateSyncInterval rejects a negative sync_interval, or one set on a type
|
||||
// without a background syncer.
|
||||
// MaxSyncInterval caps sync_interval at 30 days (seconds).
|
||||
const MaxSyncInterval = 30 * 24 * 60 * 60
|
||||
|
||||
// ValidateSyncInterval rejects a sync_interval outside 0..MaxSyncInterval, or
|
||||
// one set on a type without a background syncer.
|
||||
func (r *Remote) ValidateSyncInterval() error {
|
||||
if r.SyncInterval < 0 {
|
||||
return fmt.Errorf("sync_interval must be >= 0, got %d", r.SyncInterval)
|
||||
if r.SyncInterval < 0 || r.SyncInterval > MaxSyncInterval {
|
||||
return fmt.Errorf("sync_interval must be between 0 and %d, got %d", MaxSyncInterval, r.SyncInterval)
|
||||
}
|
||||
switch r.PackageType {
|
||||
case PackageGitHubRPM, PackageGitHubDeb, PackageGitHubAlpine:
|
||||
|
||||
@@ -153,6 +153,8 @@ func TestValidateSyncInterval(t *testing.T) {
|
||||
{PackageGitHubDeb, 3600, true},
|
||||
{PackageGitHubAlpine, 3600, true},
|
||||
{PackageGitHubRPM, -1, false},
|
||||
{PackageGitHubRPM, MaxSyncInterval, true},
|
||||
{PackageGitHubRPM, MaxSyncInterval + 1, false},
|
||||
{PackageRPM, 0, true},
|
||||
{PackageRPM, 60, false},
|
||||
} {
|
||||
|
||||
Reference in New Issue
Block a user