2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-10-04 11:50:37 +00:00
|
|
|
"fmt"
|
2023-08-31 08:37:03 +00:00
|
|
|
"io"
|
|
|
|
"net/url"
|
2023-10-04 11:50:37 +00:00
|
|
|
"strings"
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/cache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/data"
|
2023-11-28 08:29:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/handler/middleware"
|
2023-08-31 08:37:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
2023-10-04 11:50:37 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
2023-08-31 08:37:03 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-09-05 15:17:22 +00:00
|
|
|
type Config interface {
|
|
|
|
DefaultTimestamp() bool
|
|
|
|
ZipCompression() bool
|
2023-08-21 13:50:23 +00:00
|
|
|
ClientCut() bool
|
2023-08-25 11:53:59 +00:00
|
|
|
BufferMaxSizeForPut() uint64
|
2023-11-28 08:29:08 +00:00
|
|
|
NamespaceHeader() string
|
2023-09-05 15:17:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
type Handler struct {
|
|
|
|
log *zap.Logger
|
|
|
|
pool *pool.Pool
|
|
|
|
ownerID *user.ID
|
2023-09-05 15:17:22 +00:00
|
|
|
config Config
|
2023-08-31 08:37:03 +00:00
|
|
|
containerResolver *resolver.ContainerResolver
|
|
|
|
tree *tree.Tree
|
2023-10-04 11:50:37 +00:00
|
|
|
cache *cache.BucketCache
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 15:17:22 +00:00
|
|
|
func New(params *utils.AppParams, config Config, tree *tree.Tree) *Handler {
|
2023-08-31 08:37:03 +00:00
|
|
|
return &Handler{
|
|
|
|
log: params.Logger,
|
|
|
|
pool: params.Pool,
|
|
|
|
ownerID: params.Owner,
|
2023-09-05 15:17:22 +00:00
|
|
|
config: config,
|
2023-08-31 08:37:03 +00:00
|
|
|
containerResolver: params.Resolver,
|
|
|
|
tree: tree,
|
2023-10-04 11:50:37 +00:00
|
|
|
cache: params.Cache,
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// byAddress is a wrapper for function (e.g. request.headObject, request.receiveFile) that
|
|
|
|
// prepares request and object address to it.
|
|
|
|
func (h *Handler) byAddress(c *fasthttp.RequestCtx, f func(context.Context, request, oid.Address)) {
|
|
|
|
var (
|
|
|
|
idCnr, _ = c.UserValue("cid").(string)
|
|
|
|
idObj, _ = c.UserValue("oid").(string)
|
|
|
|
log = h.log.With(zap.String("cid", idCnr), zap.String("oid", idObj))
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx := utils.GetContextFromRequest(c)
|
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
bktInfo, err := h.getBucketInfo(ctx, idCnr, log)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
2023-10-04 11:50:37 +00:00
|
|
|
logAndSendBucketError(c, log, err)
|
2023-08-31 08:37:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
objID := new(oid.ID)
|
|
|
|
if err = objID.DecodeString(idObj); err != nil {
|
|
|
|
log.Error(logs.WrongObjectID, zap.Error(err))
|
|
|
|
response.Error(c, "wrong object id", fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr oid.Address
|
2023-10-04 11:50:37 +00:00
|
|
|
addr.SetContainer(bktInfo.CID)
|
2023-08-31 08:37:03 +00:00
|
|
|
addr.SetObject(*objID)
|
|
|
|
|
|
|
|
f(ctx, *h.newRequest(c, log), addr)
|
|
|
|
}
|
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
// byObjectName is a wrapper for function (e.g. request.headObject, request.receiveFile) that
|
2023-08-31 08:37:03 +00:00
|
|
|
// prepares request and object address to it.
|
2023-10-04 11:50:37 +00:00
|
|
|
func (h *Handler) byObjectName(req *fasthttp.RequestCtx, f func(context.Context, request, oid.Address)) {
|
2023-08-31 08:37:03 +00:00
|
|
|
var (
|
|
|
|
bucketname = req.UserValue("cid").(string)
|
|
|
|
key = req.UserValue("oid").(string)
|
|
|
|
log = h.log.With(zap.String("bucketname", bucketname), zap.String("key", key))
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx := utils.GetContextFromRequest(req)
|
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
bktInfo, err := h.getBucketInfo(ctx, bucketname, log)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
2023-10-04 11:50:37 +00:00
|
|
|
logAndSendBucketError(req, log, err)
|
2023-08-31 08:37:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
foundOid, err := h.tree.GetLatestVersion(ctx, &bktInfo.CID, key)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
2023-10-04 12:39:44 +00:00
|
|
|
if errors.Is(err, tree.ErrNodeAccessDenied) {
|
|
|
|
response.Error(req, "Access Denied", fasthttp.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Error(logs.GetLatestObjectVersion, zap.Error(err))
|
|
|
|
|
2023-08-31 08:37:03 +00:00
|
|
|
response.Error(req, "object wasn't found", fasthttp.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if foundOid.DeleteMarker {
|
|
|
|
log.Error(logs.ObjectWasDeleted)
|
|
|
|
response.Error(req, "object deleted", fasthttp.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var addr oid.Address
|
2023-10-04 11:50:37 +00:00
|
|
|
addr.SetContainer(bktInfo.CID)
|
2023-08-31 08:37:03 +00:00
|
|
|
addr.SetObject(foundOid.OID)
|
|
|
|
|
|
|
|
f(ctx, *h.newRequest(req, log), addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// byAttribute is a wrapper similar to byAddress.
|
|
|
|
func (h *Handler) byAttribute(c *fasthttp.RequestCtx, f func(context.Context, request, oid.Address)) {
|
2024-02-29 09:50:56 +00:00
|
|
|
scid, _ := c.UserValue("cid").(string)
|
|
|
|
key, _ := c.UserValue("attr_key").(string)
|
|
|
|
val, _ := c.UserValue("attr_val").(string)
|
|
|
|
|
|
|
|
key, err := url.QueryUnescape(key)
|
|
|
|
if err != nil {
|
|
|
|
h.log.Error(logs.FailedToUnescapeQuery, zap.String("cid", scid), zap.String("attr_key", key), zap.Uint64("id", c.ID()), zap.Error(err))
|
|
|
|
response.Error(c, "could not unescape attr_key: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err = url.QueryUnescape(val)
|
|
|
|
if err != nil {
|
|
|
|
h.log.Error(logs.FailedToUnescapeQuery, zap.String("cid", scid), zap.String("attr_val", val), zap.Uint64("id", c.ID()), zap.Error(err))
|
|
|
|
response.Error(c, "could not unescape attr_val: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log := h.log.With(zap.String("cid", scid), zap.String("attr_key", key), zap.String("attr_val", val))
|
2023-08-31 08:37:03 +00:00
|
|
|
|
|
|
|
ctx := utils.GetContextFromRequest(c)
|
|
|
|
|
2023-10-04 12:39:44 +00:00
|
|
|
bktInfo, err := h.getBucketInfo(ctx, scid, log)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
2023-10-04 12:39:44 +00:00
|
|
|
logAndSendBucketError(c, log, err)
|
2023-08-31 08:37:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:39:44 +00:00
|
|
|
res, err := h.search(ctx, &bktInfo.CID, key, val, object.MatchStringEqual)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(logs.CouldNotSearchForObjects, zap.Error(err))
|
|
|
|
response.Error(c, "could not search for objects: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Close()
|
|
|
|
|
|
|
|
buf := make([]oid.ID, 1)
|
|
|
|
|
|
|
|
n, err := res.Read(buf)
|
|
|
|
if n == 0 {
|
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
log.Error(logs.ObjectNotFound, zap.Error(err))
|
|
|
|
response.Error(c, "object not found", fasthttp.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error(logs.ReadObjectListFailed, zap.Error(err))
|
|
|
|
response.Error(c, "read object list failed: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var addrObj oid.Address
|
2023-10-04 12:39:44 +00:00
|
|
|
addrObj.SetContainer(bktInfo.CID)
|
2023-08-31 08:37:03 +00:00
|
|
|
addrObj.SetObject(buf[0])
|
|
|
|
|
|
|
|
f(ctx, *h.newRequest(c, log), addrObj)
|
|
|
|
}
|
2023-10-04 11:50:37 +00:00
|
|
|
|
|
|
|
// resolveContainer decode container id, if it's not a valid container id
|
|
|
|
// then trey to resolve name using provided resolver.
|
|
|
|
func (h *Handler) resolveContainer(ctx context.Context, containerID string) (*cid.ID, error) {
|
|
|
|
cnrID := new(cid.ID)
|
|
|
|
err := cnrID.DecodeString(containerID)
|
|
|
|
if err != nil {
|
|
|
|
cnrID, err = h.containerResolver.Resolve(ctx, containerID)
|
|
|
|
if err != nil && strings.Contains(err.Error(), "not found") {
|
2023-10-04 12:39:44 +00:00
|
|
|
err = fmt.Errorf("%w: %s", new(apistatus.ContainerNotFound), err.Error())
|
2023-10-04 11:50:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cnrID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) getBucketInfo(ctx context.Context, containerName string, log *zap.Logger) (*data.BucketInfo, error) {
|
2023-11-28 08:29:08 +00:00
|
|
|
ns, err := middleware.GetNamespace(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bktInfo := h.cache.Get(ns, containerName); bktInfo != nil {
|
2023-10-04 11:50:37 +00:00
|
|
|
return bktInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cnrID, err := h.resolveContainer(ctx, containerName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bktInfo, err := h.readContainer(ctx, *cnrID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.cache.Put(bktInfo); err != nil {
|
|
|
|
log.Warn(logs.CouldntPutBucketIntoCache,
|
|
|
|
zap.String("bucket name", bktInfo.Name),
|
|
|
|
zap.Stringer("bucket cid", bktInfo.CID),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bktInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) readContainer(ctx context.Context, cnrID cid.ID) (*data.BucketInfo, error) {
|
|
|
|
prm := pool.PrmContainerGet{ContainerID: cnrID}
|
|
|
|
res, err := h.pool.GetContainer(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get frostfs container '%s': %w", cnrID.String(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bktInfo := &data.BucketInfo{
|
|
|
|
CID: cnrID,
|
|
|
|
Name: cnrID.EncodeToString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if domain := container.ReadDomain(res); domain.Name() != "" {
|
|
|
|
bktInfo.Name = domain.Name()
|
|
|
|
bktInfo.Zone = domain.Zone()
|
|
|
|
}
|
|
|
|
|
|
|
|
bktInfo.HomomorphicHashDisabled = container.IsHomomorphicHashingDisabled(res)
|
|
|
|
|
|
|
|
return bktInfo, err
|
|
|
|
}
|