mirror of
https://github.com/minio/minio.git
synced 2025-01-24 13:13:16 -05:00
make sure lifecycle rule ID is present (#10084)
This commit is contained in:
parent
30104cb12b
commit
aa6468932b
@ -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,
|
||||
|
@ -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
|
||||
|
@ -64,6 +64,7 @@ func TestInvalidRules(t *testing.T) {
|
||||
}{
|
||||
{ // Rule without expiration action
|
||||
inputXML: ` <Rule>
|
||||
<ID>rule without expiration</ID>
|
||||
<Status>Enabled</Status>
|
||||
</Rule>`,
|
||||
expectedErr: errMissingExpirationAction,
|
||||
@ -74,14 +75,26 @@ func TestInvalidRules(t *testing.T) {
|
||||
</Rule>`,
|
||||
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
|
||||
inputXML: ` <Rule>
|
||||
<ID>rule with empty status</ID>
|
||||
<Status></Status>
|
||||
</Rule>`,
|
||||
expectedErr: errEmptyRuleStatus,
|
||||
},
|
||||
{ // Rule with invalid status
|
||||
inputXML: ` <Rule>
|
||||
<ID>rule with invalid status</ID>
|
||||
<Status>OK</Status>
|
||||
</Rule>`,
|
||||
expectedErr: errInvalidRuleStatus,
|
||||
|
Loading…
x
Reference in New Issue
Block a user