[#6] services/util: Remove SignService.HandleUnaryRequest

There is no need in a wrapper with many from-`interface{}` conversions.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2022-12-30 18:26:43 +03:00
parent fef172c5b0
commit 372160d048
6 changed files with 97 additions and 218 deletions

View file

@ -22,17 +22,6 @@ func NewSignService(key *ecdsa.PrivateKey, svc Server) Server {
} }
func (s *signService) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) { func (s *signService) Balance(ctx context.Context, req *accounting.BalanceRequest) (*accounting.BalanceResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, resp, err := util.EnsureNonNilResponse(s.svc.Balance(ctx, req))
func(ctx context.Context, req any) (util.ResponseMessage, error) { return resp, s.sigSvc.SignResponse(req, resp, err)
return s.svc.Balance(ctx, req.(*accounting.BalanceRequest))
},
func() util.ResponseMessage {
return new(accounting.BalanceResponse)
},
)
if err != nil {
return nil, err
}
return resp.(*accounting.BalanceResponse), nil
} }

View file

@ -22,113 +22,64 @@ func NewSignService(key *ecdsa.PrivateKey, svc Server) Server {
} }
func (s *signService) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) { func (s *signService) Put(ctx context.Context, req *container.PutRequest) (*container.PutResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(container.PutResponse)
return s.svc.Put(ctx, req.(*container.PutRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(container.PutResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.Put(ctx, req))
return resp.(*container.PutResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) Delete(ctx context.Context, req *container.DeleteRequest) (*container.DeleteResponse, error) { func (s *signService) Delete(ctx context.Context, req *container.DeleteRequest) (*container.DeleteResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(container.DeleteResponse)
return s.svc.Delete(ctx, req.(*container.DeleteRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(container.DeleteResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.Delete(ctx, req))
return resp.(*container.DeleteResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) Get(ctx context.Context, req *container.GetRequest) (*container.GetResponse, error) { func (s *signService) Get(ctx context.Context, req *container.GetRequest) (*container.GetResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(container.GetResponse)
return s.svc.Get(ctx, req.(*container.GetRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(container.GetResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.Get(ctx, req))
return resp.(*container.GetResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) List(ctx context.Context, req *container.ListRequest) (*container.ListResponse, error) { func (s *signService) List(ctx context.Context, req *container.ListRequest) (*container.ListResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(container.ListResponse)
return s.svc.List(ctx, req.(*container.ListRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(container.ListResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.List(ctx, req))
return resp.(*container.ListResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) SetExtendedACL(ctx context.Context, req *container.SetExtendedACLRequest) (*container.SetExtendedACLResponse, error) { func (s *signService) SetExtendedACL(ctx context.Context, req *container.SetExtendedACLRequest) (*container.SetExtendedACLResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(container.SetExtendedACLResponse)
return s.svc.SetExtendedACL(ctx, req.(*container.SetExtendedACLRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(container.SetExtendedACLResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.SetExtendedACL(ctx, req))
return resp.(*container.SetExtendedACLResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) GetExtendedACL(ctx context.Context, req *container.GetExtendedACLRequest) (*container.GetExtendedACLResponse, error) { func (s *signService) GetExtendedACL(ctx context.Context, req *container.GetExtendedACLRequest) (*container.GetExtendedACLResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(container.GetExtendedACLResponse)
return s.svc.GetExtendedACL(ctx, req.(*container.GetExtendedACLRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(container.GetExtendedACLResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.GetExtendedACL(ctx, req))
return resp.(*container.GetExtendedACLResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) AnnounceUsedSpace(ctx context.Context, req *container.AnnounceUsedSpaceRequest) (*container.AnnounceUsedSpaceResponse, error) { func (s *signService) AnnounceUsedSpace(ctx context.Context, req *container.AnnounceUsedSpaceRequest) (*container.AnnounceUsedSpaceResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(container.AnnounceUsedSpaceResponse)
return s.svc.AnnounceUsedSpace(ctx, req.(*container.AnnounceUsedSpaceRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(container.AnnounceUsedSpaceResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.AnnounceUsedSpace(ctx, req))
return resp.(*container.AnnounceUsedSpaceResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }

View file

@ -24,49 +24,28 @@ func NewSignService(key *ecdsa.PrivateKey, svc Server) Server {
func (s *signService) LocalNodeInfo( func (s *signService) LocalNodeInfo(
ctx context.Context, ctx context.Context,
req *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) { req *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(netmap.LocalNodeInfoResponse)
return s.svc.LocalNodeInfo(ctx, req.(*netmap.LocalNodeInfoRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(netmap.LocalNodeInfoResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.LocalNodeInfo(ctx, req))
return resp.(*netmap.LocalNodeInfoResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) NetworkInfo(ctx context.Context, req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) { func (s *signService) NetworkInfo(ctx context.Context, req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(netmap.NetworkInfoResponse)
return s.svc.NetworkInfo(ctx, req.(*netmap.NetworkInfoRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(netmap.NetworkInfoResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.NetworkInfo(ctx, req))
return resp.(*netmap.NetworkInfoResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *signService) Snapshot(ctx context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) { func (s *signService) Snapshot(ctx context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(netmap.SnapshotResponse)
return s.svc.Snapshot(ctx, req.(*netmap.SnapshotRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(netmap.SnapshotResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.Snapshot(ctx, req))
return resp.(*netmap.SnapshotResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }

View file

@ -105,35 +105,21 @@ func (s *SignService) Put() (PutObjectStream, error) {
} }
func (s *SignService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) { func (s *SignService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(object.HeadResponse)
return s.svc.Head(ctx, req.(*object.HeadRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(object.HeadResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.Head(ctx, req))
return resp.(*object.HeadResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *SignService) PutSingle(ctx context.Context, req *object.PutSingleRequest) (*object.PutSingleResponse, error) { func (s *SignService) PutSingle(ctx context.Context, req *object.PutSingleRequest) (*object.PutSingleResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(object.PutSingleResponse)
return s.svc.PutSingle(ctx, req.(*object.PutSingleRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(object.PutSingleResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.PutSingle(ctx, req))
return resp.(*object.PutSingleResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *searchStreamSigner) Send(resp *object.SearchResponse) error { func (s *searchStreamSigner) Send(resp *object.SearchResponse) error {
@ -172,19 +158,12 @@ func (s *SignService) Search(req *object.SearchRequest, stream SearchStream) err
} }
func (s *SignService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) { func (s *SignService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(object.DeleteResponse)
return s.svc.Delete(ctx, req.(*object.DeleteRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(object.DeleteResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.Delete(ctx, req))
return resp.(*object.DeleteResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }
func (s *getRangeStreamSigner) Send(resp *object.GetRangeResponse) error { func (s *getRangeStreamSigner) Send(resp *object.GetRangeResponse) error {
@ -209,17 +188,10 @@ func (s *SignService) GetRange(req *object.GetRangeRequest, stream GetObjectRang
} }
func (s *SignService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) { func (s *SignService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(object.GetRangeHashResponse)
return s.svc.GetRangeHash(ctx, req.(*object.GetRangeHashRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(object.GetRangeHashResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.GetRangeHash(ctx, req))
return resp.(*object.GetRangeHashResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }

View file

@ -22,17 +22,10 @@ func NewSignService(key *ecdsa.PrivateKey, svc Server) Server {
} }
func (s *signService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error) { func (s *signService) Create(ctx context.Context, req *session.CreateRequest) (*session.CreateResponse, error) {
resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, if err := s.sigSvc.VerifyRequest(req); err != nil {
func(ctx context.Context, req any) (util.ResponseMessage, error) { resp := new(session.CreateResponse)
return s.svc.Create(ctx, req.(*session.CreateRequest)) return resp, s.sigSvc.SignResponse(req, resp, err)
},
func() util.ResponseMessage {
return new(session.CreateResponse)
},
)
if err != nil {
return nil, err
} }
resp, err := util.EnsureNonNilResponse(s.svc.Create(ctx, req))
return resp.(*session.CreateResponse), nil return resp, s.sigSvc.SignResponse(req, resp, err)
} }

View file

@ -172,44 +172,39 @@ func (s *SignService) HandleServerStreamRequest(
return nil return nil
} }
func (s *SignService) HandleUnaryRequest(ctx context.Context, req any, handler UnaryHandler, blankResp ResponseConstructor) (ResponseMessage, error) { func (s *SignService) SignResponse(req RequestMessage, resp ResponseMessage, err error) error {
// handle protocol versions <=2.10 (API statuses was introduced in 2.11 only) // handle protocol versions <=2.10 (API statuses was introduced in 2.11 only)
// req argument should be strengthen with type RequestMessage // req argument should be strengthen with type RequestMessage
statusSupported := isStatusSupported(req.(RequestMessage)) // panic is OK here for now statusSupported := isStatusSupported(req)
var (
resp ResponseMessage
err error
)
// verify request signatures
if err = signature.VerifyServiceMessage(req); err != nil {
var sigErr apistatus.SignatureVerification
sigErr.SetMessage(err.Error())
err = sigErr
} else {
// process request
resp, err = handler(ctx, req)
}
if err != nil { if err != nil {
if !statusSupported { if !statusSupported {
return nil, err return err
} }
resp = blankResp()
setStatusV2(resp, err) setStatusV2(resp, err)
} }
// sign the response // sign the response
if err = signResponse(s.key, resp, statusSupported); err != nil { return signResponse(s.key, resp, statusSupported)
return nil, err }
}
return resp, nil func (s *SignService) VerifyRequest(req RequestMessage) error {
if err := signature.VerifyServiceMessage(req); err != nil {
var sigErr apistatus.SignatureVerification
sigErr.SetMessage(err.Error())
return sigErr
}
return nil
}
// EnsureNonNilResponse creates an appropriate response struct if it is nil.
func EnsureNonNilResponse[T any](resp *T, err error) (*T, error) {
if resp != nil {
return resp, err
}
return new(T), err
} }
func isStatusSupported(req RequestMessage) bool { func isStatusSupported(req RequestMessage) bool {