@@ -14,11 +14,13 @@ import (
14
14
_ "net/http/pprof" // This is essentially the main package for the orderer
15
15
"os"
16
16
17
+ "github.com/hyperledger/fabric/common/channelconfig"
17
18
"github.com/hyperledger/fabric/common/crypto"
18
19
"github.com/hyperledger/fabric/common/flogging"
19
20
"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
20
21
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
21
22
"github.com/hyperledger/fabric/core/comm"
23
+ "github.com/hyperledger/fabric/msp"
22
24
"github.com/hyperledger/fabric/orderer/common/bootstrap/file"
23
25
"github.com/hyperledger/fabric/orderer/common/ledger"
24
26
"github.com/hyperledger/fabric/orderer/common/localconfig"
@@ -75,14 +77,27 @@ func Main() {
75
77
// Start provides a layer of abstraction for benchmark test
76
78
func Start (cmd string , conf * config.TopLevel ) {
77
79
signer := localmsp .NewSigner ()
78
- manager := initializeMultichannelRegistrar (conf , signer )
80
+ secureConfig := initializeSecureServerConfig (conf )
81
+ grpcServer := initializeGrpcServer (conf , secureConfig )
82
+ caSupport := & comm.CASupport {
83
+ AppRootCAsByChain : make (map [string ][][]byte ),
84
+ OrdererRootCAsByChain : make (map [string ][][]byte ),
85
+ ClientRootCAs : secureConfig .ClientRootCAs ,
86
+ }
87
+ tlsCallback := func (bundle * channelconfig.Bundle ) {
88
+ // only need to do this if mutual TLS is required
89
+ if grpcServer .MutualTLSRequired () {
90
+ logger .Debug ("Executing callback to update root CAs" )
91
+ updateTrustedRoots (grpcServer , caSupport , bundle )
92
+ }
93
+ }
94
+ manager := initializeMultichannelRegistrar (conf , signer , tlsCallback )
79
95
server := NewServer (manager , signer , & conf .Debug )
80
96
81
97
switch cmd {
82
98
case start .FullCommand (): // "start" command
83
99
logger .Infof ("Starting %s" , metadata .GetVersionInfo ())
84
100
initializeProfilingService (conf )
85
- grpcServer := initializeGrpcServer (conf )
86
101
ab .RegisterAtomicBroadcastServer (grpcServer .Server (), server )
87
102
logger .Info ("Beginning to serve requests" )
88
103
grpcServer .Start ()
@@ -119,7 +134,7 @@ func initializeSecureServerConfig(conf *config.TopLevel) comm.SecureServerConfig
119
134
}
120
135
// check to see if TLS is enabled
121
136
if secureConfig .UseTLS {
122
- logger . Info ( "Starting orderer with TLS enabled" )
137
+ msg := " TLS"
123
138
// load crypto material from files
124
139
serverCertificate , err := ioutil .ReadFile (conf .General .TLS .Certificate )
125
140
if err != nil {
@@ -149,11 +164,13 @@ func initializeSecureServerConfig(conf *config.TopLevel) comm.SecureServerConfig
149
164
}
150
165
clientRootCAs = append (clientRootCAs , root )
151
166
}
167
+ msg = "mutual TLS"
152
168
}
153
169
secureConfig .ServerKey = serverKey
154
170
secureConfig .ServerCertificate = serverCertificate
155
171
secureConfig .ServerRootCAs = serverRootCAs
156
172
secureConfig .ClientRootCAs = clientRootCAs
173
+ logger .Infof ("Starting orderer with %s enabled" , msg )
157
174
}
158
175
return secureConfig
159
176
}
@@ -186,9 +203,7 @@ func initializeBootstrapChannel(conf *config.TopLevel, lf ledger.Factory) {
186
203
}
187
204
}
188
205
189
- func initializeGrpcServer (conf * config.TopLevel ) comm.GRPCServer {
190
- secureConfig := initializeSecureServerConfig (conf )
191
-
206
+ func initializeGrpcServer (conf * config.TopLevel , secureConfig comm.SecureServerConfig ) comm.GRPCServer {
192
207
lis , err := net .Listen ("tcp" , fmt .Sprintf ("%s:%d" , conf .General .ListenAddress , conf .General .ListenPort ))
193
208
if err != nil {
194
209
logger .Fatal ("Failed to listen:" , err )
@@ -211,7 +226,8 @@ func initializeLocalMsp(conf *config.TopLevel) {
211
226
}
212
227
}
213
228
214
- func initializeMultichannelRegistrar (conf * config.TopLevel , signer crypto.LocalSigner ) * multichannel.Registrar {
229
+ func initializeMultichannelRegistrar (conf * config.TopLevel , signer crypto.LocalSigner ,
230
+ callbacks ... func (bundle * channelconfig.Bundle )) * multichannel.Registrar {
215
231
lf , _ := createLedgerFactory (conf )
216
232
// Are we bootstrapping?
217
233
if len (lf .ChainIDs ()) == 0 {
@@ -224,5 +240,92 @@ func initializeMultichannelRegistrar(conf *config.TopLevel, signer crypto.LocalS
224
240
consenters ["solo" ] = solo .New ()
225
241
consenters ["kafka" ] = kafka .New (conf .Kafka )
226
242
227
- return multichannel .NewRegistrar (lf , consenters , signer )
243
+ return multichannel .NewRegistrar (lf , consenters , signer , callbacks ... )
244
+ }
245
+
246
+ func updateTrustedRoots (srv comm.GRPCServer , rootCASupport * comm.CASupport ,
247
+ cm channelconfig.Resources ) {
248
+ rootCASupport .Lock ()
249
+ defer rootCASupport .Unlock ()
250
+
251
+ appRootCAs := [][]byte {}
252
+ ordererRootCAs := [][]byte {}
253
+ appOrgMSPs := make (map [string ]struct {})
254
+ ordOrgMSPs := make (map [string ]struct {})
255
+
256
+ if ac , ok := cm .ApplicationConfig (); ok {
257
+ //loop through app orgs and build map of MSPIDs
258
+ for _ , appOrg := range ac .Organizations () {
259
+ appOrgMSPs [appOrg .MSPID ()] = struct {}{}
260
+ }
261
+ }
262
+
263
+ if ac , ok := cm .OrdererConfig (); ok {
264
+ //loop through orderer orgs and build map of MSPIDs
265
+ for _ , ordOrg := range ac .Organizations () {
266
+ ordOrgMSPs [ordOrg .MSPID ()] = struct {}{}
267
+ }
268
+ }
269
+
270
+ cid := cm .ConfigtxManager ().ChainID ()
271
+ logger .Debugf ("updating root CAs for channel [%s]" , cid )
272
+ msps , err := cm .MSPManager ().GetMSPs ()
273
+ if err != nil {
274
+ logger .Errorf ("Error getting root CAs for channel %s (%s)" , cid , err )
275
+ }
276
+ if err == nil {
277
+ for k , v := range msps {
278
+ // check to see if this is a FABRIC MSP
279
+ if v .GetType () == msp .FABRIC {
280
+ for _ , root := range v .GetTLSRootCerts () {
281
+ // check to see of this is an app org MSP
282
+ if _ , ok := appOrgMSPs [k ]; ok {
283
+ logger .Debugf ("adding app root CAs for MSP [%s]" , k )
284
+ appRootCAs = append (appRootCAs , root )
285
+ }
286
+ // check to see of this is an orderer org MSP
287
+ if _ , ok := ordOrgMSPs [k ]; ok {
288
+ logger .Debugf ("adding orderer root CAs for MSP [%s]" , k )
289
+ ordererRootCAs = append (ordererRootCAs , root )
290
+ }
291
+ }
292
+ for _ , intermediate := range v .GetTLSIntermediateCerts () {
293
+ // check to see of this is an app org MSP
294
+ if _ , ok := appOrgMSPs [k ]; ok {
295
+ logger .Debugf ("adding app root CAs for MSP [%s]" , k )
296
+ appRootCAs = append (appRootCAs , intermediate )
297
+ }
298
+ // check to see of this is an orderer org MSP
299
+ if _ , ok := ordOrgMSPs [k ]; ok {
300
+ logger .Debugf ("adding orderer root CAs for MSP [%s]" , k )
301
+ ordererRootCAs = append (ordererRootCAs , intermediate )
302
+ }
303
+ }
304
+ }
305
+ }
306
+ rootCASupport .AppRootCAsByChain [cid ] = appRootCAs
307
+ rootCASupport .OrdererRootCAsByChain [cid ] = ordererRootCAs
308
+
309
+ // now iterate over all roots for all app and orderer chains
310
+ trustedRoots := [][]byte {}
311
+ for _ , roots := range rootCASupport .AppRootCAsByChain {
312
+ trustedRoots = append (trustedRoots , roots ... )
313
+ }
314
+ for _ , roots := range rootCASupport .OrdererRootCAsByChain {
315
+ trustedRoots = append (trustedRoots , roots ... )
316
+ }
317
+ // also need to append statically configured root certs
318
+ if len (rootCASupport .ClientRootCAs ) > 0 {
319
+ trustedRoots = append (trustedRoots , rootCASupport .ClientRootCAs ... )
320
+ }
321
+
322
+ // now update the client roots for the gRPC server
323
+ err := srv .SetClientRootCAs (trustedRoots )
324
+ if err != nil {
325
+ msg := "Failed to update trusted roots for orderer from latest config " +
326
+ "block. This orderer may not be able to communicate " +
327
+ "with members of channel %s (%s)"
328
+ logger .Warningf (msg , cm .ConfigtxManager ().ChainID (), err )
329
+ }
330
+ }
228
331
}
0 commit comments