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"
|
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"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
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)
|
2023-07-05 14:05:45 +00:00
|
|
|
code := middleware.WriteErrorResponse(w, reqInfo, transformToS3Error(err))
|
2022-10-25 15:14:46 +00:00
|
|
|
fields := []zap.Field{
|
|
|
|
zap.Int("status", code),
|
|
|
|
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),
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
func (h *handler) logAndSendErrorNoHeader(w http.ResponseWriter, logText string, reqInfo *middleware.ReqInfo, err error, additional ...zap.Field) {
|
|
|
|
middleware.WriteErrorResponseNoHeader(w, reqInfo, transformToS3Error(err))
|
2023-03-20 08:34:50 +00:00
|
|
|
fields := []zap.Field{
|
|
|
|
zap.String("request_id", reqInfo.RequestID),
|
|
|
|
zap.String("method", reqInfo.API),
|
|
|
|
zap.String("bucket", reqInfo.BucketName),
|
|
|
|
zap.String("object", reqInfo.ObjectName),
|
|
|
|
zap.String("description", logText),
|
|
|
|
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)
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-21 14:16:40 +00:00
|
|
|
if errors.Is(err, layer.ErrAccessDenied) ||
|
|
|
|
errors.Is(err, layer.ErrNodeAccessDenied) {
|
|
|
|
return s3errors.GetAPIError(s3errors.ErrAccessDenied)
|
2022-09-13 10:35:30 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 14:16:40 +00:00
|
|
|
if errors.Is(err, layer.ErrGatewayTimeout) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-04-18 15:35:25 +00:00
|
|
|
|
2022-05-04 12:29:11 +00:00
|
|
|
func getSessionTokenSetEACL(ctx context.Context) (*session.Container, error) {
|
2023-08-14 15:34:41 +00:00
|
|
|
boxData, err := middleware.GetBoxData(ctx)
|
2022-04-18 15:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sessionToken := boxData.Gate.SessionTokenForSetEACL()
|
|
|
|
if sessionToken == nil {
|
2023-06-21 14:16:40 +00:00
|
|
|
return nil, s3errors.GetAPIError(s3errors.ErrAccessDenied)
|
2022-04-18 15:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sessionToken, nil
|
|
|
|
}
|