2020-08-24 10:05:10 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
|
2020-10-22 11:02:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-08-24 10:05:10 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-10-22 08:03:34 +00:00
|
|
|
// ResponseMessage is an interface of NeoFS response message.
|
2020-10-22 11:02:40 +00:00
|
|
|
type ResponseMessage interface {
|
|
|
|
GetMetaHeader() *session.ResponseMetaHeader
|
|
|
|
SetMetaHeader(*session.ResponseMetaHeader)
|
|
|
|
}
|
2020-10-22 08:03:34 +00:00
|
|
|
|
|
|
|
type UnaryHandler func(context.Context, interface{}) (ResponseMessage, error)
|
2020-08-24 10:05:10 +00:00
|
|
|
|
2020-08-26 09:43:39 +00:00
|
|
|
type SignService struct {
|
2020-08-24 10:05:10 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
type ResponseMessageWriter func(ResponseMessage) error
|
|
|
|
|
|
|
|
type ServerStreamHandler_ func(interface{}, ResponseMessageWriter) (ResponseMessageWriter, error)
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
type ServerStreamHandler func(context.Context, interface{}) (ResponseMessageReader, error)
|
2020-08-26 09:41:43 +00:00
|
|
|
|
2020-10-22 08:03:34 +00:00
|
|
|
type ResponseMessageReader func() (ResponseMessage, error)
|
2020-08-26 09:41:43 +00:00
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
type ResponseMessageStreamer struct {
|
2020-08-26 09:41:43 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
recv ResponseMessageReader
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestMessageWriter func(interface{}) error
|
|
|
|
|
2020-10-22 08:03:34 +00:00
|
|
|
type ClientStreamCloser func() (ResponseMessage, error)
|
2020-08-26 10:00:41 +00:00
|
|
|
|
|
|
|
type RequestMessageStreamer struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
send RequestMessageWriter
|
|
|
|
|
|
|
|
close ClientStreamCloser
|
2020-08-26 09:41:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:43:39 +00:00
|
|
|
func NewUnarySignService(key *ecdsa.PrivateKey) *SignService {
|
|
|
|
return &SignService{
|
2020-08-26 09:20:26 +00:00
|
|
|
key: key,
|
2020-08-24 10:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
func (s *RequestMessageStreamer) Send(req interface{}) error {
|
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return errors.Wrap(err, "could not verify request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.send(req)
|
|
|
|
}
|
|
|
|
|
2020-10-22 11:02:40 +00:00
|
|
|
func (s *RequestMessageStreamer) CloseAndRecv() (ResponseMessage, error) {
|
2020-08-26 10:00:41 +00:00
|
|
|
resp, err := s.close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not close stream and receive response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not sign response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SignService) CreateRequestStreamer(sender RequestMessageWriter, closer ClientStreamCloser) *RequestMessageStreamer {
|
|
|
|
return &RequestMessageStreamer{
|
|
|
|
key: s.key,
|
|
|
|
send: sender,
|
|
|
|
close: closer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-22 11:02:40 +00:00
|
|
|
func (s *ResponseMessageStreamer) Recv() (ResponseMessage, error) {
|
2020-08-26 09:41:43 +00:00
|
|
|
m, err := s.recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not receive response message for signing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(s.key, m); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not sign response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
func (s *SignService) HandleServerStreamRequest(ctx context.Context, req interface{}, handler ServerStreamHandler) (*ResponseMessageStreamer, error) {
|
2020-08-26 09:41:43 +00:00
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not verify request")
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
msgRdr, err := handler(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not create message reader")
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
return &ResponseMessageStreamer{
|
2020-08-26 09:41:43 +00:00
|
|
|
key: s.key,
|
|
|
|
recv: msgRdr,
|
|
|
|
}, nil
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *SignService) HandleServerStreamRequest_(req interface{}, respWriter ResponseMessageWriter) (ResponseMessageWriter, error) {
|
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not verify request")
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(resp ResponseMessage) error {
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
|
|
|
return errors.Wrap(err, "could not sign response message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return respWriter(resp)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-22 11:02:40 +00:00
|
|
|
func (s *SignService) HandleUnaryRequest(ctx context.Context, req interface{}, handler UnaryHandler) (ResponseMessage, error) {
|
2020-08-24 10:05:10 +00:00
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not verify request")
|
|
|
|
}
|
|
|
|
|
|
|
|
// process request
|
2020-08-26 09:20:26 +00:00
|
|
|
resp, err := handler(ctx, req)
|
2020-08-24 10:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not handle request")
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
// sign the response
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not sign response")
|
|
|
|
}
|
|
|
|
|
2020-08-24 10:05:10 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|