From 257847ebaa1e62ab92bd53d34fae608111c49a17 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 6 May 2022 15:47:28 -0400 Subject: [PATCH] feat: add flags for enabling admin and private RPC methods --- waku.go | 12 ++++++++++++ waku/node.go | 2 +- waku/options.go | 2 ++ waku/v2/rpc/waku_rpc.go | 25 ++++++++++++++++--------- waku/v2/rpc/waku_rpc_test.go | 2 +- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/waku.go b/waku.go index 55bdb83a..1ccc336e 100644 --- a/waku.go +++ b/waku.go @@ -324,6 +324,18 @@ func main() { Usage: "Listening address of the rpc server", Destination: &options.RPCServer.Address, }, + &cli.BoolFlag{ + Name: "rpc-admin", + Value: false, + Usage: "Enable access to JSON-RPC Admin API", + Destination: &options.RPCServer.Admin, + }, + &cli.BoolFlag{ + Name: "rpc-private", + Value: false, + Usage: "Enable access to JSON-RPC Private API", + Destination: &options.RPCServer.Private, + }, }, Action: func(c *cli.Context) error { // for go-libp2p loggers diff --git a/waku/node.go b/waku/node.go index bac57180..3acbb001 100644 --- a/waku/node.go +++ b/waku/node.go @@ -291,7 +291,7 @@ func Execute(options Options) { var rpcServer *rpc.WakuRpc if options.RPCServer.Enable { - rpcServer = rpc.NewWakuRpc(wakuNode, options.RPCServer.Address, options.RPCServer.Port, utils.Logger()) + rpcServer = rpc.NewWakuRpc(wakuNode, options.RPCServer.Address, options.RPCServer.Port, options.RPCServer.Admin, options.RPCServer.Private, utils.Logger()) rpcServer.Start() } diff --git a/waku/options.go b/waku/options.go index 24be38f8..54dc65f0 100644 --- a/waku/options.go +++ b/waku/options.go @@ -92,6 +92,8 @@ type RPCServerOptions struct { Enable bool Port int Address string + Admin bool + Private bool } type WSOptions struct { diff --git a/waku/v2/rpc/waku_rpc.go b/waku/v2/rpc/waku_rpc.go index ef3063be..d2fb7c7f 100644 --- a/waku/v2/rpc/waku_rpc.go +++ b/waku/v2/rpc/waku_rpc.go @@ -20,9 +20,10 @@ type WakuRpc struct { relayService *RelayService filterService *FilterService privateService *PrivateService + adminService *AdminService } -func NewWakuRpc(node *node.WakuNode, address string, port int, log *zap.SugaredLogger) *WakuRpc { +func NewWakuRpc(node *node.WakuNode, address string, port int, enableAdmin bool, enablePrivate bool, log *zap.SugaredLogger) *WakuRpc { wrpc := new(WakuRpc) wrpc.log = log.Named("rpc") @@ -46,9 +47,13 @@ func NewWakuRpc(node *node.WakuNode, address string, port int, log *zap.SugaredL wrpc.log.Error(err) } - err = s.RegisterService(&AdminService{node, log.Named("admin")}, "Admin") - if err != nil { - wrpc.log.Error(err) + if enableAdmin { + adminService := &AdminService{node, log.Named("admin")} + err = s.RegisterService(adminService, "Admin") + if err != nil { + wrpc.log.Error(err) + } + wrpc.adminService = adminService } filterService := NewFilterService(node, log) @@ -57,10 +62,13 @@ func NewWakuRpc(node *node.WakuNode, address string, port int, log *zap.SugaredL wrpc.log.Error(err) } - privateService := NewPrivateService(node, log) - err = s.RegisterService(privateService, "Private") - if err != nil { - wrpc.log.Error(err) + if enablePrivate { + privateService := NewPrivateService(node, log) + err = s.RegisterService(privateService, "Private") + if err != nil { + wrpc.log.Error(err) + } + wrpc.privateService = privateService } mux := http.NewServeMux() @@ -86,7 +94,6 @@ func NewWakuRpc(node *node.WakuNode, address string, port int, log *zap.SugaredL wrpc.server = server wrpc.relayService = relayService wrpc.filterService = filterService - wrpc.privateService = privateService return wrpc } diff --git a/waku/v2/rpc/waku_rpc_test.go b/waku/v2/rpc/waku_rpc_test.go index 018ae756..1ccee7b5 100644 --- a/waku/v2/rpc/waku_rpc_test.go +++ b/waku/v2/rpc/waku_rpc_test.go @@ -14,7 +14,7 @@ func TestWakuRpc(t *testing.T) { n, err := node.New(context.Background(), options) require.NoError(t, err) - rpc := NewWakuRpc(n, "127.0.0.1", 8080, utils.Logger()) + rpc := NewWakuRpc(n, "127.0.0.1", 8080, true, true, utils.Logger()) require.NotNil(t, rpc.server) require.Equal(t, rpc.server.Addr, "127.0.0.1:8080") }