From 6cb9c13c5e35183bb521f033cde0c8da5b566323 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 24 May 2022 11:38:45 +0300 Subject: [PATCH] [#1424] neofs-cli: Fail immediately if a client can't be created Signed-off-by: Evgenii Stratonikov --- cmd/neofs-cli/internal/client/sdk.go | 13 ++++++++++++- cmd/neofs-cli/modules/accounting/balance.go | 3 +-- cmd/neofs-cli/modules/control/util.go | 4 +--- cmd/neofs-cli/modules/object.go | 6 +++--- cmd/neofs-cli/modules/root.go | 3 +-- 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cmd/neofs-cli/internal/client/sdk.go b/cmd/neofs-cli/internal/client/sdk.go index 08991d08..e3acac65 100644 --- a/cmd/neofs-cli/internal/client/sdk.go +++ b/cmd/neofs-cli/internal/client/sdk.go @@ -5,15 +5,26 @@ import ( "errors" "fmt" + "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/pkg/network" "github.com/nspcc-dev/neofs-sdk-go/client" + "github.com/spf13/cobra" "github.com/spf13/viper" ) var errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect") // GetSDKClientByFlag returns default neofs-sdk-go client using the specified flag for the address. -func GetSDKClientByFlag(key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) { +// On error, outputs to stderr of cmd and exits with non-zero code. +func GetSDKClientByFlag(cmd *cobra.Command, key *ecdsa.PrivateKey, endpointFlag string) *client.Client { + cli, err := getSDKClientByFlag(key, endpointFlag) + if err != nil { + common.ExitOnErr(cmd, "can't create API client: %w", err) + } + return cli +} + +func getSDKClientByFlag(key *ecdsa.PrivateKey, endpointFlag string) (*client.Client, error) { var addr network.Address err := addr.FromString(viper.GetString(endpointFlag)) diff --git a/cmd/neofs-cli/modules/accounting/balance.go b/cmd/neofs-cli/modules/accounting/balance.go index 8a4b25e1..e4048b7f 100644 --- a/cmd/neofs-cli/modules/accounting/balance.go +++ b/cmd/neofs-cli/modules/accounting/balance.go @@ -35,8 +35,7 @@ var accountingBalanceCmd = &cobra.Command{ common.ExitOnErr(cmd, "can't decode owner ID wallet address: %w", oid.DecodeString(balanceOwner)) } - cli, err := internalclient.GetSDKClientByFlag(pk, commonflags.RPC) - common.ExitOnErr(cmd, "create API client: %w", err) + cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC) var prm internalclient.BalanceOfPrm prm.SetClient(cli) diff --git a/cmd/neofs-cli/modules/control/util.go b/cmd/neofs-cli/modules/control/util.go index 724c10f5..5f22ae8f 100644 --- a/cmd/neofs-cli/modules/control/util.go +++ b/cmd/neofs-cli/modules/control/util.go @@ -49,7 +49,5 @@ func verifyResponse(cmd *cobra.Command, } func getClient(cmd *cobra.Command, pk *ecdsa.PrivateKey) *client.Client { - cli, err := internalclient.GetSDKClientByFlag(pk, controlRPC) - common.ExitOnErr(cmd, "", err) - return cli + return internalclient.GetSDKClientByFlag(cmd, pk, controlRPC) } diff --git a/cmd/neofs-cli/modules/object.go b/cmd/neofs-cli/modules/object.go index b509e174..9f81a410 100644 --- a/cmd/neofs-cli/modules/object.go +++ b/cmd/neofs-cli/modules/object.go @@ -338,8 +338,7 @@ func prepareSessionPrmWithOwner( ownerID *user.ID, prms ...clientKeySession, ) { - cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) - common.ExitOnErr(cmd, "create API client: %w", err) + cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC) var sessionToken *session.Token if tokenPath, _ := cmd.Flags().GetString(sessionTokenFlag); len(tokenPath) != 0 { @@ -352,6 +351,7 @@ func prepareSessionPrmWithOwner( common.ExitOnErr(cmd, "can't unmarshal session token: %w", err) } } else { + var err error sessionToken, err = sessionCli.CreateSession(cli, ownerID, sessionTokenLifetime) common.ExitOnErr(cmd, "", err) } @@ -387,7 +387,7 @@ func prepareSessionPrmWithOwner( tok.SetIat(sessionToken.Iat()) tok.SetNbf(sessionToken.Nbf()) - err = tok.Sign(key) + err := tok.Sign(key) common.ExitOnErr(cmd, "session token signing: %w", err) prms[i].SetClient(cli) diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 36e9421a..e0fce317 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -152,8 +152,7 @@ func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) { // creates NeoFS API client and writes it to target along with the private key. func prepareAPIClientWithKey(cmd *cobra.Command, key *ecdsa.PrivateKey, dst ...clientWithKey) { - cli, err := internalclient.GetSDKClientByFlag(key, commonflags.RPC) - common.ExitOnErr(cmd, "create API client: %w", err) + cli := internalclient.GetSDKClientByFlag(cmd, key, commonflags.RPC) for _, d := range dst { d.SetClient(cli)