2020-08-25 13:36:32 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/util"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type signService struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2020-08-26 09:20:26 +00:00
|
|
|
unarySigService *util.UnarySignService
|
|
|
|
|
|
|
|
svc object.Service
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type searchStreamSigner struct {
|
2020-08-26 09:41:43 +00:00
|
|
|
stream *util.MessageStreamer
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:08:17 +00:00
|
|
|
type getStreamSigner struct {
|
2020-08-26 09:41:43 +00:00
|
|
|
stream *util.MessageStreamer
|
2020-08-25 15:08:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:45:14 +00:00
|
|
|
type putStreamSigner struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
stream object.PutObjectStreamer
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:57:57 +00:00
|
|
|
type getRangeStreamSigner struct {
|
2020-08-26 09:41:43 +00:00
|
|
|
stream *util.MessageStreamer
|
2020-08-25 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 13:36:32 +00:00
|
|
|
func NewSignService(key *ecdsa.PrivateKey, svc object.Service) object.Service {
|
|
|
|
return &signService{
|
2020-08-26 09:20:26 +00:00
|
|
|
key: key,
|
|
|
|
unarySigService: util.NewUnarySignService(key),
|
|
|
|
svc: svc,
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:08:17 +00:00
|
|
|
func (s *getStreamSigner) Recv() (*object.GetResponse, error) {
|
|
|
|
r, err := s.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not receive response")
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
return r.(*object.GetResponse), nil
|
2020-08-25 15:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signService) Get(ctx context.Context, req *object.GetRequest) (object.GetObjectStreamer, error) {
|
2020-08-26 09:41:43 +00:00
|
|
|
stream, err := s.unarySigService.HandleServerStreamRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (util.MessageReader, error) {
|
|
|
|
stream, err := s.svc.Get(ctx, req.(*object.GetRequest))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
return stream.Recv()
|
|
|
|
}, nil
|
2020-08-26 09:20:26 +00:00
|
|
|
},
|
|
|
|
)
|
2020-08-25 15:08:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &getStreamSigner{
|
2020-08-26 09:41:43 +00:00
|
|
|
stream: stream,
|
2020-08-25 15:08:17 +00:00
|
|
|
}, nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:45:14 +00:00
|
|
|
func (s *putStreamSigner) Send(req *object.PutRequest) error {
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return errors.Wrap(err, "could not verify request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.stream.Send(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *putStreamSigner) CloseAndRecv() (*object.PutResponse, error) {
|
|
|
|
r, err := s.stream.CloseAndRecv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not receive response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(s.key, r); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not sign response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signService) Put(ctx context.Context) (object.PutObjectStreamer, error) {
|
2020-08-26 09:20:26 +00:00
|
|
|
stream, err := s.svc.Put(ctx)
|
2020-08-25 15:45:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create Put object streamer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &putStreamSigner{
|
|
|
|
key: s.key,
|
|
|
|
stream: stream,
|
|
|
|
}, nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 16:07:53 +00:00
|
|
|
func (s *signService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
2020-08-26 09:20:26 +00:00
|
|
|
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return s.svc.Head(ctx, req.(*object.HeadRequest))
|
|
|
|
},
|
|
|
|
)
|
2020-08-25 16:07:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.(*object.HeadResponse), nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *searchStreamSigner) Recv() (*object.SearchResponse, error) {
|
|
|
|
r, err := s.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not receive response")
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
return r.(*object.SearchResponse), nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signService) Search(ctx context.Context, req *object.SearchRequest) (object.SearchObjectStreamer, error) {
|
2020-08-26 09:41:43 +00:00
|
|
|
stream, err := s.unarySigService.HandleServerStreamRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (util.MessageReader, error) {
|
|
|
|
stream, err := s.svc.Search(ctx, req.(*object.SearchRequest))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
return stream.Recv()
|
|
|
|
}, nil
|
2020-08-26 09:20:26 +00:00
|
|
|
},
|
|
|
|
)
|
2020-08-25 13:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &searchStreamSigner{
|
2020-08-26 09:41:43 +00:00
|
|
|
stream: stream,
|
2020-08-25 13:36:32 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-08-25 16:12:36 +00:00
|
|
|
func (s *signService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
|
2020-08-26 09:20:26 +00:00
|
|
|
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return s.svc.Delete(ctx, req.(*object.DeleteRequest))
|
|
|
|
},
|
|
|
|
)
|
2020-08-25 16:12:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.(*object.DeleteResponse), nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:57:57 +00:00
|
|
|
func (s *getRangeStreamSigner) Recv() (*object.GetRangeResponse, error) {
|
|
|
|
r, err := s.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not receive response")
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
return r.(*object.GetRangeResponse), nil
|
2020-08-25 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *signService) GetRange(ctx context.Context, req *object.GetRangeRequest) (object.GetRangeObjectStreamer, error) {
|
2020-08-26 09:41:43 +00:00
|
|
|
stream, err := s.unarySigService.HandleServerStreamRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (util.MessageReader, error) {
|
|
|
|
stream, err := s.svc.GetRange(ctx, req.(*object.GetRangeRequest))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
return stream.Recv()
|
|
|
|
}, nil
|
2020-08-26 09:20:26 +00:00
|
|
|
},
|
|
|
|
)
|
2020-08-25 15:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &getRangeStreamSigner{
|
2020-08-26 09:41:43 +00:00
|
|
|
stream: stream,
|
2020-08-25 15:57:57 +00:00
|
|
|
}, nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 16:18:24 +00:00
|
|
|
func (s *signService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
2020-08-26 09:20:26 +00:00
|
|
|
resp, err := s.unarySigService.HandleUnaryRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
return s.svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest))
|
|
|
|
},
|
|
|
|
)
|
2020-08-25 16:18:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.(*object.GetRangeHashResponse), nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|