forked from hyperledger/fabric-sdk-go
/
connection.go
executable file
·151 lines (120 loc) · 4.3 KB
/
connection.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"crypto/x509"
"sync/atomic"
"github.com/pkg/errors"
"github.com/hyperledger/fabric-sdk-go/pkg/client/common/verifier"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
fabcontext "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/comm"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var logger = logging.NewLogger("fabsdk/fab")
const (
// GRPC max message size (same as Fabric)
maxCallRecvMsgSize = 100 * 1024 * 1024
maxCallSendMsgSize = 100 * 1024 * 1024
)
// GRPCConnection manages the GRPC connection and client stream
type GRPCConnection struct {
context fabcontext.Client
conn *grpc.ClientConn
commManager fab.CommManager
tlsCertHash []byte
done int32
}
// NewConnection creates a new connection
func NewConnection(ctx fabcontext.Client, url string, opts ...options.Opt) (*GRPCConnection, error) {
if url == "" {
return nil, errors.New("server URL not specified")
}
params := defaultParams()
options.Apply(params, opts)
dialOpts, err := newDialOpts(ctx.EndpointConfig(), url, params)
if err != nil {
return nil, err
}
reqCtx, cancel := context.NewRequest(ctx, context.WithTimeout(params.connectTimeout))
defer cancel()
commManager, ok := context.RequestCommManager(reqCtx)
if !ok {
return nil, errors.New("unable to get comm manager")
}
grpcconn, err := commManager.DialContext(reqCtx, endpoint.ToAddress(url), dialOpts...)
if err != nil {
return nil, errors.Wrapf(err, "could not connect to %s", url)
}
hash, err := comm.TLSCertHash(ctx.EndpointConfig())
if err != nil {
return nil, errors.Wrapf(err, "failed to get tls cert hash")
}
return &GRPCConnection{
context: ctx,
commManager: commManager,
conn: grpcconn,
tlsCertHash: hash,
}, nil
}
// ClientConn returns the underlying GRPC connection
func (c *GRPCConnection) ClientConn() *grpc.ClientConn {
return c.conn
}
// Close closes the connection
func (c *GRPCConnection) Close() {
if !c.setClosed() {
logger.Debug("Already closed")
return
}
logger.Debug("Releasing connection....")
c.commManager.ReleaseConn(c.conn)
logger.Debug("... connection successfully closed.")
}
// Closed returns true if the connection has been closed
func (c *GRPCConnection) Closed() bool {
return atomic.LoadInt32(&c.done) == 1
}
func (c *GRPCConnection) setClosed() bool {
return atomic.CompareAndSwapInt32(&c.done, 0, 1)
}
// TLSCertHash returns the hash of the TLS cert
func (c *GRPCConnection) TLSCertHash() []byte {
return c.tlsCertHash
}
// Context returns the context of the client establishing the connection
func (c *GRPCConnection) Context() fabcontext.Client {
return c.context
}
func newDialOpts(config fab.EndpointConfig, url string, params *params) ([]grpc.DialOption, error) {
var dialOpts []grpc.DialOption
if params.keepAliveParams.Time > 0 || params.keepAliveParams.Timeout > 0 {
dialOpts = append(dialOpts, grpc.WithKeepaliveParams(params.keepAliveParams))
}
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.FailFast(params.failFast)))
if endpoint.AttemptSecured(url, params.insecure) {
tlsConfig, err := comm.TLSConfig(params.certificate, params.hostOverride, config)
if err != nil {
return nil, err
}
//verify if certificate was expired or not yet valid
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return verifier.VerifyPeerCertificate(rawCerts, verifiedChains)
}
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
logger.Debugf("Creating a secure connection to [%s] with TLS HostOverride [%s]", url, params.hostOverride)
} else {
logger.Debugf("Creating an insecure connection [%s]", url)
dialOpts = append(dialOpts, grpc.WithInsecure())
}
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxCallRecvMsgSize),
grpc.MaxCallSendMsgSize(maxCallSendMsgSize)))
return dialOpts, nil
}