Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return correct error on lock configuration check #888

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions api/handler/locking.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handler
import (
"context"
"encoding/xml"
"errors"
"fmt"
"net/http"
"strconv"
Expand All @@ -25,6 +26,11 @@ const (
legalHoldOff = "OFF"
)

var (
errEmptyDaysErrors = errors.New("you must specify Days or Years")
errNonEmptyDaysErrors = errors.New("you cannot specify Days and Years at the same time")
)

func (h *handler) PutBucketObjectLockConfigHandler(w http.ResponseWriter, r *http.Request) {
reqInfo := api.GetReqInfo(r.Context())

Expand Down Expand Up @@ -278,7 +284,7 @@ func (h *handler) GetObjectRetentionHandler(w http.ResponseWriter, r *http.Reque

func checkLockConfiguration(conf *data.ObjectLockConfiguration) error {
if conf.ObjectLockEnabled != "" && conf.ObjectLockEnabled != enabledValue {
return fmt.Errorf("invalid ObjectLockEnabled value: %s", conf.ObjectLockEnabled)
return s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, fmt.Errorf("invalid ObjectLockEnabled value: %s", conf.ObjectLockEnabled))
}

if conf.Rule == nil || conf.Rule.DefaultRetention == nil {
Expand All @@ -287,15 +293,15 @@ func checkLockConfiguration(conf *data.ObjectLockConfiguration) error {

retention := conf.Rule.DefaultRetention
if retention.Mode != governanceMode && retention.Mode != complianceMode {
return fmt.Errorf("invalid Mode value: %s", retention.Mode)
return s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, fmt.Errorf("invalid Mode value: %s", retention.Mode))
}

if retention.Days == 0 && retention.Years == 0 {
return fmt.Errorf("you must specify Days or Years")
if retention.Days <= 0 && retention.Years <= 0 {
return s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, errEmptyDaysErrors)
}

if retention.Days != 0 && retention.Years != 0 {
return fmt.Errorf("you cannot specify Days and Years at the same time")
return s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, errNonEmptyDaysErrors)
}

return nil
Expand Down
23 changes: 21 additions & 2 deletions api/handler/locking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/xml"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
Expand Down Expand Up @@ -282,11 +283,29 @@ func TestPutBucketLockConfigurationHandler(t *testing.T) {
expectedError: s3errors.GetAPIError(s3errors.ErrObjectLockConfigurationNotAllowed),
},
{
name: "invalid configuration",
name: "invalid ObjectLockEnabled",
bucket: bktLockEnabled,
expectedError: s3errors.GetAPIError(s3errors.ErrInternalError),
expectedError: s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, fmt.Errorf("invalid ObjectLockEnabled value: %s", "dummy")),
configuration: &data.ObjectLockConfiguration{ObjectLockEnabled: "dummy"},
},
{
name: "invalid retention mode",
bucket: bktLockEnabled,
expectedError: s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, fmt.Errorf("invalid Mode value: %s", "dummy")),
configuration: &data.ObjectLockConfiguration{Rule: &data.ObjectLockRule{DefaultRetention: &data.DefaultRetention{Mode: "dummy"}}},
},
{
name: "empty retention days and years",
bucket: bktLockEnabled,
expectedError: s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, errEmptyDaysErrors),
configuration: &data.ObjectLockConfiguration{Rule: &data.ObjectLockRule{DefaultRetention: &data.DefaultRetention{Mode: complianceMode}}},
},
{
name: "non empty retention days and years",
bucket: bktLockEnabled,
expectedError: s3errors.GetAPIErrorWithError(s3errors.ErrMalformedXML, errNonEmptyDaysErrors),
configuration: &data.ObjectLockConfiguration{Rule: &data.ObjectLockRule{DefaultRetention: &data.DefaultRetention{Mode: complianceMode, Days: 1, Years: 1}}},
},
{
name: "basic",
bucket: bktLockEnabled,
Expand Down
Loading