From 663a96eba0a929378832f3a2051fc267e7fc8888 Mon Sep 17 00:00:00 2001 From: Viacheslav Gonkivskyi Date: Tue, 26 Sep 2023 11:54:00 +0300 Subject: [PATCH] feat(cmd/rpc): add store flag to rpc cmds --- cmd/celestia/rpc.go | 22 ++++------- cmd/flags_node.go | 2 +- cmd/rpc.go | 91 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 91 insertions(+), 24 deletions(-) diff --git a/cmd/celestia/rpc.go b/cmd/celestia/rpc.go index 8cf51fda09..11e96c2e46 100644 --- a/cmd/celestia/rpc.go +++ b/cmd/celestia/rpc.go @@ -12,21 +12,13 @@ import ( ) func init() { - blob.Cmd.PersistentFlags().StringVar(cmd.InitURLFlag()) - das.Cmd.PersistentFlags().StringVar(cmd.InitURLFlag()) - header.Cmd.PersistentFlags().StringVar(cmd.InitURLFlag()) - p2p.Cmd.PersistentFlags().StringVar(cmd.InitURLFlag()) - share.Cmd.PersistentFlags().StringVar(cmd.InitURLFlag()) - state.Cmd.PersistentFlags().StringVar(cmd.InitURLFlag()) - node.Cmd.PersistentFlags().StringVar(cmd.InitURLFlag()) - - blob.Cmd.PersistentFlags().StringVar(cmd.InitAuthTokenFlag()) - das.Cmd.PersistentFlags().StringVar(cmd.InitAuthTokenFlag()) - header.Cmd.PersistentFlags().StringVar(cmd.InitAuthTokenFlag()) - p2p.Cmd.PersistentFlags().StringVar(cmd.InitAuthTokenFlag()) - share.Cmd.PersistentFlags().StringVar(cmd.InitAuthTokenFlag()) - state.Cmd.PersistentFlags().StringVar(cmd.InitAuthTokenFlag()) - node.Cmd.PersistentFlags().StringVar(cmd.InitAuthTokenFlag()) + blob.Cmd.PersistentFlags().AddFlagSet(cmd.RPCFlags()) + das.Cmd.PersistentFlags().AddFlagSet(cmd.RPCFlags()) + header.Cmd.PersistentFlags().AddFlagSet(cmd.RPCFlags()) + p2p.Cmd.PersistentFlags().AddFlagSet(cmd.RPCFlags()) + share.Cmd.PersistentFlags().AddFlagSet(cmd.RPCFlags()) + state.Cmd.PersistentFlags().AddFlagSet(cmd.RPCFlags()) + node.Cmd.PersistentFlags().AddFlagSet(cmd.RPCFlags()) rootCmd.AddCommand( blob.Cmd, diff --git a/cmd/flags_node.go b/cmd/flags_node.go index 8c73a06169..fe4981b6c6 100644 --- a/cmd/flags_node.go +++ b/cmd/flags_node.go @@ -15,7 +15,7 @@ import ( "github.com/celestiaorg/celestia-node/nodebuilder/p2p" ) -var ( +const ( nodeStoreFlag = "node.store" nodeConfigFlag = "node.config" ) diff --git a/cmd/rpc.go b/cmd/rpc.go index 513967f0ed..6b63d9e695 100644 --- a/cmd/rpc.go +++ b/cmd/rpc.go @@ -3,11 +3,20 @@ package cmd import ( "context" "errors" + "fmt" "os" + "path/filepath" + "github.com/cristalhq/jwt" + "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" + flag "github.com/spf13/pflag" rpc "github.com/celestiaorg/celestia-node/api/rpc/client" + "github.com/celestiaorg/celestia-node/api/rpc/perms" + "github.com/celestiaorg/celestia-node/libs/authtoken" + "github.com/celestiaorg/celestia-node/libs/keystore" + nodemod "github.com/celestiaorg/celestia-node/nodebuilder/node" ) const ( @@ -19,25 +28,52 @@ const ( var ( requestURL string authTokenFlag string + storePath string ) -func InitURLFlag() (*string, string, string, string) { - return &requestURL, "url", defaultRPCAddress, "Request URL" -} +func RPCFlags() *flag.FlagSet { + fset := &flag.FlagSet{} + + fset.StringVar( + &requestURL, + "url", + defaultRPCAddress, + "Request URL", + ) -func InitAuthTokenFlag() (*string, string, string, string) { - return &authTokenFlag, + fset.StringVar( + &authTokenFlag, "token", "", - "Authorization token (if not provided, the " + authEnvKey + " environment variable will be used)" + "Authorization token (if not provided, the "+authEnvKey+" environment variable will be used)", + ) + + storeFlag := NodeFlags().Lookup(nodeStoreFlag) + + fset.StringVar( + &storePath, + nodeStoreFlag, + "", + storeFlag.Usage, + ) + return fset } func InitClient(cmd *cobra.Command, _ []string) error { + var ( + err error + token string + ) + if authTokenFlag == "" { - authTokenFlag = os.Getenv(authEnvKey) + token, err = getToken(storePath) + } + + if err != nil { + token = os.Getenv(authEnvKey) } - client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag) + client, err := rpc.NewClient(cmd.Context(), requestURL, token) if err != nil { return err } @@ -47,6 +83,45 @@ func InitClient(cmd *cobra.Command, _ []string) error { return nil } +func getToken(path string) (string, error) { + if path == "" { + return "", errors.New("empty path") + } + + expanded, err := homedir.Expand(filepath.Clean(path)) + if err != nil { + fmt.Printf("error expanding the root dir path:%v", err) + return "", err + } + + ks, err := keystore.NewFSKeystore(filepath.Join(expanded, "keys"), nil) + if err != nil { + fmt.Printf("error creating the keystore:%v", err) + return "", err + } + + var key keystore.PrivKey + key, err = ks.Get(nodemod.SecretName) + if err != nil { + fmt.Printf("error getting the private key:%v", err) + return "", err + } + + signer, err := jwt.NewHS256(key.Body) + if err != nil { + fmt.Printf("error creating a signer:%v", err) + return "", err + } + + token, err := authtoken.NewSignedJWT(signer, perms.AllPerms) + if err != nil { + fmt.Println(err) + return "", err + } + authTokenFlag = token + return token, nil +} + type rpcClientKey struct{} func ParseClientFromCtx(ctx context.Context) (*rpc.Client, error) {