/
grpc.go
60 lines (50 loc) · 1.55 KB
/
grpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package client
import (
"crypto/tls"
"io/ioutil"
"time"
"github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/core/comm"
"github.com/pkg/errors"
)
const DefaultConnectionTimeout = 10 * time.Second
// CreateGRPCClient returns a comm.GRPCClient based on toke client config
func CreateGRPCClient(config *ConnectionConfig) (*comm.GRPCClient, error) {
timeout := config.ConnectionTimeout
if timeout <= 0 {
timeout = DefaultConnectionTimeout
}
clientConfig := comm.ClientConfig{Timeout: timeout}
if config.TLSEnabled {
if config.TLSRootCertFile == "" {
return nil, errors.New("missing TLSRootCertFile in client config")
}
caPEM, err := ioutil.ReadFile(config.TLSRootCertFile)
if err != nil {
return nil, errors.WithMessagef(err, "unable to load TLS cert from %s", config.TLSRootCertFile)
}
secOpts := &comm.SecureOptions{
UseTLS: true,
ServerRootCAs: [][]byte{caPEM},
RequireClientCert: false,
}
clientConfig.SecOpts = secOpts
}
return comm.NewGRPCClient(clientConfig)
}
// GetTLSCertHash computes SHA2-256 on tls certificate
func GetTLSCertHash(cert *tls.Certificate) ([]byte, error) {
if cert == nil || len(cert.Certificate) == 0 {
return nil, nil
}
tlsCertHash, err := factory.GetDefault().Hash(cert.Certificate[0], &bccsp.SHA256Opts{})
if err != nil {
return nil, errors.WithMessage(err, "failed to compute SHA256 on client certificate")
}
return tlsCertHash, nil
}