make tests no longer create auth tokens.

This commit is contained in:
Raúl Kripalani 2021-06-09 23:42:24 +01:00
parent 6e4eae69ac
commit 6d46be53bd
4 changed files with 39 additions and 34 deletions

View File

@ -155,7 +155,7 @@ var runCmd = &cli.Command{
log.Infof("Remote version %s", v) log.Infof("Remote version %s", v)
// Instantiate the miner node handler. // Instantiate the miner node handler.
handler, err := node.MinerHandler(minerapi) handler, err := node.MinerHandler(minerapi, true)
if err != nil { if err != nil {
return xerrors.Errorf("failed to instantiate rpc handler: %w", err) return xerrors.Errorf("failed to instantiate rpc handler: %w", err)
} }

View File

@ -363,7 +363,7 @@ var DaemonCmd = &cli.Command{
} }
// Instantiate the full node handler. // Instantiate the full node handler.
h, err := node.FullNodeHandler(api, serverOptions...) h, err := node.FullNodeHandler(api, true, serverOptions...)
if err != nil { if err != nil {
return fmt.Errorf("failed to instantiate rpc handler: %s", err) return fmt.Errorf("failed to instantiate rpc handler: %s", err)
} }

View File

@ -11,7 +11,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/filecoin-project/go-state-types/network" "github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
@ -629,18 +628,13 @@ func CreateRPCServer(t *testing.T, handler http.Handler) (*httptest.Server, mult
} }
func fullRpc(t *testing.T, nd TestFullNode) TestFullNode { func fullRpc(t *testing.T, nd TestFullNode) TestFullNode {
tok, err := nd.AuthNew(context.Background(), []auth.Permission{"admin", "read", "write", "sign"}) handler, err := node.FullNodeHandler(nd.FullNode, false)
require.NoError(t, err)
handler, err := node.FullNodeHandler(nd.FullNode)
require.NoError(t, err) require.NoError(t, err)
srv, maddr := CreateRPCServer(t, handler) srv, maddr := CreateRPCServer(t, handler)
var ret TestFullNode var ret TestFullNode
cl, stop, err := client.NewFullNodeRPCV1(context.Background(), "ws://"+srv.Listener.Addr().String()+"/rpc/v1", map[string][]string{ cl, stop, err := client.NewFullNodeRPCV1(context.Background(), "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil)
"Authorization": {"Bearer " + string(tok)},
})
require.NoError(t, err) require.NoError(t, err)
t.Cleanup(stop) t.Cleanup(stop)
ret.ListenAddr, ret.FullNode = maddr, cl ret.ListenAddr, ret.FullNode = maddr, cl
@ -649,18 +643,13 @@ func fullRpc(t *testing.T, nd TestFullNode) TestFullNode {
} }
func storerRpc(t *testing.T, nd TestMiner) TestMiner { func storerRpc(t *testing.T, nd TestMiner) TestMiner {
tok, err := nd.AuthNew(context.Background(), []auth.Permission{"admin", "read", "write"}) handler, err := node.MinerHandler(nd.StorageMiner, false)
require.NoError(t, err)
handler, err := node.MinerHandler(nd.StorageMiner)
require.NoError(t, err) require.NoError(t, err)
srv, maddr := CreateRPCServer(t, handler) srv, maddr := CreateRPCServer(t, handler)
var ret TestMiner var ret TestMiner
cl, stop, err := client.NewStorageMinerRPCV0(context.Background(), "ws://"+srv.Listener.Addr().String()+"/rpc/v0", map[string][]string{ cl, stop, err := client.NewStorageMinerRPCV0(context.Background(), "ws://"+srv.Listener.Addr().String()+"/rpc/v0", nil)
"Authorization": {"Bearer " + string(tok)},
})
require.NoError(t, err) require.NoError(t, err)
t.Cleanup(stop) t.Cleanup(stop)

View File

@ -62,32 +62,40 @@ func ServeRPC(h http.Handler, id string, addr multiaddr.Multiaddr) (StopFunc, er
} }
// FullNodeHandler returns a full node handler, to be mounted as-is on the server. // FullNodeHandler returns a full node handler, to be mounted as-is on the server.
func FullNodeHandler(a v1api.FullNode, opts ...jsonrpc.ServerOption) (http.Handler, error) { func FullNodeHandler(a v1api.FullNode, permissioned bool, opts ...jsonrpc.ServerOption) (http.Handler, error) {
m := mux.NewRouter() m := mux.NewRouter()
serveRpc := func(path string, hnd interface{}) { serveRpc := func(path string, hnd interface{}) {
rpcServer := jsonrpc.NewServer(opts...) rpcServer := jsonrpc.NewServer(opts...)
rpcServer.Register("Filecoin", hnd) rpcServer.Register("Filecoin", hnd)
ah := &auth.Handler{ var handler http.Handler = rpcServer
Verify: a.AuthVerify, if permissioned {
Next: rpcServer.ServeHTTP, handler = &auth.Handler{Verify: a.AuthVerify, Next: rpcServer.ServeHTTP}
} }
m.Handle(path, ah) m.Handle(path, handler)
} }
pma := api.PermissionedFullAPI(metrics.MetricedFullAPI(a)) fnapi := metrics.MetricedFullAPI(a)
if permissioned {
fnapi = api.PermissionedFullAPI(fnapi)
}
serveRpc("/rpc/v1", pma) serveRpc("/rpc/v1", fnapi)
serveRpc("/rpc/v0", &v0api.WrapperV1Full{FullNode: pma}) serveRpc("/rpc/v0", &v0api.WrapperV1Full{FullNode: fnapi})
// Import handler
handleImportFunc := handleImport(a.(*impl.FullNodeAPI))
if permissioned {
importAH := &auth.Handler{ importAH := &auth.Handler{
Verify: a.AuthVerify, Verify: a.AuthVerify,
Next: handleImport(a.(*impl.FullNodeAPI)), Next: handleImportFunc,
} }
m.Handle("/rest/v0/import", importAH) m.Handle("/rest/v0/import", importAH)
} else {
m.HandleFunc("/rest/v0/import", handleImportFunc)
}
// debugging // debugging
m.Handle("/debug/metrics", metrics.Exporter()) m.Handle("/debug/metrics", metrics.Exporter())
@ -101,11 +109,16 @@ func FullNodeHandler(a v1api.FullNode, opts ...jsonrpc.ServerOption) (http.Handl
} }
// MinerHandler returns a miner handler, to be mounted as-is on the server. // MinerHandler returns a miner handler, to be mounted as-is on the server.
func MinerHandler(a api.StorageMiner) (http.Handler, error) { func MinerHandler(a api.StorageMiner, permissioned bool) (http.Handler, error) {
m := mux.NewRouter() m := mux.NewRouter()
mapi := metrics.MetricedStorMinerAPI(a)
if permissioned {
mapi = api.PermissionedStorMinerAPI(mapi)
}
rpcServer := jsonrpc.NewServer() rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", api.PermissionedStorMinerAPI(metrics.MetricedStorMinerAPI(a))) rpcServer.Register("Filecoin", mapi)
m.Handle("/rpc/v0", rpcServer) m.Handle("/rpc/v0", rpcServer)
m.PathPrefix("/remote").HandlerFunc(a.(*impl.StorageMinerAPI).ServeRemote) m.PathPrefix("/remote").HandlerFunc(a.(*impl.StorageMinerAPI).ServeRemote)
@ -114,11 +127,14 @@ func MinerHandler(a api.StorageMiner) (http.Handler, error) {
m.Handle("/debug/metrics", metrics.Exporter()) m.Handle("/debug/metrics", metrics.Exporter())
m.PathPrefix("/").Handler(http.DefaultServeMux) // pprof m.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
if !permissioned {
return rpcServer, nil
}
ah := &auth.Handler{ ah := &auth.Handler{
Verify: a.AuthVerify, Verify: a.AuthVerify,
Next: m.ServeHTTP, Next: m.ServeHTTP,
} }
return ah, nil return ah, nil
} }