本文整理汇总了Golang中google/golang.org/grpc/credentials.NewClientTLSFromCert函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClientTLSFromCert函数的具体用法?Golang NewClientTLSFromCert怎么用?Golang NewClientTLSFromCert使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClientTLSFromCert函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GRPCClient
func GRPCClient(addr, token, caFile string) (*RemoteU2FClient, error) {
var err error
var tCreds credentials.TransportCredentials
if caFile == "" {
tCreds = credentials.NewClientTLSFromCert(nil, "")
} else {
tCreds, err = credentials.NewClientTLSFromFile(caFile, "")
if err != nil {
return nil, fmt.Errorf("error reading CA file: %s", err)
}
}
t := oauth2.Token{
AccessToken: token,
TokenType: "Bearer",
}
rpcCreds := oauth.NewOauthAccess(&t)
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(tCreds),
grpc.WithPerRPCCredentials(rpcCreds),
grpc.WithBlock(),
grpc.WithTimeout(30*time.Second))
if err != nil {
return nil, fmt.Errorf("error connecting to server: %s", err)
}
c := pb.NewRemoteU2FClient(conn)
return &RemoteU2FClient{c}, nil
}
示例2: newEventsClientConnectionWithAddress
//newEventsClientConnectionWithAddress Returns a new grpc.ClientConn to the configured local PEER.
func newEventsClientConnectionWithAddress(peerAddress string) (*grpc.ClientConn, error) {
var opts []grpc.DialOption
if peer.TLSEnabled() {
var sn string
if viper.GetString("peer.tls.serverhostoverride") != "" {
sn = viper.GetString("peer.tls.serverhostoverride")
}
var creds credentials.TransportAuthenticator
if viper.GetString("peer.tls.cert.file") != "" {
var err error
creds, err = credentials.NewClientTLSFromFile(viper.GetString("peer.tls.cert.file"), sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
} else {
creds = credentials.NewClientTLSFromCert(nil, sn)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
}
opts = append(opts, grpc.WithTimeout(defaultTimeout))
opts = append(opts, grpc.WithBlock())
opts = append(opts, grpc.WithInsecure())
return grpc.Dial(peerAddress, opts...)
}
示例3: DialGRPC
// DialGRPC returns a GRPC connection for use communicating with a Google cloud
// service, configured with the given ClientOptions.
func DialGRPC(ctx context.Context, opts ...option.ClientOption) (*grpc.ClientConn, error) {
var o internal.DialSettings
for _, opt := range opts {
opt.Apply(&o)
}
if o.HTTPClient != nil {
return nil, errors.New("unsupported HTTP client specified")
}
if o.GRPCConn != nil {
return o.GRPCConn, nil
}
if o.TokenSource == nil {
var err error
o.TokenSource, err = google.DefaultTokenSource(ctx, o.Scopes...)
if err != nil {
return nil, fmt.Errorf("google.DefaultTokenSource: %v", err)
}
}
grpcOpts := []grpc.DialOption{
grpc.WithPerRPCCredentials(oauth.TokenSource{o.TokenSource}),
grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
}
if appengineDialerHook != nil {
// Use the Socket API on App Engine.
grpcOpts = append(grpcOpts, appengineDialerHook(ctx))
}
grpcOpts = append(grpcOpts, o.GRPCDialOpts...)
if o.UserAgent != "" {
grpcOpts = append(grpcOpts, grpc.WithUserAgent(o.UserAgent))
}
return grpc.Dial(o.Endpoint, grpcOpts...)
}
示例4: NewContext
func NewContext() *Context {
log := log.New(os.Stderr, "QPM: ", log.LstdFlags)
address := os.Getenv("SERVER")
if address == "" {
address = Address
}
noTls := os.Getenv("NO_TLS") == "1"
var tlsOption grpc.DialOption
if noTls {
tlsOption = grpc.WithInsecure()
} else {
tlsOption = grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))
}
conn, err := grpc.Dial(address, tlsOption, grpc.WithUserAgent(UA))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
return &Context{
Log: log,
Client: msg.NewQpmClient(conn),
}
}
示例5: DialGRPC
// DialGRPC returns a GRPC connection for use communicating with a Google cloud
// service, configured with the given ClientOptions.
func DialGRPC(ctx context.Context, opt ...cloud.ClientOption) (*grpc.ClientConn, error) {
var o opts.DialOpt
for _, opt := range opt {
opt.Resolve(&o)
}
if o.HTTPClient != nil {
return nil, errors.New("unsupported HTTP base transport specified")
}
if o.GRPCClient != nil {
return o.GRPCClient, nil
}
if o.TokenSource == nil {
var err error
o.TokenSource, err = google.DefaultTokenSource(ctx, o.Scopes...)
if err != nil {
return nil, fmt.Errorf("google.DefaultTokenSource: %v", err)
}
}
grpcOpts := []grpc.DialOption{
grpc.WithPerRPCCredentials(oauth.TokenSource{o.TokenSource}),
grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
}
grpcOpts = append(grpcOpts, o.GRPCDialOpts...)
if o.UserAgent != "" {
grpcOpts = append(grpcOpts, grpc.WithUserAgent(o.UserAgent))
}
return grpc.Dial(o.Endpoint, grpcOpts...)
}
示例6: newPeerClientConnection
func newPeerClientConnection() (*grpc.ClientConn, error) {
var opts []grpc.DialOption
if viper.GetBool("peer.tls.enabled") {
var sn string
if viper.GetString("peer.tls.serverhostoverride") != "" {
sn = viper.GetString("peer.tls.serverhostoverride")
}
var creds credentials.TransportAuthenticator
if viper.GetString("peer.tls.cert.file") != "" {
var err error
creds, err = credentials.NewClientTLSFromFile(viper.GetString("peer.tls.cert.file"), sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
} else {
creds = credentials.NewClientTLSFromCert(nil, sn)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
}
opts = append(opts, grpc.WithTimeout(1*time.Second))
opts = append(opts, grpc.WithBlock())
opts = append(opts, grpc.WithInsecure())
conn, err := grpc.Dial(getPeerAddress(), opts...)
if err != nil {
return nil, err
}
return conn, err
}
示例7: getDevopsClient
func getDevopsClient(peerAddress string) (obc.DevopsClient, error) {
var opts []grpc.DialOption
if viper.GetBool("pki.validity-period.tls.enabled") {
var sn string
if viper.GetString("pki.validity-period.tls.server-host-override") != "" {
sn = viper.GetString("pki.validity-period.tls.server-host-override")
}
var creds credentials.TransportAuthenticator
if viper.GetString("pki.validity-period.tls.cert.file") != "" {
var err error
creds, err = credentials.NewClientTLSFromFile(viper.GetString("pki.validity-period.tls.cert.file"), sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
} else {
creds = credentials.NewClientTLSFromCert(nil, sn)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
}
opts = append(opts, grpc.WithTimeout(systemChaincodeTimeout))
opts = append(opts, grpc.WithBlock())
opts = append(opts, grpc.WithInsecure())
conn, err := grpc.Dial(peerAddress, opts...)
if err != nil {
return nil, fmt.Errorf("Error trying to connect to local peer: %s", err)
}
devopsClient := obc.NewDevopsClient(conn)
return devopsClient, nil
}
示例8: discovery
func discovery() {
var err error
var opts []grpc.DialOption
if insecureDiscovery {
opts = append(opts, grpc.WithInsecure())
} else {
auth := credentials.NewClientTLSFromCert(nil, getServerName(discoveryServiceURL))
opts = append(opts, grpc.WithTransportCredentials(auth))
}
discoveryConn, err = grpc.Dial(discoveryServiceURL, opts...)
if err != nil {
logrus.Fatalf("grpc.go: error while connection to discovery service %v", err)
}
discoveryClient = pb.NewDiscoveryClient(discoveryConn)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
res, err := discoveryClient.Get(ctx, &pb.DiscoveryRequest{
Environment: discoveryEnv,
SdkVersion: Version + "-otsimoctl",
OsName: runtime.GOOS,
})
if err != nil {
logrus.Fatalf("grpc:go: failed to get discovery config err=%v", err)
}
discoveryServices = res
}
示例9: TestNewSecureGRPCServer
func TestNewSecureGRPCServer(t *testing.T) {
testAddress := "localhost:9055"
srv, err := comm.NewGRPCServer(testAddress, []byte(selfSignedKeyPEM),
[]byte(selfSignedCertPEM), nil, nil)
//check for error
if err != nil {
t.Fatalf("Failed to return new GRPC server: %v", err)
}
//make sure our properties are as expected
//resolve the address
addr, err := net.ResolveTCPAddr("tcp", testAddress)
assert.Equal(t, srv.Address(), addr.String())
assert.Equal(t, srv.Listener().Addr().String(), addr.String())
//check the server certificate
cert, _ := tls.X509KeyPair([]byte(selfSignedCertPEM), []byte(selfSignedKeyPEM))
assert.Equal(t, srv.ServerCertificate(), cert)
//TlSEnabled should be true
assert.Equal(t, srv.TLSEnabled(), true)
//register the GRPC test server
testpb.RegisterTestServiceServer(srv.Server(), &testServiceServer{})
//start the server
go srv.Start()
defer srv.Stop()
//should not be needed
time.Sleep(10 * time.Millisecond)
//create the client credentials
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM([]byte(selfSignedCertPEM)) {
t.Fatal("Failed to append certificate to client credentials")
}
creds := credentials.NewClientTLSFromCert(certPool, "")
//GRPC client options
var dialOptions []grpc.DialOption
dialOptions = append(dialOptions, grpc.WithTransportCredentials(creds))
//invoke the EmptyCall service
_, err = invokeEmptyCall(testAddress, dialOptions)
if err != nil {
t.Logf("GRPC client failed to invoke the EmptyCall service on %s: %v",
testAddress, err)
} else {
t.Log("GRPC client successfully invoked the EmptyCall service: " + testAddress)
}
}
示例10: demoConn
func demoConn(gwid string) {
var opts []grpc.DialOption
var err error
if demoTLS {
var sn string
if demoOver != "" {
sn = demoOver
}
var creds credentials.TransportCredentials
if demoFile != "" {
creds, err = credentials.NewClientTLSFromFile(demoFile, sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
} else {
creds = credentials.NewClientTLSFromCert(nil, sn)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}
conn, err := grpc.Dial(demoAddr, opts...)
if err != nil {
grpclog.Fatalf("fail to dial: %v", err)
}
//defer demoCliConn.Close()
demoClient = demo.NewRPCClient(conn)
go func() {
for {
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
r, err := demoClient.GWSRPCCHECK(ctx, &demo.GWSRPCCHECKSEND{GWSRPCID: gwid})
if err != nil {
//grpclog.Printf("demoClient %v: ", err)
time.Sleep(100 * time.Millisecond)
continue
}
if r.GWSRPCID != demoID {
demoID = r.GWSRPCID
h.reinitall <- "demo"
}
time.Sleep(100 * time.Millisecond)
}
}()
}
示例11: main
func main() {
flag.Parse()
var opts []grpc.DialOption
// 如何使用: TLS呢?
if *tls {
var sn string
if *serverHostOverride != "" {
sn = *serverHostOverride
}
var creds credentials.TransportAuthenticator
if *caFile != "" {
var err error
creds, err = credentials.NewClientTLSFromFile(*caFile, sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
} else {
creds = credentials.NewClientTLSFromCert(nil, sn)
}
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
}
// 2. 连接服务器
conn, err := grpc.Dial(*serverAddr, opts...)
if err != nil {
grpclog.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := pb.NewRouteGuideClient(conn)
// Looking for a valid feature
printFeature(client, &pb.Point{409146138, -746188906})
// Feature missing.
printFeature(client, &pb.Point{0, 0})
// Looking for features between 40, -75 and 42, -73.
printFeatures(client, &pb.Rectangle{&pb.Point{400000000, -750000000}, &pb.Point{420000000, -730000000}})
// RecordRoute
runRecordRoute(client)
// RouteChat
runRouteChat(client)
}
示例12: discoveryGet
func discoveryGet(clictx *cli.Context) error {
var err error
var opts []grpc.DialOption
if insecureDiscovery {
opts = append(opts, grpc.WithInsecure())
} else {
auth := credentials.NewClientTLSFromCert(nil, getServerName(discoveryServiceURL))
opts = append(opts, grpc.WithTransportCredentials(auth))
}
logrus.Infof("discovery.go: connecting to discovery service at url %s", discoveryServiceURL)
dcon, err := grpc.Dial(discoveryServiceURL, opts...)
if err != nil {
logrus.Fatalf("discovery.go: error while connection to discovery service %v", err)
}
dc := pb.NewDiscoveryClient(dcon)
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
req := pb.DiscoveryRequest{}
if filename := clictx.String("filename"); filename != "" {
d, err := os.Open(filename)
if err != nil {
return err
}
defer d.Close()
if err := jsonpb.Unmarshal(d, &req); err != nil {
return err
}
} else {
req.Environment = clictx.String("env")
req.SdkVersion = clictx.String("sdk")
req.OsName = clictx.String("os")
req.CountryCode = clictx.String("country-code")
req.AppBundleId = clictx.String("bundle-id")
req.AppBundleVersion = clictx.String("bundle-version")
}
res, err := dc.Get(ctx, &req)
if err != nil {
return err
}
m := jsonpb.Marshaler{EnumsAsInts: false, EmitDefaults: true, Indent: " "}
s, err := m.MarshalToString(res)
fmt.Println(s)
return nil
}
示例13: InitTLSForPeer
// InitTLSForPeer returns TLS credentials for peer
func InitTLSForPeer() credentials.TransportCredentials {
var sn string
if viper.GetString("peer.tls.serverhostoverride") != "" {
sn = viper.GetString("peer.tls.serverhostoverride")
}
var creds credentials.TransportCredentials
if viper.GetString("peer.tls.cert.file") != "" {
var err error
creds, err = credentials.NewClientTLSFromFile(viper.GetString("peer.tls.cert.file"), sn)
if err != nil {
grpclog.Fatalf("Failed to create TLS credentials %v", err)
}
} else {
creds = credentials.NewClientTLSFromCert(nil, sn)
}
return creds
}
示例14: NewContext
func NewContext() *Context {
log := log.New(os.Stderr, "QPM: ", log.LstdFlags)
creds := credentials.NewClientTLSFromCert(nil, "")
address := os.Getenv("SERVER")
if address == "" {
address = Address
}
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds), grpc.WithUserAgent(UA))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
return &Context{
Log: log,
Client: msg.NewQpmClient(conn),
}
}
示例15: connectRegistry
func connectRegistry(c *Config) {
var opts []grpc.DialOption
jwtCreds := newOauthAccess(c)
if discoveryServices.UseTls {
auth := credentials.NewClientTLSFromCert(nil, getServerName(discoveryServices.RegistryGrpc))
opts = append(opts, grpc.WithTransportCredentials(auth))
} else {
jwtCreds.RequireTLS = false
opts = append(opts, grpc.WithInsecure())
}
opts = append(opts, grpc.WithPerRPCCredentials(&jwtCreds))
registryConn, err := grpc.Dial(discoveryServices.RegistryGrpc, opts...)
if err != nil {
logrus.Fatalf("grpc.go: Error while connection to registry service %v\n", err)
}
registryClient = pb.NewRegistryServiceClient(registryConn)
}