本文整理汇总了Golang中google/golang.org/grpc/credentials.NewClientTLSFromCert函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClientTLSFromCert函数的具体用法?Golang NewClientTLSFromCert怎么用?Golang NewClientTLSFromCert使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClientTLSFromCert函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:albertito,项目名称:remoteu2f,代码行数:29,代码来源:grpc.go
示例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...)
}
开发者ID:magooster,项目名称:obc-peer,代码行数:26,代码来源:consumer.go
示例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...)
}
开发者ID:Ropes,项目名称:pubbing,代码行数:34,代码来源:dial.go
示例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),
}
}
开发者ID:Cutehacks,项目名称:qpm,代码行数:28,代码来源:context.go
示例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...)
}
开发者ID:otsimo,项目名称:watch,代码行数:30,代码来源:dial.go
示例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
}
开发者ID:magooster,项目名称:obc-peer,代码行数:28,代码来源:chaincode.go
示例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
}
开发者ID:tenc,项目名称:obc-peer-pre-public,代码行数:31,代码来源:validity_period.go
示例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
}
开发者ID:otsimo,项目名称:otsimoctl,代码行数:26,代码来源:grpc.go
示例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)
}
}
开发者ID:hyperledger,项目名称:fabric,代码行数:57,代码来源:server_test.go
示例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)
}
}()
}
开发者ID:vanishs,项目名称:gwsrpc,代码行数:54,代码来源:gengateway.go
示例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)
}
开发者ID:wfxiang08,项目名称:grpc-go,代码行数:48,代码来源:client.go
示例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
}
开发者ID:otsimo,项目名称:otsimoctl,代码行数:46,代码来源:discovery.go
示例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
}
开发者ID:hyperledger,项目名称:fabric,代码行数:18,代码来源:connection.go
示例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),
}
}
开发者ID:benlau,项目名称:qpm,代码行数:18,代码来源:context.go
示例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)
}
开发者ID:otsimo,项目名称:otsimoctl,代码行数:19,代码来源:grpc.go
示例16: Init
func (g *grpcResolver) Init() error {
var err error
var creds credentials.TransportCredentials
if g.CAFile == "" {
creds = credentials.NewClientTLSFromCert(nil, "")
} else {
creds, err = credentials.NewClientTLSFromFile(g.CAFile, "")
if err != nil {
return err
}
}
conn, err := grpc.Dial(g.Upstream, grpc.WithTransportCredentials(creds))
if err != nil {
return err
}
g.client = pb.NewDNSServiceClient(conn)
return nil
}
开发者ID:albertito,项目名称:dnss,代码行数:20,代码来源:resolver.go
示例17: DialGRPC
func DialGRPC(ctx context.Context, opts ...ClientOption) (*grpc.ClientConn, error) {
settings := &ClientSettings{}
clientOptions(opts).Resolve(settings)
if settings.Connection != nil {
return settings.Connection, nil
}
var dialOpts = settings.DialOptions
if len(dialOpts) == 0 {
tokenSource, err := google.DefaultTokenSource(ctx, settings.Scopes...)
if err != nil {
return nil, fmt.Errorf("google.DefaultTokenSource: %v", err)
}
dialOpts = []grpc.DialOption{
grpc.WithPerRPCCredentials(oauth.TokenSource{TokenSource: tokenSource}),
grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
}
}
return grpc.Dial(settings.Endpoint, dialOpts...)
}
开发者ID:vburenin,项目名称:firempq,代码行数:20,代码来源:dial.go
示例18: newConn
func newConn(address string, useTLS, testCA bool, tlsServerName string) (*grpc.ClientConn, error) {
var opts []grpc.DialOption
if useTLS {
var sn string
if tlsServerName != "" {
sn = tlsServerName
}
var creds credentials.TransportCredentials
if testCA {
var err error
creds, err = credentials.NewClientTLSFromFile(testCAFile, 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())
}
return grpc.Dial(address, opts...)
}
开发者ID:tamird,项目名称:grpc-go,代码行数:23,代码来源:main.go
示例19: ConnectToServices
func (c *OtsimoAccounts) ConnectToServices(dexServiceUrl, apiServiceUrl string) {
jwtCreds := NewOauthAccess(c.tm)
var opts []grpc.DialOption
if c.roots != nil {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(c.roots, "")))
} else {
jwtCreds.RequireTLS = false
opts = append(opts, grpc.WithInsecure())
}
opts = append(opts, grpc.WithPerRPCCredentials(&jwtCreds))
conn, err := grpc.Dial(dexServiceUrl, opts...)
if err != nil {
log.Fatalf("Error while connection to dex service %v\n", err)
}
c.Dex = pb.NewDexServiceClient(conn)
apiConn, err := grpc.Dial(apiServiceUrl, opts...)
if err != nil {
log.Fatalf("Error while connection to api service %v\n", err)
}
c.Api = apipb.NewApiServiceClient(apiConn)
}
开发者ID:otsimo,项目名称:accounts,代码行数:23,代码来源:accounts.go
示例20: DialGRPC
// DialGRPC returns a GRPC connection for use communicating with a Google cloud
// service, configured with the given ClientOptions. Most developers should
// call the relevant NewClient method for the target service rather than
// invoking DialGRPC directly.
func DialGRPC(ctx context.Context, opt ...ClientOption) (*grpc.ClientConn, error) {
var o 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(credentials.TokenSource{o.tokenSource}),
grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")),
}
return grpc.Dial(o.endpoint, grpcOpts...)
}
开发者ID:rizzles,项目名称:convert,代码行数:28,代码来源:option.go
注:本文中的google/golang.org/grpc/credentials.NewClientTLSFromCert函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论