Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): add admin cmd #2701

Merged
merged 4 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading