Skip to content

Commit f709314

Browse files
committed
[FAB-7044] Refactor gRPC server config code
When creating a new core/comm#GRPCServer instance, the only options which are publicly exposed are those related to security. All other config such as keepalive and message size settings are hard-coded and/or set internally. This change creates a new top-level ServerConfig struct which will be used in future changesets to hold various configuration options. Initially it holds SecureOptions (which used to be SecureServerConfig). Change-Id: Ie6cd8c74ea9579504eb2bcba384498fede386cd7 Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
1 parent f824697 commit f709314

File tree

14 files changed

+226
-208
lines changed

14 files changed

+226
-208
lines changed

core/comm/config.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ var (
3939
}
4040
)
4141

42+
// ServerConfig defines the parameters for configuring a GRPCServer instance
43+
type ServerConfig struct {
44+
// SecOpts defines the security parameters
45+
SecOpts *SecureOptions
46+
// KaOpts defines the keepalive parameters
47+
KaOpts *KeepaliveOptions
48+
}
49+
50+
// SecureOptions defines the security parameters (e.g. TLS) for a
51+
// GRPCServer instance
52+
type SecureOptions struct {
53+
//PEM-encoded X509 public key to be used by the server for TLS communication
54+
ServerCertificate []byte
55+
//PEM-encoded private key to be used by the server for TLS communication
56+
ServerKey []byte
57+
//Set of PEM-encoded X509 certificate authorities to optionally send
58+
//as part of the server handshake
59+
ServerRootCAs [][]byte
60+
//Set of PEM-encoded X509 certificate authorities to use when verifying
61+
//client certificates
62+
ClientRootCAs [][]byte
63+
//Whether or not to use TLS for communication
64+
UseTLS bool
65+
//Whether or not TLS client must present certificates for authentication
66+
RequireClientCert bool
67+
}
68+
4269
// KeepAliveOptions is used to set the gRPC keepalive settings for both
4370
// clients and servers
4471
type KeepaliveOptions struct {

core/comm/connection_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,12 @@ func newServer(org string, port int) *srv {
258258
if err != nil {
259259
panic(fmt.Errorf("Failed listening on port %d: %v", port, err))
260260
}
261-
gSrv, err := NewGRPCServerFromListener(l, SecureServerConfig{
262-
ServerCertificate: certs["server.crt"],
263-
ServerKey: certs["server.key"],
264-
UseTLS: true,
261+
gSrv, err := NewGRPCServerFromListener(l, ServerConfig{
262+
SecOpts: &SecureOptions{
263+
ServerCertificate: certs["server.crt"],
264+
ServerKey: certs["server.key"],
265+
UseTLS: true,
266+
},
265267
})
266268
if err != nil {
267269
panic(fmt.Errorf("Failed starting gRPC server: %v", err))

core/comm/server.go

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,6 @@ import (
1717
"google.golang.org/grpc"
1818
)
1919

20-
//A SecureServerConfig structure is used to configure security (e.g. TLS) for a
21-
//GRPCServer instance
22-
type SecureServerConfig struct {
23-
//PEM-encoded X509 public key to be used by the server for TLS communication
24-
ServerCertificate []byte
25-
//PEM-encoded private key to be used by the server for TLS communication
26-
ServerKey []byte
27-
//Set of PEM-encoded X509 certificate authorities to optionally send
28-
//as part of the server handshake
29-
ServerRootCAs [][]byte
30-
//Set of PEM-encoded X509 certificate authorities to use when verifying
31-
//client certificates
32-
ClientRootCAs [][]byte
33-
//Whether or not to use TLS for communication
34-
UseTLS bool
35-
//Whether or not TLS client must present certificates for authentication
36-
RequireClientCert bool
37-
}
38-
3920
//GRPCServer defines an interface representing a GRPC-based server
4021
type GRPCServer interface {
4122
//Address returns the listen address for the GRPCServer
@@ -96,25 +77,25 @@ type grpcServerImpl struct {
9677

9778
//NewGRPCServer creates a new implementation of a GRPCServer given a
9879
//listen address
99-
func NewGRPCServer(address string, secureConfig SecureServerConfig) (GRPCServer, error) {
100-
return newGRPCServerWithKa(address, secureConfig, &keepaliveOptions)
80+
func NewGRPCServer(address string, serverConfig ServerConfig) (GRPCServer, error) {
81+
return newGRPCServerWithKa(address, serverConfig, &keepaliveOptions)
10182
}
10283

10384
//NewChaincodeGRPCServer creates a new implementation of a chaincode GRPCServer given a
10485
//listen address
105-
func NewChaincodeGRPCServer(address string, secureConfig SecureServerConfig) (GRPCServer, error) {
106-
return newGRPCServerWithKa(address, secureConfig, &chaincodeKeepaliveOptions)
86+
func NewChaincodeGRPCServer(address string, serverConfig ServerConfig) (GRPCServer, error) {
87+
return newGRPCServerWithKa(address, serverConfig, &chaincodeKeepaliveOptions)
10788
}
10889

10990
//NewGRPCServerFromListener creates a new implementation of a GRPCServer given
11091
//an existing net.Listener instance using default keepalive
111-
func NewGRPCServerFromListener(listener net.Listener, secureConfig SecureServerConfig) (GRPCServer, error) {
112-
return newGRPCServerFromListenerWithKa(listener, secureConfig, &keepaliveOptions)
92+
func NewGRPCServerFromListener(listener net.Listener, serverConfig ServerConfig) (GRPCServer, error) {
93+
return newGRPCServerFromListenerWithKa(listener, serverConfig, &keepaliveOptions)
11394
}
11495

11596
//newGRPCServerWithKa creates a new implementation of a GRPCServer given a
11697
//listen address with specified keepalive options
117-
func newGRPCServerWithKa(address string, secureConfig SecureServerConfig, ka *KeepaliveOptions) (GRPCServer, error) {
98+
func newGRPCServerWithKa(address string, serverConfig ServerConfig, ka *KeepaliveOptions) (GRPCServer, error) {
11899

119100
if address == "" {
120101
return nil, errors.New("Missing address parameter")
@@ -126,13 +107,14 @@ func newGRPCServerWithKa(address string, secureConfig SecureServerConfig, ka *Ke
126107
return nil, err
127108
}
128109

129-
return newGRPCServerFromListenerWithKa(lis, secureConfig, ka)
110+
return newGRPCServerFromListenerWithKa(lis, serverConfig, ka)
130111

131112
}
132113

133114
//newGRPCServerFromListenerWithKa creates a new implementation of a GRPCServer given
134115
//an existing net.Listener instance with specfied keepalive
135-
func newGRPCServerFromListenerWithKa(listener net.Listener, secureConfig SecureServerConfig, ka *KeepaliveOptions) (GRPCServer, error) {
116+
func newGRPCServerFromListenerWithKa(listener net.Listener, serverConfig ServerConfig,
117+
ka *KeepaliveOptions) (GRPCServer, error) {
136118
grpcServer := &grpcServerImpl{
137119
address: listener.Addr().String(),
138120
listener: listener,
@@ -141,8 +123,9 @@ func newGRPCServerFromListenerWithKa(listener net.Listener, secureConfig SecureS
141123

142124
//set up our server options
143125
var serverOpts []grpc.ServerOption
144-
//check secureConfig
145-
if secureConfig.UseTLS {
126+
//check SecOpts
127+
secureConfig := serverConfig.SecOpts
128+
if secureConfig != nil && secureConfig.UseTLS {
146129
//both key and cert are required
147130
if secureConfig.ServerKey != nil && secureConfig.ServerCertificate != nil {
148131
grpcServer.tlsEnabled = true
@@ -184,7 +167,7 @@ func newGRPCServerFromListenerWithKa(listener net.Listener, secureConfig SecureS
184167
creds := NewServerTransportCredentials(grpcServer.tlsConfig)
185168
serverOpts = append(serverOpts, grpc.Creds(creds))
186169
} else {
187-
return nil, errors.New("secureConfig must contain both ServerKey and " +
170+
return nil, errors.New("serverConfig.SecOpts must contain both ServerKey and " +
188171
"ServerCertificate when UseTLS is true")
189172
}
190173
}

0 commit comments

Comments
 (0)