Skip to content

Commit

Permalink
feat(cmd/rpc): add node store flag (celestiaorg#2762)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Oct 5, 2023
1 parent f6a1e9d commit f596e11
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 62 deletions.
82 changes: 42 additions & 40 deletions cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,59 +25,61 @@ func AuthCmd(fsets ...*flag.FlagSet) *cobra.Command {
Short: "Signs and outputs a hex-encoded JWT token with the given permissions.",
Long: "Signs and outputs a hex-encoded JWT token with the given permissions. NOTE: only use this command when " +
"the node has already been initialized and started.",
RunE: newToken,
}
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("must specify permissions")
}
permissions, err := convertToPerms(args[0])
if err != nil {
return err
}

for _, set := range fsets {
cmd.Flags().AddFlagSet(set)
}
return cmd
}
ks, err := newKeystore(StorePath(cmd.Context()))
if err != nil {
return err

func newToken(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("must specify permissions")
}
}

permissions, err := convertToPerms(args[0])
if err != nil {
return err
}
key, err := ks.Get(nodemod.SecretName)
if err != nil {
if !errors.Is(err, keystore.ErrNotFound) {
return err
}
key, err = generateNewKey(ks)
if err != nil {
return err
}
}

expanded, err := homedir.Expand(filepath.Clean(StorePath(cmd.Context())))
if err != nil {
return err
}
ks, err := keystore.NewFSKeystore(filepath.Join(expanded, "keys"), nil)
if err != nil {
return err
token, err := buildJWTToken(key.Body, permissions)
if err != nil {
return err
}
fmt.Printf("%s", token)
return nil
},
}

var key keystore.PrivKey
key, err = ks.Get(nodemod.SecretName)
if err != nil {
if !errors.Is(err, keystore.ErrNotFound) {
return err
}
// otherwise, generate and save new priv key
key, err = generateNewKey(ks)
if err != nil {
return err
}
for _, set := range fsets {
cmd.Flags().AddFlagSet(set)
}
return cmd
}

signer, err := jwt.NewHS256(key.Body)
func newKeystore(path string) (keystore.Keystore, error) {
expanded, err := homedir.Expand(filepath.Clean(path))
if err != nil {
return err
return nil, err
}
return keystore.NewFSKeystore(filepath.Join(expanded, "keys"), nil)
}

token, err := authtoken.NewSignedJWT(signer, permissions)
func buildJWTToken(body []byte, permissions []auth.Permission) (string, error) {
signer, err := jwt.NewHS256(body)
if err != nil {
return err
return "", err
}

fmt.Printf("%s", token)
return nil
return authtoken.NewSignedJWT(signer, permissions)
}

func generateNewKey(ks keystore.Keystore) (keystore.PrivKey, error) {
Expand Down
22 changes: 7 additions & 15 deletions cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)

var (
const (
nodeStoreFlag = "node.store"
nodeConfigFlag = "node.config"
)
Expand Down
57 changes: 51 additions & 6 deletions cmd/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"

"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"
nodemod "github.com/celestiaorg/celestia-node/nodebuilder/node"
)

const (
Expand All @@ -21,22 +25,45 @@ var (
authTokenFlag 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.AddFlag(storeFlag)
return fset
}

func InitClient(cmd *cobra.Command, _ []string) error {
if authTokenFlag == "" {
authTokenFlag = os.Getenv(authEnvKey)
}

if authTokenFlag == "" {
storePath := ""
if cmd.Flag(nodeStoreFlag).Changed {
storePath = cmd.Flag(nodeStoreFlag).Value.String()
}
token, err := getToken(storePath)
if err != nil {
return fmt.Errorf("cant get the access to the auth token: %v", err)
}
authTokenFlag = token
}

client, err := rpc.NewClient(cmd.Context(), requestURL, authTokenFlag)
if err != nil {
return err
Expand All @@ -47,6 +74,24 @@ func InitClient(cmd *cobra.Command, _ []string) error {
return nil
}

func getToken(path string) (string, error) {
if path == "" {
return "", errors.New("root directory was not specified")
}

ks, err := newKeystore(path)
if err != nil {
return "", err
}

key, err := ks.Get(nodemod.SecretName)
if err != nil {
fmt.Printf("error getting the JWT secret: %v", err)
return "", err
}
return buildJWTToken(key.Body, perms.AllPerms)
}

type rpcClientKey struct{}

func ParseClientFromCtx(ctx context.Context) (*rpc.Client, error) {
Expand Down

0 comments on commit f596e11

Please sign in to comment.