2020-09-26 07:54:03 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
2020-12-08 16:18:24 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"hash"
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-12-09 10:32:33 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-12-02 23:45:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
2020-09-26 07:54:03 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-12-08 16:18:24 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2020-12-02 23:45:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-12-09 10:32:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-02 23:45:25 +00:00
|
|
|
objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
|
2020-09-26 07:54:03 +00:00
|
|
|
getsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/get"
|
2020-12-08 16:26:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-12-08 16:18:24 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
|
|
|
"github.com/pkg/errors"
|
2020-09-26 07:54:03 +00:00
|
|
|
)
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStream) (*getsvc.Prm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
|
|
|
key, err := s.keyStorage.GetKey(token.NewSessionTokenFromV2(meta.GetSessionToken()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(getsvc.Prm)
|
|
|
|
p.SetPrivateKey(key)
|
|
|
|
|
|
|
|
body := req.GetBody()
|
2020-12-09 10:32:33 +00:00
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithRawFlag(body.GetRaw())
|
2020-12-02 23:45:25 +00:00
|
|
|
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
|
|
|
|
p.SetObjectWriter(&streamObjectWriter{stream})
|
2020-12-08 16:26:34 +00:00
|
|
|
p.SetCommonParameters(new(util.CommonPrm).
|
|
|
|
WithLocalOnly(meta.GetTTL() <= 1),
|
|
|
|
)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
return p, nil
|
2020-09-26 07:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.GetObjectRangeStream) (*getsvc.RangePrm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
|
|
|
key, err := s.keyStorage.GetKey(token.NewSessionTokenFromV2(meta.GetSessionToken()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(getsvc.RangePrm)
|
|
|
|
p.SetPrivateKey(key)
|
|
|
|
|
|
|
|
body := req.GetBody()
|
2020-12-09 10:32:33 +00:00
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithRawFlag(body.GetRaw())
|
|
|
|
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
|
|
|
|
p.SetChunkWriter(&streamObjectRangeWriter{stream})
|
2020-12-09 10:32:33 +00:00
|
|
|
p.SetRange(objectSDK.NewRangeFromV2(body.GetRange()))
|
2020-12-08 16:26:34 +00:00
|
|
|
p.SetCommonParameters(commonParameters(meta))
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
func (s *Service) toHashRangePrm(req *objectV2.GetRangeHashRequest) (*getsvc.RangeHashPrm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
|
|
|
key, err := s.keyStorage.GetKey(token.NewSessionTokenFromV2(meta.GetSessionToken()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(getsvc.RangeHashPrm)
|
|
|
|
p.SetPrivateKey(key)
|
|
|
|
|
|
|
|
body := req.GetBody()
|
2020-12-09 10:32:33 +00:00
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-08 16:18:24 +00:00
|
|
|
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
|
2020-12-08 16:26:34 +00:00
|
|
|
p.SetCommonParameters(commonParameters(meta))
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
rngsV2 := body.GetRanges()
|
2020-12-09 10:32:33 +00:00
|
|
|
rngs := make([]*objectSDK.Range, 0, len(rngsV2))
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
for i := range rngsV2 {
|
2020-12-09 10:32:33 +00:00
|
|
|
rngs = append(rngs, objectSDK.NewRangeFromV2(rngsV2[i]))
|
2020-12-08 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.SetRangeList(rngs)
|
|
|
|
|
|
|
|
switch t := body.GetType(); t {
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown checksum type %v", t)
|
|
|
|
case refs.SHA256:
|
|
|
|
p.SetHashGenerator(func() hash.Hash {
|
|
|
|
return sha256.New()
|
|
|
|
})
|
|
|
|
case refs.TillichZemor:
|
|
|
|
p.SetHashGenerator(func() hash.Hash {
|
|
|
|
return tz.New()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type headResponseWriter struct {
|
|
|
|
mainOnly bool
|
|
|
|
|
|
|
|
body *objectV2.HeadResponseBody
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *headResponseWriter) WriteHeader(hdr *object.Object) error {
|
|
|
|
if w.mainOnly {
|
|
|
|
w.body.SetHeaderPart(toShortObjectHeader(hdr))
|
|
|
|
} else {
|
|
|
|
w.body.SetHeaderPart(toFullObjectHeader(hdr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) toHeadPrm(req *objectV2.HeadRequest, resp *objectV2.HeadResponse) (*getsvc.HeadPrm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
|
|
|
key, err := s.keyStorage.GetKey(token.NewSessionTokenFromV2(meta.GetSessionToken()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p := new(getsvc.HeadPrm)
|
|
|
|
p.SetPrivateKey(key)
|
|
|
|
|
|
|
|
body := req.GetBody()
|
|
|
|
p.WithAddress(objectSDK.NewAddressFromV2(body.GetAddress()))
|
|
|
|
p.WithRawFlag(body.GetRaw())
|
|
|
|
p.SetRemoteCallOptions(remoteCallOptionsFromMeta(meta)...)
|
|
|
|
p.SetHeaderWriter(&headResponseWriter{
|
|
|
|
mainOnly: body.GetMainOnly(),
|
|
|
|
body: resp.GetBody(),
|
|
|
|
})
|
|
|
|
p.SetCommonParameters(commonParameters(meta))
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
// can be shared accross all services
|
|
|
|
func remoteCallOptionsFromMeta(meta *session.RequestMetaHeader) []client.CallOption {
|
|
|
|
xHdrs := meta.GetXHeaders()
|
|
|
|
|
|
|
|
opts := make([]client.CallOption, 0, 3+len(xHdrs))
|
|
|
|
|
|
|
|
opts = append(opts,
|
|
|
|
client.WithBearer(token.NewBearerTokenFromV2(meta.GetBearerToken())),
|
|
|
|
client.WithSession(token.NewSessionTokenFromV2(meta.GetSessionToken())),
|
|
|
|
client.WithTTL(meta.GetTTL()-1),
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := range xHdrs {
|
|
|
|
opts = append(opts, client.WithXHeader(pkg.NewXHeaderFromV2(xHdrs[i])))
|
2020-09-26 07:54:03 +00:00
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:26:34 +00:00
|
|
|
func commonParameters(meta *session.RequestMetaHeader) *util.CommonPrm {
|
|
|
|
return new(util.CommonPrm).
|
|
|
|
WithLocalOnly(meta.GetTTL() <= 1)
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func splitInfoResponse(info *objectSDK.SplitInfo) *objectV2.GetResponse {
|
2020-12-02 23:45:25 +00:00
|
|
|
resp := new(objectV2.GetResponse)
|
|
|
|
|
|
|
|
body := new(objectV2.GetResponseBody)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetObjectPart(info.ToV2())
|
|
|
|
|
|
|
|
return resp
|
2020-09-26 07:54:03 +00:00
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func splitInfoRangeResponse(info *objectSDK.SplitInfo) *objectV2.GetRangeResponse {
|
2020-12-07 17:49:47 +00:00
|
|
|
resp := new(objectV2.GetRangeResponse)
|
|
|
|
|
|
|
|
body := new(objectV2.GetRangeResponseBody)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetRangePart(info.ToV2())
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
2020-12-08 16:18:24 +00:00
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func setSplitInfoHeadResponse(info *objectSDK.SplitInfo, resp *objectV2.HeadResponse) {
|
|
|
|
resp.GetBody().SetHeaderPart(info.ToV2())
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
func toHashResponse(typ refs.ChecksumType, res *getsvc.RangeHashRes) *objectV2.GetRangeHashResponse {
|
|
|
|
resp := new(objectV2.GetRangeHashResponse)
|
|
|
|
|
|
|
|
body := new(objectV2.GetRangeHashResponseBody)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetType(typ)
|
|
|
|
body.SetHashList(res.Hashes())
|
|
|
|
|
|
|
|
return resp
|
|
|
|
}
|
2020-12-09 10:32:33 +00:00
|
|
|
|
|
|
|
func toFullObjectHeader(hdr *object.Object) objectV2.GetHeaderPart {
|
|
|
|
obj := hdr.ToV2()
|
|
|
|
|
|
|
|
hs := new(objectV2.HeaderWithSignature)
|
|
|
|
hs.SetHeader(obj.GetHeader())
|
|
|
|
hs.SetSignature(obj.GetSignature())
|
|
|
|
|
|
|
|
return hs
|
|
|
|
}
|
|
|
|
|
|
|
|
func toShortObjectHeader(hdr *object.Object) objectV2.GetHeaderPart {
|
|
|
|
hdrV2 := hdr.ToV2().GetHeader()
|
|
|
|
|
|
|
|
sh := new(objectV2.ShortHeader)
|
|
|
|
sh.SetOwnerID(hdrV2.GetOwnerID())
|
|
|
|
sh.SetCreationEpoch(hdrV2.GetCreationEpoch())
|
|
|
|
sh.SetPayloadLength(hdrV2.GetPayloadLength())
|
|
|
|
sh.SetVersion(hdrV2.GetVersion())
|
|
|
|
sh.SetObjectType(hdrV2.GetObjectType())
|
|
|
|
|
|
|
|
return sh
|
|
|
|
}
|