Skip to content

Commit a84cdd9

Browse files
authored
add changes to ConnectionProfileDefinition for new PeerGateway connector and add peer-gateway to nyc of caliper-fabric (#1253)
Signed-off-by: fraVlaca <ocsenarf@outlook.com>
1 parent 5ee6aaa commit a84cdd9

File tree

4 files changed

+658
-12
lines changed

4 files changed

+658
-12
lines changed

packages/caliper-fabric/lib/connector-configuration/ConnectionProfileDefinition.js

Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'use strict';
1616

1717
const CaliperUtils = require('@hyperledger/caliper-core').CaliperUtils;
18-
18+
const fs = require('fs').promises;
1919
/**
2020
*
2121
*/
@@ -72,10 +72,10 @@ class ConnectionProfileDefinition {
7272
}
7373

7474
/**
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
7676
*
7777
* @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
7979
*/
8080
getOwnedEndorsingPeersInChannel(channelName) {
8181
const channelPeers = this._getChannelPeers(channelName);
@@ -118,7 +118,6 @@ class ConnectionProfileDefinition {
118118
return this.TLSEnabled;
119119
}
120120

121-
122121
/**
123122
* Get all the peers defined in the specified channel
124123
* @param {*} channelName The name of the channel
@@ -193,6 +192,126 @@ class ConnectionProfileDefinition {
193192
return false;
194193
}
195194

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(/(grpcs|grpc)(:\/\/)/, '');
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+
}
196315
}
197316

198-
module.exports = ConnectionProfileDefinition;
317+
module.exports = ConnectionProfileDefinition;

packages/caliper-fabric/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
"lib/connector-configuration",
8585
"lib/identity-management",
8686
"lib/connector-versions/v2",
87-
"lib/connector-versions/v1"
87+
"lib/connector-versions/v1",
88+
"lib/connector-versions/peer-gateway"
8889
],
8990
"exclude": [
9091
"lib/identity-management/IWallet*",

0 commit comments

Comments
 (0)