From 99749ea04278814ffaf959f45397b6f2c3ff95dc Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 3 Mar 2022 11:29:14 +0300 Subject: [PATCH] [#1211] neofs-cli: Specify address in session token Signed-off-by: Alex Vanin --- cmd/neofs-cli/modules/container.go | 5 ++++- cmd/neofs-cli/modules/object.go | 28 ++++++++++++++++----------- cmd/neofs-cli/modules/storagegroup.go | 25 ++++++++++++++++++------ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/cmd/neofs-cli/modules/container.go b/cmd/neofs-cli/modules/container.go index 650e7758d..381521295 100644 --- a/cmd/neofs-cli/modules/container.go +++ b/cmd/neofs-cli/modules/container.go @@ -19,6 +19,7 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/eacl" "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/nspcc-dev/neofs-sdk-go/object" + addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address" "github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/nspcc-dev/neofs-sdk-go/policy" "github.com/nspcc-dev/neofs-sdk-go/session" @@ -276,7 +277,9 @@ var listContainerObjectsCmd = &cobra.Command{ var prm internalclient.SearchObjectsPrm - prepareSessionPrm(cmd, &prm) + sessionObjectCtxAddress := addressSDK.NewAddress() + sessionObjectCtxAddress.SetContainerID(id) + prepareSessionPrm(cmd, sessionObjectCtxAddress, &prm) prepareObjectPrm(cmd, &prm) prm.SetContainerID(id) prm.SetFilters(*filters) diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index f9c6f249a..5d7705309 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -283,22 +283,23 @@ type clientKeySession interface { SetSessionToken(*session.Token) } -func prepareSessionPrm(cmd *cobra.Command, prms ...clientKeySession) { +func prepareSessionPrm(cmd *cobra.Command, addr *addressSDK.Address, prms ...clientKeySession) { key, err := getKey() exitOnErr(cmd, errf("get private key: %w", err)) - prepareSessionPrmWithKey(cmd, key, prms...) + prepareSessionPrmWithKey(cmd, addr, key, prms...) } -func prepareSessionPrmWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, prms ...clientKeySession) { +func prepareSessionPrmWithKey(cmd *cobra.Command, addr *addressSDK.Address, key *ecdsa.PrivateKey, prms ...clientKeySession) { ownerID, err := getOwnerID(key) exitOnErr(cmd, errf("owner ID from key: %w", err)) - prepareSessionPrmWithOwner(cmd, key, ownerID, prms...) + prepareSessionPrmWithOwner(cmd, addr, key, ownerID, prms...) } func prepareSessionPrmWithOwner( cmd *cobra.Command, + addr *addressSDK.Address, key *ecdsa.PrivateKey, ownerID *owner.ID, prms ...clientKeySession, @@ -348,6 +349,7 @@ func prepareSessionPrmWithOwner( default: panic("invalid client parameter type") } + objectContext.ApplyTo(addr) tok := session.NewToken() tok.SetID(sessionRes.ID()) @@ -435,7 +437,9 @@ func putObject(cmd *cobra.Command, _ []string) { var prm internalclient.PutObjectPrm - prepareSessionPrmWithOwner(cmd, key, ownerID, &prm) + sessionObjectCtxAddress := addressSDK.NewAddress() + sessionObjectCtxAddress.SetContainerID(cid) + prepareSessionPrmWithOwner(cmd, sessionObjectCtxAddress, key, ownerID, &prm) prepareObjectPrm(cmd, &prm) prm.SetHeader(obj.Object()) prm.SetPayloadReader(f) @@ -453,7 +457,7 @@ func deleteObject(cmd *cobra.Command, _ []string) { var prm internalclient.DeleteObjectPrm - prepareSessionPrm(cmd, &prm) + prepareSessionPrm(cmd, objAddr, &prm) prepareObjectPrm(cmd, &prm) prm.SetAddress(objAddr) @@ -487,7 +491,7 @@ func getObject(cmd *cobra.Command, _ []string) { var prm internalclient.GetObjectPrm - prepareSessionPrm(cmd, &prm) + prepareSessionPrm(cmd, objAddr, &prm) prepareObjectPrmRaw(cmd, &prm) prm.SetAddress(objAddr) prm.SetPayloadWriter(out) @@ -521,7 +525,7 @@ func getObjectHeader(cmd *cobra.Command, _ []string) { var prm internalclient.HeadObjectPrm - prepareSessionPrm(cmd, &prm) + prepareSessionPrm(cmd, objAddr, &prm) prepareObjectPrmRaw(cmd, &prm) prm.SetAddress(objAddr) prm.SetMainOnlyFlag(mainOnly) @@ -548,7 +552,9 @@ func searchObject(cmd *cobra.Command, _ []string) { var prm internalclient.SearchObjectsPrm - prepareSessionPrm(cmd, &prm) + sessionObjectCtxAddress := addressSDK.NewAddress() + sessionObjectCtxAddress.SetContainerID(cid) + prepareSessionPrm(cmd, sessionObjectCtxAddress, &prm) prepareObjectPrm(cmd, &prm) prm.SetContainerID(cid) prm.SetFilters(sf) @@ -591,7 +597,7 @@ func getObjectHash(cmd *cobra.Command, _ []string) { objPrms = append(objPrms, &headPrm) } - prepareSessionPrm(cmd, sesPrms...) + prepareSessionPrm(cmd, objAddr, sesPrms...) prepareObjectPrm(cmd, objPrms...) tz := typ == hashTz @@ -983,7 +989,7 @@ func getObjectRange(cmd *cobra.Command, _ []string) { var prm internalclient.PayloadRangePrm - prepareSessionPrm(cmd, &prm) + prepareSessionPrm(cmd, objAddr, &prm) prepareObjectPrmRaw(cmd, &prm) prm.SetAddress(objAddr) prm.SetRange(ranges[0]) diff --git a/cmd/neofs-cli/modules/storagegroup.go b/cmd/neofs-cli/modules/storagegroup.go index ef20480a4..b7a603d14 100644 --- a/cmd/neofs-cli/modules/storagegroup.go +++ b/cmd/neofs-cli/modules/storagegroup.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "crypto/ecdsa" "errors" "fmt" @@ -11,6 +12,7 @@ import ( objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address" oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id" + "github.com/nspcc-dev/neofs-sdk-go/owner" storagegroupAPI "github.com/nspcc-dev/neofs-sdk-go/storagegroup" "github.com/spf13/cobra" ) @@ -135,10 +137,14 @@ func init() { } type sgHeadReceiver struct { - prm internalclient.HeadObjectPrm + cmd *cobra.Command + key *ecdsa.PrivateKey + ownerID *owner.ID + prm internalclient.HeadObjectPrm } func (c sgHeadReceiver) Head(addr *addressSDK.Address) (interface{}, error) { + prepareSessionPrmWithOwner(c.cmd, addr, c.key, c.ownerID, &c.prm) c.prm.SetAddress(addr) res, err := internalclient.HeadObject(c.prm) @@ -181,13 +187,18 @@ func putSG(cmd *cobra.Command, _ []string) { putPrm internalclient.PutObjectPrm ) - prepareSessionPrmWithOwner(cmd, key, ownerID, &headPrm, &putPrm) + sessionObjectCtxAddress := addressSDK.NewAddress() + sessionObjectCtxAddress.SetContainerID(cid) + prepareSessionPrmWithOwner(cmd, sessionObjectCtxAddress, key, ownerID, &putPrm) prepareObjectPrm(cmd, &headPrm, &putPrm) headPrm.SetRawFlag(true) sg, err := storagegroup.CollectMembers(sgHeadReceiver{ - prm: headPrm, + cmd: cmd, + key: key, + ownerID: ownerID, + prm: headPrm, }, cid, members) exitOnErr(cmd, errf("could not collect storage group members: %w", err)) @@ -234,7 +245,7 @@ func getSG(cmd *cobra.Command, _ []string) { var prm internalclient.GetObjectPrm - prepareSessionPrm(cmd, &prm) + prepareSessionPrm(cmd, addr, &prm) prepareObjectPrmRaw(cmd, &prm) prm.SetAddress(addr) prm.SetPayloadWriter(buf) @@ -266,7 +277,9 @@ func listSG(cmd *cobra.Command, _ []string) { var prm internalclient.SearchObjectsPrm - prepareSessionPrm(cmd, &prm) + sessionObjectCtxAddress := addressSDK.NewAddress() + sessionObjectCtxAddress.SetContainerID(cid) + prepareSessionPrm(cmd, sessionObjectCtxAddress, &prm) prepareObjectPrm(cmd, &prm) prm.SetContainerID(cid) prm.SetFilters(storagegroup.SearchQuery()) @@ -296,7 +309,7 @@ func delSG(cmd *cobra.Command, _ []string) { var prm internalclient.DeleteObjectPrm - prepareSessionPrm(cmd, &prm) + prepareSessionPrm(cmd, addr, &prm) prepareObjectPrm(cmd, &prm) prm.SetAddress(addr)