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
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
"github.com/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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
SearchStream
|
|
|
|
statusSupported bool
|
|
|
|
sigSvc *util.SignService
|
|
|
|
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
|
|
|
|
statusSupported bool
|
|
|
|
sigSvc *util.SignService
|
2020-08-25 15:08:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:45:14 +00:00
|
|
|
type putStreamSigner struct {
|
2022-12-30 17:01:13 +00:00
|
|
|
sigSvc *util.SignService
|
|
|
|
stream PutObjectStream
|
|
|
|
statusSupported bool
|
|
|
|
err error
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:57:57 +00:00
|
|
|
type getRangeStreamSigner struct {
|
2022-12-30 17:01:13 +00:00
|
|
|
GetObjectRangeStream
|
|
|
|
statusSupported bool
|
|
|
|
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
|
|
|
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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
if err := s.sigSvc.SignResponse(s.statusSupported, resp, nil); err != nil {
|
|
|
|
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)
|
|
|
|
_ = s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
|
|
|
return stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.svc.Get(req, &getStreamSigner{
|
|
|
|
GetObjectStream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
|
|
|
statusSupported: util.IsStatusSupported(req),
|
|
|
|
})
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:45:14 +00:00
|
|
|
func (s *putStreamSigner) Send(req *object.PutRequest) error {
|
2022-12-30 17:01:13 +00:00
|
|
|
s.statusSupported = util.IsStatusSupported(req)
|
2020-08-25 15:45:14 +00:00
|
|
|
|
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(req); s.err != nil {
|
|
|
|
return util.ErrAbortStream
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *putStreamSigner) CloseAndRecv() (resp *object.PutResponse, err error) {
|
|
|
|
if s.err != nil {
|
|
|
|
err = s.err
|
|
|
|
resp = new(object.PutResponse)
|
|
|
|
} else {
|
|
|
|
resp, err = s.stream.CloseAndRecv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not close stream and receive response: %w", err)
|
|
|
|
}
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 17:01:13 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(s.statusSupported, resp, err)
|
2020-08-25 15:45:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 10:53:08 +00:00
|
|
|
func (s *SignService) Put(ctx context.Context) (PutObjectStream, 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 {
|
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
|
|
|
}
|
|
|
|
|
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)
|
2022-12-30 17:01:13 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
2020-08-25 16:07:53 +00:00
|
|
|
}
|
2022-12-30 15:26:43 +00:00
|
|
|
resp, err := util.WrapResponse(s.svc.Head(ctx, req))
|
2022-12-30 17:01:13 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
2020-08-25 13:36:32 +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
|
2022-12-30 17:01:13 +00:00
|
|
|
if err := s.sigSvc.SignResponse(s.statusSupported, resp, nil); err != nil {
|
|
|
|
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)
|
|
|
|
_ = s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
|
|
|
return stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
ss := &searchStreamSigner{
|
|
|
|
SearchStream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
|
|
|
statusSupported: util.IsStatusSupported(req),
|
|
|
|
}
|
|
|
|
err := s.svc.Search(req, ss)
|
|
|
|
if err == nil && !ss.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))
|
|
|
|
}
|
|
|
|
return err
|
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)
|
2022-12-30 17:01:13 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
2020-08-25 16:12:36 +00:00
|
|
|
}
|
2022-12-30 15:26:43 +00:00
|
|
|
resp, err := util.WrapResponse(s.svc.Delete(ctx, req))
|
2022-12-30 17:01:13 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(util.IsStatusSupported(req), 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 {
|
2022-12-30 17:01:13 +00:00
|
|
|
if err := s.sigSvc.SignResponse(s.statusSupported, resp, nil); err != nil {
|
|
|
|
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)
|
|
|
|
_ = s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
|
|
|
return stream.Send(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.svc.GetRange(req, &getRangeStreamSigner{
|
|
|
|
GetObjectRangeStream: stream,
|
|
|
|
sigSvc: s.sigSvc,
|
|
|
|
statusSupported: util.IsStatusSupported(req),
|
|
|
|
})
|
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)
|
2022-12-30 17:01:13 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
2020-08-25 16:18:24 +00:00
|
|
|
}
|
2022-12-30 15:26:43 +00:00
|
|
|
resp, err := util.WrapResponse(s.svc.GetRangeHash(ctx, req))
|
2022-12-30 17:01:13 +00:00
|
|
|
return resp, s.sigSvc.SignResponse(util.IsStatusSupported(req), resp, err)
|
2020-08-25 13:36:32 +00:00
|
|
|
}
|