2020-09-26 07:54:03 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
2021-04-29 12:18:29 +00:00
|
|
|
"context"
|
2020-12-08 16:18:24 +00:00
|
|
|
"crypto/sha256"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-12-08 16:18:24 +00:00
|
|
|
"hash"
|
2021-04-29 12:18:29 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
2020-12-08 16:18:24 +00:00
|
|
|
|
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"
|
2021-04-29 12:18:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2021-11-06 11:13:04 +00:00
|
|
|
rpcclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
2021-04-29 12:18:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/signature"
|
2021-06-21 14:13:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
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"
|
2021-09-28 06:02:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/internal"
|
2020-12-08 16:26:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2021-11-10 07:08:33 +00:00
|
|
|
signature2 "github.com/nspcc-dev/neofs-sdk-go/util/signature"
|
2020-12-08 16:18:24 +00:00
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
2020-09-26 07:54:03 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
var errWrongMessageSeq = errors.New("incorrect message sequence")
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStream) (*getsvc.Prm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
p := new(getsvc.Prm)
|
2021-10-26 13:02:46 +00:00
|
|
|
p.SetCommonParameters(commonPrm)
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
2022-01-26 12:11:13 +00:00
|
|
|
p.WithAddress(addressSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithRawFlag(body.GetRaw())
|
2020-12-02 23:45:25 +00:00
|
|
|
p.SetObjectWriter(&streamObjectWriter{stream})
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.MultiAddressClient, pubkey []byte) (*object.Object, error) {
|
2021-04-29 12:18:29 +00:00
|
|
|
var err error
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1165 think how to set the other fields
|
2021-04-29 12:18:29 +00:00
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.GetObject implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
|
|
|
|
// open stream
|
2022-03-11 15:24:11 +00:00
|
|
|
var getStream *rpc.GetResponseReader
|
|
|
|
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
|
|
|
|
getStream, err = rpc.GetObject(cli, req, rpcclient.WithContext(stream.Context()))
|
|
|
|
return err
|
|
|
|
})
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("stream opening failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
headWas bool
|
|
|
|
payload []byte
|
|
|
|
obj = new(objectV2.Object)
|
|
|
|
resp = new(objectV2.GetResponse)
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
2022-03-11 15:24:11 +00:00
|
|
|
err := getStream.Read(resp)
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-04-29 12:18:29 +00:00
|
|
|
if !headWas {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
// verify response key
|
|
|
|
if err = internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetObjectPart().(type) {
|
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unexpected object part %T", v)
|
2021-04-29 12:18:29 +00:00
|
|
|
case *objectV2.GetObjectPartInit:
|
|
|
|
if headWas {
|
|
|
|
return nil, errWrongMessageSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
headWas = true
|
|
|
|
|
|
|
|
obj.SetObjectID(v.GetObjectID())
|
|
|
|
obj.SetSignature(v.GetSignature())
|
|
|
|
|
|
|
|
hdr := v.GetHeader()
|
|
|
|
obj.SetHeader(hdr)
|
|
|
|
|
|
|
|
payload = make([]byte, 0, hdr.GetPayloadLength())
|
|
|
|
case *objectV2.GetObjectPartChunk:
|
|
|
|
if !headWas {
|
|
|
|
return nil, errWrongMessageSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
payload = append(payload, v.GetChunk()...)
|
|
|
|
case *objectV2.SplitInfo:
|
2022-03-03 14:19:05 +00:00
|
|
|
si := object.NewSplitInfoFromV2(v)
|
|
|
|
return nil, object.NewSplitInfoError(si)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.SetPayload(payload)
|
|
|
|
|
|
|
|
// convert the object
|
2022-03-03 14:19:05 +00:00
|
|
|
return object.NewFromV2(obj), nil
|
2021-06-22 12:08:17 +00:00
|
|
|
}))
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
p := new(getsvc.RangePrm)
|
2021-10-26 13:02:46 +00:00
|
|
|
p.SetCommonParameters(commonPrm)
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
2022-01-26 12:11:13 +00:00
|
|
|
p.WithAddress(addressSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-07 17:49:47 +00:00
|
|
|
p.WithRawFlag(body.GetRaw())
|
|
|
|
p.SetChunkWriter(&streamObjectRangeWriter{stream})
|
2022-03-03 14:19:05 +00:00
|
|
|
p.SetRange(object.NewRangeFromV2(body.GetRange()))
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.MultiAddressClient, pubkey []byte) (*object.Object, error) {
|
2021-04-29 12:18:29 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1165 think how to set the other fields
|
2021-04-29 12:18:29 +00:00
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.ObjectPayloadRangeData implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
|
|
|
|
// open stream
|
2022-03-11 15:24:11 +00:00
|
|
|
var rangeStream *rpc.ObjectRangeResponseReader
|
|
|
|
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
|
|
|
|
rangeStream, err = rpc.GetObjectRange(cli, req, rpcclient.WithContext(stream.Context()))
|
|
|
|
return err
|
|
|
|
})
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not create Get payload range stream: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 07:41:26 +00:00
|
|
|
payload := make([]byte, 0, body.GetRange().GetLength())
|
2021-04-29 12:18:29 +00:00
|
|
|
|
|
|
|
resp := new(objectV2.GetRangeResponse)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
2022-03-11 15:24:11 +00:00
|
|
|
err := rangeStream.Read(resp)
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-04-29 12:18:29 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
// verify response key
|
|
|
|
if err = internal.VerifyResponseKeyV2(pubkey, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not verify %T: %w", resp, err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetRangePart().(type) {
|
|
|
|
case nil:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unexpected range type %T", v)
|
2021-04-29 12:18:29 +00:00
|
|
|
case *objectV2.GetRangePartChunk:
|
|
|
|
payload = append(payload, v.GetChunk()...)
|
|
|
|
case *objectV2.SplitInfo:
|
2022-03-03 14:19:05 +00:00
|
|
|
si := object.NewSplitInfoFromV2(v)
|
2021-04-29 12:18:29 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return nil, object.NewSplitInfoError(si)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.New()
|
2021-04-29 12:18:29 +00:00
|
|
|
obj.SetPayload(payload)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return obj, nil
|
2021-06-22 12:08:17 +00:00
|
|
|
}))
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
p := new(getsvc.RangeHashPrm)
|
2021-10-26 13:02:46 +00:00
|
|
|
p.SetCommonParameters(commonPrm)
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
2022-01-26 12:11:13 +00:00
|
|
|
p.WithAddress(addressSDK.NewAddressFromV2(body.GetAddress()))
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
rngsV2 := body.GetRanges()
|
2022-03-15 12:11:35 +00:00
|
|
|
rngs := make([]object.Range, len(rngsV2))
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
for i := range rngsV2 {
|
2022-03-15 12:11:35 +00:00
|
|
|
rngs[i] = *object.NewRangeFromV2(&rngsV2[i])
|
2020-12-08 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.SetRangeList(rngs)
|
2021-01-11 13:50:04 +00:00
|
|
|
p.SetSalt(body.GetSalt())
|
2020-12-08 16:18:24 +00:00
|
|
|
|
|
|
|
switch t := body.GetType(); t {
|
|
|
|
default:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unknown checksum type %v", t)
|
2020-12-08 16:18:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
func (s *Service) toHeadPrm(ctx context.Context, req *objectV2.HeadRequest, resp *objectV2.HeadResponse) (*getsvc.HeadPrm, error) {
|
2020-12-09 10:32:33 +00:00
|
|
|
meta := req.GetMetaHeader()
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
p := new(getsvc.HeadPrm)
|
2021-10-26 13:02:46 +00:00
|
|
|
p.SetCommonParameters(commonPrm)
|
2020-12-09 10:32:33 +00:00
|
|
|
|
|
|
|
body := req.GetBody()
|
2021-11-01 08:35:33 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
objAddr := addressSDK.NewAddressFromV2(body.GetAddress())
|
2021-11-01 08:35:33 +00:00
|
|
|
|
|
|
|
p.WithAddress(objAddr)
|
2020-12-09 10:32:33 +00:00
|
|
|
p.WithRawFlag(body.GetRaw())
|
|
|
|
p.SetHeaderWriter(&headResponseWriter{
|
|
|
|
mainOnly: body.GetMainOnly(),
|
|
|
|
body: resp.GetBody(),
|
|
|
|
})
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.MultiAddressClient, pubkey []byte) (*object.Object, error) {
|
2021-04-29 12:18:29 +00:00
|
|
|
var err error
|
|
|
|
|
2021-10-26 12:07:28 +00:00
|
|
|
key, err := s.keyStorage.GetKey(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
2022-02-07 13:34:02 +00:00
|
|
|
// TODO: #1165 think how to set the other fields
|
2021-04-29 12:18:29 +00:00
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.GetObjectHeader implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
|
|
|
|
// send Head request
|
2022-03-11 15:24:11 +00:00
|
|
|
var headResp *objectV2.HeadResponse
|
|
|
|
err = c.RawForAddress(addr, func(cli *rpcclient.Client) error {
|
|
|
|
headResp, err = rpc.HeadObject(cli, req, rpcclient.WithContext(ctx))
|
|
|
|
return err
|
|
|
|
})
|
2021-04-29 12:18:29 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("sending the request failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
// verify response key
|
2022-03-11 15:24:11 +00:00
|
|
|
if err = internal.VerifyResponseKeyV2(pubkey, headResp); err != nil {
|
2021-09-28 06:02:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
// verify response structure
|
2022-03-11 15:24:11 +00:00
|
|
|
if err := signature.VerifyServiceMessage(headResp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hdr *objectV2.Header
|
|
|
|
idSig *refs.Signature
|
|
|
|
)
|
|
|
|
|
2022-03-11 15:24:11 +00:00
|
|
|
switch v := headResp.GetBody().GetHeaderPart().(type) {
|
2021-04-29 12:18:29 +00:00
|
|
|
case nil:
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unexpected header type %T", v)
|
2021-04-29 12:18:29 +00:00
|
|
|
case *objectV2.ShortHeader:
|
|
|
|
if !body.GetMainOnly() {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("wrong header part type: expected %T, received %T",
|
2021-04-29 12:18:29 +00:00
|
|
|
(*objectV2.ShortHeader)(nil), (*objectV2.HeaderWithSignature)(nil),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := v
|
|
|
|
|
|
|
|
hdr = new(objectV2.Header)
|
|
|
|
hdr.SetPayloadLength(h.GetPayloadLength())
|
|
|
|
hdr.SetVersion(h.GetVersion())
|
|
|
|
hdr.SetOwnerID(h.GetOwnerID())
|
|
|
|
hdr.SetObjectType(h.GetObjectType())
|
|
|
|
hdr.SetCreationEpoch(h.GetCreationEpoch())
|
|
|
|
hdr.SetPayloadHash(h.GetPayloadHash())
|
|
|
|
hdr.SetHomomorphicHash(h.GetHomomorphicHash())
|
|
|
|
case *objectV2.HeaderWithSignature:
|
|
|
|
if body.GetMainOnly() {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("wrong header part type: expected %T, received %T",
|
2021-04-29 12:18:29 +00:00
|
|
|
(*objectV2.HeaderWithSignature)(nil), (*objectV2.ShortHeader)(nil),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdrWithSig := v
|
|
|
|
if hdrWithSig == nil {
|
|
|
|
return nil, errors.New("nil object part")
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr = hdrWithSig.GetHeader()
|
|
|
|
idSig = hdrWithSig.GetSignature()
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, ok := objAddr.ObjectID()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("missing object ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
var idV2 refs.ObjectID
|
|
|
|
id.WriteToV2(&idV2)
|
|
|
|
|
2021-04-29 12:18:29 +00:00
|
|
|
if err := signature2.VerifyDataWithSource(
|
|
|
|
signature.StableMarshalerWrapper{
|
2022-05-12 16:37:46 +00:00
|
|
|
SM: &idV2,
|
2021-04-29 12:18:29 +00:00
|
|
|
},
|
|
|
|
func() (key, sig []byte) {
|
|
|
|
return idSig.GetKey(), idSig.GetSign()
|
|
|
|
},
|
|
|
|
); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("incorrect object header signature: %w", err)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
case *objectV2.SplitInfo:
|
2022-03-03 14:19:05 +00:00
|
|
|
si := object.NewSplitInfoFromV2(v)
|
2021-04-29 12:18:29 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return nil, object.NewSplitInfoError(si)
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
objv2 := new(objectV2.Object)
|
|
|
|
objv2.SetHeader(hdr)
|
|
|
|
objv2.SetSignature(idSig)
|
2021-04-29 12:18:29 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := objAddr.ObjectID()
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := object.NewFromV2(objv2)
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetID(id)
|
2021-04-29 12:18:29 +00:00
|
|
|
|
|
|
|
// convert the object
|
2022-03-03 14:19:05 +00:00
|
|
|
return obj, nil
|
2021-06-22 12:08:17 +00:00
|
|
|
}))
|
2021-04-29 12:18:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func splitInfoResponse(info *object.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
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func splitInfoRangeResponse(info *object.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
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func setSplitInfoHeadResponse(info *object.SplitInfo, resp *objectV2.HeadResponse) {
|
2020-12-09 10:32:33 +00:00
|
|
|
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())
|
2020-12-23 09:35:36 +00:00
|
|
|
sh.SetHomomorphicHash(hdrV2.GetHomomorphicHash())
|
|
|
|
sh.SetPayloadHash(hdrV2.GetPayloadHash())
|
2020-12-09 10:32:33 +00:00
|
|
|
|
|
|
|
return sh
|
|
|
|
}
|
2021-06-22 12:08:17 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func groupAddressRequestForwarder(f func(network.Address, client.MultiAddressClient, []byte) (*object.Object, error)) getsvc.RequestForwarder {
|
|
|
|
return func(info client.NodeInfo, c client.MultiAddressClient) (*object.Object, error) {
|
2021-06-22 12:08:17 +00:00
|
|
|
var (
|
|
|
|
firstErr error
|
2022-03-03 14:19:05 +00:00
|
|
|
res *object.Object
|
2021-09-28 06:02:02 +00:00
|
|
|
|
|
|
|
key = info.PublicKey()
|
2021-06-22 12:08:17 +00:00
|
|
|
)
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
info.AddressGroup().IterateAddresses(func(addr network.Address) (stop bool) {
|
2021-06-22 12:08:17 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
stop = err == nil
|
|
|
|
|
|
|
|
if stop || firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
// would be nice to log otherwise
|
|
|
|
}()
|
|
|
|
|
2021-09-28 06:02:02 +00:00
|
|
|
res, err = f(addr, c, key)
|
2021-06-22 12:08:17 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, firstErr
|
|
|
|
}
|
|
|
|
}
|