Skip to content

Commit

Permalink
feat(rpc): add admin cmd (celestiaorg#2701)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Oct 5, 2023
1 parent e216544 commit c38fcb7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 50 deletions.
108 changes: 108 additions & 0 deletions cmd/celestia/admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"context"
"errors"
"strings"

"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/spf13/cobra"
)

func init() {
nodeCmd.AddCommand(nodeInfoCmd, logCmd, verifyCmd, authCmd)
rootCmd.AddCommand(nodeCmd)
}

var nodeCmd = &cobra.Command{
Use: "node [command]",
Short: "Allows administrating running node.",
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
rpcClient, err := newRPCClient(cmd.Context())
if err != nil {
return err
}

ctx := context.WithValue(cmd.Context(), rpcClientKey{}, rpcClient)
cmd.SetContext(ctx)
return nil
},
}

var nodeInfoCmd = &cobra.Command{
Use: "info",
Args: cobra.NoArgs,
Short: "Returns administrative information about the node.",
RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}
info, err := client.Node.Info(c.Context())
return printOutput(info, err, nil)
},
}

var logCmd = &cobra.Command{
Use: "log-level",
Args: cobra.MinimumNArgs(1),
Short: "Allows to set log level for module to in format <module>:<level>" +
"`DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL and their lower-case forms`.\n" +
"To set all modules to a particular level `*:<log.level>` should be passed",
RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}

for _, ll := range args {
params := strings.Split(ll, ":")
if len(params) != 2 {
return errors.New("cmd: log-level arg must be in form <module>:<level>," +
"e.g. pubsub:debug")
}

if err = client.Node.LogLevelSet(c.Context(), params[0], params[1]); err != nil {
return err
}
}
return nil
},
}

var verifyCmd = &cobra.Command{
Use: "permissions",
Args: cobra.ExactArgs(1),
Short: "Returns the permissions assigned to the given token.",

RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}

perms, err := client.Node.AuthVerify(c.Context(), args[0])
return printOutput(perms, err, nil)
},
}

var authCmd = &cobra.Command{
Use: "set-permissions",
Args: cobra.MinimumNArgs(1),
Short: "Signs and returns a new token with the given permissions.",
RunE: func(c *cobra.Command, args []string) error {
client, err := rpcClient(c.Context())
if err != nil {
return err
}

perms := make([]auth.Permission, len(args))
for i, p := range args {
perms[i] = (auth.Permission)(p)
}

result, err := client.Node.AuthNew(c.Context(), perms)
return printOutput(result, err, nil)
},
}
48 changes: 0 additions & 48 deletions cmd/celestia/logs.go

This file was deleted.

3 changes: 1 addition & 2 deletions cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/celestiaorg/celestia-node/state"
)

const authEnvKey = "CELESTIA_NODE_AUTH_TOKEN" //nolint:gosec
const authEnvKey = "CELESTIA_NODE_AUTH_TOKEN"

var requestURL string
var authTokenFlag string
Expand Down Expand Up @@ -60,7 +60,6 @@ func init() {
false,
"Print JSON-RPC request along with the response",
)
rpcCmd.AddCommand(logCmd, logModuleCmd)
rpcCmd.AddCommand(blobCmd)
rpcCmd.AddCommand(p2pCmd)
rpcCmd.AddCommand(dasCmd)
Expand Down

0 comments on commit c38fcb7

Please sign in to comment.