Skip to content

Commit a1c6702

Browse files
committed
[FAB-4002] Upgrade to latest version of grpc-go
There have been many updates to the grpc-go package since it was vendored in v0.6 and then v1.0.0. There are several defects / issues related to inactivity timeouts, connectivity, etc which can be more easily fixed using new features such as the client keepalive. This CR updates grpc-go as well as all of its dependencies and makes minor updates to the code where required as well as recompiles all of the protos using the protobuf compiler plugin required by this version of grpc-go. Note that for the updated vendoring, I chose to delete the old packages and add the updated ones as govendor is not perfect when it comes to trying to update packages. The only "new" code was adding custom server transport credentials in the comm package as the default provided by the grpc/credentials package was changed and breaks the functionality required for dynamically updating root certs for running grpc servers and new package config values for send/receive msg sizes as grpc-go now enforces them. Changes were also made to any grpc clients and servers used in the code to leverage the new config values. There is one TODO which is to actually set the max send/receive msg sizes based on config. This will need to be done for both the peer and the orderer. Also changed the license header to the new format for any new or modified files Change-Id: I41687a54aa21802203b04a339952881349042b70 Signed-off-by: Gari Singh <[email protected]>
1 parent c520e53 commit a1c6702

File tree

314 files changed

+12872
-48429
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

314 files changed

+12872
-48429
lines changed

core/comm/config.go

+39-31
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package comm
@@ -20,27 +10,21 @@ import (
2010
"github.com/spf13/viper"
2111
)
2212

23-
// Is the configuration cached?
24-
var configurationCached = false
25-
26-
// Cached values of commonly used configuration constants.
27-
var tlsEnabled bool
28-
29-
// CacheConfiguration computes and caches commonly-used constants and
30-
// computed constants as package variables. Routines which were previously
31-
func CacheConfiguration() (err error) {
32-
33-
tlsEnabled = viper.GetBool("peer.tls.enabled")
34-
35-
configurationCached = true
36-
37-
return
38-
}
13+
var (
14+
// Is the configuration cached?
15+
configurationCached = false
16+
// Is TLS enabled
17+
tlsEnabled bool
18+
// Max send and receive bytes for grpc clients and servers
19+
maxRecvMsgSize = 100 * 1024 * 1024
20+
maxSendMsgSize = 100 * 1024 * 1024
21+
)
3922

40-
// cacheConfiguration logs an error if error checks have failed.
23+
// cacheConfiguration caches common package scoped variables
4124
func cacheConfiguration() {
42-
if err := CacheConfiguration(); err != nil {
43-
commLogger.Errorf("Execution continues after CacheConfiguration() failure : %s", err)
25+
if !configurationCached {
26+
tlsEnabled = viper.GetBool("peer.tls.enabled")
27+
configurationCached = true
4428
}
4529
}
4630

@@ -51,3 +35,27 @@ func TLSEnabled() bool {
5135
}
5236
return tlsEnabled
5337
}
38+
39+
// MaxRecvMsgSize returns the maximum message size in bytes that gRPC clients
40+
// and servers can receive
41+
func MaxRecvMsgSize() int {
42+
return maxRecvMsgSize
43+
}
44+
45+
// SetMaxRecvMsgSize sets the maximum message size in bytes that gRPC clients
46+
// and servers can receive
47+
func SetMaxRecvMsgSize(size int) {
48+
maxRecvMsgSize = size
49+
}
50+
51+
// MaxSendMsgSize returns the maximum message size in bytes that gRPC clients
52+
// and servers can send
53+
func MaxSendMsgSize() int {
54+
return maxSendMsgSize
55+
}
56+
57+
// SetMaxSendMsgSize sets the maximum message size in bytes that gRPC clients
58+
// and servers can send
59+
func SetMaxSendMsgSize(size int) {
60+
maxSendMsgSize = size
61+
}

core/comm/config_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package comm
8+
9+
import (
10+
"testing"
11+
12+
"github.com/spf13/viper"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestConfig(t *testing.T) {
17+
// check the defaults
18+
assert.EqualValues(t, maxRecvMsgSize, MaxRecvMsgSize())
19+
assert.EqualValues(t, maxSendMsgSize, MaxSendMsgSize())
20+
assert.EqualValues(t, false, TLSEnabled())
21+
assert.EqualValues(t, true, configurationCached)
22+
23+
// set send/recv msg sizes
24+
size := 10 * 1024 * 1024
25+
SetMaxRecvMsgSize(size)
26+
SetMaxSendMsgSize(size)
27+
assert.EqualValues(t, size, MaxRecvMsgSize())
28+
assert.EqualValues(t, size, MaxSendMsgSize())
29+
30+
// reset cache
31+
configurationCached = false
32+
viper.Set("peer.tls.enabled", true)
33+
assert.EqualValues(t, true, TLSEnabled())
34+
// check that value is cached
35+
viper.Set("peer.tls.enabled", false)
36+
assert.NotEqual(t, false, TLSEnabled())
37+
// reset tls
38+
configurationCached = false
39+
viper.Set("peer.tls.enabled", false)
40+
}

core/comm/connection.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package comm
@@ -185,6 +175,8 @@ func NewClientConnectionWithAddress(peerAddress string, block bool, tslEnabled b
185175
if block {
186176
opts = append(opts, grpc.WithBlock())
187177
}
178+
opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(MaxRecvMsgSize()),
179+
grpc.MaxCallSendMsgSize(MaxSendMsgSize())))
188180
conn, err := grpc.Dial(peerAddress, opts...)
189181
if err != nil {
190182
return nil, err

core/comm/creds.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package comm
8+
9+
import (
10+
"crypto/tls"
11+
"errors"
12+
"net"
13+
14+
"golang.org/x/net/context"
15+
"google.golang.org/grpc/credentials"
16+
)
17+
18+
var (
19+
ClientHandshakeNotImplError = errors.New("core/comm: Client handshakes" +
20+
"are not implemented with serverCreds")
21+
OverrrideHostnameNotSupportedError = errors.New(
22+
"core/comm: OverrideServerName is " +
23+
"not supported")
24+
MissingServerConfigError = errors.New(
25+
"core/comm: `serverConfig` cannot be nil")
26+
// alpnProtoStr are the specified application level protocols for gRPC.
27+
alpnProtoStr = []string{"h2"}
28+
)
29+
30+
// NewServerTransportCredentials returns a new initialized
31+
// grpc/credentials.TransportCredentials
32+
func NewServerTransportCredentials(serverConfig *tls.Config) credentials.TransportCredentials {
33+
// NOTE: unlike the default grpc/credentials implementation, we do not
34+
// clone the tls.Config which allows us to update it dynamically
35+
serverConfig.NextProtos = alpnProtoStr
36+
return &serverCreds{serverConfig}
37+
}
38+
39+
// serverCreds is an implementation of grpc/credentials.TransportCredentials.
40+
type serverCreds struct {
41+
serverConfig *tls.Config
42+
}
43+
44+
// ClientHandShake is not implemented for `serverCreds`.
45+
func (sc *serverCreds) ClientHandshake(context.Context,
46+
string, net.Conn) (net.Conn, credentials.AuthInfo, error) {
47+
return nil, nil, ClientHandshakeNotImplError
48+
}
49+
50+
// ServerHandshake does the authentication handshake for servers.
51+
func (sc *serverCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
52+
conn := tls.Server(rawConn, sc.serverConfig)
53+
if err := conn.Handshake(); err != nil {
54+
return nil, nil, err
55+
}
56+
return conn, credentials.TLSInfo{conn.ConnectionState()}, nil
57+
}
58+
59+
// Info provides the ProtocolInfo of this TransportCredentials.
60+
func (sc *serverCreds) Info() credentials.ProtocolInfo {
61+
return credentials.ProtocolInfo{
62+
SecurityProtocol: "tls",
63+
SecurityVersion: "1.2",
64+
}
65+
}
66+
67+
// Clone makes a copy of this TransportCredentials.
68+
func (sc *serverCreds) Clone() credentials.TransportCredentials {
69+
creds := NewServerTransportCredentials(sc.serverConfig)
70+
return creds
71+
}
72+
73+
// OverrideServerName overrides the server name used to verify the hostname
74+
// on the returned certificates from the server.
75+
func (sc *serverCreds) OverrideServerName(string) error {
76+
return OverrrideHostnameNotSupportedError
77+
}

core/comm/creds_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package comm_test
8+
9+
import (
10+
"crypto/tls"
11+
"testing"
12+
13+
"google.golang.org/grpc/credentials"
14+
15+
"github.com/hyperledger/fabric/core/comm"
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
func TestCreds(t *testing.T) {
20+
var creds credentials.TransportCredentials
21+
creds = comm.NewServerTransportCredentials(&tls.Config{})
22+
_, _, err := creds.ClientHandshake(nil, "", nil)
23+
assert.EqualError(t, err, comm.ClientHandshakeNotImplError.Error())
24+
err = creds.OverrideServerName("")
25+
assert.EqualError(t, err, comm.OverrrideHostnameNotSupportedError.Error())
26+
clone := creds.Clone()
27+
assert.Equal(t, creds, clone)
28+
assert.Equal(t, "1.2", creds.Info().SecurityVersion)
29+
assert.Equal(t, "tls", creds.Info().SecurityProtocol)
30+
}

core/comm/server.go

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package comm
@@ -26,7 +16,6 @@ import (
2616
"sync"
2717

2818
"google.golang.org/grpc"
29-
"google.golang.org/grpc/credentials"
3019
)
3120

3221
//A SecureServerConfig structure is used to configure security (e.g. TLS) for a
@@ -168,17 +157,17 @@ func NewGRPCServerFromListener(listener net.Listener, secureConfig SecureServerC
168157
}
169158
}
170159

171-
//create credentials
172-
creds := credentials.NewTLS(grpcServer.tlsConfig)
173-
174-
//add to server options
160+
// create credentials and add to server options
161+
creds := NewServerTransportCredentials(grpcServer.tlsConfig)
175162
serverOpts = append(serverOpts, grpc.Creds(creds))
176-
177163
} else {
178164
return nil, errors.New("secureConfig must contain both ServerKey and " +
179165
"ServerCertificate when UseTLS is true")
180166
}
181167
}
168+
// set max send and recv msg sizes
169+
serverOpts = append(serverOpts, grpc.MaxSendMsgSize(MaxSendMsgSize()))
170+
serverOpts = append(serverOpts, grpc.MaxRecvMsgSize(MaxRecvMsgSize()))
182171
grpcServer.server = grpc.NewServer(serverOpts...)
183172

184173
return grpcServer, nil

core/comm/server_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,7 @@ func TestWithSignedRootCertificates(t *testing.T) {
768768
_, err = invokeEmptyCall(testAddress, dialOptions)
769769

770770
//client should not be able to connect
771-
//for now we can only test that we get a timeout error
772-
assert.EqualError(t, err, grpc.ErrClientConnTimeout.Error())
771+
assert.EqualError(t, err, x509.UnknownAuthorityError{}.Error())
773772
t.Logf("assert.EqualError: %s", err.Error())
774773

775774
//now use the CA certificate
@@ -849,8 +848,7 @@ func TestWithSignedIntermediateCertificates(t *testing.T) {
849848
_, err = invokeEmptyCall(testAddress, dialOptions)
850849

851850
//client should not be able to connect
852-
//for now we can only test that we get a timeout error
853-
assert.EqualError(t, err, grpc.ErrClientConnTimeout.Error())
851+
assert.EqualError(t, err, x509.UnknownAuthorityError{}.Error())
854852
t.Logf("assert.EqualError: %s", err.Error())
855853

856854
//now use the CA certificate

core/comm/testdata/grpc/test.pb.go

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/deliverservice/client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func newConnection() *grpc.ClientConn {
5050
type balancer struct {
5151
}
5252

53-
func (*balancer) Start(target string) error {
53+
func (*balancer) Start(target string, config grpc.BalancerConfig) error {
5454
return nil
5555
}
5656

0 commit comments

Comments
 (0)