From aa6468932be849dc7858b7616187993fe44684d3 Mon Sep 17 00:00:00 2001 From: findmyname666 <35902428+findmyname666@users.noreply.github.com> Date: Mon, 20 Jul 2020 00:10:05 +0200 Subject: [PATCH] make sure lifecycle rule ID is present (#10084) --- pkg/bucket/lifecycle/lifecycle_test.go | 2 +- pkg/bucket/lifecycle/rule.go | 24 ++++++++++++++++++++++-- pkg/bucket/lifecycle/rule_test.go | 13 +++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/pkg/bucket/lifecycle/lifecycle_test.go b/pkg/bucket/lifecycle/lifecycle_test.go index 9bfea5509..619912a69 100644 --- a/pkg/bucket/lifecycle/lifecycle_test.go +++ b/pkg/bucket/lifecycle/lifecycle_test.go @@ -146,7 +146,7 @@ func TestParseAndValidateLifecycleConfig(t *testing.T) { expectedParsingErr: nil, expectedValidationErr: nil, }, - { // lifecycle config with rules having overlapping prefix + { // lifecycle config with rules having duplicate ID inputConfig: string(duplicateIDLcConfig), expectedParsingErr: nil, expectedValidationErr: errLifecycleDuplicateID, diff --git a/pkg/bucket/lifecycle/rule.go b/pkg/bucket/lifecycle/rule.go index eadfbc852..339e611c1 100644 --- a/pkg/bucket/lifecycle/rule.go +++ b/pkg/bucket/lifecycle/rule.go @@ -19,6 +19,8 @@ package lifecycle import ( "bytes" "encoding/xml" + + "github.com/google/uuid" ) // Status represents lifecycle configuration status @@ -44,16 +46,34 @@ type Rule struct { } var ( - errInvalidRuleID = Errorf("ID must be less than 255 characters") + 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") ) +// generates random UUID +func getNewUUID() (string, error) { + u, err := uuid.NewRandom() + if err != nil { + return "", err + } + + return u.String(), nil +} + // validateID - checks if ID is valid or not. func (r Rule) validateID() error { + IDLen := len(string(r.ID)) + // generate new ID when not provided // cannot be longer than 255 characters - if len(string(r.ID)) > 255 { + if IDLen == 0 { + if newID, err := getNewUUID(); err == nil { + r.ID = newID + } else { + return err + } + } else if IDLen > 255 { return errInvalidRuleID } return nil diff --git a/pkg/bucket/lifecycle/rule_test.go b/pkg/bucket/lifecycle/rule_test.go index 7534fba17..da9bc9fa3 100644 --- a/pkg/bucket/lifecycle/rule_test.go +++ b/pkg/bucket/lifecycle/rule_test.go @@ -64,6 +64,7 @@ func TestInvalidRules(t *testing.T) { }{ { // Rule without expiration action inputXML: ` + rule without expiration Enabled `, expectedErr: errMissingExpirationAction, @@ -74,14 +75,26 @@ func TestInvalidRules(t *testing.T) { `, expectedErr: errInvalidRuleID, }, + { // Rule with empty ID + inputXML: ` + + + 365 + + Enabled + `, + expectedErr: nil, + }, { // Rule with empty status inputXML: ` + rule with empty status `, expectedErr: errEmptyRuleStatus, }, { // Rule with invalid status inputXML: ` + rule with invalid status OK `, expectedErr: errInvalidRuleStatus,