@@ -22,6 +22,7 @@ import (
22
22
"errors"
23
23
"fmt"
24
24
"io/ioutil"
25
+ "os"
25
26
"path/filepath"
26
27
"testing"
27
28
"time"
@@ -95,10 +96,13 @@ func invokeEmptyCall(address string, dialOptions []grpc.DialOption) (*testpb.Emp
95
96
}
96
97
97
98
// helper function to build an MSPConfig given root certs
98
- func createMSPConfig (rootCerts [][]byte , mspID string ) (* mspproto.MSPConfig , error ) {
99
+ func createMSPConfig (rootCerts , intermediateCerts [][]byte ,
100
+ mspID string ) (* mspproto.MSPConfig , error ) {
101
+
99
102
fmspconf := & mspproto.FabricMSPConfig {
100
- RootCerts : rootCerts ,
101
- Name : mspID }
103
+ RootCerts : rootCerts ,
104
+ IntermediateCerts : intermediateCerts ,
105
+ Name : mspID }
102
106
103
107
fmpsjs , err := proto .Marshal (fmspconf )
104
108
if err != nil {
@@ -114,130 +118,160 @@ func createConfigBlock(chainID string, appMSPConf, ordererMSPConf *mspproto.MSPC
114
118
return block , err
115
119
}
116
120
117
- func TestCreatePeerServer (t * testing.T ) {
121
+ func TestUpdateRootsFromConfigBlock (t * testing.T ) {
118
122
// load test certs from testdata
119
123
org1CA , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org1-cert.pem" ))
120
- org1Server1Key , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org1-server1-key.pem" ))
121
- org1Server1Cert , err := ioutil . ReadFile ( filepath . Join ( "testdata" , " Org1-server1-cert .pem" ))
122
- org1Server2Key , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org1-server2-key.pem" ))
123
- org1Server2Cert , err := ioutil . ReadFile ( filepath . Join ( "testdata" , " Org1-server2 -cert.pem" ))
124
+ org1Server1Key , err := ioutil .ReadFile (filepath .Join ("testdata" ,
125
+ " Org1-server1-key .pem" ))
126
+ org1Server1Cert , err := ioutil .ReadFile (filepath .Join ("testdata" ,
127
+ " Org1-server1 -cert.pem" ))
124
128
org2CA , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org2-cert.pem" ))
125
- org2Server1Key , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org2-server1-key.pem" ))
126
- org2Server1Cert , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org2-server1-cert.pem" ))
127
- org3CA , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org3-cert.pem" ))
129
+ org2Server1Key , err := ioutil .ReadFile (filepath .Join ("testdata" ,
130
+ "Org2-server1-key.pem" ))
131
+ org2Server1Cert , err := ioutil .ReadFile (filepath .Join ("testdata" ,
132
+ "Org2-server1-cert.pem" ))
133
+ org2IntermediateCA , err := ioutil .ReadFile (filepath .Join ("testdata" ,
134
+ "Org2-child1-cert.pem" ))
135
+ org2IntermediateServer1Key , err := ioutil .ReadFile (filepath .Join ("testdata" ,
136
+ "Org2-child1-server1-key.pem" ))
137
+ org2IntermediateServer1Cert , err := ioutil .ReadFile (filepath .Join ("testdata" ,
138
+ "Org2-child1-server1-cert.pem" ))
139
+ ordererOrgCA , err := ioutil .ReadFile (filepath .Join ("testdata" , "Org3-cert.pem" ))
140
+ ordererOrgServer1Key , err := ioutil .ReadFile (filepath .Join ("testdata" ,
141
+ "Org3-server1-key.pem" ))
142
+ ordererOrgServer1Cert , err := ioutil .ReadFile (filepath .Join ("testdata" ,
143
+ "Org3-server1-cert.pem" ))
128
144
129
145
if err != nil {
130
146
t .Fatalf ("Failed to load test certificates: %v" , err )
131
147
}
132
148
133
149
// create test MSPConfigs
134
- org1MSPConf , err := createMSPConfig ([][]byte {org1CA }, "Org1MSP" )
135
- org2MSPConf , err := createMSPConfig ([][]byte {org2CA }, "Org2MSP" )
136
- org3MSPConf , err := createMSPConfig ([][]byte {org3CA }, "Org3MSP" )
150
+ org1MSPConf , err := createMSPConfig ([][]byte {org1CA }, [][]byte {}, "Org1MSP" )
151
+ org2MSPConf , err := createMSPConfig ([][]byte {org2CA }, [][]byte {}, "Org2MSP" )
152
+ org2IntermediateMSPConf , err := createMSPConfig ([][]byte {org2CA },
153
+ [][]byte {org2IntermediateCA }, "Org2IntermediateMSP" )
154
+ ordererOrgMSPConf , err := createMSPConfig ([][]byte {ordererOrgCA },
155
+ [][]byte {}, "OrdererOrgMSP" )
137
156
if err != nil {
138
157
t .Fatalf ("Failed to create MSPConfigs (%s)" , err )
139
158
}
140
159
141
160
// create test channel create blocks
142
- channel1Block , err := createConfigBlock ("channel1" , org1MSPConf , org3MSPConf , "Org1MSP" , "Org3MSP" )
143
- channel2Block , err := createConfigBlock ("channel2" , org2MSPConf , org3MSPConf , "Org2MSP" , "Org3MSP" )
161
+ channel1Block , err := createConfigBlock ("channel1" , org1MSPConf ,
162
+ ordererOrgMSPConf , "Org1MSP" , "OrdererOrgMSP" )
163
+ channel2Block , err := createConfigBlock ("channel2" , org2MSPConf ,
164
+ ordererOrgMSPConf , "Org2MSP" , "OrdererOrgMSP" )
165
+ channel3Block , err := createConfigBlock ("channel3" , org2IntermediateMSPConf ,
166
+ ordererOrgMSPConf , "Org2IntermediateMSP" , "OrdererOrgMSP" )
144
167
145
168
createChannel := func (cid string , block * cb.Block ) {
146
169
viper .Set ("peer.tls.enabled" , true )
147
- viper .Set ("peer.tls.cert.file" , filepath .Join ("testdata" , "Org1-server1-cert.pem" ))
148
- viper .Set ("peer.tls.key.file" , filepath .Join ("testdata" , "Org1-server1-key.pem" ))
149
- viper .Set ("peer.tls.rootcert.file" , filepath .Join ("testdata" , "Org1-cert.pem" ))
170
+ viper .Set ("peer.tls.cert.file" , filepath .Join ("testdata" ,
171
+ "Org1-server1-cert.pem" ))
172
+ viper .Set ("peer.tls.key.file" , filepath .Join ("testdata" ,
173
+ "Org1-server1-key.pem" ))
174
+ viper .Set ("peer.tls.rootcert.file" , filepath .Join ("testdata" ,
175
+ "Org1-cert.pem" ))
176
+ viper .Set ("peer.fileSystemPath" , "/var/hyperledger/test/" )
177
+ defer os .RemoveAll ("/var/hyperledger/test/" )
150
178
err := peer .CreateChainFromBlock (block )
151
179
if err != nil {
152
180
t .Fatalf ("Failed to create config block (%s)" , err )
153
181
}
154
182
t .Logf ("Channel %s MSPIDs: (%s)" , cid , peer .GetMSPIDs (cid ))
155
- appCAs , orgCAs := comm .GetCASupport ().GetClientRootCAs ()
156
- t .Logf ("appCAs after update for channel %s: %d" , cid , len (appCAs ))
157
- t .Logf ("orgCAs after update for channel %s: %d" , cid , len (orgCAs ))
158
183
}
159
184
160
185
org1CertPool , err := createCertPool ([][]byte {org1CA })
161
- org2CertPool , err := createCertPool ([][]byte {org2CA })
162
186
163
187
if err != nil {
164
188
t .Fatalf ("Failed to load root certificates into pool: %v" , err )
165
189
}
166
190
167
- org1Creds := credentials .NewClientTLSFromCert (org1CertPool , "" )
168
- org2Creds := credentials .NewClientTLSFromCert (org2CertPool , "" )
169
-
170
191
// use server cert as client cert
171
- org1ClientCert , err := tls .X509KeyPair (org1Server2Cert , org1Server2Key )
192
+ org1ClientCert , err := tls .X509KeyPair (org1Server1Cert , org1Server1Key )
172
193
if err != nil {
173
194
t .Fatalf ("Failed to load client certificate: %v" , err )
174
195
}
175
- org1Org1Creds := credentials .NewTLS (& tls.Config {
196
+ org1Creds := credentials .NewTLS (& tls.Config {
176
197
Certificates : []tls.Certificate {org1ClientCert },
177
198
RootCAs : org1CertPool ,
178
199
})
179
200
org2ClientCert , err := tls .X509KeyPair (org2Server1Cert , org2Server1Key )
180
201
if err != nil {
181
202
t .Fatalf ("Failed to load client certificate: %v" , err )
182
203
}
183
- org1Org2Creds := credentials .NewTLS (& tls.Config {
204
+ org2Creds := credentials .NewTLS (& tls.Config {
184
205
Certificates : []tls.Certificate {org2ClientCert },
185
206
RootCAs : org1CertPool ,
186
207
})
208
+ org2IntermediateClientCert , err := tls .X509KeyPair (
209
+ org2IntermediateServer1Cert , org2IntermediateServer1Key )
210
+ if err != nil {
211
+ t .Fatalf ("Failed to load client certificate: %v" , err )
212
+ }
213
+ org2IntermediateCreds := credentials .NewTLS (& tls.Config {
214
+ Certificates : []tls.Certificate {org2IntermediateClientCert },
215
+ RootCAs : org1CertPool ,
216
+ })
217
+ ordererOrgClientCert , err := tls .X509KeyPair (ordererOrgServer1Cert ,
218
+ ordererOrgServer1Key )
219
+ if err != nil {
220
+ t .Fatalf ("Failed to load client certificate: %v" , err )
221
+ }
222
+ ordererOrgCreds := credentials .NewTLS (& tls.Config {
223
+ Certificates : []tls.Certificate {ordererOrgClientCert },
224
+ RootCAs : org1CertPool ,
225
+ })
187
226
188
227
// basic function tests
189
228
var tests = []struct {
190
229
name string
191
230
listenAddress string
192
231
secureConfig comm.SecureServerConfig
193
- expectError bool
194
232
createChannel func ()
195
233
goodOptions []grpc.DialOption
196
234
badOptions []grpc.DialOption
235
+ numAppCAs int
236
+ numOrdererCAs int
197
237
}{
198
238
199
239
{
200
- name : "NoTLS" ,
201
- listenAddress : fmt .Sprintf ("localhost:%d" , 4050 ),
202
- secureConfig : comm.SecureServerConfig {
203
- UseTLS : false ,
204
- },
205
- expectError : false ,
206
- createChannel : func () {},
207
- goodOptions : []grpc.DialOption {grpc .WithInsecure ()},
208
- badOptions : []grpc.DialOption {grpc .WithTransportCredentials (org1Creds )},
209
- },
210
- {
211
- name : "ServerTLSOrg1" ,
212
- listenAddress : fmt .Sprintf ("localhost:%d" , 4051 ),
240
+ name : "MutualTLSOrg1Org1" ,
241
+ listenAddress : fmt .Sprintf ("localhost:%d" , 4052 ),
213
242
secureConfig : comm.SecureServerConfig {
214
243
UseTLS : true ,
215
244
ServerCertificate : org1Server1Cert ,
216
245
ServerKey : org1Server1Key ,
217
246
ServerRootCAs : [][]byte {org1CA },
247
+ RequireClientCert : true ,
218
248
},
219
- expectError : false ,
220
- createChannel : func () {},
249
+ createChannel : func () { createChannel ("channel1" , channel1Block ) },
221
250
goodOptions : []grpc.DialOption {grpc .WithTransportCredentials (org1Creds )},
222
- badOptions : []grpc.DialOption {grpc .WithTransportCredentials (org2Creds )},
251
+ badOptions : []grpc.DialOption {grpc .WithTransportCredentials (ordererOrgCreds )},
252
+ numAppCAs : 2 , // each channel also has a DEFAULT MSP
253
+ numOrdererCAs : 1 ,
223
254
},
224
255
{
225
- name : "MutualTLSOrg1Org1 " ,
226
- listenAddress : fmt .Sprintf ("localhost:%d" , 4052 ),
256
+ name : "MutualTLSOrg1Org2 " ,
257
+ listenAddress : fmt .Sprintf ("localhost:%d" , 4053 ),
227
258
secureConfig : comm.SecureServerConfig {
228
259
UseTLS : true ,
229
260
ServerCertificate : org1Server1Cert ,
230
261
ServerKey : org1Server1Key ,
231
262
ServerRootCAs : [][]byte {org1CA },
232
263
RequireClientCert : true ,
233
264
},
234
- expectError : false ,
235
- createChannel : func () { createChannel ("channel1" , channel1Block ) },
236
- goodOptions : []grpc.DialOption {grpc .WithTransportCredentials (org1Org1Creds )},
237
- badOptions : []grpc.DialOption {grpc .WithTransportCredentials (org1Org2Creds )},
265
+ createChannel : func () { createChannel ("channel2" , channel2Block ) },
266
+ goodOptions : []grpc.DialOption {
267
+ grpc .WithTransportCredentials (org2Creds )},
268
+ badOptions : []grpc.DialOption {
269
+ grpc .WithTransportCredentials (ordererOrgCreds )},
270
+ numAppCAs : 4 ,
271
+ numOrdererCAs : 2 ,
238
272
},
239
273
{
240
- name : "MutualTLSOrg1Org2 " ,
274
+ name : "MutualTLSOrg1Org2Intermediate " ,
241
275
listenAddress : fmt .Sprintf ("localhost:%d" , 4053 ),
242
276
secureConfig : comm.SecureServerConfig {
243
277
UseTLS : true ,
@@ -246,10 +280,13 @@ func TestCreatePeerServer(t *testing.T) {
246
280
ServerRootCAs : [][]byte {org1CA },
247
281
RequireClientCert : true ,
248
282
},
249
- expectError : false ,
250
- createChannel : func () { createChannel ("channel2" , channel2Block ) },
251
- goodOptions : []grpc.DialOption {grpc .WithTransportCredentials (org1Org2Creds )},
252
- badOptions : []grpc.DialOption {grpc .WithTransportCredentials (org1Creds )},
283
+ createChannel : func () { createChannel ("channel3" , channel3Block ) },
284
+ goodOptions : []grpc.DialOption {
285
+ grpc .WithTransportCredentials (org2IntermediateCreds )},
286
+ badOptions : []grpc.DialOption {
287
+ grpc .WithTransportCredentials (ordererOrgCreds )},
288
+ numAppCAs : 7 ,
289
+ numOrdererCAs : 3 ,
253
290
},
254
291
}
255
292
@@ -258,10 +295,8 @@ func TestCreatePeerServer(t *testing.T) {
258
295
t .Run (test .name , func (t * testing.T ) {
259
296
t .Logf ("Running test %s ..." , test .name )
260
297
_ , err := peer .CreatePeerServer (test .listenAddress , test .secureConfig )
261
- // check to see whether to not we expect an error
262
- // we don't check the exact error because the comm package covers these cases
263
- if test .expectError {
264
- assert .Error (t , err , "CreatePeerServer should have returned an error" )
298
+ if err != nil {
299
+ t .Fatalf ("CreatePeerServer failed with error [%s]" , err )
265
300
} else {
266
301
assert .NoError (t , err , "CreatePeerServer should not have returned an error" )
267
302
// get the server from peer
@@ -272,15 +307,28 @@ func TestCreatePeerServer(t *testing.T) {
272
307
go server .Start ()
273
308
defer server .Stop ()
274
309
275
- // invoke the EmptyCall service with bad options
276
- _ , err = invokeEmptyCall (test .listenAddress , test .badOptions )
277
- assert .Error (t , err , "Expected error using bad dial options" )
310
+ // invoke the EmptyCall service with good options but should fail
311
+ // until channel is created and root CAs are updated
312
+ _ , err = invokeEmptyCall (test .listenAddress , test .goodOptions )
313
+ assert .Error (t , err , "Expected error invoking the EmptyCall service " )
314
+
278
315
// creating channel should update the trusted client roots
279
316
test .createChannel ()
317
+
318
+ // make sure we have the expected number of CAs
319
+ appCAs , ordererCAs := comm .GetCASupport ().GetClientRootCAs ()
320
+ assert .Equal (t , test .numAppCAs , len (appCAs ),
321
+ "Did not find expected number of app CAs for channel" )
322
+ assert .Equal (t , test .numOrdererCAs , len (ordererCAs ),
323
+ "Did not find expected number of orderer CAs for channel" )
324
+
280
325
// invoke the EmptyCall service with good options
281
326
_ , err = invokeEmptyCall (test .listenAddress , test .goodOptions )
282
327
assert .NoError (t , err , "Failed to invoke the EmptyCall service" )
283
328
329
+ // invoke the EmptyCall service with bad options
330
+ _ , err = invokeEmptyCall (test .listenAddress , test .badOptions )
331
+ assert .Error (t , err , "Expected error using bad dial options" )
284
332
}
285
333
})
286
334
}
0 commit comments