From 6425fec36615e66f9898dd759183bdd5e7e79e40 Mon Sep 17 00:00:00 2001 From: Anis Eleuch Date: Fri, 26 May 2023 18:13:18 +0100 Subject: [PATCH] s3: Add x-minio-error-code header for S3 HEAD requests (#17283) --- cmd/bucket-handlers.go | 8 +++++++- cmd/object-handlers.go | 12 +++++++++--- cmd/s3-zip-handlers.go | 19 +++++++++++-------- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/cmd/bucket-handlers.go b/cmd/bucket-handlers.go index 59772d131..de24939a1 100644 --- a/cmd/bucket-handlers.go +++ b/cmd/bucket-handlers.go @@ -67,6 +67,9 @@ const ( objectLockConfig = "object-lock.xml" bucketTaggingConfig = "tagging.xml" bucketReplicationConfig = "replication.xml" + + xMinIOErrCodeHeader = "x-minio-error-code" + xMinIOErrDescHeader = "x-minio-error-desc" ) // Check if there are buckets on server without corresponding entry in etcd backend and @@ -1504,7 +1507,10 @@ func (api objectAPIHandlers) HeadBucketHandler(w http.ResponseWriter, r *http.Re } if s3Error := checkRequestAuthType(ctx, r, policy.ListBucketAction, bucket, ""); s3Error != ErrNone { - writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(s3Error)) + errCode := errorCodes.ToAPIErr(s3Error) + w.Header().Set(xMinIOErrCodeHeader, errCode.Code) + w.Header().Set(xMinIOErrDescHeader, "\""+errCode.Description+"\"") + writeErrorResponseHeadersOnly(w, errCode) return } diff --git a/cmd/object-handlers.go b/cmd/object-handlers.go index fb1fc51b4..16299b88b 100644 --- a/cmd/object-handlers.go +++ b/cmd/object-handlers.go @@ -657,7 +657,10 @@ func (api objectAPIHandlers) headObjectHandler(ctx context.Context, objectAPI Ob } } } - writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(s3Error)) + errCode := errorCodes.ToAPIErr(s3Error) + w.Header().Set(xMinIOErrCodeHeader, errCode.Code) + w.Header().Set(xMinIOErrDescHeader, "\""+errCode.Description+"\"") + writeErrorResponseHeadersOnly(w, errCode) return } @@ -680,7 +683,7 @@ func (api objectAPIHandlers) headObjectHandler(ctx context.Context, objectAPI Ob objInfo = oi } if proxy.Err != nil { - writeErrorResponse(ctx, w, toAPIError(ctx, proxy.Err), r.URL) + writeErrorResponseHeadersOnly(w, toAPIError(ctx, proxy.Err)) return } } @@ -692,7 +695,10 @@ func (api objectAPIHandlers) headObjectHandler(ctx context.Context, objectAPI Ob } if s3Error := authorizeRequest(ctx, r, policy.GetObjectAction); s3Error != ErrNone { - writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL) + errCode := errorCodes.ToAPIErr(s3Error) + w.Header().Set(xMinIOErrCodeHeader, errCode.Code) + w.Header().Set(xMinIOErrDescHeader, "\""+errCode.Description+"\"") + writeErrorResponseHeadersOnly(w, errCode) return } diff --git a/cmd/s3-zip-handlers.go b/cmd/s3-zip-handlers.go index 63c0d1c2c..ebb8501be 100644 --- a/cmd/s3-zip-handlers.go +++ b/cmd/s3-zip-handlers.go @@ -421,7 +421,10 @@ func (api objectAPIHandlers) headObjectInArchiveFileHandler(ctx context.Context, } } } - writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(s3Error)) + errCode := errorCodes.ToAPIErr(s3Error) + w.Header().Set(xMinIOErrCodeHeader, errCode.Code) + w.Header().Set(xMinIOErrDescHeader, "\""+errCode.Description+"\"") + writeErrorResponseHeadersOnly(w, errCode) return } @@ -432,18 +435,18 @@ func (api objectAPIHandlers) headObjectInArchiveFileHandler(ctx context.Context, // We do not allow offsetting into extracted files. if opts.PartNumber != 0 { - writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrInvalidPartNumber), r.URL) + writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(ErrInvalidPartNumber)) return } if r.Header.Get(xhttp.Range) != "" { - writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrInvalidRange), r.URL) + writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(ErrInvalidRange)) return } zipObjInfo, err := getObjectInfo(ctx, bucket, zipPath, opts) if err != nil { - writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL) + writeErrorResponseHeadersOnly(w, toAPIError(ctx, err)) return } @@ -451,22 +454,22 @@ func (api objectAPIHandlers) headObjectInArchiveFileHandler(ctx context.Context, if len(zipInfo) == 0 { opts.EncryptFn, err = zipObjInfo.metadataEncryptFn(r.Header) if err != nil { - writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL) + writeErrorResponseHeadersOnly(w, toAPIError(ctx, err)) return } zipInfo, err = updateObjectMetadataWithZipInfo(ctx, objectAPI, bucket, zipPath, opts) } if err != nil { - writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL) + writeErrorResponseHeadersOnly(w, toAPIError(ctx, err)) return } file, err := zipindex.FindSerialized(zipInfo, object) if err != nil { if err == io.EOF { - writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrNoSuchKey), r.URL) + writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(ErrNoSuchKey)) } else { - writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL) + writeErrorResponseHeadersOnly(w, toAPIError(ctx, err)) } return }