Skip to content

Commit

Permalink
fix: improve rpc.skip-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Jan 31, 2024
1 parent f235d10 commit eb4e640
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
14 changes: 6 additions & 8 deletions api/rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
16 changes: 8 additions & 8 deletions api/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions nodebuilder/rpc/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions nodebuilder/tests/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit eb4e640

Please sign in to comment.