From 1c7195666c0d3cc68e508cc472f756d10852fb60 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 8 Apr 2022 17:58:21 +0300 Subject: [PATCH] [#722] neofs-adm: Unify pasword retrieval functions Signed-off-by: Evgenii Stratonikov --- .../internal/modules/config/config.go | 7 +++--- .../internal/modules/morph/generate.go | 9 ++++---- cmd/neofs-adm/internal/modules/morph/group.go | 23 ++++++------------- .../internal/modules/morph/initialize.go | 11 +++++---- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/config/config.go b/cmd/neofs-adm/internal/modules/config/config.go index 53b186024..1a9ba65c9 100644 --- a/cmd/neofs-adm/internal/modules/config/config.go +++ b/cmd/neofs-adm/internal/modules/config/config.go @@ -144,13 +144,12 @@ func generateConfigExample(appDir string, credSize int) (string, error) { return buf.String(), nil } -func AlphabetPassword(v *viper.Viper, index int) (string, error) { - letter := innerring.GlagoliticLetter(index) - key := "credentials." + letter.String() +func GetPassword(v *viper.Viper, name string) (string, error) { + key := "credentials." + name if v.IsSet(key) { return v.GetString(key), nil } - prompt := "Password for " + letter.String() + " wallet > " + prompt := "Password for " + name + " wallet > " return input.ReadPassword(prompt) } diff --git a/cmd/neofs-adm/internal/modules/morph/generate.go b/cmd/neofs-adm/internal/modules/morph/generate.go index 7c7bd758c..7b2b5ec60 100644 --- a/cmd/neofs-adm/internal/modules/morph/generate.go +++ b/cmd/neofs-adm/internal/modules/morph/generate.go @@ -38,13 +38,14 @@ func generateAlphabetCreds(cmd *cobra.Command, args []string) error { return errors.New("size must be > 0") } + v := viper.GetViper() walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag)) - pwds, err := initializeWallets(walletDir, int(size)) + pwds, err := initializeWallets(v, walletDir, int(size)) if err != nil { return err } - w, err := initializeContractWallet(walletDir) + w, err := initializeContractWallet(v, walletDir) if err != nil { return err } @@ -59,13 +60,13 @@ func generateAlphabetCreds(cmd *cobra.Command, args []string) error { return nil } -func initializeWallets(walletDir string, size int) ([]string, error) { +func initializeWallets(v *viper.Viper, walletDir string, size int) ([]string, error) { wallets := make([]*wallet.Wallet, size) pubs := make(keys.PublicKeys, size) passwords := make([]string, size) for i := range wallets { - password, err := config.AlphabetPassword(viper.GetViper(), i) + password, err := config.GetPassword(v, innerring.GlagoliticLetter(i).String()) if err != nil { return nil, fmt.Errorf("can't fetch password: %w", err) } diff --git a/cmd/neofs-adm/internal/modules/morph/group.go b/cmd/neofs-adm/internal/modules/morph/group.go index 3f48eb3dc..99ddd38f4 100644 --- a/cmd/neofs-adm/internal/modules/morph/group.go +++ b/cmd/neofs-adm/internal/modules/morph/group.go @@ -6,22 +6,22 @@ import ( "os" "path/filepath" - "github.com/nspcc-dev/neo-go/cli/input" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/wallet" + "github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/config" "github.com/spf13/cobra" "github.com/spf13/viper" ) const ( contractWalletFilename = "contract.json" - contractWalletPasswordKey = "credentials.contract" + contractWalletPasswordKey = "contract" ) -func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { - password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") +func initializeContractWallet(v *viper.Viper, walletDir string) (*wallet.Wallet, error) { + password, err := config.GetPassword(v, contractWalletPasswordKey) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func initializeContractWallet(walletDir string) (*wallet.Wallet, error) { return w, nil } -func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) { +func openContractWallet(v *viper.Viper, cmd *cobra.Command, walletDir string) (*wallet.Wallet, error) { p := filepath.Join(walletDir, contractWalletFilename) w, err := wallet.NewWalletFromFile(p) if err != nil { @@ -58,10 +58,10 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e } cmd.Printf("Contract group wallet is missing, initialize at %s\n", p) - return initializeContractWallet(walletDir) + return initializeContractWallet(v, walletDir) } - password, err := getPassword(contractWalletPasswordKey, "Password for contract wallet > ") + password, err := config.GetPassword(v, contractWalletPasswordKey) if err != nil { return nil, err } @@ -75,15 +75,6 @@ func openContractWallet(cmd *cobra.Command, walletDir string) (*wallet.Wallet, e return w, nil } -func getPassword(key string, promps string) (string, error) { - if viper.IsSet(key) { - return viper.GetString(key), nil - } - - prompt := "Password for contract wallet > " - return input.ReadPassword(prompt) -} - func (c *initializeContext) addManifestGroup(h util.Uint160, cs *contractState) error { priv := c.ContractWallet.Accounts[0].PrivateKey() pub := priv.PublicKey() diff --git a/cmd/neofs-adm/internal/modules/morph/initialize.go b/cmd/neofs-adm/internal/modules/morph/initialize.go index 6fed2df98..a325640a1 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize.go @@ -111,12 +111,12 @@ func (c *initializeContext) close() { func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) { walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag)) - wallets, err := openAlphabetWallets(walletDir) + wallets, err := openAlphabetWallets(v, walletDir) if err != nil { return nil, err } - w, err := openContractWallet(cmd, walletDir) + w, err := openContractWallet(v, cmd, walletDir) if err != nil { return nil, err } @@ -207,7 +207,7 @@ func (c *initializeContext) nativeHash(name string) util.Uint160 { return c.Natives[name] } -func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) { +func openAlphabetWallets(v *viper.Viper, walletDir string) ([]*wallet.Wallet, error) { walletFiles, err := os.ReadDir(walletDir) if err != nil { return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err) @@ -231,13 +231,14 @@ loop: wallets := make([]*wallet.Wallet, size) for i := 0; i < size; i++ { - p := filepath.Join(walletDir, innerring.GlagoliticLetter(i).String()+".json") + letter := innerring.GlagoliticLetter(i).String() + p := filepath.Join(walletDir, letter+".json") w, err := wallet.NewWalletFromFile(p) if err != nil { return nil, fmt.Errorf("can't open wallet: %w", err) } - password, err := config.AlphabetPassword(viper.GetViper(), i) + password, err := config.GetPassword(v, letter) if err != nil { return nil, fmt.Errorf("can't fetch password: %w", err) }