// the test here creates a fake server implementation, a fake client // implementation, and runs the test suite against the setup. func TestVtctlServer(t *testing.T) { ts := vtctlclienttest.CreateTopoServer(t) // Listen on a random port listener, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } port := listener.Addr().(*net.TCPAddr).Port // Create a Go Rpc server and listen on the port server := rpcplus.NewServer() server.Register(gorpcvtctlserver.NewVtctlServer(ts)) // create the HTTP server, serve the server from it handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, server, false) httpServer := http.Server{ Handler: handler, } go httpServer.Serve(listener) // Create a VtctlClient Go Rpc client to talk to the fake server client, err := goRpcVtctlClientFactory(fmt.Sprintf("localhost:%v", port), 30*time.Second) if err != nil { t.Fatalf("Cannot create client: %v", err) } defer client.Close() vtctlclienttest.VtctlClientTestSuite(t, ts, client) }
// StartActionLoop will start the action loop for a fake tablet, // using ft.FakeMysqlDaemon as the backing mysqld. func (ft *FakeTablet) StartActionLoop(t *testing.T, wr *wrangler.Wrangler) { if ft.Agent != nil { t.Fatalf("Agent for %v is already running", ft.Tablet.Alias) } // Listen on a random port var err error ft.Listener, err = net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } port := ft.Listener.Addr().(*net.TCPAddr).Port // create a test agent on that port, and re-read the record // (it has new ports and IP) ft.Agent = tabletmanager.NewTestActionAgent(wr.TopoServer(), ft.Tablet.Alias, port, ft.FakeMysqlDaemon) ft.Tablet = ft.Agent.Tablet().Tablet // create the RPC server ft.RpcServer = rpcplus.NewServer() gorpctmserver.RegisterForTest(ft.RpcServer, ft.Agent) // create the HTTP server, serve the server from it handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, ft.RpcServer, false) httpServer := http.Server{ Handler: handler, } go httpServer.Serve(ft.Listener) }
// the test here creates a fake server implementation, a fake client // implementation, and runs the test suite against the setup. func TestGoRpcTMServer(t *testing.T) { // Listen on a random port listener, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } port := listener.Addr().(*net.TCPAddr).Port // Create a Go Rpc server and listen on the port server := rpcplus.NewServer() server.Register(&TabletManager{agentrpctest.NewFakeRpcAgent(t)}) // create the HTTP server, serve the server from it handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, server, false) httpServer := http.Server{ Handler: handler, } go httpServer.Serve(listener) // Create a Go Rpc client to talk to the fake tablet client := &gorpctmclient.GoRpcTabletManagerClient{} ti := topo.NewTabletInfo(&topo.Tablet{ Alias: topo.TabletAlias{ Cell: "test", Uid: 123, }, Hostname: "localhost", Portmap: map[string]int{ "vt": port, }, }, 0) // and run the test suite agentrpctest.AgentRpcTestSuite(t, client, ti) }
// ServeAuthRPC handles authenticated rpc requests using the hijack // scheme of rpc func ServeAuthRPC(codecName string, cFactory ServerCodecFactory) { http.Handle(GetRpcPath(codecName, true), &rpcHandler{cFactory, AuthenticatedServer, true}) } // ServeCustomRPC serves the given rpc requests with the provided ServeMux, // authenticated or not func ServeCustomRPC(handler *http.ServeMux, server *rpc.Server, useAuth bool, codecName string, cFactory ServerCodecFactory) { handler.Handle(GetRpcPath(codecName, useAuth), &rpcHandler{cFactory, server, useAuth}) } // AuthenticatedServer is an rpc.Server instance that serves // authenticated calls. var AuthenticatedServer = rpc.NewServer() // rpcHandler handles rpc queries for a 'CONNECT' method. type rpcHandler struct { cFactory ServerCodecFactory server *rpc.Server useAuth bool } // ServeHTTP implements http.Handler's ServeHTTP func (h *rpcHandler) ServeHTTP(c http.ResponseWriter, req *http.Request) { if req.Method != "CONNECT" { c.Header().Set("Content-Type", "text/plain; charset=utf-8") c.WriteHeader(http.StatusMethodNotAllowed) io.WriteString(c, "405 must CONNECT\n") return
return err } if err = json.Unmarshal(data, c); err != nil { return err } log.Infof("Loaded credentials from %s.", filename) return nil } // CRAMMD5MaxRequests is the maximum number of requests that an // unauthenticated client is allowed to make (this should be enough to // perform authentication). const CRAMMD5MaxRequests = 2 var ( AuthenticationServer = rpc.NewServer() DefaultAuthenticatorCRAMMD5 = NewAuthenticatorCRAMMD5() ) // AuthenticationFailed is returned when the client fails to // authenticate. var AuthenticationFailed = errors.New("authentication error: authentication failed") func NewAuthenticatorCRAMMD5() *AuthenticatorCRAMMD5 { return &AuthenticatorCRAMMD5{make(cramMD5Credentials)} } // LoadCredentials loads credentials stored in the JSON file named // filename into the default authenticator. func LoadCredentials(filename string) error { return DefaultAuthenticatorCRAMMD5.Credentials.Load(filename)
"github.com/henryanand/vitess/go/rpcwrap/bsonrpc" ) var ( // The flags used when calling RegisterDefaultSecureFlags. SecurePort *int CertFile *string KeyFile *string CACertFile *string // Flags to alter the behavior of the library. secureThrottle = flag.Int64("secure-accept-rate", 64, "Maximum number of secure connection accepts per second") secureMaxBuffer = flag.Int("secure-max-buffer", 1500, "Maximum number of secure accepts allowed to accumulate") // The rpc servers to use secureRpcServer = rpcplus.NewServer() authenticatedSecureRpcServer = rpcplus.NewServer() ) // secureRegister registers the provided server to be served on the // secure port, if enabled by the service map. func secureRegister(name string, rcvr interface{}) { if SecurePort == nil || *SecurePort == 0 { return } if ServiceMap["bsonrpc-vts-"+name] { log.Infof("Registering %v for bsonrpc over vts port, disable it with -bsonrpc-vts-%v service_map parameter", name, name) secureRpcServer.Register(rcvr) } else { log.Infof("Not registering %v for bsonrpc over vts port, enable it with bsonrpc-vts-%v service_map parameter", name, name) }
"net" "net/http" "os" log "github.com/golang/glog" "github.com/henryanand/vitess/go/rpcplus" "github.com/henryanand/vitess/go/rpcwrap" "github.com/henryanand/vitess/go/rpcwrap/bsonrpc" ) var ( // The flags used when calling RegisterDefaultSocketFileFlags. SocketFile *string // The rpc servers to use socketFileRpcServer = rpcplus.NewServer() authenticatedSocketFileRpcServer = rpcplus.NewServer() ) // socketFileRegister registers the provided server to be served on the // SocketFile, if enabled by the service map. func socketFileRegister(name string, rcvr interface{}) { if SocketFile == nil || *SocketFile == "" { return } if ServiceMap["bsonrpc-unix-"+name] { log.Infof("Registering %v for bsonrpc over unix socket, disable it with -bsonrpc-unix-%v service_map parameter", name, name) socketFileRpcServer.Register(rcvr) } else { log.Infof("Not registering %v for bsonrpc over unix socket, enable it with bsonrpc-unix-%v service_map parameter", name, name) }