[#1424] neofs-cli: Fail immediately if a key can't be fetched

If the key can't be fetched, an error is always returned, so it makes
sense to fail the whole command inside of a `key.Get*()`.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-24 11:22:23 +03:00 committed by LeL
parent d9c5ca5e77
commit 295ec3700a
19 changed files with 70 additions and 69 deletions

View file

@ -1,4 +1,4 @@
package cmd package key
import ( import (
"bytes" "bytes"
@ -11,13 +11,12 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"golang.org/x/term" "golang.org/x/term"
) )
func Test_getKey(t *testing.T) { func Test_getOrGenerate(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
wallPath := filepath.Join(dir, "wallet.json") wallPath := filepath.Join(dir, "wallet.json")
@ -56,13 +55,13 @@ func Test_getKey(t *testing.T) {
Writer: io.Discard, Writer: io.Discard,
}, "") }, "")
checkKeyError(t, filepath.Join(dir, "badfile"), key.ErrInvalidKey) checkKeyError(t, filepath.Join(dir, "badfile"), ErrInvalidKey)
t.Run("wallet", func(t *testing.T) { t.Run("wallet", func(t *testing.T) {
checkKeyError(t, wallPath, key.ErrInvalidPassword) checkKeyError(t, wallPath, ErrInvalidPassword)
in.WriteString("invalid\r") in.WriteString("invalid\r")
checkKeyError(t, wallPath, key.ErrInvalidPassword) checkKeyError(t, wallPath, ErrInvalidPassword)
in.WriteString("pass\r") in.WriteString("pass\r")
checkKey(t, wallPath, acc2.PrivateKey()) // default account checkKey(t, wallPath, acc2.PrivateKey()) // default account
@ -72,12 +71,12 @@ func Test_getKey(t *testing.T) {
checkKey(t, wallPath, acc1.PrivateKey()) checkKey(t, wallPath, acc1.PrivateKey())
viper.Set(commonflags.Account, "not an address") viper.Set(commonflags.Account, "not an address")
checkKeyError(t, wallPath, key.ErrInvalidAddress) checkKeyError(t, wallPath, ErrInvalidAddress)
acc, err := wallet.NewAccount() acc, err := wallet.NewAccount()
require.NoError(t, err) require.NoError(t, err)
viper.Set(commonflags.Account, acc.Address) viper.Set(commonflags.Account, acc.Address)
checkKeyError(t, wallPath, key.ErrInvalidAddress) checkKeyError(t, wallPath, ErrInvalidAddress)
}) })
t.Run("WIF", func(t *testing.T) { t.Run("WIF", func(t *testing.T) {
@ -85,10 +84,10 @@ func Test_getKey(t *testing.T) {
}) })
t.Run("NEP-2", func(t *testing.T) { t.Run("NEP-2", func(t *testing.T) {
checkKeyError(t, nep2, key.ErrInvalidPassword) checkKeyError(t, nep2, ErrInvalidPassword)
in.WriteString("invalid\r") in.WriteString("invalid\r")
checkKeyError(t, nep2, key.ErrInvalidPassword) checkKeyError(t, nep2, ErrInvalidPassword)
in.WriteString("pass\r") in.WriteString("pass\r")
checkKey(t, nep2, nep2Key) checkKey(t, nep2, nep2Key)
@ -96,7 +95,7 @@ func Test_getKey(t *testing.T) {
t.Run("password from config", func(t *testing.T) { t.Run("password from config", func(t *testing.T) {
viper.Set("password", "invalid") viper.Set("password", "invalid")
in.WriteString("pass\r") in.WriteString("pass\r")
checkKeyError(t, nep2, key.ErrInvalidPassword) checkKeyError(t, nep2, ErrInvalidPassword)
viper.Set("password", "pass") viper.Set("password", "pass")
in.WriteString("invalid\r") in.WriteString("invalid\r")
@ -110,7 +109,7 @@ func Test_getKey(t *testing.T) {
t.Run("generate", func(t *testing.T) { t.Run("generate", func(t *testing.T) {
viper.Set(commonflags.GenerateKey, true) viper.Set(commonflags.GenerateKey, true)
actual, err := getKey() actual, err := getOrGenerate()
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, actual) require.NotNil(t, actual)
for _, p := range []*keys.PrivateKey{nep2Key, rawKey, wifKey, acc1.PrivateKey(), acc2.PrivateKey()} { for _, p := range []*keys.PrivateKey{nep2Key, rawKey, wifKey, acc1.PrivateKey(), acc2.PrivateKey()} {
@ -121,13 +120,13 @@ func Test_getKey(t *testing.T) {
func checkKeyError(t *testing.T, desc string, err error) { func checkKeyError(t *testing.T, desc string, err error) {
viper.Set(commonflags.WalletPath, desc) viper.Set(commonflags.WalletPath, desc)
_, actualErr := getKey() _, actualErr := getOrGenerate()
require.ErrorIs(t, actualErr, err) require.ErrorIs(t, actualErr, err)
} }
func checkKey(t *testing.T, desc string, expected *keys.PrivateKey) { func checkKey(t *testing.T, desc string, expected *keys.PrivateKey) {
viper.Set(commonflags.WalletPath, desc) viper.Set(commonflags.WalletPath, desc)
actual, err := getKey() actual, err := getOrGenerate()
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, &expected.PrivateKey, actual) require.Equal(t, &expected.PrivateKey, actual)
} }

View file

@ -8,7 +8,9 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -22,7 +24,13 @@ var errCantGenerateKey = errors.New("can't generate new private key")
// Ideally we want to touch file-system on the last step. // Ideally we want to touch file-system on the last step.
// However, asking for NEP-2 password seems to be confusing if we provide a wallet. // However, asking for NEP-2 password seems to be confusing if we provide a wallet.
// This function assumes that all flags were bind to viper in a `PersistentPreRun`. // This function assumes that all flags were bind to viper in a `PersistentPreRun`.
func Get() (*ecdsa.PrivateKey, error) { func Get(cmd *cobra.Command) *ecdsa.PrivateKey {
pk, err := get()
common.ExitOnErr(cmd, "can't fetch private key: %w", err)
return pk
}
func get() (*ecdsa.PrivateKey, error) {
keyDesc := viper.GetString(commonflags.WalletPath) keyDesc := viper.GetString(commonflags.WalletPath)
priv, err := keys.NewPrivateKeyFromWIF(keyDesc) priv, err := keys.NewPrivateKeyFromWIF(keyDesc)
if err == nil { if err == nil {
@ -47,7 +55,13 @@ func Get() (*ecdsa.PrivateKey, error) {
} }
// GetOrGenerate is similar to get but generates a new key if commonflags.GenerateKey is set. // GetOrGenerate is similar to get but generates a new key if commonflags.GenerateKey is set.
func GetOrGenerate() (*ecdsa.PrivateKey, error) { func GetOrGenerate(cmd *cobra.Command) *ecdsa.PrivateKey {
pk, err := getOrGenerate()
common.ExitOnErr(cmd, "can't fetch private key: %w", err)
return pk
}
func getOrGenerate() (*ecdsa.PrivateKey, error) {
if viper.GetBool(commonflags.GenerateKey) { if viper.GetBool(commonflags.GenerateKey) {
priv, err := keys.NewPrivateKey() priv, err := keys.NewPrivateKey()
if err != nil { if err != nil {
@ -55,7 +69,7 @@ func GetOrGenerate() (*ecdsa.PrivateKey, error) {
} }
return &priv.PrivateKey, nil return &priv.PrivateKey, nil
} }
return Get() return get()
} }
func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) { func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) {

View file

@ -26,8 +26,7 @@ var accountingBalanceCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var oid user.ID var oid user.ID
pk, err := key.GetOrGenerate() pk := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "", err)
balanceOwner, _ := cmd.Flags().GetString(ownerFlag) balanceOwner, _ := cmd.Flags().GetString(ownerFlag)
if balanceOwner == "" { if balanceOwner == "" {

View file

@ -15,6 +15,7 @@ import (
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/core/version" "github.com/nspcc-dev/neofs-node/pkg/core/version"
"github.com/nspcc-dev/neofs-sdk-go/acl" "github.com/nspcc-dev/neofs-sdk-go/acl"
"github.com/nspcc-dev/neofs-sdk-go/container" "github.com/nspcc-dev/neofs-sdk-go/container"
@ -116,8 +117,7 @@ var listContainersCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var idUser user.ID var idUser user.ID
key, err := getKey() key := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "", err)
if containerOwner == "" { if containerOwner == "" {
user.IDFromKey(&idUser, key.PublicKey) user.IDFromKey(&idUser, key.PublicKey)
@ -164,8 +164,7 @@ It will be stored in sidechain when inner ring will accepts it.`,
tok, err := getSessionToken(sessionTokenPath) tok, err := getSessionToken(sessionTokenPath)
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
key, err := getKey() key := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "", err)
var idOwner *user.ID var idOwner *user.ID

View file

@ -6,6 +6,7 @@ import (
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address" addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -18,7 +19,7 @@ var dropObjectsCmd = &cobra.Command{
Short: "Drop objects from the node's local storage", Short: "Drop objects from the node's local storage",
Long: "Drop objects from the node's local storage", Long: "Drop objects from the node's local storage",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
pk := getKey(cmd) pk := key.Get(cmd)
dropObjectsList, _ := cmd.Flags().GetStringSlice(dropObjectsFlag) dropObjectsList, _ := cmd.Flags().GetStringSlice(dropObjectsFlag)
binAddrList := make([][]byte, 0, len(dropObjectsList)) binAddrList := make([][]byte, 0, len(dropObjectsList))

View file

@ -6,6 +6,7 @@ import (
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir" ircontrol "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
ircontrolsrv "github.com/nspcc-dev/neofs-node/pkg/services/control/ir/server" ircontrolsrv "github.com/nspcc-dev/neofs-node/pkg/services/control/ir/server"
@ -34,7 +35,7 @@ func initControlHealthCheckCmd() {
} }
func healthCheck(cmd *cobra.Command, _ []string) { func healthCheck(cmd *cobra.Command, _ []string) {
pk := getKey(cmd) pk := key.Get(cmd)
cli := getClient(cmd, pk) cli := getClient(cmd, pk)

View file

@ -6,6 +6,7 @@ import (
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -43,7 +44,7 @@ func initControlSetNetmapStatusCmd() {
} }
func setNetmapStatus(cmd *cobra.Command, _ []string) { func setNetmapStatus(cmd *cobra.Command, _ []string) {
pk := getKey(cmd) pk := key.Get(cmd)
var status control.NetmapStatus var status control.NetmapStatus

View file

@ -4,6 +4,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -21,7 +22,7 @@ var dumpShardCmd = &cobra.Command{
} }
func dumpShard(cmd *cobra.Command, _ []string) { func dumpShard(cmd *cobra.Command, _ []string) {
pk := getKey(cmd) pk := key.Get(cmd)
body := new(control.DumpShardRequest_Body) body := new(control.DumpShardRequest_Body)
body.SetShardID(getShardID(cmd)) body.SetShardID(getShardID(cmd))

View file

@ -7,6 +7,7 @@ import (
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -27,7 +28,7 @@ func initControlShardsListCmd() {
} }
func listShards(cmd *cobra.Command, _ []string) { func listShards(cmd *cobra.Command, _ []string) {
pk := getKey(cmd) pk := key.Get(cmd)
req := new(control.ListShardsRequest) req := new(control.ListShardsRequest)
req.SetBody(new(control.ListShardsRequest_Body)) req.SetBody(new(control.ListShardsRequest_Body))

View file

@ -4,6 +4,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client" "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -21,7 +22,7 @@ var restoreShardCmd = &cobra.Command{
} }
func restoreShard(cmd *cobra.Command, _ []string) { func restoreShard(cmd *cobra.Command, _ []string) {
pk := getKey(cmd) pk := key.Get(cmd)
body := new(control.RestoreShardRequest_Body) body := new(control.RestoreShardRequest_Body)
body.SetShardID(getShardID(cmd)) body.SetShardID(getShardID(cmd))

View file

@ -7,6 +7,7 @@ import (
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -46,7 +47,7 @@ func initControlSetShardModeCmd() {
} }
func setShardMode(cmd *cobra.Command, _ []string) { func setShardMode(cmd *cobra.Command, _ []string) {
pk := getKey(cmd) pk := key.Get(cmd)
var mode control.ShardMode var mode control.ShardMode

View file

@ -5,6 +5,7 @@ import (
rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client" rawclient "github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/control" "github.com/nspcc-dev/neofs-node/pkg/services/control"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -18,7 +19,7 @@ var snapshotCmd = &cobra.Command{
Short: "Get network map snapshot", Short: "Get network map snapshot",
Long: "Get network map snapshot", Long: "Get network map snapshot",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
pk := getKey(cmd) pk := key.Get(cmd)
req := new(control.NetmapSnapshotRequest) req := new(control.NetmapSnapshotRequest)
req.SetBody(new(control.NetmapSnapshotRequest_Body)) req.SetBody(new(control.NetmapSnapshotRequest_Body))

View file

@ -7,7 +7,6 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server" controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
"github.com/nspcc-dev/neofs-sdk-go/client" "github.com/nspcc-dev/neofs-sdk-go/client"
neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto" neofscrypto "github.com/nspcc-dev/neofs-sdk-go/crypto"
@ -54,9 +53,3 @@ func getClient(cmd *cobra.Command, pk *ecdsa.PrivateKey) *client.Client {
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
return cli return cli
} }
func getKey(cmd *cobra.Command) *ecdsa.PrivateKey {
pk, err := key.Get()
common.ExitOnErr(cmd, "", err)
return pk
}

View file

@ -6,6 +6,7 @@ import (
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object" objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
@ -34,8 +35,7 @@ var cmdObjectLock = &cobra.Command{
common.ExitOnErr(cmd, fmt.Sprintf("Incorrect object arg #%d: %%v", i+1), err) common.ExitOnErr(cmd, fmt.Sprintf("Incorrect object arg #%d: %%v", i+1), err)
} }
key, err := getKey() key := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "can't fetch private key: %w", err)
idOwner, err := getOwnerID(key) idOwner, err := getOwnerID(key)
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)

View file

@ -20,6 +20,7 @@ import (
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session" sessionCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/session"
"github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/bearer"
"github.com/nspcc-dev/neofs-sdk-go/checksum" "github.com/nspcc-dev/neofs-sdk-go/checksum"
@ -318,10 +319,9 @@ type clientKeySession interface {
} }
func prepareSessionPrm(cmd *cobra.Command, addr *addressSDK.Address, prms ...clientKeySession) { func prepareSessionPrm(cmd *cobra.Command, addr *addressSDK.Address, prms ...clientKeySession) {
key, err := getKey() pk := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "get private key: %w", err)
prepareSessionPrmWithKey(cmd, addr, key, prms...) prepareSessionPrmWithKey(cmd, addr, pk, prms...)
} }
func prepareSessionPrmWithKey(cmd *cobra.Command, addr *addressSDK.Address, key *ecdsa.PrivateKey, prms ...clientKeySession) { func prepareSessionPrmWithKey(cmd *cobra.Command, addr *addressSDK.Address, key *ecdsa.PrivateKey, prms ...clientKeySession) {
@ -421,10 +421,9 @@ func prepareObjectPrmRaw(cmd *cobra.Command, prm interface {
} }
func putObject(cmd *cobra.Command, _ []string) { func putObject(cmd *cobra.Command, _ []string) {
key, err := getKey() pk := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "can't fetch private key: %w", err)
ownerID, err := getOwnerID(key) ownerID, err := getOwnerID(pk)
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
@ -475,7 +474,7 @@ func putObject(cmd *cobra.Command, _ []string) {
sessionObjectCtxAddress := addressSDK.NewAddress() sessionObjectCtxAddress := addressSDK.NewAddress()
sessionObjectCtxAddress.SetContainerID(*cnr) sessionObjectCtxAddress.SetContainerID(*cnr)
prepareSessionPrmWithOwner(cmd, sessionObjectCtxAddress, key, ownerID, &prm) prepareSessionPrmWithOwner(cmd, sessionObjectCtxAddress, pk, ownerID, &prm)
prepareObjectPrm(cmd, &prm) prepareObjectPrm(cmd, &prm)
prm.SetHeader(obj) prm.SetHeader(obj)

View file

@ -139,19 +139,13 @@ func initConfig() {
} }
} }
// getKey returns private key that was provided in global arguments.
func getKey() (*ecdsa.PrivateKey, error) {
return key.GetOrGenerate()
}
type clientWithKey interface { type clientWithKey interface {
SetClient(*client.Client) SetClient(*client.Client)
} }
// reads private key from command args and call prepareAPIClientWithKey with it. // reads private key from command args and call prepareAPIClientWithKey with it.
func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) { func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
p, err := getKey() p := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "get private key: %w", err)
prepareAPIClientWithKey(cmd, p, dst...) prepareAPIClientWithKey(cmd, p, dst...)
} }

View file

@ -48,10 +48,7 @@ func init() {
} }
func createSession(cmd *cobra.Command, _ []string) error { func createSession(cmd *cobra.Command, _ []string) error {
privKey, err := key.Get() privKey := key.Get(cmd)
if err != nil {
return err
}
var netAddr network.Address var netAddr network.Address
addrStr, _ := cmd.Flags().GetString(commonflags.RPC) addrStr, _ := cmd.Flags().GetString(commonflags.RPC)

View file

@ -9,6 +9,7 @@ import (
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client" internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup" "github.com/nspcc-dev/neofs-node/pkg/services/object_manager/storagegroup"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address" addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
@ -163,10 +164,9 @@ func (c sgHeadReceiver) Head(addr *addressSDK.Address) (interface{}, error) {
} }
func putSG(cmd *cobra.Command, _ []string) { func putSG(cmd *cobra.Command, _ []string) {
key, err := getKey() pk := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "", err)
ownerID, err := getOwnerID(key) ownerID, err := getOwnerID(pk)
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
cnr, err := getCID(cmd) cnr, err := getCID(cmd)
@ -186,14 +186,14 @@ func putSG(cmd *cobra.Command, _ []string) {
sessionObjectCtxAddress := addressSDK.NewAddress() sessionObjectCtxAddress := addressSDK.NewAddress()
sessionObjectCtxAddress.SetContainerID(*cnr) sessionObjectCtxAddress.SetContainerID(*cnr)
prepareSessionPrmWithOwner(cmd, sessionObjectCtxAddress, key, ownerID, &putPrm) prepareSessionPrmWithOwner(cmd, sessionObjectCtxAddress, pk, ownerID, &putPrm)
prepareObjectPrm(cmd, &headPrm, &putPrm) prepareObjectPrm(cmd, &headPrm, &putPrm)
headPrm.SetRawFlag(true) headPrm.SetRawFlag(true)
sg, err := storagegroup.CollectMembers(sgHeadReceiver{ sg, err := storagegroup.CollectMembers(sgHeadReceiver{
cmd: cmd, cmd: cmd,
key: key, key: pk,
ownerID: ownerID, ownerID: ownerID,
prm: headPrm, prm: headPrm,
}, cnr, members) }, cnr, members)

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags" "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
"github.com/nspcc-dev/neofs-node/pkg/util/keyer" "github.com/nspcc-dev/neofs-node/pkg/util/keyer"
locodedb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db" locodedb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db"
airportsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/airports" airportsdb "github.com/nspcc-dev/neofs-node/pkg/util/locode/db/airports"
@ -283,10 +284,9 @@ func signBearerToken(cmd *cobra.Command, _ []string) {
btok, err := getBearerToken(cmd, "from") btok, err := getBearerToken(cmd, "from")
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
key, err := getKey() pk := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "", err)
err = btok.Sign(*key) err = btok.Sign(*pk)
common.ExitOnErr(cmd, "", err) common.ExitOnErr(cmd, "", err)
to := cmd.Flag("to").Value.String() to := cmd.Flag("to").Value.String()
@ -321,10 +321,9 @@ func signSessionToken(cmd *cobra.Command, _ []string) {
common.ExitOnErr(cmd, "", fmt.Errorf("can't read session token from %s: %w", path, err)) common.ExitOnErr(cmd, "", fmt.Errorf("can't read session token from %s: %w", path, err))
} }
key, err := getKey() pk := key.GetOrGenerate(cmd)
common.ExitOnErr(cmd, "can't get private key, make sure it is provided: %w", err)
err = stok.Sign(key) err = stok.Sign(pk)
common.ExitOnErr(cmd, "can't sign token: %w", err) common.ExitOnErr(cmd, "can't sign token: %w", err)
data, err := stok.MarshalJSON() data, err := stok.MarshalJSON()