mirror of
https://github.com/versity/versitygw.git
synced 2026-09-24 08:54:47 +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:
+30
-26
@@ -230,9 +230,9 @@ func (az *Azure) CreateBucket(ctx context.Context, input *s3.CreateBucketInput,
|
||||
}
|
||||
|
||||
if acl.Owner == acct.Access {
|
||||
return s3err.GetAPIError(s3err.ErrBucketAlreadyOwnedByYou)
|
||||
return s3err.GetBucketErr(s3err.ErrBucketAlreadyOwnedByYou, *input.Bucket)
|
||||
}
|
||||
return s3err.GetAPIError(s3err.ErrBucketAlreadyExists)
|
||||
return s3err.GetBucketErr(s3err.ErrBucketAlreadyExists, *input.Bucket)
|
||||
}
|
||||
return azureErrToS3Err(err)
|
||||
}
|
||||
@@ -322,7 +322,6 @@ func (az *Azure) DeleteBucket(ctx context.Context, bucket string) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err := az.client.DeleteContainer(ctx, bucket, nil)
|
||||
return azureErrToS3Err(err)
|
||||
}
|
||||
@@ -338,7 +337,7 @@ func (az *Azure) GetBucketOwnershipControls(ctx context.Context, bucket string)
|
||||
return ownship, err
|
||||
}
|
||||
if len(ownership) == 0 {
|
||||
return ownship, s3err.GetAPIError(s3err.ErrOwnershipControlsNotFound)
|
||||
return ownship, s3err.GetBucketErr(s3err.ErrOwnershipControlsNotFound, bucket)
|
||||
}
|
||||
|
||||
return types.ObjectOwnership(ownership), nil
|
||||
@@ -451,7 +450,7 @@ func (az *Azure) GetBucketTagging(ctx context.Context, bucket string) (map[strin
|
||||
}
|
||||
|
||||
if len(tagsJson) == 0 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrBucketTaggingNotFound)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrBucketTaggingNotFound, bucket)
|
||||
}
|
||||
|
||||
var tags map[string]string
|
||||
@@ -514,7 +513,7 @@ func (az *Azure) GetObject(ctx context.Context, input *s3.GetObjectInput) (*s3.G
|
||||
totalParts := int32(len(mpMeta.Parts))
|
||||
partsCount = &totalParts
|
||||
if partNum > totalParts {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidPartNumberRange)
|
||||
return nil, s3err.GetInvalidPartNumberRangeErr(totalParts, partNum)
|
||||
}
|
||||
|
||||
var startOffset int64
|
||||
@@ -534,7 +533,7 @@ func (az *Azure) GetObject(ctx context.Context, input *s3.GetObjectInput) (*s3.G
|
||||
},
|
||||
}
|
||||
} else if *input.PartNumber > 1 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidPartNumberRange)
|
||||
return nil, s3err.GetInvalidPartNumberRangeErr(1, *input.PartNumber)
|
||||
} else {
|
||||
// partNumber=1 on a non-multipart object: fall through and serve the
|
||||
// full object without a range (opts remains nil)
|
||||
@@ -636,7 +635,7 @@ func (az *Azure) HeadObject(ctx context.Context, input *s3.HeadObjectInput) (*s3
|
||||
totalParts := int32(len(mpMeta.Parts))
|
||||
partsCount = &totalParts
|
||||
if partNum > totalParts {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidPartNumberRange)
|
||||
return nil, s3err.GetInvalidPartNumberRangeErr(totalParts, partNum)
|
||||
}
|
||||
|
||||
var startOffset int64
|
||||
@@ -646,7 +645,7 @@ func (az *Azure) HeadObject(ctx context.Context, input *s3.HeadObjectInput) (*s3
|
||||
length = mpMeta.Parts[partNum-1] - startOffset
|
||||
contentRange = backend.GetPtrFromString(fmt.Sprintf("bytes %d-%d/%d", startOffset, startOffset+length-1, size))
|
||||
} else if *input.PartNumber > 1 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidPartNumberRange)
|
||||
return nil, s3err.GetInvalidPartNumberRangeErr(1, *input.PartNumber)
|
||||
} else {
|
||||
// partNumber=1 on a non-multipart object: return full object size,
|
||||
// no Content-Range, no PartsCount.
|
||||
@@ -1093,12 +1092,14 @@ func (az *Azure) DeleteObjects(ctx context.Context, input *s3.DeleteObjectsInput
|
||||
if err == nil {
|
||||
delResult = append(delResult, types.DeletedObject{Key: obj.Key})
|
||||
} else {
|
||||
serr, ok := err.(s3err.APIError)
|
||||
serr, ok := err.(s3err.S3Error)
|
||||
if ok {
|
||||
code := serr.BaseError().Code
|
||||
message := serr.BaseError().Description
|
||||
errs = append(errs, types.Error{
|
||||
Key: obj.Key,
|
||||
Code: &serr.Code,
|
||||
Message: &serr.Description,
|
||||
Code: &code,
|
||||
Message: &message,
|
||||
})
|
||||
} else {
|
||||
errs = append(errs, types.Error{
|
||||
@@ -1543,7 +1544,7 @@ func (az *Azure) ListParts(ctx context.Context, input *s3.ListPartsInput) (s3res
|
||||
partNumberMarker, err = strconv.Atoi(*input.PartNumberMarker)
|
||||
if err != nil {
|
||||
return s3response.ListPartsResult{},
|
||||
s3err.GetInvalidMaxLimiterErr("part-number-marker")
|
||||
s3err.GetInvalidArgMaxLimiter("part-number-marker", *input.PartNumberMarker)
|
||||
}
|
||||
}
|
||||
if input.MaxParts != nil {
|
||||
@@ -1726,7 +1727,7 @@ func (az *Azure) AbortMultipartUpload(ctx context.Context, input *s3.AbortMultip
|
||||
}
|
||||
|
||||
if resp.LastModified != nil && resp.LastModified.Unix() != input.IfMatchInitiatedTime.Unix() {
|
||||
return s3err.GetAPIError(s3err.ErrPreconditionFailed)
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfMatchInitiatedTime)
|
||||
}
|
||||
}
|
||||
_, err := az.client.DeleteBlob(ctx, *input.Bucket, tmpPath, nil)
|
||||
@@ -1823,7 +1824,7 @@ func (az *Azure) CompleteMultipartUpload(ctx context.Context, input *s3.Complete
|
||||
}
|
||||
|
||||
if len(blockList.UncommittedBlocks)+len(zbParts) != len(input.MultipartUpload.Parts) {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrInvalidPart)
|
||||
return res, "", s3err.GetInvalidPartErr(*input.UploadId, 0, "")
|
||||
}
|
||||
|
||||
uncommittedBlocks := map[int32]*blockblob.Block{}
|
||||
@@ -1844,10 +1845,13 @@ func (az *Azure) CompleteMultipartUpload(ctx context.Context, input *s3.Complete
|
||||
last := len(input.MultipartUpload.Parts) - 1
|
||||
for i, part := range input.MultipartUpload.Parts {
|
||||
if part.PartNumber == nil {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrInvalidPart)
|
||||
return res, "", s3err.GetAPIError(s3err.ErrMalformedXML)
|
||||
}
|
||||
if part.ETag == nil {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrMalformedXML)
|
||||
}
|
||||
if *part.PartNumber < 1 {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrInvalidCompleteMpPartNumber)
|
||||
return res, "", s3err.GetInvalidArgumentErr(s3err.InvalidArgCompleteMpPartNumber, fmt.Sprint(*part.PartNumber))
|
||||
}
|
||||
if *part.PartNumber <= partNumber {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrInvalidPartOrder)
|
||||
@@ -1860,26 +1864,26 @@ func (az *Azure) CompleteMultipartUpload(ctx context.Context, input *s3.Complete
|
||||
if zbPartsMap[*part.PartNumber] {
|
||||
expectedETag := blockIDInt32ToBase64(*part.PartNumber)
|
||||
if getString(part.ETag) != expectedETag {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrInvalidPart)
|
||||
return res, "", s3err.GetInvalidPartErr(*input.UploadId, *part.PartNumber, expectedETag)
|
||||
}
|
||||
// Non-last zero-byte parts violate the minimum part size.
|
||||
if i < last {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrEntityTooSmall)
|
||||
return res, "", s3err.GetEntityTooSmallErr(0, backend.MinPartSize)
|
||||
}
|
||||
// Zero-byte parts contribute no data; skip adding to blockIds.
|
||||
partSizes = append(partSizes, totalSize)
|
||||
continue
|
||||
}
|
||||
return res, "", s3err.GetAPIError(s3err.ErrInvalidPart)
|
||||
return res, "", s3err.GetInvalidPartErr(*input.UploadId, *part.PartNumber, "")
|
||||
}
|
||||
|
||||
if *part.ETag != *block.Name {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrInvalidPart)
|
||||
return res, "", s3err.GetInvalidPartErr(*input.UploadId, *part.PartNumber, getString(part.ETag))
|
||||
}
|
||||
// all parts except the last need to be greater, than
|
||||
// the minimum allowed size (5 Mib)
|
||||
if i < last && *block.Size < backend.MinPartSize {
|
||||
return res, "", s3err.GetAPIError(s3err.ErrEntityTooSmall)
|
||||
return res, "", s3err.GetEntityTooSmallErr(*block.Size, backend.MinPartSize)
|
||||
}
|
||||
totalSize += *block.Size
|
||||
partSizes = append(partSizes, totalSize)
|
||||
@@ -1958,7 +1962,7 @@ func (az *Azure) GetBucketPolicy(ctx context.Context, bucket string) ([]byte, er
|
||||
return nil, err
|
||||
}
|
||||
if len(p) == 0 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrNoSuchBucketPolicy)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrNoSuchBucketPolicy, bucket)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
@@ -1981,7 +1985,7 @@ func (az *Azure) GetBucketCors(ctx context.Context, bucket string) ([]byte, erro
|
||||
return nil, err
|
||||
}
|
||||
if len(p) == 0 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrNoSuchCORSConfiguration)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrNoSuchCORSConfiguration, bucket)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
@@ -2001,7 +2005,7 @@ func (az *Azure) GetObjectLockConfiguration(ctx context.Context, bucket string)
|
||||
}
|
||||
|
||||
if len(cfg) == 0 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrObjectLockConfigurationNotFound)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrObjectLockConfigurationNotFound, bucket)
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
@@ -2538,7 +2542,7 @@ func (az *Azure) checkIfMpExists(ctx context.Context, bucket, obj, uploadId stri
|
||||
|
||||
_, err = blobClient.GetProperties(ctx, nil)
|
||||
if err != nil {
|
||||
return s3err.GetAPIError(s3err.ErrNoSuchUpload)
|
||||
return s3err.GetNoSuchUploadErr(uploadId)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+44
-53
@@ -89,10 +89,7 @@ func TrimEtag(etag *string) *string {
|
||||
}
|
||||
|
||||
var (
|
||||
errInvalidRange = s3err.GetAPIError(s3err.ErrInvalidRange)
|
||||
errInvalidCopySourceRange = s3err.GetAPIError(s3err.ErrInvalidCopySourceRange)
|
||||
errPreconditionFailed = s3err.GetAPIError(s3err.ErrPreconditionFailed)
|
||||
errNotModified = s3err.GetAPIError(s3err.ErrNotModified)
|
||||
errNotModified = s3err.GetAPIError(s3err.ErrNotModified)
|
||||
)
|
||||
|
||||
// ParseObjectRange parses input range header and returns startoffset, length, isValid
|
||||
@@ -121,7 +118,7 @@ func ParseObjectRange(size int64, acceptRange string) (int64, int64, bool, error
|
||||
// Parse start; empty start indicates a suffix-byte-range-spec (e.g. bytes=-100)
|
||||
startOffset, err := strconv.ParseInt(bRange[0], 10, strconv.IntSize)
|
||||
if startOffset > int64(math.MaxInt) || startOffset < int64(math.MinInt) {
|
||||
return 0, size, false, errInvalidRange
|
||||
return 0, size, false, s3err.GetInvalidRangeErr(acceptRange, size)
|
||||
}
|
||||
if err != nil && bRange[0] != "" { // invalid numeric start (non-empty) -> ignore range
|
||||
return 0, size, false, nil
|
||||
@@ -134,7 +131,7 @@ func ParseObjectRange(size int64, acceptRange string) (int64, int64, bool, error
|
||||
}
|
||||
// start beyond or at size is unsatisfiable -> error (RequestedRangeNotSatisfiable)
|
||||
if startOffset >= size {
|
||||
return 0, 0, false, errInvalidRange
|
||||
return 0, 0, false, s3err.GetInvalidRangeErr(acceptRange, size)
|
||||
}
|
||||
// bytes=100- => from start to end
|
||||
return startOffset, size - startOffset, true, nil
|
||||
@@ -142,7 +139,7 @@ func ParseObjectRange(size int64, acceptRange string) (int64, int64, bool, error
|
||||
|
||||
endOffset, err := strconv.ParseInt(bRange[1], 10, strconv.IntSize)
|
||||
if endOffset > int64(math.MaxInt) {
|
||||
return 0, size, false, errInvalidRange
|
||||
return 0, size, false, s3err.GetInvalidRangeErr(acceptRange, size)
|
||||
}
|
||||
if err != nil { // invalid numeric end -> ignore range
|
||||
return 0, size, false, nil
|
||||
@@ -152,7 +149,7 @@ func ParseObjectRange(size int64, acceptRange string) (int64, int64, bool, error
|
||||
if bRange[0] == "" {
|
||||
// Disallow -0 (always unsatisfiable)
|
||||
if endOffset == 0 {
|
||||
return 0, 0, false, errInvalidRange
|
||||
return 0, 0, false, s3err.GetInvalidRangeErr(acceptRange, size)
|
||||
}
|
||||
// For zero-sized objects any positive suffix is treated as invalid (ignored, no error)
|
||||
if size == 0 {
|
||||
@@ -169,7 +166,7 @@ func ParseObjectRange(size int64, acceptRange string) (int64, int64, bool, error
|
||||
}
|
||||
// Start beyond or at end of object -> error
|
||||
if startOffset >= size {
|
||||
return 0, 0, false, errInvalidRange
|
||||
return 0, 0, false, s3err.GetInvalidRangeErr(acceptRange, size)
|
||||
}
|
||||
// Adjust end beyond object size (trim)
|
||||
if endOffset >= size {
|
||||
@@ -185,6 +182,7 @@ func ParseCopySourceRange(size int64, acceptRange string) (int64, int64, error)
|
||||
return 0, size, nil
|
||||
}
|
||||
|
||||
errInvalidCopySourceRange := s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceRange, acceptRange)
|
||||
rangeKv := strings.Split(acceptRange, "=")
|
||||
|
||||
if len(rangeKv) != 2 {
|
||||
@@ -206,7 +204,7 @@ func ParseCopySourceRange(size int64, acceptRange string) (int64, int64, error)
|
||||
}
|
||||
|
||||
if startOffset >= size {
|
||||
return 0, 0, s3err.CreateExceedingRangeErr(size)
|
||||
return 0, 0, s3err.GetInvalidArgExceedingRange(size)
|
||||
}
|
||||
|
||||
if bRange[1] == "" {
|
||||
@@ -223,7 +221,7 @@ func ParseCopySourceRange(size int64, acceptRange string) (int64, int64, error)
|
||||
}
|
||||
|
||||
if endOffset >= size {
|
||||
return 0, 0, s3err.CreateExceedingRangeErr(size)
|
||||
return 0, 0, s3err.GetInvalidArgExceedingRange(size)
|
||||
}
|
||||
|
||||
return startOffset, endOffset - startOffset + 1, nil
|
||||
@@ -252,12 +250,12 @@ func ParseCopySource(copySourceHeader string) (string, string, string, error) {
|
||||
// correctly before we split on a literal '/'.
|
||||
decoded, err := url.QueryUnescape(rawSource)
|
||||
if err != nil {
|
||||
return "", "", "", s3err.GetAPIError(s3err.ErrInvalidCopySourceEncoding)
|
||||
return "", "", "", s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceEncoding, rawSource)
|
||||
}
|
||||
|
||||
srcBucket, srcObject, ok := strings.Cut(decoded, "/")
|
||||
if !ok {
|
||||
return "", "", "", s3err.GetAPIError(s3err.ErrInvalidCopySourceBucket)
|
||||
return "", "", "", s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceBucket, rawSource)
|
||||
}
|
||||
|
||||
return srcBucket, srcObject, versionId, nil
|
||||
@@ -281,47 +279,47 @@ func ParseObjectTags(tagging string) (map[string]string, error) {
|
||||
}
|
||||
|
||||
key, value, found := strings.Cut(tag, "=")
|
||||
// if key is empty, but "=" is present, return invalid url ecnoding err
|
||||
// if key is empty, but "=" is present, return invalid url encoding err
|
||||
if found && key == "" {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidURLEncodedTagging)
|
||||
return nil, s3err.GetInvalidArgumentErr(s3err.InvalidArgURLEncodedTagging, tagging)
|
||||
}
|
||||
|
||||
// return invalid tag key, if the key is longer than 128
|
||||
if len(key) > 128 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagKey)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagKey, key, "")
|
||||
}
|
||||
|
||||
// return invalid tag value, if tag value is longer than 256
|
||||
if len(value) > 256 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagValue)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagValue, key, value)
|
||||
}
|
||||
|
||||
// query unescape tag key
|
||||
key, err := url.QueryUnescape(key)
|
||||
if err != nil {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidURLEncodedTagging)
|
||||
return nil, s3err.GetInvalidArgumentErr(s3err.InvalidArgURLEncodedTagging, tagging)
|
||||
}
|
||||
|
||||
// query unescape tag value
|
||||
value, err = url.QueryUnescape(value)
|
||||
if err != nil {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidURLEncodedTagging)
|
||||
return nil, s3err.GetInvalidArgumentErr(s3err.InvalidArgURLEncodedTagging, tagging)
|
||||
}
|
||||
|
||||
// check tag key to be valid
|
||||
if !isValidTagComponent(key) {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagKey)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagKey, key, "")
|
||||
}
|
||||
|
||||
// check tag value to be valid
|
||||
if !isValidTagComponent(value) {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagValue)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagValue, key, value)
|
||||
}
|
||||
|
||||
// duplicate keys are not allowed: return invalid url encoding err
|
||||
_, ok := tagSet[key]
|
||||
if ok {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidURLEncodedTagging)
|
||||
return nil, s3err.GetInvalidArgumentErr(s3err.InvalidArgURLEncodedTagging, tagging)
|
||||
}
|
||||
|
||||
tagSet[key] = value
|
||||
@@ -347,23 +345,23 @@ func ParseCreateBucketTags(tagging []types.Tag) (map[string]string, error) {
|
||||
// validate tag key length
|
||||
key := GetStringFromPtr(tag.Key)
|
||||
if len(key) == 0 || len(key) > 128 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagKey)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagKey, key, "")
|
||||
}
|
||||
|
||||
// validate tag key string chars
|
||||
if !isValidTagComponent(key) {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagKey)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagKey, key, "")
|
||||
}
|
||||
|
||||
// validate tag value length
|
||||
value := GetStringFromPtr(tag.Value)
|
||||
if len(value) > 256 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagValue)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagValue, key, value)
|
||||
}
|
||||
|
||||
// validate tag value string chars
|
||||
if !isValidTagComponent(value) {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidTagValue)
|
||||
return nil, s3err.GetInvalidTagErr(s3err.ErrInvalidTagValue, key, value)
|
||||
}
|
||||
|
||||
// make sure there are no duplicate keys
|
||||
@@ -390,9 +388,15 @@ func isValidTagComponent(str string) bool {
|
||||
func GetMultipartMD5(parts []types.CompletedPart) (string, error) {
|
||||
var partsEtagBytes []byte
|
||||
for _, part := range parts {
|
||||
if part.ETag == nil {
|
||||
return "", s3err.GetAPIError(s3err.ErrMalformedXML)
|
||||
}
|
||||
if part.PartNumber == nil {
|
||||
return "", s3err.GetAPIError(s3err.ErrMalformedXML)
|
||||
}
|
||||
bts, err := getEtagBytes(*part.ETag)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decode etag: %w", err)
|
||||
return "", s3err.GetAPIError(s3err.ErrInvalidPart)
|
||||
}
|
||||
partsEtagBytes = append(partsEtagBytes, bts...)
|
||||
}
|
||||
@@ -614,9 +618,9 @@ func EvaluatePreconditions(etag string, modTime time.Time, preconditions PreCond
|
||||
}
|
||||
|
||||
if ifMatch != nil {
|
||||
// if `if-match` doesn't matches, return PreconditionFailed
|
||||
// if `if-match` doesn't match, return PreconditionFailed
|
||||
if !*ifMatch {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfMatch)
|
||||
}
|
||||
|
||||
// if-match matches
|
||||
@@ -646,7 +650,7 @@ func EvaluatePreconditions(etag string, modTime time.Time, preconditions PreCond
|
||||
// if `if-none-match` is true, but `if-unmodified-since` is false
|
||||
// return PreconditionFailed
|
||||
if ifUnmodeSince != nil && !*ifUnmodeSince {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfUnmodifiedSince)
|
||||
}
|
||||
|
||||
// ignore `if-modified-since` as `if-none-match` is true
|
||||
@@ -655,7 +659,7 @@ func EvaluatePreconditions(etag string, modTime time.Time, preconditions PreCond
|
||||
// if `if-none-match` is false and `if-unmodified-since` is false
|
||||
// return PreconditionFailed
|
||||
if ifUnmodeSince != nil && !*ifUnmodeSince {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfUnmodifiedSince)
|
||||
}
|
||||
|
||||
// in all other cases when `if-none-match` is false return NotModified
|
||||
@@ -667,7 +671,7 @@ func EvaluatePreconditions(etag string, modTime time.Time, preconditions PreCond
|
||||
// if both `if-modified-since` and `if-unmodified-since` are false
|
||||
// return PreconditionFailed
|
||||
if ifUnmodeSince != nil && !*ifUnmodeSince {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfUnmodifiedSince)
|
||||
}
|
||||
|
||||
// if only `if-modified-since` is false, return NotModified
|
||||
@@ -676,20 +680,7 @@ func EvaluatePreconditions(etag string, modTime time.Time, preconditions PreCond
|
||||
|
||||
// if `if-unmodified-since` is false return PreconditionFailed
|
||||
if ifUnmodeSince != nil && !*ifUnmodeSince {
|
||||
return errPreconditionFailed
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// EvaluateMatchPreconditions evaluates if-match and if-none-match preconditions
|
||||
func EvaluateMatchPreconditions(etag string, ifMatch, ifNoneMatch *string) error {
|
||||
etag = strings.Trim(etag, `"`)
|
||||
if ifMatch != nil && *ifMatch != etag {
|
||||
return errPreconditionFailed
|
||||
}
|
||||
if ifNoneMatch != nil && *ifNoneMatch == etag {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfUnmodifiedSince)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -703,15 +694,15 @@ func EvaluateObjectPutPreconditions(etag string, ifMatch, ifNoneMatch *string, o
|
||||
}
|
||||
|
||||
if ifNoneMatch != nil && *ifNoneMatch != "*" {
|
||||
return s3err.GetAPIError(s3err.ErrNotImplemented)
|
||||
return s3err.GetNotImplementedErr("If-None-Match", s3err.NmpAdditionalMessageIfNoneMatch)
|
||||
}
|
||||
|
||||
if ifNoneMatch != nil && ifMatch != nil {
|
||||
return s3err.GetAPIError(s3err.ErrNotImplemented)
|
||||
return s3err.GetNotImplementedErr("If-Match,If-None-Match", s3err.NmpAdditionalMessageMultipleCondHeaders)
|
||||
}
|
||||
|
||||
if ifNoneMatch != nil && objExists {
|
||||
return s3err.GetAPIError(s3err.ErrPreconditionFailed)
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfNoneMatch)
|
||||
}
|
||||
|
||||
if ifMatch != nil && !objExists {
|
||||
@@ -721,7 +712,7 @@ func EvaluateObjectPutPreconditions(etag string, ifMatch, ifNoneMatch *string, o
|
||||
etag = strings.Trim(etag, `"`)
|
||||
|
||||
if ifMatch != nil && *ifMatch != etag {
|
||||
return s3err.GetAPIError(s3err.ErrPreconditionFailed)
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfMatch)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -738,17 +729,17 @@ func EvaluateObjectDeletePreconditions(etag string, modTime time.Time, size int6
|
||||
etag = strings.Trim(etag, `"`)
|
||||
ifMatch := preconditions.IfMatch
|
||||
if ifMatch != nil && *ifMatch != etag {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfMatch)
|
||||
}
|
||||
|
||||
ifMatchTime := preconditions.IfMatchLastModTime
|
||||
if ifMatchTime != nil && ifMatchTime.Unix() != modTime.Unix() {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfMatchLastModTime)
|
||||
}
|
||||
|
||||
ifMatchSize := preconditions.IfMatchSize
|
||||
if ifMatchSize != nil && *ifMatchSize != size {
|
||||
return errPreconditionFailed
|
||||
return s3err.GetPreconditionFailedErr(s3err.ConditionIfMatchSize)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -115,7 +115,7 @@ func TestParseCopySource(t *testing.T) {
|
||||
wantObject string
|
||||
wantVersionId string
|
||||
wantErr bool
|
||||
wantErrCode s3err.ErrorCode
|
||||
wantErrValue error
|
||||
}{
|
||||
{
|
||||
name: "simple path",
|
||||
@@ -212,7 +212,7 @@ func TestParseCopySource(t *testing.T) {
|
||||
wantObject: "",
|
||||
wantVersionId: "",
|
||||
wantErr: true,
|
||||
wantErrCode: s3err.ErrInvalidCopySourceEncoding,
|
||||
wantErrValue: s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceEncoding, "mybucket/object%"),
|
||||
},
|
||||
{
|
||||
name: "invalid URL encoding - invalid hex",
|
||||
@@ -221,7 +221,7 @@ func TestParseCopySource(t *testing.T) {
|
||||
wantObject: "",
|
||||
wantVersionId: "",
|
||||
wantErr: true,
|
||||
wantErrCode: s3err.ErrInvalidCopySourceEncoding,
|
||||
wantErrValue: s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceEncoding, "mybucket/object%ZZ"),
|
||||
},
|
||||
{
|
||||
name: "missing object",
|
||||
@@ -230,7 +230,7 @@ func TestParseCopySource(t *testing.T) {
|
||||
wantObject: "",
|
||||
wantVersionId: "",
|
||||
wantErr: true,
|
||||
wantErrCode: s3err.ErrInvalidCopySourceBucket,
|
||||
wantErrValue: s3err.GetInvalidArgumentErr(s3err.InvalidArgCopySourceBucket, "mybucket"),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -243,8 +243,8 @@ func TestParseCopySource(t *testing.T) {
|
||||
t.Errorf("ParseCopySource() error = nil, wantErr %v", tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !errors.Is(err, s3err.GetAPIError(tt.wantErrCode)) {
|
||||
t.Errorf("ParseCopySource() error = %v, want error code %v", err, tt.wantErrCode)
|
||||
if !errors.Is(err, tt.wantErrValue) {
|
||||
t.Errorf("ParseCopySource() error = %v, want error %v", err, tt.wantErrValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -80,11 +80,11 @@ func (l *MultipartUploadLister) Run() (*ListMultipartUploadsPage, error) {
|
||||
// any invalid uuid is considered as an invalid uploadIdMarker
|
||||
_, err := uuid.Parse(uploadIDMarker)
|
||||
if err != nil {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidUploadIdMarker)
|
||||
return nil, s3err.GetInvalidArgumentErr(s3err.InvalidArgUploadIdMarker, uploadIDMarker)
|
||||
}
|
||||
startIndex = l.findUploadIdMarkerIndex(uploadIDMarker)
|
||||
if startIndex == -1 {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidUploadIdMarker)
|
||||
return nil, s3err.GetInvalidArgumentErr(s3err.InvalidArgUploadIdMarker, uploadIDMarker)
|
||||
}
|
||||
if startIndex >= len(l.Uploads) {
|
||||
return out, nil
|
||||
|
||||
+189
-184
File diff suppressed because it is too large
Load Diff
@@ -179,7 +179,7 @@ func (s *S3Proxy) CreateBucket(ctx context.Context, input *s3.CreateBucketInput,
|
||||
input.GrantWriteACP = nil
|
||||
}
|
||||
if *input.Bucket == s.metaBucket {
|
||||
return s3err.GetAPIError(s3err.ErrBucketAlreadyExists)
|
||||
return s3err.GetBucketErr(s3err.ErrBucketAlreadyExists, *input.Bucket)
|
||||
}
|
||||
|
||||
acct, ok := ctx.Value("account").(auth.Account)
|
||||
@@ -196,9 +196,9 @@ func (s *S3Proxy) CreateBucket(ctx context.Context, input *s3.CreateBucketInput,
|
||||
}
|
||||
|
||||
if acl.Owner == acct.Access {
|
||||
return s3err.GetAPIError(s3err.ErrBucketAlreadyOwnedByYou)
|
||||
return s3err.GetBucketErr(s3err.ErrBucketAlreadyOwnedByYou, *input.Bucket)
|
||||
}
|
||||
return s3err.GetAPIError(s3err.ErrBucketAlreadyExists)
|
||||
return s3err.GetBucketErr(s3err.ErrBucketAlreadyExists, *input.Bucket)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1645,7 +1645,7 @@ func (s *S3Proxy) PutObjectLockConfiguration(ctx context.Context, bucket string,
|
||||
}
|
||||
|
||||
func (s *S3Proxy) GetObjectLockConfiguration(ctx context.Context, bucket string) ([]byte, error) {
|
||||
return nil, s3err.GetAPIError(s3err.ErrObjectLockConfigurationNotFound)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrObjectLockConfigurationNotFound, bucket)
|
||||
}
|
||||
|
||||
func (s *S3Proxy) PutObjectRetention(ctx context.Context, bucket, object, versionId string, retention []byte) error {
|
||||
@@ -1769,9 +1769,9 @@ func handleMetaBucketObjectNotFoundErr(prefix metaPrefix) ([]byte, error) {
|
||||
// If bucket acl is not found, return default acl
|
||||
return []byte{}, nil
|
||||
case metaPrefixPolicy:
|
||||
return nil, s3err.GetAPIError(s3err.ErrNoSuchBucketPolicy)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrNoSuchBucketPolicy, "")
|
||||
case metaPrefixCors:
|
||||
return nil, s3err.GetAPIError(s3err.ErrNoSuchCORSConfiguration)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrNoSuchCORSConfiguration, "")
|
||||
}
|
||||
|
||||
return []byte{}, nil
|
||||
|
||||
@@ -334,12 +334,12 @@ func (s *ScoutFS) GetObject(ctx context.Context, input *s3.GetObjectInput) (*s3.
|
||||
object := *input.Key
|
||||
|
||||
if !s.isBucketValid(bucket) {
|
||||
return nil, s3err.GetAPIError(s3err.ErrInvalidBucketName)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrInvalidBucketName, bucket)
|
||||
}
|
||||
|
||||
_, err := os.Stat(bucket)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, s3err.GetAPIError(s3err.ErrNoSuchBucket)
|
||||
return nil, s3err.GetBucketErr(s3err.ErrNoSuchBucket, *input.Bucket)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("stat bucket: %w", err)
|
||||
@@ -429,12 +429,12 @@ func (s *ScoutFS) RestoreObject(_ context.Context, input *s3.RestoreObjectInput)
|
||||
object := *input.Key
|
||||
|
||||
if !s.isBucketValid(bucket) {
|
||||
return s3err.GetAPIError(s3err.ErrInvalidBucketName)
|
||||
return s3err.GetBucketErr(s3err.ErrInvalidBucketName, bucket)
|
||||
}
|
||||
|
||||
_, err := os.Stat(bucket)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return s3err.GetAPIError(s3err.ErrNoSuchBucket)
|
||||
return s3err.GetBucketErr(s3err.ErrNoSuchBucket, *input.Bucket)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("stat bucket: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user