//this should be called exactly once and the result cached //NOTE- this crypto func might rightly belong in a crypto package //and universally accessed func getSecHelper() (crypto.Peer, error) { var secHelper crypto.Peer var err error once.Do(func() { if core.SecurityEnabled() { enrollID := viper.GetString("security.enrollID") enrollSecret := viper.GetString("security.enrollSecret") if peer.ValidatorEnabled() { logger.Debugf("Registering validator with enroll ID: %s", enrollID) if err = crypto.RegisterValidator(enrollID, nil, enrollID, enrollSecret); nil != err { return } logger.Debugf("Initializing validator with enroll ID: %s", enrollID) secHelper, err = crypto.InitValidator(enrollID, nil) if nil != err { return } } else { logger.Debugf("Registering non-validator with enroll ID: %s", enrollID) if err = crypto.RegisterPeer(enrollID, nil, enrollID, enrollSecret); nil != err { return } logger.Debugf("Initializing non-validator with enroll ID: %s", enrollID) secHelper, err = crypto.InitPeer(enrollID, nil) if nil != err { return } } } }) return secHelper, err }
func getSecHelper() (crypto.Peer, error) { var secHelper crypto.Peer var err error if viper.GetBool("security.enabled") { enrollID := viper.GetString("security.enrollID") enrollSecret := viper.GetString("security.enrollSecret") if viper.GetBool("peer.validator.enabled") { if err = crypto.RegisterValidator(enrollID, nil, enrollID, enrollSecret); nil != err { return nil, err } secHelper, err = crypto.InitValidator(enrollID, nil) if nil != err { return nil, err } } else { if err = crypto.RegisterPeer(enrollID, nil, enrollID, enrollSecret); nil != err { return nil, err } secHelper, err = crypto.InitPeer(enrollID, nil) if nil != err { return nil, err } } } return secHelper, err }
func initVP() { var opts []grpc.ServerOption if viper.GetBool("peer.tls.enabled") { creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) if err != nil { grpclog.Fatalf("Failed to generate credentials %v", err) } opts = []grpc.ServerOption{grpc.Creds(creds)} } grpcServer := grpc.NewServer(opts...) //lis, err := net.Listen("tcp", viper.GetString("peer.address")) //use a different address than what we usually use for "peer" //we override the peerAddress set in chaincode_support.go peerAddress := "0.0.0.0:40404" var err error lis, err = net.Listen("tcp", peerAddress) if err != nil { return } getPeerEndpoint := func() (*pb.PeerEndpoint, error) { return &pb.PeerEndpoint{ID: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil } ccStartupTimeout := time.Duration(chaincodeStartupTimeoutDefault) * time.Millisecond userRunsCC := true // Install security object for peer var secHelper crypto.Peer if viper.GetBool("security.enabled") { enrollID := viper.GetString("security.enrollID") enrollSecret := viper.GetString("security.enrollSecret") var err error if viper.GetBool("peer.validator.enabled") { testLogger.Debug("Registering validator with enroll ID: %s", enrollID) if err = crypto.RegisterValidator(enrollID, nil, enrollID, enrollSecret); nil != err { panic(err) } testLogger.Debug("Initializing validator with enroll ID: %s", enrollID) secHelper, err = crypto.InitValidator(enrollID, nil) if nil != err { panic(err) } } else { testLogger.Debug("Registering non-validator with enroll ID: %s", enrollID) if err = crypto.RegisterPeer(enrollID, nil, enrollID, enrollSecret); nil != err { panic(err) } testLogger.Debug("Initializing non-validator with enroll ID: %s", enrollID) secHelper, err = crypto.InitPeer(enrollID, nil) if nil != err { panic(err) } } } pb.RegisterChaincodeSupportServer(grpcServer, chaincode.NewChaincodeSupport(chaincode.DefaultChain, getPeerEndpoint, userRunsCC, ccStartupTimeout, secHelper)) grpcServer.Serve(lis) }