2020-08-24 10:05:10 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-08-24 10:05:10 +00:00
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not verify request: %w", err)
|
2020-08-26 10:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not close stream and receive response: %w", err)
|
2020-08-26 10:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not sign response: %w", err)
|
2020-08-26 10:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not receive response message for signing: %w", err)
|
2020-08-26 09:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(s.key, m); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not sign response message: %w", err)
|
2020-08-26 09:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
2020-08-25 13:01:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 15:17:06 +00:00
|
|
|
func (s *SignService) HandleServerStreamRequest(req interface{}, respWriter ResponseMessageWriter) (ResponseMessageWriter, error) {
|
2020-12-02 23:45:25 +00:00
|
|
|
// verify request signatures
|
|
|
|
if err := signature.VerifyServiceMessage(req); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not verify request: %w", err)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return func(resp ResponseMessage) error {
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not sign response message: %w", err)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not verify request: %w", err)
|
2020-08-24 10:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not handle request: %w", err)
|
2020-08-24 10:05:10 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:41:43 +00:00
|
|
|
// sign the response
|
|
|
|
if err := signature.SignServiceMessage(s.key, resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not sign response: %w", err)
|
2020-08-26 09:41:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 10:05:10 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|