mirror of
https://github.com/versity/versitygw.git
synced 2026-09-07 00:26:54 +00:00
feat: global error refactoring
Fixes #2123 Fixes #2120 Fixes #2116 Fixes #2111 Fixes #2108 Fixes #2086 Fixes #2085 Fixes #2083 Fixes #2081 Fixes #2080 Fixes #2073 Fixes #2072 Fixes #2071 Fixes #2069 Fixes #2044 Fixes #2043 Fixes #2042 Fixes #2041 Fixes #2040 Fixes #2039 Fixes #2036 Fixes #2035 Fixes #2034 Fixes #2028 Fixes #2020 Fixes #1842 Fixes #1810 Fixes #1780 Fixes #1775 Fixes #1736 Fixes #1705 Fixes #1663 Fixes #1645 Fixes #1583 Fixes #1526 Fixes #1514 Fixes #1493 Fixes #1487 Fixes #959 Fixes #779 Closes #823 Closes #85 Refactor global S3 error handling around structured error types and centralized XML response generation. All S3 errors now share the common APIError base for the fields every error has: Code, HTTP status code, and Message. Non-traditional errors that need AWS-compatible XML fields now have dedicated typed errors in the s3err package. Each typed error implements the shared S3Error behavior so controllers and middleware can handle errors consistently while still emitting error-specific XML fields. Add a dedicated InvalidArgumentError type because InvalidArgument is used widely across request validation, auth, copy source handling, object lock validation, multipart validation, and header parsing. The new InvalidArgument path uses explicit InvalidArgErrorCode constants with predefined descriptions and ArgumentName values, keeping call sites readable while preserving the correct InvalidArgument XML shape and optional ArgumentValue. New structured errors added in s3err: - `AccessForbiddenError`: Method, ResourceType - `BadDigestError`: CalculatedDigest, ExpectedDigest - `BucketError`: BucketName - `ContentSHA256MismatchError`: ClientComputedContentSHA256, S3ComputedContentSHA256 - `EntityTooLargeError`: ProposedSize, MaxSizeAllowed - `EntityTooSmallError`: ProposedSize, MinSizeAllowed - `ExpiredPresignedURLError`: ServerTime, XAmzExpires, Expires - `InvalidAccessKeyIdError`: AWSAccessKeyId - `InvalidArgumentError`: Description, ArgumentName, ArgumentValue - `InvalidChunkSizeError`: Chunk, BadChunkSize - `InvalidDigestError`: ContentMD5 - `InvalidLocationConstraintError`: LocationConstraint - `InvalidPartError`: UploadId, PartNumber, ETag - `InvalidRangeError`: RangeRequested, ActualObjectSize - `InvalidTagError`: TagKey, TagValue - `KeyTooLongError`: Size, MaxSizeAllowed - `MetadataTooLargeError`: Size, MaxSizeAllowed - `MethodNotAllowedError`: Method, ResourceType, AllowedMethods - `NoSuchUploadError`: UploadId - `NoSuchVersionError`: Key, VersionId - `NotImplementedError`: Header, AdditionalMessage - `PreconditionFailedError`: Condition - `RequestTimeTooSkewedError`: RequestTime, ServerTime, MaxAllowedSkewMilliseconds - `SignatureDoesNotMatchError`: AWSAccessKeyId, StringToSign, SignatureProvided, StringToSignBytes, CanonicalRequest, CanonicalRequestBytes Fix CompleteMultipartUpload validation in the Azure backend so missing or empty `ETag` values return the appropriate S3 error instead of allowing a gateway panic. Fix presigned authentication expiration validation to compare server time in `UTC`, matching the `UTC` timestamp used by presigned URL signing. Add request ID and host ID support across S3 requests. Each request now receives AWS S3-like identifiers, returned in response headers as `x-amz-request-id` and `x-amz-id-2` and included in all XML error responses as RequestId and HostId. The generated ID structure is designed to resemble AWS S3 request IDs and host IDs. The request signature calculation/validation for streaming uploads was previously delayed until the request body was fully read, both for Authorization header authentication and presigned URLs. Now, the signature is validated immediately in the authorization middlewares without reading the request body, since the signature calculation itself does not depend on the request body. Instead, only the `x-amz-content-sha256` SHA-256 hash calculation is delayed.
This commit is contained in:
+23
-20
@@ -150,6 +150,8 @@ func ProcessHandlers(controller Controller, s3action string, svc *Services, hand
|
||||
// and metrics. It also handles the error parsing
|
||||
func WrapMiddleware(handler fiber.Handler, logger s3log.AuditLogger, mm metrics.Manager) fiber.Handler {
|
||||
return func(ctx *fiber.Ctx) error {
|
||||
requestID, hostID := utils.EnsureRequestIDs(ctx)
|
||||
|
||||
err := handler(ctx)
|
||||
if err != nil {
|
||||
if mm != nil {
|
||||
@@ -163,18 +165,19 @@ func WrapMiddleware(handler fiber.Handler, logger s3log.AuditLogger, mm metrics.
|
||||
|
||||
ctx.Response().Header.SetContentType(fiber.MIMEApplicationXML)
|
||||
|
||||
serr, ok := err.(s3err.APIError)
|
||||
if ok {
|
||||
ctx.Status(serr.HTTPStatusCode)
|
||||
return ctx.Send(s3err.GetAPIErrorResponse(serr, "", "", ""))
|
||||
if serr, ok := err.(s3err.S3Error); ok {
|
||||
if mnaErr, ok := serr.(s3err.MethodNotAllowedError); ok && len(mnaErr.AllowedMethods) != 0 {
|
||||
// for MethodNotAllowed errors, set the 'Allow' header
|
||||
ctx.Response().Header.Set("Allow", mnaErr.AllowedMethodsString())
|
||||
}
|
||||
return ctx.Status(serr.StatusCode()).Send(serr.XMLBody(requestID, hostID))
|
||||
}
|
||||
|
||||
debuglogger.InternalError(err)
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
|
||||
// If the error is not 's3err.APIError' return 'InternalError'
|
||||
return ctx.Send(s3err.GetAPIErrorResponse(
|
||||
s3err.GetAPIError(s3err.ErrInternalError), "", "", ""))
|
||||
// If the error is not 's3err.S3Error' return 'InternalError'
|
||||
return ctx.Send(s3err.GetAPIError(s3err.ErrInternalError).XMLBody(requestID, hostID))
|
||||
}
|
||||
|
||||
return ctx.Next()
|
||||
@@ -188,6 +191,7 @@ func ProcessController(ctx *fiber.Ctx, controller Controller, s3action string, s
|
||||
|
||||
// Set the response headers
|
||||
SetResponseHeaders(ctx, response.Headers)
|
||||
requestID, hostID := utils.EnsureRequestIDs(ctx)
|
||||
ensureExposeMetaHeaders(ctx)
|
||||
|
||||
opts := response.MetaOpts
|
||||
@@ -216,18 +220,17 @@ func ProcessController(ctx *fiber.Ctx, controller Controller, s3action string, s
|
||||
// set content type to application/xml
|
||||
ctx.Response().Header.SetContentType(fiber.MIMEApplicationXML)
|
||||
|
||||
serr, ok := err.(s3err.APIError)
|
||||
if ok {
|
||||
ctx.Status(serr.HTTPStatusCode)
|
||||
return ctx.Send(s3err.GetAPIErrorResponse(serr, "", "", ""))
|
||||
if serr, ok := err.(s3err.S3Error); ok {
|
||||
if mnaErr, ok := serr.(s3err.MethodNotAllowedError); ok && len(mnaErr.AllowedMethods) != 0 {
|
||||
ctx.Response().Header.Set("Allow", mnaErr.AllowedMethodsString())
|
||||
}
|
||||
return ctx.Status(serr.StatusCode()).Send(serr.XMLBody(requestID, hostID))
|
||||
}
|
||||
|
||||
debuglogger.InternalError(err)
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
|
||||
// If the error is not 's3err.APIError' return 'InternalError'
|
||||
return ctx.Send(s3err.GetAPIErrorResponse(
|
||||
s3err.GetAPIError(s3err.ErrInternalError), "", "", ""))
|
||||
// If the error is not 's3err.S3Error' return 'InternalError'
|
||||
return ctx.Status(http.StatusInternalServerError).Send(s3err.GetAPIError(s3err.ErrInternalError).XMLBody(requestID, hostID))
|
||||
}
|
||||
|
||||
// At this point, the S3 action has succeeded in the backend and
|
||||
@@ -276,8 +279,9 @@ func ProcessController(ctx *fiber.Ctx, controller Controller, s3action string, s
|
||||
ObjectSize: opts.ObjectSize,
|
||||
})
|
||||
}
|
||||
return ctx.Status(http.StatusInternalServerError).Send(s3err.GetAPIErrorResponse(
|
||||
s3err.GetAPIError(s3err.ErrInternalError), "", "", ""))
|
||||
|
||||
err := s3err.GetAPIError(s3err.ErrInternalError)
|
||||
return ctx.Status(err.HTTPStatusCode).Send(err.XMLBody(requestID, hostID))
|
||||
}
|
||||
|
||||
if len(responseBytes) > 0 {
|
||||
@@ -312,13 +316,12 @@ func ProcessController(ctx *fiber.Ctx, controller Controller, s3action string, s
|
||||
ObjectSize: opts.ObjectSize,
|
||||
})
|
||||
}
|
||||
ctx.Status(http.StatusInternalServerError)
|
||||
|
||||
// set content type to application/xml
|
||||
ctx.Response().Header.SetContentType(fiber.MIMEApplicationXML)
|
||||
|
||||
return ctx.Send(s3err.GetAPIErrorResponse(
|
||||
s3err.GetAPIError(s3err.ErrInternalError), "", "", ""))
|
||||
err := s3err.GetAPIError(s3err.ErrInternalError)
|
||||
return ctx.Status(err.HTTPStatusCode).Send(err.XMLBody(requestID, hostID))
|
||||
}
|
||||
res := make([]byte, 0, msglen)
|
||||
res = append(res, xmlhdr...)
|
||||
|
||||
@@ -39,6 +39,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
testRequestID = "5MRQJ97RHWJ4FMX9"
|
||||
testHostID = "eS8nILxNKeV1pNi2Z7Pv6mwC+nuquA2UTBwrBSxGq62e9NZ6f2G9aJPRetuD0/lF3OgqRF7N3GU="
|
||||
|
||||
defaultLocals map[utils.ContextKey]any = map[utils.ContextKey]any{
|
||||
utils.ContextKeyIsRoot: true,
|
||||
utils.ContextKeyParsedAcl: auth.ACL{
|
||||
@@ -109,7 +112,7 @@ func testController(t *testing.T, ctrl Controller, resp *Response, expectedErr e
|
||||
assert.Error(t, err)
|
||||
|
||||
switch expectedErr.(type) {
|
||||
case s3err.APIError:
|
||||
case s3err.S3Error:
|
||||
assert.EqualValues(t, expectedErr, err)
|
||||
default:
|
||||
assert.ErrorContains(t, err, expectedErr.Error())
|
||||
@@ -323,7 +326,7 @@ func TestProcessController(t *testing.T) {
|
||||
},
|
||||
expected: expected{
|
||||
status: http.StatusBadRequest,
|
||||
body: s3err.GetAPIErrorResponse(s3err.GetAPIError(s3err.ErrInvalidRequest), "", "", ""),
|
||||
body: s3err.GetAPIError(s3err.ErrInvalidRequest).XMLBody(testRequestID, testHostID),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -336,7 +339,7 @@ func TestProcessController(t *testing.T) {
|
||||
},
|
||||
expected: expected{
|
||||
status: http.StatusInternalServerError,
|
||||
body: s3err.GetAPIErrorResponse(s3err.GetAPIError(s3err.ErrInternalError), "", "", ""),
|
||||
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -351,7 +354,7 @@ func TestProcessController(t *testing.T) {
|
||||
},
|
||||
expected: expected{
|
||||
status: http.StatusInternalServerError,
|
||||
body: s3err.GetAPIErrorResponse(s3err.GetAPIError(s3err.ErrInternalError), "", "", ""),
|
||||
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -426,7 +429,7 @@ func TestProcessController(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "large paylod: should return internal error",
|
||||
name: "large payload: should return internal error",
|
||||
args: args{
|
||||
svc: services,
|
||||
controller: func(ctx *fiber.Ctx) (*Response, error) {
|
||||
@@ -464,7 +467,7 @@ func TestProcessController(t *testing.T) {
|
||||
},
|
||||
},
|
||||
expected: expected{
|
||||
body: s3err.GetAPIErrorResponse(s3err.GetAPIError(s3err.ErrInternalError), "", "", ""),
|
||||
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
||||
status: http.StatusInternalServerError,
|
||||
},
|
||||
},
|
||||
@@ -492,11 +495,15 @@ func TestProcessController(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := fiber.New().AcquireCtx(&fasthttp.RequestCtx{})
|
||||
utils.ContextKeyRequestID.Set(ctx, testRequestID)
|
||||
utils.ContextKeyHostID.Set(ctx, testHostID)
|
||||
err := ProcessController(ctx, tt.args.controller, metrics.ActionAbortMultipartUpload, tt.args.svc)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// check the status
|
||||
assert.Equal(t, tt.expected.status, ctx.Response().StatusCode())
|
||||
assert.Equal(t, testRequestID, string(ctx.Response().Header.Peek(utils.HeaderAmzRequestID)))
|
||||
assert.Equal(t, testHostID, string(ctx.Response().Header.Peek(utils.HeaderAmzID2)))
|
||||
|
||||
// check the response headers to be set
|
||||
if tt.expected.headers != nil {
|
||||
@@ -556,7 +563,7 @@ func TestProcessHandlers(t *testing.T) {
|
||||
svc: &Services{},
|
||||
},
|
||||
expected: expected{
|
||||
body: s3err.GetAPIErrorResponse(s3err.GetAPIError(s3err.ErrAccessDenied), "", "", ""),
|
||||
body: s3err.GetAPIError(s3err.ErrAccessDenied).XMLBody(testRequestID, testHostID),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -591,6 +598,9 @@ func TestProcessHandlers(t *testing.T) {
|
||||
app := fiber.New()
|
||||
|
||||
app.Post("/:bucket/*", func(ctx *fiber.Ctx) error {
|
||||
utils.ContextKeyRequestID.Set(ctx, testRequestID)
|
||||
utils.ContextKeyHostID.Set(ctx, testHostID)
|
||||
|
||||
// set the request locals
|
||||
if tt.args.locals != nil {
|
||||
for key, val := range tt.args.locals {
|
||||
@@ -654,7 +664,7 @@ func TestWrapMiddleware(t *testing.T) {
|
||||
logger: &mockAuditLogger{},
|
||||
},
|
||||
expected: expected{
|
||||
body: s3err.GetAPIErrorResponse(s3err.GetAPIError(s3err.ErrAclNotSupported), "", "", ""),
|
||||
body: s3err.GetAPIError(s3err.ErrAclNotSupported).XMLBody(testRequestID, testHostID),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -665,7 +675,7 @@ func TestWrapMiddleware(t *testing.T) {
|
||||
},
|
||||
},
|
||||
expected: expected{
|
||||
body: s3err.GetAPIErrorResponse(s3err.GetAPIError(s3err.ErrInternalError), "", "", ""),
|
||||
body: s3err.GetAPIError(s3err.ErrInternalError).XMLBody(testRequestID, testHostID),
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -675,6 +685,9 @@ func TestWrapMiddleware(t *testing.T) {
|
||||
app := fiber.New()
|
||||
|
||||
app.Post("/:bucket/*", func(ctx *fiber.Ctx) error {
|
||||
utils.ContextKeyRequestID.Set(ctx, testRequestID)
|
||||
utils.ContextKeyHostID.Set(ctx, testHostID)
|
||||
|
||||
// call the controller by passing the ctx
|
||||
err := mdlwr(ctx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -663,7 +663,7 @@ func TestS3ApiController_ListObjectVersions(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetInvalidMaxLimiterErr(utils.LimiterTypeMaxKeys),
|
||||
err: s3err.GetInvalidArgMaxLimiter(string(utils.LimiterTypeMaxKeys), "invalid"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -988,7 +988,7 @@ func TestS3ApiController_ListMultipartUploads(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetInvalidMaxLimiterErr(utils.LimiterTypeMaxUploads),
|
||||
err: s3err.GetInvalidArgMaxLimiter(string(utils.LimiterTypeMaxUploads), "invalid"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1095,7 +1095,7 @@ func TestS3ApiController_ListObjectsV2(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetNegativeMaxLimiterErr(utils.LimiterTypeMaxKeys),
|
||||
err: s3err.GetInvalidArgNegativeMaxLimiter(string(utils.LimiterTypeMaxKeys), "-1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1204,7 +1204,7 @@ func TestS3ApiController_ListObjects(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetInvalidMaxLimiterErr(utils.LimiterTypeMaxKeys),
|
||||
err: s3err.GetInvalidArgMaxLimiter(string(utils.LimiterTypeMaxKeys), "bla"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -51,7 +51,7 @@ func TestS3ApiController_ListBuckets(t *testing.T) {
|
||||
response: &Response{
|
||||
MetaOpts: &MetaOptions{},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidMaxBuckets),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMaxBuckets, "-1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -66,7 +66,7 @@ func TestS3ApiController_ListBuckets(t *testing.T) {
|
||||
response: &Response{
|
||||
MetaOpts: &MetaOptions{},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidMaxBuckets),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMaxBuckets, "10001"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -81,7 +81,7 @@ func TestS3ApiController_ListBuckets(t *testing.T) {
|
||||
response: &Response{
|
||||
MetaOpts: &MetaOptions{},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidMaxBuckets),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMaxBuckets, "0"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -96,7 +96,7 @@ func TestS3ApiController_ListBuckets(t *testing.T) {
|
||||
response: &Response{
|
||||
MetaOpts: &MetaOptions{},
|
||||
},
|
||||
err: s3err.GetInvalidMaxLimiterErr("max-buckets"),
|
||||
err: s3err.GetInvalidArgMaxLimiter("max-buckets", "bla"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -433,7 +433,7 @@ func TestS3ApiController_POSTObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrMetadataTooLarge),
|
||||
err: s3err.GetMetadataTooLargeErr(2053, 2048),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -17,7 +17,6 @@ package controllers
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -530,7 +529,7 @@ func (c S3ApiController) CreateBucket(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: bucketOwner.Access,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidBucketName)
|
||||
}, s3err.GetBucketErr(s3err.ErrInvalidBucketName, bucket)
|
||||
}
|
||||
|
||||
// both bucket canned ACL and acl grants is not allowed
|
||||
@@ -566,14 +565,10 @@ func (c S3ApiController) CreateBucket(ctx *fiber.Ctx) (*Response, error) {
|
||||
// validate the object ownership value
|
||||
if ok := utils.IsValidOwnership(objectOwnership); !ok {
|
||||
return &Response{
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: bucketOwner.Access,
|
||||
},
|
||||
}, s3err.APIError{
|
||||
Code: "InvalidArgument",
|
||||
Description: fmt.Sprintf("Invalid x-amz-object-ownership header: %v", objectOwnership),
|
||||
HTTPStatusCode: http.StatusBadRequest,
|
||||
}
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: bucketOwner.Access,
|
||||
},
|
||||
}, s3err.GetInvalidArgObjectOwnership(string(objectOwnership))
|
||||
}
|
||||
|
||||
// any bucket ACL(canned, grants) is not allowed with object ownership 'BucketOwnerEnforced'
|
||||
@@ -610,7 +605,7 @@ func (c S3ApiController) CreateBucket(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: bucketOwner.Access,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidLocationConstraint)
|
||||
}, s3err.GetInvalidLocationConstraintErr(*body.LocationConstraint)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -737,7 +737,7 @@ func TestS3ApiController_CreateBucket(t *testing.T) {
|
||||
BucketOwner: adminAcc.Access,
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidBucketName),
|
||||
err: s3err.GetBucketErr(s3err.ErrInvalidBucketName, "invalid_bucket_name"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -770,7 +770,7 @@ func TestS3ApiController_CreateBucket(t *testing.T) {
|
||||
response: &Response{
|
||||
MetaOpts: &MetaOptions{BucketOwner: adminAcc.Access},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidArgument),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgCannedAcl, "invalid_acl"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -786,7 +786,7 @@ func TestS3ApiController_CreateBucket(t *testing.T) {
|
||||
response: &Response{
|
||||
MetaOpts: &MetaOptions{BucketOwner: adminAcc.Access},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidLocationConstraint),
|
||||
err: s3err.GetInvalidLocationConstraintErr("us-west-1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -805,11 +805,7 @@ func TestS3ApiController_CreateBucket(t *testing.T) {
|
||||
BucketOwner: adminAcc.Access,
|
||||
},
|
||||
},
|
||||
err: s3err.APIError{
|
||||
Code: "InvalidArgument",
|
||||
Description: "Invalid x-amz-object-ownership header: invalid_ownership",
|
||||
HTTPStatusCode: http.StatusBadRequest,
|
||||
},
|
||||
err: s3err.GetInvalidArgObjectOwnership("invalid_ownership"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1110,7 +1106,7 @@ func TestS3ApiController_PutBucketAcl(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidArgument),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgCannedAcl, "invalid_acl"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -457,7 +457,7 @@ func (c S3ApiController) GetObject(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: parsedAcl.Owner,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidPartNumber)
|
||||
}, s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, ctx.Query("partNumber"))
|
||||
}
|
||||
|
||||
if acceptRange != "" {
|
||||
@@ -533,7 +533,7 @@ func (c S3ApiController) GetObject(ctx *fiber.Ctx) (*Response, error) {
|
||||
BucketOwner: parsedAcl.Owner,
|
||||
Status: status,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidRange)
|
||||
}, s3err.GetInvalidRangeErr("", *res.ContentLength)
|
||||
}
|
||||
contentLen = int(*res.ContentLength)
|
||||
}
|
||||
|
||||
@@ -455,7 +455,7 @@ func TestS3ApiController_ListParts(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetInvalidMaxLimiterErr(utils.LimiterTypePartNumberMarker),
|
||||
err: s3err.GetInvalidArgMaxLimiter(string(utils.LimiterTypePartNumberMarker), "foo"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -472,7 +472,7 @@ func TestS3ApiController_ListParts(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetNegativeMaxLimiterErr(utils.LimiterTypeMaxParts),
|
||||
err: s3err.GetInvalidArgNegativeMaxLimiter(string(utils.LimiterTypeMaxParts), "-1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -580,7 +580,7 @@ func TestS3ApiController_GetObjectAttributes(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidObjectAttributes),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgObjectAttributes, "invalid_attribute"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -719,7 +719,7 @@ func TestS3ApiController_GetObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidPartNumber),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, "-2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -105,7 +105,7 @@ func (c S3ApiController) HeadObject(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: parsedAcl.Owner,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidPartNumber)
|
||||
}, s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, ctx.Query("partNumber"))
|
||||
}
|
||||
|
||||
if objRange != "" {
|
||||
|
||||
@@ -96,7 +96,7 @@ func TestS3ApiController_HeadObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidPartNumber),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, "-4"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -250,7 +250,7 @@ func TestS3ApiController_CreateMultipartUpload(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrMetadataTooLarge),
|
||||
err: s3err.GetMetadataTooLargeErr(2051, 2048),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -267,7 +267,7 @@ func TestS3ApiController_CreateMultipartUpload(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrObjectLockInvalidHeaders),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMissingObjectLockRetainDate, ""),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -362,17 +362,9 @@ func TestS3ApiController_CompleteMultipartUpload(t *testing.T) {
|
||||
Parts: []types.CompletedPart{},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
pn := int32(1)
|
||||
|
||||
validMpBody, err := xml.Marshal(s3response.CompleteMultipartUploadRequestBody{
|
||||
Parts: []types.CompletedPart{
|
||||
{
|
||||
PartNumber: &pn,
|
||||
ETag: utils.GetStringPtr("ETag"),
|
||||
},
|
||||
},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
validMpBody := []byte(`<CompleteMultipartUpload xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Part><PartNumber>1</PartNumber><ETag>ETag</ETag></Part></CompleteMultipartUpload>`)
|
||||
s3cmdMpBody := []byte("<CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>ETag</ETag></Part></CompleteMultipartUpload>")
|
||||
|
||||
versionId, ETag := "versionId", "mock-ETag"
|
||||
|
||||
@@ -569,6 +561,32 @@ func TestS3ApiController_CompleteMultipartUpload(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "successful response with s3cmd request body",
|
||||
input: testInput{
|
||||
locals: defaultLocals,
|
||||
body: s3cmdMpBody,
|
||||
beRes: s3response.CompleteMultipartUploadResult{ETag: &ETag},
|
||||
extraMockErr: s3err.GetAPIError(s3err.ErrObjectLockConfigurationNotFound),
|
||||
},
|
||||
output: testOutput{
|
||||
response: &Response{
|
||||
Data: s3response.CompleteMultipartUploadResult{
|
||||
ETag: &ETag,
|
||||
Location: utils.GetStringPtr("http://example.com/bucket/object"),
|
||||
},
|
||||
Headers: map[string]*string{
|
||||
"x-amz-version-id": &versionId,
|
||||
},
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: "root",
|
||||
EventName: s3event.EventCompleteMultipartUpload,
|
||||
VersionId: &versionId,
|
||||
ObjectETag: &ETag,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
@@ -261,7 +261,7 @@ func (c S3ApiController) UploadPart(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: parsedAcl.Owner,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidPartNumber)
|
||||
}, s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, ctx.Query("partNumber"))
|
||||
}
|
||||
|
||||
contentLength, err := strconv.ParseInt(contentLengthStr, 10, 64)
|
||||
@@ -396,7 +396,7 @@ func (c S3ApiController) UploadPartCopy(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: parsedAcl.Owner,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidPartNumber)
|
||||
}, s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, ctx.Query("partNumber"))
|
||||
}
|
||||
|
||||
preconditionHdrs := utils.ParsePreconditionHeaders(ctx, utils.WithCopySource())
|
||||
@@ -566,7 +566,7 @@ func (c S3ApiController) CopyObject(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: parsedAcl.Owner,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidMetadataDirective)
|
||||
}, s3err.GetInvalidArgumentErr(s3err.InvalidArgMetadataDirective, string(metaDirective))
|
||||
}
|
||||
|
||||
if taggingDirective != "" && taggingDirective != types.TaggingDirectiveCopy && taggingDirective != types.TaggingDirectiveReplace {
|
||||
@@ -575,7 +575,7 @@ func (c S3ApiController) CopyObject(ctx *fiber.Ctx) (*Response, error) {
|
||||
MetaOpts: &MetaOptions{
|
||||
BucketOwner: parsedAcl.Owner,
|
||||
},
|
||||
}, s3err.GetAPIError(s3err.ErrInvalidTaggingDirective)
|
||||
}, s3err.GetInvalidArgumentErr(s3err.InvalidArgTaggingDirective, string(taggingDirective))
|
||||
}
|
||||
|
||||
checksumAlgorithm := types.ChecksumAlgorithm(ctx.Get("x-amz-checksum-algorithm"))
|
||||
|
||||
@@ -439,7 +439,7 @@ func TestS3ApiController_UploadPart(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidPartNumber),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, "-2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -620,7 +620,7 @@ func TestS3ApiController_UploadPartCopy(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidCopySourceEncoding),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceEncoding, "bad%G1"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -661,7 +661,7 @@ func TestS3ApiController_UploadPartCopy(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidPartNumber),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgPartNumber, "-2"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -860,7 +860,7 @@ func TestS3ApiController_CopyObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidCopySourceBucket),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceBucket, ""),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -896,7 +896,7 @@ func TestS3ApiController_CopyObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrMetadataTooLarge),
|
||||
err: s3err.GetMetadataTooLargeErr(2051, 2048),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -916,7 +916,7 @@ func TestS3ApiController_CopyObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidMetadataDirective),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMetadataDirective, "invalid_metadat_directive"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -934,7 +934,7 @@ func TestS3ApiController_CopyObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrInvalidTaggingDirective),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgTaggingDirective, "invalid_tagging_directive"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -970,7 +970,7 @@ func TestS3ApiController_CopyObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrObjectLockInvalidHeaders),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMissingObjectLockRetainDate, ""),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1159,7 +1159,7 @@ func TestS3ApiController_PutObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrMetadataTooLarge),
|
||||
err: s3err.GetMetadataTooLargeErr(2059, 2048),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -1177,7 +1177,7 @@ func TestS3ApiController_PutObject(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrObjectLockInvalidHeaders),
|
||||
err: s3err.GetInvalidArgumentErr(s3err.InvalidArgMissingObjectLockRetainDate, ""),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@ package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/versity/versitygw/auth"
|
||||
@@ -32,6 +33,7 @@ func (s S3ApiController) CORSOptions(ctx *fiber.Ctx) (*Response, error) {
|
||||
origin := ctx.Get("Origin")
|
||||
method := auth.CORSHTTPMethod(ctx.Get("Access-Control-Request-Method"))
|
||||
headers := ctx.Get("Access-Control-Request-Headers")
|
||||
resourceType := utils.DetectResourceType(ctx)
|
||||
|
||||
// Origin is required
|
||||
if origin == "" {
|
||||
@@ -67,7 +69,8 @@ func (s S3ApiController) CORSOptions(ctx *fiber.Ctx) (*Response, error) {
|
||||
if err != nil {
|
||||
debuglogger.Logf("failed to get bucket cors: %v", err)
|
||||
if errors.Is(err, s3err.GetAPIError(s3err.ErrNoSuchCORSConfiguration)) {
|
||||
err = s3err.GetAPIError(s3err.ErrCORSIsNotEnabled)
|
||||
// weirdly s3 always returns BUCKET resource type
|
||||
err = s3err.GetAccessForbiddenErr(s3err.ErrCORSIsNotEnabled, http.MethodOptions, s3err.ResourceTypeBucket)
|
||||
debuglogger.Logf("bucket cors is not set: %v", err)
|
||||
}
|
||||
return &Response{
|
||||
@@ -86,7 +89,7 @@ func (s S3ApiController) CORSOptions(ctx *fiber.Ctx) (*Response, error) {
|
||||
}, err
|
||||
}
|
||||
|
||||
allowConfig, err := corsConfig.IsAllowed(origin, method, parsedHeaders)
|
||||
allowConfig, err := corsConfig.IsAllowed(origin, method, parsedHeaders, resourceType)
|
||||
if err != nil {
|
||||
debuglogger.Logf("cors access forbidden: %v", err)
|
||||
return &Response{
|
||||
|
||||
@@ -143,7 +143,7 @@ func TestS3ApiController_CORSOptions(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrCORSIsNotEnabled),
|
||||
err: s3err.GetAccessForbiddenErr(s3err.ErrCORSIsNotEnabled, http.MethodOptions, s3err.ResourceTypeBucket),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -183,7 +183,7 @@ func TestS3ApiController_CORSOptions(t *testing.T) {
|
||||
BucketOwner: "root",
|
||||
},
|
||||
},
|
||||
err: s3err.GetAPIError(s3err.ErrCORSForbidden),
|
||||
err: s3err.GetAccessForbiddenErr(s3err.ErrCORSForbidden, http.MethodOptions, s3err.ResourceTypeObject),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user