Skip to content

Commit

Permalink
Rename internal errors to s3errors (#803)
Browse files Browse the repository at this point in the history
close #786
  • Loading branch information
roman-khimov authored Jul 20, 2023
2 parents 1ee16bd + cad239e commit 943c787
Show file tree
Hide file tree
Showing 36 changed files with 259 additions and 259 deletions.
24 changes: 12 additions & 12 deletions api/auth/center.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
v4 "github.com/nspcc-dev/neofs-s3-gw/api/auth/signer/v4"
"github.com/nspcc-dev/neofs-s3-gw/api/cache"
apiErrors "github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
"github.com/nspcc-dev/neofs-s3-gw/creds/tokens"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
Expand Down Expand Up @@ -104,12 +104,12 @@ func New(neoFS tokens.NeoFS, key *keys.PrivateKey, prefixes []string, config *ca
func (c *center) parseAuthHeader(header string) (*authHeader, error) {
submatches := c.reg.GetSubmatches(header)
if len(submatches) != authHeaderPartsNum {
return nil, apiErrors.GetAPIError(apiErrors.ErrAuthorizationHeaderMalformed)
return nil, s3errors.GetAPIError(s3errors.ErrAuthorizationHeaderMalformed)
}

accessKey := strings.Split(submatches["access_key_id"], "0")
if len(accessKey) != accessKeyPartsNum {
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidAccessKeyID)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidAccessKeyID)
}

signedFields := strings.Split(submatches["signed_header_fields"], ";")
Expand All @@ -127,7 +127,7 @@ func (c *center) parseAuthHeader(header string) (*authHeader, error) {
func (a *authHeader) getAddress() (oid.Address, error) {
var addr oid.Address
if err := addr.DecodeString(strings.ReplaceAll(a.AccessKeyID, "0", "/")); err != nil {
return addr, apiErrors.GetAPIError(apiErrors.ErrInvalidAccessKeyID)
return addr, s3errors.GetAPIError(s3errors.ErrInvalidAccessKeyID)
}
return addr, nil
}
Expand Down Expand Up @@ -219,12 +219,12 @@ func (c center) checkAccessKeyID(accessKeyID string) error {
}
}

return apiErrors.GetAPIError(apiErrors.ErrAccessDenied)
return s3errors.GetAPIError(s3errors.ErrAccessDenied)
}

func (c *center) checkFormData(r *http.Request) (*Box, error) {
if err := r.ParseMultipartForm(maxFormSizeMemory); err != nil {
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidArgument)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidArgument)
}

if err := prepareForm(r.MultipartForm); err != nil {
Expand All @@ -238,7 +238,7 @@ func (c *center) checkFormData(r *http.Request) (*Box, error) {

submatches := c.postReg.GetSubmatches(MultipartFormValue(r, "x-amz-credential"))
if len(submatches) != 4 {
return nil, apiErrors.GetAPIError(apiErrors.ErrAuthorizationHeaderMalformed)
return nil, s3errors.GetAPIError(s3errors.ErrAuthorizationHeaderMalformed)
}

signatureDateTime, err := time.Parse("20060102T150405Z", MultipartFormValue(r, "x-amz-date"))
Expand All @@ -248,7 +248,7 @@ func (c *center) checkFormData(r *http.Request) (*Box, error) {

var addr oid.Address
if err = addr.DecodeString(strings.ReplaceAll(submatches["access_key_id"], "0", "/")); err != nil {
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidAccessKeyID)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidAccessKeyID)
}

box, err := c.cli.GetBox(r.Context(), addr)
Expand All @@ -261,7 +261,7 @@ func (c *center) checkFormData(r *http.Request) (*Box, error) {

signature := signStr(secret, service, region, signatureDateTime, policy)
if signature != MultipartFormValue(r, "x-amz-signature") {
return nil, apiErrors.GetAPIError(apiErrors.ErrSignatureDoesNotMatch)
return nil, s3errors.GetAPIError(s3errors.ErrSignatureDoesNotMatch)
}

return &Box{AccessBox: box}, nil
Expand Down Expand Up @@ -296,10 +296,10 @@ func (c *center) checkSign(authHeader *authHeader, box *accessbox.Box, request *
if authHeader.IsPresigned {
now := time.Now()
if signatureDateTime.Add(authHeader.Expiration).Before(now) {
return apiErrors.GetAPIError(apiErrors.ErrExpiredPresignRequest)
return s3errors.GetAPIError(s3errors.ErrExpiredPresignRequest)
}
if now.Before(signatureDateTime) {
return apiErrors.GetAPIError(apiErrors.ErrBadRequest)
return s3errors.GetAPIError(s3errors.ErrBadRequest)
}
if _, err := signer.Presign(request, nil, authHeader.Service, authHeader.Region, authHeader.Expiration, signatureDateTime); err != nil {
return fmt.Errorf("failed to pre-sign temporary HTTP request: %w", err)
Expand All @@ -314,7 +314,7 @@ func (c *center) checkSign(authHeader *authHeader, box *accessbox.Box, request *
}

if authHeader.SignatureV4 != signature {
return apiErrors.GetAPIError(apiErrors.ErrSignatureDoesNotMatch)
return s3errors.GetAPIError(s3errors.ErrSignatureDoesNotMatch)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions api/auth/center_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -35,12 +35,12 @@ func TestAuthHeaderParse(t *testing.T) {
},
{
header: strings.ReplaceAll(defaultHeader, "Signature=2811ccb9e242f41426738fb1f", ""),
err: errors.GetAPIError(errors.ErrAuthorizationHeaderMalformed),
err: s3errors.GetAPIError(s3errors.ErrAuthorizationHeaderMalformed),
expected: nil,
},
{
header: strings.ReplaceAll(defaultHeader, "oid0cid", "oidcid"),
err: errors.GetAPIError(errors.ErrInvalidAccessKeyID),
err: s3errors.GetAPIError(s3errors.ErrInvalidAccessKeyID),
expected: nil,
},
} {
Expand All @@ -51,7 +51,7 @@ func TestAuthHeaderParse(t *testing.T) {
}

func TestAuthHeaderGetAddress(t *testing.T) {
defaulErr := errors.GetAPIError(errors.ErrInvalidAccessKeyID)
defaulErr := s3errors.GetAPIError(s3errors.ErrInvalidAccessKeyID)

for _, tc := range []struct {
authHeader *authHeader
Expand Down
12 changes: 6 additions & 6 deletions api/handler/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/data"
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
"github.com/nspcc-dev/neofs-sdk-go/eacl"
"github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
Expand Down Expand Up @@ -221,7 +221,7 @@ func (h *handler) PutBucketACLHandler(w http.ResponseWriter, r *http.Request) {
return
}
} else if err = xml.NewDecoder(r.Body).Decode(list); err != nil {
h.logAndSendError(w, "could not parse bucket acl", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
h.logAndSendError(w, "could not parse bucket acl", reqInfo, s3errors.GetAPIError(s3errors.ErrMalformedXML))
return
}

Expand Down Expand Up @@ -356,7 +356,7 @@ func (h *handler) PutObjectACLHandler(w http.ResponseWriter, r *http.Request) {
return
}
} else if err = xml.NewDecoder(r.Body).Decode(list); err != nil {
h.logAndSendError(w, "could not parse bucket acl", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
h.logAndSendError(w, "could not parse bucket acl", reqInfo, s3errors.GetAPIError(s3errors.ErrMalformedXML))
return
}

Expand Down Expand Up @@ -423,7 +423,7 @@ func checkOwner(info *data.BucketInfo, owner string) error {

// may need to convert owner to appropriate format
if info.Owner.String() != owner {
return errors.GetAPIError(errors.ErrAccessDenied)
return s3errors.GetAPIError(s3errors.ErrAccessDenied)
}

return nil
Expand Down Expand Up @@ -543,7 +543,7 @@ func parseGrantee(grantees string) ([]*Grantee, error) {
for _, pair := range split {
split2 := strings.Split(pair, "=")
if len(split2) != 2 {
return nil, errors.GetAPIError(errors.ErrInvalidArgument)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidArgument)
}

grantee, err := formGrantee(split2[0], split2[1])
Expand Down Expand Up @@ -601,7 +601,7 @@ func addPredefinedACP(acp *AccessControlPolicy, cannedACL string) (*AccessContro
Permission: awsPermRead,
})
default:
return nil, errors.GetAPIError(errors.ErrInvalidArgument)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidArgument)
}

return acp, nil
Expand Down
14 changes: 7 additions & 7 deletions api/handler/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/data"
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -101,7 +101,7 @@ func (h *handler) GetObjectAttributesHandler(w http.ResponseWriter, r *http.Requ
}

if err = encryptionParams.MatchObjectEncryption(layer.FormEncryptionInfo(info.Headers)); err != nil {
h.logAndSendError(w, "encryption doesn't match object", reqInfo, errors.GetAPIError(errors.ErrBadRequest), zap.Error(err))
h.logAndSendError(w, "encryption doesn't match object", reqInfo, s3errors.GetAPIError(s3errors.ErrBadRequest), zap.Error(err))
return
}

Expand Down Expand Up @@ -148,13 +148,13 @@ func parseGetObjectAttributeArgs(r *http.Request) (*GetObjectAttributesArgs, err

attributesVal := r.Header.Get(api.AmzObjectAttributes)
if attributesVal == "" {
return nil, errors.GetAPIError(errors.ErrInvalidAttributeName)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidAttributeName)
}

attributes := strings.Split(attributesVal, ",")
for _, a := range attributes {
if _, ok := validAttributes[a]; !ok {
return nil, errors.GetAPIError(errors.ErrInvalidAttributeName)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidAttributeName)
}
res.Attributes = append(res.Attributes, a)
}
Expand All @@ -164,13 +164,13 @@ func parseGetObjectAttributeArgs(r *http.Request) (*GetObjectAttributesArgs, err
if maxPartsVal == "" {
res.MaxParts = layer.MaxSizePartsList
} else if res.MaxParts, err = strconv.Atoi(maxPartsVal); err != nil || res.MaxParts < 0 {
return nil, errors.GetAPIError(errors.ErrInvalidMaxKeys)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidMaxKeys)
}

markerVal := r.Header.Get(api.AmzPartNumberMarker)
if markerVal != "" {
if res.PartNumberMarker, err = strconv.Atoi(markerVal); err != nil || res.PartNumberMarker < 0 {
return nil, errors.GetAPIError(errors.ErrInvalidPartNumberMarker)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidPartNumberMarker)
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func formUploadAttributes(info *data.ObjectInfo, maxParts, marker int) (*ObjectP
}
}
if !found {
return nil, errors.GetAPIError(errors.ErrInvalidPartNumberMarker)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidPartNumberMarker)
}
}

Expand Down
14 changes: 7 additions & 7 deletions api/handler/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/auth"
"github.com/nspcc-dev/neofs-s3-gw/api/data"
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
"github.com/nspcc-dev/neofs-sdk-go/session"
"go.uber.org/zap"
)
Expand All @@ -32,7 +32,7 @@ var copySourceMatcher = auth.NewRegexpMatcher(regexp.MustCompile(`^/?(?P<bucket_
func path2BucketObject(path string) (string, string, error) {
matches := copySourceMatcher.GetSubmatches(path)
if len(matches) != 2 {
return "", "", errors.GetAPIError(errors.ErrInvalidRequest)
return "", "", s3errors.GetAPIError(s3errors.ErrInvalidRequest)
}

return matches["bucket_name"], matches["object_name"], nil
Expand Down Expand Up @@ -111,7 +111,7 @@ func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
}

if isCopyingToItselfForbidden(reqInfo, srcBucket, srcObject, settings, args) {
h.logAndSendError(w, "copying to itself without changing anything", reqInfo, errors.GetAPIError(errors.ErrInvalidCopyDest))
h.logAndSendError(w, "copying to itself without changing anything", reqInfo, s3errors.GetAPIError(s3errors.ErrInvalidCopyDest))
return
}

Expand Down Expand Up @@ -149,12 +149,12 @@ func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
}

if err = encryptionParams.MatchObjectEncryption(layer.FormEncryptionInfo(srcObjInfo.Headers)); err != nil {
h.logAndSendError(w, "encryption doesn't match object", reqInfo, errors.GetAPIError(errors.ErrBadRequest), zap.Error(err))
h.logAndSendError(w, "encryption doesn't match object", reqInfo, s3errors.GetAPIError(s3errors.ErrBadRequest), zap.Error(err))
return
}

if err = checkPreconditions(srcObjInfo, args.Conditional); err != nil {
h.logAndSendError(w, "precondition failed", reqInfo, errors.GetAPIError(errors.ErrPreconditionFailed))
h.logAndSendError(w, "precondition failed", reqInfo, s3errors.GetAPIError(s3errors.ErrPreconditionFailed))
return
}

Expand Down Expand Up @@ -288,12 +288,12 @@ func parseCopyObjectArgs(headers http.Header) (*copyObjectArgs, error) {

copyArgs.MetadataDirective = headers.Get(api.AmzMetadataDirective)
if !isValidDirective(copyArgs.MetadataDirective) {
return nil, errors.GetAPIError(errors.ErrInvalidMetadataDirective)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidMetadataDirective)
}

copyArgs.TaggingDirective = headers.Get(api.AmzTaggingDirective)
if !isValidDirective(copyArgs.TaggingDirective) {
return nil, errors.GetAPIError(errors.ErrInvalidTaggingDirective)
return nil, s3errors.GetAPIError(s3errors.ErrInvalidTaggingDirective)
}

return copyArgs, nil
Expand Down
8 changes: 4 additions & 4 deletions api/handler/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"

"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -145,12 +145,12 @@ func (h *handler) Preflight(w http.ResponseWriter, r *http.Request) {

origin := r.Header.Get(api.Origin)
if origin == "" {
h.logAndSendError(w, "origin request header needed", reqInfo, errors.GetAPIError(errors.ErrBadRequest))
h.logAndSendError(w, "origin request header needed", reqInfo, s3errors.GetAPIError(s3errors.ErrBadRequest))
}

method := r.Header.Get(api.AccessControlRequestMethod)
if method == "" {
h.logAndSendError(w, "Access-Control-Request-Method request header needed", reqInfo, errors.GetAPIError(errors.ErrBadRequest))
h.logAndSendError(w, "Access-Control-Request-Method request header needed", reqInfo, s3errors.GetAPIError(s3errors.ErrBadRequest))
return
}

Expand Down Expand Up @@ -197,7 +197,7 @@ func (h *handler) Preflight(w http.ResponseWriter, r *http.Request) {
}
}
}
h.logAndSendError(w, "Forbidden", reqInfo, errors.GetAPIError(errors.ErrAccessDenied))
h.logAndSendError(w, "Forbidden", reqInfo, s3errors.GetAPIError(s3errors.ErrAccessDenied))
}

func checkSubslice(slice []string, subSlice []string) bool {
Expand Down
12 changes: 6 additions & 6 deletions api/handler/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/data"
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
"github.com/nspcc-dev/neofs-s3-gw/api/s3errors"
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
"github.com/nspcc-dev/neofs-sdk-go/session"
Expand Down Expand Up @@ -86,7 +86,7 @@ func (h *handler) DeleteObjectHandler(w http.ResponseWriter, r *http.Request) {
deletedObject := deletedObjects[0]
if deletedObject.Error != nil {
if isErrObjectLocked(deletedObject.Error) {
h.logAndSendError(w, "object is locked", reqInfo, errors.GetAPIError(errors.ErrAccessDenied))
h.logAndSendError(w, "object is locked", reqInfo, s3errors.GetAPIError(s3errors.ErrAccessDenied))
} else {
h.logAndSendError(w, "could not delete object", reqInfo, deletedObject.Error)
}
Expand Down Expand Up @@ -158,21 +158,21 @@ func (h *handler) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Re
// Content-Md5 is required and should be set
// http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
if _, ok := r.Header[api.ContentMD5]; !ok {
h.logAndSendError(w, "missing Content-MD5", reqInfo, errors.GetAPIError(errors.ErrMissingContentMD5))
h.logAndSendError(w, "missing Content-MD5", reqInfo, s3errors.GetAPIError(s3errors.ErrMissingContentMD5))
return
}

// Content-Length is required and should be non-zero
// http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html
if r.ContentLength <= 0 {
h.logAndSendError(w, "missing Content-Length", reqInfo, errors.GetAPIError(errors.ErrMissingContentLength))
h.logAndSendError(w, "missing Content-Length", reqInfo, s3errors.GetAPIError(s3errors.ErrMissingContentLength))
return
}

// Unmarshal list of keys to be deleted.
requested := &DeleteObjectsRequest{}
if err := xml.NewDecoder(r.Body).Decode(requested); err != nil {
h.logAndSendError(w, "couldn't decode body", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
h.logAndSendError(w, "couldn't decode body", reqInfo, s3errors.GetAPIError(s3errors.ErrMalformedXML))
return
}

Expand Down Expand Up @@ -222,7 +222,7 @@ func (h *handler) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Re
for _, obj := range deletedObjects {
if obj.Error != nil {
code := "BadRequest"
if s3err, ok := obj.Error.(errors.Error); ok {
if s3err, ok := obj.Error.(s3errors.Error); ok {
code = s3err.Code
}
response.Errors = append(response.Errors, DeleteError{
Expand Down
Loading

0 comments on commit 943c787

Please sign in to comment.