2021-08-05 09:18:52 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-04-18 15:35:25 +00:00
|
|
|
"context"
|
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
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
2022-04-18 15:35:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2021-11-25 15:05:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/errors"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2022-06-02 12:09:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/internal/misc"
|
2022-04-18 15:35:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2021-08-05 09:18:52 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *handler) logAndSendError(w http.ResponseWriter, logText string, reqInfo *api.ReqInfo, err error, additional ...zap.Field) {
|
2022-06-02 12:09:00 +00:00
|
|
|
fields := []zap.Field{zap.String("request_id", misc.SanitizeString(reqInfo.RequestID)),
|
|
|
|
zap.String("method", misc.SanitizeString(reqInfo.API)),
|
|
|
|
zap.String("bucket_name", misc.SanitizeString(reqInfo.BucketName)),
|
|
|
|
zap.String("object_name", misc.SanitizeString(reqInfo.ObjectName)),
|
2021-08-05 09:18:52 +00:00
|
|
|
zap.Error(err)}
|
|
|
|
fields = append(fields, additional...)
|
|
|
|
|
|
|
|
h.log.Error(logText, fields...)
|
|
|
|
api.WriteErrorResponse(w, reqInfo, err)
|
|
|
|
}
|
2021-08-23 08:19:41 +00:00
|
|
|
|
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) {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.TrimPrefix(s, prefix)
|
|
|
|
|
|
|
|
valuesStr := strings.Split(s, "-")
|
|
|
|
if len(valuesStr) != 2 {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
values := make([]uint64, 0, len(valuesStr))
|
|
|
|
for _, v := range valuesStr {
|
|
|
|
num, err := strconv.ParseUint(v, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidRange)
|
|
|
|
}
|
|
|
|
values = append(values, num)
|
|
|
|
}
|
|
|
|
if values[0] > values[1] {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-04-18 15:35:25 +00:00
|
|
|
boxData, err := layer.GetBoxData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sessionToken := boxData.Gate.SessionTokenForSetEACL()
|
|
|
|
if sessionToken == nil {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrAccessDenied)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sessionToken, nil
|
|
|
|
}
|