From 7dbeb09b9435809948e2afed81048aec9306a822 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 4 Apr 2023 13:20:57 +0300 Subject: [PATCH] [#193] getsvc: Reduce private key requests Get private key only once for request forwaring. Signed-off-by: Dmitrii Stepanov --- pkg/services/object/get/v2/get_forwarder.go | 11 ++++------ .../object/get/v2/get_range_forwarder.go | 11 ++++------ pkg/services/object/get/v2/head_forwarder.go | 11 +++------- pkg/services/object/get/v2/util.go | 21 ++++++++++++++++--- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/pkg/services/object/get/v2/get_forwarder.go b/pkg/services/object/get/v2/get_forwarder.go index 7314cceb5..330a0642f 100644 --- a/pkg/services/object/get/v2/get_forwarder.go +++ b/pkg/services/object/get/v2/get_forwarder.go @@ -2,6 +2,7 @@ package getsvc import ( "context" + "crypto/ecdsa" "errors" "fmt" "io" @@ -16,7 +17,6 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal" internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal/client" - "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" ) @@ -24,16 +24,13 @@ type getRequestForwarder struct { OnceResign *sync.Once OnceHeaderSending *sync.Once GlobalProgress int - KeyStorage *util.KeyStorage + Key *ecdsa.PrivateKey Request *objectV2.GetRequest Stream *streamObjectWriter } func (f *getRequestForwarder) forwardRequestToNode(ctx context.Context, addr network.Address, c client.MultiAddressClient, pubkey []byte) (*object.Object, error) { - key, err := f.KeyStorage.GetKey(nil) - if err != nil { - return nil, err - } + var err error // once compose and resign forwarding request f.OnceResign.Do(func() { @@ -44,7 +41,7 @@ func (f *getRequestForwarder) forwardRequestToNode(ctx context.Context, addr net metaHdr.SetOrigin(f.Request.GetMetaHeader()) writeCurrentVersion(metaHdr) f.Request.SetMetaHeader(metaHdr) - err = signature.SignServiceMessage(key, f.Request) + err = signature.SignServiceMessage(f.Key, f.Request) }) if err != nil { diff --git a/pkg/services/object/get/v2/get_range_forwarder.go b/pkg/services/object/get/v2/get_range_forwarder.go index 8fa4351d4..5893f8de3 100644 --- a/pkg/services/object/get/v2/get_range_forwarder.go +++ b/pkg/services/object/get/v2/get_range_forwarder.go @@ -2,6 +2,7 @@ package getsvc import ( "context" + "crypto/ecdsa" "errors" "fmt" "io" @@ -16,23 +17,19 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal" internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal/client" - "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" ) type getRangeRequestForwarder struct { OnceResign *sync.Once GlobalProgress int - KeyStorage *util.KeyStorage + Key *ecdsa.PrivateKey Request *objectV2.GetRangeRequest Stream *streamObjectRangeWriter } func (f *getRangeRequestForwarder) forwardRequestToNode(ctx context.Context, addr network.Address, c client.MultiAddressClient, pubkey []byte) (*object.Object, error) { - key, err := f.KeyStorage.GetKey(nil) - if err != nil { - return nil, err - } + var err error // once compose and resign forwarding request f.OnceResign.Do(func() { @@ -45,7 +42,7 @@ func (f *getRangeRequestForwarder) forwardRequestToNode(ctx context.Context, add f.Request.SetMetaHeader(metaHdr) - err = signature.SignServiceMessage(key, f.Request) + err = signature.SignServiceMessage(f.Key, f.Request) }) if err != nil { diff --git a/pkg/services/object/get/v2/head_forwarder.go b/pkg/services/object/get/v2/head_forwarder.go index e0b58a35d..45c0174fd 100644 --- a/pkg/services/object/get/v2/head_forwarder.go +++ b/pkg/services/object/get/v2/head_forwarder.go @@ -2,6 +2,7 @@ package getsvc import ( "context" + "crypto/ecdsa" "errors" "fmt" "sync" @@ -15,7 +16,6 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/internal" - "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util" frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto" "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object" oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id" @@ -26,17 +26,12 @@ type headRequestForwarder struct { Response *objectV2.HeadResponse OnceResign *sync.Once ObjectAddr oid.Address - KeyStorage *util.KeyStorage + Key *ecdsa.PrivateKey } func (f *headRequestForwarder) forwardRequestToNode(ctx context.Context, addr network.Address, c client.MultiAddressClient, pubkey []byte) (*object.Object, error) { var err error - key, err := f.KeyStorage.GetKey(nil) - if err != nil { - return nil, err - } - // once compose and resign forwarding request f.OnceResign.Do(func() { // compose meta header of the local server @@ -48,7 +43,7 @@ func (f *headRequestForwarder) forwardRequestToNode(ctx context.Context, addr ne f.Request.SetMetaHeader(metaHdr) - err = signature.SignServiceMessage(key, f.Request) + err = signature.SignServiceMessage(f.Key, f.Request) }) if err != nil { diff --git a/pkg/services/object/get/v2/util.go b/pkg/services/object/get/v2/util.go index 75228e2b1..3a50a6ca5 100644 --- a/pkg/services/object/get/v2/util.go +++ b/pkg/services/object/get/v2/util.go @@ -56,11 +56,16 @@ func (s *Service) toPrm(req *objectV2.GetRequest, stream objectSvc.GetObjectStre p.SetObjectWriter(streamWrapper) if !commonPrm.LocalOnly() { + key, err := s.keyStorage.GetKey(nil) + if err != nil { + return nil, err + } + forwarder := &getRequestForwarder{ OnceResign: &sync.Once{}, OnceHeaderSending: &sync.Once{}, GlobalProgress: 0, - KeyStorage: s.keyStorage, + Key: key, Request: req, Stream: streamWrapper, } @@ -107,10 +112,15 @@ func (s *Service) toRangePrm(req *objectV2.GetRangeRequest, stream objectSvc.Get } if !commonPrm.LocalOnly() { + key, err := s.keyStorage.GetKey(nil) + if err != nil { + return nil, err + } + forwarder := &getRangeRequestForwarder{ OnceResign: &sync.Once{}, GlobalProgress: 0, - KeyStorage: s.keyStorage, + Key: key, Request: req, Stream: streamWrapper, } @@ -239,12 +249,17 @@ func (s *Service) toHeadPrm(ctx context.Context, req *objectV2.HeadRequest, resp return p, nil } + key, err := s.keyStorage.GetKey(nil) + if err != nil { + return nil, err + } + forwarder := &headRequestForwarder{ Request: req, Response: resp, OnceResign: &sync.Once{}, ObjectAddr: objAddr, - KeyStorage: s.keyStorage, + Key: key, } p.SetRequestForwarder(groupAddressRequestForwarder(forwarder.forwardRequestToNode))