// ServerSecurePort obtains a listener that accepts secure connections. // If the provided port is zero, the listening is disabled. func ServeSecurePort(securePort int, certFile, keyFile, caCertFile string) { if securePort == 0 { log.Info("Not listening on secure port") return } config := tls.Config{} // load the server cert / key cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { log.Fatalf("SecureServe.LoadX509KeyPair(%v, %v) failed: %v", certFile, keyFile, err) } config.Certificates = []tls.Certificate{cert} // load the ca if necessary // FIXME(alainjobart) this doesn't quite work yet, have // to investigate if caCertFile != "" { config.ClientCAs = x509.NewCertPool() pemCerts, err := ioutil.ReadFile(caCertFile) if err != nil { log.Fatalf("SecureServe: cannot read ca file %v: %v", caCertFile, err) } if !config.ClientCAs.AppendCertsFromPEM(pemCerts) { log.Fatalf("SecureServe: AppendCertsFromPEM failed: %v", err) } config.ClientAuth = tls.RequireAndVerifyClientCert } l, err := tls.Listen("tcp", fmt.Sprintf(":%d", securePort), &config) if err != nil { log.Fatalf("Error listening on secure port %v: %v", securePort, err) } log.Infof("Listening on secure port %v", securePort) throttled := NewThrottledListener(l, *secureThrottle, *secureMaxBuffer) cl := proc.Published(throttled, "SecureConnections", "SecureAccepts") // rpc.HandleHTTP registers the default GOB handler at /_goRPC_ // and the debug RPC service at /debug/rpc (it displays a list // of registered services and their methods). if ServiceMap["gob-vts"] { log.Infof("Registering GOB handler and /debug/rpc URL for vts port") secureRpcServer.HandleHTTP(rpcwrap.GetRpcPath("gob", false), rpcplus.DefaultDebugPath) } if ServiceMap["gob-auth-vts"] { log.Infof("Registering GOB handler and /debug/rpcs URL for SASL vts port") authenticatedSecureRpcServer.HandleHTTP(rpcwrap.GetRpcPath("gob", true), rpcplus.DefaultDebugPath+"s") } handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, secureRpcServer, false) bsonrpc.ServeCustomRPC(handler, authenticatedSecureRpcServer, true) httpServer := http.Server{ Handler: handler, } go httpServer.Serve(cl) }
// 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 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) }
// ServeSocketFile listen to the named socket and serves RPCs on it. func ServeSocketFile(name string) { if name == "" { log.Infof("Not listening on socket file") return } // try to delete if file exists if _, err := os.Stat(name); err == nil { err = os.Remove(name) if err != nil { log.Fatalf("Cannot remove socket file %v: %v", name, err) } } l, err := net.Listen("unix", name) if err != nil { log.Fatalf("Error listening on socket file %v: %v", name, err) } log.Infof("Listening on socket file %v", name) // HandleHTTP registers the default GOB handler at /_goRPC_ // and the debug RPC service at /debug/rpc (it displays a list // of registered services and their methods). if ServiceMap["gob-unix"] { log.Infof("Registering GOB handler and /debug/rpc URL for unix socket") socketFileRpcServer.HandleHTTP(rpcwrap.GetRpcPath("gob", false), rpcplus.DefaultDebugPath) } if ServiceMap["gob-auth-unix"] { log.Infof("Registering GOB handler and /debug/rpcs URL for SASL unix socket") authenticatedSocketFileRpcServer.HandleHTTP(rpcwrap.GetRpcPath("gob", true), rpcplus.DefaultDebugPath+"s") } handler := http.NewServeMux() bsonrpc.ServeCustomRPC(handler, socketFileRpcServer, false) bsonrpc.ServeCustomRPC(handler, authenticatedSocketFileRpcServer, true) httpServer := http.Server{ Handler: handler, } go httpServer.Serve(l) }
// 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) }