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"
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr "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"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
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"
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
func (h *handler) logAndSendError(ctx context.Context, 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-09-27 09:26:08 +00:00
|
|
|
if code, wrErr := middleware.WriteErrorResponse(w, reqInfo, apierr.TransformToS3Error(err)); wrErr != nil {
|
2024-03-04 12:53:00 +00:00
|
|
|
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{
|
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...)
|
2024-10-25 01:39:59 +00:00
|
|
|
h.reqLogger(ctx).Error(logs.RequestFailed, fields...)
|
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")
|
2024-09-27 09:18:41 +00:00
|
|
|
return fmt.Errorf("%w: %s", apierr.GetAPIError(target.ErrorCode), err)
|
2023-10-17 11:21:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 12:28:30 +00:00
|
|
|
func (h *handler) ResolveBucket(ctx context.Context, bucket string) (*data.BucketInfo, error) {
|
2024-11-19 11:48:59 +00:00
|
|
|
return h.getBucketInfo(ctx, bucket)
|
2022-12-23 12:28:30 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-11-19 11:48:59 +00:00
|
|
|
func (h *handler) getBucketInfo(ctx context.Context, bucket string) (*data.BucketInfo, error) {
|
|
|
|
if err := checkBucketName(bucket); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return h.obj.GetBucketInfo(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
func (h *handler) getBucketAndCheckOwner(r *http.Request, bucket string, header ...string) (*data.BucketInfo, error) {
|
2024-11-19 11:48:59 +00:00
|
|
|
bktInfo, err := h.getBucketInfo(r.Context(), bucket)
|
2022-03-18 13:04:09 +00:00
|
|
|
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 {
|
2024-10-03 11:23:06 +00:00
|
|
|
if r.ContentLength >= 0 {
|
|
|
|
return uint64(r.ContentLength)
|
|
|
|
}
|
|
|
|
return 0
|
2024-09-05 07:47:05 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 11:23:06 +00:00
|
|
|
return uint64(decodedSize)
|
2024-09-05 07:47:05 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrInvalidRange)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.TrimPrefix(s, prefix)
|
|
|
|
|
|
|
|
valuesStr := strings.Split(s, "-")
|
|
|
|
if len(valuesStr) != 2 {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.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 {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrInvalidRange)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
values = append(values, num)
|
|
|
|
}
|
|
|
|
if values[0] > values[1] {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrInvalidRange)
|
2021-11-25 15:05:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &layer.RangeParams{
|
|
|
|
Start: values[0],
|
|
|
|
End: values[1],
|
|
|
|
}, nil
|
|
|
|
}
|