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-node/pkg/services/util"
|
2024-11-07 14:32:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
2020-08-25 13:36:32 +00:00
|
|
|
)
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
type SignService struct {
|
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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
SearchStream
|
2023-07-26 12:47:32 +00:00
|
|
|
sigSvc *util.SignService
|
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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
GetObjectStream
|
2023-07-26 12:47:32 +00:00
|
|
|
sigSvc *util.SignService
|
2020-08-25 15:08:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:45:14 +00:00
|
|
|
type putStreamSigner struct {
|
2023-07-26 12:47:32 +00:00
|
|
|
sigSvc *util.SignService
|
|
|
|
stream PutObjectStream
|
|
|
|
err error
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 10:01:57 +00:00
|
|
|
type patchStreamSigner struct {
|
|
|
|
sigSvc *util.SignService
|
2024-08-12 14:11:10 +00:00
|
|
|
stream PatchObjectStream
|
2024-08-12 10:01:57 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:57:57 +00:00
|
|
|
type getRangeStreamSigner struct {
|
2022-12-30 17:01:13 +00:00
|
|
|
GetObjectRangeStream
|
2023-07-26 12:47:32 +00:00
|
|
|
sigSvc *util.SignService
|
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
|
|
|
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 {
|
2023-08-17 11:24:38 +00:00
|
|
|
return s.send(resp, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *getStreamSigner) send(resp *object.GetResponse, err error) error {
|
|
|
|
if err := s.sigSvc.SignResponse(resp, err); err != nil {
|
2022-12-30 17:01:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.GetObjectStream.Send(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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
if err := s.sigSvc.VerifyRequest(req); err != nil {
|
|
|
|
resp := new(object.GetResponse)
|
2023-07-26 12:47:32 +00:00
|
|
|
_ = s.sigSvc.SignResponse(resp, err)
|
2022-12-30 17:01:13 +00:00
|
|
|
return stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
2023-08-17 11:24:38 +00:00
|
|
|
w := &getStreamSigner{
|
2022-12-30 17:01:13 +00:00
|
|
|
GetObjectStream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
2023-08-17 11:24:38 +00:00
|
|
|
}
|
|
|
|
if err := s.svc.Get(req, w); err != nil {
|
|
|
|
return w.send(new(object.GetResponse), err)
|
|
|
|
}
|
|
|
|
return nil
|
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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
if s.err = s.sigSvc.VerifyRequest(req); s.err != nil {
|
|
|
|
return util.ErrAbortStream
|
|
|
|
}
|
|
|
|
if s.err = s.stream.Send(ctx, req); s.err != nil {
|
|
|
|
return util.ErrAbortStream
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *putStreamSigner) CloseAndRecv(ctx context.Context) (resp *object.PutResponse, err error) {
|
|
|
|
if s.err != nil {
|
|
|
|
err = s.err
|
|
|
|
resp = new(object.PutResponse)
|
|
|
|
} else {
|
|
|
|
resp, err = s.stream.CloseAndRecv(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not close stream and receive response: %w", err)
|
|
|
|
}
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
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{
|
2022-12-30 17:01:13 +00:00
|
|
|
stream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
2020-08-25 15:45:14 +00:00
|
|
|
}, nil
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 10:01:57 +00:00
|
|
|
func (s *patchStreamSigner) Send(ctx context.Context, req *object.PatchRequest) error {
|
|
|
|
if s.err = s.sigSvc.VerifyRequest(req); s.err != nil {
|
|
|
|
return util.ErrAbortStream
|
|
|
|
}
|
|
|
|
if s.err = s.stream.Send(ctx, req); s.err != nil {
|
|
|
|
return util.ErrAbortStream
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *patchStreamSigner) CloseAndRecv(ctx context.Context) (resp *object.PatchResponse, err error) {
|
|
|
|
if s.err != nil {
|
|
|
|
err = s.err
|
|
|
|
resp = new(object.PatchResponse)
|
|
|
|
} else {
|
|
|
|
resp, err = s.stream.CloseAndRecv(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not close stream and receive response: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
|
|
|
}
|
|
|
|
|
2024-08-12 14:11:10 +00:00
|
|
|
func (s *SignService) Patch() (PatchObjectStream, error) {
|
2024-08-12 10:01:57 +00:00
|
|
|
stream, err := s.svc.Patch()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create Put object streamer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &patchStreamSigner{
|
|
|
|
stream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 23:45:25 +00:00
|
|
|
func (s *SignService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
2022-12-30 15:26:43 +00:00
|
|
|
if err := s.sigSvc.VerifyRequest(req); err != nil {
|
|
|
|
resp := new(object.HeadResponse)
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
2020-08-25 16:07:53 +00:00
|
|
|
}
|
2022-12-30 15:26:43 +00:00
|
|
|
resp, err := util.EnsureNonNilResponse(s.svc.Head(ctx, req))
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
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) {
|
2024-02-15 14:13:29 +00:00
|
|
|
req.GetBody().SetMarshalData(req.GetBody().StableMarshal(nil))
|
2022-12-30 15:26:43 +00:00
|
|
|
if err := s.sigSvc.VerifyRequest(req); err != nil {
|
|
|
|
resp := new(object.PutSingleResponse)
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
2023-07-03 08:36:20 +00:00
|
|
|
}
|
2022-12-30 15:26:43 +00:00
|
|
|
resp, err := util.EnsureNonNilResponse(s.svc.PutSingle(ctx, req))
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
2023-07-03 08:36:20 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-08-17 11:24:38 +00:00
|
|
|
return s.send(resp, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *searchStreamSigner) send(resp *object.SearchResponse, err error) error {
|
|
|
|
if err := s.sigSvc.SignResponse(resp, err); err != nil {
|
2022-12-30 17:01:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.SearchStream.Send(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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
if err := s.sigSvc.VerifyRequest(req); err != nil {
|
|
|
|
resp := new(object.SearchResponse)
|
2023-07-26 12:47:32 +00:00
|
|
|
_ = s.sigSvc.SignResponse(resp, err)
|
2022-12-30 17:01:13 +00:00
|
|
|
return stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
ss := &searchStreamSigner{
|
2023-07-26 12:47:32 +00:00
|
|
|
SearchStream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
2022-12-30 17:01:13 +00:00
|
|
|
}
|
|
|
|
err := s.svc.Search(req, ss)
|
2023-08-17 11:24:38 +00:00
|
|
|
if err != nil || !ss.nonEmptyResp {
|
2022-12-30 17:01:13 +00:00
|
|
|
// 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).
|
2023-08-17 11:24:38 +00:00
|
|
|
return ss.send(new(object.SearchResponse), err)
|
2022-12-30 17:01:13 +00:00
|
|
|
}
|
2023-08-17 11:24:38 +00:00
|
|
|
return nil
|
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) {
|
2022-12-30 15:26:43 +00:00
|
|
|
if err := s.sigSvc.VerifyRequest(req); err != nil {
|
|
|
|
resp := new(object.DeleteResponse)
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
2020-08-25 16:12:36 +00:00
|
|
|
}
|
2022-12-30 15:26:43 +00:00
|
|
|
resp, err := util.EnsureNonNilResponse(s.svc.Delete(ctx, req))
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (s *getRangeStreamSigner) Send(resp *object.GetRangeResponse) error {
|
2023-08-17 11:24:38 +00:00
|
|
|
return s.send(resp, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *getRangeStreamSigner) send(resp *object.GetRangeResponse, err error) error {
|
|
|
|
if err := s.sigSvc.SignResponse(resp, err); err != nil {
|
2022-12-30 17:01:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.GetObjectRangeStream.Send(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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
if err := s.sigSvc.VerifyRequest(req); err != nil {
|
|
|
|
resp := new(object.GetRangeResponse)
|
2023-07-26 12:47:32 +00:00
|
|
|
_ = s.sigSvc.SignResponse(resp, err)
|
2022-12-30 17:01:13 +00:00
|
|
|
return stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
2023-08-17 11:24:38 +00:00
|
|
|
w := &getRangeStreamSigner{
|
2022-12-30 17:01:13 +00:00
|
|
|
GetObjectRangeStream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
2023-08-17 11:24:38 +00:00
|
|
|
}
|
|
|
|
if err := s.svc.GetRange(req, w); err != nil {
|
|
|
|
return w.send(new(object.GetRangeResponse), err)
|
|
|
|
}
|
|
|
|
return nil
|
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) {
|
2022-12-30 15:26:43 +00:00
|
|
|
if err := s.sigSvc.VerifyRequest(req); err != nil {
|
|
|
|
resp := new(object.GetRangeHashResponse)
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
2020-08-25 16:18:24 +00:00
|
|
|
}
|
2022-12-30 15:26:43 +00:00
|
|
|
resp, err := util.EnsureNonNilResponse(s.svc.GetRangeHash(ctx, req))
|
2023-07-26 12:47:32 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(resp, err)
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|