2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-12 13:57:37 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-06-24 12:31:50 +00:00
|
|
|
"path/filepath"
|
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/pkg/crypto/keys"
|
2022-03-28 09:36:04 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2022-03-28 10:23:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-01-20 15:34:53 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/acl"
|
2022-03-28 09:09:49 +00:00
|
|
|
bearerCli "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/modules/bearer"
|
2021-06-20 16:11:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/misc"
|
2020-10-12 13:59:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/token"
|
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-10-12 14:35:04 +00:00
|
|
|
|
2020-12-24 11:25:36 +00:00
|
|
|
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 13:57:37 +00:00
|
|
|
)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
const (
|
|
|
|
// Common CLI flag keys, shorthands, default
|
|
|
|
// values and their usage descriptions.
|
|
|
|
generateKey = "generate-key"
|
2021-12-10 12:30:03 +00:00
|
|
|
generateKeyShorthand = "g"
|
2021-09-27 15:36:14 +00:00
|
|
|
generateKeyDefault = false
|
|
|
|
generateKeyUsage = "generate new private key"
|
|
|
|
|
|
|
|
walletPath = "wallet"
|
|
|
|
walletPathShorthand = "w"
|
|
|
|
walletPathDefault = ""
|
2022-01-19 10:56:10 +00:00
|
|
|
walletPathUsage = "WIF (NEP-2) string or path to the wallet or binary key"
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
address = "address"
|
|
|
|
addressShorthand = ""
|
|
|
|
addressDefault = ""
|
|
|
|
addressUsage = "address of wallet account"
|
|
|
|
|
|
|
|
rpc = "rpc-endpoint"
|
|
|
|
rpcShorthand = "r"
|
|
|
|
rpcDefault = ""
|
|
|
|
rpcUsage = "remote node address (as 'multiaddr' or '<host>:<port>')"
|
|
|
|
|
|
|
|
verbose = "verbose"
|
|
|
|
verboseShorthand = "v"
|
|
|
|
verboseDefault = false
|
|
|
|
verboseUsage = "verbose output"
|
|
|
|
|
|
|
|
ttl = "ttl"
|
|
|
|
ttlShorthand = ""
|
|
|
|
ttlDefault = 2
|
|
|
|
ttlUsage = "TTL value in request meta header"
|
|
|
|
|
|
|
|
xHeadersKey = "xhdr"
|
|
|
|
xHeadersShorthand = "x"
|
|
|
|
xHeadersUsage = "Request X-Headers in form of Key=Value"
|
|
|
|
)
|
|
|
|
|
|
|
|
var xHeadersDefault []string
|
|
|
|
|
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!`,
|
2021-06-20 16:11:49 +00:00
|
|
|
Run: entryPoint,
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 13:57:37 +00:00
|
|
|
var (
|
2020-10-12 13:59:33 +00:00
|
|
|
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")
|
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() {
|
2021-07-06 12:27:54 +00:00
|
|
|
err := rootCmd.Execute()
|
|
|
|
exitOnErr(rootCmd, err)
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
2020-11-05 13:49:05 +00:00
|
|
|
// use stdout as default output for cmd.Print()
|
|
|
|
rootCmd.SetOut(os.Stdout)
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports persistent flags, which, if defined here,
|
|
|
|
// will be global for your application.
|
2020-10-12 13:57:37 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.config/neofs-cli/config.yaml)")
|
2022-01-28 18:03:44 +00:00
|
|
|
rootCmd.PersistentFlags().BoolP(verbose, verboseShorthand, verboseDefault, verboseUsage)
|
|
|
|
|
|
|
|
_ = viper.BindPFlag(verbose, rootCmd.PersistentFlags().Lookup(verbose))
|
2020-10-12 15:14:17 +00:00
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
2021-06-20 16:11:49 +00:00
|
|
|
rootCmd.Flags().Bool("version", false, "Application version and NeoFS API compatibility")
|
2022-01-20 15:34:53 +00:00
|
|
|
|
|
|
|
rootCmd.AddCommand(acl.Cmd)
|
2022-03-28 09:09:49 +00:00
|
|
|
rootCmd.AddCommand(bearerCli.Cmd)
|
2021-06-20 16:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func entryPoint(cmd *cobra.Command, _ []string) {
|
|
|
|
printVersion, _ := cmd.Flags().GetBool("version")
|
|
|
|
if printVersion {
|
|
|
|
cmd.Printf(
|
|
|
|
"Version: %s \nBuild: %s \nDebug: %s\n",
|
|
|
|
misc.Version,
|
|
|
|
misc.Build,
|
|
|
|
misc.Debug,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = cmd.Usage()
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(rootCmd, err)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-06-25 00:23:10 +00:00
|
|
|
// Search config in `$HOME/.config/neofs-cli/` with name "config.yaml"
|
2021-06-24 12:31:50 +00:00
|
|
|
viper.AddConfigPath(filepath.Join(home, ".config", "neofs-cli"))
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.SetConfigType("yaml")
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// getKey returns private key that was provided in global arguments.
|
|
|
|
func getKey() (*ecdsa.PrivateKey, error) {
|
2021-09-27 15:36:14 +00:00
|
|
|
if viper.GetBool(generateKey) {
|
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
|
|
|
}
|
2022-01-26 07:53:30 +00:00
|
|
|
return getKeyNoGenerate()
|
|
|
|
}
|
2021-04-21 12:27:32 +00:00
|
|
|
|
2022-01-26 07:53:30 +00:00
|
|
|
func getKeyNoGenerate() (*ecdsa.PrivateKey, error) {
|
2022-03-28 10:23:45 +00:00
|
|
|
return key.Get(viper.GetString(walletPath), viper.GetString(address))
|
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.
|
2021-10-15 11:24:02 +00:00
|
|
|
func getEndpointAddress(endpointFlag string) (addr network.Address, err error) {
|
|
|
|
endpoint := viper.GetString(endpointFlag)
|
2020-10-12 15:14:17 +00:00
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
err = addr.FromString(endpoint)
|
2020-10-12 13:59:33 +00:00
|
|
|
if err != nil {
|
2021-06-18 06:00:21 +00:00
|
|
|
err = errInvalidEndpoint
|
2020-10-12 13:59:33 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 06:00:21 +00:00
|
|
|
return
|
2020-10-12 13:59:33 +00:00
|
|
|
}
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
type clientWithKey interface {
|
2022-01-13 15:01:50 +00:00
|
|
|
SetClient(*client.Client)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// reads private key from command args and call prepareAPIClientWithKey with it.
|
|
|
|
func prepareAPIClient(cmd *cobra.Command, dst ...clientWithKey) {
|
2022-03-28 10:23:45 +00:00
|
|
|
p, err := getKey()
|
2021-10-28 14:48:46 +00:00
|
|
|
exitOnErr(cmd, errf("get private key: %w", err))
|
|
|
|
|
2022-03-28 10:23:45 +00:00
|
|
|
prepareAPIClientWithKey(cmd, p, dst...)
|
2021-10-28 14:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 := getSDKClient(key)
|
|
|
|
exitOnErr(cmd, errf("create API client: %w", err))
|
|
|
|
|
|
|
|
for _, d := range dst {
|
|
|
|
d.SetClient(cli)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bearerPrm interface {
|
|
|
|
SetBearerToken(prm *token.BearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareBearerPrm(cmd *cobra.Command, prm bearerPrm) {
|
|
|
|
btok, err := getBearerToken(cmd, bearerTokenFlag)
|
|
|
|
exitOnErr(cmd, errf("bearer token: %w", err))
|
|
|
|
|
|
|
|
prm.SetBearerToken(btok)
|
|
|
|
}
|
|
|
|
|
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.
|
2022-01-13 15:01:50 +00:00
|
|
|
func getSDKClient(key *ecdsa.PrivateKey) (*client.Client, error) {
|
2021-10-15 11:24:02 +00:00
|
|
|
netAddr, err := getEndpointAddress(rpc)
|
2020-10-12 14:19:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-28 09:36:04 +00:00
|
|
|
return internalclient.GetSDKClient(key, netAddr)
|
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 {
|
2021-09-27 15:36:14 +00:00
|
|
|
ttl := viper.GetUint32(ttl)
|
2020-11-16 08:25:42 +00:00
|
|
|
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{}) {
|
2021-09-27 15:36:14 +00:00
|
|
|
if viper.GetBool(verbose) {
|
2020-10-14 12:36:27 +00:00
|
|
|
fmt.Printf(format+"\n", a...)
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 11:25:36 +00:00
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
func parseXHeaders() []*session.XHeader {
|
|
|
|
xs := make([]*session.XHeader, 0, len(xHeaders))
|
2020-12-24 11:25:36 +00:00
|
|
|
|
|
|
|
for i := range xHeaders {
|
|
|
|
kv := strings.SplitN(xHeaders[i], "=", 2)
|
|
|
|
if len(kv) != 2 {
|
|
|
|
panic(fmt.Errorf("invalid X-Header format: %s", xHeaders[i]))
|
|
|
|
}
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
x := session.NewXHeader()
|
2020-12-24 11:25:36 +00:00
|
|
|
x.SetKey(kv[0])
|
|
|
|
x.SetValue(kv[1])
|
|
|
|
|
|
|
|
xs = append(xs, x)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
// add common flags to the command:
|
|
|
|
// - key;
|
|
|
|
// - wallet;
|
|
|
|
// - WIF;
|
|
|
|
// - address;
|
|
|
|
// - RPC;
|
|
|
|
func initCommonFlags(cmd *cobra.Command) {
|
|
|
|
ff := cmd.Flags()
|
|
|
|
|
|
|
|
ff.BoolP(generateKey, generateKeyShorthand, generateKeyDefault, generateKeyUsage)
|
|
|
|
ff.StringP(walletPath, walletPathShorthand, walletPathDefault, walletPathUsage)
|
|
|
|
ff.StringP(address, addressShorthand, addressDefault, addressUsage)
|
|
|
|
ff.StringP(rpc, rpcShorthand, rpcDefault, rpcUsage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind common command flags to the viper
|
|
|
|
func bindCommonFlags(cmd *cobra.Command) {
|
|
|
|
ff := cmd.Flags()
|
|
|
|
|
|
|
|
_ = viper.BindPFlag(generateKey, ff.Lookup(generateKey))
|
|
|
|
_ = viper.BindPFlag(walletPath, ff.Lookup(walletPath))
|
|
|
|
_ = viper.BindPFlag(address, ff.Lookup(address))
|
|
|
|
_ = viper.BindPFlag(rpc, ff.Lookup(rpc))
|
|
|
|
}
|
2021-10-07 10:37:57 +00:00
|
|
|
|
|
|
|
func bindAPIFlags(cmd *cobra.Command) {
|
|
|
|
ff := cmd.Flags()
|
|
|
|
|
|
|
|
_ = viper.BindPFlag(ttl, ff.Lookup(ttl))
|
|
|
|
_ = viper.BindPFlag(xHeadersKey, ff.Lookup(xHeadersKey))
|
|
|
|
}
|