2020-08-25 13:36:32 +00:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-08-25 13:36:32 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util"
|
2020-08-25 13:36:32 +00:00
|
|
|
)
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
type SignService struct {
|
2020-08-25 13:36:32 +00:00
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
2020-08-26 09:43:39 +00:00
|
|
|
sigSvc *util.SignService
|
2020-08-26 09:20:26 +00:00
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
svc ServiceServer
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type searchStreamSigner struct {
|
2020-12-10 12:26:40 +00:00
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
respWriter util.ResponseMessageWriter
|
2021-11-06 11:13:04 +00:00
|
|
|
|
|
|
|
nonEmptyResp bool // set on first Send call
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:08:17 +00:00
|
|
|
type getStreamSigner struct {
|
2020-12-02 23:45:25 +00:00
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
respWriter util.ResponseMessageWriter
|
2020-08-25 15:08:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:45:14 +00:00
|
|
|
type putStreamSigner struct {
|
2020-08-26 10:00:41 +00:00
|
|
|
stream *util.RequestMessageStreamer
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:57:57 +00:00
|
|
|
type getRangeStreamSigner struct {
|
2020-12-07 17:49:47 +00:00
|
|
|
util.ServerStream
|
|
|
|
|
|
|
|
respWriter util.ResponseMessageWriter
|
2020-08-25 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func NewSignService(key *ecdsa.PrivateKey, svc ServiceServer) *SignService {
|
|
|
|
return &SignService{
|
2020-08-26 09:43:39 +00:00
|
|
|
key: key,
|
|
|
|
sigSvc: util.NewUnarySignService(key),
|
|
|
|
svc: svc,
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *getStreamSigner) Send(resp *object.GetResponse) error {
|
|
|
|
return s.respWriter(resp)
|
2020-08-25 15:08:17 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *SignService) Get(req *object.GetRequest, stream GetObjectStream) error {
|
2023-04-12 13:56:56 +00:00
|
|
|
return s.sigSvc.HandleServerStreamRequest(req,
|
2020-12-02 23:45:25 +00:00
|
|
|
func(resp util.ResponseMessage) error {
|
|
|
|
return stream.Send(resp.(*object.GetResponse))
|
2020-08-26 09:20:26 +00:00
|
|
|
},
|
2021-11-06 11:13:04 +00:00
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.GetResponse)
|
|
|
|
},
|
|
|
|
func(respWriter util.ResponseMessageWriter) error {
|
|
|
|
return s.svc.Get(req, &getStreamSigner{
|
|
|
|
ServerStream: stream,
|
|
|
|
respWriter: respWriter,
|
|
|
|
})
|
|
|
|
},
|
2020-08-26 09:20:26 +00:00
|
|
|
)
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (s *putStreamSigner) Send(ctx context.Context, req *object.PutRequest) error {
|
|
|
|
return s.stream.Send(ctx, req)
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (s *putStreamSigner) CloseAndRecv(ctx context.Context) (*object.PutResponse, error) {
|
|
|
|
r, err := s.stream.CloseAndRecv(ctx)
|
2020-08-25 15:45:14 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not receive response: %w", err)
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 10:00:41 +00:00
|
|
|
return r.(*object.PutResponse), nil
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (s *SignService) Put() (PutObjectStream, error) {
|
|
|
|
stream, err := s.svc.Put()
|
2020-08-25 15:45:14 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not create Put object streamer: %w", err)
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &putStreamSigner{
|
2020-08-26 10:00:41 +00:00
|
|
|
stream: s.sigSvc.CreateRequestStreamer(
|
2023-04-03 11:23:53 +00:00
|
|
|
func(ctx context.Context, req any) error {
|
|
|
|
return stream.Send(ctx, req.(*object.PutRequest))
|
2020-08-26 10:00:41 +00:00
|
|
|
},
|
2023-04-03 11:23:53 +00:00
|
|
|
func(ctx context.Context) (util.ResponseMessage, error) {
|
|
|
|
return stream.CloseAndRecv(ctx)
|
2020-08-26 10:00:41 +00:00
|
|
|
},
|
2021-11-06 11:13:04 +00:00
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.PutResponse)
|
|
|
|
},
|
2020-08-26 10:00:41 +00:00
|
|
|
),
|
2020-08-25 15:45:14 +00:00
|
|
|
}, nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *SignService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
2020-08-26 09:43:39 +00:00
|
|
|
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req,
|
2023-02-21 11:42:45 +00:00
|
|
|
func(ctx context.Context, req any) (util.ResponseMessage, error) {
|
2020-08-26 09:20:26 +00:00
|
|
|
return s.svc.Head(ctx, req.(*object.HeadRequest))
|
|
|
|
},
|
2021-11-06 11:13:04 +00:00
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.HeadResponse)
|
|
|
|
},
|
2020-08-26 09:20:26 +00:00
|
|
|
)
|
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
|
|
|
}
|
|
|
|
|
2023-07-03 08:36:20 +00:00
|
|
|
func (s *SignService) PutSingle(ctx context.Context, req *object.PutSingleRequest) (*object.PutSingleResponse, error) {
|
|
|
|
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req,
|
|
|
|
func(ctx context.Context, req any) (util.ResponseMessage, error) {
|
|
|
|
return s.svc.PutSingle(ctx, req.(*object.PutSingleRequest))
|
|
|
|
},
|
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.PutSingleResponse)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.(*object.PutSingleResponse), nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *searchStreamSigner) Send(resp *object.SearchResponse) error {
|
2021-11-06 11:13:04 +00:00
|
|
|
s.nonEmptyResp = true
|
2020-12-10 12:26:40 +00:00
|
|
|
return s.respWriter(resp)
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *SignService) Search(req *object.SearchRequest, stream SearchStream) error {
|
2023-04-12 13:56:56 +00:00
|
|
|
return s.sigSvc.HandleServerStreamRequest(req,
|
2020-12-10 12:26:40 +00:00
|
|
|
func(resp util.ResponseMessage) error {
|
|
|
|
return stream.Send(resp.(*object.SearchResponse))
|
2020-08-26 09:20:26 +00:00
|
|
|
},
|
2021-11-06 11:13:04 +00:00
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.SearchResponse)
|
|
|
|
},
|
|
|
|
func(respWriter util.ResponseMessageWriter) error {
|
|
|
|
stream := &searchStreamSigner{
|
|
|
|
ServerStream: stream,
|
|
|
|
respWriter: respWriter,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.svc.Search(req, stream)
|
|
|
|
|
|
|
|
if err == nil && !stream.nonEmptyResp {
|
|
|
|
// The higher component does not write any response in the case of an empty result (which is correct).
|
|
|
|
// With the introduction of status returns at least one answer must be signed and sent to the client.
|
|
|
|
// This approach is supported by clients who do not know how to work with statuses (one could make
|
|
|
|
// a switch according to the protocol version from the request, but the costs of sending an empty
|
|
|
|
// answer can be neglected due to the gradual refusal to use the "old" clients).
|
|
|
|
return stream.Send(new(object.SearchResponse))
|
|
|
|
}
|
|
|
|
|
2022-01-21 09:00:08 +00:00
|
|
|
return err
|
2021-11-06 11:13:04 +00:00
|
|
|
},
|
2020-08-26 09:20:26 +00:00
|
|
|
)
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *SignService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
|
2020-08-26 09:43:39 +00:00
|
|
|
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req,
|
2023-02-21 11:42:45 +00:00
|
|
|
func(ctx context.Context, req any) (util.ResponseMessage, error) {
|
2020-08-26 09:20:26 +00:00
|
|
|
return s.svc.Delete(ctx, req.(*object.DeleteRequest))
|
|
|
|
},
|
2021-11-06 11:13:04 +00:00
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.DeleteResponse)
|
|
|
|
},
|
2020-08-26 09:20:26 +00:00
|
|
|
)
|
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-12-07 17:49:47 +00:00
|
|
|
func (s *getRangeStreamSigner) Send(resp *object.GetRangeResponse) error {
|
|
|
|
return s.respWriter(resp)
|
2020-08-25 15:57:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (s *SignService) GetRange(req *object.GetRangeRequest, stream GetObjectRangeStream) error {
|
2023-04-12 13:56:56 +00:00
|
|
|
return s.sigSvc.HandleServerStreamRequest(req,
|
2020-12-07 17:49:47 +00:00
|
|
|
func(resp util.ResponseMessage) error {
|
|
|
|
return stream.Send(resp.(*object.GetRangeResponse))
|
2020-08-26 09:20:26 +00:00
|
|
|
},
|
2021-11-06 11:13:04 +00:00
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.GetRangeResponse)
|
|
|
|
},
|
|
|
|
func(respWriter util.ResponseMessageWriter) error {
|
|
|
|
return s.svc.GetRange(req, &getRangeStreamSigner{
|
|
|
|
ServerStream: stream,
|
|
|
|
respWriter: respWriter,
|
|
|
|
})
|
|
|
|
},
|
2020-08-26 09:20:26 +00:00
|
|
|
)
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *SignService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
2020-08-26 09:43:39 +00:00
|
|
|
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req,
|
2023-02-21 11:42:45 +00:00
|
|
|
func(ctx context.Context, req any) (util.ResponseMessage, error) {
|
2020-08-26 09:20:26 +00:00
|
|
|
return s.svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest))
|
|
|
|
},
|
2021-11-06 11:13:04 +00:00
|
|
|
func() util.ResponseMessage {
|
|
|
|
return new(object.GetRangeHashResponse)
|
|
|
|
},
|
2020-08-26 09:20:26 +00:00
|
|
|
)
|
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
|
|
|
}
|