2021-08-05 09:18:52 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-04-18 15:35:25 +00:00
|
|
|
"context"
|
2023-06-21 14:16:40 +00:00
|
|
|
"errors"
|
2023-10-17 11:21:39 +00:00
|
|
|
"fmt"
|
2021-08-05 09:18:52 +00:00
|
|
|
"net/http"
|
2021-11-25 15:05:58 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-08-05 09:18:52 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-06-21 14:16:40 +00:00
|
|
|
s3errors "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2024-09-27 08:13:13 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/tree"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-06-21 14:16:40 +00:00
|
|
|
frosterrors "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/errors"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2024-03-01 14:07:08 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-08-29 09:27:50 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-08-05 09:18:52 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
func (h *handler) reqLogger(ctx context.Context) *zap.Logger {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqLogger := middleware.GetReqLog(ctx)
|
2023-06-09 13:19:23 +00:00
|
|
|
if reqLogger != nil {
|
|
|
|
return reqLogger
|
|
|
|
}
|
|
|
|
return h.log
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
func (h *handler) logAndSendError(w http.ResponseWriter, logText string, reqInfo *middleware.ReqInfo, err error, additional ...zap.Field) {
|
2023-10-17 11:21:39 +00:00
|
|
|
err = handleDeleteMarker(w, err)
|
2024-03-04 12:53:00 +00:00
|
|
|
if code, wrErr := middleware.WriteErrorResponse(w, reqInfo, transformToS3Error(err)); wrErr != nil {
|
|
|
|
additional = append(additional, zap.NamedError("write_response_error", wrErr))
|
|
|
|
} else {
|
|
|
|
additional = append(additional, zap.Int("status", code))
|
|
|
|
}
|
2022-10-25 15:14:46 +00:00
|
|
|
fields := []zap.Field{
|
|
|
|
zap.String("request_id", reqInfo.RequestID),
|
2022-07-27 15:02:52 +00:00
|
|
|
zap.String("method", reqInfo.API),
|
2022-10-25 15:14:46 +00:00
|
|
|
zap.String("bucket", reqInfo.BucketName),
|
|
|
|
zap.String("object", reqInfo.ObjectName),
|
|
|
|
zap.String("description", logText),
|
2024-05-27 14:41:32 +00:00
|
|
|
zap.String("user", reqInfo.User),
|
2021-08-05 09:18:52 +00:00
|
|
|
zap.Error(err)}
|
|
|
|
fields = append(fields, additional...)
|
2023-08-29 09:27:50 +00:00
|
|
|
if traceID, err := trace.TraceIDFromHex(reqInfo.TraceID); err == nil && traceID.IsValid() {
|
|
|
|
fields = append(fields, zap.String("trace_id", reqInfo.TraceID))
|
|
|
|
}
|
2023-08-23 11:07:52 +00:00
|
|
|
h.log.Error(logs.RequestFailed, fields...) // consider using h.reqLogger (it requires accept context.Context or http.Request)
|
2023-03-20 08:34:50 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 11:21:39 +00:00
|
|
|
func handleDeleteMarker(w http.ResponseWriter, err error) error {
|
|
|
|
var target layer.DeleteMarkerError
|
|
|
|
if !errors.As(err, &target) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set(api.AmzDeleteMarker, "true")
|
|
|
|
return fmt.Errorf("%w: %s", s3errors.GetAPIError(target.ErrorCode), err)
|
|
|
|
}
|
|
|
|
|
2022-09-13 10:35:30 +00:00
|
|
|
func transformToS3Error(err error) error {
|
2023-06-21 14:16:40 +00:00
|
|
|
err = frosterrors.UnwrapErr(err) // this wouldn't work with errors.Join
|
|
|
|
if _, ok := err.(s3errors.Error); ok {
|
2022-09-13 10:35:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
if errors.Is(err, frostfs.ErrAccessDenied) ||
|
2024-09-27 08:13:13 +00:00
|
|
|
errors.Is(err, tree.ErrNodeAccessDenied) {
|
2023-06-21 14:16:40 +00:00
|
|
|
return s3errors.GetAPIError(s3errors.ErrAccessDenied)
|
2022-09-13 10:35:30 +00:00
|
|
|
}
|
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
if errors.Is(err, frostfs.ErrGatewayTimeout) {
|
2023-06-21 14:16:40 +00:00
|
|
|
return s3errors.GetAPIError(s3errors.ErrGatewayTimeout)
|
2023-06-13 14:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 14:16:40 +00:00
|
|
|
return s3errors.GetAPIError(s3errors.ErrInternalError)
|
2021-08-05 09:18:52 +00:00
|
|
|
}
|
2021-08-23 08:19:41 +00:00
|
|
|
|
2022-12-23 12:28:30 +00:00
|
|
|
func (h *handler) ResolveBucket(ctx context.Context, bucket string) (*data.BucketInfo, error) {
|
|
|
|
return h.obj.GetBucketInfo(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:07:08 +00:00
|
|
|
func (h *handler) ResolveCID(ctx context.Context, bucket string) (cid.ID, error) {
|
|
|
|
return h.obj.ResolveCID(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
func (h *handler) getBucketAndCheckOwner(r *http.Request, bucket string, header ...string) (*data.BucketInfo, error) {
|
|
|
|
bktInfo, err := h.obj.GetBucketInfo(r.Context(), bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:19:41 +00:00
|
|
|
var expected string
|
|
|
|
if len(header) == 0 {
|
|
|
|
expected = r.Header.Get(api.AmzExpectedBucketOwner)
|
|
|
|
} else {
|
2022-03-18 13:04:09 +00:00
|
|
|
expected = r.Header.Get(header[0])
|
2021-08-23 08:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(expected) == 0 {
|
2022-03-18 13:04:09 +00:00
|
|
|
return bktInfo, nil
|
2021-08-23 08:19:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
return bktInfo, checkOwner(bktInfo, expected)
|
2021-08-23 08:19:41 +00:00
|
|
|
}
|
2021-11-25 15:05:58 +00:00
|
|
|
|
2024-09-05 07:47:05 +00:00
|
|
|
func (h *handler) getPutPayloadSize(r *http.Request) uint64 {
|
|
|
|
decodeContentSize := r.Header.Get(api.AmzDecodedContentLength)
|
|
|
|
decodedSize, err := strconv.Atoi(decodeContentSize)
|
|
|
|
if err != nil {
|
|
|
|
decodedSize = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var size uint64
|
|
|
|
if decodedSize > 0 {
|
|
|
|
size = uint64(decodedSize)
|
|
|
|
} else if r.ContentLength > 0 {
|
|
|
|
size = uint64(r.ContentLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:05:58 +00:00
|
|
|
func parseRange(s string) (*layer.RangeParams, error) {
|
|
|
|
if s == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := "bytes="
|
|
|
|
|
|
|
|
if !strings.HasPrefix(s, prefix) {
|
2023-06-21 14:16:40 +00:00
|
|
|
return nil, s3errors.GetAPIError(s3errors.ErrInvalidRange)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.TrimPrefix(s, prefix)
|
|
|
|
|
|
|
|
valuesStr := strings.Split(s, "-")
|
|
|
|
if len(valuesStr) != 2 {
|
2023-06-21 14:16:40 +00:00
|
|
|
return nil, s3errors.GetAPIError(s3errors.ErrInvalidRange)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
values := make([]uint64, 0, len(valuesStr))
|
|
|
|
for _, v := range valuesStr {
|
|
|
|
num, err := strconv.ParseUint(v, 10, 64)
|
|
|
|
if err != nil {
|
2023-06-21 14:16:40 +00:00
|
|
|
return nil, s3errors.GetAPIError(s3errors.ErrInvalidRange)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
values = append(values, num)
|
|
|
|
}
|
|
|
|
if values[0] > values[1] {
|
2023-06-21 14:16:40 +00:00
|
|
|
return nil, s3errors.GetAPIError(s3errors.ErrInvalidRange)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &layer.RangeParams{
|
|
|
|
Start: values[0],
|
|
|
|
End: values[1],
|
|
|
|
}, nil
|
|
|
|
}
|