make sure lifecycle rule ID is present (#10084)

master
findmyname666 4 years ago committed by GitHub
parent 30104cb12b
commit aa6468932b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pkg/bucket/lifecycle/lifecycle_test.go
  2. 24
      pkg/bucket/lifecycle/rule.go
  3. 13
      pkg/bucket/lifecycle/rule_test.go

@ -146,7 +146,7 @@ func TestParseAndValidateLifecycleConfig(t *testing.T) {
expectedParsingErr: nil, expectedParsingErr: nil,
expectedValidationErr: nil, expectedValidationErr: nil,
}, },
{ // lifecycle config with rules having overlapping prefix { // lifecycle config with rules having duplicate ID
inputConfig: string(duplicateIDLcConfig), inputConfig: string(duplicateIDLcConfig),
expectedParsingErr: nil, expectedParsingErr: nil,
expectedValidationErr: errLifecycleDuplicateID, expectedValidationErr: errLifecycleDuplicateID,

@ -19,6 +19,8 @@ package lifecycle
import ( import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"github.com/google/uuid"
) )
// Status represents lifecycle configuration status // Status represents lifecycle configuration status
@ -44,16 +46,34 @@ type Rule struct {
} }
var ( 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") errEmptyRuleStatus = Errorf("Status should not be empty")
errInvalidRuleStatus = Errorf("Status must be set to either Enabled or Disabled") errInvalidRuleStatus = Errorf("Status must be set to either Enabled or Disabled")
errMissingExpirationAction = Errorf("No expiration action found") 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. // validateID - checks if ID is valid or not.
func (r Rule) validateID() error { func (r Rule) validateID() error {
IDLen := len(string(r.ID))
// generate new ID when not provided
// cannot be longer than 255 characters // 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 errInvalidRuleID
} }
return nil return nil

@ -64,6 +64,7 @@ func TestInvalidRules(t *testing.T) {
}{ }{
{ // Rule without expiration action { // Rule without expiration action
inputXML: ` <Rule> inputXML: ` <Rule>
<ID>rule without expiration</ID>
<Status>Enabled</Status> <Status>Enabled</Status>
</Rule>`, </Rule>`,
expectedErr: errMissingExpirationAction, expectedErr: errMissingExpirationAction,
@ -74,14 +75,26 @@ func TestInvalidRules(t *testing.T) {
</Rule>`, </Rule>`,
expectedErr: errInvalidRuleID, expectedErr: errInvalidRuleID,
}, },
{ // Rule with empty ID
inputXML: `<Rule>
<ID></ID>
<Expiration>
<Days>365</Days>
</Expiration>
<Status>Enabled</Status>
</Rule>`,
expectedErr: nil,
},
{ // Rule with empty status { // Rule with empty status
inputXML: ` <Rule> inputXML: ` <Rule>
<ID>rule with empty status</ID>
<Status></Status> <Status></Status>
</Rule>`, </Rule>`,
expectedErr: errEmptyRuleStatus, expectedErr: errEmptyRuleStatus,
}, },
{ // Rule with invalid status { // Rule with invalid status
inputXML: ` <Rule> inputXML: ` <Rule>
<ID>rule with invalid status</ID>
<Status>OK</Status> <Status>OK</Status>
</Rule>`, </Rule>`,
expectedErr: errInvalidRuleStatus, expectedErr: errInvalidRuleStatus,

Loading…
Cancel
Save