-
Notifications
You must be signed in to change notification settings - Fork 0
/
pte-util.js
1028 lines (910 loc) · 37.1 KB
/
pte-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2016 IBM All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var fs = require('fs-extra');
var hfc = require('fabric-client');
var FabricCAServices = require('fabric-ca-client');
var jsrsa = require('jsrsasign');
var path = require('path');
var yaml = require('js-yaml');
var winston = require('winston');
var copService = require('fabric-ca-client/lib/FabricCAServices.js');
var User = require('fabric-common/lib/User.js');
var PTEid = parseInt(process.argv[5]);
PTEid = PTEid ? PTEid : 0
var loggerMsg = 'PTE ' + PTEid + ' util';
var logger = new PTELogger({ "prefix": loggerMsg, "level": "info" });
module.exports.CHAINCODE_PATH = 'github.com/example_cc';
module.exports.CHAINCODE_UPGRADE_PATH = 'github.com/example_cc1';
module.exports.CHAINCODE_MARBLES_PATH = 'github.com/marbles_cc';
module.exports.END2END = {
channel: 'mychannel',
chaincodeId: 'end2end',
chaincodeVersion: 'v0'
};
// directory for file based KeyValueStore
module.exports.KVS = '/tmp/hfc-test-kvs';
module.exports.storePathForOrg = function (networkid, org) {
return module.exports.KVS + '_' + networkid + '_' + org;
};
// temporarily set $GOPATH to the test fixture folder
module.exports.setupChaincodeDeploy = function () {
process.env.GOPATH = path.join(__dirname, '../fixtures');
};
// specifically set the values to defaults because they may have been overridden when
// running in the overall test bucket ('gulp test')
module.exports.resetDefaults = function () {
global.hfc.config = undefined;
require('nconf').reset();
};
module.exports.cleanupDir = function (keyValStorePath) {
var absPath = path.resolve(process.cwd(), keyValStorePath);
var exists = module.exports.existsSync(absPath);
if (exists) {
fs.removeSync(absPath);
}
};
module.exports.getUniqueVersion = function (prefix) {
if (!prefix) prefix = 'v';
return prefix + Date.now();
};
// utility function to check if directory or file exists
// uses entire / absolute path from root
module.exports.existsSync = function (absolutePath /*string*/) {
try {
var stat = fs.statSync(absolutePath);
if (stat.isDirectory() || stat.isFile()) {
return true;
} else
return false;
}
catch (e) {
return false;
}
};
module.exports.readFile = readFile;
var tlsOptions = {
trustedRoots: [],
verify: false
};
// is an object empty?
function isEmpty(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return false;
}
return true;
}
// total number of peers in all orgs listed in orgList
function getTotalPeers(cpList, orgList) {
logger.info('[getTotalPeers] ');
var totalPeers = 0;
for (var i = 0; i< orgList.length; i++ ) {
var org = orgList[i]
var cpf = findOrgConnProfile(cpList, org);
if ( cpf === null ) {
logger.info('[getTargetPeerList] cannot find org(%s) in any connection profile)', org);
continue;
}
totalPeers = totalPeers + cpf['organizations'][org]['peers'].length;
}
logger.info('[getTotalPeers] find peer number: %d ', totalPeers);
return totalPeers;
}
module.exports.getTotalPeersSubmitter = function (cpList, orgList) {
return getTotalPeers(cpList, orgList);
}
// find target peers according to the targetPeerType (ANCHORPEER or ALLPEERS)
function getTargetPeerList(cpList, orgList, targetPeerType) {
logger.info('[getTargetPeerList] targetPeerType: ', targetPeerType);
var targetPeers = {};
if ( targetPeerType === 'ANCHORPEER' || targetPeerType === 'ALLPEERS' ) {
for (var i = 0; i< orgList.length; i++ ) {
var org = orgList[i]
targetPeers[org]=[];
var cpf = findOrgConnProfile(cpList, org);
if ( cpf === null ) {
logger.info('[getTargetPeerList] cannot find org(%s) in any connection profile)', org);
continue;
}
var cpOrg = cpf['organizations'];
for ( var j=0; j<cpOrg[org]['peers'].length; j++ ) {
var peer = cpOrg[org]['peers'][j];
targetPeers[org].push(peer);
if ( targetPeerType === 'ANCHORPEER' ) {
break;
}
}
}
} else {
logger.error('[getTargetPeerList] invalid targetPeerType: %s ', targetPeerType);
return null;
}
logger.info('[getTargetPeerList] find peers: %j ', targetPeers);
return targetPeers;
}
module.exports.getTargetPeerListSubmitter = function (cpList, orgList, targetPeerType) {
return getTargetPeerList(cpList, orgList, targetPeerType);
}
function getOrgOrdererID(org, cpList) {
var ordererID;
var cpf = findOrgConnProfile(cpList, org);
if (0 === getConnProfilePropCnt(cpf, 'orderers')) {
logger.error('[getOrgOrdererID] org: %s, no orderer found in the connection profile', org);
process.exit(1);
}
var cpOrgs = cpf['organizations'];
if (typeof cpOrgs[org].ordererID !== 'undefined') {
ordererID = cpOrgs[org].ordererID;
} else {
var orderersCPFList = {};
orderersCPFList = getNodetypeFromConnProfiles(cpList, 'orderers');
ordererID = Object.getOwnPropertyNames(orderersCPFList)[0];
}
return ordererID;
}
function assignChannelOrderer(channel, client, org, cpPath, TLS) {
var data;
var cpList = [];
cpList = getConnProfileList(cpPath);
var orderersCPFList = {};
orderersCPFList = getNodetypeFromConnProfiles(cpList, 'orderers');
var cpf = findOrgConnProfile(cpList, org);
if (0 === getConnProfilePropCnt(cpf, 'orderers')) {
logger.error('[assignChannelOrderer] org: %s, no orderer is found in the connection profile', org);
process.exit(1);
}
var ordererID = getOrgOrdererID(org, cpList);
var orderer;
if (TLS > TLSDISABLED) {
data = getTLSCert('orderer', ordererID, cpf, cpPath);
if (data !== null) {
orderer = client.newOrderer(
orderersCPFList[ordererID].url,
{
'pem': Buffer.from(data).toString(),
'ssl-target-name-override': orderersCPFList[ordererID]['grpcOptions']['ssl-target-name-override']
}
);
}
} else {
orderer = client.newOrderer(orderersCPFList[ordererID].url)
}
if (channel) {
channel.addOrderer(orderer);
logger.info('[assignChannelOrderer] channel orderers: %s', channel.getOrderers());
}
return orderer;
}
module.exports.assignChannelOrdererSubmitter = function (channel, client, org, cpPath, TLS) {
return assignChannelOrderer(channel, client, org, cpPath, TLS);
}
// add peer(s) to a channel
function assignChannelPeers(cpList, channel, client, targetPeers, TLS, cpPath, evtType, invokeType, peerFOList, peerList, eventHubs) {
var peerTmp;
var eh;
var data;
var peername;
var targets = [];
logger.info('[assignChannelPeers] target peer list: %j', targetPeers);
for (var j=0; j< Object.keys(targetPeers).length; j++) {
var key = Object.keys(targetPeers)[j];
// find the connection profile of the specified org
var cpfTmp = findOrgConnProfile(cpList, key);
var cpf = findOrgConnProfile(cpList, key);
if (0 === getConnProfilePropCnt(cpf, 'peers')) {
logger.info('[assignChannelPeers] no peer is found in the connection profile for org (%s)', key);
continue;
}
var cpPeersTmp = cpfTmp['peers'];
for ( var i = 0; i < targetPeers[key].length; i++) {
peername = targetPeers[key][i];
if (cpPeersTmp.hasOwnProperty(peername)) {
if (cpPeersTmp[peername].url) {
if (TLS > TLSDISABLED) {
data = getTLSCert(key, peername, cpfTmp, cpPath);
if (data !== null) {
peerTmp = client.newPeer(
cpPeersTmp[peername].url,
{
pem: Buffer.from(data).toString(),
'ssl-target-name-override': cpPeersTmp[peername]['grpcOptions']['ssl-target-name-override']
}
);
}
} else {
peerTmp = client.newPeer(cpPeersTmp[peername].url);
}
if ( channel ) {
channel.addPeer(peerTmp);
}
if (peerFOList == 'TARGETPEERS') {
peerList.push(peerTmp);
}
if (invokeType == 'MOVE') {
eh = channel.newChannelEventHub(peerTmp);
eventHubs.push(eh);
if (evtType == 'FILTEREDBLOCK') {
eh.connect();
} else {
eh.connect(true);
}
}
targets.push(peerTmp);
}
} else {
logger.error('[assignChannelPeers] the targeted peer does not exist in connection profile: %s', peername);
process.exit(1);
}
}
}
if (channel) {
logger.info('[assignChannelPeers] getPeers: : %s', channel.getPeers());
}
return targets;
}
module.exports.assignChannelPeersSubmitter = function (cpList, channel, client, targetPeers, TLS, cpPath, evtType, invokeType, peerFOList, peerList, eventHubs) {
return assignChannelPeers(cpList, channel, client, targetPeers, TLS, cpPath, evtType, invokeType, peerFOList, peerList, eventHubs);
}
// get connection profiles from directory cpPath or a single connection profile file
function getConnProfileList(cpPath) {
var cpList = [];
if ((/(yml|yaml|json)$/i).test(cpPath)) {
cpList.push(cpPath)
} else {
fs.readdirSync(cpPath).forEach(file => {
logger.debug('[getConnProfileList] file', file);
var file_ext = path.extname(file);
if ((/(yml|yaml|json)$/i).test(file_ext)) {
var cpf = path.join(cpPath, file);
cpList.push(cpf);
}
});
}
logger.debug('[getConnProfileList] file:', cpList);
return cpList;
}
module.exports.getConnProfileListSubmitter = function (fileList) {
return getConnProfileList(fileList);
}
// get orderers or peers from all connection profiles
function getNodetypeFromConnProfiles(fileList, nodeType) {
var nodeTypeList = {};
logger.debug('[getNodetypeFromConnProfiles] input nodeType (%s), File list: ', nodeType, fileList);
for (i = 0; i < fileList.length; i++) {
var cpf = fileList[i];
var ntwk = readConfigFile(cpf, 'test-network');
if (ntwk[nodeType] && (!isEmpty(ntwk[nodeType]))) {
for (var key in ntwk[nodeType]) {
let found = 0;
for (var key1 in nodeTypeList) {
if (ntwk[nodeType][key].url === nodeTypeList[key1].url) {
found = 1;
break;
}
}
if (found === 0) {
nodeTypeList[key] = ntwk[nodeType][key];
}
}
}
}
if (Object.keys(nodeTypeList).length == 0) {
logger.warn('[getNodetypeFromConnProfiles] no %s found in all connection profiles', nodeType);
}
logger.debug('[getNodetypeFromConnProfiles] %s List: %j', nodeType, nodeTypeList);
return nodeTypeList;
}
module.exports.getNodetypeFromConnProfilesSubmitter = function (fileList, prop) {
return getNodetypeFromConnProfiles(fileList, prop);
}
// get Connection Profile property counts
function getConnProfilePropCnt(cpf, prop) {
if (cpf === null) {
logger.error('[getConnProfilePropCnt] the connection profile is invalid: null');
return 0;
}
if (!cpf.hasOwnProperty(prop)) {
logger.error('[getConnProfilePropCnt] prop (%s) is not found in connectino profile', prop);
return 0;
}
if (true === isEmpty(cpf[prop])) {
logger.error('[getConnProfilePropCnt] prop (%s) is empty in connectino profile', prop);
return 0;
}
let cnt = Object.getOwnPropertyNames(cpf[prop]).length;
logger.debug('[getConnProfilePropCnt] prop (%s) cnt: %d', prop, cnt);
return cnt;
}
module.exports.getConnProfilePropCntSubmitter = function (cpf, key) {
return getConnProfilePropCnt(cpf, key);
}
// find Connection Profile for an org
function findOrgConnProfile(fileList, orgname) {
logger.debug('[findOrgConnProfile] orgname(%s), input File list: ', orgname, fileList);
for (i = 0; i < fileList.length; i++) {
var cpf = fileList[i];
var temp = readConfigFile(cpf, 'test-network');
if (temp['organizations'] && temp['organizations'][orgname]) {
logger.debug('[findOrgConnProfile] orgname(%s) found File: ', orgname, cpf);
return temp;
}
}
return null;
}
module.exports.findOrgConnProfileSubmitter = function (fileList, orgname) {
return findOrgConnProfile(fileList, orgname);
}
// find all org from Connection Profiles
function findAllOrgFromConnProfile(fileList) {
var orgList = [];
for (i = 0; i < fileList.length; i++) {
var cpf = fileList[i];
var temp = readConfigFile(cpf, 'test-network');
if (temp['organizations']) {
for (var org in temp['organizations']) {
if ( !(orgList.indexOf(org) > -1) ) {
orgList.push(org);
}
}
}
}
logger.debug('[findAllOrgFromConnProfile] org list:', orgList);
return orgList;
}
module.exports.findAllOrgFromConnProfileSubmitter = function (fileList) {
return findAllOrgFromConnProfile(fileList);
}
// set pointer to the keyreq in the input File
// if key is missing or invalid, set the pointer to the beginning of the File
function readConfigFile(inFile, keyreq) {
var temp;
var file_ext = path.extname(inFile);
logger.debug('[readConfigFile] input File: ', inFile);
if ((/(yml|yaml)$/i).test(file_ext)) {
temp = yaml.safeLoad(fs.readFileSync(inFile, 'utf8'));
} else {
temp = JSON.parse(fs.readFileSync(inFile));
}
if (temp[keyreq]) {
logger.debug('[readConfigFile] set pointer to %s[%s] ', inFile, keyreq);
return temp[keyreq];
} else {
logger.debug('[readConfigFile] set pointer to %s', inFile);
return temp;
}
}
module.exports.readConfigFileSubmitter = function (inFile, keyreq) {
return readConfigFile(inFile, keyreq);
}
// find org CA
function findOrgCA(inPtr, org) {
var cpOrgs = inPtr['organizations'];
if (0 === getConnProfilePropCnt(inPtr, 'certificateAuthorities')) {
logger.error('[findOrgCA] no certificateAuthority is found in the connection profile');
process.exit(1);
}
var cpCAs = inPtr['certificateAuthorities'];
var orgCA;
if (cpOrgs[org].hasOwnProperty('certificateAuthorities')) {
orgCA = cpOrgs[org].certificateAuthorities[0];
} else {
orgCA = Object.getOwnPropertyNames(cpCAs)[0];
}
return orgCA;
}
// get enroll ID
function getOrgEnrollId(inPtr, org) {
if (0 === getConnProfilePropCnt(inPtr, 'certificateAuthorities')) {
logger.error('[getOrgEnrollId] no certificateAuthority is found in the connection profile');
process.exit(1);
}
var cpCAs = inPtr['certificateAuthorities'];
var orgCA = findOrgCA(inPtr, org);
if (cpCAs[orgCA].hasOwnProperty('registrar') && cpCAs[orgCA].registrar.hasOwnProperty('enrollId')) {
return cpCAs[orgCA].registrar.enrollId;
} else {
return 'admin';
}
}
module.exports.getOrgEnrollIdSubmitter = function (inPtr, org) {
return getOrgEnrollId(inPtr, org);
}
// get enroll secret
function getOrgEnrollSecret(inPtr, org) {
if (0 === getConnProfilePropCnt(inPtr, 'certificateAuthorities')) {
logger.error('[getOrgEnrollSecret] no certificateAuthority is found in the connection profile');
process.exit(1);
}
var cpCAs = inPtr['certificateAuthorities'];
var orgCA = findOrgCA(inPtr, org);
if (cpCAs[orgCA].hasOwnProperty('registrar') && cpCAs[orgCA].registrar.hasOwnProperty('enrollSecret')) {
return cpCAs[orgCA].registrar.enrollSecret;
} else {
return 'adminpw';
}
}
module.exports.getOrgEnrollSecretSubmitter = function (inPtr, org) {
return getOrgEnrollSecret(inPtr, org);
}
function getMember(username, password, client, nid, userOrg, cpf) {
var cpOrgs = cpf['organizations'];
if (0 === getConnProfilePropCnt(cpf, 'certificateAuthorities')) {
logger.error('[getMember] no certificateAuthority is found in the connection profile');
process.exit(1);
}
var cpCAs = cpf['certificateAuthorities'];
var orgCA = findOrgCA(cpf, userOrg);
var caUrl = cpCAs[orgCA].url;
logger.debug('[getMember] getMember, name: org %s ca url: %s', userOrg, caUrl);
logger.debug('[getMember] getMember, name: ' + username + ', client.getUserContext(' + username + ', true)');
return client.getUserContext(username, true)
.then((user) => {
return new Promise((resolve, reject) => {
if (user && user.isEnrolled()) {
logger.info('[getMember] Successfully loaded member from persistence');
return resolve(user);
}
var member = new User(username);
var cryptoSuite = client.getCryptoSuite();
if (!cryptoSuite) {
cryptoSuite = hfc.newCryptoSuite();
if (userOrg) {
cryptoSuite.setCryptoKeyStore(Client.newCryptoKeyStore({ path: module.exports.storePathForOrg(nid, cpOrgs[userOrg].name) }));
client.setCryptoSuite(cryptoSuite);
}
}
member.setCryptoSuite(cryptoSuite);
// need to enroll it with CA server
var orgCA = cpOrgs[userOrg].certificateAuthorities[0];
var cop = new copService(caUrl, tlsOptions, cpCAs[orgCA].caName, cryptoSuite);
return cop.enroll({
enrollmentID: username,
enrollmentSecret: password
}).then((enrollment) => {
logger.info('[getMember] Successfully enrolled user \'' + username + '\'');
return member.setEnrollment(enrollment.key, enrollment.certificate, cpOrgs[userOrg].mspid);
}).then(() => {
var skipPersistence = false;
if (!client.getStateStore()) {
skipPersistence = true;
}
return client.setUserContext(member, skipPersistence);
}).then(() => {
return resolve(member);
}).catch((err) => {
logger.error('[getMember] Failed to enroll and persist user. Error: ' + err.stack ? err.stack : err);
process.exit(1);
});
});
}).catch((err) => {
logger.error(err)
process.exit(1);
});
}
function getAdmin(client, nid, userOrg, cpf) {
var cpOrgs = cpf['organizations'];
var keyPath;
var keyPEM;
var certPath;
var certPEM;
if (typeof cpOrgs[userOrg].admin_cert !== 'undefined') {
logger.debug('[getAdmin] %s admin_cert defined', userOrg);
keyPEM = cpOrgs[userOrg].priv;
certPEM = cpOrgs[userOrg].admin_cert;
} else if ((typeof cpOrgs[userOrg].adminPrivateKey !== 'undefined') &&
(typeof cpOrgs[userOrg].signedCert !== 'undefined')) {
logger.debug('[getAdmin] %s adminPrivateKey and signedCert defined', userOrg);
if (typeof cpOrgs[userOrg].adminPrivateKey.path !== 'undefined') {
keyPath = path.resolve(cpOrgs[userOrg].adminPrivateKey.path, 'keystore');
keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
logger.debug('[getAdmin] keyPath: %s', keyPath);
} else if (typeof cpOrgs[userOrg].adminPrivateKey.pem !== 'undefined') {
keyPEM = cpOrgs[userOrg].adminPrivateKey.pem;
} else {
logger.error('[getAdmin] error: adminPrivateKey invalid');
return null;
}
if (typeof cpOrgs[userOrg].signedCert.path !== 'undefined') {
certPath = path.resolve(cpOrgs[userOrg].signedCert.path, 'signcerts');
certPEM = Buffer.from(readAllFiles(certPath)[0]).toString();
logger.debug('[getAdmin] certPath: %s', certPath);
} else if (typeof cpOrgs[userOrg].signedCert.pem !== 'undefined') {
certPEM = cpOrgs[userOrg].signedCert.pem;
} else {
logger.error('[getAdmin] error: signedCert invalid');
return null;
}
} else if (typeof cpOrgs[userOrg].adminPath !== 'undefined') {
logger.debug('[getAdmin] %s adminPath defined', userOrg);
keyPath = path.resolve(cpOrgs[userOrg].adminPath, 'keystore');
keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
certPath = path.resolve(cpOrgs[userOrg].adminPath, 'signcerts');
certPEM = readAllFiles(certPath)[0];
logger.debug('[getAdmin] keyPath: %s', keyPath);
logger.debug('[getAdmin] certPath: %s', certPath);
}
var cryptoSuite = hfc.newCryptoSuite();
if (userOrg) {
cryptoSuite.setCryptoKeyStore(hfc.newCryptoKeyStore({ path: module.exports.storePathForOrg(nid, cpOrgs[userOrg].name) }));
client.setCryptoSuite(cryptoSuite);
}
return Promise.resolve(client.createUser({
username: 'peer' + userOrg + 'Admin',
mspid: cpOrgs[userOrg].mspid,
cryptoContent: {
privateKeyPEM: keyPEM.toString(),
signedCertPEM: certPEM.toString()
}
}));
}
function getOrdererAdmin(client, userOrg, cpPath) {
var cpList = [];
var orderersCPFList = {};
cpList = getConnProfileList(cpPath);
orderersCPFList = getNodetypeFromConnProfiles(cpList, 'orderers');
if (Object.getOwnPropertyNames(orderersCPFList).length === 0) {
logger.error('[org:id=%s:%d getOrdererAdmin] no orderer found', org, pid);
process.exit(1);
}
var keyPath;
var keyPEM;
var certPath;
var certPEM;
var ordererID;
var cpf = findOrgConnProfile(cpList, userOrg);
var cpOrgs = cpf['organizations'];
// get ordererID
if (cpOrgs[userOrg].hasOwnProperty('ordererID')) {
ordererID = cpOrgs[userOrg]['ordererID'];
} else {
ordererID = Object.getOwnPropertyNames(orderersCPFList)[0];
}
logger.debug('[getOrdererAdmin] orderer ID= %s', ordererID);
if ((typeof orderersCPFList.admin_cert !== 'undefined') &&
(typeof orderersCPFList.priv !== 'undefined')) {
logger.debug('[getOrdererAdmin] %s global orderer admin_cert and priv defined', userOrg);
keyPEM = orderersCPFList.priv;
certPEM = orderersCPFList.admin_cert;
} else if ((typeof orderersCPFList[ordererID].admin_cert !== 'undefined') &&
(typeof orderersCPFList[ordererID].priv !== 'undefined')) {
logger.debug('[getOrdererAdmin] %s local orderer admin_cert and priv defined', userOrg);
keyPEM = orderersCPFList[ordererID].priv;
certPEM = orderersCPFList[ordererID].admin_cert;
} else if ((typeof orderersCPFList[ordererID].adminPrivateKey !== 'undefined') &&
(typeof orderersCPFList[ordererID].signedCert !== 'undefined')) {
logger.debug('[getOrdererAdmin] %s adminPrivateKey and signedCert defined', ordererID);
if (typeof orderersCPFList[ordererID].adminPrivateKey.path !== 'undefined') {
keyPath = path.resolve(orderersCPFList[ordererID].adminPrivateKey.path, 'keystore');
keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
logger.debug('[getOrdererAdmin] %s keyPath: %s', ordererID, keyPath);
} else if (typeof orderersCPFList[ordererID].adminPrivateKey.pem !== 'undefined') {
keyPEM = orderersCPFList[ordererID].adminPrivateKey.pem;
} else {
logger.error('[getOrdererAdmin] %s error: adminPrivateKey invalid', ordererID);
return null;
}
if (typeof orderersCPFList[ordererID].signedCert.path !== 'undefined') {
certPath = path.resolve(orderersCPFList[ordererID].signedCert.path, 'signcerts');
certPEM = Buffer.from(readAllFiles(certPath)[0]).toString();
logger.debug('[getOrdererAdmin] %s certPath: %s', ordererID, certPath);
} else if (typeof orderersCPFList[ordererID].signedCert.pem !== 'undefined') {
certPEM = orderersCPFList[ordererID].signedCert.pem;
} else {
logger.error('[getOrdererAdmin] %s error: signedCert invalid', ordererID);
return null;
}
} else if (typeof orderersCPFList.adminPath !== 'undefined') {
logger.debug('[getOrdererAdmin] %s global orderer adminPath defined', userOrg);
keyPath = path.resolve(orderersCPFList.adminPath, 'keystore');
keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
certPath = path.resolve(orderersCPFList.adminPath, 'signcerts');
certPEM = readAllFiles(certPath)[0];
logger.debug('[getOrdererAdmin] keyPath: %s', keyPath);
logger.debug('[getOrdererAdmin] certPath: %s', certPath);
} else if (typeof orderersCPFList[ordererID].adminPath !== 'undefined') {
logger.debug('[getOrdererAdmin] %s local orderer adminPath defined', userOrg);
keyPath = path.resolve(orderersCPFList[ordererID].adminPath, 'keystore');
keyPEM = Buffer.from(readAllFiles(keyPath)[0]).toString();
certPath = path.resolve(orderersCPFList[ordererID].adminPath, 'signcerts');
certPEM = readAllFiles(certPath)[0];
logger.debug('[getOrdererAdmin] keyPath: %s', keyPath);
logger.debug('[getOrdererAdmin] certPath: %s', certPath);
}
return Promise.resolve(client.createUser({
username: 'ordererAdmin',
mspid: orderersCPFList[ordererID].mspid,
cryptoContent: {
privateKeyPEM: keyPEM.toString(),
signedCertPEM: certPEM.toString()
}
}));
}
function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (!!err)
reject(new Error('Failed to read file ' + path + ' due to error: ' + err));
else
resolve(data);
});
});
}
function readAllFiles(dir) {
var currentDirectory = __dirname
var homeDirectory = currentDirectory.split("src/github.com")[0]
dir = dir.split("PTE/")[1]
dir = path.join(homeDirectory, dir)
var files = fs.readdirSync(dir);
var certs = [];
files.forEach((file_name) => {
let file_path = path.resolve(dir, file_name);
logger.debug('[readAllFiles] looking at file ::' + file_path);
let data = fs.readFileSync(file_path);
certs.push(data);
});
return certs;
}
module.exports.getOrderAdminSubmitter = function (client, userOrg, cpPath) {
return getOrdererAdmin(client, userOrg, cpPath);
};
module.exports.getSubmitter = function (username, secret, client, peerOrgAdmin, nid, org, cpf) {
if (arguments.length < 2) throw new Error('"client" and "test" are both required parameters');
var peerAdmin, userOrg;
if (typeof peerOrgAdmin === 'boolean') {
peerAdmin = peerOrgAdmin;
} else {
peerAdmin = false;
}
// if the 3rd argument was skipped
if (typeof peerOrgAdmin === 'string') {
userOrg = peerOrgAdmin;
} else {
if (typeof org === 'string') {
userOrg = org;
} else {
userOrg = 'org1';
}
}
if (peerAdmin) {
logger.debug(' >>>> getting the org admin');
return getAdmin(client, nid, userOrg, cpf);
} else {
return getMember(username, secret, client, nid, userOrg, cpf);
}
};
// set up PTE logger
function PTELogger(opts) {
var winstonLogger = new winston.Logger({
transports: [
new (winston.transports.Console)({ colorize: true })
]
}),
levels = ['debug', 'info', 'warn', 'error'],
logger = Object.assign({}, winstonLogger);
if (opts.level) {
if (levels.includes(opts.level)) {
// why, oh why, does logger.level = opts.level not work?
winstonLogger.level = opts.level;
}
}
levels.forEach(function (method) {
var func = winstonLogger[method];
logger[method] = (function (context, tag, f) {
return function () {
if (arguments.length > 0) {
var timenow = new Date().toISOString();
var prefix = '[' + timenow + ' ' + tag + ']: ';
arguments[0] = prefix + arguments[0];
}
f.apply(context, arguments);
};
}(winstonLogger, opts.prefix, func));
});
return logger;
}
module.exports.PTELogger = PTELogger;
function getTLSCert(key, subkey, cpf, cpPath) {
var data;
logger.debug('[getTLSCert] key: %s, subkey: %s', key, subkey);
if (getConnProfilePropCnt(cpf, 'peers') === 0) {
logger.error('[getTLSCert] no peers is found in the connection profile');
process.exit(1);
}
var cpList = [];
var orderersCPFList = {};
cpList = getConnProfileList(cpPath);
orderersCPFList = getNodetypeFromConnProfiles(cpList, 'orderers');
if (Object.getOwnPropertyNames(orderersCPFList).length === 0) {
logger.error('[org:id=%s:%d getTLSCert] no orderer found', org, pid);
process.exit(1);
}
var cpOrgs = cpf['organizations'];
var cpPeers = cpf['peers'];
var currentDirectory = __dirname
var homeDirectory = currentDirectory.split("src/github.com")[0]
var cpPtr;
if (cpPeers.hasOwnProperty(subkey)) {
cpPtr = cpPeers;
} else if (orderersCPFList.hasOwnProperty(subkey)) {
cpPtr = orderersCPFList;
} else {
logger.error('[getTLSCert] key: not found');
return;
}
logger.debug('[getTLSCert] key found: %j', cpPtr[subkey]);
if (typeof (cpf.tls_cert) !== 'undefined') {
data = cpf.tls_cert;
} else {
if (typeof (cpPtr[subkey].tlsCACerts.pem) != 'undefined') {
//tlscerts is a cert
data = cpPtr[subkey].tlsCACerts['pem'];
} else if (typeof (cpPtr[subkey].tlsCACerts.path) != 'undefined') {
//tlscerts is a path
var caRootsPath = path.join(homeDirectory, cpPtr[subkey].tlsCACerts['path']);
if (fs.existsSync(caRootsPath)) {
//caRootsPath is a cert path
data = fs.readFileSync(caRootsPath);
} else {
logger.error('[getTLSCert] tls_cacerts does not exist: caRootsPath: %s, key: %s, subkey: %s', caRootsPath, key, subkey);
return null;
}
} else {
logger.error('[getTLSCert] tls_cacerts does not exist: key: %s, subkey: %s', key, subkey);
return null;
}
}
return data;
}
module.exports.getTLSCert = getTLSCert;
module.exports.tlsEnroll = async function (client, orgName, cpf) {
try {
var cpOrgs = cpf['organizations'];
if (getConnProfilePropCnt(cpf, 'certificateAuthorities') === 0) {
logger.error('[tlsEnroll] no certificateAuthority is found in the connection profile');
process.exit(1);
}
var cpCAs = cpf['certificateAuthorities'];
logger.debug('[tlsEnroll] CA tls enroll: %s, cpf: %s', orgName, cpf);
return new Promise(function (resolve, reject) {
if (!cpOrgs[orgName]) {
throw new Error('Invalid org name: ' + orgName);
}
var orgCA = cpOrgs[orgName].certificateAuthorities[0];
let fabricCAEndpoint = cpCAs[orgCA].url;
let tlsOptions = {
trustedRoots: [],
verify: false
};
let caService = new FabricCAServices(fabricCAEndpoint, tlsOptions, cpCAs[orgCA].caName);
logger.debug('[tlsEnroll] CA tls enroll ca name: %j', cpCAs[orgCA].caName);
let req = {
enrollmentID: 'admin',
enrollmentSecret: 'adminpw',
profile: 'tls'
};
caService.enroll(req).then(
function (enrollment) {
const key = enrollment.key.toBytes();
const cert = enrollment.certificate;
client.setTlsClientCertAndKey(cert, key);
logger.info('[tlsEnroll] CA tls enroll succeeded');
return resolve(enrollment);
},
function (err) {
logger.error('[tlsEnroll] CA tls enroll failed: %j', err);
return reject(err);
}
);
});
} catch (err) {
logger.error(err)
process.exit(1)
}
}
var TLSDISABLED = 0;
var TLSSERVERAUTH = 1;
var TLSCLIENTAUTH = 2;
module.exports.TLSDISABLED = TLSDISABLED;
module.exports.TLSSERVERAUTH = TLSSERVERAUTH;
module.exports.TLSCLIENTAUTH = TLSCLIENTAUTH;
module.exports.setTLS = function (txCfgPtr) {
var TLSin = txCfgPtr.TLS.toUpperCase();
var TLS = TLSDISABLED; // default
if ((TLSin == 'SERVERAUTH') || (TLSin == 'ENABLED')) {
TLS = TLSSERVERAUTH;
} else if (TLSin == 'CLIENTAUTH') {
TLS = TLSCLIENTAUTH;
}
logger.info('[setTLS] TLSin: %s, TLS: %d', TLSin, TLS);
return TLS;
}
// get ordererID for transactions
module.exports.getOrdererID = function (pid, orgName, org, txCfgPtr, cpf, method, cpPath) {
var cpOrgs = cpf['organizations'];
var ordererID;
var cpList = [];
var orderersCPFList = {};
cpList = getConnProfileList(cpPath);
orderersCPFList = getNodetypeFromConnProfiles(cpList, 'orderers');
if (Object.getOwnPropertyNames(orderersCPFList).length === 0) {
logger.error('[org:id=%s:%d getOrdererID] no orderer found', org, pid);
process.exit(1);
}
logger.info('[org:id=%s:%d getOrdererID] orderer method: %s', org, pid, method);
// find ordererID
if (method == 'ROUNDROBIN') {
// Round Robin
var orgNameLen = orgName.length;
var orgIdx = orgName.indexOf(org);
var SCordList = Object.keys(orderersCPFList);
logger.debug('[org:id=%s:%d getOrdererID] SC orderer list: %j', org, pid, SCordList);
var ordLen = SCordList.length;
var nOrderers = 0;
if (txCfgPtr.ordererOpt && txCfgPtr.ordererOpt.nOrderers) {
nOrderers = parseInt(txCfgPtr.ordererOpt.nOrderers);
}
if (nOrderers == 0) {
nOrderers = ordLen;
} else if (ordLen < nOrderers) {
nOrderers = ordLen;
}
logger.debug('[org:id=%s:%d getOrdererID] orderer orgNameLen %d, ordLen %d, nOrderers %d', org, pid, orgNameLen, ordLen, nOrderers);
var ordIdx = (pid * orgNameLen + orgIdx) % nOrderers;
ordererID = SCordList[ordIdx];
} else {
// default method: USERDEFINED
if (typeof cpOrgs[org].ordererID !== 'undefined') {
ordererID = cpOrgs[org].ordererID;
} else {
ordererID = Object.getOwnPropertyNames(orderersCPFList)[0];
}
}
logger.info('[org:id=%s:%d getOrdererID] orderer assigned to the test: %s', org, pid, ordererID);
return ordererID;
}
// get peerID for transactions
module.exports.getPeerID = function (pid, org, txCfgPtr, cpf, method) {
var cpOrgs = cpf['organizations'];
if (getConnProfilePropCnt(cpf, 'peers') === 0) {
logger.error('[getPeerID] no peer is found in the connection profile');
process.exit(1);
}
var cpPeers = cpf['peers'];