Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Undo all of the CLI changes for network admins (#2825)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stone <[email protected]>
  • Loading branch information
Simon Stone authored Nov 23, 2017
1 parent 974bf75 commit 1f49a61
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 374 deletions.
2 changes: 1 addition & 1 deletion packages/composer-admin/lib/adminconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ class AdminConnection {
* });
* @param {BusinessNetworkDefinition} businessNetworkDefinition - The business network to start
* @param {Object} [startOptions] connector specific start options
* NetworkAdmins: [ { name, certificate } , { name, enrollmentSecret }]
* networkAdmins: [ { userName, certificate } , { userName, enrollmentSecret }]
*
* @return {Promise} A promise that will be fufilled when the business network has been
* deployed - with a MAP of cards key is name
Expand Down
3 changes: 0 additions & 3 deletions packages/composer-cli/lib/cmds/archive/createCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ module.exports.builder = function (yargs){
// enforce singletons
yargs.check(checkFn);

// grouping for the card options - moves them away from the standard --version etc.
yargs.group(['archiveFile','sourceType','sourceName'],'Archive options');

return yargs;
};

Expand Down
3 changes: 0 additions & 3 deletions packages/composer-cli/lib/cmds/card/createCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ module.exports.builder = function (yargs) {
// enforce the option after these options
yargs.requiresArg(['file','businessNetworkName','connectionProfileFile','user','enrollSecret','certificate','privateKey','roles']);

// grouping for the card options - moves them away from the standard --version etc.
yargs.group(['f','n','p','u','s','c','k','r'],'Card options');

yargs.implies('certificate','privateKey');
yargs.implies('privateKey','certificate');

Expand Down
1 change: 0 additions & 1 deletion packages/composer-cli/lib/cmds/card/deleteCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module.exports.builder = (yargs) => {
name: { alias: 'n', required: true, describe: 'The name of the card to delete', type: 'string' }
});

yargs.group(['n'],'Card options');
yargs.requiresArg(['n']);

return yargs;
Expand Down
3 changes: 0 additions & 3 deletions packages/composer-cli/lib/cmds/identity/issueCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ module.exports.builder =function (yargs) {
'file': {alias: 'f', required: false, describe: 'The card file name for the new identity', type: 'string' }
});

yargs.group(['c','f'],'Business Network Cards');
yargs.group(['u','a','x'],'Identity Options');

return yargs;
};

Expand Down
5 changes: 1 addition & 4 deletions packages/composer-cli/lib/cmds/network/deployCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports.builder = function (yargs) {
optionsFile: { alias: 'O', required: false, describe: 'A file containing options that are specific to connection', type: 'string' },
networkAdmin: { alias: 'A', required: true, description: 'The identity name of the business network administrator', type: 'string' },
networkAdminCertificateFile: { alias: 'C', required: false, description: 'The certificate of the business network administrator', type: 'string' },
networkAdminSecret: { alias: 'S', required: false, description: 'The enrollment secret for the business network administrator', type: 'string' },
networkAdminEnrollSecret: { alias: 'S', required: false, description: 'The enrollment secret for the business network administrator', type: 'string' },
card: { alias: 'c', required: true, description: 'The cardname to use to deploy the network', type:'string'},
file: { alias: 'f', required: false, description: 'File name of the card to be created', type: 'string'}
});
Expand All @@ -37,9 +37,6 @@ module.exports.builder = function (yargs) {
// Mark the options that conflict
yargs.conflicts('C','S');

// group the options
yargs.group(['loglevel','file','archiveFile','networkAdmin','networkAdminCertificateFile','networkAdminEnrollSecret','card'],'Deploy Options');

return yargs;
};

Expand Down
64 changes: 1 addition & 63 deletions packages/composer-cli/lib/cmds/network/lib/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Start {
// grab the network admins
// what we want is an array of the following
// {userName, certificate, secret, file}
startOptions.networkAdmins = networkAdmins = Start.createNetworkAdmins(argv);
startOptions.networkAdmins = networkAdmins = cmdUtil.parseNetworkAdmins(argv);
cmdUtil.log(chalk.bold.blue('Processing these Network Admins: '));
startOptions.networkAdmins.forEach((e)=>{
cmdUtil.log(chalk.blue('\tuserName: ')+e.userName);
Expand Down Expand Up @@ -150,67 +150,5 @@ class Start {
}
return archiveFileContents;
}

/** Parse the argv structure to get an array of Network Admins
* @param {array} argv standard YARGS structure
* @return {array} simple objects with details of the network admins to create
*/
static createNetworkAdmins(argv){
let networkAdmins;
if (typeof argv.networkAdmin.id === 'object'){

networkAdmins = Object.keys(argv.networkAdmin.id).map((index)=>{
let admin={};
admin.userName = argv.networkAdmin.id[index];
if (argv.networkAdmin.cert && argv.networkAdmin.cert[index]){
admin.certificate = argv.networkAdmin.cert[index];
}

if (!admin.certificate && argv.networkAdmin.secret) {
admin.secret = argv.networkAdmin.secret[index];
} else {
throw new Error('Need to have certificate or secret');
}

if (argv.networkAdmin.file && argv.networkAdmin.file[index]){
admin.file = argv.networkAdmin.file[index];
}
return admin;
});

} else if (typeof argv.networkAdmin === 'object') {
let admin={};
admin.userName = argv.networkAdmin.id;
if (argv.networkAdmin.certificate){
admin.certificate = argv.networkAdmin.certificate;
} else if (argv.networkAdmin.secret){
admin.enrollmentSecret = argv.networkAdmin.secret;
} else {
throw new Error('Need to have certificate or secret');
}

if (argv.networkAdmin.file){
admin.file = argv.networkAdmin.file;
}
networkAdmins = [ admin ];
} else {
// old school
let admin={};
admin.userName = argv.networkAdmin;
if (argv.networkAdminCertificateFile){
admin.certificate = argv.networkAdminCertificateFile;
} else if (argv.networkAdminSecret){
admin.enrollmentSecret = argv.networkAdminSecret;
} else {
throw new Error('Need to have certificate or secret');
}

if (argv.file){
admin.file = argv.file;
}
networkAdmins = [ admin ];
}
return networkAdmins;
}
}
module.exports = Start;
2 changes: 1 addition & 1 deletion packages/composer-cli/lib/cmds/network/startCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports.builder = function (yargs) {
optionsFile: { alias: 'O', required: false, describe: 'A file containing options that are specific to connection', type: 'string' },
networkAdmin: { alias: 'A', required: true, description: 'The identity name of the business network administrator', type: 'string' },
networkAdminCertificateFile: { alias: 'C', required: false, description: 'The certificate of the business network administrator', type: 'string' },
networkAdminSecret: { alias: 'S', required: false, description: 'The enrollment secret for the business network administrator', type: 'string', default: undefined },
networkAdminEnrollSecret: { alias: 'S', required: false, description: 'The enrollment secret for the business network administrator', type: 'string', default: undefined },
card: { alias: 'c', required: true, description: 'The cardname to use to start the network', type:'string'},
file: { alias: 'f', required: false, description: 'File name of the card to be created', type: 'string'}
});
Expand Down
2 changes: 0 additions & 2 deletions packages/composer-cli/lib/cmds/participant/addCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ module.exports.builder = (yargs) =>{

yargs.check(checkFn);

yargs.group(['card','data'],'Participant options');

return yargs;
};

Expand Down
97 changes: 21 additions & 76 deletions packages/composer-cli/lib/cmds/utils/cmdutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ const AdminConnection = require('composer-admin').AdminConnection;
const BusinessNetworkCardStore = require('composer-common').BusinessNetworkCardStore;
const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;
const fs = require('fs');
const Logger = require('composer-common').Logger;
const prompt = require('prompt');

const LOG = Logger.getLog('CmdUtil');

/**
* Internal Utility Class
* <p><a href="diagrams/util.svg"><img src="diagrams/util.svg" style="width:100%;"/></a></p>
Expand Down Expand Up @@ -114,7 +111,7 @@ class CmdUtil {
const certificateFile = networkAdminCertificateFiles[index];
const certificate = fs.readFileSync(certificateFile, { encoding: 'utf8' });
return {
name: networkAdmin,
userName: networkAdmin,
certificate
};

Expand All @@ -134,10 +131,10 @@ class CmdUtil {
return networkAdmins.map((networkAdmin, index) => {

// Grab the secret for the network admin.
const secret = networkAdminEnrollSecrets[index];
const enrollmentSecret = networkAdminEnrollSecrets[index];
return {
name: networkAdmin,
secret
userName: networkAdmin,
enrollmentSecret
};

});
Expand All @@ -155,6 +152,7 @@ class CmdUtil {
const networkAdmins = CmdUtil.arrayify(argv.networkAdmin);
const networkAdminCertificateFiles = CmdUtil.arrayify(argv.networkAdminCertificateFile);
const networkAdminEnrollSecrets = CmdUtil.arrayify(argv.networkAdminEnrollSecret);
const files = CmdUtil.arrayify(argv.file);

// It's valid not to specify any network administrators.
if (networkAdmins.length === 0) {
Expand All @@ -167,85 +165,32 @@ class CmdUtil {
}

// Check that enough certificate files have been specified.
let result;
if (networkAdmins.length === networkAdminCertificateFiles.length) {
return CmdUtil.parseNetworkAdminsWithCertificateFiles(networkAdmins, networkAdminCertificateFiles);
result = CmdUtil.parseNetworkAdminsWithCertificateFiles(networkAdmins, networkAdminCertificateFiles);
}

// Check that enough enrollment secrets have been specified.
if (networkAdmins.length === networkAdminEnrollSecrets.length) {
return CmdUtil.parseNetworkAdminsWithEnrollSecrets(networkAdmins, networkAdminEnrollSecrets);
else if (networkAdmins.length === networkAdminEnrollSecrets.length) {
result = CmdUtil.parseNetworkAdminsWithEnrollSecrets(networkAdmins, networkAdminEnrollSecrets);
}

// Not enough certificate files or enrollment secrets!
throw new Error('You must specify certificate files or enrollment secrets for all network administrators');

}
else {
console.log(JSON.stringify(argv, null, 4));
throw new Error('You must specify certificate files or enrollment secrets for all network administrators');
}

/**
* Build the bootstrap transactions for any business network administrators specified on the command line.
* @param {BusinessNetworkDefinition} businessNetworkDefinitinon The business network definition.
* @param {Object} argv The command line arguments as parsed by yargs.
* @return {Object[]} The bootstrap transactions.
*/
static buildBootstrapTransactions(businessNetworkDefinitinon, argv) {
const method = 'buildBootstrapTransactions';
LOG.entry(method, businessNetworkDefinitinon, argv);

// Grab the useful things from the business network definition.
const factory = businessNetworkDefinitinon.getFactory();
const serializer = businessNetworkDefinitinon.getSerializer();

// Parse the network administrators.
const networkAdmins = CmdUtil.parseNetworkAdmins(argv);

// Convert the network administrators into add participant transactions.
const addParticipantTransactions = networkAdmins.map((networkAdmin) => {
const participant = factory.newResource('org.hyperledger.composer.system', 'NetworkAdmin', networkAdmin.name);
const targetRegistry = factory.newRelationship('org.hyperledger.composer.system', 'ParticipantRegistry', participant.getFullyQualifiedType());
const addParticipantTransaction = factory.newTransaction('org.hyperledger.composer.system', 'AddParticipant');
Object.assign(addParticipantTransaction, {
resources: [ participant ],
targetRegistry
// If any files specified, check we have enough, and merge them into the result.
if (files.length && files.length !== result.length) {
throw new Error('If you specify a network administrators card file name, you must specify one for all network administrators');
} else if (files.length) {
files.forEach((file, index) => {
result[index].file = file;
});
LOG.debug(method, 'Created bootstrap transaction to add participant', addParticipantTransaction);
return addParticipantTransaction;
});

// Convert the network administrators into issue or bind identity transactions.
const identityTransactions = networkAdmins.map((networkAdmin) => {

// Handle a certificate which requires a bind identity transaction.
let identityTransaction;
if (networkAdmin.certificate) {
identityTransaction = factory.newTransaction('org.hyperledger.composer.system', 'BindIdentity');
Object.assign(identityTransaction, {
participant: factory.newRelationship('org.hyperledger.composer.system', 'NetworkAdmin', networkAdmin.name),
certificate: networkAdmin.certificate
});
LOG.debug(method, 'Created bootstrap transaction to bind identity', identityTransaction);
}

// Handle an enrollment secret which requires an issue identity transactiom.
if (networkAdmin.secret) {
identityTransaction = factory.newTransaction('org.hyperledger.composer.system', 'IssueIdentity');
Object.assign(identityTransaction, {
participant: factory.newRelationship('org.hyperledger.composer.system', 'NetworkAdmin', networkAdmin.name),
identityName: networkAdmin.name
});
LOG.debug(method, 'Created bootstrap transaction to issue identity', identityTransaction);
}
return identityTransaction;

});

// Serialize all of the transactions into a single array.
const transactions = addParticipantTransactions.concat(identityTransactions);
const json = transactions.map((transaction) => {
return serializer.toJSON(transaction);
});
}
return result;

LOG.exit(method, json);
return json;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/composer-cli/test/archive/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('composer archive cmd launcher unit tests', function () {
sandbox = sinon.sandbox.create();
sandbox.stub(yargs, 'check').returns(yargs);
sandbox.stub(yargs, 'conflicts').returns(yargs);
sandbox.stub(yargs, 'group').returns(yargs);
sandbox.stub(yargs, 'options').returns(yargs);
sandbox.stub(yargs, 'usage').returns(yargs);
sandbox.stub(yargs, 'requiresArg').returns(yargs);
Expand Down Expand Up @@ -74,7 +73,6 @@ describe('composer archive cmd launcher unit tests', function () {
sinon.assert.calledOnce(yargs.options);
sinon.assert.calledOnce(yargs.requiresArg);
sinon.assert.calledOnce(yargs.check);
sinon.assert.calledOnce(yargs.group);
});


Expand Down
3 changes: 0 additions & 3 deletions packages/composer-cli/test/card/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('composer participant cmd launcher unit tests', function () {

sandbox.stub(yargs, 'check').returns(yargs);
sandbox.stub(yargs, 'conflicts').returns(yargs);
sandbox.stub(yargs, 'group').returns(yargs);
sandbox.stub(yargs, 'options').returns(yargs);
sandbox.stub(yargs, 'requiresArg').returns(yargs);
sandbox.stub(yargs, 'demandCommand').returns(yargs);
Expand Down Expand Up @@ -64,14 +63,12 @@ describe('composer participant cmd launcher unit tests', function () {
it('should drive the yargs builder fn correctly',()=>{
cardCommand.builder(yargs);
sinon.assert.calledOnce(yargs.options);
sinon.assert.calledOnce(yargs.group);
sinon.assert.calledOnce(yargs.check);
});

it('should drive the yargs builder fn correctly',()=>{
deleteCommand.builder(yargs);
sinon.assert.calledOnce(yargs.options);
sinon.assert.calledOnce(yargs.group);
sinon.assert.calledOnce(yargs.requiresArg);
});
});
Expand Down
2 changes: 0 additions & 2 deletions packages/composer-cli/test/identity/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('composer identity cmd launcher unit tests', function () {
sandbox = sinon.sandbox.create();
sandbox.stub(yargs, 'usage').returns(yargs);
sandbox.stub(yargs, 'conflicts').returns(yargs);
sandbox.stub(yargs, 'group').returns(yargs);
sandbox.stub(yargs, 'options').returns(yargs);
sandbox.stub(yargs, 'requiresArg').returns(yargs);
sandbox.stub(yargs, 'demandCommand').returns(yargs);
Expand Down Expand Up @@ -63,7 +62,6 @@ describe('composer identity cmd launcher unit tests', function () {
it('should drive the yargs builder fn correctly',()=>{
issueCommand.builder(yargs);
sinon.assert.calledOnce(yargs.options);
sinon.assert.calledTwice(yargs.group);
});

});
Expand Down
Loading

0 comments on commit 1f49a61

Please sign in to comment.