Example #1
0
// NewCommInstance creates a new comm instance that binds itself to the given gRPC server
func NewCommInstance(s *grpc.Server, idStore identity.Mapper, peerIdentity api.PeerIdentityType, dialOpts ...grpc.DialOption) (Comm, error) {
	commInst, err := NewCommInstanceWithServer(-1, idStore, peerIdentity, dialOpts...)
	if err != nil {
		return nil, err
	}
	proto.RegisterGossipServer(s, commInst.(*commImpl))
	return commInst, nil
}
Example #2
0
// NewCommInstanceWithServer creates a comm instance that creates an underlying gRPC server
func NewCommInstanceWithServer(port int, idMapper identity.Mapper, peerIdentity api.PeerIdentityType, dialOpts ...grpc.DialOption) (Comm, error) {
	var ll net.Listener
	var s *grpc.Server
	var secOpt grpc.DialOption

	if len(dialOpts) == 0 {
		dialOpts = []grpc.DialOption{grpc.WithTimeout(dialTimeout)}
	}

	if port > 0 {
		s, ll, secOpt = createGRPCLayer(port)
		dialOpts = append(dialOpts, secOpt)
	}

	commInst := &commImpl{
		PKIID:             idMapper.GetPKIidOfCert(peerIdentity),
		idMapper:          idMapper,
		logger:            util.GetLogger(util.LOGGING_COMM_MODULE, fmt.Sprintf("%d", port)),
		peerIdentity:      peerIdentity,
		opts:              dialOpts,
		port:              port,
		lsnr:              ll,
		gSrv:              s,
		msgPublisher:      NewChannelDemultiplexer(),
		lock:              &sync.RWMutex{},
		deadEndpoints:     make(chan common.PKIidType, 100),
		stopping:          int32(0),
		exitChan:          make(chan struct{}, 1),
		subscriptions:     make([]chan ReceivedMessage, 0),
		blackListedPKIIDs: make([]common.PKIidType, 0),
	}
	commInst.connStore = newConnStore(commInst, commInst.logger)
	commInst.idMapper.Put(idMapper.GetPKIidOfCert(peerIdentity), peerIdentity)

	if port > 0 {
		go func() {
			commInst.stopWG.Add(1)
			defer commInst.stopWG.Done()
			s.Serve(ll)
		}()
		proto.RegisterGossipServer(s, commInst)
	}

	commInst.logger.SetLevel(logging.WARNING)

	return commInst, nil
}
Example #3
0
func createDiscoveryInstanceThatGossips(port int, id string, bootstrapPeers []string, shouldGossip bool) *gossipInstance {
	comm := &dummyCommModule{
		conns:        make(map[string]*grpc.ClientConn),
		streams:      make(map[string]proto.Gossip_GossipStreamClient),
		incMsgs:      make(chan *proto.GossipMessage, 1000),
		presumeDead:  make(chan common.PKIidType, 10000),
		id:           id,
		detectedDead: make(chan string, 10000),
		lock:         &sync.RWMutex{},
		lastSeqs:     make(map[string]uint64),
		shouldGossip: shouldGossip,
	}

	endpoint := fmt.Sprintf("localhost:%d", port)
	self := NetworkMember{
		Metadata: []byte{},
		PKIid:    []byte(endpoint),
		Endpoint: endpoint,
	}

	listenAddress := fmt.Sprintf("%s:%d", "", port)
	ll, err := net.Listen("tcp", listenAddress)
	if err != nil {
		fmt.Printf("Error listening on %v, %v", listenAddress, err)
	}
	s := grpc.NewServer()

	discSvc := NewDiscoveryService(bootstrapPeers, self, comm, comm)
	discSvc.(*gossipDiscoveryImpl).logger.SetLevel(logging.WARNING)
	gossInst := &gossipInstance{comm: comm, gRGCserv: s, Discovery: discSvc, lsnr: ll, shouldGossip: shouldGossip}

	proto.RegisterGossipServer(s, gossInst)
	go s.Serve(ll)

	return gossInst
}
Example #4
0
func TestCertificateGeneration(t *testing.T) {
	err := generateCertificates("key.pem", "cert.pem")
	assert.NoError(t, err, "%v", err)
	if err != nil {
		return
	}
	defer os.Remove("cert.pem")
	defer os.Remove("key.pem")
	var ll net.Listener
	creds, err := credentials.NewServerTLSFromFile("cert.pem", "key.pem")
	assert.NoError(t, err, "%v", err)
	if err != nil {
		return
	}
	s := grpc.NewServer(grpc.Creds(creds))
	ll, err = net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5511))
	assert.NoError(t, err, "%v", err)
	if err != nil {
		return
	}
	srv := &gossipTestServer{msgChan: make(chan uint64)}
	proto.RegisterGossipServer(s, srv)
	go s.Serve(ll)
	defer func() {
		s.Stop()
		ll.Close()
	}()
	time.Sleep(time.Second * time.Duration(2))
	ta := credentials.NewTLS(&tls.Config{
		InsecureSkipVerify: true,
	})
	assert.NoError(t, err, "%v", err)
	if err != nil {
		return
	}
	conn, err := grpc.Dial("localhost:5511", grpc.WithTransportCredentials(&authCreds{tlsCreds: ta}), grpc.WithBlock(), grpc.WithTimeout(time.Second))
	assert.NoError(t, err, "%v", err)
	if err != nil {
		return
	}
	cl := proto.NewGossipClient(conn)
	stream, err := cl.GossipStream(context.Background())
	assert.NoError(t, err, "%v", err)
	if err != nil {
		return
	}

	time.Sleep(time.Duration(1) * time.Second)

	clientTLSUnique := ExtractTLSUnique(stream.Context())
	serverTLSUnique := srv.getTLSUnique()

	assert.NotNil(t, clientTLSUnique)
	assert.NotNil(t, serverTLSUnique)

	assert.True(t, bytes.Equal(clientTLSUnique, serverTLSUnique), "Client and server TLSUnique are not equal")

	msg := createGossipMsg()
	stream.Send(msg)
	select {
	case nonce := <-srv.msgChan:
		assert.Equal(t, msg.Nonce, nonce)
		break
	case <-time.NewTicker(time.Second).C:
		assert.Fail(t, "Timed out reading from stream")
	}
}