2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-12 13:57:37 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-26 16:30:04 +00:00
|
|
|
"crypto/tls"
|
2020-10-12 13:57:37 +00:00
|
|
|
"errors"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-12-24 11:25:36 +00:00
|
|
|
"strings"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2021-04-21 12:27:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2020-12-24 11:25:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
2020-10-12 14:19:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-10-12 14:31:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-10-12 13:59:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2020-10-12 15:14:17 +00:00
|
|
|
const (
|
|
|
|
envPrefix = "NEOFS_CLI"
|
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
ttlDefaultValue = 2
|
2020-10-12 15:14:17 +00:00
|
|
|
)
|
2020-10-12 14:35:04 +00:00
|
|
|
|
2020-12-24 11:25:36 +00:00
|
|
|
const xHeadersFlag = "xhdr"
|
|
|
|
|
|
|
|
var xHeaders []string
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
// Global scope flags.
|
|
|
|
var (
|
2020-10-12 15:14:17 +00:00
|
|
|
cfgFile string
|
2020-10-12 14:00:30 +00:00
|
|
|
verbose bool
|
2020-10-12 13:57:37 +00:00
|
|
|
)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "neofs-cli",
|
|
|
|
Short: "Command Line Tool to work with NeoFS",
|
|
|
|
Long: `NeoFS CLI provides all basic interactions with NeoFS and it's services.
|
|
|
|
|
|
|
|
It contains commands for interaction with NeoFS nodes using different versions
|
|
|
|
of neofs-api and some useful utilities for compiling ACL rules from JSON
|
|
|
|
notation, managing container access through protocol gates, querying network map
|
|
|
|
and much more!`,
|
|
|
|
}
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
var (
|
2020-10-12 13:59:33 +00:00
|
|
|
errInvalidKey = errors.New("provided key is incorrect")
|
|
|
|
errInvalidEndpoint = errors.New("provided RPC endpoint is incorrect")
|
2020-10-12 14:35:04 +00:00
|
|
|
errCantGenerateKey = errors.New("can't generate new private key")
|
2021-04-21 12:27:32 +00:00
|
|
|
errInvalidAddress = errors.New("--address option must be specified and valid")
|
|
|
|
errInvalidPassword = errors.New("invalid password for the encrypted key")
|
2020-10-12 13:57:37 +00:00
|
|
|
)
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports persistent flags, which, if defined here,
|
|
|
|
// will be global for your application.
|
|
|
|
|
2020-11-05 13:49:05 +00:00
|
|
|
// use stdout as default output for cmd.Print()
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)")
|
2020-10-12 15:14:17 +00:00
|
|
|
|
2021-06-15 15:43:09 +00:00
|
|
|
// Key options.
|
|
|
|
rootCmd.PersistentFlags().BoolP("generate-key", "", false, "generate new private key")
|
|
|
|
_ = viper.BindPFlag("generate-key", rootCmd.PersistentFlags().Lookup("generate-key"))
|
|
|
|
|
|
|
|
rootCmd.PersistentFlags().StringP("key", "k", "", "private key in hex, WIF, NEP-2 or filepath")
|
2020-10-12 15:14:17 +00:00
|
|
|
_ = viper.BindPFlag("key", rootCmd.PersistentFlags().Lookup("key"))
|
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
rootCmd.PersistentFlags().StringP("address", "", "", "address of wallet account")
|
|
|
|
_ = viper.BindPFlag("address", rootCmd.PersistentFlags().Lookup("address"))
|
|
|
|
|
2020-10-12 15:14:17 +00:00
|
|
|
rootCmd.PersistentFlags().StringP("rpc-endpoint", "r", "", "remote node address (as 'multiaddr' or '<host>:<port>')")
|
|
|
|
_ = viper.BindPFlag("rpc", rootCmd.PersistentFlags().Lookup("rpc-endpoint"))
|
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
rootCmd.PersistentFlags().Uint32("ttl", ttlDefaultValue, "TTL value in request meta header")
|
|
|
|
_ = viper.BindPFlag("ttl", rootCmd.PersistentFlags().Lookup("ttl"))
|
|
|
|
|
2020-10-12 14:00:30 +00:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-12-24 11:25:36 +00:00
|
|
|
rootCmd.PersistentFlags().StringSliceVarP(&xHeaders, xHeadersFlag, "x", nil,
|
|
|
|
"Request X-Headers in form of Key=Value")
|
|
|
|
_ = viper.BindPFlag(xHeadersFlag, rootCmd.PersistentFlags().Lookup(xHeadersFlag))
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
|
|
|
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
// Find home directory.
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search config in home directory with name ".main" (without extension).
|
|
|
|
viper.AddConfigPath(home)
|
|
|
|
viper.SetConfigName(".config/neofs-cli")
|
|
|
|
}
|
|
|
|
|
2020-10-12 15:14:17 +00:00
|
|
|
viper.SetEnvPrefix(envPrefix)
|
2020-08-04 14:46:12 +00:00
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2020-10-14 12:36:27 +00:00
|
|
|
printVerbose("Using config file: %s", viper.ConfigFileUsed())
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 13:57:37 +00:00
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
const nep2Base58Length = 58
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
// getKey returns private key that was provided in global arguments.
|
|
|
|
func getKey() (*ecdsa.PrivateKey, error) {
|
2021-06-15 15:43:09 +00:00
|
|
|
if viper.GetBool("generate-key") {
|
2021-05-31 08:55:38 +00:00
|
|
|
priv, err := keys.NewPrivateKey()
|
2020-10-12 14:35:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errCantGenerateKey
|
|
|
|
}
|
2021-05-31 08:55:38 +00:00
|
|
|
return &priv.PrivateKey, nil
|
2021-04-21 12:27:32 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 15:43:09 +00:00
|
|
|
privateKey := viper.GetString("key")
|
2021-04-21 12:27:32 +00:00
|
|
|
w, err := wallet.NewWalletFromFile(privateKey)
|
|
|
|
if err == nil {
|
|
|
|
return getKeyFromWallet(w, viper.GetString("address"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(privateKey) == nep2Base58Length {
|
|
|
|
return getKeyFromNEP2(privateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errInvalidKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKeyFromNEP2(encryptedWif string) (*ecdsa.PrivateKey, error) {
|
|
|
|
pass, err := input.ReadPassword("Enter password > ")
|
|
|
|
if err != nil {
|
|
|
|
printVerbose("Can't read password: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
k, err := keys.NEP2Decrypt(encryptedWif, pass)
|
2020-10-12 13:57:37 +00:00
|
|
|
if err != nil {
|
2021-04-21 12:27:32 +00:00
|
|
|
printVerbose("Invalid key or password: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
2020-10-12 13:57:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
return &k.PrivateKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKeyFromWallet(w *wallet.Wallet, addrStr string) (*ecdsa.PrivateKey, error) {
|
|
|
|
if addrStr == "" {
|
|
|
|
printVerbose("Address is empty")
|
|
|
|
return nil, errInvalidAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := flags.ParseAddress(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
printVerbose("Can't parse address: %s", addrStr)
|
|
|
|
return nil, errInvalidAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := w.GetAccount(addr)
|
|
|
|
if acc == nil {
|
|
|
|
printVerbose("Can't find wallet account for %s", addrStr)
|
|
|
|
return nil, errInvalidAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
pass, err := input.ReadPassword("Enter password > ")
|
|
|
|
if err != nil {
|
|
|
|
printVerbose("Can't read password: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := acc.Decrypt(pass); err != nil {
|
|
|
|
printVerbose("Can't decrypt account: %v", err)
|
|
|
|
return nil, errInvalidPassword
|
|
|
|
}
|
|
|
|
|
|
|
|
return &acc.PrivateKey().PrivateKey, nil
|
2020-10-12 13:57:37 +00:00
|
|
|
}
|
2020-10-12 13:59:33 +00:00
|
|
|
|
|
|
|
// getEndpointAddress returns network address structure that stores multiaddr
|
|
|
|
// inside, parsed from global arguments.
|
|
|
|
func getEndpointAddress() (*network.Address, error) {
|
2020-10-12 15:14:17 +00:00
|
|
|
endpoint := viper.GetString("rpc")
|
|
|
|
|
2020-10-12 13:59:33 +00:00
|
|
|
addr, err := network.AddressFromString(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errInvalidEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
2020-10-12 14:19:57 +00:00
|
|
|
|
|
|
|
// getSDKClient returns default neofs-api-go sdk client. Consider using
|
|
|
|
// opts... to provide TTL or other global configuration flags.
|
2021-04-21 12:27:32 +00:00
|
|
|
func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) {
|
2020-10-12 14:19:57 +00:00
|
|
|
netAddr, err := getEndpointAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:04:32 +00:00
|
|
|
hostAddr, err := netAddr.HostAddrString()
|
2020-10-12 14:19:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errInvalidEndpoint
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:30:04 +00:00
|
|
|
options := []client.Option{
|
|
|
|
client.WithAddress(hostAddr),
|
|
|
|
client.WithDefaultPrivateKey(key),
|
|
|
|
}
|
|
|
|
|
|
|
|
if netAddr.TLSEnabled() {
|
|
|
|
options = append(options, client.WithTLSConfig(&tls.Config{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := client.New(options...)
|
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
return c, err
|
2020-10-12 14:19:57 +00:00
|
|
|
}
|
2020-10-12 14:31:00 +00:00
|
|
|
|
2020-11-16 08:25:42 +00:00
|
|
|
func getTTL() uint32 {
|
|
|
|
ttl := viper.GetUint32("ttl")
|
|
|
|
printVerbose("TTL: %d", ttl)
|
|
|
|
|
|
|
|
return ttl
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:31:00 +00:00
|
|
|
// ownerFromString converts string with NEO3 wallet address to neofs owner ID.
|
|
|
|
func ownerFromString(s string) (*owner.ID, error) {
|
2020-10-15 08:45:00 +00:00
|
|
|
result := owner.NewID()
|
2020-10-12 14:31:00 +00:00
|
|
|
|
2020-10-15 08:45:00 +00:00
|
|
|
err := result.Parse(s)
|
|
|
|
if err != nil {
|
2020-10-12 14:31:00 +00:00
|
|
|
return nil, errors.New("can't decode owner ID wallet address")
|
|
|
|
}
|
|
|
|
|
2020-10-15 08:45:00 +00:00
|
|
|
return result, nil
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
2020-10-14 12:36:27 +00:00
|
|
|
|
|
|
|
func printVerbose(format string, a ...interface{}) {
|
|
|
|
if verbose {
|
|
|
|
fmt.Printf(format+"\n", a...)
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 11:25:36 +00:00
|
|
|
|
|
|
|
func parseXHeaders() []*pkg.XHeader {
|
|
|
|
xs := make([]*pkg.XHeader, 0, len(xHeaders))
|
|
|
|
|
|
|
|
for i := range xHeaders {
|
|
|
|
kv := strings.SplitN(xHeaders[i], "=", 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
panic(fmt.Errorf("invalid X-Header format: %s", xHeaders[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
x := pkg.NewXHeader()
|
|
|
|
x.SetKey(kv[0])
|
|
|
|
x.SetValue(kv[1])
|
|
|
|
|
|
|
|
xs = append(xs, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs
|
|
|
|
}
|
|
|
|
|
|
|
|
func globalCallOptions() []client.CallOption {
|
|
|
|
xHdrs := parseXHeaders()
|
|
|
|
|
|
|
|
opts := make([]client.CallOption, 0, len(xHdrs)+1) // + TTL
|
|
|
|
opts = append(opts, client.WithTTL(getTTL()))
|
|
|
|
|
|
|
|
for i := range xHdrs {
|
|
|
|
opts = append(opts, client.WithXHeader(xHdrs[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|