2020-09-22 06:51:47 +00:00
|
|
|
package searchsvc
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-04-29 08:54:40 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-04-29 08:54:40 +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 08:54:40 +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-10 12:26:40 +00:00
|
|
|
objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
|
2021-09-28 06:02:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/internal"
|
2020-09-22 06:51:47 +00:00
|
|
|
searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search"
|
2020-09-29 15:05:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-09-22 06:51:47 +00:00
|
|
|
)
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStream) (*searchsvc.Prm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
2020-09-22 06:51:47 +00:00
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
p := new(searchsvc.Prm)
|
2021-10-26 13:02:46 +00:00
|
|
|
p.SetCommonParameters(commonPrm)
|
2020-12-11 11:59:16 +00:00
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
p.SetWriter(&streamWriter{
|
|
|
|
stream: stream,
|
|
|
|
})
|
|
|
|
|
2021-04-29 08:54:40 +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-01-26 12:11:13 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.MultiAddressClient, pubkey []byte) ([]*oidSDK.ID, error) {
|
2021-04-29 08:54:40 +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 08:54:40 +00:00
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:13:08 +00:00
|
|
|
stream, err := rpc.SearchObjects(c.RawForAddress(addr), req, rpcclient.WithContext(stream.Context()))
|
2021-04-29 08:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.SearchObjects implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
var (
|
2022-01-26 12:11:13 +00:00
|
|
|
searchResult []*oidSDK.ID
|
2021-04-29 08:54:40 +00:00
|
|
|
resp = new(objectV2.SearchResponse)
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
|
|
|
err := stream.Read(resp)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-04-29 08:54:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2021-04-29 08:54:40 +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 08:54:40 +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 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunk := resp.GetBody().GetIDList()
|
|
|
|
for i := range chunk {
|
2022-01-26 12:11:13 +00:00
|
|
|
searchResult = append(searchResult, oidSDK.NewIDFromV2(chunk[i]))
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResult, nil
|
2021-06-22 12:19:30 +00:00
|
|
|
}))
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
body := req.GetBody()
|
2021-05-31 11:03:17 +00:00
|
|
|
p.WithContainerID(cid.NewFromV2(body.GetContainerID()))
|
2022-03-03 14:19:05 +00:00
|
|
|
p.WithSearchFilters(object.NewSearchFiltersFromV2(body.GetFilters()))
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
return p, nil
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
2021-06-22 12:19:30 +00:00
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func groupAddressRequestForwarder(f func(network.Address, client.MultiAddressClient, []byte) ([]*oidSDK.ID, error)) searchsvc.RequestForwarder {
|
|
|
|
return func(info client.NodeInfo, c client.MultiAddressClient) ([]*oidSDK.ID, error) {
|
2021-06-22 12:19:30 +00:00
|
|
|
var (
|
|
|
|
firstErr error
|
2022-01-26 12:11:13 +00:00
|
|
|
res []*oidSDK.ID
|
2021-09-28 06:02:02 +00:00
|
|
|
|
|
|
|
key = info.PublicKey()
|
2021-06-22 12:19:30 +00:00
|
|
|
)
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
info.AddressGroup().IterateAddresses(func(addr network.Address) (stop bool) {
|
2021-06-22 12:19:30 +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:19:30 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, firstErr
|
|
|
|
}
|
|
|
|
}
|