From 5f7bd2b1da78ff2c085bd4fd519b759989bde8a7 Mon Sep 17 00:00:00 2001 From: KevinSmile Date: Tue, 25 Aug 2020 04:56:50 +0800 Subject: [PATCH] fix: lifecycle-expiration validation bug (#10327) --- pkg/bucket/lifecycle/expiration.go | 2 +- pkg/bucket/lifecycle/expiration_test.go | 2 +- pkg/bucket/lifecycle/rule.go | 12 ++++-------- pkg/bucket/lifecycle/rule_test.go | 2 +- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/pkg/bucket/lifecycle/expiration.go b/pkg/bucket/lifecycle/expiration.go index 6037c6529..fd70e45b9 100644 --- a/pkg/bucket/lifecycle/expiration.go +++ b/pkg/bucket/lifecycle/expiration.go @@ -24,7 +24,7 @@ import ( var ( errLifecycleInvalidDate = Errorf("Date must be provided in ISO 8601 format") errLifecycleInvalidDays = Errorf("Days must be positive integer when used with Expiration") - errLifecycleInvalidExpiration = Errorf("At least one of Days or Date should be present inside Expiration") + errLifecycleInvalidExpiration = Errorf("Exactly one of Days (positive integer) or Date (positive ISO 8601 format) should be present inside Expiration.") errLifecycleInvalidDeleteMarker = Errorf("Delete marker cannot be specified with Days or Date in a Lifecycle Expiration Policy") errLifecycleDateNotMidnight = Errorf("'Date' must be at midnight GMT") ) diff --git a/pkg/bucket/lifecycle/expiration_test.go b/pkg/bucket/lifecycle/expiration_test.go index e1394c24f..f863f9e1e 100644 --- a/pkg/bucket/lifecycle/expiration_test.go +++ b/pkg/bucket/lifecycle/expiration_test.go @@ -80,7 +80,7 @@ func TestInvalidExpiration(t *testing.T) { `, expectedErr: errLifecycleInvalidExpiration, }, - { // Expiration with both number of days nor a date + { // Expiration with both number of days and a date inputXML: ` 3 2019-04-20T00:00:00Z diff --git a/pkg/bucket/lifecycle/rule.go b/pkg/bucket/lifecycle/rule.go index 339e611c1..c6c523a72 100644 --- a/pkg/bucket/lifecycle/rule.go +++ b/pkg/bucket/lifecycle/rule.go @@ -46,10 +46,9 @@ type Rule struct { } var ( - errInvalidRuleID = Errorf("ID length is limited to 255 characters") - errEmptyRuleStatus = Errorf("Status should not be empty") - errInvalidRuleStatus = Errorf("Status must be set to either Enabled or Disabled") - errMissingExpirationAction = Errorf("No expiration action found") + errInvalidRuleID = Errorf("ID length is limited to 255 characters") + errEmptyRuleStatus = Errorf("Status should not be empty") + errInvalidRuleStatus = Errorf("Status must be set to either Enabled or Disabled") ) // generates random UUID @@ -94,10 +93,7 @@ func (r Rule) validateStatus() error { } func (r Rule) validateAction() error { - if r.Expiration == (Expiration{}) { - return errMissingExpirationAction - } - return nil + return r.Expiration.Validate() } func (r Rule) validateFilter() error { diff --git a/pkg/bucket/lifecycle/rule_test.go b/pkg/bucket/lifecycle/rule_test.go index da9bc9fa3..80156712e 100644 --- a/pkg/bucket/lifecycle/rule_test.go +++ b/pkg/bucket/lifecycle/rule_test.go @@ -67,7 +67,7 @@ func TestInvalidRules(t *testing.T) { rule without expiration Enabled `, - expectedErr: errMissingExpirationAction, + expectedErr: errLifecycleInvalidExpiration, }, { // Rule with ID longer than 255 characters inputXML: `