當前位置: 首頁>>代碼示例>>Golang>>正文


Golang credentials.NewServerTLSFromFile函數代碼示例

本文整理匯總了Golang中google/golang.org/grpc/credentials.NewServerTLSFromFile函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewServerTLSFromFile函數的具體用法?Golang NewServerTLSFromFile怎麽用?Golang NewServerTLSFromFile使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewServerTLSFromFile函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: startTLSCA

func startTLSCA(t *testing.T) {
    CacheConfiguration() // Cache configuration
    ecaS = NewECA(nil)
    tlscaS = NewTLSCA(ecaS)

    var opts []grpc.ServerOption
    creds, err := credentials.NewServerTLSFromFile(viper.GetString("server.tls.cert.file"), viper.GetString("server.tls.key.file"))
    if err != nil {
        t.Logf("Failed creating credentials for TLS-CA service: %s", err)
        t.Fail()
    }

    opts = []grpc.ServerOption{grpc.Creds(creds)}

    srv = grpc.NewServer(opts...)

    ecaS.Start(srv)
    tlscaS.Start(srv)

    sock, err := net.Listen("tcp", viper.GetString("server.port"))
    if err != nil {
        t.Logf("Failed to start TLS-CA service: %s", err)
        t.Fail()
    }

    srv.Serve(sock)
}
開發者ID:yoshiharay,項目名稱:fabric,代碼行數:27,代碼來源:tlsca_test.go

示例2: startPKI

func startPKI() {
    var opts []grpc.ServerOption
    if viper.GetBool("peer.pki.tls.enabled") {
        // TLS configuration
        creds, err := credentials.NewServerTLSFromFile(
            filepath.Join(viper.GetString("server.rootpath"), "tlsca.cert"),
            filepath.Join(viper.GetString("server.rootpath"), "tlsca.priv"),
        )
        if err != nil {
            panic("Failed creating credentials for OBC-CA: " + err.Error())
        }
        opts = []grpc.ServerOption{grpc.Creds(creds)}
    }

    fmt.Printf("open socket...\n")
    sockp, err := net.Listen("tcp", viper.GetString("server.port"))
    if err != nil {
        panic("Cannot open port: " + err.Error())
    }
    fmt.Printf("open socket...done\n")

    server = grpc.NewServer(opts...)

    eca.Start(server)
    tca.Start(server)
    tlsca.Start(server)

    fmt.Printf("start serving...\n")
    server.Serve(sockp)
}
開發者ID:tenc,項目名稱:obc-peer-pre-public,代碼行數:30,代碼來源:crypto_test.go

示例3: Start

//Start Start
func Start(port int, tls bool, certFile, keyFile string, ch chan string) {

    log.Println("demo start")
    sid = utils.Randstr20()

    s := new(grpcService)
    lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
    if err != nil {
        grpclog.Fatalf("failed to listen: %v", err)
    }
    var opts []grpc.ServerOption
    if tls {
        creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
        if err != nil {
            grpclog.Fatalf("Failed to generate credentials %v", err)
        }
        opts = []grpc.ServerOption{grpc.Creds(creds)}
    }

    grpcServer := grpc.NewServer(opts...)
    demo.RegisterRPCServer(grpcServer, s)
    go func() {
        <-ch
        grpcServer.Stop()
    }()
    grpcServer.Serve(lis)

    log.Println("demo end")
}
開發者ID:vanishs,項目名稱:gwsrpc,代碼行數:30,代碼來源:demo.go

示例4: startTLSCA

func startTLSCA(t *testing.T) {
    LogInit(ioutil.Discard, os.Stdout, os.Stdout, os.Stderr, os.Stdout)

    eca_s = NewECA()
    tlsca_s = NewTLSCA(eca_s)

    var opts []grpc.ServerOption
    creds, err := credentials.NewServerTLSFromFile(viper.GetString("server.tls.certfile"), viper.GetString("server.tls.keyfile"))
    if err != nil {
        t.Logf("Failed creating credentials for TLS-CA service: %s", err)
        t.Fail()
    }

    opts = []grpc.ServerOption{grpc.Creds(creds)}

    srv = grpc.NewServer(opts...)

    eca_s.Start(srv)
    tlsca_s.Start(srv)

    sock, err := net.Listen("tcp", viper.GetString("server.port"))
    if err != nil {
        t.Logf("Failed to start TLS-CA service: %s", err)
        t.Fail()
    }

    srv.Serve(sock)
}
開發者ID:tenc,項目名稱:obc-peer-pre-public,代碼行數:28,代碼來源:tlsca_test.go

示例5: main

func main() {
    flag.Parse()
    lis, err := net.Listen("tcp", *serverAddr)
    if err != nil {
        grpclog.Fatalf("failed to listen: %v", err)
    }
    var opts []grpc.ServerOption
    if *tls {
        creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
        if err != nil {
            grpclog.Fatalf("Failed to generate credentials %v", err)
        }
        opts = append(opts, grpc.Creds(creds))
    }
    grpcServer := grpc.NewServer(opts...)
    oidcClient, err := util.GetOIDCClient(*clientID, *clientSecret, *discovery, *redirectURL)
    if err != nil {
        grpclog.Fatalf("unable to get oidc client: %s", err)
    }
    s, err := server.NewRoloServer(oidcClient, *policyFile)
    if err != nil {
        grpclog.Fatalln("unable to create ca from parent:", err)
    }
    pb.RegisterRoloServer(grpcServer, s)
    grpclog.Println("serving at", *serverAddr)
    grpcServer.Serve(lis)
}
開發者ID:polvi,項目名稱:rolo,代碼行數:27,代碼來源:main.go

示例6: ListenGRPC

func (s *Server) ListenGRPC() error {
    grpcPort := s.Config.GetGrpcPortString()
    //Listen
    lis, err := net.Listen("tcp", grpcPort)
    if err != nil {
        return fmt.Errorf("server.go: failed to listen, %v", err)
    }

    var opts []grpc.ServerOption
    if s.Config.TlsCertFile != "" && s.Config.TlsKeyFile != "" {
        creds, err := credentials.NewServerTLSFromFile(s.Config.TlsCertFile, s.Config.TlsKeyFile)
        if err != nil {
            return fmt.Errorf("server.go: Failed to generate credentials %v", err)
        }
        opts = []grpc.ServerOption{grpc.Creds(creds)}
    }
    s.checker = health.New(s, s.DB)
    grpcServer := grpc.NewServer(opts...)
    man := newABManager(s)
    s.ab = man

    ab := &abTestServer{m: man, s: s}
    registryGrpc := &RegistryServer{server: s}
    otsimopb.RegisterRegistryServiceServer(grpcServer, registryGrpc)
    otsimopb.RegisterABTestServer(grpcServer, ab)

    grpc_health_v1.RegisterHealthServer(grpcServer, s.checker)

    log.Infof("server.go: Binding %s for grpc", grpcPort)
    return grpcServer.Serve(lis)
}
開發者ID:otsimo,項目名稱:distribution,代碼行數:31,代碼來源:server.go

示例7: ListenGRPC

func (s *Server) ListenGRPC() error {
    grpcPort := s.Config.GetGrpcPortString()
    //Listen
    lis, err := net.Listen("tcp", grpcPort)
    if err != nil {
        return fmt.Errorf("server.go: failed to listen: %v", err)
    }

    var opts []grpc.ServerOption
    if s.Config.TlsCertFile != "" && s.Config.TlsKeyFile != "" {
        creds, err := credentials.NewServerTLSFromFile(s.Config.TlsCertFile, s.Config.TlsKeyFile)
        if err != nil {
            log.Fatalf("server.go: Failed to generate credentials %v", err)
        }
        opts = []grpc.ServerOption{grpc.Creds(creds)}
        s.tlsCheck = tlscheck.New(s.Config.TlsCertFile, s.Config.TlsKeyFile, time.Hour*24*16)
    }

    h := health.New(s, s.Storage)
    grpcServer := grpc.NewServer(opts...)
    catalogGrpc := &catalogGrpcServer{
        server: s,
    }
    pb.RegisterCatalogServiceServer(grpcServer, catalogGrpc)
    grpc_health_v1.RegisterHealthServer(grpcServer, h)

    go http.ListenAndServe(s.Config.GetHealthPortString(), h)

    log.Infof("server.go: Binding %s for grpc", grpcPort)
    //Serve
    return grpcServer.Serve(lis)
}
開發者ID:otsimo,項目名稱:catalog,代碼行數:32,代碼來源:server.go

示例8: main

func main() {
    // 2. 解析flags
    flag.Parse()

    // 3. 監聽端口
    lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
    if err != nil {
        grpclog.Fatalf("failed to listen: %v", err)
    }

    // 4. 指定TLS
    var opts []grpc.ServerOption
    if *tls {
        creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
        if err != nil {
            grpclog.Fatalf("Failed to generate credentials %v", err)
        }
        opts = []grpc.ServerOption{grpc.Creds(creds)}
    }

    // 關聯Server和Handler
    grpcServer := grpc.NewServer(opts...)
    pb.RegisterRouteGuideServer(grpcServer, newServer())
    grpcServer.Serve(lis)
}
開發者ID:wfxiang08,項目名稱:grpc-go,代碼行數:25,代碼來源:server.go

示例9: Serve

func (cc *CCServer) Serve() error {
    l, err := net.Listen("tcp", cc.cfg.ListenAddress)
    if err != nil {
        return err
    }
    var opts []grpc.ServerOption
    if cc.cfg.UseTLS {
        if cc.cfg.MutualTLS {
            mtc, err := mutualTLS(*cc.cfg)
            if err != nil {
                return err
            }
            creds := credentials.NewTLS(mtc)
            opts = []grpc.ServerOption{grpc.Creds(creds)}
        } else {
            creds, err := credentials.NewServerTLSFromFile(cc.cfg.CertFile, cc.cfg.KeyFile)
            if err != nil {
                return err
            }
            opts = []grpc.ServerOption{grpc.Creds(creds)}
        }
    }
    s := grpc.NewServer(opts...)
    pb.RegisterCmdCtrlServer(s, cc)
    return s.Serve(l)
}
開發者ID:pandemicsyn,項目名稱:cmdctrl,代碼行數:26,代碼來源:cmdctrl.go

示例10: launchSyndicates

func (rs *RingSyndicates) launchSyndicates(k int) {
    rs.Syndics[k].Lock()
    defer rs.waitGroup.Done()
    rs.waitGroup.Add(1)
    l, err := net.Listen("tcp", fmt.Sprintf(":%d", rs.Syndics[k].config.Port))
    if err != nil {
        log.Fatalln(err)
        return
    }
    var opts []grpc.ServerOption
    creds, err := credentials.NewServerTLSFromFile(rs.Syndics[k].config.CertFile, rs.Syndics[k].config.KeyFile)
    if err != nil {
        log.Fatalln("Error load cert or key:", err)
    }
    opts = []grpc.ServerOption{grpc.Creds(creds)}
    rs.Syndics[k].gs = grpc.NewServer(opts...)

    if rs.Syndics[k].config.Master {
        pb.RegisterSyndicateServer(rs.Syndics[k].gs, rs.Syndics[k].server)
        log.Println("Master starting up on", rs.Syndics[k].config.Port)
        rs.Syndics[k].gs.Serve(l)
    } else {
        //pb.RegisterRingDistServer(s, newRingDistServer())
        //log.Printf("Starting ring slave up on %d...\n", cfg.Port)
        //s.Serve(l)
        log.Fatalln("Syndicate slaves not implemented yet")
    }
    rs.Syndics[k].Unlock()
}
開發者ID:getcfs,項目名稱:cfs-binary-release,代碼行數:29,代碼來源:main.go

示例11: createEventHubServer

func createEventHubServer() (net.Listener, *grpc.Server, error) {
    var lis net.Listener
    var grpcServer *grpc.Server
    var err error
    if viper.GetBool("peer.validator.enabled") {
        lis, err = net.Listen("tcp", viper.GetString("peer.validator.events.address"))
        if err != nil {
            return nil, nil, fmt.Errorf("failed to listen: %v", err)
        }

        //TODO - do we need different SSL material for events ?
        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 {
                return nil, nil, fmt.Errorf("Failed to generate credentials %v", err)
            }
            opts = []grpc.ServerOption{grpc.Creds(creds)}
        }

        grpcServer = grpc.NewServer(opts...)
        ehServer := producer.NewOpenchainEventsServer(uint(viper.GetInt("peer.validator.events.buffersize")), viper.GetInt("peer.validator.events.timeout"))
        pb.RegisterOpenchainEventsServer(grpcServer, ehServer)
    }
    return lis, grpcServer, err
}
開發者ID:tenc,項目名稱:obc-peer-pre-public,代碼行數:26,代碼來源:main.go

示例12: TestExecuteInvokeInvalidTransaction

// Test the execution of an invalid transaction.
func TestExecuteInvokeInvalidTransaction(t *testing.T) {
    testDBWrapper.CleanDB(t)
    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...)
    viper.Set("peer.fileSystemPath", "/var/hyperledger/test/tmpdb")

    //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:21212"

    lis, err := net.Listen("tcp", peerAddress)
    if err != nil {
        t.Fail()
        t.Logf("Error starting peer listener %s", err)
        return
    }

    getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
        return &pb.PeerEndpoint{ID: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil
    }

    ccStartupTimeout := time.Duration(chaincodeStartupTimeoutDefault) * time.Millisecond
    pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(DefaultChain, getPeerEndpoint, false, ccStartupTimeout, nil))

    go grpcServer.Serve(lis)

    var ctxt = context.Background()

    url := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
    chaincodeID := &pb.ChaincodeID{Path: url}

    //FAIL, FAIL!
    args := []string{"x", "-1"}
    err = invokeExample02Transaction(ctxt, chaincodeID, args, false)

    //this HAS to fail with expectedDeltaStringPrefix
    if err != nil {
        errStr := err.Error()
        t.Logf("Got error %s\n", errStr)
        t.Logf("InvalidInvoke test passed")
        GetChain(DefaultChain).Stop(ctxt, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID}})

        closeListenerAndSleep(lis)
        return
    }

    t.Fail()
    t.Logf("Error invoking transaction %s", err)

    GetChain(DefaultChain).Stop(ctxt, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeID: chaincodeID}})

    closeListenerAndSleep(lis)
}
開發者ID:yoshiharay,項目名稱:fabric,代碼行數:61,代碼來源:exectransaction_test.go

示例13: main

func main() {

    f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("error opening file: %v", err)
    }
    log.SetOutput(io.MultiWriter(f, os.Stdout))

    http.HandleFunc("/", fronthandler)
    http.HandleFunc("/_ah/health", healthhandler)
    go http.ListenAndServe(httpport, nil)

    ce, err := credentials.NewServerTLSFromFile("server_crt.pem", "server_key.pem")
    if err != nil {
        log.Fatalf("Failed to generate credentials %v", err)
    }

    lis, err := net.Listen("tcp", grpcport)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }

    sopts := []grpc.ServerOption{grpc.MaxConcurrentStreams(10)}
    sopts = append(sopts, grpc.Creds(ce))
    s := grpc.NewServer(sopts...)

    echo.RegisterEchoServerServer(s, &server{})
    log.Printf("Starting gRPC server on port %v", grpcport)

    s.Serve(lis)
}
開發者ID:salrashid123,項目名稱:gcegrpc,代碼行數:31,代碼來源:grpc_server.go

示例14: createGRPCLayer

func createGRPCLayer(port int) (*grpc.Server, net.Listener, grpc.DialOption) {
    var s *grpc.Server
    var ll net.Listener
    var err error
    var serverOpts []grpc.ServerOption
    var dialOpts grpc.DialOption

    keyFileName := fmt.Sprintf("key.%d.pem", time.Now().UnixNano())
    certFileName := fmt.Sprintf("cert.%d.pem", time.Now().UnixNano())

    defer os.Remove(keyFileName)
    defer os.Remove(certFileName)

    err = generateCertificates(keyFileName, certFileName)
    if err == nil {
        var creds credentials.TransportCredentials
        creds, err = credentials.NewServerTLSFromFile(certFileName, keyFileName)
        serverOpts = append(serverOpts, grpc.Creds(creds))
        ta := credentials.NewTLS(&tls.Config{
            InsecureSkipVerify: true,
        })
        dialOpts = grpc.WithTransportCredentials(&authCreds{tlsCreds: ta})
    } else {
        dialOpts = grpc.WithInsecure()
    }

    listenAddress := fmt.Sprintf("%s:%d", "", port)
    ll, err = net.Listen("tcp", listenAddress)
    if err != nil {
        panic(err)
    }

    s = grpc.NewServer(serverOpts...)
    return s, ll, dialOpts
}
開發者ID:kchristidis,項目名稱:fabric,代碼行數:35,代碼來源:comm_impl.go

示例15: NewGrpcServer

// NewGrpcServer -
func NewGrpcServer(serv services.Servicer, opts options.Options, logger *logging.Entry) (Serverer, error) {

    addr, addrOk := opts.Get("grpc-addr")

    if !addrOk {
        return nil, errors.New("You must provide `grpc-addr` in order to create gRPC server...")
    }

    listener, err := net.Listen("tcp", addr.String())

    if err != nil {
        return nil, fmt.Errorf("Failed to listen: %v", err)
    }

    var grpcOpts []grpc.ServerOption

    if useGrpc, ok := opts.Get("grpc-tls"); ok && useGrpc.Bool() {
        certFile, certOk := opts.Get("grpc-tls-cert")
        certKey, keyOk := opts.Get("grpc-tls-key")

        if !certOk {
            return nil, errors.New("You must provide `grpc-tls-cert` in order to create gRPC server...")
        }

        if !keyOk {
            return nil, errors.New("You must provide `grpc-tls-key` in order to create gRPC server...")
        }

        creds, err := credentials.NewServerTLSFromFile(certFile.String(), certKey.String())

        if err != nil {
            return nil, fmt.Errorf("Failed to generate gRPC credentials: %v", err)
        }

        grpcOpts = []grpc.ServerOption{grpc.Creds(creds)}
    }

    if maxStreams, msOk := opts.Get("grpc-max-concurrent-streams"); msOk {
        grpcOpts = []grpc.ServerOption{grpc.MaxConcurrentStreams(maxStreams.UInt32())}
    }

    grpcServer := grpc.NewServer(grpcOpts...)

    s := &Grpc{
        Options:           opts,
        Server:            grpcServer,
        Listener:          listener,
        Entry:             logger,
        Servicer:          serv,
        ConnectivityState: &ConnectivityState{},
        Interrupt:         serv.GetInterruptChan(),
    }

    if listenForever, lfOk := opts.Get("grpc-listen-forever"); lfOk {
        s.ListenForever = listenForever.Bool()
    }

    return Serverer(s), nil
}
開發者ID:golanghr,項目名稱:platform,代碼行數:60,代碼來源:grpc.go


注:本文中的google/golang.org/grpc/credentials.NewServerTLSFromFile函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。