15
15
'use strict' ;
16
16
17
17
const CaliperUtils = require ( '@hyperledger/caliper-core' ) . CaliperUtils ;
18
-
18
+ const fs = require ( 'fs' ) . promises ;
19
19
/**
20
20
*
21
21
*/
@@ -72,10 +72,10 @@ class ConnectionProfileDefinition {
72
72
}
73
73
74
74
/**
75
- * get the list of peers in this connection profile organisation that are part of the requested chanel
75
+ * get the list of peers in this connection profile organization that are part of the requested chanel
76
76
*
77
77
* @param {* } channelName The channel name
78
- * @returns {[string] } The array of peers that are in the channel for this organisation 's CCP
78
+ * @returns {[string] } The array of peers that are in the channel for this organization 's CCP
79
79
*/
80
80
getOwnedEndorsingPeersInChannel ( channelName ) {
81
81
const channelPeers = this . _getChannelPeers ( channelName ) ;
@@ -118,7 +118,6 @@ class ConnectionProfileDefinition {
118
118
return this . TLSEnabled ;
119
119
}
120
120
121
-
122
121
/**
123
122
* Get all the peers defined in the specified channel
124
123
* @param {* } channelName The name of the channel
@@ -193,6 +192,126 @@ class ConnectionProfileDefinition {
193
192
return false ;
194
193
}
195
194
195
+ /**
196
+ * Return the list of peers for organization of the connection profile
197
+ * @param {* } mspid the mspId of the org
198
+ * @returns {[string] } an array containing the list of all the peers
199
+ */
200
+ getPeersListForOrganization ( mspid ) {
201
+ if ( ! this . connectionProfile . organizations ) {
202
+ throw new Error ( 'No organizations property can be found for the connection profile provided' ) ;
203
+ }
204
+ let peers ;
205
+ for ( const org in this . connectionProfile . organizations ) {
206
+ if ( this . connectionProfile . organizations [ org ] . mspid === mspid ) {
207
+ if ( ! this . connectionProfile . organizations [ org ] . peers ) {
208
+ throw new Error ( `Org with mspid "${ mspid } " listed in connectionProfile.organizations does not have any peers property` ) ;
209
+ }
210
+ peers = this . connectionProfile . organizations [ org ] . peers ;
211
+ }
212
+ }
213
+ if ( ! peers ) {
214
+ throw new Error ( `Org with mspid ${ mspid } cannot be found in connectionProfile.organizations` ) ;
215
+ }
216
+ return peers ;
217
+ }
218
+
219
+ /**
220
+ * Return the tls certificate for the specified peer, can only be called when a grps url and respective certs are provided
221
+ * @param {string } peer the name of the peer
222
+ * @returns {string } tls certificate
223
+ */
224
+ async getTlsCertForPeer ( peer ) {
225
+ const peerObj = this . _getPeerIfValid ( peer ) ;
226
+
227
+ if ( ! peerObj . tlsCACerts ) {
228
+ throw new Error ( `No tlsCACert property of ${ peer } in the connection profile was not provided` ) ;
229
+ }
230
+
231
+ if ( peerObj . tlsCACerts . pem ) {
232
+ if ( ! peerObj . tlsCACerts . pem . startsWith ( '-----BEGIN ' ) ) {
233
+ throw new Error ( `pem provided for ${ peer } in the connection profile .tlsCACerts.pem is not valid` ) ;
234
+ }
235
+ return peerObj . tlsCACerts . pem ;
236
+ } else if ( peerObj . tlsCACerts . path ) {
237
+ const pemPath = peerObj . tlsCACerts . path ;
238
+ const resolvedPemPath = CaliperUtils . resolvePath ( pemPath ) ;
239
+ try {
240
+ await fs . stat ( resolvedPemPath ) ;
241
+ } catch ( err ) {
242
+ if ( err . errno === - 2 || err . errno === - 4058 ) {
243
+ throw new Error ( `path property does not point to a file that exists at ${ resolvedPemPath } for ${ peer } ` ) ;
244
+ }
245
+ throw err ;
246
+ }
247
+
248
+ const pem = ( await fs . readFile ( resolvedPemPath ) ) . toString ( ) ;
249
+
250
+ if ( ! pem . startsWith ( '-----BEGIN ' ) ) {
251
+ throw new Error ( `path property does not point to a valid pem file for ${ resolvedPemPath } for ${ peer } ` ) ;
252
+ }
253
+
254
+ return pem ;
255
+ } else {
256
+ throw new Error ( `No valid tls cert option provided in the ${ peer } .tlsCACert property of connection profile` ) ;
257
+ }
258
+ }
259
+
260
+ /**
261
+ * Return the end point of the peer
262
+ * @param {string } peer the name of the peer
263
+ * @returns {string } end point for peer
264
+ */
265
+ getGrpcEndPointForPeer ( peer ) {
266
+ const url = this . _getPeerIfValid ( peer ) . url ;
267
+
268
+ if ( ! url . startsWith ( 'grpcs://' ) && ! url . startsWith ( 'grpc://' ) ) {
269
+ throw new Error ( `${ url } is not a valid grpc/grpcs url, make sure to prefix grpc:// or grpcs:// at the beginning of the url` ) ;
270
+ }
271
+ return url . replace ( / ( g r p c s | g r p c ) ( : \/ \/ ) / , '' ) ;
272
+ }
273
+
274
+ /**
275
+ * Return if the end point of the peer requires tls
276
+ * @param {string } peer the name of the peer
277
+ * @returns {boolean } returns true if tls is required for the endpoint, false otherwise
278
+ */
279
+ isTLSRequiredForEndpoint ( peer ) {
280
+ return this . _getPeerIfValid ( peer ) . url . startsWith ( 'grpcs://' ) ;
281
+ }
282
+
283
+ /**
284
+ * Return the gRpc options for the specified peer
285
+ * @param {* } peer the name of the peer
286
+ * @returns {[*] } the list of grpc options
287
+ */
288
+ getGrpcOptionsForPeer ( peer ) {
289
+ const grpcOptions = this . _getPeerIfValid ( peer ) . grpcOptions ;
290
+
291
+ if ( ! grpcOptions ) {
292
+ return { } ;
293
+ }
294
+ return grpcOptions ;
295
+ }
296
+
297
+ /**
298
+ * Check if peer provided is present in peers property and return it
299
+ * @param {string } peer the name of the peer
300
+ * @returns {* } the peer object
301
+ */
302
+ _getPeerIfValid ( peer ) {
303
+ if ( ! this . connectionProfile . peers ) {
304
+ throw new Error ( 'No peers property can be found in the connection profile provided' ) ;
305
+ }
306
+ if ( ! this . connectionProfile . peers [ peer ] ) {
307
+ throw new Error ( `${ peer } provided is not present in the connection profile` ) ;
308
+ }
309
+
310
+ if ( ! this . connectionProfile . peers [ peer ] . url ) {
311
+ throw new Error ( `${ peer } provided does not have url property provided in the connection Profile` ) ;
312
+ }
313
+ return this . connectionProfile . peers [ peer ] ;
314
+ }
196
315
}
197
316
198
- module . exports = ConnectionProfileDefinition ;
317
+ module . exports = ConnectionProfileDefinition ;
0 commit comments