diff --git a/api/rpc/server.go b/api/rpc/server.go index 3bf58870bc..f4629fa9d7 100644 --- a/api/rpc/server.go +++ b/api/rpc/server.go @@ -61,17 +61,15 @@ func (s *Server) verifyAuth(_ context.Context, token string) ([]auth.Permission, // RegisterService registers a service onto the RPC server. All methods on the service will then be // exposed over the RPC. -func (s *Server) RegisterService(namespace string, service interface{}) { +func (s *Server) RegisterService(namespace string, service interface{}, out interface{}, authEnabled bool) { + if authEnabled { + auth.PermissionedProxy(perms.AllPerms, perms.DefaultPerms, service, getInternalStruct(out)) + s.rpc.Register(namespace, out) + return + } s.rpc.Register(namespace, service) } -// RegisterAuthedService registers a service onto the RPC server. All methods on the service will -// then be exposed over the RPC. -func (s *Server) RegisterAuthedService(namespace string, service interface{}, out interface{}) { - auth.PermissionedProxy(perms.AllPerms, perms.DefaultPerms, service, getInternalStruct(out)) - s.RegisterService(namespace, out) -} - func getInternalStruct(api interface{}) interface{} { return reflect.ValueOf(api).Elem().FieldByName("Internal").Addr().Interface() } diff --git a/api/rpc_test.go b/api/rpc_test.go index ec308a2320..2d6f9c5a6b 100644 --- a/api/rpc_test.go +++ b/api/rpc_test.go @@ -302,14 +302,14 @@ func setupNodeWithAuthedRPC(t *testing.T, auth jwt.Signer) (*nodebuilder.Node, * // given the behavior of fx.Invoke, this invoke will be called last as it is added at the root // level module. For further information, check the documentation on fx.Invoke. invokeRPC := fx.Invoke(func(srv *rpc.Server) { - srv.RegisterAuthedService("state", mockAPI.State, &statemod.API{}) - srv.RegisterAuthedService("share", mockAPI.Share, &share.API{}) - srv.RegisterAuthedService("fraud", mockAPI.Fraud, &fraud.API{}) - srv.RegisterAuthedService("header", mockAPI.Header, &header.API{}) - srv.RegisterAuthedService("das", mockAPI.Das, &das.API{}) - srv.RegisterAuthedService("p2p", mockAPI.P2P, &p2p.API{}) - srv.RegisterAuthedService("node", mockAPI.Node, &node.API{}) - srv.RegisterAuthedService("blob", mockAPI.Blob, &blob.API{}) + srv.RegisterService("fraud", mockAPI.Fraud, &fraud.API{}, true) + srv.RegisterService("das", mockAPI.Das, &das.API{}, true) + srv.RegisterService("header", mockAPI.Header, &header.API{}, true) + srv.RegisterService("state", mockAPI.State, &statemod.API{}, true) + srv.RegisterService("share", mockAPI.Share, &share.API{}, true) + srv.RegisterService("p2p", mockAPI.P2P, &p2p.API{}, true) + srv.RegisterService("node", mockAPI.Node, &node.API{}, true) + srv.RegisterService("blob", mockAPI.Blob, &blob.API{}, true) }) // fx.Replace does not work here, but fx.Decorate does nd := nodebuilder.TestNode(t, node.Full, invokeRPC, fx.Decorate(func() (jwt.Signer, error) { diff --git a/nodebuilder/rpc/constructors.go b/nodebuilder/rpc/constructors.go index 194dea8a03..580126ed1c 100644 --- a/nodebuilder/rpc/constructors.go +++ b/nodebuilder/rpc/constructors.go @@ -25,15 +25,17 @@ func registerEndpoints( nodeMod node.Module, blobMod blob.Module, serv *rpc.Server, + cfg *Config, ) { - serv.RegisterAuthedService("fraud", fraudMod, &fraud.API{}) - serv.RegisterAuthedService("das", daserMod, &das.API{}) - serv.RegisterAuthedService("header", headerMod, &header.API{}) - serv.RegisterAuthedService("state", stateMod, &state.API{}) - serv.RegisterAuthedService("share", shareMod, &share.API{}) - serv.RegisterAuthedService("p2p", p2pMod, &p2p.API{}) - serv.RegisterAuthedService("node", nodeMod, &node.API{}) - serv.RegisterAuthedService("blob", blobMod, &blob.API{}) + auth := !cfg.SkipAuth + serv.RegisterService("fraud", fraudMod, &fraud.API{}, auth) + serv.RegisterService("das", daserMod, &das.API{}, auth) + serv.RegisterService("header", headerMod, &header.API{}, auth) + serv.RegisterService("state", stateMod, &state.API{}, auth) + serv.RegisterService("share", shareMod, &share.API{}, auth) + serv.RegisterService("p2p", p2pMod, &p2p.API{}, auth) + serv.RegisterService("node", nodeMod, &node.API{}, auth) + serv.RegisterService("blob", blobMod, &blob.API{}, auth) } func server(cfg *Config, auth jwt.Signer) *rpc.Server { diff --git a/nodebuilder/tests/helpers_test.go b/nodebuilder/tests/helpers_test.go index 1e7f14d823..978b66553d 100644 --- a/nodebuilder/tests/helpers_test.go +++ b/nodebuilder/tests/helpers_test.go @@ -6,12 +6,12 @@ import ( "testing" "time" + "github.com/filecoin-project/go-jsonrpc/auth" + "github.com/stretchr/testify/require" + "github.com/celestiaorg/celestia-node/api/rpc/client" "github.com/celestiaorg/celestia-node/libs/authtoken" "github.com/celestiaorg/celestia-node/nodebuilder" - - "github.com/filecoin-project/go-jsonrpc/auth" - "github.com/stretchr/testify/require" ) func getAdminClient(ctx context.Context, nd *nodebuilder.Node, t *testing.T) *client.Client {